Skip to content

Instantly share code, notes, and snippets.

View pbaille's full-sized avatar
🏠
Working from home

Pierre Baille pbaille

🏠
Working from home
  • Freelance
  • France
View GitHub Profile
@pbaille
pbaille / dfn.clj
Created February 9, 2017 22:18
experimental function structure
(ns exp.dfn
(:refer-clojure :exclude [take eval compile apply]))
(alias 'c 'clojure.core)
(defn destructure*
"stolen from core, just add options"
[bindings & [{:keys [unbound-val default-bindings on-unbound]
:or {unbound-val nil default-bindings {} on-unbound identity}}]]
(let [bents (partition 2 bindings)
;; error handling -----------------------------------
(defn check-cyclic-prefs [sd spec specs]
(assert (not-any? #(contains? (set (get @(:prefs sd) %))
spec)
specs)
(str "circular dependency:"
"\nname: " (:name sd)
"\nspec:" spec
"\nconflicts: " (keep #(contains? (set (get @(:prefs sd) %))
; Copyright (c) 2015 Designed.ly, Marcin Bilski
; The use and distribution terms for this software are covered by the
; Eclipse Public License which can be found in the file LICENSE at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns reforms.core.import
(:require [cljs.analyzer.api :as ana-api]))
(defmacro import-vars [[_quote ns]]
(ns reforms.core.import
(:require [cljs.analyzer.api :as ana-api]))
(defmacro import-vars [[_quote ns]]
`(do
~@(->>
(ana-api/ns-interns ns)
(remove (comp :macro second))
(map (fn [[k# _]]
`(def ~(symbol k#) ~(symbol (name ns) (name k#))))))))
@pbaille
pbaille / upload-file.cljs
Last active January 31, 2017 09:41
file uploader in cljs
(defn file-input [on-result]
[:input {:type "file"
:on-change
(fn [e]
(let [f (first (array-seq (.. e -target -files)))
reader (js/FileReader.)]
(aset reader "onload"
(fn [e]
(on-result (.. e -target -result))))
(.readAsText reader f)))}])
@pbaille
pbaille / download-file.cljs
Last active January 31, 2017 09:22
simple file downloader in cljs
(defn download [data filename & [format]]
(let [format (or format "application/octet-stream")
a (.createElement js/document "a")]
(aset a "href" (str "data:text/plain;charset=utf-8," (pr-str data)))
(aset a "download" filename)
(.click a)
(.removeChild js/document a)))
;; Transparent Functions
;; I was experimenting with transducers and wanted a way to understand how they worked. Transducer
;; code uses many nested functions in various locations with other nested functions defined as local
;; variables in scope. Typically after an anonymous Clojure function is defined you have no visibility
;; into the locals that were in scope when the function was defined, where the function came from,
;; or the code in the function. I defined a macro, tfn, that creates a transparent function. It's
;; a normal Clojure function with additional metadata including the function code and local
;; variable names and values.
(require '[reagent.core :as r])
(defn i []
(let [dims (r/atom {})
this (atom nil)
resized? (atom nil)]
(fn [][:div
[:img {:src "http://www.lispcast.com/img/pre-conj/rich-hickey.jpeg"
:ref (fn [x] (reset! this x))
:on-load
@pbaille
pbaille / qparamsParser.js
Created January 6, 2017 09:10
query params to hashmap
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
(defn set-timeout [f millis]
(let [running (atom true)]
(future
(while @running
(f)
(Thread/sleep millis)))
#(reset! running nil)))
;; start an interval, the stop function is returned and bound to stop-interval var
(def stop-interval (set-interval #(println "yop") 1000))