src/bookofnim/helloworld/modules/loopsIterator

Source   Edit  

loops and iterators

TLDR

  • iterators are included, as their only used with loops
    • the parallel iterator is in asyncParMem

links

TODOs

loop/iterator related procs

  • finished determine if a first class iterator has finished
  • countup == .. == ..< (zero index countup)
  • countdown == ..^ == ..^1 (zero index countdown)
  • items for i blah.items: always called if only 1 identifer is used
  • pairs for i,z blah.pairs: always called if two identifiers are used
  • low(blah) .. high(blah)
  • lines(somefile) each line in the file

iterators

  • inlined at the callsite when compiled
    • do not have the overhead from function calling
    • prone to code bloat
    • useful for defining custom loops on complex objects
  • can be used as operators if you enclose the name in back ticks
  • can be wrapped in a proc with the same name to accumulate the result and return it as a seq
  • distinction with procs
    • can only be called from loops
    • uses yield instead of return keyword
    • doesnt have an implicit result
    • dont support recursion
    • cant be forward declared

Vars

finishedCorrect = mycount
Source   Edit  
finishedIncorrect = mycount
Source   Edit  
num6 = 0
Source   Edit  

Lets

intArr = [5, 4, 3, 2, 1]
Source   Edit  

Iterators

iterator `...`[T](a: T; b: T): T
Source   Edit  
iterator countTo(n: int): int {....raises: [], tags: [], forbids: [].}
Source   Edit  
iterator mycount(a, b: int): int {.closure, ...raises: [], tags: [], forbids: [].}
Source   Edit  
iterator myIter(): int {....raises: [], tags: [], forbids: [].}
Source   Edit