A monoid library for Nim.
A monoid is a type with an operation to combine 2 values and a neutral element for this operation.
- C
- C++
- Objective-C
- JavaScript
- NimScript (not tested yet)
nimble install 'https://github.com/thenjip/nimonoid'
-
somemonoid.nim
type SomeMonoid = string proc neutral*(X: typedesc[SomeMonoid]): X = "" proc fold*(left, right: SomeMonoid): SomeMonoid = left & right
-
test.nim
import somemonoid import pkg/nimonoid/[laws] import pkg/funcynim/[curry, run] import std/[unittest] suite "somemonoid": test "SomeMonoid should verify the monoid laws.": proc doTest(spec: AllLawsSpec[SomeMonoid]) = let (leftIdentity, rightIdentity, associativity) = spec.verify() check(leftIdentity.isVerified()) check(rightIdentity.isVerified()) check(associativity.isVerified()) allLawsSpec(leftIdentitySpec("abc")) .with(rightIdentitySpec("0213 ")) .run(associativitySpec("jUFha")("pdAbhqc")("5JK0jkty")) .doTest()
- Integers
- Linear data structures
- And more.