Skip to content

Instantly share code, notes, and snippets.

@acobster
Last active February 8, 2020 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acobster/fa5eab1ca56ecf591cc953793bbe51b1 to your computer and use it in GitHub Desktop.
Save acobster/fa5eab1ca56ecf591cc953793bbe51b1 to your computer and use it in GitHub Desktop.
(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)))))
(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