Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
@freddi301
freddi301 / javascript_functional_double_linked_list.js
Created December 12, 2015 16:24
JavaScript Functional Double-linked List
function prev(prev){return (self)=>(next)=>prev};
function self(prev){return (self)=>(next)=>self};
function next(prev){return (self)=>(next)=>next};
function node(self){return (next)=>(prev)=>(select)=>select(prev)(self)(next?next(node(self)(next)(prev)):next)};
function array2doublelist(array, index){
return index<array.length?
node(array[index])(array2doublelist(array,index+1))
:null;
@freddi301
freddi301 / lambda.js
Created June 17, 2016 09:35
Lambda JS in Thunks
// @flow
//"use strict";
interface Evaluable {
evaluate(scope: Object): ()=> Evaluable
}
/*
{ type: Lambda ,name:String , argument: String, body: Evaluable }
*/
@freddi301
freddi301 / graphql.js
Created June 17, 2016 09:38
GraphQL Node Hello World with thron
const g = require('graphql'),
graphql = g.graphql,
GraphQLSchema = g.GraphQLSchema,
GraphQLObjectType = g.GraphQLObjectType,
GraphQLString = g.GraphQLString
const schema = new GraphQLSchema({
query: new GraphQLObjectType({ name: 'RootQueryType', fields: {
hello: { type: GraphQLString, resolve() { return 'world'; } },
ciao: { type: GraphQLString, resolve(){return Promise.resolve("heila")} }
@freddi301
freddi301 / MongoCappedEventStore.js
Created June 17, 2016 09:40
Mongo EventStore with capped collection + throng
const mongodb = require('mongodb')
const Bluebird = require('bluebird')
class EventStore {
constructor(dbUrl, collectionName){
this.db = mongodb.MongoClient.connect(dbUrl);
this.cappedCollection = this.db.then(db=>
db.createCollection(collectionName, { capped: true, size: 1024*1024 })
.then(()=> db.collection(collectionName).createIndex({__t: 1}, {unique: true}))
.then(()=> db.collection(collectionName))
@freddi301
freddi301 / pgnotify.js
Created June 17, 2016 09:44
Postgres Notify Trigger on eany table in JSON + node listener
var pg = require ('pg');
var pgConString = "postgres://postgres:mysecretpassword@localhost/postgres"
pg.connect(pgConString, function(err, client) {
if(err) {
console.log(err);
}
client.on('notification', function(msg) {
console.log(msg);
@freddi301
freddi301 / optimisticLoopMongo.js
Created June 20, 2016 17:04
Optimistic Loop Mongo Node + throng
import throng from 'throng';
import {MongoClient} from 'mongodb';
let dups = 0;
async function insertOptimisticLoop(doc, collection, retry=100){
const cursor = await collection // find greatest __t progrssive number
.find( { __t: { $exists: true } }, { __t: 1, __id: -1} )
.sort( { __t: -1 } ).limit(1);
const next = await cursor.hasNext() ? (await cursor.next()).__t + 1 : 1; // calculate next __t
@freddi301
freddi301 / docker_proxy_set.sh
Last active June 28, 2016 18:23
Docker Utils Scripts
#!/bin/sh
mkdir -p /etc/systemd/system/docker.service.d
echo "[Service]
Environment=\"HTTP_PROXY=http://10.215.2.2:8080/\"
Environment=\"HTTPS_PROXY=https://10.215.2.2:8080/\""\
> /etc/systemd/system/docker.service.d/http-proxy.conf
echo sudo systemctl daemon-reload
@freddi301
freddi301 / clone_gists.js
Created June 28, 2016 10:38
Clone Public gists
const gistUser = process.argv[2]; if (!gistUser) {
console.error('no gist user specified'); process.exit(1); }
const destDir = process.argv[3] || './';
const exec = require('child_process').exec;
const path = require('path');
exec(`curl -s https://api.github.com/users/${gistUser}/gists`, (e, so, se) => {
gists = JSON.parse(so).map(gist => ({
folder: gist.description || gist.id,
pull: gist.git_pull_url }));
@freddi301
freddi301 / rx_transducers.js
Last active October 6, 2016 01:58
rx + transducers
const map = f => r => (m, i) => r(m, f(i));
const filter = p => r => (m, i) => (p(i) ? r(m, i) : m);
function conc(m, i) { return m.concat(i); }
[1, 2, 3].reduce(
map(x => (console.log('map ' + x), x + x))(
filter(x => (console.log('filter ' + x), x !== 4))(conc)),
[]);
class S {
@freddi301
freddi301 / doc_build.sh
Created March 1, 2017 02:43
Automatic gh-pages branch generation script
#!/usr/bin/env bash
set -e
set -x
COMMIT=$(git rev-parse master)
TAG=$(git describe $(git rev-list --tags --max-count=1))
DATE=`date +%Y-%m-%d`
mkdir -p doc-building