Skip to content

Instantly share code, notes, and snippets.

View beders's full-sized avatar

Jochen Bedersdorfer beders

View GitHub Profile
@rickerbh
rickerbh / octopress-to-cryogen.clj
Created July 26, 2020 06:40
Babashka script to port posts from jekyll/octopress format to cryogen
(require '[clojure.java.io :as io]
'[clojure.string :as string]
'[clojure.tools.cli :refer [parse-opts]])
(def cli-options
[["-o" "--out DIR" "Output directory"
:default "out"]
["-s" "--source SOURCE" "Source directory"
:default "source/_posts"]
["-h" "--help"]])
@zoren
zoren / download-blob.cljs
Created June 18, 2020 15:21
Download a BLOB via a object URL in ClojureScript
;; heavily inspired by Mariano Guerra
;; http://marianoguerra.org/posts/download-frontend-generated-data-to-a-file-with-clojurescript.html
(defn download-blob [file-name blob]
(let [object-url (js/URL.createObjectURL blob)
anchor-element
(doto (js/document.createElement "a")
(-> .-href (set! object-url))
(-> .-download (set! file-name)))]
(.appendChild (.-body js/document) anchor-element)
(.click anchor-element)
@lispyclouds
lispyclouds / monads.clj
Last active May 17, 2020 02:37
Monads, simple made easier
; A simple demo of monadic composition of side effects
; Program to take 3 nubers as input and print their sum.
(defn read-and-add!
[prev]
(print "Enter a number: ")
(+ prev (do (flush)
(Integer/parseInt (read-line)))))
(defn bind
@borkdude
borkdude / js_parser.clj
Last active August 6, 2023 22:14
Parse JavaScript in Clojure using the Google Closure Compiler
#!/bin/sh
#_(
"exec" "clojure" "-Sdeps" "{:deps {org.clojure/clojurescript {:mvn/version \"1.10.520\"}}}" "$0" "$@"
)
;; running js_parser.clj "function foo(x) { var y = x + 1; }" will print:
;; [{:type :function, :name "foo", :body [{:variable-statement [{:lvalue "y", :initializer {:type :binary-op, :left "x", :operator "+", :right "1"}}]}], :params ["x"]}]
@sudodoki
sudodoki / flatten-maps.clj
Last active November 29, 2023 00:26
Flatten nested maps using clojure / clojurescript using compound keys
(defn get-key
[prefix key]
(if (nil? prefix)
key
(str prefix "-" key)))
(defn flatten-map-kvs
([map] (flatten-map-kvs map nil))
([map prefix]
(reduce
(fn [memo [k v]]
Benchmark: transform values of a small map (1000000 iterations)
Avg(ms) vs best Code
141.61 1.00 (map-vals-map-iterable data inc)
146.86 1.04 (reduce-kv (fn [m k v] (assoc m k (inc v))) {} data)
156.66 1.11 (reduce-kv (fn [m k v] (assoc m k (inc v))) (empty data) data)
161.20 1.14 (transform MAP-VALS inc data)
211.53 1.49 (persistent! (reduce-kv (fn [m k v] (assoc! m k (inc v))) (transient {}) data))
227.91 1.61 (map-vals-map-iterable-transient data inc)
332.09 2.35 (transform [ALL LAST] inc data)
@ravihara
ravihara / jjs-require.js
Created January 13, 2016 13:23
CommonJS require function implementation for Nashorn / Java8
// Based on https://github.com/nodyn/jvm-npm
"use strict";
(function() {
var System = java.lang.System,
File = java.io.File,
FileInputStream = java.io.FileInputStream,
StringBuilder = java.lang.StringBuilder,
BufferedReader = java.io.BufferedReader,
InputStreamReader = java.io.InputStreamReader;
@CraZySacX
CraZySacX / git.clj
Last active November 24, 2020 19:23
Clojure git wrapper
(ns org.ozias.git
(:require [me.raynes.conch :refer (with-programs)]))
(def ^:private git-porcelain
["add" "am" "bisect" "branch" "bundle" "checkout" "cherry-pick" "citool"
"clean" "clone" "commit" "describe" "diff" "fetch" "format-patch"
"gc" "grep" "gui" "init" "log" "merge" "mv" "notes" "pull" "push"
"rebase" "reset" "revert" "rm" "shortlog" "show" "stash" "status"
"submodule" "tag"])
@hiredman
hiredman / boot.cljs
Created March 15, 2013 04:43
clojurescript drag and drop
(defn handle-file-select [evt]
(.stopPropagation evt)
(.preventDefault evt)
(let [files (.-files (.-dataTransfer evt))]
(dotimes [i (.-length files)]
(let [rdr (js/FileReader.)
the-file (aget files i)]
(set! (.-onload rdr)
(fn [e]
(let [file-content (.-result (.-target e))
@jboner
jboner / latency.txt
Last active May 29, 2024 05:28
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD