package olinq

  1. Overview
  2. Docs
LINQ inspired queries on in-memory data

Install

Dune Dependency

Authors

Maintainers

Sources

v0.3.tar.gz
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])]
Sourcetype 'a iter = ('a -> unit) -> unit
Sourcetype 'a equal = 'a -> 'a -> bool
Sourcetype 'a ord = 'a -> 'a -> int
Sourcetype 'a hash = 'a -> int
Sourcetype 'a or_error = [
  1. | `Ok of 'a
  2. | `Error of string
]
Sourcetype 'a printer = Format.formatter -> 'a -> unit
Sourcemodule Iterable : sig ... end

Polymorphic Maps

Sourcetype ('a, 'b) map = ('a, 'b) OLinq_map.t

Main Type

Sourcetype ('a, +'card) t constraint 'card = [< `One | `AtMostOne | `Any ]

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.

Sourcetype 'a t_any = ('a, [ `Any ]) t
Sourcetype 'a t_one = ('a, [ `One ]) t
Sourcetype 'a t_at_most_one = ('a, [ `AtMostOne ]) t

Initial values

Sourceval empty : ('a, [> `AtMostOne ]) t

Empty collection

Sourceval return : 'a -> ('a, [> `One ]) t

Return one value

Sourceval of_list : 'a list -> ('a, [ `Any ]) t

Query that just returns the elements of the list

Sourceval of_array : 'a array -> ('a, [ `Any ]) t
Sourceval of_array_i : 'a array -> (int * 'a, [ `Any ]) t
Sourceval range : int -> int -> (int, [ `Any ]) t

range i j goes from i up to j included

Sourceval (--) : int -> int -> (int, [ `Any ]) t

Synonym to range

Sourceval of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b, [ `Any ]) t
Sourceval of_iter : 'a iter -> ('a, [ `Any ]) t

Query that returns the elements of the given sequence.

Sourceval of_vec : 'a OLinq_vec.t -> ('a, [ `Any ]) t
Sourceval of_queue : 'a Queue.t -> ('a, [ `Any ]) t
Sourceval of_stack : 'a Stack.t -> ('a, [ `Any ]) t
Sourceval of_string : string -> (char, [ `Any ]) t

Traverse the characters of the string

Sourceval of_map : ('a, 'b) map -> ('a * 'b, [ `Any ]) t

of_map m yields each binding of m

Sourceval of_multimap : ('a, 'b list) map -> ('a * 'b, [ `Any ]) t

of_multimap m yields each single binding of m

Execution
Sourceval run : ?limit:int -> ('a, _) t -> 'a Iterable.t

Execute the query, possibly returning an error if things go wrong

  • parameter limit

    max number of values to return

Sourceval run_list : ?limit:int -> ('a, _) t -> 'a list
Sourceval run_array : ?limit:int -> ('a, _) t -> 'a array
Sourceval run_vec : ?limit:int -> ('a, _) t -> 'a OLinq_vec.t
Sourceval run1 : ('a, [ `One ]) t -> 'a

Run the query and return the only value

Sourceval run_head : ('a, _) t -> 'a option

Return first result

Sourceval run1_exn : ('a, _) t -> 'a

unsafe shortcut for run_head.

  • raises Not_found

    if the query contains 0 element

Basics
Sourceval map : ('a -> 'b) -> ('a, 'card) t -> ('b, 'card) t

Map each value

Sourceval (>|=) : ('a, 'card) t -> ('a -> 'b) -> ('b, 'card) t

Infix synonym of map

Sourceval filter : ('a -> bool) -> ('a, _) t -> ('a, [ `Any ]) t

Filter out values that do not satisfy predicate. We lose precision on the cardinality because of type system constraints.

Sourceval size : (_, _) t -> (int, [> `One ]) t

size t returns one value, the number of items returned by t

Sourceval choose : ('a, _) t -> ('a, [> `AtMostOne ]) t

Choose one element (if any, otherwise empty) in the collection. This is like a "cut" in prolog.

Sourceval filter_map : ('a -> 'b option) -> ('a, _) t -> ('b, [ `Any ]) t

Filter and map elements at once

Sourceval flat_map_seq : ('a -> 'b Seq.t) -> ('a, _) t -> ('b, [ `Any ]) t

Same as flat_map but using seq

Sourceval flat_map_iter : ('a -> 'b iter) -> ('a, _) t -> ('b, [ `Any ]) t

Same as flat_map but using iterators

Sourceval flat_map_l : ('a -> 'b list) -> ('a, _) t -> ('b, [ `Any ]) t

map each element to a collection and flatten the result

Sourceval flatten_list : ('a list, _) t -> ('a, [ `Any ]) t
Sourceval flatten_seq : ('a Seq.t, _) t -> ('a, [ `Any ]) t
Sourceval flatten_iter : ('a iter, _) t -> ('a, [ `Any ]) t
Sourceval flatten_map : (('a, 'b) map, _) t -> ('a * 'b, [ `Any ]) t
Sourceval flatten_multimap : (('a, 'b list) map, _) t -> ('a * 'b, [ `Any ]) t
Sourceval take : int -> ('a, _) t -> ('a, [ `Any ]) t

Take at most n elements

Sourceval take1 : ('a, _) t -> ('a, [> `AtMostOne ]) t

Specialized version of take that keeps only the first element

Sourceval take_while : ('a -> bool) -> ('a, _) t -> ('a, [ `Any ]) t

Take elements while they satisfy a predicate

Sourceval sort : ?cmp:'a ord -> unit -> ('a, [ `Any ]) t -> ('a, [ `Any ]) t

Sort items by the given comparison function. Only meaningful when there are potentially many elements

Sourceval sort_by : ?cmp:'b ord -> ('a -> 'b) -> ('a, [ `Any ]) t -> ('a, [ `Any ]) t

sort_by proj c sorts the collection c by projecting elements using proj, then using cmp to order them

Sourceval distinct : ?cmp:'a ord -> unit -> ('a, [ `Any ]) t -> ('a, [ `Any ]) t

Remove duplicate elements from the input collection. All elements in the result are distinct.

Aggregation
Sourceval 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.

Sourceval 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.

Sourceval 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)

Sourceval 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.

Sourceval fold : ('b -> 'a -> 'b) -> 'b -> ('a, _) t -> ('b, [> `One ]) t

Fold over the collection

Sourceval is_empty : ('a, [< `AtMostOne | `Any ]) t -> (bool, [> `One ]) t
Sourceval sum : (int, [< `AtMostOne | `Any ]) t -> (int, [> `One ]) t
Sourceval contains : ?eq:'a equal -> 'a -> ('a, _) t -> (bool, [> `One ]) t

contains x q returns true if x is among the elements returned by q. Careful, this runs q and might be slow!

Sourceval average : (int, _) t -> (int, [> `One ]) t
Sourceval max : (int, _) t -> (int, [> `One ]) t
Sourceval min : (int, _) t -> (int, [> `One ]) t
Sourceval for_all : ('a -> bool) -> ('a, _) t -> (bool, [> `One ]) t
Sourceval exists : ('a -> bool) -> ('a, _) t -> (bool, [> `One ]) t
Sourceval find : ('a -> bool) -> ('a, _) t -> ('a option, [> `One ]) t
Sourceval find_map : ('a -> 'b option) -> ('a, _) t -> ('b option, [> `One ]) t
Binary Operators
Sourceval 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.

Sourceval 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:

  • compute the list l1 of elements of a that map to k
  • compute the list l2 of elements of b that map to k
  • call 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.
  • since 0.2
Sourceval 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 []

Sourceval 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

Sourceval product : ('a, _) t -> ('b, _) t -> ('a * 'b, [ `Any ]) t

Cartesian product

Sourceval append : ('a, _) t -> ('a, _) t -> ('a, [ `Any ]) t

Append two collections together

Sourceval 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

Sourceval 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

Sourceval diff : ?cmp:'a ord -> ?eq:'a equal -> ?hash:'a hash -> ('a, _) t -> ('a, _) t -> ('a, [ `Any ]) t

Set difference

Sourceval 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

Tuple and Options

Specialized projection operators

Sourceval map_fst : ('a -> 'b) -> ('a * 'c, 'card) t -> ('b * 'c, 'card) t
Sourceval map_snd : ('a -> 'b) -> ('c * 'a, 'card) t -> ('c * 'b, 'card) t
Sourceval flatten_opt : ('a option, _) t -> ('a, [ `Any ]) t

Flatten the collection by removing None and mapping Some x to x.

Applicative
Sourceval pure : 'a -> ('a, _) t

Synonym to return

Sourceval app : ('a -> 'b, 'card) t -> ('a, 'card) t -> ('b, 'card) t

Apply each function to each value. The cardinality should be the lowest upper bound of both input cardinalities (any,_) -> any, (one,one) -> one, etc.

Sourceval (<*>) : ('a -> 'b, 'card) t -> ('a, 'card) t -> ('b, 'card) t

Infix synonym to app

Monad

Careful, those operators do not allow any optimization before running the query, they might therefore be pretty slow.

Sourceval flat_map : ('a -> ('b, _) t) -> ('a, _) t -> ('b, [ `Any ]) t

Use the result of a query to build another query and immediately run it.

Sourceval (>>=) : ('a, _) t -> ('a -> ('b, _) t) -> ('b, [ `Any ]) t

Infix version of flat_map

Misc
Sourceval lazy_ : ('a lazy_t, 'card) t -> ('a, 'card) t
Sourceexception UnwrapNone
Sourceval opt_unwrap_exn : ('a option, 'card) t -> ('a, 'card) t
Infix
Sourcemodule Infix : sig ... end
Adapters
Sourceval 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

Sourceval reflect_list : ('a, _) t -> ('a list, [> `One ]) t

reflect_list q evaluates all values in q and returns a list of all those values. Also blocks optimizations

Sourceval reflect_hashtbl : ('a * 'b, _) t -> (('a, 'b) Hashtbl.t, [> `One ]) t

Build a hashtable from the collection

Sourceval reflect_queue : ('a, _) t -> ('a Queue.t, [> `One ]) t
Sourceval reflect_stack : ('a, _) t -> ('a Stack.t, [> `One ]) t
Sourcemodule AdaptSet (S : Set.S) : sig ... end
Sourcemodule AdaptMap (M : Map.S) : sig ... end
Sourcemodule IO : sig ... end
Sourceval print : ?sep:string -> 'a printer -> ('a, _) t printer

Evaluate the sequence of elements and print them

  • since 0.2
OCaml

Innovation. Community. Security.