Created
August 17, 2012 21:55
-
-
Save bryanwoods/3383040 to your computer and use it in GitHub Desktop.
More ClojureScript for Engibeering...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Proper namespaces | |
(ns app-utils) | |
; JavaScript interop | |
(defn -alert [message] | |
(js/alert message)) | |
(defn -log [message] | |
(.log js/console message)) | |
(ns app | |
; Namespaced module loading | |
(:require [app-utils :as utils])) | |
; Custom Data Types / "OOP" | |
(deftype Person [name] | |
Object | |
(welcome [_, level] | |
; Lexical scope, first-class functions, lambdas | |
(let [hello (fn [name] (str "Hello, " name "!"))] | |
(if (= level "log") | |
(utils/-log (hello name)) | |
(utils/-alert (hello name)))))) | |
; Export to external JavaScript | |
(defn ^:export build-person [name] | |
(Person. name)) | |
; Named parameters and default arguments | |
(defn ^:export greet | |
[person & {:keys [level] :or {level "alert"}}] | |
(.welcome person level)) | |
(defn ^:export greet-everyone [] | |
(greet (Person. "Marco")) | |
(greet (Person. "Rebecca") :level "alert") | |
(greet (Person. "George") :level "log")) | |
; Shared state | |
(def clicks (atom 0)) | |
; Atomic updates to shared state | |
(defn ^:export click [] | |
(swap! clicks inc)) | |
(defn ^:export clicks-count [] | |
@clicks) | |
; Functional programming, lazy evaluation, infinite sequences | |
(defn odds-to [n] | |
(into [] (filter odd? (take n (range))))) | |
(defn ^:export sum-of-odds-to [n] | |
(reduce + (odds-to n))) | |
; Resources | |
; http://clojurescriptone.com/ | |
; https://github.com/clojure/clojurescript | |
; http://himera.herokuapp.com/index.html | |
; http://himera.herokuapp.com/synonym.html | |
; https://developers.google.com/closure/compiler/ | |
; http://dosync.posterous.com/comparing-javascript-coffeescript-clojurescri | |
; https://github.com/swannodette/mori |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!!!5 | |
%html | |
%head | |
%script{type:"text/javascript", src:"https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"} | |
%script{type:"text/javascript", src:"app.js"} | |
:css | |
#clickCount { | |
font-size: 250px; | |
} | |
%body | |
#clickCount 0 | |
:javascript | |
$(function() { | |
var bryan = app.build_person("Bryan"); | |
var max = 1000; | |
app.greet(bryan); | |
$('#clickCount').click(function() { | |
app.click(); | |
$(this).html(app.clicks_count()); | |
}); | |
alert( | |
"The sum of all odds to " + max + " is: " + app.sum_of_odds_to(max) | |
); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def doc js/document) | |
(def console js/console) | |
(js/alert "Hi!") | |
(.-title doc) | |
(.log console (.-title doc)) | |
(defn set-html [id value] | |
(set! | |
(.-innerHTML (.getElementById doc id)) | |
value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment