Created
June 27, 2020 01:49
-
-
Save tdakkota/1d3df290fa6c3cf0111159876aa6288b 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 | |
import ( | |
"fmt" | |
"strings" | |
) | |
type Tuple(type A, B) interface { | |
Head() A | |
Tail() B | |
Unwrap() (A, B) | |
} | |
type tuple(type A, B) struct { | |
A A | |
B B | |
} | |
func (u tuple(A, B)) Head() A { | |
return u.A | |
} | |
func (u tuple(A, B)) Tail() B { | |
return u.B | |
} | |
func (u tuple(A, B)) Unwrap() (A, B) { | |
return u.A, u.B | |
} | |
func New(type A, B)(a A, b B) Tuple(A, B) { | |
return tuple(A, B){a, b} | |
} | |
func NewNothing(type A)(a A) Tuple(A, Nothing) { | |
return tuple(A, Nothing){a, Nothing{}} | |
} | |
type Nothing struct{} | |
func curry(type | |
IA, IB, RA, RB interface{}, | |
R Tuple(RA, RB), | |
)(a IA, fn func(t Tuple(IA, IB)) R) func(IB) R { | |
return func(b IB) R { | |
return fn(New(a, b)) | |
} | |
} | |
func Curried(type A, B, R)(f func(A, B) R) func(t Tuple(A, B)) Tuple(R, Nothing) { | |
return func(t Tuple(A, B)) Tuple(R, Nothing) { | |
return NewNothing(f(t.Head(), t.Tail())) | |
} | |
} | |
func Curried2(type A, B, RA, RB)(f func(A, B) (RA, RB)) func(t Tuple(A, B)) Tuple(RA, RB) { | |
return func(t Tuple(A, B)) Tuple(RA, RB) { | |
return New(f(t.Head(), t.Tail())) | |
} | |
} | |
func Join(t Tuple(string, []string)) Tuple(string, Nothing) { | |
return NewNothing(strings.Join(t.Tail(), t.Head())) | |
} | |
func main() { | |
cur := curry(string, []string, string, Nothing, Tuple(string, Nothing))(", ", Join) | |
fmt.Println(cur([]string{"Hello", "playground"}).Unwrap()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment