Skip to content

Instantly share code, notes, and snippets.

@josher19
josher19 / LimitedQueue.coffee
Created April 20, 2012 09:38
Limited Queue based on suggestions from http://stereopsis.com/async/
# On user event -> push event into a n-element queue.
# If the number of outstanding requests is <n, fire request immediately.
# When an outstanding request works or fails, fire a pending queued request if one is waiting.
EventEmitter = process.EventEmitter or require?('events').EventEmitter or {}
LimitedQueue = (@n = 2, @log = false) ->
@log = console.log if @log is true and console? and console.log
@q = []
@req = []
@josher19
josher19 / runLimitedQueue.coffee
Created April 20, 2012 09:39
Example run of a LimitedQueue
LimitedQ = require './LimitedQueue'
assert = require 'assert'
lq = new LimitedQ(3)
lq2 = new LimitedQ()
lq.addListener "alldone", ->
console.log "alldone", arguments
lq.addListener "done", ->
@josher19
josher19 / yql-geocoder.html
Created April 20, 2012 09:40
Use Google and YQL to geocode addresses
<!DOCTYPE html>
<html>
<head>
<title>Geocode TSV</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
</head>
<body>
<form>
<textarea rows=10 cols=80 name=inp id=inp >
Post_author,post_date,post_date_gmt,post_title,category,IMAGE,tags,post_content,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent,menu_order,post_type,post_mime_type,comment_count,geo_address,geo_latitude,geo_longitude,map_view,add_feature,timing,contact,email,twitter,facebook,proprty_feature,post_city_id,video,is_featured,paid_amount,alive_days,paymentmethod,remote_ip,ip_status,pkg_id,featured_type,total_amount,website,comments_data,rating_data
@josher19
josher19 / vm_node.js
Created May 16, 2012 06:26
Simple shallow copy of global will not work as expected with runInNewContext
function clone(obj) { var nobj={}, key; for(key in obj) nobj[key]=obj[key]; return nobj; }
var local = null
var g = clone(global);
local = 3
console.assert(local === 3, 'not initialized to 3')
console.assert(null == g.local, 'g.local should be null or undefined')
@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!
@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 / 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 / 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 / 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
// 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