Skip to content

Instantly share code, notes, and snippets.

View madstap's full-sized avatar

Aleksander Madland Stapnes madstap

View GitHub Profile
(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]
@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.
;; 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 / 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))
@madstap
madstap / ampersand.cljs
Created January 16, 2017 07:57
Clojurescript &
;; I can use & as a name with def
(def & identity)
& ;=> #object[Function "function (a){return a}"]
;; But if I do
(& 42)
;; It throws the exception.
@madstap
madstap / enlive_parse_unparse.clj
Created January 15, 2017 08:00
I always forget which fns are parse and unparse in enlive
(ns foo.core
(:require
[net.cgrand.enlive-html :as e]))
(def x "<p>asd</p><a href=\"google.com\">google</a>")
;; Parse
(def y (e/html-snippet x))
y ;=> ({:tag :p, :attrs nil, :content ("asd")} {:tag :a, :attrs {:href "google.com"}, :content ("google")})
@madstap
madstap / conform-let.clj
Last active October 28, 2016 21:02
let with conforming bindings
(ns conform-let
(:require
[clojure.spec :as s]
[clojure.core.specs :as core-specs]))
(s/def ::conform-bindings
(s/and vector?
(s/spec (s/* (s/cat :binding ::core-specs/binding-form
:spec some?
;; Eval this in *scratch* (or emacs setup)
(setq cider-cljs-lein-repl
"(do (require 'figwheel-sidecar.repl-api)
(figwheel-sidecar.repl-api/start-figwheel!)
(figwheel-sidecar.repl-api/cljs-repl))")
;; Then m-x cider-jack-in-clojurescript