fn: add fundamental functional primitives

This commit is contained in:
Keagan McClelland committed 2024-07-10 14:42:11 -07:00
1 parent 3526f82b5d
commit 94acbe90a8
2 files changed
+87

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
package fn
// Unit is a type alias for the empty struct to make it a bit less noisy to
// communicate the informationaless type.
type Unit = struct{}
// Comp is left to right function composition. Comp(f, g)(x) == g(f(x)). This
// can make it easier to create on the fly closures that we may use as
// arguments to other functions defined in this package (or otherwise).
func Comp[A, B, C any](f func(A) B, g func(B) C) func(A) C {
return func(a A) C {
return g(f(a))
}
}
// Iden is the left and right identity of Comp. It is a function that simply
// returns its argument. The utility of this function is only apparent in
// conjunction with other functions in this package.
func Iden[A any](a A) A {
return a
}
// Const is a function that accepts an argument and returns a function that
// always returns that value irrespective of the returned function's argument.
// This is also quite useful in conjunction with higher order functions.
func Const[A, B any](a A) func(B) A {
return func(_ B) A {
return a
}
}