-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.nim
176 lines (105 loc) · 3.78 KB
/
slice.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import ../stream
import ../stream/loop
import ../stream/loop/[loopscope]
import ../monad/[reader]
import ../optics/[lens]
import ../utils/[convert, nimnodes, operators, partialprocs]
import std/[sugar]
export stream
type
SignedSliceStepIndex* = BiggestInt
SliceStep* [T] = object
i: T
SignedSliceStep* = SliceStep[SignedSliceStepIndex]
func slice* [T](low, high: T): Slice[T] =
low .. high
func low* [T](self: Slice[T]): T =
self.a
func high* [T](self: Slice[T]): T =
self.b
func sliceStep* [T](i: T): SliceStep[T] =
SliceStep[T](i: i)
func signedSliceStep* [T: Ordinal](i: T): SignedSliceStep =
## Since 0.2.0.
sliceStep(i as SignedSliceStepIndex)
func sliceStepSigned* [T: Ordinal](i: T): SignedSliceStep {.deprecated.} =
##[
Deprecated since 0.2.0.
Use `signedSliceStep` instead.
]##
signedSliceStep(i)
func current* [T](S: typedesc[SliceStep[T]]): Lens[S, T] =
lens((self: S) => self.i, (_: S, i: T) => sliceStep(i))
func ordinals* [T: Ordinal](s: Slice[T]): Stream[SignedSliceStep, T] =
let
lens = result.typeof().stepType().current()
readCurrent = lens.read()
readCurrent
.map(partial(?_ < s.high().convert(SignedSliceStepIndex).next()))
.looped(lens.modify(next))
.generating(readCurrent.map(partial(?_ as T)))
.startingAt(() => signedSliceStep(s.low()))
func ordinalsReverse* [T: Ordinal](s: Slice[T]): Stream[SignedSliceStep, T] =
## Since 0.3.0.
s.ordinals().map(i => s.high() - i)
func items* [T: Ordinal](s: Slice[T]): Stream[SliceStep[T], T] =
let
lens = SliceStep[T].current()
readCurrent = lens.read()
readCurrent
.map(partial(?_ < s.high().next()))
.looped(lens.modify(next))
.generating(readCurrent)
.startingAt(() => sliceStep(s.low()))
func itemsReverse* [T: Ordinal](s: Slice[T]): Stream[SliceStep[T], T] =
## Since 0.3.0.
s.items().map(i => s.high() - i)
when isMainModule:
import ../utils/[ignore]
import std/[os, unittest]
proc main () =
suite currentSourcePath().splitFile().name:
test """Counting the number of values in "char.low() .. char.high()" with "ordinals()" should return the length of this slice.""":
proc doTest () =
let
slice = char.low() .. char.high()
actual = slice.ordinals().count(Natural)
expected = slice.len()
check:
actual == expected
doTest()
test """Counting the number of values in the "char" type with "items()" should raise an "OverflowError".""":
proc doTest () =
when defined(js):
# The generated JS code does not seem to raise an "OverflowError".
skip()
else:
expect OverflowError:
slice(char.low(), char.high()).items().count(Natural).ignore()
doTest()
test """Counting the number of items in an empty slice should return 0.""":
proc doTest [T: Ordinal](slice: Slice[T]) =
let
actual = slice.items().count(BiggestUInt)
expected = slice.len() as actual.typeof()
require:
expected == 0
check:
actual == expected
doTest(-1i32 .. -53i32)
doTest(156u64 .. 1u64)
test """Counting the number of odd numbers in an integer slice "s" at compile time should return "s.len() div 2".""":
when defined(js):
skip()
else:
func isOdd [I: SomeInteger](i: I): bool =
i mod 2 == 1
proc doTest [I: SomeInteger](low, high: static I) =
const
s = slice(low, high)
actual = s.items().filter(isOdd[I]).count(BiggestUInt)
expected = s.len() div 2 as actual.typeof()
check:
actual == expected
doTest(0u, 10u)
main()