Skip to content

RomanAgeev/bs-monad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bs-monad

The project provides an implementation of all basic monads in ReasonML.

The following monads are available so far:

  1. Writer Monad
  2. Reader Monad
  3. State Monad
  4. List Monad
  5. Option Monad
  6. Promise Monad
  7. Continuation Monad
  8. Indentity Monad

Example with the "fish" (>=>) operator

module TestWriterMonad = WriterMonad.Make({
    type t = string;

    let mappend = (s1, s2) => s1 ++ ", then " ++ s2;
    let mempty = () => "";
});

module TestWriterMonadEx = Monad.Ex(TestWriterMonad);

let () = {
  open TestWriterMonad;
  open TestWriterMonadEx;

  let mult = x => make((x * 2, "Multiply by 2"));
  let plus = x => make((x + 2, "Plus 2")); 

  let process = mult >=> plus;

  let (res, log) = process(10) |> extract; // (22, "Multiply by 2, then Plus 2")
};

Example with the "bind" (>>=) operator

module TestWriterMonad = WriterMonad.Make({
    type t = string;

    let mappend = (s1, s2) => s1 ++ ", then " ++ s2;
    let mempty = () => "";
});

module TestWriterMonadEx = Monad.Ex(TestWriterMonad);

let () = {
  open TestWriterMonad;
  open TestWriterMonadEx;

  let mult = x => make((x * 2, "Multiply by 2"));
  let plus = x => make((x + 2, "Plus 2")); 

  let monad = make((10, "Take 10")) >>= mult >>= plus;
            
  let (res, log) = monad |> extract; // (22, "Take 10, then Multiply by 2, then Plus 2")
};

See the tests for more examples.

About

ReasonML implementation of the basic monads

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published