Skip to content

Instantly share code, notes, and snippets.

@cdanyl
cdanyl / composable.js
Last active January 24, 2017 10:54
FP Composable
const contrivedEx1 = str => {
const trimmed = str.trim();
const number = parseInt(trimmed);
const nextNumber = number + 1;
return String.fromCharCode(nextNumber);
}
const contrivedEx1 = str => {
[str.trim()]
@cdanyl
cdanyl / func.js
Created January 23, 2017 19:26 — forked from marc-rutkowski/func.js
ES6 func
const isUnique = (value, index, self) => self.indexOf(value) === index;
const unique = items => items.filter(isUnique);
const mapBy = (items, key) => items.map(item => item[key]);
const uniqueKeys = (items, key) => unique(mapBy(items, key));
const someKeys = (item, keys) => keys.reduce((result, key) => {
result[key] = item[key];
return result;
}, {});
@cdanyl
cdanyl / CreditCard-complex-functional.js
Created January 10, 2017 09:15 — forked from mrdavidlaing/CreditCard-complex-functional.js
A simple example of the "switch" code smell
function CreditCard(http) {
this.process = function(cardType, cardNumber) {
var cardProcessor = {
"VISA": this.processVisa,
"MASTERCARD": this.processMasterCard,
"AMEX": this.processAmex
};
cardProcessor[cardType](cardNumber);
};
this.processVisa = function(cardNumber) {
@cdanyl
cdanyl / options-objects.js
Created January 5, 2017 17:00 — forked from rwaldron/options-objects.js
ES6 Object.assign( target, source ) as a native options object merging pattern.
let defaults = { file: null };
function foo(options) {
// Merging defaults and options objects in ES6
options = [ defaults, options ].reduce(Object.assign, {});
}
foo({ file: "happy.md" });
class Maybe {
constructor(val) {
this.__val = val;
this.__error = this.isError();
this.__defaultVal = null;
}
isError() {
const val = this.__val;
return (val === null)
@cdanyl
cdanyl / arrowFunctions.md
Created December 22, 2016 16:17 — forked from jefflau/arrowFunctions.md
A journey through ES6 - Part 2

#Arrow Functions

Arrow functions (also known as fat arrow functions) are awesome. They do two main things for you. Create a shorter syntax, and lexically scope this.

##Shorter syntax

//ES5
[1, 2, 3].map(function(n){ return n + 1 } // [2, 3, 4]
//ES6
@cdanyl
cdanyl / LICENSE
Created December 15, 2016 14:41 — forked from ourmaninamsterdam/LICENSE
Arrayzing - The JavaScript array cheatsheet
The MIT License (MIT)
Copyright (c) 2015 Justin Perry
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
@cdanyl
cdanyl / 0. JavaScript Function Programming TOC.md
Created November 27, 2016 17:06 — forked from Integralist/0. JavaScript Function Programming TOC.md
JavaScript Function Programming (scratch pad) -> Most of the code here is modified from the excellent O'Reilly book "Functional JavaScript".

This code is modified from the excellent O'Reilly book "Functional JavaScript". You should buy it, I highly recommend it! Don't kid yourself into thinking this gist even remotely covers the great content from a 200+ page technical book on the subject; it doesn't. Buy the book and get the in-depth knowledge for yourself. It's worth it.

@cdanyl
cdanyl / index.html
Created October 30, 2016 20:38 — forked from anonymous/index.html
Angular Nested recursive directives // source http://jsbin.com/ferotu/1
<!doctype html>
<html ng-app='APP'>
<head>
<meta name="description" content="Angular Nested recursive directives" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
</head>
<body>
<div ng-controller="IndexCtrl">
<button ng-click="groupByTour()">tours</button>
<button ng-click="groupByOrders()">orders</button>
function groupByTour(orders) {
return orders.reduce((result, order) => {
const group = result.find(({ tour }) => tour.id === order.tour.id);
group ? group.order.push(order) : result.push({ tour : order.tour, order : [order] });
return result;
}, [])
}