package clarity
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=d6d4119872ba2f5aa6f4b3d628a925bb8a0c48110f97dd99cddd5d0d8bc9e6d4
md5=924bffde2be6cd2b8d398d571c408a26
Description
The goal of this project is to make pure functional programming idioms as useful as possible given OCaml's absence of higher-kinded types and typeclasses.
Published: 09 Sep 2018
README
Clarity - functional programming library for OCaml
Description
The goal of this project is to make pure functional programming idioms as useful as possible given OCaml's absence of higher-kinded types and typeclasses.
Main features are:
Standard "classes" like Functor-Applicative-Monad
Concrete instances like Reader-Writer-State
Useful data types like Either, These or Vector
Design notes
All concrete datatypes also have its constructors defined as values where name is prefixed with underscore. Sometimes it's more convenient to use "curried", first-class version of a constructor, e.g. following two are equivalent:
let long = List.map (fun x -> Some x) a
let short = List.map _Some x
Applicative operator
ap
and its infix version(<~>)
are "lazy" by its second argument. This allows for an applicative to "fail-fast" and don't compute unneeded values. "Strict" versions are calledap'
and(<*>)
respectively. "Laziness" here is just (unit -> 'a) closure, so you can use function combinators from Fn module for convenience:
open Clarity
open Option
(*
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val (<~>) : ('a -> 'b) t -> (unit -> 'a t) -> 'b t
val serialize : int -> int -> string -> string
val idx : int option
val long_computation : int -> int option
val title : string option
*)
open Fn
let res : string Option.t =
map serialize idx
<~> defer long_computation 1024
<*> title
Right folds are also "lazy" by "accumulator" argument of a folding function. Strict right fold is called
foldr'
. This allows for shortcut when function no more needs data. For example, here isany
function from Foldable module that checks if at least one element of a Foldable satisfies given predicate:
let any p = foldr (fun x a -> p x || a ()) (const false)
Documentation
You can find ocamldoc here.
Manual installation
$ make && make install