Created
July 27, 2022 17:39
-
-
Save plexus/8c37aec5f53a00f795d84455676f5a8c to your computer and use it in GitHub Desktop.
This file contains 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 the-clouncil) | |
(def posts | |
(sort-by :date (read-string (slurp "posts.edn")))) | |
;; Original version: mapcat + reduce | |
;; <-- in | code | out --> | |
(->> posts | |
(mapcat (fn [{:keys [categories] :as post}] | |
(map (fn [category] | |
[category post]) | |
categories))) | |
(reduce (fn [acc [category post]] | |
(update acc category #(conj % post))) | |
{})) | |
;; ->>, map, filter, remove, distict, reduce | |
(->> posts | |
(mapcat (fn [{:keys [categories] :as post}] | |
(map (fn [category] | |
[category post]) | |
categories)))) | |
;; composability? | |
;; - it's all just values-in-values-out | |
;; - but syntax-wise? ->> / xforms / ... | |
(for [post posts | |
category (:categories post)] | |
[category post]) | |
(for [i (range 10) | |
c ["-" "=" "+"]] | |
(str c i c)) | |
;; "cartesion product" | |
(map | |
(fn [i] (str "-" i "-")) | |
(range 10)) | |
;; posts x categories | |
;; "cartesian product" | |
(let [categories (distinct (mapcat :categories posts))] | |
(into {} | |
(map | |
(fn [category] | |
[category | |
(filter | |
#(contains? (:categories %) category) | |
posts)]) | |
categories))) | |
;; avoid reduce? | |
;; - reduce is super powerful! | |
;; - but often there's a simpler thing that you can use | |
;; reduce version | |
(reduce | |
(fn [acc {:keys [categories] :as post}] | |
(reduce | |
(fn [acc category] | |
(update acc category conj post)) | |
acc | |
categories)) | |
{} | |
posts) | |
(update-vals | |
(->> (for [post posts | |
category (:categories post)] | |
[category post]) | |
(group-by first)) | |
) | |
;; for + group-by | |
(update-vals | |
(group-by | |
first | |
(for [post posts | |
category (:categories post)] | |
[category post])) | |
#(map second %)) | |
;; maybe nicer with reduce? | |
(reduce | |
(fn [acc [cat post]] | |
(update acc cat conj post)) | |
{} | |
(for [post posts | |
category (:categories post)] | |
[category post])) | |
;; debugging inside a #(...) | |
#(do | |
(println ...) | |
(inc %)) | |
#(inc (doto % println)) | |
(def posts [{:title "Blambda!" | |
:file "2022-07-03-blambda.md" | |
:categories #{"aws" "s3" "lambda" "clojure"} | |
:date "2022-07-03"} | |
{:title "Dogfooding Blambda! : revenge of the pod people" | |
:file "2022-07-04-dogfooding-blambda-1.md" | |
:categories ["aws" "s3" "lambda" "clojure" "blambda"] | |
:date "2022-07-04"}]) | |
;; challenge = posts x categories | |
;; cartesian product | |
;; -> for (list comprehension) | |
(group-by :categories posts) | |
(group-by (fn [p] | |
(get p :categories)) | |
posts) | |
(def m {"category" "clojure"}) | |
(get m "category") | |
(m "category") | |
(get m "catxxegory" "no category") | |
(m "category" "no category") | |
(def category->id | |
{"clojure" 132 | |
"aws" 456}) | |
(defn category->id [id] | |
,,,) | |
(category->id "clojure") | |
(assoc nil :foo :bar) | |
(conj nil :hello) | |
(group-by (fn [p] | |
(:categories p)) | |
posts) | |
(group-by (fn [i] | |
(mod i 3)) | |
(range 20)) | |
;;=> | |
{0 [0 3 6 9 12 15 18], | |
1 [1 4 7 10 13 16 19], | |
2 [2 5 8 11 14 17]} |
This file contains 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
[{:title "Blambda!" | |
:file "2022-07-03-blambda.md" | |
:categories #{"aws" "s3" "lambda" "clojure"} | |
:date "2022-07-03"} | |
{:title "Dogfooding Blambda! : revenge of the pod people" | |
:file "2022-07-04-dogfooding-blambda-1.md" | |
:categories #{"aws" "s3" "lambda" "clojure" "blambda"} | |
:date "2022-07-04"} | |
{:title "Hacking the blog: favicon" | |
:file "2022-07-05-hacking-blog-favicon.md" | |
:categories #{"clojure" "blog"} | |
:date "2022-07-05"} | |
{:title "Hacking the blog: categories" | |
:file "2022-07-06-hacking-blog-categories.md" | |
:categories #{"clojure" "blog"} | |
:date "2022-07-06"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment