Oops, I realized I only answered half of the question. The scala for
comprehension does operate only on iterables.  While it has quite a bit of
flexibility over the size of the collection or stream over which it is
iterating, it still is just an iterator.

In order to achieve the ability to determine the looping conditions from
within the loop, one could define similar "for" syntax as sugar for a
looping or tail recursive construct.

e.g.

for (pred) { ... }  would generate a self recursive anonymous function that
would run  to the end of ... then call itself again if pred is true.
we introduce continue and break,  continue would simply perform the self
invocation early, while break would return.

We could even go so far as to share scala's yield syntax, in which the for
loop actually behaves like a coroutine/generator and produces items in a
list, or feeds values to a coroutine consumer.  (Slightly OT but since we
are discussing loops and iterators, I think we should be sure that bitC
supports lazy lists, and coroutines seem like they would be the most
efficient and flexible route. )

My informal loop grammar follows:

There would be three different result types for the for loop.  Unit, a
scalar, or a vector.  It might be possible to infer these types, but it
would probably be best to be explicit about the return type. I suggest that
the result type follow the for declaration but before the body.

There would also be 2 different predicates for a loop, the for comprehension
over a collection, and the imperative style conditional loop.

So a loop over a collection that returns a new collection (map) would be
for (a <- b ) : collection[`c]
{...; yield c; }

or

for(a <- b | a % 2 == 0) : collection['c]
{...; yield c; }

A conditional loop which returns nothing

for (a <= 10)
{... }

or

for (a <= 10) : void
{ ... }

a loop over a collection which returns a scalar
for (a <- b) : `c
{
  if (a == 7)
    return "found";   // alternately:  break "found";  to keep with the
nomenclature
}

a conditional loop which returns a collection
for (i < 10) : collection[`c]
{ ... yield c; }


Things to note:

If the return type is a collection, the body must contain a yield (perhaps
in the future it could be inferred if the last expression in the body
matched the appropriate type)

The body of the scalar result type must result in an expression, or have no
paths covered by break statements which return the result.


Slightly bothersome is the fact that <- looks a lot like <=. So we should
probably find a replacement for <-, perhaps <-- is enough.



On Mon, Mar 22, 2010 at 3:58 PM, Rick Richardson
<[email protected]>wrote:

>
>
>> Suggestions? Other languages to look at?
>>
>>
> I like Scala's for loop syntax because it's technically a list
> comprehension, but it has syntax that is familiar for imperative
> programmers. It does actually apply its block as a closure to each
> (filtered) item in the list, so it can filter and generate new types just
> like a list comprehension, but it has the ability to return Unit (void) as
> well. So it can be used in both a functional and imperative style.
>
> It also has the ability to operate on any Iterable, which means that it
> will work on generators or other lazy/infinite lists.
>
> That said, I almost always tend to use the builtin methods of scala's
> collection classes such as map, flatMap and foreach.
>
>
> http://www.scala-lang.org/node/111
>
> http://www.codecommit.com/blog/scala/infinite-lists-for-the-finitely-patient
>
>
I realize
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to