Arc Forumnew | comments | leaders | submitlogin
1 point by d0m 5137 days ago | link | parent

Thank you, I get it.. And yeah, I think it could be cool if we could concat string instead of only characters in map.

Maybe a way to do that would be to coerce the string to a list (as you did), to use a normal map on this, and then, to join the new list with (string)

(apply string (map .. (coerce "test" 'cons)))

(And by the way, I didn't know about '(coerce string 'cons))



2 points by aw 5137 days ago | link

Here's a mapstr which does this:

  (def mapstr (f . seqs)
    (string (accum a (for i 0 (- (apply min (map1 len seqs)) 1)
                       (a (string (apply f (map1 [_ i] seqs))))))))

  arc> (mapstr [string "." _] "test")
  ".t.e.s.t"
And we can plug that into the definition of map:

  (def map (f . seqs)
    (if (some [isa _ 'string] seqs) 
         (apply mapstr f seqs)
        ;; the rest is the same as the arc3.1 def
        (no (cdr seqs)) 
         (map1 f (car seqs))
        ((afn (seqs)
          (if (some no seqs)  
              nil
              (cons (apply f (map1 car seqs))
                    (self (map1 cdr seqs)))))
         seqs)))

  arc> (map [string "." _] "test")
  ".t.e.s.t"

-----

3 points by aw 5137 days ago | link

Though you know, it's funny, my brain doesn't actually like it that map returns different things depending in the types of its arguments. In the same way that a long run-on sentence is annoying if I can't tell what's it's saying until the end, if I see a map I don't know if it's returning a list or a string or whatever until I've looked at its arguments and seen what they are.

So I might like map to always return a list, but still have it treat a string as if it were a list of characters. The example then becomes:

  (string:map [string "." _]  "test")

-----

1 point by d0m 5137 days ago | link

Cool, I've learn a couple of things by reading this. (Mainly accum). Do you think modifying map this way could break things?

-----

1 point by aw 5137 days ago | link

Assuming that I didn't introduce some gratuitous bugs, it's safe because it works the same for programs which don't throw an error in Arc 3.1.

-----