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 / yt-mpv.el
Created February 5, 2019 10:58
Opening YouTube Links In MPV from Emacs
(defun mpv-play-url (url &rest args)
""
(interactive)
(start-process "mpv" nil "mpv" url))
(setq browse-url-browser-function
(quote
(("youtu\\.?be" . mpv-play-url)
("." . eww-browse-url))))
@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 / paths.clj
Created April 5, 2020 07:21
all paths in a nested map
(defn map-or-vec?
[x]
(or (map? x) (vector? x)))
;;; https://www.reddit.com/r/Clojure/comments/7wl7nt/get_a_list_of_all_paths_in_a_nested_map/
(defn paths
([m]
(paths [] [] m))
([ps ks m]