Skip to content

Instantly share code, notes, and snippets.

See
https://gist.github.com/ericnormand/7174aaccc71025de86ddac77553f8595
How many digits?
Imagine you took all the integers between n and m (exclusive, n <
m) and concatenated them together. How many digits would you have?
Write a function that takes two numbers and returns how many
digits. Note that the numbers can get very big, so it is not
possible to build the string in the general case.
@KingCode
KingCode / gist:30894e53da19d712021906629cf1583c
Last active February 13, 2021 15:35
frequencies-by function
(defn frequencies-by
"Returns a map from distinct results of (f item) in coll
to a vector duple of all (k item) (if k is provided, or items otherwise)
with the same (f item) value, and the number of times (f item) appears.
"
([f coll]
(frequencies-by f nil coll))
([f k coll]
(persistent! ;; copied from clojure.core
@KingCode
KingCode / gist:679561f6da86863316499fe134c31dd7
Last active December 22, 2020 16:51
assoc 3-arity vs var-args arity performance comparison, see: https://twitter.com/borkdude/status/1341009566012731392/photo/1
(defn slow-assoc [m & [k v & kvs :as dat]]
(if (empty? dat)
m
(apply slow-assoc (assoc m k v) kvs)))
(defn emit-assoc [ [ksym vsym]]
`(assoc ~ksym ~vsym))
(defmacro fast-assoc [msym & kvsyms]
(let [assocs (map #(emit-assoc %)
(defn monoid* [init body]
`(fn f#
([]
(fn [& ~'xs] ~init))
([~'r]
(fn [& ~'xs] (~body (apply ~'r ~'xs))))
([~'r1 ~'r2]
(fn [& ~'xs] (~body(apply ~'r1 ~'xs) (apply ~'r2 ~'xs))))
([~'r1 ~'r2 & ~'rs]
(fn [& ~'xs]
@KingCode
KingCode / gist:8970fe4e2308127ba467ac7f57d3f78b
Created October 16, 2020 20:41
Hand rolled sequence to prevent chunking
(defn lazify [[x & xs :as coll]]
(lazy-seq
(when (seq coll)
(cons x (lazify xs)))))
(def my-lazy-input (lazify [1 2 4 1 2 3 1]))
(type my-lazy-input) ;; => clojure.lang.LazySeq
(type (rest my-lazy-input)) ;; => clojure.lang.LazySeq
;; So my-lazy-input is truly lazy all the wal...Now I want to feed it to 'sequence
@KingCode
KingCode / my-ns-test.clj
Last active July 1, 2020 00:57
Properties test for Functional-TV challenge #384
;; see
;; https://gist.github.com/ericnormand/81722b80fc7d0b2972fda68652489f65
(ns my-ns-test
(:require [my-ns :as sut]
[clojure.test.check.properties :as prop]
[clojure.test.check.generators :as gen]
[clojure.test.check.clojure-test :refer [defspec]]))
(let [alias-str "sut"
sym (symbol (str alias-str "/parallel?"))]
@KingCode
KingCode / gist:773560f4ab5bf91e660a2a26e581b036
Last active November 2, 2020 23:49
cond-let and cond-let> macros, to leverage bindings between test and result expressions, as well as earlier ones (for cond-let)
(ns cond-let)
;; (cond-let
;; (odd? x) [x n] (inc x)
;; (< n 10) [y (inc n)] 10
;; :else n))
;; we want the above to yield
;; (let [x n]
;; (if (odd? x)
;; (inc x)
@KingCode
KingCode / gist:a954ced2aeaa22f34332fc81f75f9969
Last active October 27, 2018 10:46
Scoping transducer for pipelining sub-structures without losing the container.
(require '[clojure.core.async :as async :refer [chan <!! put!]])
(defn save-to [c save-f]
(fn [rf]
(fn
([] (rf))
([acc] (rf acc))
([acc x]
(put! c (save-f x))
(rf acc x)))))
@KingCode
KingCode / transducer.clj
Last active October 1, 2018 13:58
An implemementation of the transducer API from scratch, as a learning aid.
(ns scratch.transducer
(:refer-clojure :exclude [comp map filter mapcat take drop
take-while drop-while
dedupe
distinct
reductions
;; sequence
transduce]))
@KingCode
KingCode / gist:ef2ac029dbcf2881ad1762611f837d56
Last active February 19, 2017 13:37
fizzbuzz example spec
(ns hellospec.fizzbuzz
(:require [clojure.spec :as s]
[clojure.spec.test :as stest]
;; to accomodate KLIPSE:
;; "[...]cljs.spec.gen.frequency is undefined[...]"
#?(:clj [clojure.spec.gen :as gen]
:cljs [clojure.test.check.generators :as gen])))
;; A fizzbuzz which doesn't use condition checking/branching
(defn fizzbuzz