Skip to content

Instantly share code, notes, and snippets.

View fogfish's full-sized avatar

fogfish

View GitHub Profile
/*
FromEq is a combinator that lifts T ⟼ T ⟼ bool function to
an instance of Eq type trait
*/
type FromEq[T any] func(T, T) bool
// implementation of Eq type class
func (f FromEq[T]) Equal(a, b T) bool { return f(a, b)}
// ExampleType product type is product of primitive types int × string
type ExampleType struct {
A int
B string
}
// Instances of Eq type trait for primitive types
var (
Int Eq[int] = FromEq[int](equal[int])
String Eq[string] = FromEq[string](equal[string])
/*
UnApply2 is like contra-map function for data type T that unwrap product type
*/
type UnApply2[T, A, B any] func(T) (A, B)
/*
ProductEq2 is a container, product of type trait instances.
Here, the implementation is a shortcut due to the absence of heterogeneous lists in Golang.
/*
ContraMapEq is a combinator that build a new instance of type trait Eq[B] using
existing instance of Eq[A] and f: b ⟼ a
*/
type ContraMapEq[A, B any] struct{ Eq[A] }
// implementation of contra variant functor
func (c ContraMapEq[A, B]) FMap(f func(B) A) Eq[B] {
return FromEq[B](func(a, b B) bool {
type Foldable[T any] interface {
Fold(T, []T) T
}
type Semigroup[T any] interface {
Combine(T, T) T
}
type Folder[T any] struct{ Semigroup[T] }
/*
Seq defines fundamental general purpose sequence
*/
type Seq[S any, T any] interface {
Head(S) *T
Tail(S) S
IsVoid(S) bool
}
/*
SeqEq is a heterogeneous product of Seq and Eq laws.
It composes two types together that "knows" how to compare sequences.
*/
type SeqEq[S, T any] struct {
Seq[S, T]
Eq[T]
}
package main
import (
"fmt"
"strconv"
)
//
//
func filter[A any](f func(A) bool, a A) A {
class HKT<F, A> {}
interface Functor<F> {
map<A, B>(f: (a: A) => B, fa: HKT<F, A>): HKT<F, B>
}
type Seq<A> = HKT<'Seq', A>
type Abc<A> = HKT<'Abc', A>
type seq<A> = Array<A>
package main
import (
"fmt"
"strings"
)
//
//
type HKT[F, A any] interface {