Arc Forumnew | comments | leaders | submitlogin
How about (each (k v) table ...)
9 points by CatDancer 5685 days ago | 6 comments
Whenever I need to iterate through the keys and values of some table g, my brain seems to naturally reach for

  (each (k v) g
    ... do stuff with k and v ...)
Currently there's ontable to do that, and each called with a table iterates through the values. Code using ontable does have two less parentheses

  (ontable k v g
    ... do stuff with k and v ...)
though that seems to be offset for me by having one less syllable in "each".

I don't know how often each is currently called with tables in the news code, but I thought I'd throw it out there as I seem to need to iterate through keys and values a lot and rarely (never?) needed to iterate just through the values.



5 points by pg 5682 days ago | link

Ok, I've changed each to work this way. I couldn't find a single place where I depended on the old behavior, and this will let me get rid of ontable, which I've always been dubious about.

-----

2 points by CatDancer 5682 days ago | link

Oh good, my brain is happy now :)

-----

1 point by conanite 5685 days ago | link

"me too!!!"

I totally have to remind myself every time that it's "ontable" for tables, not "each"

The (each (k v) h ...) syntax feels like a natural destructuring way to iterate over a table. And the cost of an extra pair of parens is more than offset by having an each that behaves the way you'd expect it to.

-----

3 points by rntz 5685 days ago | link

Thirded. I'd very much like to see this get into arc3's final release. This makes iterating over a given table just like iterating over the corresponding association list.

It's a single-line patch:

    diff --git a/arc.arc b/arc.arc
    index a2a6a8d..ac5ea3e 100644
    --- a/arc.arc
    +++ b/arc.arc
    @@ -467,7 +467,7 @@
                      (self (cdr ,g))))
                  ,gseq)
                (isa ,gseq 'table)
    -            (maptable (fn (,g ,var) ,@body)
    +            (maptable (fn ,var ,@body)
                           ,gseq)
                 (for ,g 0 (- (len ,gseq) 1)
                   (let ,var (,gseq ,g) ,@body))))))

-----

1 point by CatDancer 5682 days ago | link

This makes iterating over a given table just like iterating over the corresponding association list

That's a cool point!

If calling a list wasn't already doing a lookup by position, I might like a call on a cons to do an alref so that I could use x!foo to do a lookup by 'foo whether x was a table or an assoc list.

-----

2 points by CatDancer 5685 days ago | link

It's a single-line patch if no other code in Arc/News uses each with tables...

-----