Skip to content

Instantly share code, notes, and snippets.

View madstap's full-sized avatar

Aleksander Madland Stapnes madstap

View GitHub Profile
@madstap
madstap / code_golfer.clj
Last active April 16, 2017 04:34
Code golfer as a macro
(ns golf.code-golfer)
(def ^:private safe-neighbors
"The characters that indicate neighboring spaces are safe to remove."
(set "(){}[]\""))
(defn- whitespace? [x]
{:pre [(char? x)]}
(Character/isWhitespace x))
;; In core.cljs
(ns foo.core
(:require
[pushy.core :as pushy]
[re-frame.core :as rf]]))
(defn routing! []
(pushy/start! (pushy/pushy #(rf/dispatch [:routes/set-page %]) routes/match)))
(defn ^:export init []
@madstap
madstap / iphone_connect_files.sh
Created August 28, 2017 02:37
How to get at the file system in an iphone
#/bin/sh
# https://askubuntu.com/questions/799414/how-to-move-files-from-ubuntu-to-iphone
idevicepair pair
# Will exit with an error if there's a password on the device
# so unlock the device,
# then it will error again, but open a dialogue on the device.
(defn merge-consecutive-by-with-xf ; this name is less than ideal...
"Returns a transducer that reduces merge-fn over consecutive values for which
(consecutive-by value) is the same."
[consecutive-by merge-fn]
(fn [rf]
(let [prev (atom ::none)]
(fn
([] (rf))
([res]
(let [p @prev]
(defn merge-consecutive-by-with-xf ; this name is less than ideal...
"Returns a transducer that reduces merge-fn over consecutive values for which
(consecutive-by value) is the same."
[consecutive-by merge-fn]
(fn [rf]
(let [prev (atom ::none)]
(fn
([] (rf))
([res]
(let [p @prev]
(ns foo.bool-diff
"Have you ever wondered \"Can I write this boolean expression like that instead?\"
Well, I have. So I wrote this to verify whether I'm right.
Say I've got the expression:
(and (not (foo? x))
(not (bar? y)))
I might think \"Isn't that the same as negating an or?\"
@madstap
madstap / when_letp.clj
Last active September 27, 2017 20:12
(defmacro when-letp
{:style/indent 2}
[pred bindings & body]
(if (empty? bindings)
`(do ~@body)
(let [[[binding expr] more] (split-at 2 bindings)]
`(let [temp# ~expr
pred# ~pred]
(when (pred# temp#)
(let [~binding temp#]
;; Reducing function:
;; A function that takes an accumulator and an item and returns a new accumulator.
;; Like the function you would pass to reduce.
;; Transducer:
;; A function that takes a reducing function and
;; returns a new reducing function.
;; Slightly simplified, this is the meat of map with arity one.
@madstap
madstap / car.clj
Last active September 13, 2022 19:15
car, cdr, cadadr and friends in clojure
(ns car
"The sixties are back!"
(:require
[clojure.math.combinatorics :as combo]))
(defn ad-combos [n]
(distinct
(mapcat (fn [i]
(combo/selections '[a d] i))
(range 1 (inc n)))))