Skip to content

Instantly share code, notes, and snippets.

@joinr
joinr / pyparser.clj
Last active December 8, 2019 10:24
a parser for python arguments into arg lists for libpython-clj
(ns pyparser
(:require [clojure.spec.alpha :as s]
[clojure.string]))
;;given:
;;def foo(a, b, c=5, *args, d="dog", **kwargs):
;; ...
;;define a function that emits
;;'([a b & [args {c :c d :d :as kwargs :or {c 5 d "dog"}}]]
@joinr
joinr / mapextract.clj
Created November 12, 2019 05:15
performance inquiries into extracting info from maps
(ns mapextract
(:require [criterium.core :as criterium]))
(def m
(zipmap [:a :b :c :d :e :f :g :h :i :j :k :l :m :n :o :p]
(repeat :bingo)))
(def arr-map (select-keys m [:a :b :c :d]))
(def xs (vec (repeat 100 m )))
(def arr-xs (vec (repeat 100 m )))
@joinr
joinr / iter.clj
Created November 6, 2019 11:46
messing with iterators
(def able
(reify Iterable
(iterator [this] (let [i (atom 0)]
(reify java.util.Iterator
(hasNext [this] true)
(next [this]
(print ".")
(swap! i inc)))))))
(defn lazyrator [^java.util.Iterator iter]
@joinr
joinr / splice.clj
Created September 24, 2019 21:06
a demonstration of how trivial it is to splice things in clojure
`[~(range 10) ~[:this :vector :is] ~{:brought "to" :you "by"} ~@[:splicing :vectors]
~#{:sets} :and ~@'(:lists :together) :somewhat :trivially
~@{:even {:nested [:vectors]} :and [:maps :of :stuff] :are :easy} #{~@[:so :are :sets]}
~(eval `[:its :even [:easy ~@[:to :splice :quoted :forms]
~{:of :varying :levels #{:of-depth}}]])]
;;yields
[(0 1 2 3 4 5 6 7 8 9)
[:this :vector :is]
{:brought "to", :you "by"}
@joinr
joinr / pubsubcategories.clj
Created September 19, 2019 21:41
A simple core.async example of splitting up processing flows using pub/sub and pipeline-blocking
(ns workdist
(:require [clojure.core.async :as a :refer [chan >!! <!! <! >! go go-loop]]))
;;define some simple workloads
(def payloads
{:red (vec "red")
:green (vec "green")
:blue (vec "blue")})
;;Generate 100 messages of :red :green :blue categories
(ns par
(:require [clojure.core.async :as a]))
(def my-data-log (atom []))
(defn fetch-data! [n]
(Thread/sleep 1e3)
(swap! my-data-log conj (str "Fetched record: " n))
n)
@joinr
joinr / numberdemo.clj
Created June 26, 2019 20:41
a simple exploration of different ways to print monetarily formatted strings of numbers
(ns numberdemo
(:require [clojure.pprint :as pprint]))
;;from Craig
;;doesn't handle decimals.
(defn commarize
"given the string representation of a number, insert a comma after
every three digits."
[num-str]
;;reverse the string since the first comma starts from the right
@joinr
joinr / faster.clj
Created June 20, 2019 06:17
messing with clojure to see if any more performance can squeeze out for perfect.reader.fast
(ns perfect.reader.faster
(:require
[perfect.reader.generics :as generics]
[clojure.spec.alpha :as s]
[net.danielcompton.defn-spec-alpha :as ds])
(:import
(org.dhatim.fastexcel.reader ReadableWorkbook
Sheet
Row
Cell
@joinr
joinr / shim.clj
Last active June 20, 2019 22:42
example of a main shim namespace for clojure to avoid AOT'ing everything and allow fast starts
;;Shim class for running app without
;;aot compilation issues.
;;entrypoint for some gui.
(ns blah.main
(:gen-class :main true))
;;This is the main entry point for the app.
;;It's a good example of a shim-class, and
;;requires some arcane features to get things
;;working to avoid aot compilation of dependent
@joinr
joinr / spectraining.clj
Created June 4, 2019 18:39
spec experiment with org
;;A port of an awk script that munges
;;code to .org mode documentation,
;;let's do it with spec!
(ns spectraining.doc
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[clojure.spec.test.alpha :as stest]
[clojure.string :as str]))