Skip to content

Instantly share code, notes, and snippets.

View jpablo's full-sized avatar

juan pablo romero jpablo

View GitHub Profile
@jpablo
jpablo / position_change.scala
Last active September 12, 2016 01:56
Effective ML notes in Scala (https://vimeo.com/14313378)
trait PositionChange1 {
type OrderIdT
type ExecutionT
sealed trait Message
case class Cancel(order: OrderIdT) extends Message
case class Exec(e: ExecutionT) extends Message
def positionChange(m: Message) = m match {
function Vector() { ... }
Vector.prototype.norm = function() { ... }
function Vector2D() {
// Watch out!
// We need to invoque the parent constructor
// so we don't miss any initialization behaviour
Vector.call(this);
// (or Vector.apply(this, arguments))
function Vector() {
this.data = [];
}
Vector.prototype.norm = function() {
var sumsq = 0;
for(var i in this.data) {
sumsq += this.data[i] * this.data[i];
}
return Math.sqrt(sumsq);
@jpablo
jpablo / vector2d.js
Last active December 16, 2015 14:29
function Vector2D(x,y) {
this.data = [x,y];
}
Vector2D.prototype.sum = function(o) {
var sx = this.data[0] + o.data[0],
sy = this.data[1] + o.data[1];
return new Vector2D(sx,sy)
};
// file: client.js.
// Define the client object
var client = {
url: 'http://echo.jsontest.com/u/1/b/c',
fetch: function() {
return $.getJSON(this.url)
}
}
@jpablo
jpablo / getjson1.js
Last active December 14, 2015 18:48
var p = $.getJSON('http://echo.jsontest.com/u/1/b/c');
function displayData(data) {
$('.content').html(JSON.stringify(data, null, 2))
}
function logData(data) {
console.log('data received', data);
}
@jpablo
jpablo / gist:3971573
Created October 29, 2012 04:42
water drops
f[a_, x_] := a - .5 (x - .7)^2
randomize[r_][x_] := x + RandomReal[{-Abs[x]*r, Abs[x]*r}]
drop[a_][x_] := With[{
rx = randomize[.15][.006],
ry = randomize[.15][.004],
yd = RandomChoice[{1, -1}]*randomize[1][x*.003]},
Circle[{x, f[a, x] + yd}, {rx, ry}]]
@jpablo
jpablo / gist:3885206
Created October 13, 2012 16:20
scala version of some java code
val items = getNodes("/List/Item", document) map(_.getTextContent())
@jpablo
jpablo / gist:3885136
Created October 13, 2012 16:04
java "map"
import com.google.common.collect.Iterables;
// ...
Iterable<String> items = Iterables.transform(getNodes("/List/Item", document),
new Function<Node, String>() {
public String apply(@Nullable Node node) {
//noinspection ConstantConditions
return node.getTextContent();
}
});
@jpablo
jpablo / async_map.js
Created July 16, 2012 02:20
async map
function asyncMap(promises, callback) {
var results = [];
for(var p in promises) {
if(!promises.hasOwnProperty(p)) { continue; }
promises[p].done(function(result) {
results.push(callback(result))
})
}
return $.when.apply(null,promises).pipe(function() { return results });
}​