Skip to content

Instantly share code, notes, and snippets.

@joinr
joinr / wierd.clj
Last active December 12, 2017 10:27
bizarre destructuring behavior
(def m {:a ["a" [1 2 3]] :b ["b" [4 5 6]]})
(for [[k v] m
[l xs] v]
[l xs])
;;returns...
;;([\a nil] [1 2] [\b nil] [4 5])
;;Wat?
;;Why are strings being destructured this way?
;;Expected
@joinr
joinr / heatwierd.clj
Created February 3, 2018 18:03
incanter heat-map artifact example
(ns heat
(:require [incanter [core :as i] [charts :as c]]))
(defn f [x y] (i/minus (i/sq x) (i/sq y)))
;;looks good
(i/view (c/heat-map f -10 10 -10 10))
;;boxes for xyblock renderer are too small,
;;leaving gaps that show the grey background
;;behind, giving the appearance of "lines"
;;all over the plot.
@joinr
joinr / demo.lisp
Last active March 22, 2018 07:49
error demo
(defpackage :err-demo
(:use :common-lisp))
(in-package :err-demo)
(defun str (x &rest xs)
(format nil "~{~a~}" (cons x xs)))
(defun show-excuse (excuse)
((print (str excuse " is no excuse for poor errors!"))))
@joinr
joinr / Clojure.java
Last active March 30, 2018 20:52
output from Common Lisp Clojure Compiler, Clojure Java RT Front-end, circa May 2006
/* Generated by Clojure */
package org.clojure;
import org.clojure.runtime.*;
public class Clojure{
/* Generated by Clojure from the following Lisp:
(defn* f0 (NIL))
*/
static public class f0 extends AFn{public Object invoke(ThreadLocalData __tld) throws Exception
{return null;}}
/* Generated by Clojure from the following Lisp:
@joinr
joinr / specproblem.clj
Created May 22, 2018 08:19
spec problem?
(ns spec-problem)
;;This is a smalle example of some circa clojure 1.7 code I found
;;that spec seemed to dislike when I was trying to port it.
;;It comes from the fn-fx library by way of the freactive.core
;;library fyi.
;;dumb function pre-defined...
(defn my-fn [x] (+ x 2))
;;dumb macro that just builds an anonymous fn that
@joinr
joinr / incdebug.clj
Created July 26, 2018 19:38
error in incanter's delegation to core.matrix for building datasets from xls due to sparse data
(ns incdebug.core
(:require [incanter [excel :as xls] [core :as i]]
[clojure.core.matrix.impl.dataset :as ds]
[clojure.core.matrix.protocols :as mp]))
;;this is what incanter is doing....we'll tease it apart
;;to debug.
#_(defn- read-sheet [rows-it header-keywords]
(let [colnames (incanter.excel.cells/read-line-values (first rows-it))
rows (->> (rest rows-it)
@joinr
joinr / asyncpromise.clj
Created August 9, 2018 07:30
example of hacky promise implementation in core.async
(ns blah
(:require [clojure.core.async :as a]))
(defn error [x] {:error x})
(defn success [x] {:success x})
(defmacro maybe [expr]
`(->> (try (success ~expr)
(catch ~'Exception e# (error e#)))))
@joinr
joinr / spectruncate.clj
Created August 18, 2018 10:50
example of using spec to truncate keys....
;;So for my incoming API boundary, I only would retain keys defined in
;;spec after receiving data from client (to wholesale limit potentially
;;dangerous crafting of json/edn payload).
;;Likewise, at my outgoing boundary, I would similarly only retain keys
;;defined in spec (sending absolute minimum data necessary).
(ns incoming
(:require [clojure.spec.alpha :as s]))
@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))))