Skip to content

Instantly share code, notes, and snippets.

@wagjo
wagjo / lazy seq from reducers
Last active December 21, 2015 16:39
lazy seq and interleave from reducers with core.async
(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:27ce6a34d5d5257a0790
Last active September 24, 2015 19:41
JWT Parsing in Dunaj
(ns foo.core
(:api dunaj)
(:require [dunaj.host.int :refer [i== iDOT]]
[dunaj.host.array :as dha]
[dunaj.format.base64 :refer [base64-safe]]
[dunaj.coll.recipe :refer [concat*]]
[dunaj.concurrent.port :refer [reduce! onto-chan!]]))
(def+ ByteColl [java.lang.Byte]) ;; type signature
@wagjo
wagjo / gist:e3a273636b7d5a542172
Created August 18, 2015 14:49
emacs keybinding for setting window width
(defun set-window-width (n)
"Set the selected window's width."
(adjust-window-trailing-edge (selected-window) (- n (window-width)) t))
(defun set-70-columns ()
"Set the selected window to 80 columns."
(interactive)
(set-window-width 70))
(global-set-key "\C-x`" 'set-70-columns)
;;;; transducer parser
;; channel that parses input bytes into utf8 chars
(def c (chan 10 (parse utf8)))
;;=> #'foo.baz/c
;; vector of utf8 encoded characters
(def v [-16 -99 -109 -105 -16 -99 -109 -82 -16 -99 -109 -75 -16 -99 -109 -75 -16 -99 -109 -72 32 -16 -99 -108 -128 -16 -99 -109 -72 -16 -99 -109 -69 -16 -99 -109 -75 -16 -99 -109 -83])
;;=> #'foo.baz/v
@wagjo
wagjo / gist:9212ca5e3a395f839707
Created October 8, 2014 09:30
transducers with wrap
(defprotocol IReducing
"A value protocol for augmented reducing functions."
(-init
"Returns default initial unwrapped value."
[this])
(-finish
"Returns final wrapped result, and performs flushing or cleaning
of internal state. Returned wrap should have cleaned state,
so that eventual subsequent calls to -finish will perform
correctly.
@wagjo
wagjo / gist:30000277f6923ac8d88f
Last active August 29, 2015 14:04
Wrapping reducer vs reducer with atom
(ns redtest.core
(:require [clojure.core.reducers]
[clojure.core.protocols :refer
[CollReduce coll-reduce]]))
;; state is kept in a ret
(deftype WrapState [ret n])
(deftype WrapTake [coll n]
@wagjo
wagjo / gist:d6419a289a64c503010c
Last active August 29, 2015 14:02
Linked data in EDN
JSON-LD
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
@wagjo
wagjo / gist:b687158ab0ff6aa8cb33
Last active August 29, 2015 14:00
Foldable multireducible
;; excerpt of foldable multireducible impl
;;
;; - introducing concept of mutable iterator, which is
;; much faster than seq
;; - this is for a (yet unreleased) custom patched clojure,
;; where clojure.lang interfaces can behave like protocols
;; - map is reducer variant, thats why I have to call seq on it
;; - seq is patched to support any reducible, not just Seqable
(defn fold-sectionable
@wagjo
wagjo / gist:10017343
Last active August 29, 2015 13:58
Upcoming library* for multireducibles
;; Multireducibles (with support for folding)
(def s1 "Hello World")
(def s2 "lllllllllll")
;; create multireducible with zip
(seq (zip s1 s2))
;; => ((\H \l) (\e \l) (\l \l) (\l \l) (\o \l) (\space \l) (\W \l) (\o \l) (\r \l) (\l \l) (\d \l))