Monads for Crystal.
Inspired from https://github.com/dry-rb/dry-monads
Add this to your application's shard.yml:
dependencies:
monads:
github: alex-lairan/monadsrequire "monads"Many monads exists.
The Maybe monad help to avoid nil and chain instructions.
There is two kind of Maybe, Just and Nothing.
This is just a value.
Monads::Just.new(5)This a absance of value.
Monads::Nothing(Int32).newThe Either monad help to manage errors at the end of the chain instruction.
There is two kind of Either, Right and Left.
This is just a value.
Monads::Right.new("Hello world")This is an error.
Monads::Left.new("User password is incorrect")The List monad help to manipulate an Array like a monad.
Monads::List[1, 6, 4, 2]head returns the first element wrapped with a Maybe.
tail returns the list without the first element.
The Try monad is a layer between Object Oriented Exception and Fuctional Programming Monads.
It can be transformed into a Maybe or an Either.
The Task monad is a parallelized Try monad.
Its goal is to use the power of fibers with monads.
Monads have some methods who help to chain instructions.
ps: Try and Task monads should be translated into a Maybe(T) of an Either(Exception, T) one.
The fmap procedure modify internal value of a monad.
This doesn't affect Nothing and Left monads.
Example :
value = Monads::Just.new(5)
.fmap(->(x : Int32) { x.to_s })
.fmap(->(x : String) { x + "12" })
.fmap(->(x : String) { x.to_i })
.value!
value.should eq(512)The bind procedure allow to create a whole new monads from the internal data of another.
This doesn't affect Nothing and Left monads.
Example :
value = Monads::Just.new(5)
.bind(->(x : Int32) { Monads::Try(Int32).new(-> { x / 0}).to_maybe })
value.should eq(Monads::Nothing(Int32).new)Clone then let's go, no special requirements.
- Fork it (https://github.com/alex-lairan/monads/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
- alex-lairan Alexandre Lairan - creator, maintainer
- moba1 moba - maintainer