Arc Forumnew | comments | leaders | submitlogin
List comprehensions in Arc
5 points by musk_fan 802 days ago | 6 comments

I really love Python's List Comprehensions: l = [i for i in range(1, 10)] If this can be included in Arc, that would be great. If it's in the documentation but just not yet in the tutorial, please add it to the tutorial ( Thanks! I love Arc; it's so beautiful and tiny


5 points by akkartik 797 days ago | link

musk_fan, your initial attempt inspired me to build on it :) Now that we can enumerate from a start number to an end, I hanker after something more comprehensive. Also, after reading malisper's description of iterate at, I thought I'd try to mimic its syntax, in hopes that it'll fit better with a Lisp and be extensible.

Here's what I ended up with: Is the description clear?


3 points by akkartik 771 days ago | link

List comprehensions are now in Anarki:


5 points by i4cu 801 days ago | link

I don't know python per say, but I don't believe arc supports them. I do know the iterator functions are there for you:


4 points by akkartik 801 days ago | link

Yes, Arc doesn't come with list comprehensions. But it sounds like a fun exercise to build say a collect macro:

    (collect x for x in xs if odd.x)
I think it may be similar to the loop macro in Common Lisp. There's a Common Lisp implementation of loop at (via by malisper)


3 points by musk_fan 797 days ago | link

I wrote a simple version that only works like so (l 0 9) == '(0 1 2 3 4 5 6 7 8 9):

     (def l (start end)
       (let lis `(1)
         (pop lis)
         (while (<= start end)
           (= lis `(,@lis ,start))
           (++ start))
The (collect ...) structure is really cool; I'd forgotten about that; it's been awhile since I touched CLisp


2 points by akkartik 797 days ago | link

Great! A few comments:

1. Is there a reason you start with `(1) and then pop? Why not just initialize lis to nil?

2. Here's a slightly more idiomatic implementation (also more efficient, because it avoids deconstructing and reconstructing lis on every iteration):

    (def l (start end)
      (accum acc
        (for i start (<= i end) ++.i
Read more about accum and for:

    arc> (help accum)
    arc> (help for)
(I'm assuming Anarki in the above example. The Arc version is slightly different.)