Skip to content

Instantly share code, notes, and snippets.

View made2591's full-sized avatar
🎯
Focusing

Matteo Madeddu made2591

🎯
Focusing
View GitHub Profile
...
func (l *Log) ToString() string {
return fmt.Sprintf("Res: %d, Ops: %s", l.Val, l.Op)
}
...
...
x := 0
fmt.Printf("%s\n", bind(bind(bind(unit(x), f1), f2), f3).ToString())
...
...
// no more required and not repeated
log += log2 + "; "
log += log3 + "; "
...
...
func bind(x *Log, f Increment) *Log {
val, op := f(x.Val)
return &Log{Val: val, Op: fmt.Sprintf("%s%s; ", x.Op, op)}
}
...
...
func unit(x int) *Log {
return &Log{Val: x, Op: ""}
}
...
...
type Increment func(y int) (int, string)
type Log struct {
Val int
Op string
}
...
...
func unit(x int) *Log {
return &Log{Val: x, Op: ""}
}
func bind(x *Log, f Increment) *Log {
...
x := 0
log := "Ops: "
res, log1 := f1(x)
log += log1 + "; "
res, log2 := f2(res)
log += log2 + "; "
...
func f1(x int) (int, string) {
return x + 1, fmt.Sprintf("%d+1", x)
}
func f2(x int) (int, string) {
@made2591
made2591 / mm.go
Created January 14, 2020 22:03
Monad in go
package main
import (
"fmt"
)
func f1(x int) (int, string) {
return x + 1, fmt.Sprintf("%d+1", x)