Arc Forumnew | comments | leaders | submitlogin
4 points by twilightsentry 5498 days ago | link | parent

  > I'm sure there must be a better way to initialize the
  > count in Arc, but I couldn't figure one out.
Table lookups can take a default value as a third arg, so you could do something like:

  (def hist (seq)
    (w/table h
      (each n seq
        (++ (h n 0)))
      h))


3 points by palsecam 5497 days ago | link

FTR, 'hist could be just:

  (def hist (seq)
    (w/table h
      (each n seq
        (++ (h n 0)))))
I just learned about 'w/table reading your post, and I checked, and it returns the created hash. This is quite smart, I often find myself doing things like:

  (let h (table)
    ...  ; do stuff with h
    h)  ; return it
Where it's actually possible to do:

  (w/table h
    ...)  ; implicit return of "h"
But yes, here, use 'counts.

-----

4 points by aw 5498 days ago | link

Also see "counts" in arc.arc:

  arc> (counts '(a b c a a a c))
  #hash((a . 4) (c . 2) (b . 1))

-----

3 points by kens 5497 days ago | link

Oh, yeah, I forgot that Arc 3 has default values for table. Someone should write some documentation on this :-)

Thanks for the suggestions; I've updated my blog post.

-----