Skip to content

Instantly share code, notes, and snippets.

@guipn
guipn / inAny.js
Created February 15, 2013 03:04
inAny is a predicate determining whether some property exists in any of its first parameter's elements.
function inAny(a, prop) {
return a.some(function (each) {
return typeof each[prop] !== 'undefined';
};
}
var a = [{}, {}, {found: 0}];
inAny(a, 'found'); // true
@guipn
guipn / parseInts.js
Last active December 13, 2015 16:39
Example input: '(3,5)'
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);
}
});
}
@guipn
guipn / NoTypeError.js
Last active December 12, 2015 06:19
This won't throw a TypeError.
// 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' ) {
...
}
@guipn
guipn / countOccurr.hs
Created December 30, 2012 20:52
Counting occurrences in a list
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)]
@guipn
guipn / then.js
Created November 4, 2012 05:44
Function.prototype.then
function square(x) {
return x*x;
}
function add3(x) {
return x + 3;
}
function sub10(x) {
return x - 10;
@guipn
guipn / binsearch.c
Created October 23, 2012 16:17
Generic binary search in C
/*
* 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.
*
*/
@guipn
guipn / Major.hs
Created October 21, 2012 05:05
Major encoder
{- 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`.
-
@guipn
guipn / roman.hs
Created October 8, 2012 02:26
Roman Notation
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")
]
@guipn
guipn / qsort.js
Created September 22, 2012 04:17
Simple quicksort implementations
// The simplest implementation I can write.
function qsort(array) {
var lower, upper, pivot;
if (array.length <= 1) {
return array;
}
@guipn
guipn / stripcomment.c
Created August 25, 2012 04:34
C Comment Stripper
/**
* 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>