Skip to content

Instantly share code, notes, and snippets.

@tdakkota
Created January 2, 2021 03:25
Show Gist options
  • Save tdakkota/7164009e272fc0e88bc071916950adde to your computer and use it in GitHub Desktop.
Save tdakkota/7164009e272fc0e88bc071916950adde to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
)
type HList interface {
hlist()
}
func (n Nil) hlist() {}
func (c Cons[H, T]) hlist() {}
type Nil struct{}
type Cons[H any, T HList] struct {
Head H
Tail T
}
func Create[H any](h H) Cons[H, Nil] {
return Cons[H, Nil]{
Head: h,
Tail: Nil{},
}
}
func Prepend[H, T any, L HList](l Cons[T, L], h H) Cons[H, Cons[T, L]] {
return Cons[H, Cons[T, L]]{
Head: h,
Tail: l,
}
}
func PopFront[H, T any, L HList](l Cons[H, Cons[T, L]]) (H, Cons[T, L]) {
return l.Head, l.Tail
}
func main() {
a := Create(1)
b := Prepend(a, "abc")
c := Prepend(b, true)
d := Prepend(c, new(http.Client))
fmt.Println(d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment