Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
50
Task/Monads-Maybe-monad/Go/monads-maybe-monad.go
Normal file
50
Task/Monads-Maybe-monad/Go/monads-maybe-monad.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type maybe struct{ value *int }
|
||||
|
||||
func (m maybe) bind(f func(p *int) maybe) maybe {
|
||||
return f(m.value)
|
||||
}
|
||||
|
||||
func unit(p *int) maybe {
|
||||
return maybe{p}
|
||||
}
|
||||
|
||||
func decrement(p *int) maybe {
|
||||
if p == nil {
|
||||
return unit(nil)
|
||||
} else {
|
||||
q := *p - 1
|
||||
return unit(&q)
|
||||
}
|
||||
}
|
||||
|
||||
func triple(p *int) maybe {
|
||||
if p == nil {
|
||||
return unit(nil)
|
||||
} else {
|
||||
q := (*p) * 3
|
||||
return unit(&q)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
i, j, k := 3, 4, 5
|
||||
for _, p := range []*int{&i, &j, nil, &k} {
|
||||
m1 := unit(p)
|
||||
m2 := m1.bind(decrement).bind(triple)
|
||||
var s1, s2 string = "none", "none"
|
||||
if m1.value != nil {
|
||||
s1 = strconv.Itoa(*m1.value)
|
||||
}
|
||||
if m2.value != nil {
|
||||
s2 = strconv.Itoa(*m2.value)
|
||||
}
|
||||
fmt.Printf("%4s -> %s\n", s1, s2)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue