Skip to content

Instantly share code, notes, and snippets.

View sbocq's full-sized avatar

Sébastien Bocq sbocq

View GitHub Profile
@sbocq
sbocq / permutations.clj
Last active February 21, 2021 11:45
Compute all permutations of a collection in Clojure (non-lazy, iterative)
(defn permutations [coll]
(letfn [(rotations [c vs] (reduce (fn [rotations _]
(conj rotations
(let [[head & tail] (peek rotations)]
(conj (vec tail) head))))
[(conj vs c)]
vs))]
(cond-> (reduce (fn [permutations c] (mapcat (partial rotations c) permutations))
[[]]
coll)
@sbocq
sbocq / flatten-keys.clj
Created February 18, 2015 23:10
Flatten keys
;; (flatten-keys {:name {:first "Rich" :last "Hickey"} :number [1 415 123 4567]})
;; => {"$.number[0]" 1, "$.number[1]" 415, "$.number[2]" 123, "$.number[3]" 4567, "$.name.first" "Rich", "$.name.last" "Hickey"}
(defn flatten-keys [thing]
(letfn [(-map-key [prefix k] (str prefix "." (name k)))
(-seq-key [prefix i] (str prefix "[" i "]"))
(-flatten-entry [make-key prefix result entry]
(let [[k v] entry]
(-flatten-thing result (make-key prefix k) v)))
(-flatten-thing [result key thing]
@sbocq
sbocq / hg-ediff-init.el
Created June 14, 2013 20:02
Enable ediff support in the *hg-log-view* of emacs.
;; Assuming you have already installed the 'mercurial' package in
;; emacs, follow the steps below to enable 'ediff' support in
;; the *hg-log-view*:
;;
;; 1) Add the following lines to your '.hgrc' such that the *hg-log-view*
;; of emacs shows the files that have changed in the detailed information
;; of a changeset:
;;
;; [ui]
;; verbose = True
@sbocq
sbocq / FizzBuzz.scala
Created November 25, 2012 17:53
FizzBuzz, without modulo, imperative and functional
// Imperative
var fizzbuzz = 15
var fizz = 3
var buzz = 5
for(i <- 1 to 100) {
val s =
if (i == fizzbuzz) {
fizz = fizzbuzz + 3
buzz = fizzbuzz + 5