Skip to content

Instantly share code, notes, and snippets.

(require '[clojure.core.async :refer [go-loop chan <! close!]])
(require '[fun-utils.core :refer [go-seq]])
;When writing go blocks there is a repeating pattern to avoid 100% cpu spin when the channel is closed.
;e.g.
;if we run the below code, the loop will spin without pause.
;;running this code will cause nil to be printed without pause
@gerritjvv
gerritjvv / merge-stats-map.clj
Last active August 29, 2015 14:01
The map is formed of key = topic, value = stats map of numeric stats The task was to aggregate the numeric values in the stats map for similar topics, each message could have multiple topics.
(def msgs [ {"a" {:stat1 1 :stat2 10} "b" {:stat1 1}} {"a" {:stat1 1}}])
(def merged (apply merge-with (partial merge-with +) msgs))
;; {"a" {:stat1 2, :stat2 10}, "b" {:stat1 1}}
(prn merged)
;; {"a" {:stat1 2, :stat2 10}, "b" {:stat1 1}}
(= (-> merged first second :stat1) 2)
;; true
@gerritjvv
gerritjvv / Vagrant
Created September 2, 2014 08:54
Cassandra Vagrant File
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", "2048"]
@gerritjvv
gerritjvv / bootstrap.sh
Created September 2, 2014 08:57
Cassandra Vagrant bootstrap
!/usr/bin/env bash
NODE="$1"
echo "Bootstrapping node $1"
function install_setup(){
yum update
yum -y install wget vim snappy-devel snappy lzo lzop elinks glibc.i686
}
@gerritjvv
gerritjvv / gist:3b0e938bdcf91d2a450d
Created November 23, 2014 17:01
NPETypeHintFix
(defn safe-inc [l ^long x]
(if l
(+ ^long l x)
x))
// Compiled from form-init2140241509718408406.clj (version 1.5 : 49.0, super bit)
public final class no.disassemble$eval1752$safe_inc__1753 extends clojure.lang.AFunction implements clojure.lang.IFn$OLO {
// Field descriptor #9 Lclojure/lang/Var;
public static final clojure.lang.Var const__0;
@gerritjvv
gerritjvv / gist:4c353066e6bd1135449a
Created May 7, 2015 22:23
using bounded clojure agents from fun-utils
(require '[fun-utils.agent :as fagent] :reload)
(def agnt (fagent/agent {} :mailbox-len 10))
;send to the agent
(fagent/send agnt assoc-in [:a :b] 2)
;deref
@agnt
;;{:a {:b 2}}
@gerritjvv
gerritjvv / linecounting.clj
Created September 16, 2015 08:28
efficient clojure line counting
(import '(java.io InputStreamReader FileInputStream BufferedReader))
(import '(java.util.zip GZIPInputStream))
(defn gz-line-counter [^String file-name]
(let [^BufferedReader reader (BufferedReader. (InputStreamReader. (GZIPInputStream. (FileInputStream. file-name))))]
(try
(loop [i 0]
(if (.readLine reader)
(recur (inc i))
i))
@gerritjvv
gerritjvv / gist:5755871
Created June 11, 2013 10:24
Clojure LazySequence From Java
package lazyseq;
import org.junit.Test;
import clojure.lang.AFn;
import clojure.lang.ASeq;
import clojure.lang.ArraySeq;
import clojure.lang.Cons;
import clojure.lang.IPersistentMap;
import clojure.lang.ISeq;
@gerritjvv
gerritjvv / gist:5866679
Created June 26, 2013 11:19
Call external process from Java
int ret = new ProcessBuilder("gzip", "-t", "t.txt.gz").inheritIO().start().waitFor()
@gerritjvv
gerritjvv / gist:5948398
Created July 8, 2013 12:29
Partitioning Lazy Sequences
Caution: Partitioning lazy sequence code freeze
(def l [1 2 3 4 5])
;create a simple lazy sequence function testing only
;(rdr l) returns a lazy sequence from l
(def rdr (fn reader[x] (cons (first x) (lazy-seq (reader (rest x))))))
;the line below will freeze
(doall (partition-all 2 (rdr l)) )