any all each
Posted: Sat Jul 26, 2008 8:11 pm
Here are some ideas I'm playing around with, presented here for comment:
Python has any(seq) and all(seq) functions that return true if any element of the sequence is true, or if all elements of the sequence are true. A post at http://eikke.com/python-all-odity/ gives some examples and also points out that there is some performance overhead for this expressiveness.
In Cobra, I'm thinking about making this a language level feature:
Because this is a language feature, the overhead discovered in that blog post can probably be eliminated by the compiler.
On a related note, I sometimes think that "first <seq>" and "last <seq>" would be interesting/useful. If so, might as well throw in a LISP-like "rest <seq>" (AKA "cdr").
I'm also contemplating an each keyword for convenience. Many times I find myself looping through a sequence just to invoke a method or collect the return values thereof, without any interest in the "loop control variable". Examples:
While 'each' is only a convenient syntax, I really like its succinctness. When I read "each customers.billIfNeeded" there is no decoding of intent or repeating of a var name. There's just less stuff to read and write.
Btw when I say "sequence" or "seq" I really mean anything IEnumerable including lists, arrays and any class that implements the IEnumerable interface such as Dictionary.keys or classes you write yourself.
Feedback is welcome.
-Chuck
Python has any(seq) and all(seq) functions that return true if any element of the sequence is true, or if all elements of the sequence are true. A post at http://eikke.com/python-all-odity/ gives some examples and also points out that there is some performance overhead for this expressiveness.
In Cobra, I'm thinking about making this a language level feature:
- Code: Select all
# form is:
# any <seq> ==> true or false
# all <seq> ==> true or false
print any for i in numbers get i > 100
assert all for i in numbers get i > 0 and i < 100
Because this is a language feature, the overhead discovered in that blog post can probably be eliminated by the compiler.
On a related note, I sometimes think that "first <seq>" and "last <seq>" would be interesting/useful. If so, might as well throw in a LISP-like "rest <seq>" (AKA "cdr").
I'm also contemplating an each keyword for convenience. Many times I find myself looping through a sequence just to invoke a method or collect the return values thereof, without any interest in the "loop control variable". Examples:
- Code: Select all
# form is:
# each <seq>.messages ==> <seq of .messages return values>
# can be statement or expression
each shapes.display
each customers.billIfNeeded
names = each names.trim
# 'each' causes Cobra to break off the method invocation and apply it to each element.
# It's a short cut for:
for cust in customers, cust.billIfNeeded
names = for name in names get name.trim
# Like 'for expressions', 'each expressions' could have a 'where':
names = each names.trim where .trim.length
While 'each' is only a convenient syntax, I really like its succinctness. When I read "each customers.billIfNeeded" there is no decoding of intent or repeating of a var name. There's just less stuff to read and write.
Btw when I say "sequence" or "seq" I really mean anything IEnumerable including lists, arrays and any class that implements the IEnumerable interface such as Dictionary.keys or classes you write yourself.
Feedback is welcome.
-Chuck