summary refs log tree commit diff stats
path: root/tests/iter/titer12.nim
blob: f7fc64da4eb567ff70e6aac49263625f5c28dfc9 (plain) (blame)
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
discard """
output: '''
Selecting 2
1.0
Selecting 4
2.0
'''
"""


# bug #5522
import macros, sugar, sequtils

proc tryS(f: () -> void): void =
  (try: f() except: discard)

template trySTImpl(body: untyped): untyped =
  tryS do() -> auto:
    `body`

macro tryST*(body: untyped): untyped =
  var b = if body.kind == nnkDo: body[^1] else: body
  result = quote do:
    trySTImpl((block:
      `b`
    ))

iterator testIt(): int {.closure.} =
  for x in 0..10:
    yield x

var xs = newSeq[int]()
proc test = tryST do:
  for x in testIt():
    xs.add(x)

test()

doAssert xs == toSeq(0..10)



# bug #5690
proc filter[T](it: (iterator : T), f: proc(x: T): bool): (iterator : T) =
  return iterator (): T {.closure.} =
    for x in it():
      if f(x): 
        yield x

proc len[T](it : iterator : T) : Natural =
  for i in it():
    result += 1

proc simpleSeqIterator(s :seq[int]) : iterator : int =
  iterator it: int {.closure.} =
    for x in s:
      yield x
  result = it

let a = newSeq[int](99)

doAssert len(simpleSeqIterator(a).filter(proc(x : int) : bool = true)) == 99



# bug #5340
proc where[A](input: seq[A], filter: (A) -> bool): iterator (): A =
  result = iterator (): A {.closure.} = 
    for item in input:
      if filter(item):
        yield item

proc select[A,B](input: iterator(): A {.closure.}, selector: (A) -> B): iterator (): B {.closure.} = 
  result = iterator (): B =
    for item in input():
      echo "Selecting " & $item
      yield selector(item)

let query = @[1,2,3,4].where(x=>x mod 2==0).select((x)=>x/2)

for i in query():
  echo $i
x, y, z var en: E en = a # Bug #4066 macro genEnum(): untyped = newNimNode(nnkEnumTy).add(newEmptyNode(), newIdentNode("geItem1")) type GeneratedEnum = genEnum() doAssert(type(geItem1) is GeneratedEnum) block tenum2: type TEnumHole = enum eA = 0, eB = 4, eC = 5 var e: TEnumHole = eB case e of eA: echo "A" of eB: echo "B" of eC: echo "C" block tenum3: type TEnumHole {.size: sizeof(int).} = enum eA = 0, eB = 4, eC = 5 var e: TEnumHole = eB case e of eA: echo "A" of eB: echo "B" of eC: echo "C" block tbasic: type MyEnum = enum A,B,C,D # trick the optimizer with an seq: var x = @[A,B,C,D] echo x[0],x[1],x[2],x[3],MyEnum(2) block talias: # bug #5148 type A = enum foo, bar B = A echo B.foo block thole: type Holed = enum hFirst = (0,"first") hSecond = (32,"second") hThird = (64,"third") var x = @[0,32,64] # This is just to avoid the compiler inlining the value of the enum echo Holed(x[0]),ord Holed(x[0]),Holed(x[1]),ord Holed(x[1]),Holed(x[2]),ord Holed(x[2]) block toffset: const strValB = "my value B" type TMyEnum = enum valueA = (1, "my value A"), valueB = strValB & "conc", valueC, valueD = (4, "abc") proc getValue(i:int): TMyEnum = TMyEnum(i) # trick the optimizer with a variable: var x = getValue(4) echo getValue(1), ord(valueA), getValue(2), ord(valueB), getValue(3), getValue(4), ord(valueD), x block tnamedfields: const strValB = "my value B" type TMyEnum = enum valueA = (0, "my value A"), valueB = strValB & "conc", valueC, valueD = (3, "abc"), valueE = 4 # trick the optimizer with a variable: var x = valueD echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x doAssert $x == $valueD, $x doAssert $x == "abc", $x block tfakeOptions: type TFakeOption = enum fakeNone, fakeForceFullMake, fakeBoehmGC, fakeRefcGC, fakeRangeCheck, fakeBoundsCheck, fakeOverflowCheck, fakeNilCheck, fakeAssert, fakeLineDir, fakeWarns, fakeHints, fakeListCmd, fakeCompileOnly, fakeSafeCode, # only allow safe code fakeStyleCheck, fakeOptimizeSpeed, fakeOptimizeSize, fakeGenDynLib, fakeGenGuiApp, fakeStackTrace TFakeOptionset = set[TFakeOption] var gFakeOptions: TFakeOptionset = {fakeRefcGC, fakeRangeCheck, fakeBoundsCheck, fakeOverflowCheck, fakeAssert, fakeWarns, fakeHints, fakeLineDir, fakeStackTrace} compilerArgs: int gExitcode: int8 block nonzero: # bug #6959 type SomeEnum = enum A = 10 B C let slice = SomeEnum.low..SomeEnum.high block size_one_byte: #issue 15752 type Flag = enum Disabled = 0x00 Enabled = 0xFF static: assert 1 == sizeof(Flag)