Arc Forumnew | comments | leaders | submitlogin
Is it just me, or is zap's behaviour kinda weird?
5 points by NickSmith 6147 days ago | 5 comments
Intuitively, this is how I would expect zap to behave:

   arc> (= x '(1 2 3))
   (1 2 3)

   arc> (map [+ _ 10] x)
   (11 12 13)
   arc> x
   (1 2 3)

   arc> (zap [+ _ 10] x)
   (11 12 13)
   arc> x
   (11 12 13)
Except it doesn't. The expression (zap [+ _ 10] x) above throws an error. I am new to programming, so apologies in advance if I am missing something obvious.


8 points by absz 6147 days ago | link

It looks like your confusion is about what zap does. zap is not a destructive variant of map; zap has no relation to map at all. What zap is is a generalized version of =. You often see expressions of the form (= foo (bar foo)), and zap abstracts them and add redundancy. When you write (zap func name), Arc will expand that into (= name (func name)). So in your first example, (zap [+ _ 10] x) turned into (= x ([+ _ 10] x)), which is the same as (= x (+ x 10)); x is the list '(1 2 3), and you can't add a number to a list, so you get an error. Cchooper's example, on the other hand, turns into (= x ([map [+ _ 10] _] x)), which is the same as (= x (map [+ _ 10] x)), which, as you can see, does what you want.

-----

3 points by NickSmith 6147 days ago | link

Ahhh... now I see! It irked me a little that I knew the answer but didn't fully understand the reasoning. Thank you absz, this is really helpful.

-----

2 points by absz 6147 days ago | link

Glad to be of assistance :)

-----

8 points by cchooper 6147 days ago | link

You need:

  (zap [map [+ _ 10] _] x)
P.S. Welcome to the programming club :)

-----

3 points by NickSmith 6147 days ago | link

Thanks cchooper. I guess if I'm going to get good at this then I'd be better leaving my expectations behind ;)

-----