Skip to content

Instantly share code, notes, and snippets.

View ericelliott's full-sized avatar
💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
View GitHub Profile
@ericelliott
ericelliott / wtf_node
Created February 12, 2014 01:01
wtf node?
$ node
> var _ = require('underscore-node');
undefined
> _
undefined
> var stampit = require('stampit');
undefined
> stampit
{ [Function: stampit]
compose: [Function: compose],
@ericelliott
ericelliott / log-samples
Last active August 29, 2015 14:08
Broken redirect.
Nginx:
```
173.245.55.164 - - [05/Nov/2014:18:46:21 -0500] "GET /js/index.js HTTP/1.1" 200 23893 "http://moonshotcon.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"
```
Apache2
@ericelliott
ericelliott / idiomatic-coffeescript.litcoffee
Last active August 29, 2015 14:10
Idiomatic CoffeeScript

Idiomatic CoffeeScript

I'm new to CoffeeScript, so I'm just using this to document idioms that I am using. Suggestions / improvements are very welcome.

IIFE (Immediately Invoked Function Expressions)

increment = do ->
  x = 0

In JS FP do u ever extend objs with other objs? tweet

Yes, frequently to copy data from one collection to another during object creation, but once created and returned, objs are treated as immutable collections. Contrast against shared state with methods that mutate properties as in OOP. As a consequence of the immutable nature of collections in FP, pure functions boast stronger encapsulation features than private methods in OOP, because they guarantee against side-effects.

Unlike in OOP, most functions in FP are generic and independent of object properties and collection element contents, so there is typically no need to copy methods from object to object.

One major exception is that collections (or observable streams in FRP) may be enhanced with a number of functional utility methods which all return new values rather than mutating the properties of the parent object.

Do u have an example of FP vs OOP? tweet

Any example would be contrived, and will not adequately illustrate the fundamental philosophical differences between OOP and functional programming. For much more on pros and cons of OOP vs Functional programming, see "The Two Pillars of JavaScript" Part 1 (OOP) and Part 2 (Functional Programming).

OOP collection.append():

var collection = function collection(arr) {
  return {
    elements: [].slice.call(arr || []),
@ericelliott
ericelliott / .jshintrc
Last active August 29, 2015 14:15
Idiomatic JavaScript .jshintrc for Isomorphic JavaScript with Node style modules and Browserify
{
"maxerr": 10,
"node": true,
"browser": true,
"typed": true,
"worker": true,
"browserify": true,
"predef": ["define", "require"],
"camelcase": false,
"curly": true,
// A ray of sunshine in the belly of
// the beast...
var object = {};
_.extend(object, Backbone.Events);
object.on("alert", function(msg) {
alert("Triggered " + msg);
});
@ericelliott
ericelliott / inherit-from-array.js
Created April 21, 2015 23:51
Inherit from Array
var a = [];
var b = Object.create(a);
b.push('foo');
b[0]; //=> 'foo'
b instanceof Array // => true
b.length // => 1
@ericelliott
ericelliott / ancient-module-pattern.js
Created April 29, 2015 18:37
Ancient module pattern
var myModule = (function () {
return {
hello: function hello() {
return 'Hello, world!';
}
};
}());
@ericelliott
ericelliott / broken-concise-method.md
Last active August 29, 2015 14:20
Broken concise method name

Broken concise method names

There's a relatively common problem confusing users of ES6. This code demonstrates the issue.

const repeater = {
  name: 'Repeater',
  types: [
    { f: 'function' },
 { n: 'number' }