This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inAny(a, prop) { | |
return a.some(function (each) { | |
return typeof each[prop] !== 'undefined'; | |
}; | |
} | |
var a = [{}, {}, {found: 0}]; | |
inAny(a, 'found'); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseInts(str) { | |
return str.split(/\s*,\s*/).map(function (v, i) { | |
switch (i) { | |
case 0: return parseInt(v.split('').pop(), 10); | |
case 1: return parseInt(v.split('').shift(), 10); | |
default: return parseInt(v, 10); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// re-written to eliminate need for premises regarding existence of `contacts[i]`. | |
if ( typeof contacts === 'object' && | |
typeof contacts[i] === 'object' && | |
typeof contacts[i].name === 'object' && | |
typeof contacts[i].name.formatted !== 'undefined' && | |
typeof contacts[i].phoneNumbers !== 'undefined' ) { | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
count :: (Eq a, Ord a) => [a] -> [(a, Int)] | |
count = map wrap . group . sort | |
where wrap g = (head g, length g) | |
---------------------------------------- | |
increment :: (Eq a) => [(a, Int)] -> a -> [(a, Int)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function square(x) { | |
return x*x; | |
} | |
function add3(x) { | |
return x + 3; | |
} | |
function sub10(x) { | |
return x - 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This differs from the standard function `bsearch` in the following important ways: | |
* | |
* 1. Parameter ordering: this version groups entities with their sizes; | |
* 2. Return value type: this version returns an index, or -1 if the key isn't present. This is more useful; | |
* 3. Speed: this version is likely slower in an implementation-defined way; | |
* 4. Consistence: this version has not been thoroughly tested. | |
* | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- Major.hs | |
- ======== | |
- | |
- This program can be used to convert from strings of digits to lists of | |
- possible mnemonic consonant sounds, as defined by the major memory system, | |
- presented in correct order. | |
- | |
- Input must be served as a string, so that cases like `0001` are not misinterpreted | |
- as being `1`. | |
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trivial :: [(Int, String)] | |
trivial = [ | |
{- Bridge values -} | |
(1, "I"), (5, "V"), (10, "X"), (50, "L"), | |
(100, "C"), (500, "D"), (1000, "M"), | |
{- Subtractions -} | |
(4, "IV"), (9, "IX"), (40, "XL"), (90, "XC"), | |
(400, "CD"), (900, "CM") | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The simplest implementation I can write. | |
function qsort(array) { | |
var lower, upper, pivot; | |
if (array.length <= 1) { | |
return array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Takes C source code as input and removes comments. | |
* | |
* gcc -std=c99 -Wall -Wextra -Wall -pedantic stripcomments.c -o sc | |
*/ | |
#include <errno.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> |