Skip to content

Instantly share code, notes, and snippets.

@joinr
joinr / hangman.clj
Created April 26, 2024 15:10
port of demo hangman project to maps
(ns first-cloj.core)
(def list-of-words ["horse" "dog" "bird"])
(def init {:guesses [] :incorrect-guesses 0})
(def hang-vec
[" ______"
" | |"
" O |"
"/|\\ |"
@joinr
joinr / transform.clj
Created March 29, 2024 03:10
Meander experiments with vega
(require '[meander.epsilon :as m])
(require '[meander.strategy.epsilon :as m*])
(def simplify-conjunctions
(m*/rewrite
[:and_expr ?x] ?x
[:or_expr ?x] ?x))
(def simplify-all-conjs
(m*/bottom-up
@joinr
joinr / transit.cljs
Last active March 21, 2024 00:20
messing with transit to avoid the clj->js tax
(require '[cognitect.transit :as t])
(def jt js/transit)
(def r (t/reader :json))
(def w (t/writer :json))
(def jr (jt.reader "json"))
@joinr
joinr / parallelism.txt
Created January 16, 2024 21:26
paralleldiscuss
Outside of the bevy of good answers, I think there's some utility in discerning between concurrency and parallelism. I like rob pike's talk "Concurrency is not Parallelism". Clojure has great features for handling concurrency (dealing with lots of things happening at the same time) and enabling correct solutions without much effort. Those features extend into enabling parallelism (doing lots of things at the same time). I think the immutable data model and the early focus on integrated STM constructs (designed to be used with the immutable data structures) helped a lot. core.async borrowing the communicating sequential processes (CSP) from go added even more options for correct concurrency.
So if you can write correct concurrent programs, then you can begin to leverage system resources (e.g. cores) to distribute how work is done and maybe get some performance improvement on top (or maybe not, or maybe up to a point....it depends).
Alternately, if you have a bunch of independent work that you want to chew th
@joinr
joinr / brute.clj
Last active December 5, 2023 21:48
revised brute force day 5-2
(defn some-fast [f xs]
(reduce (fn [acc x]
(let [res (f x)]
(if res (reduced res)
acc)))
false xs))
(defn toDigits [s] (mapv read-string (re-seq #"\d+" s)))
(defn parseInput [d]
@joinr
joinr / primred.clj
Created October 22, 2023 07:12
primitive reducing, now primitive backed vectors don't have to suck as much.
(ns prims)
(defprotocol IPrimitiveReducible
(reduce-prim- [this f init]))
(definterface IPrimitiveReduce
(^long LLLreduce [^clojure.lang.IFn$LLL f ^long init])
(^double DDDreduce [^clojure.lang.IFn$DDD f ^double init]))
(def am->type
@joinr
joinr / project.clj
Created July 19, 2023 12:58
upated harmonikit project file
(require '[leiningen.core.utils :refer [get-os]])
(def JVMOPTS
"Per os jvm options. Options common to all cases go under
`:any`. Options specific to one OS go under the key returned by
`leiningen.core.eval/get-os` for that system. Temporarily disabled
options can be kept under `:disabled`."
{:any
["-Xms512m" "-Xmx1g" ; Minimum and maximum sizes of the heap
"-XX:MaxGCPauseMillis=20" ; Specify a target of 20ms for max gc pauses
@joinr
joinr / threadjunk.clj
Created June 15, 2023 05:21
exgenous pseuo thread-local variable concept.
(ns threadjunk
(:import [java.util.concurrent ConcurrentHashMap]
[java.util.function Function]))
;;satisfy conc hashmap's needs..
(defn Func ^Function [f]
(reify Function
(apply [this v] (f v))))
(defn get-binding! [^ConcurrentHashMap ctx k ^Function f]
@joinr
joinr / tutorialmod.clj
Created June 13, 2023 02:04
modifications of a clojure tutorial
(def data {:people [{:username :bilbo} {:username :jim}]})
;;benign mutation.
(defn person-joined2 [state person]
(let [username (:username person)
seen (atom nil)
is-joined-user? #(when (= username (:username %))
(reset! seen username))]
(update state :people
(fn [people]
@joinr
joinr / partial.clj
Created June 12, 2023 06:05
partial function application with named args
(defprotocol INominal
(partial-nominal [this args]))
(defn ->args [xs]
(let [arg->idx (zipmap xs (range (count xs)))]
(reduce (fn [acc k] (assoc acc k :binding/unbound))
(sorted-map-by (fn [l r]
(compare (arg->idx l) (arg->idx r)))) xs)))
(defn supply [params xs]