tags:

views:

248

answers:

4

REBOL has no built-in way to perform list comprehensions. However, REBOL has a powerful facility (known as parse) that can be used to create domain-specific languages (DSLs). I've used parse to create such a mini-DSL for list comprehensions. In order to interpret the expression, the block containing the comprehension is passed to a function, which for lack of a better term I've called comprehend.

Example:

comprehend [(a * b) for a in 1x100 for b in 4x10 where (all [odd? a odd? b])]

For some reason, comprehend doesn't sound right to me, but something like eval is too general.

I haven't found any other language that requires a keyword or function for list comprehensions. They are pure syntactic sugar wherever they exist. Unfortunately I don't have that option. So, seeing that I must have a function, what's a good, succinct, logical name for it?

+1  A: 

Because list comprehensions can be thought of as analogous to map, you might think about calling it something like "listmap". Alternately, because list comprehensions are based on set-builder notation, you could call it something along the lines of "build" or "buildlist".

(Disclaimer: I know very little about REBOL, so forgive me if these names are already taken)

igowen
+2  A: 

How about select?

select [(a * b) for a in 1x100 for b in 4x10 where (all [odd? a odd? b])]

Ahnfelt
+1  A: 

do could be appropriate, as list comprehensions are just one instance of Monad comprehensions, and do is the keyword used in Haskell for sugared Monadic computations but I suspect it's too vague for a user library. I called my list comprehension function comp, but that's just an abbreviation of what you already have. Perhaps yielding? E.g. yielding [(a * b) for a in 1x100 for b in 4x10 where (all [odd? a odd? b])]. Just sort of squint and pretend the [ ] aren't there.

Logan Capaldo
Unfortunately DO is already a REBOL word.
Gregory Higley
+1  A: 

transmogrify

John