Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am lwhorton on github.
  • I am lwhorton (https://keybase.io/lwhorton) on keybase.
  • I have a public key ASB18ki5khnROa1xhWw_MiTwxIXHahAD0UgHVOVZfyQq4Ao

To claim this, I am signing this object:

@lwhorton
lwhorton / why-clojure#5.js
Created March 18, 2018 03:32
why-clojure#5.clj
// optimizing this code:
function unusedFunction(note) {
alert(note['text']);
}
function displayNoteTitle(note) {
alert(note['title']);
}
var flowerNote = {};
flowerNote['title'] = "Flowers";
displayNoteTitle(flowerNote);
@lwhorton
lwhorton / why-clojure#4.clj
Last active March 18, 2018 03:15
why-clojure#4.clj
(let [arr []]
(type arr) ;; clojure.lang.PersistentVector
(instance? clojure.lang.PersistentVector arr) ;; true
)
(= 1 "1") ;; false
(= 1 [1]) ;; false
(= 0 false) ;; false
(= "" false) ;; false
(= nil false) ;; false
@lwhorton
lwhorton / why-clojure#3.clj
Created March 18, 2018 02:08
why-clojure#3.clj
(let [old {:a 1 :b 2}
new-o (assoc old :c 3)]
(prn old) ;; {:a 1 :b 2}
)
(let [arr [1 2 3]
new-a (conj arr 4)]
(prn arr) ;; [1 2 3]
)
@lwhorton
lwhorton / why-clojure#2.clj
Created March 18, 2018 01:54
why-clojure#2.clj
;; refs are never mutated
;; equality is value-based, not identity based
;; but you can still compare identity
(let [a [1 2 3]
b (conj a 4)]
(prn a) ;; [1 2 3]
(prn b) ;; [1 2 3 4]
(= a b) ;; false
(= a (drop-last b)) ;; true
(identical? a (drop-last b)) ;; false
@lwhorton
lwhorton / why-clojure#1.clj
Last active March 18, 2018 00:48
Why Clojure(script)?
;; when not every value in transient-states is saved, return a string"
(defn confirm-leave? [db]
(when-not (->> (:transient-states db)
(vals)
(every? :saved?))
"There is unsaved data, do you want to continue?"))
@lwhorton
lwhorton / implicit-view-model-in–view.js
Created October 4, 2015 16:48
React, please let me drive
shouldComponentUpdate: function () {
if (this.state.someState) {
// do stuff
} else {
// do different stuff
}
}
, componentDidMount: function() {
if (this.props.someProp && this.state.someOtherState) {
if (!this.props.anotherProp) {
@lwhorton
lwhorton / line-chart.js
Created February 11, 2014 16:34
Basic flag-configurable line chart.
// These are the most basic properties required by a line chart.
function Chart() {}
Chart.prototype.data = null;
Chart.prototype.svgWidth = 600;
Chart.prototype.svgHeight = 500;
Chart.prototype.margin = {top: 20.0, right: 20.0, bottom: 20.0, left: 20.0};
Chart.prototype.width = 500;
Chart.prototype.height = 400;
Chart.prototype.xValue = function (d) { return d[0]; };
Chart.prototype.yValue = function (d) { return d[1]; };