Skip to content

Instantly share code, notes, and snippets.

@spasiu
spasiu / breadth-first_search.py
Last active May 2, 2017 06:31
breadth-first search in Python
def dequeue(q):
"""takes an array representing a queue and removes the first element of that array and returns it"""
dequeued = q[0]
del(q[0])
return dequeued
def check(target, node):
"""takes a number representing the targeted node and a number representing the current node and
compares them, returning True if they are equivalent and false otherwise"""
print "checking %d" %node
@spasiu
spasiu / gist:899275142b5edf2d0af9
Created October 24, 2014 13:32
unconditional depth search
SAMPLE_GRAPH = {1:[2,3,4],
2:[5,6,7,8],
3:[9],
4:[10],
5:[9],
6:[3,4],
7:[2,11],
8:[9],
9:[1,2,3,4],
10:[6],
//creating a firebase object
var datafire = new Firebase('https://purchases.firebaseio.com');
//a listener that watches the firebase server for changes to our object and executes a callback
//I guess we're going to need to generate one when a user logs in so they can watch only their object for changes
datafire.on('value', function(snapshot){
var firedata = snapshot.val();
console.log(firedata);
});

Four chapters into Learn you a Haskell for Great Good I decided to tackle a codewars problem in Haskell

The problem was to take an array and a number, and return the array ommitting occurences of any integer in excess of the given number. So like func ([1, 1, 2, 3], 1) -> [1, 2, 3]. Great, so at first I was like, let's do this!

...

... and drew a complete blank. So I did it in JavaScript.

@spasiu
spasiu / gist:f6bb98ce9217f4309bac
Created June 11, 2015 00:29
middleware to attach raw request body, as a promise, to express request
function attachRawBody(req, res, next) {
var body = '';
req.rawBody = Q.defer();
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
req.rawBody.resolve(body);
});
next();
@spasiu
spasiu / req.js
Created August 28, 2015 15:31
Non JWT requests
'use strict';
var request = require('superagent');
request
.post('https://supportkit-staging.herokuapp.com/api/login')
.send({
email: '<EMAIL>',
password: '<PASSWORD>'
})
@spasiu
spasiu / async.md
Last active May 2, 2021 20:55
writing async javascript

No more bullshit async code

This is async code written with ES6 generators (and a library like Co or Koa):

var item = yield get.db('id...');
item.key = yield get('http://...').body.value;
yield item.save();
console.log('done!');

So, I create a generator and wrap it in an event emitter

const util = require('util')
const EventEmitter = require('events').EventEmitter

// declare a generator that counts forever
function *Generator(n) {
    while (true) yield n++
}
@spasiu
spasiu / stream_readable.js
Created November 4, 2015 01:57
readable streams
const Readable = require('stream').Readable;
const util = require('util');
function Reader() {
Readable.call(this);
this.arr = [0,1,2,3,4,5,6,7,8,9,10,11];
}
util.inherits(Reader, Readable);

you know how to use Express middleware

app.get(‘/‘, function(req, res) {
  var someVar1;
  promisesomeshit()
    .then(function(someshit){
      someVar1 = someshit;
      return promiseSomeOtherShit();  
    })
 .then(function(otherShit){