package spin

  1. Overview
  2. Docs
OCaml project generator

Install

Dune Dependency

Authors

Maintainers

Sources

opam-spin-0.8.3.tbz
sha256=eeef9d3b89cca796d8d47ba425cee131bf14b5b0f1349927c4d8b22297b1f6b6
sha512=1850cad87f180b37e658e716986f6aa0ba76e57d6638d40b8f6e8ae2c6f60deee82218464e02356632022b394f49cc5a341e7255112aef455ffec9ca2db4cc32

doc/src/spin.std/result.ml.html

Source file result.ml

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
include Stdlib.Result

let both a b =
  match a with
  | Error e ->
    Error e
  | Ok a ->
    (match b with Error e -> Error e | Ok b -> Ok (a, b))

module Syntax = struct
  let ( >>= ) t f = bind t f

  let ( >>| ) t f = map f t

  let ( let* ) = ( >>= )

  let ( let+ ) = ( >>| )

  let ( and+ ) = both
end

open Syntax

module List = struct
  let map f t =
    let rec loop acc = function
      | [] ->
        Ok (List.rev acc)
      | x :: xs ->
        f x >>= fun x -> loop (x :: acc) xs
    in
    loop [] t

  let all =
    let rec loop acc = function
      | [] ->
        Ok (List.rev acc)
      | t :: l ->
        t >>= fun x -> loop (x :: acc) l
    in
    fun l -> loop [] l

  let concat_map =
    let rec loop f acc = function
      | [] ->
        Ok (List.rev acc)
      | x :: l ->
        f x >>= fun y -> loop f (List.rev_append y acc) l
    in
    fun l f -> loop f [] l

  let rec iter f t =
    match t with [] -> Ok () | x :: xs -> f x >>= fun () -> iter f xs

  let rec fold_left f init t =
    match t with
    | [] ->
      Ok init
    | x :: xs ->
      f init x >>= fun init -> fold_left f init xs

  let rec iter_left f t =
    match t with [] -> Ok () | x :: xs -> f x >>= fun () -> iter_left f xs

  let filter_map t f =
    fold_left
      (fun acc x -> f x >>| function None -> acc | Some y -> y :: acc)
      []
      t
    >>| List.rev
end
OCaml

Innovation. Community. Security.