Skip to content
This repository
tag: v1345
Fetching contributors…

Cannot retrieve contributors at this time

file 82 lines (68 sloc) 1.972 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
@author Louis Gesbert
**)

type 'a t = ('a -> unit) -> unit

module Ops = struct
  let (@>) f k = f k
  let (|>) x k = k x
end
open Ops

module List = struct
  let rec map f l k = match l with
    | [] -> [] |> k
    | hd::tl -> f hd @> fun hd -> map f tl @> fun tl -> hd::tl |> k

  let rec fold f acc l k = match l with
    | [] -> acc |> k
    | hd::tl -> f acc hd @> fun acc -> fold f acc tl @> k
end

module Option = struct
  let map f opt k = match opt with
    | None -> None |> k
    | Some x -> f x @> fun x -> Some x |> k
end

module Lazy = struct
  type 'a t = {
    push : (unit -> unit) -> unit;
    mutable value : 'a option;
    mutable waiters : ('a -> unit) list;
    cps : ('a -> unit) -> unit;
  }

  let make push cps = {
    push = push;
    value = None;
    waiters = [];
    cps = cps;
  }

  let force l k =
    match l.value with
    | Some x -> x |> k
    | None when l.waiters != [] ->
        l.waiters <- k::l.waiters
    | None ->
        l.waiters <- k::l.waiters;
        l.cps
        @> function x ->
          Base.List.iter (fun k -> l.push (fun () -> k x)) l.waiters;
          l.value <- Some x;
          l.waiters <- []

  let get_state cps = cps.value

  let lazy_from_val x = {
    push = (fun _ -> ());
    value = Some x;
    waiters = [];
    cps = fun k -> k x;
  }
end
Something went wrong with that request. Please try again.