Arc Forumnew | comments | leaders | submitlogin
2 points by fallintothis 3855 days ago | link | parent

Still to do: make it varargs along both dimensions (xs and fs).

What do you mean by this? Something like the following?

  arc> (-> 1 2 /)
  2
From reading Clojure's source (https://raw.github.com/clojure/clojure/c6756a8bab137128c8119...), I think the following is an Arc translation of their macro:

  (mac -> (x . rest)
    (if (no rest)
        x
        (let (expr . more) rest
          (if more         `(-> (-> ,x ,expr) ,@more)
              (acons expr) `(,(car expr) ,x ,@(cdr expr))
                           (list expr x)))))
But it seems pretty screwy:

  arc> (macex '(-> 1 2 (fn (x y) (/ x y))))
  (fn (-> 1 2) (x y) (/ x y))
Probably a type system thing with Clojure's idea of seq?.

I like the simplicity of something like this:

  (mac -> exprs
    (reduce (fn (l r) (list r l)) exprs))
but it fails to capture the (-> 1 2 /) use-case.

Maybe this?

  (def -> exprs
    (reduce (fn (l r)
              (let ls (check l alist (list l))
                (if (~isa r 'fn) ; but what about callable objs?
                    (cons r ls)
                    (apply r ls))))
            exprs))

  arc> (def flip (f) (fn (y x) (f x y)))
  #<procedure: flip>
  arc> (-> 1 2 + [fn (x) (+ x _)] '(1 2 3) (flip map))
  (4 5 6)
Though at this point, may as well just model a stack machine in general. Or just use Factor. :)


1 point by akkartik 3854 days ago | link

Yeah, if I had to choose one axis over the other I'd rather have multiple functions than multiple arguments.

Here's the current varargs version of -> in wart:

  mac (transform x ... fs|thru)
    if no.fs
      x
      `(transform (,x -> ,car.fs) ,@cdr.fs)
Now you can call it like this:

  (transform 3 :thru (fn(_) (_ * 2))
                     (fn(_) (_ + 1)))
I think I'd support multiple args by using :thru as a delimiter:

  (transform 1 2 :thru (fn(a b) (a * b))
                       (fn(_) (_ + 1)))
But really, there's no need:

  (transform 1*2 :thru (fn(_) (_ + 1)))

-----

2 points by akkartik 3854 days ago | link

"Or just use Factor. :)"

I love how I could switch programming models for the one line in this example where it made sense: http://rosettacode.org/wiki/Rot-13#Wart

-----