Skip to content

Commit

Permalink
- monad/[ion optional, predicate, reader]:
Browse files Browse the repository at this point in the history
  - Add static qualifier to compile time test parameters when possible.
    Although, the following is not possible in stable Nim yet (nim-lang/Nim#9679).
  • Loading branch information
thenjip committed Jun 18, 2020
1 parent 63fbfd6 commit 63e3bf5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/nim_iterator_stream_experiment/monad/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ when isMainModule:
test """"IO[T]" without side effects should be compatible with compile time execution.""":
template doTest [T](
sut: IO[T]{noSideEffect};
expected: T
expected: static T
): proc () {.nimcall.} =
(
proc () =
Expand Down
2 changes: 1 addition & 1 deletion src/nim_iterator_stream_experiment/monad/optional.nim
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ when isMainModule:
template doTest [A; B](
sut: static[proc (argument: Optional[A]): B {.noSideEffect.}];
argument: Optional[A]{noSideEffect};
expected: B
expected: static B
): proc () {.nimcall.} =
(
proc () =
Expand Down
41 changes: 33 additions & 8 deletions src/nim_iterator_stream_experiment/monad/predicate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func alwaysTrue* [T](_: T): bool =

when isMainModule:
import identity
import ../utils/[ignore, operators, unit, variables]
import ../utils/[call, ignore, operators, unit, variables]

import std/[os, unittest]
import std/[os, strutils, unittest]



Expand Down Expand Up @@ -118,14 +118,12 @@ when isMainModule:

var pathTaken = 0.Natural

proc incrementPathCounter [T](value: T): T =
pathTaken.modify(plus1).apply(_ => value)
let incPathCounter =
(value: B) => pathTaken.modify(plus1).apply(_ => value)

self
.ifElse(
then.map(incrementPathCounter),
`else`.map(incrementPathCounter)
).run(value)
.ifElse(then.map(incPathCounter), `else`.map(incPathCounter))
.run(value)
.ignore()

let actual = pathTaken.read()
Expand All @@ -136,3 +134,30 @@ when isMainModule:

doTest(alwaysTrue[Unit], itself[Unit], itself, unit())
doTest(alwaysFalse[int16], partial($ ?:int16), _ => "abc", 542)



test """"self.test(value)" at compile time should return the expected boolean.""":
template doTest [T](
self: Predicate[T]{noSideEffect};
value: static T;
expected: static bool
): proc () {.nimcall.} =
(
proc () =
const actual = predicate.test(self, value)

check:
actual == expected
)


for t in [
doTest(partial(?:int > 0), -1, false),
doTest(
partial(?:char in Letters) and partial(?:char != '\0'),
'a',
true
)
]:
t.call()
4 changes: 2 additions & 2 deletions src/nim_iterator_stream_experiment/monad/reader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ when isMainModule:
test """"Reader[S, T]" without side effects should be compatible with compile time execution.""":
template doTest [S; T](
sut: Reader[S, T]{noSideEffect};
state: S{noSideEffect};
expected: T
state: static S;
expected: static T
): proc () {.nimcall.} =
(
proc () =
Expand Down

0 comments on commit 63e3bf5

Please sign in to comment.