Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am dbasch on github.
  • I am dbasch (https://keybase.io/dbasch) on keybase.
  • I have a public key whose fingerprint is 9F30 87BA 2ABB 3795 2B33 1A42 7B7B 067F EDBC 6E73

To claim this, I am signing this object:

(ns markov.core)
(defn transform
[words]
(->> words
(partition 2 1)
(reduce (fn [acc [w next-w]]
(update-in acc
[w next-w]
(fnil inc 0)))
(defn wrand
"given a vector of slice sizes, returns the index of a slice given a
random spin of a roulette wheel with compartments proportional to
slices."
[slices]
(let [total (reduce + slices)
r (rand total)]
(loop [i 0 sum 0]
(if (< r (+ (slices i) sum))
i
(defn markers [line]
(concat [:start]
(clojure.string/split line #"\s+")
[:end]))
(defn lazy-lines [file]
(letfn [(helper [rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(concat (markers line) (helper rdr))
(defn transform
[words]
(->> words
(partition 2 1)
(reduce (fn [acc [w next-w]]
(update-in acc
[w next-w]
(fnil inc 0)))
{})))
@dbasch
dbasch / life.clj
Last active December 25, 2015 05:19
(defn next-gen [b r c]
(let [neighbors (fn [[cell :as [i j]]]
(remove #(= % [i j])
(for [x (filter (-> r range set) (range (- i 1) (+ 2 i)))
y (filter (-> c range set) (range (- j 1) (+ 2 j)))]
[x y])))
alive? (fn [[cell :as [i j]]]
(nth (nth b i) j))]
(for [i (range r)]
(for [j (range c)]
with open("file.bin", "rb") as infile, open("reversed", "wb") as outfile:
data = infile.read()
for i in xrange(len(data) / 2):
outfile.write(data[i*2+1])
outfile.write(data[i*2])
import java.io.File;
import java.util.Scanner;
import com.google.common.collect.HashMultimap;
/** create in-memory mappings from words to the files that contain them */
public class Indexer {
public static HashMultimap<String,String> buildIndex(String dirName) throws java.io.IOException {
HashMultimap<String,String> map = HashMultimap.create();
for (File f : new File(dirName).listFiles()) {
String fname = f.getName();
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
/** create in-memory mappings from words to the files that contain them */
public class Indexer2 {
public static HashMap<String, HashSet<String>> buildIndex(String dirName) throws java.io.IOException {
HashMap<String,HashSet<String>> map = new HashMap<String,HashSet<String>>();
for (File f : new File(dirName).listFiles()) {
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
/** create in-memory mappings from words to the files that contain them */
public class Indexer2 {
public static HashMap<String, HashSet<String>> buildIndex(String dirName) throws java.io.IOException {
HashMap<String,HashSet<String>> map = new HashMap<String,HashSet<String>>();
for (File f : new File(dirName).listFiles()) {