Skip to content

Instantly share code, notes, and snippets.

@dillonforrest
dillonforrest / js_chaining.md
Last active May 31, 2020 16:18
Discussion of various types of chaining in JavaScript.

Chaining

Chaining in javascript can come in many forms. Here, I'll discuss method chaining, lazy chaining, and pipelining. My aim is to help me, and maybe you, understand these chaining options. This gist is inspired by Michael Fogus's O'Reilly book 'Functional JavaScript.' Like Fogus's book, I'll assume use of underscore.js as a supplement. If you're interested in functional programming in javascript, I highly recommend Fogus's book.

A quick example

What do I mean by 'chaining'? Let's look over a quick example just to be on the same page. This example uses underscore.js's _.chain method.

function findFruitCosts(groceries) {
@dillonforrest
dillonforrest / fp_js_node_callbacks.md
Created August 19, 2013 00:38
Applying functional programming techniques to some common Node.js callback patterns

Functional programming exercises

This is a thought exercise. Nothing to be taken seriously. I'm not a serious node programmer (yet), and I'm still relatively new to functional programming. But, I'd like to look at some common node patterns using callbacks and apply funtional programming techniques to them. Some of these exercises might seem impractical or contrived, but the point is learning. Seriously, I'm very new to this. Please be forgiving if I make a stupid mistake, but also let me know. :) Let's have some fun!

The examples are derived from Andrew Kelley's article here.

The simplest example

This is directly from Andrew's post:

Quoted from David Walsh on tech.pro

Source

There's not a developer out there that hasn't been bitten by JavaScript's pass-objects-by-reference nature. Oftentimes developers will attempt to empty an array but mistakenly create a new one instead:

var myArray = yourArray = [1, 2, 3];

// :(
@dillonforrest
dillonforrest / effective_tdd.md
Created September 12, 2013 03:37
Effective test-driven development for the rest of us, implemented in javascript

Traditional TDD is not always actionable

Traditional TDD dicates that you write code like this:

  1. Write the smallest test possible that fails
  2. Write code to make the test pass
  3. Refactor while the test still passes
  4. Repeat

In practice, the TDD process doesn't always work out. You might waste too much time writing tests that don't actually matter. You might spend too much time mocking data. You might not actually know what test to write.

@dillonforrest
dillonforrest / js_tilde.md
Last active December 24, 2015 16:29
Using the super-rare tilde operator in javascript

I just came across this interesting bit of javascript, which I've paraphrased:

var _ = require('underscore');
var coll = require('./some/external/array');

function contains(str, key) {
  return ~str.indexOf(key);
}
@dillonforrest
dillonforrest / om-example.md
Last active January 3, 2016 09:49
figuring out cljs's Om by David Nolen

This is what I used to have:

(defn static-data []
  (for [row (range 10)]
    {:a "some" :b "fake" :c "data"}))

(def app-state (atom {:items (static-data)}))

;;; components
@dillonforrest
dillonforrest / broken.cljs
Created January 28, 2014 21:03
why doesn't this compile?
(ns om-tut.coreeee
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(def app-state (atom {:text "Hello world!"}))
(om/root
app-state
//////////////////////////////////////////////////
// data generation
//////////////////////////////////////////////////
var _ = require('underscore');
function genStaticData() {
var alphabet = 'abcdefghijklmnopqrstuvqxyz';
return _.range(69).map(function makePublisher() {
[
{
"site": "aaaaa.com",
"status": "live",
"size": "large",
"owner": "some dude",
"guid": 1
},
{
"site": "bbbbb.com",
/*
* Trying to ensure users are logged in. I don't want unverified users to look
* at any of the pages. So, on either $locationChangeStart or $routeChangeStart,
* I'd like to check if I'm logged in. If I'm not logged in, abort the route
* change and go to login route. If I AM logged in, continue changing the route.
*/
angular.module('myModule', [
'ui.state', // using AngularUI's $stateProvider
'ui.router', // using AngularUI's $urlRouterProvider