Skip to content

Instantly share code, notes, and snippets.

View msszczep's full-sized avatar

Mitchell Szczepanczyk msszczep

View GitHub Profile
@msszczep
msszczep / gist:11266360
Created April 24, 2014 19:22
Reconnection in Langohr
;;adapted from http://clojurerabbitmq.info/articles/error_handling.html
(ns killwabbit.core
(:gen-class)
(:require [langohr.core :as rmq]
[langohr.channel :as lch]
[langohr.queue :as lq]
[langohr.exchange :as lx]
[langohr.consumers :as lc]
[langohr.basic :as lb])
(:import java.io.IOException
@msszczep
msszczep / gist:12168573f29159e86dfc
Created July 22, 2014 20:38
Build an evaluator in Clojure (Lambda Jam 2014)
; variable evaluation: find and substitute
; look for Var X in B and substitute a
; environment, symbol
(def MyEnv {"One" 1
"Two" 2
"Three" 3
"MyAdd" +})
@msszczep
msszczep / gist:a436e25601a6a031f5e1
Created December 8, 2014 00:22
Clojure Bridge exercise: Caesar Cipher
(def mystring "my name is stephanie")
(def alphabet "abcdefghijklmnopqrstuvwxyz")
(def alphabet-chars (map char alphabet))
alphabet-chars
(def alphabet-shifted (drop 5 (take 100 (cycle alphabet-chars))))
@msszczep
msszczep / gist:b20b847a7e8ddc6fbad4
Created December 10, 2014 14:25
Proposed Clojure Bridge Exercise: Caesar cipher
A Caesar cipher is a very simple kind of encoding of alphabetic text (like English)
in which each letter in the text is replaced by another letter some fixed number of
positions down the alphabet. For example, the string
"hello"
can be replaced by a Caesar cipher one letter down the alphabet to the following:
"ifmmp"
@msszczep
msszczep / gist:4b5409d5a256a0717585
Created March 20, 2015 19:09
Clojure exercise: Sentiment Scoring for Words
(defn sentiment-scorer [& maps]
(letfn [(sentiment-scorer-intermediate [m]
(let [words (-> (:c m)
(clojure.string/lower-case)
(clojure.string/replace #"[\.\!\@]" "")
(clojure.string/split #"\s+"))
rs (repeat (count words) [(:r m)])]
(apply hash-map (interleave words rs))))]
(let [intermediate-map (->> (apply map sentiment-scorer-intermediate maps)
(apply merge-with concat))]
(let [d {1 "Project #1"
2 "Project #2"
3 "Project #3"
4 "Project #4"
5 "Project #5"
6 "Project #6"}]
(d (-> (rand-int 6)
inc)))
(let [d ["Project #1"
"Project #2"
"Project #3"
"Project #4"
"Project #5"
"Project #6"]]
(rand-nth d))
@msszczep
msszczep / gist:d572f265e8318ccddbcd
Last active August 29, 2015 14:19
Clojure rewrite of Scrabble surnames' scorer
; See http://www.szcz.org/blog/?cat=33 for context
(let [surnames (->> (slurp "/Users/msszczep1/current_2011-01-2.txt")
(clojure.string/split-lines)
distinct)
points {\A 1 \B 3 \C 3 \D 2 \E 1 \F 4 \G 2 \H 4 \I 1 \J 8 \K 5 \L 1 \M 3
\N 1 \O 1 \P 3 \Q 10 \R 1 \S 1 \T 1 \U 1 \V 4 \W 4 \X 8 \Y 4 \Z 10 \- 0 \' 0}
scores (map #(reduce + (map points (seq %))) surnames)]
(->> (zipmap surnames scores)
(sort-by val)
@msszczep
msszczep / gist:fadb318cadb286ffffbc
Last active November 4, 2015 07:36
Instant average
(defn instant-avg [nums]
[(apply + nums)
(/ (float (apply + nums)) (count nums))
(count nums)])
;; assumes nums is a vector of integers
@msszczep
msszczep / gist:7f713a7bbdd75220313e
Created December 12, 2015 17:23
Coffee in Java and Clojure
// https://www.facebook.com/WondersofEngineering/photos/a.255298751271223.61509.255293124605119/701667366634357/?type=3&theater
Coffee coffee = new Coffee();
if (coffee.Empty) {
coffee.Refill();
} else {
coffee.Drink();
}
;; I can rewrite it in Clojure in two lines: