Created
July 16, 2015 16:05
-
-
Save he9lin/4607bbafd1b9948df6a6 to your computer and use it in GitHub Desktop.
Fun with deterministic gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'deterministic' | |
module Deterministic | |
module Monad | |
alias :>= :fmap | |
end | |
end | |
class Foo | |
include Deterministic | |
include Deterministic::Prelude::Result | |
alias :m :method # method conveniently returns a Proc to a method | |
def call(params) | |
Success(params) >> | |
m(:validate).curry['x'] >> | |
m(:send).curry['y'] >= | |
m(:report) | |
end | |
def validate(a, params) | |
Success(params + a) | |
end | |
def send(a, params) | |
Success(params + a) | |
end | |
def report(params) | |
params + 'report' | |
end | |
def calculate | |
Success(1) >= m(:plus_one) | |
end | |
def plus_one(n) | |
n += 1 | |
end | |
end | |
p Foo.new.call('data') | |
p Foo.new.calculate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment