tcapture.nim 536 B

1234567891011121314151617181920212223242526272829303132333435
  1. discard """
  2. output: '''
  3. to be, or not to be
  4. (v: 1)
  5. (w: -1)
  6. (v: 1)
  7. (w: -1)
  8. '''
  9. joinable: false
  10. """
  11. import sequtils, sugar
  12. let m = @[proc (s: string): string = "to " & s, proc (s: string): string = "not to " & s]
  13. var l = m.mapIt(capture([it], proc (s: string): string = it(s)))
  14. let r = l.mapIt(it("be"))
  15. echo r[0] & ", or " & r[1]
  16. type
  17. O = object
  18. v: int
  19. U = object
  20. w: int
  21. var o = O(v: 1)
  22. var u = U(w: -1)
  23. var execute: proc()
  24. capture o, u:
  25. execute = proc() =
  26. echo o
  27. echo u
  28. execute()
  29. o.v = -1
  30. u.w = 1
  31. execute()