Skip to content

Instantly share code, notes, and snippets.

View emdeesee's full-sized avatar
🤔
hmm

Michael Cornelius emdeesee

🤔
hmm
View GitHub Profile
@emdeesee
emdeesee / mergesort.clj
Created October 1, 2012 15:04
Merge Sort in Clojure
(defn merge-sort
"Produce a sorted sequence from an input sequence. Sort into ascending order by default, or use optional comparison function."
([x] (merge-sort < x))
([swo x]
(letfn [(merge [left right]
(loop [l left r right result []]
(let [l-first (first l) r-first (first r)]
(cond (nil? l-first) (concat result r)
(nil? r-first) (concat result l)
:default (if (swo l-first r-first)
@emdeesee
emdeesee / codec.clj
Created October 3, 2012 14:52
Encode and decode "binary" strings with Clojure
(use '[clojure.string :only [trim replace]]
'[clojure.pprint :only [cl-format] :rename {cl-format format}])
(defn- unspace [s]
(trim (replace s " " "")))
(defn decode [bitstring]
"\"01100110 01101111 01101111\" -> \"foo\"
Whitespace in the input string is ignored."
@emdeesee
emdeesee / sieve.clj
Last active December 7, 2016 20:20
Sieve of Eratosthenes
(defn gen-primes []
(letfn [(reinsert [table x prime]
(update-in table [(+ prime x)] conj prime))
(step [table d]
(if-let [factors (table d)]
(recur (reduce #(reinsert %1 d %2) (dissoc table d) factors)
(inc d))
(lazy-seq (cons d
(step (assoc table (* d d) (list d)) (inc d))))))]
(step {} 2)))
@emdeesee
emdeesee / rot13.clj
Created January 22, 2013 17:35
Rot13 vs. Clojure
(let [alpha (into #{} (concat (map char (range (int \a) (inc (int \z))))
(map char (range (int \A) (inc (int \Z))))))
rot13-map (zipmap alpha (take 52 (drop 26 (cycle alpha))))]
(defn rot13
"Given an input string, produce the rot 13 version of the string. \"hello\" -> \"uryyb\""
[s] (apply str (map #(get rot13-map % %) s))))
@emdeesee
emdeesee / fb.clj
Created January 29, 2013 18:50
FizzBuzz vs. Clojure
(map #(let [s (str %2 %3)] (if (seq s) s %)) (range 1 101) (cycle ["" "" "Fizz"]) (cycle ["" "" "" "" "Buzz"]))
(defn first-unique [s]
(let [f (frequencies s)]
(first (filter #(= (f %) 1) s))))
@emdeesee
emdeesee / memfn.cc
Last active December 26, 2015 06:29
Simple use of pointers to member functions
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct O
{
void m(int n) const
@emdeesee
emdeesee / fizzbuzz2.clj
Created July 25, 2015 03:20
FizzBuzz vs. Clojure, round 2
;; less code golf, more readability
;; also complies with specs of the problem by sending results to stdout (rather than just generating a sequence)
(doseq [n (map (some-fn #(and (= (mod % 3) 0) (= (mod % 5) 0) "FizzBuzz")
#(and (= (mod % 3) 0) "Fizz")
#(and (= (mod % 5) 0) "Buzz")
identity) (range 1 101))]
(println n))
@emdeesee
emdeesee / calculator.clj
Last active August 29, 2015 14:27
My solution to 4clojure problem 121 (http://www.4clojure.com/problem/121)
(fn e [exp]
(fn [env]
(apply ({'+ + '- - '* * '/ /} (first exp))
(map #(if (seq? %) ((e %) env) (env % %))
(rest exp)))))
(fn red
([f [v & vs]] (red f v vs))
([f v [u & us]]
(cons v (when u (lazy-seq (red f (f v u) us))))))