Skip to content

Instantly share code, notes, and snippets.

View hoodwink73's full-sized avatar

Arijit Bhattacharya hoodwink73

View GitHub Profile
@hoodwink73
hoodwink73 / pingPongInCSP.js
Created December 5, 2016 05:43
Obligatory CSP examples
csp.go(function* () {
// this routine acts
// like the referee
var table = csp.chan();
// two routines represents two players
// they have access to the same channel
csp.go(player, ["ping", table])
csp.go(player, ["pong", table])
@hoodwink73
hoodwink73 / hideMonkeyPatching.js
Created August 15, 2016 18:46
Middlewares - a way to facilitate third party extension points around a framework's core operations
// original method
function canBeEnhancedLater (x) {
return x
}
// enhancement modules
function double (next) {
return function (x) {
return next(2 * x)
@hoodwink73
hoodwink73 / getFile.js
Created July 12, 2016 04:22
The thunk way to run parallel asynchronous operation but control the order of the callbacks
function getFile (file) {
var text, fn;
fakeAjax(file, function oldSchoolCb (response) {
if (fn) {
fn(response)
} else {
text = response
}
})
@hoodwink73
hoodwink73 / truncateTextWithReadMore.js
Created January 23, 2016 14:07
Some common handlebars helpers
Handlebars.registerHelper('trimTextWithReadMoreLink', function (textToTrim) {
var link, textToShow, el;
var wordsAllowed = 30;
if ( textToTrim === undefined) {
return "";
}
var wordsTokenized = textToTrim.split(' ');
if (wordsTokenized.length > wordsAllowed) {
@hoodwink73
hoodwink73 / modulePattern.js
Last active August 29, 2015 14:20
A quick revision of the infamous Module Pattern
// MAKING GLOBALS EXPLICIT
// -----------------------
// large projects may make finding globals
// inside a module confusing
// to spot globals easily, lets just
// pass it as arguments
(function ($, YAHOO) {
// now have access to globals jQuery (as $) and YAHOO in this code
}(jQuery, YAHOO));
@hoodwink73
hoodwink73 / jink.jade
Created July 20, 2014 03:13
Mixins for creating Ink modules
mixin container()
table.container(class!=attributes.class)
tr
td
if block
block
mixin row()
table.row(class!=attributes.class)
tr
1. Two space soft indents (fake tabs) OR tabs... BUT NEVER BOTH - DO NOT MIX
2. Whitespace, Parens, Braces, Linebreaks
if/else/for/while/try always have spaces, braces and multiple lines.
--------------------------------------------------------------------
@hoodwink73
hoodwink73 / pipeline.js
Created July 7, 2014 03:09
Using Array.reduce to pipeline functions
var Pipeline = function (series, initialInput) {
this.series = series;
this.initial = function () {
return initialInput;
};
this.hasPerformed = false;
};
Pipeline.prototype.addFunction = function (fn, index) {
if (index === undefined) {
@hoodwink73
hoodwink73 / FSM_Simulator.py
Created April 22, 2014 04:51
A finite state machine simulator.
# FSM Simulation
# edges are how an FSM is represented as a data unit
# Every edge is a key value pair
# A key is a tuple of (current_state, character)
# The value is the next state achieved after the previous
# state consumes a particular character
# r'a+1+'
edges = {(1, 'a') : 2,
@hoodwink73
hoodwink73 / formatUnit.js
Last active August 29, 2015 14:00
A general method which splits a metric expressed in a single smaller unit into multiple higher units
// A general method which splits a metric
// expressed in a single smaller unit into multiple
// higher units
// 1689 minutes => 1 day 9 hours 9 minutes
// 1678 millimeter => 1 meter 67 centimeter 8 millimeter
// The method takes two parameters, the metric itself
// and an array which defines the conversion