package ppx_monad

  1. Overview
  2. Docs
A Syntax Extension for all Monadic Syntaxes

Install

Dune Dependency

Authors

Maintainers

Sources

v0.1.0.tar.gz
md5=9fc7e483877beb07c56c1d92875c5034
sha512=5efb1026d9b12f537d2c1f1c608c450de90267090dd81a4ce521d52ac752628248bed42c1a23898f5742970c81b24f1d030b029fa8beb4e4b7c33b7004ec196b

doc/src/ppx_monad_either/ppx_monad_either.ml.html

Source file ppx_monad_either.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
open Ppxlib

let mk_return ~loc x =
  [%expr Stdlib.Either.Right [%e x]]

let mk_bind ~loc e f =
  [%expr
    (fun e f -> match e with
       | Stdlib.Either.Left y -> Stdlib.Either.Left y
       | Stdlib.Either.Right x -> f x) [%e e] [%e f]]

let mk_fail ~loc y =
  [%expr Stdlib.Result.Left [%e y]]

let mk_catch ~loc e f =
  [%expr
    (fun e f -> match e with
       | Stdlib.Either.Right x -> Stdlib.Either.Right x
       | Stdlib.Either.Left y -> f y) [%e e] [%e f]]

let () = Ppx_monad.register "either.right"
    ~applies_on:"either|(either.)?right"
    ~mk_return ~mk_bind
    ~mk_fail ~mk_catch

let () = Ppx_monad.register "either.left"
    ~applies_on:"(either.)?left"
    ~mk_return:mk_fail
    ~mk_bind:mk_catch
OCaml

Innovation. Community. Security.