Juxtaposition

Started by jopython, November 11, 2012, 04:49:56 PM

Previous topic - Next topic

jopython

The clojure.core has this juxtapostion function which i find very useful. I wonder how easy is it to implement in newLisp.



Takes a set of functions and returns a fn that is the juxtaposition
of those fns. The returned fn takes a variable number of args, and
returns a vector containing the result of applying each fn to the
args (left-to-right).
e.g
((juxt a b c) x) => [(a x) (b x) (c x)]


I am not worried whether this function returns a vector or a list.

rickyboy

#1
Try this on for size.  Maybe there is a better way though.


(define (juxt)
  (letex (_args (args))
    (lambda (x)
      (let (fs '_args)
        (map (lambda (f) (f x)) fs)))))
(λx. x x) (λx. x x)

jopython

#2
That is amazing.

Someday I will also get to learn the black art of macros.