Trojan horsing Clojure with Javascript
Trojan Horse with me
http://developers.theguardian.com
Why
Javascript?
- Dynamic
- Uses maps as their main data structure
- Derived from lisp
- Use first order functions
- Equally unpopular with "proper" language fans
So we're using Clojurescript, yeah?
No
- Clojurescript is very opinionated
- It doesn't fit Node-based build pipelines
- It uses a library that virtually no-one knows
- You have to take it whole, no cherry-picking
Wisp
A Clojure-like Lisp syntax
Wisp
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
Javascript
(defn greet [name] (.concat "Hello " name))
(greet "Robert")
(defn currentSections
[allSections, activeSections]
(.filter list (fn [section]
(.some activeSections
(fn [activeSection]
(identical?
section.id activeSection.id))))))
var greet = exports.greet
= function greet(name) {
return 'Hello '.concat(name);
};
greet('Robert');
var currentSections
= exports.currentSections
= function currentSections(
allSections, activeSections) {
return list.filter(
function (section) {
return activeSections.some(
function (activeSection) {
return section.id
=== activeSection.id;
});
});
};
The parens haven't gone away
Packaging (NPM)
Immutability
Mori
Compiled Clojurescript
Prefix notation
Includes quite a few Clojure standard library functions
Clojure data structures
ImmutableJS
Javascript
Prototype extension
Arrays-compatible interface
Feature comparable with Lo-Dash and Underscore
Influenced by Clojure data structures
Om
Love your library
not your language
Omniscient
- Javascript
- React
- ImmutableJS
- Immstruct
How does Om work?
- Uses ReactJS to render an in-memory data structure
- Uses immutable data structures to prevent unnecessary renders
- Allows React components to be attached to sections of a data structure
- Uses core.async to handle event changes
Immstruct
- Wraps ImmutableJS structures
- Provides an event model
- Implements cursors
Cursors
var data = Immutable.fromJS({'person' :
{'name' :
{'first' : 'Robert', 'family' : 'Rees'}
}
, 'age' : 452});
var nameCursor = Cursor.from(data, ['person', 'name']);
nameCursor.update('first', function(name) {return name.toUpperCase();} );
data.getIn(['person', 'name', 'first']); // ROBERT
Event driven rendering
var immstruct = require('immstruct'),
React = require('react');
var appData = immstruct({greeting: "Hello", guest: {name: ""}});
function render() {
React.render(Component(appData.cursor()), documentElement);
}
render();
appData.on('next-animation-frame', render);
Wot no core.async?
Transducers?
Yes you can!
Why not Clojure?
- Confident community
- ES 6, transpilation and ES7
- libraries > framework
Thank you
Blog: rrees.me
Twitter: rrees
http://slides.com/rrees/trojan-horsing-clojure-with-js
Trojan-horsing Clojure with Javascript
By Robert Rees
Trojan-horsing Clojure with Javascript
How to smuggle Clojure into your work with Javascript
- 2,594