Skip to content

Instantly share code, notes, and snippets.

@rafd
Created September 2, 2021 01:09
Show Gist options
  • Save rafd/66978a5c66a56b6bc18e1b56d46f6e61 to your computer and use it in GitHub Desktop.
Save rafd/66978a5c66a56b6bc18e1b56d46f6e61 to your computer and use it in GitHub Desktop.
Clojure Meetup September
(ns exercises.september
(:require [criterium.core :as criterium]))
#_(+ 1 2 3)
#_(println "asdasd")
(defn clamp [v min max]
(cond
(< v min) min
(< max v) max
:else v))
(defn clamp2 [v min max]
(clojure.core/max min (clojure.core/min v max)))
#_(clamp2 5 0 10)
#_(clamp2 -1 0 10)
#_(clamp2 11 0 10)
#_(criterium/quick-bench
(do
(clamp2 -2 0 10)
(clamp2 -1 0 10)
(clamp2 -3 0 10)))
#_(criterium/quick-bench
(do
(clamp 6 0 10)
(clamp 7 0 10)
(clamp 8 0 10)))
(name :foo/barl)
(namespace :foo/bar)
;; TODO list
;; add
;; "checking off"
;; listing all (w/ option to ignore completed)
(defonce tdl
(atom
[{:task/desc "a"
:task/status :task.status/open}
{:task/desc "b"
:task/status :task.status/open}
{:task/desc "c"
:task/status :task.status/closed}]))
(defn add-todo! [i]
(swap! tdl #(conj % {:task/desc i :task/status :open})))
(defn add-todo2! [i]
(swap! tdl conj {:task/desc i :task/status :open}))
#_(add-todo! "new task")
(defn complete-todo! [i]
(->> @tdl
(map #(if (= (:task/desc %) i)
(assoc % :task/status :closed)
%))
(reset! tdl)))
(defn complete-todo-swap! [i]
(swap! tdl (partial map (fn [task]
(if (= (:task/desc task) i)
(assoc task :task/status :closed)
task)))))
#_(complete-todo! "new task")
(defn list-todos [tdl]
(when (seq tdl)
(println (first tdl))
(recur (rest tdl))))
(defn list-todos2 [status tdl]
(map println
(if (= status :all)
@tdl
(filter (fn [task]
(= status (:task/status task)))
@tdl))))
(defn list-todos3 [status tdl]
(map println
(filter (fn [task]
(or (= status :all)
(= status (:task/status task))))
@tdl)))
(defn list-todos4 [status tdl]
(->> @tdl
(filter (fn [task]
(or (= status :all)
(= status (:task/status task)))))
(map println)))
(defn list-todos5 [status tdl]
(->> @tdl
(filter (fn [task]
(#{:all (:task/status task)} status)))
(map println)))
(defn filter-by [k f coll]
(filter (fn [item]
(f (k item))) coll))
(defn filter-by2 [k f coll]
(filter (comp f k) coll))
(defn list-todos6 [status tdl]
(->> @tdl
(filter-by2 :task/status (fn [s] (or (= :all status)
(= s status))))
(map println)))
#_(list-todos (deref tdl))
#_(list-todos6 :open tdl)
#_(list-todos2 :closed tdl)
#_(list-todos2 :all tdl)
#_tdl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment