Skip to content

Instantly share code, notes, and snippets.

View pablodenadai's full-sized avatar
🤖

Pablo De Nadai pablodenadai

🤖
View GitHub Profile
@pablodenadai
pablodenadai / partial-evaluation.js
Created February 9, 2017 00:55 — forked from lovasoa/partial-evaluation.js
Partial-evaluation for javascript.
/** pe
* @argument f: the multiple-argument function to turn into a partially-evaluatable
* @returns : A single-argument function that applies its argument as the first argument of f, and returns the partially-evaluated function
* @exemple: pe((a,b)=>a+b)(9)(1) === 10
*/
function pe(f, context, args) {
if(!args) args = [];
if (args.length === f.length) return f.apply(context, args);
return function partial (a) {
var args_copy = args.concat.apply(args, arguments);
@pablodenadai
pablodenadai / fn.bind.js
Created February 8, 2017 05:57
.bind - JavaScript
/**
* The bind() method creates a new function that, when called,
* has its this keyword set to the provided value,
* with a given sequence of arguments preceding any
* provided when the new function is called.
*/
this.x = 9; // this refers to global "window" object here in the browser
var module = {
x: 81,
@pablodenadai
pablodenadai / mode.js
Last active February 8, 2017 05:31
Calculate the mode of a given string - JavaScript ES6
const mode = (str) => {
return str
.split(' ')
.reduce((data, key) => {
let counter = data.map[key] + 1 || 1
data.map[key] = counter
if (counter > data.counter) {
data.counter = counter
data.mode = key
@pablodenadai
pablodenadai / pascalTriangleRecursive.js
Created February 8, 2017 01:10
Implementation of Pascal Triangle using Recursive Function - JavaScript ES6
const pascalTriangleRecursive = (n, arr) => {
if (!arr) arr = [[1]] // Create triangle
if (n < 2) return arr // Exit
const prevLine = arr[arr.length - 1] // Get previous line
const newLine = [1] // Create new line
// Populate new line
prevLine.forEach((item, index) => {
@pablodenadai
pablodenadai / RouteChangeStart.js
Last active May 23, 2019 13:28
AngularJS: On route change start - Check if user has permission to proceed or redirect it to the Sign In page.
app.run(function ($rootScope, $location, AuthenticationModel) {
// Register listener to watch route changes.
$rootScope.$on('$routeChangeStart', function (event, next, current) {
if (!AuthenticationModel.isSignedIn() && next.requireAuthentication === true) {
$location.path('/signin');
}
});
});