Skip to content

Instantly share code, notes, and snippets.

@svanellewee
svanellewee / nodelisp.js
Last active August 29, 2015 14:17
javascript mcarthy etc
var util = require("util");
var Cons = function(a,b) {
var cons = function(fn) { return fn(a,b); };
return cons;
};
var Car = function(cons) {
var car = function(a,b) { return a; };
return cons(car);
@svanellewee
svanellewee / remove_lowest_mark.js
Last active August 29, 2015 14:18
Remove the lowest mark of documents
var mc = require("mongodb").MongoClient
mc.connect("mongodb://localhost:27017/school", function(err, db) {
if (err) {
return console.error(err);
}
/*
db.collection("students").find({}).toArray(function(err, students) {
if (err) {
return console.error(err);
}
@svanellewee
svanellewee / harmony.js
Created June 4, 2015 14:07
Node ES6/Harmony Examples
//node --harmony b.js
"use strict";
var b = [1,20,1000].map(e => e+100)
b.forEach(e => console.log(e));
if( true) {
let b = "spartaaa"+12;
console.log(b);
}
b.forEach(e => console.log(e));
@svanellewee
svanellewee / 01.js
Last active August 29, 2015 14:23 — forked from martinaglv/01.js
function whatDoesItDo(val){
return val ? 1 : 2;
}
@svanellewee
svanellewee / transformjsonstring.js
Created June 30, 2015 20:58
Transform input string (json) to output string (json) with less fields:
bash-3.2$ echo '{ "name": "Foo", "phone": "555-1212", "email": "[email protected]", "id": 123 }' | node b.js
[{"name":"Foo","id":123}]:buffer
@svanellewee
svanellewee / oldstyle.js
Last active August 29, 2015 14:24
Readable stream
var Stream = require("stream");
var stream = new Stream();
stream.readable = true;
var i = 0;
var x = setInterval(function() {
stream.emit("data", "."+i);
i+= 1;
}, 0);
@svanellewee
svanellewee / cons in python.py
Created July 3, 2015 20:41
Python Cons/Reduce
# Cons cells are functions! But they're also data structures!
def cons(head, tail) :
def _(fn):
return fn(head, tail)
return _
# imperative version cns.head if cns was a struct/ tuple
def head(cns) :
return cns(lambda head, tail : head)
def tail(cns):
data = '''a narrow fellow in the grass
occasionally rides;
you may have met him, did you not,
his notice sudden is.
the grass divides as with a comb,
a spotted shaft is seen;
and then it closes at your feet
and opens further on.
@svanellewee
svanellewee / mongodb node.js stream.js
Created July 4, 2015 22:05
Node js streams with mongo and filter transforms! note the ???ObjectMode settings!
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var fs = require("fs");
var Transform = require("stream").Transform;
var name_surname_email_filter = new Transform({readableObjectMode:true, writableObjectMode:true} );
name_surname_email_filter._transform = function(data, enc, cb) {
//console.log(data);
var newdata = { first_name: data.first_name,
@svanellewee
svanellewee / functorhackery.js
Last active August 29, 2015 14:25
Functors just take stuff out of "type-boxes" apply a function and put them back into the same "type-boxes".
/*
*
* Functor :
* fmap : (a -> b) -> fa -> fb
*
*/
function array_fmap1(fn, typeA) {
var typeB = []
typeA.forEach(function(aVal) {