Skip to content

Instantly share code, notes, and snippets.

View Sophia-Gold's full-sized avatar

Sophia Gold Sophia-Gold

View GitHub Profile
@Sophia-Gold
Sophia-Gold / dlet.clj
Created February 12, 2018 00:04
clojure macro for debugging let bindings
;; From e.g. https://github.com/scottjad/uteal
(defmacro dlet
"let with inspected bindings"
[bindings & body]
`(let [~@(mapcat (fn [[n v]]
(if (or (vector? n) (map? n))
[n v]
[n v '_ `(println (name '~n) ":" ~v)]))
(partition 2 bindings))]
~@body))
@Sophia-Gold
Sophia-Gold / dft.clj
Last active January 13, 2018 08:33
Discrete Fourier Transform
(ns dft.core)
(defn fft [x]
(let [length (count x)]
(if (<= length 1)
(->> x
(persistent!)
(vec))
(let [x (transient x)
even (recur (map #(nth % x) (range length)))
{ pkgs }:
{
allowUnfree = true;
allowBroken = true;
packageOverrides = self: with self; rec {
etsEnv = self.buildEnv {
name = "etsEnv";
(defn str-to-longs [^String s]
(map #(Character/codePointAt s (long %)) (range (count s))))
(defn map-longest [f default & colls]
(lazy-seq
(when (some seq colls)
(cons
(apply f (map #(if (seq %) (first %) default) colls))
(apply map-longest f default (map rest colls))))))
@Sophia-Gold
Sophia-Gold / AdditionChains.clj
Created August 18, 2017 03:53
using binary method (not always shortest)
(defn double [i]
(bit-shift-left i 1))
(defn to-binary-seq [^long x]
(map #(- (int %) (int \0))
(Long/toBinaryString x)))
(defn addition-chain [x]
(reduce #(if (zero? %2)
(conj %1 (double (peek %1)))
(defn halve [i]
(bit-shift-right i 1))
(defn double [i]
(bit-shift-left i 1))
(defn mul [x y]
(let [x (take-while #(not= % 0) (iterate halve x))]
(->> y
(iterate double)
(defn Y [f]
((fn [x] (x x))
(fn [x]
(f (fn [& args]
(apply (x x) args))))))
(def fac
(fn [f]
(fn [n]
(if (zero? n) 1 (* n (f (dec n)))))))
(defn max-index [v]
(->> v
(map-indexed vector)
(apply max-key second)
(first)))
(defn swap [v i1 i2]
(assoc v i2 (v i1) i1 (v i2)))