Skip to content

Instantly share code, notes, and snippets.

@bsless
bsless / lambda_on_dict.py
Created November 27, 2018 07:49
Applying lambdas to dictionaries in python
d = dict(a=1, b=2, c=3)
# it really should not be that weird
list(map(lambda k, v: (k, 2*v), *zip(*d.items())))
@bsless
bsless / core-logic-perf-gotcha.md
Last active January 7, 2020 12:48
Small Clojure core.logic performance gotcha

Seems like when using a logic db in core.logic most of the CPU cicles are spent on manipulating the thead bindings the *logic-dbs* dynamic var. However, it is only used as a wrapper when calling the underlying -run macro. Invoking it directly speeds things up significantly

Let's set up some dummy data:

(require
 '[clojure.core.logic.pldb :as pldb]
@bsless
bsless / split.clj
Created February 17, 2020 07:15
All the ways to split a sequence in two based on a predicate
(ns clj-seperate-bench.core
(:gen-class)
(:require
[criterium.core :as c]))
;;; Run with:
;;; java -Xmx8G -Xms8G -XX:+UseG1GC -jar target/uberjar/clj-seperate-bench-0.1.0-SNAPSHOT-standalone.jar
;; 54, 60 ms range
;; 54, 34 ms vector
@bsless
bsless / burrito.clj
Created March 1, 2020 15:44
Emit lojure code to
(defn camel->snake
[s]
(-> s
(clojure.string/replace #"([^_A-Z])([A-Z])" "$1-$2")
clojure.string/lower-case))
(defn gen-param*
[c]
(->
c
@bsless
bsless / burrito.clj
Last active March 1, 2020 18:08
Emit Clojure code to warp java class
(defn camel->snake
[s]
(-> s
(clojure.string/replace #"([^_A-Z])([A-Z])" "$1-$2")
clojure.string/lower-case))
(defn gen-param*
[c]
(->
c
(defn get-lens [k] (fn [m] (get m k)))
(defn get-in-lens
[ks]
(reduce
(fn [f k]
(fn [m]
(let [lens (get-lens k)]
(lens (f m)))))
identity
@bsless
bsless / dissoc-in.clj
Last active March 26, 2020 15:59
probably the simplest way to implement dissoc-in
(defn dissoc-in
[m ks]
(let [[k & ks] ks]
(if ks
(let [found (find m k)]
(if found
(assoc m k (dissoc-in (val found) ks))
m))
(dissoc m k))))
@bsless
bsless / cl-cond.clj
Created April 4, 2020 14:39
common lisp cond allows returning the predicate's value. This form does it via reader macros
(defmacro cond*
[& clauses]
(when clauses
(let [p (first clauses)
return? (:return (meta p))]
(if return?
`(if ~p
~p
(cond* ~@(next clauses)))
`(if ~p
@bsless
bsless / gol.clj
Created May 4, 2020 14:40
Game of life in Clojure + seesaw, based on reddit thread, see comments
;;;
;;; see https://www.reddit.com/r/Clojure/comments/gcry9d/why_does_my_clojure_implementation_of_conways/
;;; for initial implementation
;;;
(ns gol.core
(:gen-class)
(:import
[java.awt Color Graphics])
(:use seesaw.core
@bsless
bsless / prod.clj
Created May 26, 2020 12:40
Eager cartesian product between two seqs in Clojure
(defn- pair
[k v]
(clojure.lang.MapEntry. k v))
(defn prod
[xs ys]
(persistent!
(reduce
(fn [acc x]
(reduce