package core_extended

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Lazy values reimplementation.

There are two advantages to reimplementing the lazy module in pure Ocaml. Advantage number one is speed: I've measured ~140 clocks to force a standard lazy value the first time, and ~80 clocks to force it the second time. If the lazy computation you're avoiding is creating a simple cons cell, this is horribly expensive. The following implementation is like ~30 clocks to force the lazy value the first time, and single-digit clocks to force it the second time.

There are two downsides to doing it this way. One, you can't use lazy keyword. And two, this implementation uses a little more memory per lazy value (it currently uses 5 words/lazy value).

A non-threadsafe reimplementation of Lazy

type 'a t
val of_val : 'a -> 'a t

Create a lazy value of a non-lazy value. The lazy value created will be already forced, for obvious reasons.

val of_fun : (unit -> 'a) -> 'a t

Create a lazy value of a function. The function will not be executed until the lazy value is first forced, and will only be executed once.

If the function raises an exception, all futures forces of the lazy value will also raise the same exception.

val force : 'a t -> 'a

Force the lazy value.

If the function that produces the value throws an exception, this function will throw the same exception.

val is_val : _ t -> bool

Returns true if the lazy value has been forced and did not throw an exception.

include Core.Interfaces.Monad with type 'a t := 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.

val (>>|) : 'a t -> ('a -> 'b) -> 'b t

t >>| f is t >>= (fun a -> return (f a)).

module Monad_infix : sig ... end
val bind : 'a t -> f:('a -> 'b t) -> 'b t

bind t ~f = t >>= f

val return : 'a -> 'a t

return v returns the (trivial) computation that returns v.

val map : 'a t -> f:('a -> 'b) -> 'b t

map t ~f is t >>| f.

val join : 'a t t -> 'a t

join t is t >>= (fun t' -> t').

val ignore_m : 'a t -> unit t

ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Pervasives.ignore. Some monads still do let ignore = ignore_m for historical reasons.

val all : 'a t list -> 'a list t
val all_ignore : unit t list -> unit t
module Let_syntax : sig ... end

These are convenient to have in scope when programming with a monad

OCaml

Innovation. Community. Security.