SlideShare a Scribd company logo
Exploring ClojureScript
Introduction to building apps with
ClojureScript
Luke Donnet (Github @BadAlgorithm)
(In 2019, if you are not doing ClojureScript you are doing JavaScript wrong.)
1. Introduction
1.2 – About me
• Software Developer for Adzurra
• Current day job stack
§ React/Redux
§ Angular 5/Ionic 3
§ Clojure
§ AWS
• Have been doing Clojure for ~1 year
1.3 – “Why should I care?”
• Something new?
• Has made my life easier
• Functional Programming
1.3 – Clojure (.clj)
• Part of the LISP (List Processor) Family
• Processes code as data
• Fully functional programming
• Clojure runs on the JVM
• Rich Hickey is the Benevolent dictator for life (BDFL)
1.4 – Syntax is Easy
(+ 1 2) ; equals 3
That’s it!
Operator
Arguments
1.5 – ClojureScript (.cljs)
• ClojureScript compiles Clojure to JS
• Can pretty much run wherever JS can
§ Web
§ Nodejs
http://clojurescript.org
2. Language Features
2.1 – Basics: Hello world
(ns hello-world.core)
(def hello-message "Hello world")
(defn print-message
[message]
(print message))
(print-message hello-message)
; Prints "Hello world" - returns nil
2.2 – Basics: Returning from functions
(ns hello-world.core)
(def hello-message "Hello world")
(defn print-message
[message]
(print message)
"Return me")
(print-message hello-message)
; Prints "Hello world" – returns ”Return me"
2.3 – Types
(def a-number 1)
(def also-a-number 4.321)
(def a-string "I'm a String")
(def a-keyword :foo-bar)
(def a-namespaced-keyword ::foo-bar) ; turns into :explore-types.core/foo-bar
(def a-bool? true)
(ns explore-types.core)
2.4 – Data Structures
(def a-map {:key-foo "foo" :key-bar :bar :baz-key a-bool?})
(def a-list '(1 2 3 4 "Hello" :foo a-string))
(def a-vector [:hello "bye" {:foo-bar a-number}])
(def a-set #{1 2 3 4 5 6})
2.5 – Anonymous Functions
(def a-anon-fn (fn [] "I'm a function"))
(def shorthand-function #(print %))
2.6 – Example comparison
const result = "foo" === "bar" ? "yea" : "nah";
(def result (if (= "foo" "bar") "yea" "nah"))
Ternary operator
Destructuring
const {foo, bar} = {foo: "foo", bar: "bar", baz: "baz"}
(let [{:keys [foo bar]} {:foo "foo" :bar "bar" :baz "baz"}])
2.7 – Simple in Cljs… no so much in JS
Remove multiple keys from object
A sum of numbers in a vector
Generate a range of values
(def my-numbers [1 2 3 4 5 6 7 8 9 10])
(apply + my-numbers)
(def my-map {:one "one" :two "two" :three "three" :four "four"})
(dissoc my-map :one :two)
; Or if the list of items to remove is in a vector
(def remove-these [:one :two])
(apply (partial dissoc my-map) remove-these)
(range 11)
2.8 – Useful functions
The ones we know and love
(for [number my-numbers] (println number)) ; Print each value
(map inc my-numbers) ; Increment all numbers
(filter odd? my-numbers) ; Returns only odd numbers
(reduce #(conj (vec %)) [] my-numbers) ; Returns [[1] [2] [3] ...]
2.9 – Useful functions (cont)
The ones we don’t know we love yet…
(walk) ; Traverse a datastructure
(mapcat) ; concat all items in a vector
(interleave) ; Return new collection in alternating order
(interpose) ; Insert something in between every item
(take-while) ; Take each item while predicate is true
(drop-while) ; Remove each item while predicate is true
(take-last) ; Get last item
(next) ; Get everything expect first item
And many more…
2.10 – Many useful functions come built-in
• Works out-of-the-box
• Limits the number of deps
(think of left-pad)
• No more scouring stack-
overflow for trivial tasks
2.12 – Macros (might never be in ES*)
• Being a Lisp we can use Macros
• Extends the language
• Clojure which writes clojure…
• Use only when needed
(defmacro infix
"Addition for mere mortals"
[infixed]
(list
(second infixed)
(first infixed)
(last infixed)))
(infix (1 + 1))
2.13 – Built-in macro example
Threading macro, passes the output of one function into
the input of another
(->> my-numbers
(map inc)
(filter odd?)
(reduce #(conj (vec %)) []))
Chain of responsibility pattern made simple
(reduce #(conj (vec %)) [] (filter odd? (map inc my-numbers)))This
Becomes this
2.14 – Atoms
• Immutability?
• Nothing will change if nothing
changes
• Atoms gives us shared state
• Changes are synchronous
(let [my-val 0]
(println my-val) ; 0
(inc my-val)
(println my-val)) ; 0
(let [my-val (atom 0)]
(println @my-val) ; 0
(swap! my-val inc)
(println @my-val)) ; 1
Nothing changes
Value changes
2.15 – Unit Testing
• A lot simpler since functions are designed to be pure
• No need to integrate with a zillion libraries
(deftest validate-form
(testing "Should return true if the fields are complete and no fields are empty"
(let [mock-signup {:company "my company"
:email "email@account.com"
:password "thisisapassword"}]
(is (not (contains? (validate-signup mock-signup) :response)))
(is (every? (validate-signup mock-signup) [:company :email :password])))))
2.16 – Mocking with-redef
• Mocking is easy
• Eliminate the need for
complex integration
with spies, stubs,
mocks…
• Out-of-the-box
(defn call-me-maybe?
[]
"Hey, I just met you")
(defn test-me
[]
(call-me-maybe?))
(deftest called?
(testing "Check if function is called"
(let [count (atom 0)]
(with-redefs [call-me-maybe? #(swap! count inc)]
(test-me)
(is (= @count 1))))))
2.17 – Further reading
We’ve only just scratched the surface of Clojure/ClojureScript
• Core Async
• Cljs and JS interop
• Structural sharing (answers: how can immutability be efficient?)
• Advanced compilation
• Lazy sequences
• Transducers (very powerful tool) / Transients
• GoF patterns in Clojure http://mishadoff.com/blog/clojure-design-patterns/
3. Production Ready?
3.1 – Community/Support
• Small but active
• Many leading star open source projects
and devs
• Docs are great
• Heaps of support through slack/github
and others
3.2 – Cljs in the wild - CircleCI
• Great CI/CD system
• Use Clojure and ClojureScript full stack
3.3 – Cljs in the wild - JESI
• Journey Management app
• Built with reframe and
reagent
• Manage to operate with a
small team (lean and
mean)
3.4 – Cljs in the wild – Roudolph
• Christmas light finding app
• Completely serverless backend
built with Cljs
• Mobile app being rebuilt as a
PWA with cljs
4.Tooling
4.1 – Leiningen
• Build automation and
dependency management
• Easy to use cli, spin up projects
in seconds
https://leiningen.org/
4.2 – Figwheel and Shadow Cljs
• Build system for cljs projects
• Super fast quick reloading
• No need to re-enter form data across
reloads
• Shadow-cljs excels at ease of use and
support for multiple targets
https://github.com/thheller/shadow-cljs
https://github.com/bhauman/lein-figwheel
4.3 – Cursive
• IDE plugin (has one for IntelliJ)
for Clojure projects
• Handy tools such as automatic
bracket insertion
https://cursive-ide.com/
5. Building Web Apps
5.1 – Hiccup Templating
• Templating language using
clojure vectors
• Defined as functions
• Native clojure datastructure
means we can manipulate it as
we please
(defn main-panel
[:div.main-panel
[:h1 "Hello"]
[:div.main-body
[:p "This is hiccup”]]])
https://github.com/weavejester/hiccup
5.2 – React apps with Reagent
• Reagent is a React wrapper
• Uses hiccup templates
• Utilizes atoms as state
(defn main-panel
[]
[:div.main-panel
[:h1 "Hello"]
[:div.main-body
[:p "This is a reagent component”]]])
(defn mount-root []
(reagent/render [main-panel]
(.getElementById js/document "app")))
(defn ^:export init []
(mount-root))
https://reagent-project.github.io/
5.3 – Stateful Reagent component
(defn warning-view
[warning?]
[:div {:class (when @warning? "warning")}
[:span "This is a warning view"]
[:button {:on-click #(swap! warning? not)}]])
(defn main-panel
[]
(let [warning? (reagent/atom false)]
[:div.main-panel
[:h1 "Hello"]
[:div.main-body
[warning-view warning?]]]))
5.4 – Reframe
• App state management system
• Uses Reagent
• Shines for hardcore large-scale
apps
• Similar philosophy to Redux
• Very good docs
https://github.com/Day8/re-frame
5.5 – 1000 Mile view of Reframe
Event
Dispatched
Event
Hander
DBFx Handler
Subscription
View
Subscription
ViewRaw
Subscription
5.6 – Key points on Reframe
• Components don’t know the structure of the db, view
is fully decoupled
• Subs can subscribe to other subs to derive data
• Event handers update the db and trigger side effects
• Raw subscriptions for listening to data-sources outside
of re-frames. Forces all updates to go to db.
Demo Time
6.0 – Other cool things with cljs
• Build react native applications with re-frame
• Shadow cljs can compile node modules to
publish on npm or deploy on lambda functions
7.0 – Want to know more?
• Clojure for the brave and true
• PurelyFunctionalTV Youtube Channel
• Effective Programs by Rich Hickey
https://www.youtube.com/watch?v=2V1FtfBDsLU
• Willing to tell you everything I know over
a beer… https://www.linkedin.com/in/luke-donnet-24699aaa/
The End
Questions?

More Related Content

What's hot

Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
Nelson Glauber Leal
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
Felix Geisendörfer
 
Testing Backbone applications with Jasmine
Testing Backbone applications with JasmineTesting Backbone applications with Jasmine
Testing Backbone applications with Jasmine
Leon van der Grient
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancement
up2soul
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
async_io
 
A Taste of Clojure
A Taste of ClojureA Taste of Clojure
A Taste of Clojure
David Leung
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsLeonardo Borges
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
Domenic Denicola
 
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
Codemotion
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
Alex Payne
 
Professional Help for PowerShell Modules
Professional Help for PowerShell ModulesProfessional Help for PowerShell Modules
Professional Help for PowerShell Modules
June Blender
 
Lua: the world's most infuriating language
Lua: the world's most infuriating languageLua: the world's most infuriating language
Lua: the world's most infuriating language
jgrahamc
 
Async Frontiers
Async FrontiersAsync Frontiers
Async Frontiers
Domenic Denicola
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
Frank Lyaruu
 
bluespec talk
bluespec talkbluespec talk
bluespec talk
Suman Karumuri
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
Graham Dumpleton
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
Sebastian Springer
 
Dynamo: Not Just For Datastores
Dynamo: Not Just For DatastoresDynamo: Not Just For Datastores
Dynamo: Not Just For Datastores
Susan Potter
 

What's hot (19)

Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
 
iSoligorsk #3 2013
iSoligorsk #3 2013iSoligorsk #3 2013
iSoligorsk #3 2013
 
Testing Backbone applications with Jasmine
Testing Backbone applications with JasmineTesting Backbone applications with Jasmine
Testing Backbone applications with Jasmine
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancement
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
 
A Taste of Clojure
A Taste of ClojureA Taste of Clojure
A Taste of Clojure
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Professional Help for PowerShell Modules
Professional Help for PowerShell ModulesProfessional Help for PowerShell Modules
Professional Help for PowerShell Modules
 
Lua: the world's most infuriating language
Lua: the world's most infuriating languageLua: the world's most infuriating language
Lua: the world's most infuriating language
 
Async Frontiers
Async FrontiersAsync Frontiers
Async Frontiers
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
bluespec talk
bluespec talkbluespec talk
bluespec talk
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
 
Dynamo: Not Just For Datastores
Dynamo: Not Just For DatastoresDynamo: Not Just For Datastores
Dynamo: Not Just For Datastores
 

Similar to Exploring Clojurescript

Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
thnetos
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
Michiel Borkent
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
John Stevenson
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
Skills Matter
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lispelliando dias
 
Lobos Introduction
Lobos IntroductionLobos Introduction
Lobos Introduction
Nicolas Buduroi
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
bobmcwhirter
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
JAX London
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularityelliando dias
 
React native
React nativeReact native
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
Pavlo Baron
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
Mattias Karlsson
 
Introductory Clojure Presentation
Introductory Clojure PresentationIntroductory Clojure Presentation
Introductory Clojure Presentation
Jay Victoria
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019
Leonardo Borges
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
Jacek Laskowski
 

Similar to Exploring Clojurescript (20)

Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
 
Lobos Introduction
Lobos IntroductionLobos Introduction
Lobos Introduction
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularity
 
JS Essence
JS EssenceJS Essence
JS Essence
 
React native
React nativeReact native
React native
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
 
Introductory Clojure Presentation
Introductory Clojure PresentationIntroductory Clojure Presentation
Introductory Clojure Presentation
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 

Recently uploaded

Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 

Recently uploaded (20)

Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 

Exploring Clojurescript

  • 1. Exploring ClojureScript Introduction to building apps with ClojureScript Luke Donnet (Github @BadAlgorithm) (In 2019, if you are not doing ClojureScript you are doing JavaScript wrong.)
  • 3. 1.2 – About me • Software Developer for Adzurra • Current day job stack § React/Redux § Angular 5/Ionic 3 § Clojure § AWS • Have been doing Clojure for ~1 year
  • 4. 1.3 – “Why should I care?” • Something new? • Has made my life easier • Functional Programming
  • 5. 1.3 – Clojure (.clj) • Part of the LISP (List Processor) Family • Processes code as data • Fully functional programming • Clojure runs on the JVM • Rich Hickey is the Benevolent dictator for life (BDFL)
  • 6. 1.4 – Syntax is Easy (+ 1 2) ; equals 3 That’s it! Operator Arguments
  • 7. 1.5 – ClojureScript (.cljs) • ClojureScript compiles Clojure to JS • Can pretty much run wherever JS can § Web § Nodejs http://clojurescript.org
  • 9. 2.1 – Basics: Hello world (ns hello-world.core) (def hello-message "Hello world") (defn print-message [message] (print message)) (print-message hello-message) ; Prints "Hello world" - returns nil
  • 10. 2.2 – Basics: Returning from functions (ns hello-world.core) (def hello-message "Hello world") (defn print-message [message] (print message) "Return me") (print-message hello-message) ; Prints "Hello world" – returns ”Return me"
  • 11. 2.3 – Types (def a-number 1) (def also-a-number 4.321) (def a-string "I'm a String") (def a-keyword :foo-bar) (def a-namespaced-keyword ::foo-bar) ; turns into :explore-types.core/foo-bar (def a-bool? true) (ns explore-types.core)
  • 12. 2.4 – Data Structures (def a-map {:key-foo "foo" :key-bar :bar :baz-key a-bool?}) (def a-list '(1 2 3 4 "Hello" :foo a-string)) (def a-vector [:hello "bye" {:foo-bar a-number}]) (def a-set #{1 2 3 4 5 6})
  • 13. 2.5 – Anonymous Functions (def a-anon-fn (fn [] "I'm a function")) (def shorthand-function #(print %))
  • 14. 2.6 – Example comparison const result = "foo" === "bar" ? "yea" : "nah"; (def result (if (= "foo" "bar") "yea" "nah")) Ternary operator Destructuring const {foo, bar} = {foo: "foo", bar: "bar", baz: "baz"} (let [{:keys [foo bar]} {:foo "foo" :bar "bar" :baz "baz"}])
  • 15. 2.7 – Simple in Cljs… no so much in JS Remove multiple keys from object A sum of numbers in a vector Generate a range of values (def my-numbers [1 2 3 4 5 6 7 8 9 10]) (apply + my-numbers) (def my-map {:one "one" :two "two" :three "three" :four "four"}) (dissoc my-map :one :two) ; Or if the list of items to remove is in a vector (def remove-these [:one :two]) (apply (partial dissoc my-map) remove-these) (range 11)
  • 16. 2.8 – Useful functions The ones we know and love (for [number my-numbers] (println number)) ; Print each value (map inc my-numbers) ; Increment all numbers (filter odd? my-numbers) ; Returns only odd numbers (reduce #(conj (vec %)) [] my-numbers) ; Returns [[1] [2] [3] ...]
  • 17. 2.9 – Useful functions (cont) The ones we don’t know we love yet… (walk) ; Traverse a datastructure (mapcat) ; concat all items in a vector (interleave) ; Return new collection in alternating order (interpose) ; Insert something in between every item (take-while) ; Take each item while predicate is true (drop-while) ; Remove each item while predicate is true (take-last) ; Get last item (next) ; Get everything expect first item And many more…
  • 18. 2.10 – Many useful functions come built-in • Works out-of-the-box • Limits the number of deps (think of left-pad) • No more scouring stack- overflow for trivial tasks
  • 19. 2.12 – Macros (might never be in ES*) • Being a Lisp we can use Macros • Extends the language • Clojure which writes clojure… • Use only when needed (defmacro infix "Addition for mere mortals" [infixed] (list (second infixed) (first infixed) (last infixed))) (infix (1 + 1))
  • 20. 2.13 – Built-in macro example Threading macro, passes the output of one function into the input of another (->> my-numbers (map inc) (filter odd?) (reduce #(conj (vec %)) [])) Chain of responsibility pattern made simple (reduce #(conj (vec %)) [] (filter odd? (map inc my-numbers)))This Becomes this
  • 21. 2.14 – Atoms • Immutability? • Nothing will change if nothing changes • Atoms gives us shared state • Changes are synchronous (let [my-val 0] (println my-val) ; 0 (inc my-val) (println my-val)) ; 0 (let [my-val (atom 0)] (println @my-val) ; 0 (swap! my-val inc) (println @my-val)) ; 1 Nothing changes Value changes
  • 22. 2.15 – Unit Testing • A lot simpler since functions are designed to be pure • No need to integrate with a zillion libraries (deftest validate-form (testing "Should return true if the fields are complete and no fields are empty" (let [mock-signup {:company "my company" :email "email@account.com" :password "thisisapassword"}] (is (not (contains? (validate-signup mock-signup) :response))) (is (every? (validate-signup mock-signup) [:company :email :password])))))
  • 23. 2.16 – Mocking with-redef • Mocking is easy • Eliminate the need for complex integration with spies, stubs, mocks… • Out-of-the-box (defn call-me-maybe? [] "Hey, I just met you") (defn test-me [] (call-me-maybe?)) (deftest called? (testing "Check if function is called" (let [count (atom 0)] (with-redefs [call-me-maybe? #(swap! count inc)] (test-me) (is (= @count 1))))))
  • 24. 2.17 – Further reading We’ve only just scratched the surface of Clojure/ClojureScript • Core Async • Cljs and JS interop • Structural sharing (answers: how can immutability be efficient?) • Advanced compilation • Lazy sequences • Transducers (very powerful tool) / Transients • GoF patterns in Clojure http://mishadoff.com/blog/clojure-design-patterns/
  • 26. 3.1 – Community/Support • Small but active • Many leading star open source projects and devs • Docs are great • Heaps of support through slack/github and others
  • 27. 3.2 – Cljs in the wild - CircleCI • Great CI/CD system • Use Clojure and ClojureScript full stack
  • 28. 3.3 – Cljs in the wild - JESI • Journey Management app • Built with reframe and reagent • Manage to operate with a small team (lean and mean)
  • 29. 3.4 – Cljs in the wild – Roudolph • Christmas light finding app • Completely serverless backend built with Cljs • Mobile app being rebuilt as a PWA with cljs
  • 31. 4.1 – Leiningen • Build automation and dependency management • Easy to use cli, spin up projects in seconds https://leiningen.org/
  • 32. 4.2 – Figwheel and Shadow Cljs • Build system for cljs projects • Super fast quick reloading • No need to re-enter form data across reloads • Shadow-cljs excels at ease of use and support for multiple targets https://github.com/thheller/shadow-cljs https://github.com/bhauman/lein-figwheel
  • 33. 4.3 – Cursive • IDE plugin (has one for IntelliJ) for Clojure projects • Handy tools such as automatic bracket insertion https://cursive-ide.com/
  • 35. 5.1 – Hiccup Templating • Templating language using clojure vectors • Defined as functions • Native clojure datastructure means we can manipulate it as we please (defn main-panel [:div.main-panel [:h1 "Hello"] [:div.main-body [:p "This is hiccup”]]]) https://github.com/weavejester/hiccup
  • 36. 5.2 – React apps with Reagent • Reagent is a React wrapper • Uses hiccup templates • Utilizes atoms as state (defn main-panel [] [:div.main-panel [:h1 "Hello"] [:div.main-body [:p "This is a reagent component”]]]) (defn mount-root [] (reagent/render [main-panel] (.getElementById js/document "app"))) (defn ^:export init [] (mount-root)) https://reagent-project.github.io/
  • 37. 5.3 – Stateful Reagent component (defn warning-view [warning?] [:div {:class (when @warning? "warning")} [:span "This is a warning view"] [:button {:on-click #(swap! warning? not)}]]) (defn main-panel [] (let [warning? (reagent/atom false)] [:div.main-panel [:h1 "Hello"] [:div.main-body [warning-view warning?]]]))
  • 38. 5.4 – Reframe • App state management system • Uses Reagent • Shines for hardcore large-scale apps • Similar philosophy to Redux • Very good docs https://github.com/Day8/re-frame
  • 39. 5.5 – 1000 Mile view of Reframe Event Dispatched Event Hander DBFx Handler Subscription View Subscription ViewRaw Subscription
  • 40. 5.6 – Key points on Reframe • Components don’t know the structure of the db, view is fully decoupled • Subs can subscribe to other subs to derive data • Event handers update the db and trigger side effects • Raw subscriptions for listening to data-sources outside of re-frames. Forces all updates to go to db.
  • 42. 6.0 – Other cool things with cljs • Build react native applications with re-frame • Shadow cljs can compile node modules to publish on npm or deploy on lambda functions
  • 43. 7.0 – Want to know more? • Clojure for the brave and true • PurelyFunctionalTV Youtube Channel • Effective Programs by Rich Hickey https://www.youtube.com/watch?v=2V1FtfBDsLU • Willing to tell you everything I know over a beer… https://www.linkedin.com/in/luke-donnet-24699aaa/