Skip to content

Instantly share code, notes, and snippets.

@joinr
joinr / forfold.clj
Created August 25, 2018 02:32
for/fold from racket interpreted to clojure
(ns forfold)
;;From racket example
;;https://docs.racket-lang.org/reference/for.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Ffold%29%29
;; (for/fold ([acc '()]
;; [seen (hash)]
;; #:result (reverse acc))
;; ([x (in-list '(0 1 1 2 3 4 4 4))])
;; (cond
;; [(hash-ref seen x #f)
@joinr
joinr / magic-let.lisp
Created August 30, 2018 17:40
A simple example of providing reader and macro support for custom data structures, to include allowing them as binding forms.
;;util function to help with bindings
(defun partition (n l)
(do ((remaining l (setf remaining (subseq remaining 2)))
(acc (list)))
((null remaining) (nreverse acc))
(let ((nxt (subseq remaining 0 2)))
(if (= (length nxt) n)
(push nxt acc)
(setf remaining nil)))))
;;a dumb struct to wrap a list. solely for custom
@joinr
joinr / picker.clj
Created September 18, 2018 09:49
An exploration into various schemes for picking values relative to preferred/prioritized indices according to predicates
(ns picking
(:require [clojure.pprint :as pprint]))
;original implementation from reddit.
(defn pick-best? [pred possible-positions]
(let [ps (mapv #(when (pred %) %) possible-positions)]
(or (second ps)
(first ps)
(nth ps 2))))
@joinr
joinr / bubble.clj
Last active September 18, 2018 23:31
examples to help engender an understanding for how to do things in clojure like you'd do in other languages
(ns bubble
(:require [clojure.pprint :as pprint]))
;;1.summing up a list of numbers and storing the result
(def the-sum
(let [xs (range 10)]
(reduce + xs)))
;;2.iterating through a list looking for a specific value(s)
@joinr
joinr / fnfxformexample.clj
Created January 8, 2019 19:32
an extend example of the fn-fx form example, this time with a timer and a label that updates the model (reflected in the UI) programatically.
(ns getting-started.reactlike
(:require [fn-fx.fx-dom :as dom]
[fn-fx.diff :refer [component defui render should-update?]]
[fn-fx.controls :as ui]))
(def firebrick
(ui/color :red 0.69 :green 0.13 :blue 0.13))
;; The main login window component, notice the authed? parameter, this defines a function
;; we can use to construct these ui components, named "login-form"
@joinr
joinr / todocorrect.clj
Created January 8, 2019 23:07
Corrected example of TODO app in fn-fx
(ns other-examples.todo
(:require [fn-fx.fx-dom :as dom]
[fn-fx.diff :refer [component defui render should-update?]]
[fn-fx.controls :as ui]))
(def main-font (ui/font :family "Helvetica" :size 20))
(defui TodoItem
(render [this {:keys [done? idx text]}]
(ui/border-pane
@joinr
joinr / todocorrect.clj
Created January 8, 2019 23:07
Corrected example of TODO app in fn-fx
(ns other-examples.todo
(:require [fn-fx.fx-dom :as dom]
[fn-fx.diff :refer [component defui render should-update?]]
[fn-fx.controls :as ui]))
(def main-font (ui/font :family "Helvetica" :size 20))
(defui TodoItem
(render [this {:keys [done? idx text]}]
(ui/border-pane
@joinr
joinr / cljdisassembly.java
Last active January 11, 2019 21:55
clj loop disassembled
//(decomp/decompile (let [bound 10]
// (loop [x 1
// acc (java.util.ArrayList.)]
// (if (== bound x)
// acc
// (recur (unchecked-inc x)
// (if (odd? x) (doto ^java.util.ArrayList acc (.add (* x x)))
// acc))))))
// Decompiling class: user$fn__13411
@joinr
joinr / cardflip.clj
Last active February 14, 2019 15:51
optimizing a brute force search for the card flipping game....
(ns clojure-playground.core)
(def samples
{"0100110" "1 0 2 3 5 4 6"
"01001100111" "No solution"
"100001100101000" "0 1 2 3 4 6 5 7 8 11 10 9 12 13 14"
})
(def easy "0100110")
(def hard "001011011101001001000")
@joinr
joinr / system-example.clj
Created March 27, 2019 21:41
an example of a core.async process manager thing
(ns system-example
(:require [spork.async.system :as sys]
[clojure.core.async :as async :refer [chan]]))
(def ins (chan (async/dropping-buffer 1)))
(def outs (chan (async/dropping-buffer 1)))
(sys/go-push :clock ins (let [res (async/<! (async/timeout 1000))]
(System/currentTimeMillis)))