Top Banner
Clojure compiled to JS A Introduction @friemens
30

ClojureScript Introduction

Apr 12, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ClojureScript Introduction

Clojure compiled to JS

A Introduction

@friemens

Page 2: ClojureScript Introduction

Agenda

Some general remarks (a.k.a „blah-blah“)

Getting started with the tooling

Exploring the language and core library

React / Om / core.async

Wrap up

Page 3: ClojureScript Introduction

Powerful core library

Why?

Functional programming

One language in backend and frontend

Page 4: ClojureScript Introduction

Unfamiliar tooling

Why not?

Too much for quick-and-dirty UI enhancements

An alien language

Page 5: ClojureScript Introduction

Sweet spot: full stack Clojure

ClojureScript

Clojure

Datomic

„Rich-Client“in a browser

Backend

Database

Page 6: ClojureScript Introduction

Fundamentals(a.k.a things-we-need-to-understand-first)

It's a Lisp (OMG!)

The core idea of Functional Programming™

A hosted language

Page 7: ClojureScript Introduction

Lispy syntax

Ordered

Associative

Function

Invocation

var x = [1,2,3];(def x [1 2 3])

(def m {:a “Foo“}) var m = {a: “Foo“};

function bar (a) { return a + 42;}

(defn bar [a] (+ a 42))

bar(0);(bar 0)

Page 8: ClojureScript Introduction

The REPL*

Read-Eval-Print-Loop*

Page 9: ClojureScript Introduction

Pure functions

Side effects

Context

The core idea of Functional Programming

Page 10: ClojureScript Introduction

Thou shalt not mutate in place

Page 11: ClojureScript Introduction

Mutability causes cognitive load

var x = [1,2,3];foo(x);

After foo,what's in x?

Page 12: ClojureScript Introduction

ClojureScript is built on immutability

(def x [1 2 3])(foo x)(assert (= x [1 2 3]))

Page 13: ClojureScript Introduction

… on efficient immutability

Structuralsharing

x x'

Page 14: ClojureScript Introduction

ClojureScript is a hosted language

Your host: the browser

Clojure Script

Page 15: ClojureScript Introduction

Getting started with the tooling

Page 16: ClojureScript Introduction

Ideal tooling

Browser

Console

Editor

REPL

Instant feedback

Page 17: ClojureScript Introduction

Highly recommended tutorial:

https://github.com/clojure/clojurescript/wiki/Quick-Start

cljs.jar

Page 18: ClojureScript Introduction

Or follow the steps onhttps://github.com/adzerk-oss/boot-cljs-example

Getting started with Boot

Either use the example fromhttps://github.com/friemen/cugb/tree/master/cljsintro

Page 19: ClojureScript Introduction

Traditional architecture of UIs

View

Model Controller

Page 20: ClojureScript Introduction

Dataflow approach to UI

Om / React

core.async

App. State View

Page 21: ClojureScript Introduction

Databinding

Appstate

DOM

event

updates

mutations

Controller

e.g. Angular

transform

renderdiff + merge

DOM

Appstate

swap

React+Om

Virtual DOM

event

DOM operations are soooo sloooow.

Bi-directional

Uni-directional

Page 22: ClojureScript Introduction

Burn Hollywood Burn!*

Public Enemy (1990)*

Page 23: ClojureScript Introduction

Callbacks are ok, but, uhm ...

function reloadAddressbook (state, event) {

ask("Are you sure?", function (answer) {

if (answer) {

ajax.get("/addresses", function (response) {

if (response.statusCode == 200) {

state.addresses.items = response.body; } }); } });}

Ask for confirmation

ok?

Issue GET request

success?

Replace items

But how do you implement concurrent requests?

Page 24: ClojureScript Introduction

CSP* with core.async = channels + go blocks

Communicating Sequential Processes, Tony Hoare (1978)*

(def c1 (chan))

(go-loop [xs []] (let [x (<! c1)] (println "Got" x ", xs so far:" xs) (recur (conj xs x))))

(put! c1 "foo");; outputs: Got bar , xs so far: [foo]

a blocking read

make a new channelcreates a lightweight

process

async write

Page 25: ClojureScript Introduction

core.async: no need for callbacks

Ask for confirmation

ok?

Issue GET request

success?

Replace items

(defn <addressbook-reload [state event]

(go (if (= :ok (<! (<ask "Are you sure?")))

(let [{s :status addresses :body} (<! (http/get "/addresses"))]

(if (= 200 s)

(assoc-in state [:addresses :items] addresses) state)))

Page 26: ClojureScript Introduction

Getting started withReact / Om / core.async

Page 27: ClojureScript Introduction

Wrap up

Page 28: ClojureScript Introduction

ClojureScript is ...

... overkill for little enhancements

… a mature language to tackle UI complexity

… demanding to learn, but this will pay-of

Page 29: ClojureScript Introduction

Links to resources on GitHub

https://github.com/friemen/cugb/

Page 30: ClojureScript Introduction

Thank you for listening!

Questions?

@friemens [email protected]