Last active
February 8, 2020 06:54
-
-
Save acobster/fa5eab1ca56ecf591cc953793bbe51b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns core) | |
(defn- keyed-by [coll f] | |
(into {} (map (fn [x] [(f x) x]) coll))) | |
(defn- vconj [coll & xs] | |
(vec (apply conj coll xs))) | |
(def people [{:name "Jeff" | |
:instrument "Mbira"} | |
{:name "Raven" | |
:instrument "Drums"} | |
{:name "Coby" | |
:instrument "Bass"}]) | |
(def songs [{:name "Brand New Day" | |
:writer "Jeff"} | |
{:name "Piece of Mind" | |
:writer "Jeff"} | |
{:name "Nearly No" | |
:writer "Coby"}]) | |
(defn merge-related [originators belongings from to unite] | |
(let [keyed (keyed-by originators from)] | |
(vals (reduce (fn [origs belonging] | |
(update origs (to belonging) unite belonging)) | |
keyed | |
belongings)))) | |
(defn people->songwriters [ppl songs] | |
(merge-related ppl songs :name :writer (fn [writer song] | |
(update writer :songs vconj (:name song))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns lando-clojure.handler-test | |
(:require [clojure.test :refer :all] | |
[core])) | |
(deftest merges-maps-together | |
(is (= [{:name "Jeff" | |
:instrument "Mbira" | |
:songs ["Brand New Day" "Piece of Mind"]} | |
{:name "Raven" | |
:instrument "Drums"} | |
{:name "Coby" | |
:instrument "Bass" | |
:songs ["Nearly No"]}] | |
(core/people->songwriters subject/people subject/songs)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment