Skip to content

Instantly share code, notes, and snippets.

hard sell intro key points we know clojure let's leverage that ie functions. ideally pure ones across the repos, we should follow conventions unless there's a good reason not to what problems are there? duplicated code every project has some about of setup boilerplate

@henryw374
henryw374 / index-by.cljc
Created May 16, 2019 10:30
clojure index-by
(ns corr.core
(:require [medley.core :as m]))
(defn index-by [f coll]
(->> coll
(group-by f)
(m/map-vals (fn [xs]
(assert (= 1 (count (take 2 xs))))
(first xs)))))
@henryw374
henryw374 / tidy-ns-requires.clj
Last active May 14, 2019 13:06
clojure clojurescript namespace require tidy
(ns tidy-requires)
(defn tidy-requires [[ _ & requires]]
(cons :require (sort-by first requires)))
(comment
(tidy-requires '(:require [foo]
[bar] ))
@henryw374
henryw374 / tick_recipes.cljc
Last active June 30, 2021 05:35
juxt tick
;local date to native date (js/Date or java.util.Date.)
(-> d
(cljc.java-time.local-date/at-start-of-day (t/zone "UTC"))
(t/inst))
; native date (js/Date or java.util.Date.) to local date
(-> (java.util.Date.)
t/instant
(cljc.java-time.zoned-date-time/of-instant (t/zone "UTC"))
t/date)
(ns transducers)
;;;;; simplified traditional impls
(defn mymap [f coll]
(reduce
(fn [acc n]
(conj acc (f n)))
[]
coll))
@henryw374
henryw374 / ns-graph-data.clj
Created February 15, 2019 10:56
use ns-graph to create a function that returns all transitive dependencies of a namespace
(ns deps
"use https://github.com/alexander-yakushev/ns-graph
to create a function that returns all transitive dependencies of a namespace.
handy if you're trying to pull a ns out of a project
"
(:require [ns-graph.core :as nsg]
[clojure.java.io :as io]))
@henryw374
henryw374 / git.sh
Created November 16, 2018 12:12
some git aliases
alias commit='git commit -a -m '
alias glog='git log --pretty=oneline'
alias glog='git log --pretty=format:"%h%x09%an%x09%ad%x09%s"'
alias gsoft='git reset --soft HEAD~1'
alias gshowf='git show --name-only'
alias gshow='git diff-tree --no-commit-id --name-only -r '
alias gint='git rebase --interactive '
alias ginto='git rebase --interactive origin/master'
alias rgc='git rebase --continue'
alias rga='git rebase --abort'
@henryw374
henryw374 / weekdays.cljc
Last active September 24, 2018 07:03
calculate weekdays between 2 dates
(ns test-tick.weekdays
(:require [tick.alpha.api :as t]))
; calculate weekdays between 2 local dates with tick
; 2 methods, one is quite readable, the other is probably more environmentally friendly
; https://www.timeanddate.com/date/workdays.html
; java.time weekday ordinals
; 1 2 3 4 5 6 7
; M T W T F S S
; stack dump
(require '[clojure.stacktrace :as stck])
(defn jstack [n]
(let [threadMXBean (java.lang.management.ManagementFactory/getThreadMXBean)
info (.getThreadInfo threadMXBean (.getAllThreadIds threadMXBean) 100)]
(print
(for [threadInfo info]
(str
@henryw374
henryw374 / deflet.clj
Created May 3, 2018 07:24
def all variables in a let expression
;; handy when working at repl. something in a let statement is not working and so you want to def the variables so you can try
bits one by one
(defmacro deflet [bindings & body]
`(let ~bindings
~@(for [v (map first (partition 2 (destructure bindings)))]
(list 'def v v))))