Skip to content

TM002 'for' syntactic construct for iteration

Daniel Patterson edited this page May 30, 2013 · 6 revisions

Motivation

To support 'for' style iteration (ie, map, fold, filter) in a generalizable and Pyret-friendly way.

Examples

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

Desugaring Specification

(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]*)

Generalized examples:

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

Resolution

Implemented at of 67c1a477b5a3525e99af2cca1418009527bcd12e

Clone this wiki locally