Skip to content

Instantly share code, notes, and snippets.

@brentonashworth
Created November 7, 2011 00:50
Show Gist options
  • Save brentonashworth/1343920 to your computer and use it in GitHub Desktop.
Save brentonashworth/1343920 to your computer and use it in GitHub Desktop.
ClojureScript Demo
;; ClojureScript Demo
;; ==================
(use 'cljs.closure)
;; ClojureScript compiler examples
(println (-compile '(defn add [a b] (+ a b)) {}))
(println (-compile '(defn add [& args] (reduce + args)) {}))
;; Optimization levels
(def js "var x = 3 + 2; alert(x);")
(optimize {:optimizations :whitespace} js)
(optimize {:optimizations :simple} js)
(optimize {:optimizations :advanced} js)
(def js "function addThree(a) {
return a + 3;
};
var x = addThree(2);
alert(x);")
(optimize {:optimizations :simple} js)
(optimize {:optimizations :advanced} js)
;; Tools
;; =====
;; Command line
;; ../../bin/cljsc src > twitterbuzz.js
;; Development mode outputs a dependencies file and all of the
;; individual files into the "out" directory.
;; ../../bin/cljsc src '{:optimizations :advanced}' > twitterbuzz.js
;; Any of the GClosure modes puts everything into a single file.
;; REPL Compilation
;; Compile twitterbuzz in development mode.
;;
;; If you would like to interact with twitterbuzz from the REPL then,
;; in the twitterbuzz.core namespace, require:
;;
;; [clojure.browser.repl :as repl]
;;
;; and add the line:
;;
;; (repl/connect "http://localhost:9000/repl")
;;
;; just after the namespace form. With this change in place, compile
;; twitterbuzz as shown below.
(def opts {:output-to "samples/twitterbuzz/twitterbuzz.js"
:output-dir "samples/twitterbuzz/out"})
(time (build "samples/twitterbuzz/src" opts))
;; Start a ClojureScript browser-connected REPL
(do (require '[cljs.repl :as repl])
(require '[cljs.repl.browser :as browser])
(def env (browser/repl-env))
(repl/repl env))
;; In a browser, open samples/twitterbuzz/index.html.
;; ClojureScript REPL Demo
;; =======================
(ns demo
(:require [twitterbuzz.core :as buzz]
[clojure.browser.dom :as dom]
[goog.dom :as gdom]))
(+ 1 1)
(js/alert "Hello")
(buzz/parse-mentions {:text "What's up @sue: and @Larry"})
(defn random-image []
(let [r (* (js/Math.random) 100)
size (+ 48 (js/Math.round r))]
(str (if (< r 50)
"http://placekitten.com/"
"http://placedog.com/")
size "/" size)))
(defn add-image [m]
(assoc m :profile_image_url (random-image)))
(def test-tweets
(map add-image
[{:text "Clojure rocks."
:from_user "Jim"}
{:text "JavaScript reaches."
:from_user "dan"}
{:text "ClojureScript lets you rock Clojure everywhere
JavaScript reaches."
:from_user "phil"}
{:text "@jim All I see are parens."
:from_user "sue"}
{:text "@dan Hopefully it reaches end of life before I do."
:from_user "bob"}
{:text "@phil What?"
:from_user "sam"}
{:text "@sam I'm with you, why didn't they target PHP?"
:from_user "james"}
{:text "@phil There's no eval, I'm never using
#ClojureScript."
:from_user "larry"}]))
(buzz/send-event :new-tweets test-tweets)
(buzz/update-graph {} test-tweets)
(buzz/send-event :graph-update *1)
(defn new-search [tag]
(gdom/setProperties (dom/get-element "twitter-search-tag")
(.strobj {"value" tag}))
(buzz/do-track-button-clicked))
(new-search "ClojureScript")
(new-search "Clojure")
(def test-graph
(apply hash-map (flatten (take 20 (:graph @buzz/state)))))
(buzz/send-event :graph-update test-graph)
(time (reduce + (range 100000)))
(doseq [x (range 10)]
(println x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment