Library
Module
Module type
Parameter
Class
Class type
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])]
module Iterable : sig ... end
type ('a, 'b) map = ('a, 'b) OLinq_map.t
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
.
type 'a t_any = ('a, [ `Any ]) t
type 'a t_one = ('a, [ `One ]) t
type 'a t_at_most_one = ('a, [ `AtMostOne ]) t
val empty : ('a, [> `AtMostOne ]) t
Empty collection
val return : 'a -> ('a, [> `One ]) t
Return one value
val of_list : 'a list -> ('a, [ `Any ]) t
Query that just returns the elements of the list
val of_array : 'a array -> ('a, [ `Any ]) t
val of_array_i : 'a array -> (int * 'a, [ `Any ]) t
val range : int -> int -> (int, [ `Any ]) t
range i j
goes from i
up to j
included
val of_hashtbl : ('a, 'b) Stdlib.Hashtbl.t -> ('a * 'b, [ `Any ]) t
val of_vec : 'a OLinq_vec.t -> ('a, [ `Any ]) t
val of_queue : 'a Stdlib.Queue.t -> ('a, [ `Any ]) t
val of_stack : 'a Stdlib.Stack.t -> ('a, [ `Any ]) t
val of_string : string -> (char, [ `Any ]) t
Traverse the characters of the string
of_multimap m
yields each single binding of m
val run : ?limit:int -> ('a, _) t -> 'a Iterable.t
Execute the query, possibly returning an error if things go wrong
val run_list : ?limit:int -> ('a, _) t -> 'a list
val run_array : ?limit:int -> ('a, _) t -> 'a array
val run_vec : ?limit:int -> ('a, _) t -> 'a OLinq_vec.t
val run1 : ('a, [ `One ]) t -> 'a
Run the query and return the only value
val run_head : ('a, _) t -> 'a option
Return first result
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.
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.
val group_by :
?cmp:'b ord ->
?eq:'b equal ->
?hash:'b hash ->
('a -> 'b) ->
('a, [ `Any ]) t ->
('b * 'a list, [ `Any ]) t
group_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 ]) t
group_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 ]) t
count 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 ]) t
count_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!
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 ]) t
join 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 ]) t
outer_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:
l1
of elements of a
that map to k
l2
of elements of b
that map to k
merge k l1 l2
. If merge
returns None
, the combination of values is discarded, otherwise it returns Some c
and c
is 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 ]) t
group_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 ]) t
Same 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 ]) t
Intersection 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 ]) t
Union 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 ]) t
Set difference
val subset :
?cmp:'a ord ->
?eq:'a equal ->
?hash:'a hash ->
('a, _) t ->
('a, _) t ->
(bool, [ `One ]) t
subset () a b
returns true
if all elements of a
belong to b
Specialized projection operators
Flatten the collection by removing None
and mapping Some x
to x
.
Apply each function to each value. The cardinality should be the lowest upper bound of both input cardinalities (any,_) -> any, (one,one) -> one, etc.
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.
module Infix : sig ... end
val reflect_vec : ('a, _) t -> ('a OLinq_vec.t, [> `One ]) t
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
module IO : sig ... end