Skip to content

Instantly share code, notes, and snippets.

@battmanz
battmanz / select-with-partial-application.cs
Last active November 26, 2017 23:45
Demonstrates using the Enumerable.Select method with a partially applied function.
using System;
using System.Linq;
using System.Web.Script.Serialization;
class Main {
public static void Main (string[] args) {
string username = "George";
string[] orderedHeroes = { "Luke Skywalker", "Han Solo", "Leia Organa", "Rey", "Finn", "Poe Dameron" };
// Store CreateRanking in a variable as a first-class function.
@battmanz
battmanz / map-with-curry.js
Created November 26, 2017 21:29
Demonstrates using the Array.prototype.map method with a curried function.
const curriedCreateRanking = _.curry(createRanking);
const whatIsThis = curriedCreateRanking(currentUser);
console.log(orderedHeroes.map(whatIsThis));
// Output: [
// {'username':'George','rebel':'Luke Skywalker','index':0},
// {'username':'George','rebel':'Han Solo','index':1},
// {'username':'George','rebel':'Leia Organa','index':2},
// {'username':'George','rebel':'Rey','index':3},
// {'username':'George','rebel':'Finn','index':4},
@battmanz
battmanz / map-with-partial-application.js
Created November 26, 2017 21:21
Demonstrates using the Array.prototype.map method with a partially applied function.
const partialCreateRanking = _.partial(createRanking, currentUser);
console.log(orderedHeroes.map(partialCreateRanking));
// Output: [
// {'username':'George','rebel':'Luke Skywalker','index':0},
// {'username':'George','rebel':'Han Solo','index':1},
// {'username':'George','rebel':'Leia Organa','index':2},
// {'username':'George','rebel':'Rey','index':3},
// {'username':'George','rebel':'Finn','index':4},
// {'username':'George','rebel':'Poe Dameron','index':5}
@battmanz
battmanz / curry-vs-partial-application-setup.js
Last active November 26, 2017 21:29
The necessary setup required to demonstrate currying vs partial application.
const currentUser = 'George';
const starWarsHeroes = [
'Luke Skywalker',
'Han Solo',
'Leia Organa',
'Rey',
'Finn',
'Poe Dameron'
];
@battmanz
battmanz / immutable-js-demo.js
Created June 19, 2017 06:49
Immutable.js persistent data structures.
// Use in place of `[]`.
const list1 = Immutable.List(['A', 'B', 'C']);
const list2 = list1.push('D', 'E');
console.log([...list1]); // ['A', 'B', 'C']
console.log([...list2]); // ['A', 'B', 'C', 'D', 'E']
// Use in place of `new Map()`
const map1 = Immutable.Map([
@battmanz
battmanz / set-mutator-method-replacement.js
Created June 19, 2017 05:25
Functions that can be used in place of Set mutator methods.
const set = new Set(['A', 'B', 'C']);
// Instead of: set.add('D');
const set2 = new Set([...set, 'D']);
// Instead of: set.delete('B');
const set3 = new Set([...set].filter(key => key !== 'B'));
// Instead of: set.clear();
const set4 = new Set();
@battmanz
battmanz / map-mutator-method-replacement.js
Last active June 19, 2017 05:16
Functions that can be used in place of Map mutator methods.
const map = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three']
]);
// Instead of: map.set(4, 'four');
const map2 = new Map([...map, [4, 'four']]);
// Instead of: map.delete(1);
@battmanz
battmanz / array-mutator-method-replacement.js
Last active March 2, 2022 12:54
Functions that can be used in place of array mutator methods.
'use strict';
const a = Object.freeze([4, 5, 6]);
// Instead of: a.push(7, 8, 9);
const b = a.concat(7, 8, 9);
// Instead of: a.pop();
const c = a.slice(0, -1);
@battmanz
battmanz / captured-mutable-object.js
Last active June 18, 2017 23:37
A captured, mutable object.
const constants = {
heightRequirement: 46,
// ... other constants go here
};
function canRide(height) {
return height >= constants.heightRequirement;
}
@battmanz
battmanz / immutable-state.js
Last active June 18, 2017 23:37
Capturing an immutable variable leaves the function pure.
const heightRequirement = 46;
function canRide(height) {
return height >= heightRequirement;
}