Skip to content

Instantly share code, notes, and snippets.

(defn separate-by [f coll]
"Separates coll into two groups by predicate f
example:
=> (separate-by odd? (range 20))
[[1 3 5 7 9 11 13 15 17 19] [0 2 4 6 8 10 12 14 16 18]]
"
(let [groups (group-by (comp boolean f) coll)]
[(groups true) (groups false)]))
;; or
def _get_class(class_name):
parts = class_name.split(".")
module = ".".join(parts[:-1])
m = __import__(module)
for comp in parts[1:]:
m = getattr(m, comp)
return m
@rotaliator
rotaliator / app.cljs
Last active February 20, 2019 09:19
Minimal shadow-cljs app
(ns example.app
(:require [reagent.core :as reagent :refer [atom]]))
(defn app []
[:div
[:h1 "Shadow cljs!"]])
(defn mount-root []
(reagent/render [app] (.getElementById js/document "app")))
(defn lazy-file-lines [file]
(letfn [(helper [rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (helper rdr))
(do (.close rdr) nil))))]
(helper (clojure.java.io/reader file))))
@rotaliator
rotaliator / save_pixels_as_png.clj
Last active January 29, 2019 08:13
Save sequence of pixel values as grayscale image
(ns gist.core
(:require [clojure.string :as str])
(:import (java.io File)
(java.awt.image BufferedImage
WritableRaster)
(javax.imageio ImageIO))
(:gen-class))
(defn save-pixels-as-png
(def conn (atom nil))
(defn ensure-conn [config] (swap! conn #(or % (connect config))))
import json
from functools import wraps
from hashlib import sha1
CACHE_DIR = '_cache/'
MAX_FILENAME_LENGTH = 40
class CacheNotFound(Exception):
pass
@rotaliator
rotaliator / copy_to_clipboard.cljs
Last active February 1, 2021 15:04
#clojurescript copy to clipboard by Robert Stuttaford @RobStuttaford
;; https://twitter.com/RobStuttaford/status/1095434901950734337
(defn copy-to-clipboard [val]
(let [el (js/document.createElement "textarea")]
(set! (.-value el) val)
(.appendChild js/document.body el)
(.select el)
(js/document.execCommand "copy")
(.removeChild js/document.body el)))
(defn str->int
([s] (str->int s 10))
([s base]
#?(:clj (java.lang.Integer/parseInt s base)
:cljs (js/parseInt s base))))
(ns edn-pprint.core
(:require
[clojure.pprint :refer [pprint]]
[clojure.edn :as edn]))
(pprint (edn/read-string (apply str (line-seq (java.io.BufferedReader. *in*)))))