Skip to content

Instantly share code, notes, and snippets.

View AloofBuddha's full-sized avatar

Benjamin Cohen AloofBuddha

View GitHub Profile
@AloofBuddha
AloofBuddha / associations.md
Last active February 27, 2024 08:25
Sequelize Relationships: hasOne vs belongsTo
const initialState = {
activeIndex: null,
flights: []
}
export default function reducer (state = initialState, action){
switch(action.type) {
// lets create first.
// This means we create a flight then add it to a *copy* of the list.
case CREATE_NEW_FLIGHT:
// ES6 of the week: arrow functions and lexical scope
// https://github.com/lukehoban/es6features
// arrow functions are a shorthand for function syntax,
// which make them a lot more succint for functions that take functions
// (i.e. 'higher order functions')
var evens = [2,4,6,8,10];
@AloofBuddha
AloofBuddha / sequelize-belongs-to-example.js
Created September 29, 2016 15:15
An explanation of how to define a One-to-One relationship in Sequelize and different ways of create the linked objects
// Product Model definition, called 'product' and associated with table 'products'
var Product = this.sequelize.define('product', {
title: Sequelize.STRING
});
// User Model definition, called 'user' and associated with table 'users'
var User = this.sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
@AloofBuddha
AloofBuddha / sequelize-cheat-sheet.md
Created September 26, 2016 14:28
Sequelize cheat sheet

Note: $> means command line input

Dependencies

Dependencies to use Sequelize $> npm install --save sequelize pg pg-hstore

Setup

Must create the DB first $> createdb my-music

@AloofBuddha
AloofBuddha / db-server.js
Last active August 29, 2015 14:25
node.js database server
var http = require('http');
url = require('url');
var database = {}; // will need to read from file here
var setRegex = /^\/set\?.+=.+$/, // /set?somekey=somevalue
getRegex = /^\/get\?key=.+$/; // /get?key=somekey
http.createServer(requestListener).listen(4000);
@AloofBuddha
AloofBuddha / gist:d95ad453031b860c484b
Created November 13, 2014 08:54
Linked List search in Python
# we want to match data, not a Node, as search
# shouldn't require any Node creation either
def search(self, data):
node = self.head
while node:
if node.data == data:
return node
else:
node = node.nextNode
@AloofBuddha
AloofBuddha / permutations.hs
Last active July 4, 2022 03:31
This program defines a functions "perms" in a number of different (but functionally equivalent) ways. "perms" takes as input a list (of *any* kind) and as output, returns a list of all possible permutations of that list.
import Data.List (inits, tails)
main :: IO ()
main = do
print $ perms [1..4]
print $ perms' "abcd"
print $ perms'' [Nothing, Just 1, Just 2, Just 3]
perms, perms', perms'' :: [a] -> [[a]]