Juxtaposition

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Juxtaposition

Post by jopython »

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

Code: Select all

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
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Juxtaposition

Post by rickyboy »

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

Code: Select all

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

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Juxtaposition

Post by jopython »

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

Locked