- 
                Notifications
    
You must be signed in to change notification settings  - Fork 120
 
TM002 'for' syntactic construct for iteration
        Daniel Patterson edited this page May 30, 2013 
        ·
        6 revisions
      
    To support 'for' style iteration (ie, map, fold, filter) in a generalizable and Pyret-friendly way.
for each(sublist from lists):
  sublist.length()
end
# ==> given a list of lists, returns a list of the lengths of each sublist
for acc(sum from 0, elt from numbers):
  sum.plus(elt)
end
# ==> given a list of numbers, returns the sum
for filter(num from lst):
  num.greaterthan(10)
end
# ==> given a list of numbers, returns those bigger than 10
(note: all e-foo are expressions, distinguished only to make more readable)
for e-fun(binding from e-arg [, binding from e-arg]*) [-> annotation]:
  e-body
end
===>
e-fun(\binding[, binding]* [-> annotation]: (e-body), e-arg [, e-arg]*)
fun delay(thunk, n):
  sleep(n)
  thunk(n)
end
for delay(n from 10):
  print("Hello!")
end
# ==> waits 10 seconds and then prints Hello.
fun every-ten-seconds(thunk):
  sleep(10)
  thunk()
  every-ten-seconds(thunk)
end
for every-ten-seconds():
  print("Hello")
end
# ==> every ten seconds prints Hello
# ... anything else you can think of
Implemented at of 67c1a477b5a3525e99af2cca1418009527bcd12e