repo.or.cz
/
ruby-do-notation.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
history
|
raw
|
HEAD
Now uses arrows!!
[ruby-do-notation.git]
/
lib
/
maybe.rb
blob
9bfd5dc824b01cc852483435b11e240c7bf251a0
1
class Maybe
2
extend Monad
3
4
class << self
5
alias_method :nothing, :new
6
alias_method :just, :new
7
alias_method :unit, :just
8
end
9
10
attr_accessor :value, :nothing
11
12
def initialize *args
13
if args.empty?
14
@nothing = true
15
else
16
@value = args.shift
17
end
18
end
19
20
def ==(m)
21
m.is_a? Maybe and ((nothing and m.nothing) or (value = m.value))
22
end
23
24
def bind &f
25
if self.nothing
26
self
27
else
28
f.call(self.value)
29
end
30
end
31
32
def to_s
33
if @nothing
34
"nothing"
35
else
36
"just(#{@value})"
37
end
38
end
39
end