(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
| /** | |
| * Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version | |
| * see: https://sites.google.com/site/abapexamples/javascript/luhn-validation | |
| * @author ShirtlessKirk. Copyright (c) 2012. | |
| * Licensed under WTFPL (http://www.wtfpl.net/txt/copying) | |
| */ | |
| function luhnChk(luhn) { | |
| var len = luhn.length, | |
| mul = 0, |
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
| function lens(get, set) { | |
| var f = function (a) { return get(a); }; | |
| f.set = set; | |
| f.mod = function (f, a) { return set(a, f(get(a))); }; | |
| return f; | |
| } | |
| var first = lens( | |
| function (a) { return a[0]; }, | |
| function (a, b) { return [b].concat(a.slice(1)); } |
| // based on http://msdn.microsoft.com/en-us/magazine/hh335067.aspx | |
| // usage example at bottom | |
| function pso (number_of_dimensions, function_to_optimize, number_of_particles, number_of_iterations, fitness_threshold, inertia_weight, cognitive_weight, social_weight) { | |
| var particles = []; | |
| var swarm_best_position = []; | |
| var swarm_best_fitness = null; | |
| for (var p = 0; p < number_of_particles; p++) { | |
| particles.push({ | |
| particle_position: [], |
When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:
const Article = require('../../../../app/models/article');Those suck for maintenance and they're ugly.
| /** | |
| * Create an array with `len` elements. | |
| * | |
| * @param [initFunc] Optional: a function that returns | |
| * the elements with which to fill the array. | |
| * If the function is omitted, all elements are `undefined`. | |
| * `initFunc` receives a single parameter: the index of an element. | |
| */ | |
| function initArray(len, initFunc) { | |
| if (typeof initFunc !== 'function') { |
| /*global angular: true, google: true, _ : true */ | |
| 'use strict'; | |
| angular.module('geocoder', ['ngStorage']).factory('Geocoder', function ($localStorage, $q, $timeout) { | |
| var locations = $localStorage.locations ? JSON.parse($localStorage.locations) : {}; | |
| var queue = []; | |
| // Amount of time (in milliseconds) to pause between each trip to the |
| (function(global) { | |
| global.Functor = function(conf) { | |
| Functor.types[conf.key] = { | |
| obj: conf.obj, | |
| fmap: conf.fmap | |
| }; | |
| }; | |
| Functor.types = {}; |
| (function(global) { | |
| var types = function(obj) { | |
| throw new TypeError("fmap called on unregistered type: " + obj); | |
| }; | |
| // inefficient as hell, but as long as there aren't too many types.... | |
| global.Functor = function(type, defs) { | |
| var oldTypes = types; | |
| types = function(obj) { | |
| if (type.prototype.isPrototypeOf(obj)) { |