Skip to content

Instantly share code, notes, and snippets.

View mhuebert's full-sized avatar

Matt Huebert mhuebert

View GitHub Profile
@mhuebert
mhuebert / eval-with-ns.cljs
Last active August 29, 2015 14:26
`def` in a namespace, cljs.js/eval
(cljs/eval (cljs/empty-state)
'(def x 1)
{:eval cljs/js-eval
:context :expr
:def-emits-var true
:ns cells.core
}
#(pprint %))
@mhuebert
mhuebert / cljs-example.md
Last active August 29, 2015 14:26
ClojureScript with zero dependencies (browser)

ClojureScript from master w/ zero dependencies

  1. git clone https://github.com/clojure/clojurescript.git
  2. cd clojurescript; ./scripts/uberjar
  3. (^this creates cljs.jar in clojurescript/target; you will use it below)
  4. Create the following directory & file structure
 foo
 ├── build.clj
@mhuebert
mhuebert / get.clj
Created August 4, 2015 16:02
a simple http-get fn
(ns cells.io
(:require
[cljs.core.async :refer [put! chan]]
[goog.net.XhrIo :as xhr]))
(defn GET [path]
(let [c (chan)]
(xhr/send path #(do
(put! c (-> % .-target .getResponseText)) "GET"))
c))
@mhuebert
mhuebert / cljs-js-analysis-cache-usage.md
Last active September 9, 2020 11:59
Using existing namespaces from cljs.js

Let's say you want to use cljs.js to eval code using functions declared in your project.

Expressions that only use core functions are simple to evaluate:

(ns foo.try-eval
  (:require [cljs.js :as cljs]))

(def compiler-state (cljs/empty-state))
; in clojure an if-then statement looks like: (if test do-this-if-true do-this-if-false)
; eg (if has-water (prn "content") (water-plant))
(defn find-replace [zipper pred replacement]
(loop [loc zipper] ;begin a loop - when we call `recur` at the bottom,this is where we come back to.
(if (z/end? loc) ;if we are at the end, it means we've visited every node, so...
(root loc) ;we return the zipper
(if
(pred loc) ;`pred` is a function that we supply as a "test" for each location. eg. is this an 'x'?
(recur (z/next (z/replace loc (replacement loc)))) ;this is what we do if `pred` returned true
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
(ns layout.core
(:require
[sablono.core :include-macros true :refer-macros [html]]
[goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]))
(def leaves
(atom {1 {:leaf/label "Root"
:leaf/id 1
@mhuebert
mhuebert / cljs_core_cache.cljs
Last active February 24, 2016 16:57
CLJS core analysis cache for use with self-hosted compiler
(ns app.cljs-core-cache
(:require
[cognitect.transit :as transit]
[cljs.js :as cljs]))
(defn print-core []
(let [writer (transit/writer :json)
cache (cljs.js/dump-core)]
(.log js/console (transit/write writer cache))))
@mhuebert
mhuebert / defroutes.clj
Last active March 3, 2016 16:40
defroutes macro for ClojureScript/Secretary - quickly define many routes at once
(defmacro defroutes [& routes]
`(do
~@(map (fn [[[path] body]]
(let [has-args? (or (vector? (first body)) (map? (first body)))
body (if has-args? body (cons [] body))]
`(~'secretary.core/defroute ~path ~@body)))
(partition 2 (partition-by string? routes)))))
@mhuebert
mhuebert / flat_map.cljs
Created March 7, 2016 18:47
Flat-Map, for multi-path atomic updates in Firebase
(defn flat-map
([m] (clj->js (apply hash-map (flat-map "" m))))
([path m]
(mapcat (fn [[k v]] (if (map? v)
((partial flat-map (str path "/" (name k))) v)
[(str path "/" (name k)) v])) m)))
(= (flat-map {:users {"x0x0x0" {:name "Matt"
:email "[email protected]"}}})
{"users/x0x0x0/name" "Matt"