August 21, 2018

Just Juxt #7: Flipping Out (4clojure #46)

Guy flipping pancakes

Write a higher-order function which flips the order of the arguments of an input function.

Here's a live testing area for you to play with. See if you can solve it with juxt. Then check out the answer below!

(ns live.test
  (:require [cljs.test :refer-macros [deftest is testing run-tests]]))
  
(defn flipper [f]
  )

(deftest test-46
  (is (= 3 ((flipper nth) 2 [1 2 3 4 5])))
  (is (= true ((flipper >) 7 8)))
  (is (= 4 ((flipper quot) 2 8)))
  (is (= [1 2 3] ((flipper take) [1 2 3 4 5] 3))))
  
(run-tests)

First, a common answer:

(defn flipper [f]
  #(f %2 %))
  
(run-tests)

Ok... Now I'm actually wondering if we're being trolled by maximental:

(defn flipper [f]
  ((partial partial
           (comp (partial apply apply)
                 (juxt first
                       (comp (juxt second first)
                             rest))
                 list)) f))
                 
(run-tests)

It's a juxt within a juxt!

Run... they're multiplying!

Tags: coding exercises KLIPSE 4clojure Cryogen juxt