September 13, 2018

Just Juxt #30: Symmetric Difference (4clojure #88)

Symmetric Difference

Write a function which returns the symmetric difference of two sets. The symmetric difference is the set of items belonging to one but not both of the two sets.

(ns live.test
  (:require [cljs.test :refer-macros [deftest is testing run-tests]]))
  
(defn sym-diff [a b]
  (set (mapcat identity
               ((juxt #(remove % %2)
                      #(remove %2 %)) a b))))

(deftest sym-diff-test
  (is (= (sym-diff #{1 2 3 4 5 6} #{1 3 5 7}) #{2 4 6 7}))
  (is (= (sym-diff #{:a :b :c} #{}) #{:a :b :c}))
  (is (= (sym-diff #{} #{4 5 6}) #{4 5 6}))
  (is (= (sym-diff #{[1 2] [2 3]} #{[2 3] [3 4]}) #{[1 2] [3 4]})))
  
(run-tests)
Tags: coding exercises KLIPSE 4clojure Cryogen juxt