Last active
June 21, 2020 21:22
-
-
Save tdakkota/53b2f08f08bcc3aa0edd3f4478499ef7 to your computer and use it in GitHub Desktop.
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
package main | |
type Optional(type T) struct { | |
p *T | |
} | |
func OptionalFrom(type T)(value T) Optional(T) { | |
return Optional(T){p: &value} | |
} | |
func (o Optional(T)) Val() (T, bool) { | |
if o.p != nil { | |
return *o.p, true | |
} | |
var zero T | |
return zero, false | |
} | |
type Result(type T) struct { | |
p T | |
err error | |
} | |
func ResultFrom(type T)(value T, err error) Result(T) { | |
return Result(T){p: value, err: err} | |
} | |
func (o Result(T)) Val() (T, error) { | |
if o.err != nil { | |
var zero T | |
return zero, o.err | |
} | |
return o.p, nil | |
} | |
type generic(type T) interface{} | |
type g(type T) interface { | |
type T, generic(T) | |
} | |
func F(type | |
V interface{}, | |
A g(V), | |
B g(V), | |
)(o A, f func(A) B) B { | |
return f(o) | |
} | |
func Print(type T g(T))(s []T) { | |
for _, v := range s { | |
// A(T) -> T | |
_ = F(T, Optional(T), *T)(OptionalFrom(T)(v), func(o Optional(T)) (r *T) { | |
return | |
}) | |
// A -> B(T) | |
_ = F(T, T, Result(T))(v, func(o T) (r Result(T)) { | |
return | |
}) | |
// A(T) -> B(T) | |
_ = F(T, Optional(T), Result(T))(OptionalFrom(T)(v), func(o Optional(T)) (r Result(T)) { | |
return | |
}) | |
// A(T) -> C(B(T)) | |
_ = F( | |
T, | |
Optional(T), | |
Optional(Result(T)), | |
)(OptionalFrom(T)(v), func(o Optional(T)) (zero Optional( Result(T) ) ) { | |
return | |
}) | |
} | |
} | |
func main() { | |
Print([]string{"Hello, ", "playground"}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment