package olinq
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=586bfad3e744f8c459fa9f17db594c64
sha512=564efbea08d6e67ffa6ac6ba46c4737632ab206656f376a4b4b145984da7adcf14a731133c10201282105dae4345d924c423415830dcdaba78c98a0ffdda4c37
doc/olinq/OLinq/index.html
Module OLinqSource
LINQ-like operations on collections
The purpose is to provide powerful combinators to express iteration, transformation and combination of collections of items.
Functions and operations are assumed to be referentially transparent, i.e. they should not rely on external side effects, they should not rely on the order of execution.
OLinq.(
of_list [1;2;3]
|> flat_map (fun x -> (x -- (x+10)))
|> count ()
|> sort ()
|> run_list
);;
- : (int * int) list = [(1, 1); (2, 2); (3, 3); (4, 3); (5, 3); (6, 3);
(7, 3); (8, 3); (9, 3); (10, 3); (11, 3); (12, 2); (13, 1)]
OLinq.(
IO.read_file "/tmp/foo"
|> IO.lines
|> sort ()
|> IO.to_file_lines "/tmp/bar"
);;
- : `Ok () OLinq.(
1 -- 20
|> group_by (fun x -> x mod 3)
|> run_list
) ;;
- : (int * int list) list =
[(2, [20; 17; 14; 11; 8; 5; 2]);
(0, [18; 15; 12; 9; 6; 3; 0]);
(1, [19; 16; 13; 10; 7; 4; 1])]Polymorphic Maps
Main Type
Type of a query that returns zero, one or more values of type 'a. The parameter 'card indicates how many elements are in the collection, with `Any indicating the number is unknown, `AtMostOne that there are 0 or 1 elements and `One exactly one.
To simplify, this is very similar to a type 'a t that would behave like a collection of 'a. The ghost parameter 'card is only useful to restrict the kind of operations one can perform on these collections. For example , a value of type ('a, [`One]) t contains exactly one element so we can access it safely.
Conceptually, the cardinalities are ordered from most precise (`One) to least precise (`Any): `One < `AtMostOne < `Any.
Initial values
Query that returns the elements of the given sequence.
of_multimap m yields each single binding of m
Execution
Execute the query, possibly returning an error if things go wrong
Basics
Filter out values that do not satisfy predicate. We lose precision on the cardinality because of type system constraints.
size t returns one value, the number of items returned by t
Choose one element (if any, otherwise empty) in the collection. This is like a "cut" in prolog.
Filter and map elements at once
Same as flat_map but using seq
Same as flat_map but using iterators
map each element to a collection and flatten the result
Specialized version of take that keeps only the first element
Take elements while they satisfy a predicate
Sort items by the given comparison function. Only meaningful when there are potentially many elements
sort_by proj c sorts the collection c by projecting elements using proj, then using cmp to order them
Remove duplicate elements from the input collection. All elements in the result are distinct.
Aggregation
val group_by :
?cmp:'b ord ->
?eq:'b equal ->
?hash:'b hash ->
('a -> 'b) ->
('a, [ `Any ]) t ->
('b * 'a list, [ `Any ]) tgroup_by f takes a collection c as input, and returns a collection of pairs k, l where every element x of l satifies f x = k. In other words, elements of the collection that have the same image by f are grouped in the same list.
val group_by_reflect :
?cmp:'b ord ->
?eq:'b equal ->
?hash:'b hash ->
('a -> 'b) ->
('a, [ `Any ]) t ->
(('b, 'a list) map, [> `One ]) tgroup_by_reflect f takes a collection c as input, and returns a multimap m such that for each x in c, x occurs in m under the key f x. In other words, f is used to obtain a key from x, and x is added to the multimap using this key.
val count :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
unit ->
('a, [ `Any ]) t ->
('a * int, [ `Any ]) tcount c counts how many times each element of the collection occur, and returns pairs of x, count(x)
val count_reflect :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
unit ->
('a, [ `Any ]) t ->
(('a, int) map, [> `One ]) tcount_reflect c returns a map from elements of c to the number of time those elements occur.
contains x q returns true if x is among the elements returned by q. Careful, this runs q and might be slow!
Binary Operators
val join :
?cmp:'key ord ->
?eq:'key equal ->
?hash:'key hash ->
('a -> 'key) ->
('b -> 'key) ->
merge:('key -> 'a -> 'b -> 'c option) ->
('a, _) t ->
('b, _) t ->
('c, [ `Any ]) tjoin key1 key2 ~merge is a binary operation that takes two collections a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded.
val outer_join :
?cmp:'key ord ->
?eq:'key equal ->
?hash:'key hash ->
('a -> 'key) ->
('b -> 'key) ->
merge:('key -> 'a list -> 'b list -> 'c option) ->
('a, _) t ->
('b, _) t ->
('c, [ `Any ]) touter_join key1 key2 ~merge is a binary operation that takes two collections a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
- compute the list
l1of elements ofathat map tok - compute the list
l2of elements ofbthat map tok - call
merge k l1 l2. IfmergereturnsNone, the combination of values is discarded, otherwise it returnsSome candcis inserted in the result.
val group_join :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('b -> 'a) ->
('a, _) t ->
('b, _) t ->
('a * 'b list, [ `Any ]) tgroup_join key2 associates to every element x of the first collection, all the elements y of the second collection such that eq x (key y). Elements of the first collections without corresponding values in the second one are mapped to []
val group_join_reflect :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('b -> 'a) ->
('a, _) t ->
('b, _) t ->
(('a, 'b list) map, [> `One ]) tSame as group_join, but reflects the groups as a multimap
val inter :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('a, _) t ->
('a, _) t ->
('a, [ `Any ]) tIntersection of two collections. Each element will occur at most once in the result
val union :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('a, _) t ->
('a, _) t ->
('a, [ `Any ]) tUnion of two collections. Each element will occur at most once in the result
val diff :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('a, _) t ->
('a, _) t ->
('a, [ `Any ]) tSet difference
val subset :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('a, _) t ->
('a, _) t ->
(bool, [ `One ]) tsubset () a b returns true if all elements of a belong to b
Tuple and Options
Specialized projection operators
Flatten the collection by removing None and mapping Some x to x.
Applicative
Apply each function to each value. The cardinality should be the lowest upper bound of both input cardinalities (any,_) -> any, (one,one) -> one, etc.
Monad
Careful, those operators do not allow any optimization before running the query, they might therefore be pretty slow.
Use the result of a query to build another query and immediately run it.
Misc
Infix
Adapters
reflect_seq q evaluates all values in q and returns a sequence of all those values. Also blocks optimizations
reflect_list q evaluates all values in q and returns a list of all those values. Also blocks optimizations
Build a hashtable from the collection