Skip to content

Instantly share code, notes, and snippets.

View pietro909's full-sized avatar

pietro909 pietro909

View GitHub Profile
@pietro909
pietro909 / defensive-approach-javascript.js
Last active October 28, 2016 08:33
Example of how to handle unknown input
function contains(c, s) {
var index = typeof s === 'string' ? s.indexOf(c) : -1;
return index > -1;
}
console.log(contains("hi", "there")); // false
console.log(contains("hi", "hi, all")); // true
console.log(contains("hi", 1)); // false
console.log(contains("hi", null)); // false
@pietro909
pietro909 / runtime-error-typescript.ts
Last active October 28, 2016 08:33
A runtime error in Typescript
function contains(c: string, arr: string) {
var index = arr.indexOf(c);
return index > -1;
}
console.clear();
console.log(contains("hi", "there")); // false
console.log(contains("hi", "hi, all")); // true
console.log(contains("hi", 1)); // doesn't compile
console.log(contains("hi", null)); // TypeError: Cannot read property 'indexOf' of null
@pietro909
pietro909 / runtime-error-javascript.js
Last active October 28, 2016 16:59
A runtime error in Javascript
function contains(c, s) {
var index = s.indexOf(c);
return index > -1;
}
console.log(contains("hi", "there")); // false
console.log(contains("hi", "hi, all")); // true
console.log(contains("hi", 1)); // TypeError: s.indexOf is not a function
console.log(contains("hi", null)); // TypeError: Cannot read property 'indexOf' of null
@pietro909
pietro909 / gulp-for-elm.js
Created September 24, 2016 17:55
Gulp for Elm, thanks to Stef from Slack channel
'use strict';
var gulp = require('gulp'),
http = require('http'),
st = require('st'),
exec = require('child_process').exec,
gutil = require('gulp-util'),
clear = require('clear'),
counter = 0;
{- OVERVIEW ------------------------------------------------------
A "Tree" represents a binary tree. A "Node" in a binary tree
always has two children. A tree can also be "Empty". Below I have
defined "Tree" and a number of useful functions.
This example also includes some challenge problems!
-----------------------------------------------------------------}
@pietro909
pietro909 / SLISW-IO-3.1.io
Created May 18, 2016 20:49
SLISW - IO - 3.1
OperatorTable addAssignOperator(":", "atPutNumber")
curlyBrackets := method(
r := Map clone
call message arguments foreach(arg,
r doMessage(arg)
)
r
)
@pietro909
pietro909 / SLISW-IO-2.5.io
Last active May 18, 2016 09:55
SLISW - IO - 2.5
Matrix := Object clone
Matrix dim := method(x, y,
self matrix := list();
for(1, 0, x - 1, 1,
row := list();
for(1, 0, y - 1, row push(0));
matrix push(row)
)
)
@pietro909
pietro909 / SLISW-IO-2.4.io
Created May 10, 2016 20:10
SLISW - IO - 2.4
l = list(1,2,3,4,5)
l average := method(
s := self size;
if(
size==0,
0,
self reduce(+) / size
)
)
l average # => 3
@pietro909
pietro909 / SLISW-IO-2.3.io
Created May 10, 2016 20:00
SLISW - IO - 2.3
a := list(list(1,2,3), list(4,5,6), list(7,8,9))
a flatten reduce(+)
@pietro909
pietro909 / SLISW-IO-2.2.io
Created May 10, 2016 19:50
SLISW - IO - 2.2
old_div := Number getSlot("/")
Number / := method(den, if( den == 0, 0, self old_div(den)))
2/0 # => 0
4/2 # => 2