async works again
This commit is contained in:
parent
813f98fb34
commit
b4c62d5fed
3 changed files with 188 additions and 20 deletions
87
tests/closure/tclosure0.nim
Normal file
87
tests/closure/tclosure0.nim
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
discard """
|
||||
output: '''foo88
|
||||
23 24foo 88
|
||||
18
|
||||
18
|
||||
99
|
||||
99
|
||||
99
|
||||
99 99
|
||||
99 99
|
||||
12 99 99
|
||||
12 99 99'''
|
||||
"""
|
||||
|
||||
when true:
|
||||
# test simple closure within dummy 'main':
|
||||
proc dummy =
|
||||
proc main2(param: int) =
|
||||
var fooB = 23
|
||||
proc outer(outerParam: string) =
|
||||
var outerVar = 88
|
||||
echo outerParam, outerVar
|
||||
proc inner() =
|
||||
block Test:
|
||||
echo fooB, " ", param, outerParam, " ", outerVar
|
||||
inner()
|
||||
outer("foo")
|
||||
main2(24)
|
||||
|
||||
dummy()
|
||||
|
||||
when true:
|
||||
proc outer2(x:int) : proc(y:int):int = # curry-ed application
|
||||
return proc(y:int):int = x*y
|
||||
|
||||
var fn = outer2(6) # the closure
|
||||
echo fn(3) # it works
|
||||
|
||||
var rawP = fn.rawProc()
|
||||
var rawE = fn.rawEnv()
|
||||
|
||||
# A type to cast the function pointer into a nimcall
|
||||
type
|
||||
TimesClosure = proc(a: int, x: pointer): int {.nimcall.}
|
||||
|
||||
# Call the function with its closure
|
||||
echo cast[TimesClosure](rawP)(3, rawE)
|
||||
|
||||
when true:
|
||||
proc outer =
|
||||
var x, y: int = 99
|
||||
proc innerA = echo x
|
||||
proc innerB =
|
||||
echo y
|
||||
innerA()
|
||||
|
||||
innerA()
|
||||
innerB()
|
||||
|
||||
outer()
|
||||
|
||||
when true:
|
||||
proc indirectDep =
|
||||
var x, y: int = 99
|
||||
proc innerA = echo x, " ", y
|
||||
proc innerB =
|
||||
innerA()
|
||||
|
||||
innerA()
|
||||
innerB()
|
||||
|
||||
indirectDep()
|
||||
|
||||
when true:
|
||||
proc needlessIndirection =
|
||||
var x, y: int = 99
|
||||
proc indirection =
|
||||
var z = 12
|
||||
proc innerA = echo z, " ", x, " ", y
|
||||
proc innerB =
|
||||
innerA()
|
||||
|
||||
innerA()
|
||||
innerB()
|
||||
indirection()
|
||||
|
||||
needlessIndirection()
|
||||
73
tests/iter/tclosureiters.nim
Normal file
73
tests/iter/tclosureiters.nim
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
discard """
|
||||
output: '''0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
5 5
|
||||
7 7
|
||||
9 9
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
1
|
||||
2'''
|
||||
"""
|
||||
|
||||
when true:
|
||||
proc main() =
|
||||
let
|
||||
lo=0
|
||||
hi=10
|
||||
|
||||
iterator itA(): int =
|
||||
for x in lo..hi:
|
||||
yield x
|
||||
|
||||
for x in itA():
|
||||
echo x
|
||||
|
||||
var y: int
|
||||
|
||||
iterator itB(): int =
|
||||
while y <= hi:
|
||||
yield y
|
||||
inc y
|
||||
|
||||
y = 5
|
||||
for x in itB():
|
||||
echo x, " ", y
|
||||
inc y
|
||||
|
||||
main()
|
||||
|
||||
|
||||
iterator infinite(): int {.closure.} =
|
||||
var i = 0
|
||||
while true:
|
||||
yield i
|
||||
inc i
|
||||
|
||||
iterator take[T](it: iterator (): T, numToTake: int): T {.closure.} =
|
||||
var i = 0
|
||||
for x in it():
|
||||
if i >= numToTake:
|
||||
break
|
||||
yield x
|
||||
inc i
|
||||
|
||||
# gives wrong reasult (3 times 0)
|
||||
for x in infinite.take(3):
|
||||
echo x
|
||||
|
||||
# does what we want
|
||||
let inf = infinite
|
||||
for x in inf.take(3):
|
||||
echo x
|
||||
Loading…
Add table
Add a link
Reference in a new issue