Skip to content

Instantly share code, notes, and snippets.

View metametadata's full-sized avatar

Yuri Govorushchenko metametadata

View GitHub Profile
@metametadata
metametadata / core.cljs
Created November 2, 2017 10:11
ClojureScript read env in NodeJS
(defn -js->clj+
"For cases when built-in js->clj doesn't work. Source: https://stackoverflow.com/a/32583549/4839573"
[x]
(into {} (for [k (js-keys x)]
[k (aget x k)])))
(defn env
"Returns current env vars as a Clojure map."
[]
(-js->clj+ (.-env js/process)))
@metametadata
metametadata / spec-plus.cljc
Last active March 4, 2019 12:22
Helpers for core.spec. Also see the related discussion: https://groups.google.com/forum/#!topic/clojure/i8Rz-AnCoa8
(ns libs.spec-plus
"Helpers for core.spec. Only tested in Lumo at the moment."
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[clojure.set :as set]
[cljs.analyzer :as ana])
#?(:cljs (:require-macros [libs.spec-plus])))
(defmacro speced-keys
"The same as s/keys but asserts that all keys have specs already registered.
@metametadata
metametadata / core.cljs
Last active September 29, 2017 15:38
a taste of core.async with <?
"
Naming conventions:
go - executes code which contains special blocking operations (e.g. <!), returns a channel, throws global uncaught exceptions
<! - reads from channel without any special treatment of returned error values
<? - reads from maybe-channel, if there's an error value - throws it
<?go - the same as go but \"swallows\" exceptions and returns a maybe-channel
<foo/<?foo:
1) channel/maybe-channel
2) a function returning a channel (e.g. <?fetch-items)
@metametadata
metametadata / spawn-in-lumo.cljs
Last active July 14, 2017 18:51
Example of using NodeJS spawn in Lumo
(def child-process (js/require "child_process"))
(def on-exit (js/require "signal-exit"))
(defn -spawn
"Thin wrapper around NodeJS function.
Spawns the process in the background. Returns the created ChildProcess instance."
[command args options]
(let [name (str command " " (str/join " " args))]
(println "ᐅ" name)
(-> (.spawn child-process
@metametadata
metametadata / react-bootstrap.cljs
Last active June 6, 2017 07:56
Fixing of caret/cursor jumps in text inputs of cljsjs.react-bootstrap
(ns app.components.react-bootstrap
(:require [cljsjs.react-bootstrap]
[reagent.core :as r]))
(def Alert (r/adapt-react-class (.-Alert js/ReactBootstrap)))
(def Button (r/adapt-react-class (.-Button js/ReactBootstrap)))
; ...
(def FormControl (r/adapt-react-class (.-FormControl js/ReactBootstrap))) ; WARNING: use FormControlFixed for text input controls instead
(defn FormControlFixed
(ns frontend.counter
(:require [frontend.ui :as ui]
[cljs.core.match :refer-macros [match]]
[reagent.core :as r]))
(defn init
"Creates a model intance."
[x]
x)
@metametadata
metametadata / asserts.cljc
Last active June 8, 2017 02:50
Portable Clojure/ClojureScript wrappers for (is (thrown-with-msg? ...)) test assertions
#?(:clj
(defn- cljs-env?
"Take the &env from a macro, and tell whether we are expanding into cljs.
Source: http://v.gd/rmKNdf"
[env]
(boolean (:ns env))))
#?(:clj
(defmacro is-exception-thrown
"(is (thrown-with-msg? ...)) for specified exceptions in Clojure/ClojureScript."
@metametadata
metametadata / frontend-api-core.cljs
Created June 24, 2015 13:04
core.async error handling using <? macro (ClojureScript)
(ns frontend.api.core
(:require [ajax.core :as ajax]
[cljs.core.async :refer [chan put! close!]]
[frontend.async.core :refer [channel-error]]))
(defn <?GET [url]
"Async. Returns a maybe-channel."
(let [<?chan (chan)]
(ajax/GET url
{:handler #(do (put! <?chan %)
@metametadata
metametadata / gist:7ea557f8a2f1bdddd574
Created June 18, 2015 11:48
sleep in clojurescript
(defn sleep [msec]
(let [deadline (+ msec (.getTime (js/Date.)))]
(while (> deadline (.getTime (js/Date.))))))
@metametadata
metametadata / gist:0097efa1a081ce6d7bb3
Last active June 8, 2017 02:51
listen to clipboard in Clojure using core.async
;...
(:import java.awt.Toolkit)
(:import (java.awt.datatransfer DataFlavor))
;...
(defn- printer
[in]
(go (while true (println (<! in)))))
(defn- clipboard-listener