Each versus for-each and for-all

Something else I've been thinking about (that @BlackATTR has brought up in the past) is the fact that you often want to know when you're on the first or last step of an iteration.

There could be some sensitivity in EACH to whether the code it is running has a /FIRST or /LAST refinement (which bolsters my point about first of and last of):

>> e: each x [1 2 3] func [value /first /last] [
    if first [print "first!"]
    print [value]
    if last [print "last!"]
]

>> for :e
first!
1
2
3
last!

This could be shortened with a lambda syntax:

>> for each x [1 2 3] value/first/last -> [
    if first [print "first!"]
    print [value]
    if last [print "last!"]
]

first!
1
2
3
last!

But maybe it's EACH that should be parameterized, and they should be normal arguments, so you don't get pinned to the names:

>> for each/first/last x [1 2 3] [val fst lst] -> [
    if fst [print "first!"]
    print [value]
    if lst [print "last!"]
]

first!
1
2
3
last!

There could be a lambda shorthand for that too, maybe TUPLE!

>> for each/first/last x [1 2 3] val.fst.lst -> [
    if fst [print "first!"]
    print [value]
    if lst [print "last!"]
]

first!
1
2
3
last!

Just one of the kinds of axes to explore with generalized EACH.

2 Likes