Skip to content

Instantly share code, notes, and snippets.

View madstap's full-sized avatar

Aleksander Madland Stapnes madstap

View GitHub Profile
@madstap
madstap / pretty_xml.clj
Last active December 27, 2021 14:44
The stackoverflow answer to "how to pretty print xml in java" in clojure
(ns pretty-xml
(:import (java.io ByteArrayInputStream StringWriter)
(javax.xml.parsers DocumentBuilderFactory)
(javax.xml.transform TransformerFactory OutputKeys)
(javax.xml.transform.dom DOMSource)
(javax.xml.transform.stream StreamResult)
(javax.xml.xpath XPathFactory XPathConstants)
(org.w3c.dom Document NodeList Node)
(org.xml.sax InputSource))
@madstap
madstap / bowling.clj
Last active April 21, 2021 23:49
Bowling kata
(ns bowling)
(def empty-state
{:frames []
:current-frame []
:score 0
:strike-counter 0
:spare-counter 0})
(defn dec-zero [n]
@madstap
madstap / gql_ns.cljc
Created June 19, 2018 21:41
namespaces for graphql
(ns gql-ns
(:require
[clojure.string :as str]))
(defn valid-gql-key?
"Checks that a key has at most a single namespace separator."
[k]
(-> (name k)
(str/replace #"^__" "")
(str/replace #"__$" "")
@madstap
madstap / let.clj
Last active January 8, 2020 16:39
Debugging let
;; A macro for debugging stuff that happens inside a let
;; just change let to lets and all the bindings will be defined
;; as globals instead of locals and the body will be ignored.
(require '[madstap.comfy :refer [defs]])
(require '[integrant.core :as ig])
(defmacro lets
{:style/indent 1}
[bindings & _body]
`(do ~@(map (fn [[b exp]] `(madstap.comfy/defs ~b ~exp)) (partition 2 bindings))))
@madstap
madstap / workaround.el
Last active April 26, 2018 12:06
Cider cljr issue workaround
;; error in process sentinel: nrepl-server-sentinel: Could not start nREPL server: Retrieving refactor-nrepl/refactor-nrepl/2.4.0-SNAPSHOT/refactor-nrepl-2.4.0-20180410.152833-6.jar from clojars
;; Could not transfer artifact refactor-nrepl:refactor-nrepl:jar:2.4.0-20180410.152833-6 from/to clojars (https://repo.clojars.org/): GET request of: refactor-nrepl/refactor-nrepl/2.4.0-SNAPSHOT/refactor-nrepl-2.4.0-20180410.152833-6.jar from clojars failed
;; This could be due to a typo in :dependencies, file system permissions, or network issues.
;; If you are behind a proxy, try setting the 'http_proxy' environment variable.
;; Bozhidar:
;; Seems like some problem related to fetching the dep.
;; I guess you can just disable `clj-refactor`’s injection of dependencies.
(setq cljr-inject-dependencies-at-jack-in nil)
(ns bank.account
(:require
[clojure.spec.alpha :as s]))
(s/def ::fname string?)
(s/def ::lname string?)
(s/def ::cents integer?)
;; It's currently valid to have a negative balance.
;; Could alternatively do something like (s/and ::cents (complement neg?))
@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)))))
;; 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 / 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#]
(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?\"