Brings monads with nice syntactic sugar to Haxe
Haxe JavaScript Shell
Switch branches/tags
Nothing to show
Latest commit 8682443 Apr 28, 2014 @sledorze Update README.textile
Permalink
Failed to load latest commit information.
bin Haxe 3 compatibility Mar 8, 2013
src new version Feb 24, 2014
test use hamcrest matchers in tests Feb 24, 2014
.munit added .munit file Jan 3, 2012
README.textile Update README.textile Apr 28, 2014
monax.hxproj added more lightweight syntactic sugar. Jan 25, 2012
package.sh add package.sh Aug 22, 2013
test.hxml added tests Jan 3, 2012

README.textile

Monax macro library.

This library, built on top of Haxe macros give the user the possibility to define specific Monad instance and get automatically access to a haskell like sugared syntax.
About monad: http://en.wikipedia.org/wiki/Monad_(functional_programming)#Background

The syntactic sugar rely on the existence ‘ret’ and ‘flatMap’ in the Monad instance.
It also requiers the existence of ‘map’ to provide automatic optimisations.

Learn the (not so) hard way – Defining a Monad instance.



package mypackage;

class OptionM {

  public static function monad<T>(o : Option<T>) return OptionM; // will help with syntactic Sugar (see below)
    
  @:macro public static function dO(body : Expr) return // the function to trigger the Monad macro.
    Monad.dO("mypackage.OptionM", body, Context)

  inline public static function ret<T>(x : T) return // creates an element
    Some(x)
  
  inline public static function map < T, U > (x : Option<T>, f : T -> U) : Option<U> {
    switch (x) {
      case Some(x) : return Some(f(x));
      default : return None;
    }
  }

  inline public static function flatMap<T, U>(x : Option<T>, f : T -> Option<U>) : Option<U> {
    switch (x) {
      case Some(x) : return f(x);
      default : return None;
    }
  }
}


Using a Monad instance (Option here).


        OptionM.dO({
          value <= ret(55);
          value1 <= ret(value * 2);
          ret(value1 + value);
        });

Due to optimisations; this code reduce to Some(165), those optimisations are applied by default.
One can define his own optimisations (feeding the Monad.dO method its last parameter)


  @:macro public static function dO(body : Expr) return
    Monad.dO("mypackage.OptionM", body, Context, myCustomTransformation)

(please refer to the default function code to make sense of how to make optimization – macro knowledge requiered).

or using no optimisation at all:


  @:macro public static function dO(body : Expr) return
    Monad.dO("mypackage.OptionM", body, Context, Monad.noOpt)

Syntactic Sugar support.

Monax provides some leaner syntax:


import com.mindrocks.monads.Monad.dO in Do; ... Do({ value <= [55]; value1 <= return value * 2; return value1 + value; });