Skip to content

Instantly share code, notes, and snippets.

@wagjo
wagjo / lazy seq from reducers
Last active Dec 21, 2015
lazy seq and interleave from reducers with core.async
View lazy seq from reducers
(defn lazy-seq* [reducible]
(let [c (chan)
NIL (Object.)
encode-nil #(if (nil? %) NIL %)
decode-nil #(if (identical? NIL %) nil %)
reduce-fn (fn [r v] (>!! c (encode-nil v)))]
(thread
(reduce reduce-fn nil reducible)
(close! c))
(->> #(<!! c)
@wagjo
wagjo / gist:6442931
Created Sep 4, 2013
benchmarking string splitting
View gist:6442931
;; machine: amd64 Linux 2.6.32-48-server 16 cpu(s)
;; Java HotSpot(TM) 64-Bit Server VM 23.25-b01
;;
;; == without lazy seq realization
;; regex split: 258.567183 ms
;; parallelized regex split: 65.053176 ms - lazy seqs are not realized!
;; correct reduce: 623.541446 ms
;; correct fold: 252.817020 ms
;;
;; == lazy seq realization with (into [] %)
View 01-original.c
// original
int CheckExtension (char *filename, char *ext)
{
char *s;
if (!filename) return(0);
if (strlen(filename) == 0) return(0);
s = strrchr(filename,'.');
if (!s) return(0);
if (!strcmp(s,ext)) return(1);
@wagjo
wagjo / gist:6743885
Created Sep 28, 2013
reducible slurp
View gist:6743885
(require '[clojure.java.io :as jio])
(deftype Slurp [filename]
clojure.core.protocols/CollReduce
(coll-reduce [this f1]
(clojure.core.protocols/coll-reduce this f1 (f1)))
(coll-reduce [_ f1 init]
(with-open [#^java.io.Reader r
(apply jio/reader filename nil)]
(loop [ret init
View gist:7640187
Type Error (user:1:5) Polymorphic function clojure.core/merge could not be applied to arguments:
Polymorphic Variables:
k36143
v36144
Domains:
nil *
(clojure.lang.IPersistentMap k v) (clojure.lang.IPersistentMap k v) *
(Option (clojure.lang.IPersistentMap k v)) *
@wagjo
wagjo / gist:8290237
Last active Jan 2, 2016
Bug in nrepl?
View gist:8290237
;; AOT before loading (e.g. with :aot :all in project.clj)
;; $ lein deps :tree
;; [clojure-complete "0.2.3" :exclusions [[org.clojure/clojure]]]
;; [org.clojure/clojure "1.5.1"]
;; [org.clojure/tools.nrepl "0.2.3" :exclusions [[org.clojure/clojure]]]
(ns ccc.core
(:gen-class))
(defprotocol P)
@wagjo
wagjo / gist:8731671
Created Jan 31, 2014
Playing with quil
View gist:8731671
(ns foo.bar
(:use [quil.core])
(:require [clojure.string :as s]))
(defn setup []
(frame-rate 1)
(background 255))
(def *scale* 2)
View gist:8786305
(defprotocol IMutable
(-cas! [o oldval newval]
"Sets the value to newval if and only if the current value
is identical to oldval. Returns true if set happened,
otherwse returns false. Mutates `o`.")
(-alter! [o f] [o f x] [o f x y] [o f x y z] [o f x y z args]
"Changes referenced value with (apply f val args).
Returns new value. Mutates `o`.")
(-reset! [o val]
"Resets the referenced value to `val`.
View gist:9813500
(defn update-in*
[m [k & ks] f & args]
(let [k (if (and (instance? clojure.lang.Indexed m)
(integer? k)
(neg? k))
(+ (count m) k)
k)]
(if ks
(assoc m k (apply update-in* (get m k) ks f args))
(assoc m k (apply f (get m k) args)))))
@wagjo
wagjo / gist:9958354
Created Apr 3, 2014
Upcoming reducible IO library for Clojure
View gist:9958354
;; Input vector of bytes
(def vec (into (vector-of :byte)
(repeatedly 1000000 #(rand-int 128))))
;; Output file
(def out (java.io.FileOutputStream. "out.bin"))
;; Classic reduction -> 3835 ms
(let [write-fn (fn [r w] (.write out w))]
(time (reduce write-fn nil (map dec vec))))