Skip to content

Instantly share code, notes, and snippets.

@skatenerd
skatenerd / gist:5541107
Created May 8, 2013 15:09
only interesting when run from repl
1.9.3-p194 :002 > def foo
1.9.3-p194 :003?> require 'pry'
1.9.3-p194 :004?> Process.fork do
1.9.3-p194 :005 > binding.pry
1.9.3-p194 :006?> end
1.9.3-p194 :007?> Process.fork do
1.9.3-p194 :008 > binding.pry
1.9.3-p194 :009?> end
1.9.3-p194 :010?> end
@skatenerd
skatenerd / just_use_lists.clj
Created May 21, 2013 00:39
I start out with the vector-version of [1 2 3 4 5]. When I call `(map identity...)`, it should theoretically return something just like what I put in, since the `identity` function just returns what you give it. However, the result of `(map identity ...)` is much different from the original [1 2 3 4 5] vector!
user=> [1 2 3 4 5]
[1 2 3 4 5]
user=> (map identity [1 2 3 4 5])
(1 2 3 4 5)
user=> (conj [1 2 3 4 5] 6)
[1 2 3 4 5 6]
user=> (conj (map identity [1 2 3 4 5]) 6)
(6 1 2 3 4 5)
(defn increment-in-map [map key]
(update-in map [key] #(inc (or % 0))))
(defn count-occurrences [to-count]
(loop [counted-so-far {}
remaining-to-count to-count]
(if (empty? remaining-to-count)
counted-so-far
(recur (increment-in-map counted-so-far (first remaining-to-count))
(rest remaining-to-count)))))
(with-open [rdr (io/reader "hello_goodbye.txt")]
(prn (frequencies (flatten (map seq (line-seq rdr))))))
;{\h 3, \e 9, \l 6, \o 15, \g 6, \d 6, \b 6, \y 6}
interface Quacker {
void Quack();
}
class Duck implements Quacker{
public void Quack(){
System.out.println("quack");
}
}
@skatenerd
skatenerd / clpfd.clj
Last active December 20, 2015 23:58
macros are weird.
(def a (lg/lvar 'a))
(def b (lg/lvar 'b))
(def lvars [a b])
;simple attempt:
(lg/run* [q]
(fd/in a b (fd/interval 0 1))
(lg/== q lvars))
; this gives me the answer i want --> ([0 0] [0 1] [1 0] [1 1])
@skatenerd
skatenerd / lol.clj
Last active December 21, 2015 18:38
lol
(defn divides? [bottom top]
(zero? (mod top bottom)))
(def naturals (iterate inc 1))
(defn multiples-of
([n]
(map #(* n %) (rest naturals)))
([n max]
(take-while #(<= % max) (multiples-of n))))
@skatenerd
skatenerd / 5.clj
Created August 30, 2013 06:10
euler-5
(defn max-merge [first second]
(reduce
(fn [factors-so-far current-key]
(let [higher-value (max (get first current-key 0) (get second current-key 0))]
(assoc factors-so-far current-key higher-value)))
{}
(clojure.set/union (set (keys first)) (set (keys second)))))
(def naturals (iterate inc 1))
@skatenerd
skatenerd / graph.clj
Last active December 25, 2015 18:19
longest path through digraph
(ns fun.graph)
(defn unvisited-neighbors [graph node path-so-far]
(clojure.set/difference
(get graph node)
(set path-so-far)))
(defn path-with-node [path new-node]
(conj path new-node))
@skatenerd
skatenerd / lazy.clj
Created October 17, 2013 11:44
lazy seq plus print debug
(defn hard-to-debug [& args]
(prn "this is a debug statement")
(if (not (empty? args))
(prn (map (fn [_] (hard-to-debug)) (range 3)))))