Skip to content

Instantly share code, notes, and snippets.

Avatar

fogfish

View GitHub Profile
@fogfish
fogfish / error-behaviour.go
Last active Jul 6, 2022
Example of behaviour used for “Assert errors for behavior, not type”
View error-behaviour.go
//
// See the post: https://tech.fog.fish/2022/07/05/assert-golang-errors-for-behavior.html
//
// ErrorCodes provides a raw code originared by the failed package
type ErrorCode interface{ ErrorCode() string }
// ErrorType is the primary identifier (IRI) for the problem type, defined by the ontology
type ErrorType interface{ ErrorType() string }
View lightweight-hkt.go
package main
import (
"fmt"
"strings"
)
//
//
type HKT[F, A any] interface {
View lightweight-hkt.ts
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>
View pipe.go
package main
import (
"fmt"
"strconv"
)
//
//
func filter[A any](f func(A) bool, a A) A {
View heterogeneous-product.go
/*
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]
}
View heterogeneous-product-types.go
/*
Seq defines fundamental general purpose sequence
*/
type Seq[S any, T any] interface {
Head(S) *T
Tail(S) S
IsVoid(S) bool
}
View semigroup-fold.go
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] }
View contramap.go
/*
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 {
View homogenous-product.go
/*
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.
View homogenous-product-types.go
// 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])