Skip to content

Instantly share code, notes, and snippets.

@josher19
josher19 / surrealDecimals.js
Created October 26, 2012 06:21
Random Numbers, including Surreal Decimals
// random number between -1 and 1 with 0 most common result, linearly decreasing
function triangle() { return Math.random() + Math.random() - 1.0 }
// random number between -1.5 and 1.5 with 0 most common, roughly following the bell curve
function bell() { return Math.random() + Math.random() + Math.random() - 1.5 }
// random number between -1.5e-300 and 1.5e+300 with 50% of the numbers between -0.75 and 0.75
function wild() { return (Math.random() + Math.random() + Math.random() - 1.5) / (Math.random()+1e-300) }
// show distribution counts after 10,000 runs.
@josher19
josher19 / rotate-fonts.js
Created October 10, 2012 09:37
Get & Test Google Web Fonts and rotate through them by clicking on text. Chrome bookmarklet, requires jQuery.
(function() {
sel=document.getSelection()
// if (sel.focusNode) sel.focusNode.parentElement.style.fontFamily="'Poller One',sans-serif"
// function styling(ev) { sel.focusNode.parentElement.style.fontFamily="'Poller One',sans-serif"; }
// jQuery(document.body).bind('mouseup', styling, false);
@josher19
josher19 / type-of.coffee
Created October 9, 2012 22:46
Testing type-of across frames in LiveScript
type-of = (typeof!)
y = type-of([1,2]) #=> 'Array'
x = type-of [1,2] #=> ['Number', 'Number']
iframe = document.createElement('iframe');
iframe.width = 50;
iframe.height= 50;

Work-around is:

name-to-upper = (person) -> person=^^person; person.name .= to-upper-case!
# or, in this case:
name-to-upper = (person) -> ^^person.name .= to-upper-case!

How would it handle default arguments? (person=^^defaultV) works: clone defaultV when person is not defined.

// von n semi-random, but repeats too quickly
function nextr(n) { var d=(n*n); if (d%100==0) d /= 100; if (d < 100) d = d*5242870; d=d.toString(); var s=d.length; return d.substring(s-6, s-2)-0 }
function nextr(n) { var d=(n*n); if (d%100==0) d /= 100; if (d < 100) d *= 132645290; d=d.toString(); var s=d.length; return d.substring(d.length / 2 - 2, d.length / 2 + 2)-0 }
function testn(n,max) { max=max||1000; for(var h={}, i=0; i<max; ++i) { n = nextr(last=n); if(h[n]) break; h[n]=last||true; } return [i,n,last,h]; }
// primes
@josher19
josher19 / isaacRand.js
Created August 20, 2012 10:19
ISAAC random number generator
/**
------------------------------------------------------------------------------
isaacRand.js: By Josh Weinstein, based on
Rand.java: By Bob Jenkins. My random number generator, ISAAC.
rand.init() -- initialize
rand.val() -- get a random value
MODIFIED:
960327: Creation (addition of randinit, really)
970719: use context, not global variables, for internal state
980224: Translate to Java
@josher19
josher19 / livescript-docs.js
Created June 21, 2012 10:57
Start of docs for livescript
module.exports = {
"<-!": {
"definition": "Suppress returns on backcalls.",
"details": "Temporary variable to be assigned in callback on the left and function to call on the right. Returns undefined (void). ",
"example": "val <-! $.ajax 'examples/helloworld.roy'\nval2 <-! $.ajax 'examples/alias.roy'\nconsole.log val + val2",
"compilesTo": "$.ajax('examples/helloworld.roy', function(val){\n $.ajax('examples/alias.roy', function(val2){\n console.log(val + val2);\n });\n});",
"see": [
"!",
"<-",
"backcall",
@josher19
josher19 / esc.js
Created June 18, 2012 02:15
escape all letters
var esc = function (c) { var x=parseInt(c, 36); return isFinite(x) ? "%" + (x+87).toString(16) : c; }
var escw = function(word) { return word.replace(/[a-z]/g, esc); }
@josher19
josher19 / repl_snippet.js
Created June 15, 2012 09:48
Add to repl to search through history
repl.defineCommand('search', {
help: 'search through repl history',
action: function(cmd) {
if (this.rli.history) this.outputStream.write(this.rli.history.filter(function(line,n) {return n && -1 !== line.indexOf(cmd)}).reverse().join("\n")+"\n");
this.displayPrompt();
}
});
@josher19
josher19 / ultimate.ls
Created May 16, 2012 08:57
LiveScript: works in a file but not from command-line REPL
## Looks like functions defined with `->` work but those defined with `function` do not.
answer = 42
deepthought = -> "let me think about it for a while"
function question then answer
console.assert deepthought! == 'let me think about it for a while'
console.log deepthought!
console.assert question! == answer
console.log question!