View Source Funx.List (funx v0.1.0)
The Funx.List
module provides utility functions for working with lists while respecting Eq
and Ord
instances. This allows for set-like operations, uniqueness constraints, and sorted collections that align with functional programming principles.
Features
- Equality-based Operations: Use
Eq
instances to compare elements for uniqueness, intersection, and difference. - Ordering Functions: Leverage
Ord
instances to sort and enforce uniqueness in sorted collections. - Set Operations: Perform union, intersection, difference, and symmetric difference while preserving custom equality logic.
- Subset & Superset Checks: Verify relationships between lists in terms of inclusion.
- Functional Constructs: Implements
Monad
andFoldable
protocols for lists, supporting mapping, binding, and folding.
Usage Overview
- Deduplicate: Use
uniq/1
to remove duplicates based onEq
. - Combine: Use
union/2
to merge lists without duplicates. - Filter: Use
intersection/2
ordifference/2
for set operations. - Sort: Use
sort/2
orstrict_sort/2
withOrd
instances. - Check Membership: Use
subset?/2
orsuperset?/2
to verify inclusion relationships.
Equality-Based Operations
uniq/1
: Removes duplicates usingEq
.union/2
: Merges lists while preserving uniqueness.intersection/2
: Returns elements common to both lists.difference/2
: Returns elements from the first list not in the second.symmetric_difference/2
: Returns elements unique to each list.
Ordering Functions
sort/2
: Sorts a list usingOrd
.strict_sort/2
: Sorts while ensuring uniqueness.
Set Operations
subset?/2
: Checks if one list is a subset of another.superset?/2
: Checks if one list is a superset of another.
Monad Implementation
map/2
: Transforms list elements.bind/2
: Applies a function returning lists and flattens the result.ap/2
: Applies functions in a list to elements in another list.
Foldable Implementation
fold_l/3
: Performs left-associative folding.fold_r/3
: Performs right-associative folding.
Summary
Functions
Concatenates a list of lists from left to right.
Returns the difference of two lists.
Returns the intersection of two lists.
Sorts a list using the given ordering module.
Sorts a list while ensuring uniqueness.
Checks if the first list is a subset of the second.
Checks if the first list is a superset of the second.
Returns the symmetric difference of two lists.
Returns the union of two lists, removing duplicates.
Removes duplicate elements from a list based on the given equality module.
Functions
Concatenates a list of lists from left to right.
This uses the ListConcat
monoid, preserving the original order of elements.
Examples
iex> Funx.List.concat([[1], [2, 3], [4]])
[1, 2, 3, 4]
@spec difference([term()], [term()], Funx.Eq.Utils.eq_t()) :: [term()]
Returns the difference of two lists.
Examples
iex> Funx.List.difference([1, 2, 3, 4], [3, 4, 5])
[1, 2]
@spec intersection([term()], [term()], Funx.Eq.Utils.eq_t()) :: [term()]
Returns the intersection of two lists.
Examples
iex> Funx.List.intersection([1, 2, 3, 4], [3, 4, 5])
[3, 4]
@spec sort([term()], Funx.Ord.Utils.ord_t()) :: [term()]
Sorts a list using the given ordering module.
Examples
iex> Funx.List.sort([3, 1, 4, 1, 5])
[1, 1, 3, 4, 5]
@spec strict_sort([term()], Funx.Ord.Utils.ord_t()) :: [term()]
Sorts a list while ensuring uniqueness.
Examples
iex> Funx.List.strict_sort([3, 1, 4, 1, 5])
[1, 3, 4, 5]
@spec subset?([term()], [term()], Funx.Eq.Utils.eq_t()) :: boolean()
Checks if the first list is a subset of the second.
Examples
iex> Funx.List.subset?([1, 2], [1, 2, 3, 4])
true
iex> Funx.List.subset?([1, 5], [1, 2, 3, 4])
false
@spec superset?([term()], [term()], Funx.Eq.Utils.eq_t()) :: boolean()
Checks if the first list is a superset of the second.
Examples
iex> Funx.List.superset?([1, 2, 3, 4], [1, 2])
true
iex> Funx.List.superset?([1, 2, 3, 4], [1, 5])
false
@spec symmetric_difference([term()], [term()], Funx.Eq.Utils.eq_t()) :: [term()]
Returns the symmetric difference of two lists.
Examples
iex> Funx.List.symmetric_difference([1, 2, 3], [3, 4, 5])
[1, 2, 4, 5]
@spec union([term()], [term()], Funx.Eq.Utils.eq_t()) :: [term()]
Returns the union of two lists, removing duplicates.
Examples
iex> Funx.List.union([1, 2, 3], [3, 4, 5])
[1, 2, 3, 4, 5]
@spec uniq([term()], Funx.Eq.Utils.eq_t()) :: [term()]
Removes duplicate elements from a list based on the given equality module.
Examples
iex> Funx.List.uniq([1, 2, 2, 3, 1, 4, 5])
[1, 2, 3, 4, 5]