Skip to content

Instantly share code, notes, and snippets.

@hughfdjackson
hughfdjackson / example
Last active December 14, 2015 20:09 — forked from andyhd/maybe-monad.js
var peeps = { hugh: { name: 'hugh jackson' } }
var titleCase = function(str){
return str.split(' ').map(function(str){ return str.substr(0, 1).toUpperCase() + str.substr(1) }).join(' ')
}
var val = maybe(peeps)
.map(function(peeps){ return peeps.tahir })
.map(function(peep){ return peep.name })
.map(titleCase)
.getOrElse('couldn\'t find person')
$ flesh-out require('my-module').toNumber
provide type signature for inputs
> Int
What do you expect for: NaN?
> NaN
What do you expect for: Infinity?
> Infinity
// as an approximation of the classical model, Num will return an object with methods on it
// these methods cannot be added to at run-time for all numbers, since there's no prototype
// chain going on here
var Num = function(val){
return {
val: val,
mul: function(val) { return Num(this.val * val) },
div: function(val) { return Num(this.val / val) },
add: function(val) { return Num(this.val + val) },
var validators = {
password: function(pass) {
return /[0-9]/.test(pass) && pass.length > 10
}
}
var validate = function(el){
return validators[el.getAttribute('data-validator')](el.value)
}
var toArray = function(val){ return [].slice.call(val) }
toArray(document.getElementsByTagName('a')).forEach(function(node){
node.href = "http://hughfdjackson.com"
})
"this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string this is a really long string"
var f = function(){
var str = "this is a really long stringthis is a really long stringthis is a really long stringthis is a really long stringthis is a really long stringthis is a really long stringthis is a really long stringthis is a really long stringthis is a really long string"
var short = "str"
console.log('i love js muchly')
}
var jumble = function(arr){
return arr.slice().sort(function(){ return Math.random() > 0.5 });
};
@hughfdjackson
hughfdjackson / generators.js
Last active December 14, 2015 07:09
playing with possible js quickcheck API
// generators make random content of a type
// -- utils
var randNth = function(coll) {
return coll[Math.floor(Math.random() * coll.length)]
}
var randChar = function(){
return randNth('qwertyuiopasdfghjklzxcvbnm')
}
void function(root){
var data = new WeakMap
var meta = function(o, v){
if ( arguements.length === 1 ) return data.get(o)
else return data.set(o, v)
}
root.meta = meta
$(function()
{
$.ajax({
url: 'check.php', //the script to call to get data
success: function(data) {
$('#count').html(data); //Set output element html
}
});
});