Skip to content

Instantly share code, notes, and snippets.

@luxbock
Created June 23, 2016 09:11
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 luxbock/f93529b82792ef35db12fcf5e5a78fdb to your computer and use it in GitHub Desktop.
Save luxbock/f93529b82792ef35db12fcf5e5a78fdb to your computer and use it in GitHub Desktop.
(transform
[MAP-VALS
(view frequencies)
(collect-one (view #(apply max (vals %))))]
(fn [mx fs] (transform MAP-VALS #(/ % mx) fs))
{:a [1 1 3 4] :b [1 1 2 2 2 3]})
;; => {:a {1 1, 3 1/2, 4 1/2}, :b {1 2/3, 2 1, 3 1/3}}
;; could in theory be written as:
(xform-> {:a [1 1 3 4] :b [1 1 2 2 2 3]}
MAP-VALS
(view frequencies)
(collect-one (view #(apply max (vals %))))
(fn [mx fs]
(xform-> fs MAP-VALS #(/ % mx))))
;; could in theory be written as:
(defni normalize
"Returns `m` with each of its values divided by `mx`."
[mx (m map-vals)]
(/ m mx))
(defni map-vals-normalize
"Destructures the view of `frequencies` over the values of the passed map to
`m` and the max value of `m`'s values as `mx`. Returns a map of normalized
frequency maps for the values of the map passed in."
[([m mx] map-vals
(view frequencies)
(collect-one (view #(apply max (vals %)))))]
(normalize mx m))
;; where a list inside the argument vector represents applying a selector to the
;; passed in value and destructuring its results to the symbols in the first
;; element of the list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment