-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8653 from ProofOfKeags/fn-prim
DynComms [0/n]: `fn` package additions
- Loading branch information
Showing
14 changed files
with
1,586 additions
and
65 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package fn | ||
|
||
import ( | ||
"testing" | ||
"testing/quick" | ||
) | ||
|
||
func TestPropConstructorEliminatorDuality(t *testing.T) { | ||
f := func(i int, s string, isRight bool) bool { | ||
Len := func(s string) int { return len(s) } // smh | ||
if isRight { | ||
v := ElimEither( | ||
Iden[int], | ||
Len, | ||
NewRight[int, string](s), | ||
) | ||
return v == Len(s) | ||
} | ||
|
||
v := ElimEither( | ||
Iden[int], | ||
Len, | ||
NewLeft[int, string](i), | ||
) | ||
return v == i | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPropWhenClauseExclusivity(t *testing.T) { | ||
f := func(i int, isRight bool) bool { | ||
var e Either[int, int] | ||
if isRight { | ||
e = NewRight[int, int](i) | ||
} else { | ||
e = NewLeft[int, int](i) | ||
} | ||
z := 0 | ||
e.WhenLeft(func(x int) { z += x }) | ||
e.WhenRight(func(x int) { z += x }) | ||
|
||
return z != 2*i && e.IsLeft() != e.IsRight() | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPropSwapEitherSelfInverting(t *testing.T) { | ||
f := func(i int, s string, isRight bool) bool { | ||
var e Either[int, string] | ||
if isRight { | ||
e = NewRight[int, string](s) | ||
} else { | ||
e = NewLeft[int, string](i) | ||
} | ||
return e.Swap().Swap() == e | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPropMapLeftIdentity(t *testing.T) { | ||
f := func(i int, s string, isRight bool) bool { | ||
var e Either[int, string] | ||
if isRight { | ||
e = NewRight[int, string](s) | ||
} else { | ||
e = NewLeft[int, string](i) | ||
} | ||
return MapLeft[int, string, int](Iden[int])(e) == e | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPropMapRightIdentity(t *testing.T) { | ||
f := func(i int, s string, isRight bool) bool { | ||
var e Either[int, string] | ||
if isRight { | ||
e = NewRight[int, string](s) | ||
} else { | ||
e = NewLeft[int, string](i) | ||
} | ||
return MapRight[int, string, string](Iden[string])(e) == e | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPropToOptionIdentities(t *testing.T) { | ||
f := func(i int, s string, isRight bool) bool { | ||
var e Either[int, string] | ||
if isRight { | ||
e = NewRight[int, string](s) | ||
|
||
r2O := e.RightToOption() == Some(s) | ||
o2R := e == OptionToRight[string, int, string]( | ||
Some(s), i, | ||
) | ||
l2O := e.LeftToOption() == None[int]() | ||
|
||
return r2O && o2R && l2O | ||
} else { | ||
e = NewLeft[int, string](i) | ||
l2O := e.LeftToOption() == Some(i) | ||
o2L := e == OptionToLeft[int, int](Some(i), s) | ||
r2O := e.RightToOption() == None[string]() | ||
|
||
return l2O && o2L && r2O | ||
} | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPropUnwrapIdentities(t *testing.T) { | ||
f := func(i int, s string, isRight bool) bool { | ||
var e Either[int, string] | ||
if isRight { | ||
e = NewRight[int, string](s) | ||
return e.UnwrapRightOr("") == s && | ||
e.UnwrapLeftOr(0) == 0 | ||
} else { | ||
e = NewLeft[int, string](i) | ||
return e.UnwrapLeftOr(0) == i && | ||
e.UnwrapRightOr("") == "" | ||
} | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 | ||
} | ||
} | ||
|
||
// Eq is a curried function that returns true if its eventual two arguments are | ||
// equal. | ||
func Eq[A comparable](x A) func(A) bool { | ||
return func(y A) bool { | ||
return x == y | ||
} | ||
} | ||
|
||
// Neq is a curried function that returns true if its eventual two arguments are | ||
// not equal. | ||
func Neq[A comparable](x A) func(A) bool { | ||
return func(y A) bool { | ||
return x != y | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.