Skip to content

Latest commit

 

History

History
80 lines (56 loc) · 2.02 KB

README.md

File metadata and controls

80 lines (56 loc) · 2.02 KB

nimonoid

Build Status Licence

A monoid library for Nim.

A monoid is a type with an operation to combine 2 values and a neutral element for this operation.

Backend compatibility

  • C
  • C++
  • Objective-C
  • JavaScript
  • NimScript (not tested yet)

Installation

nimble install 'https://github.com/thenjip/nimonoid'

Dependencies

Documentation

Features

API to check the monoid laws for an implementation

  • 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()

Monoid implementations for standard types