Skip to content

Instantly share code, notes, and snippets.

;Stolen from "Structure and Interpretation of Computer Programs"
;https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-20.html#%_sec_3.1.1
;Implementation:
;---------------
(define (make-account balance)
;withdraw method:
(define (withdraw amount)
(if (>= balance amount)
/*
A function that rotates a 2-dimensional array by 90 degrees.
> _([
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]
]).rotate().value()
[
@abuseofnotation
abuseofnotation / .vimrc
Last active February 12, 2016 15:30
Debug node.js Apps from Vim
" A handy mapping for debugging node.js applications from vim using the node build-in debugger.
" Add the following mapping to your .vimrc file:
nnoremap <C-D> Odebugger<esc>:w<CR>:!node debug %:p <CR>dd:w<CR>
" Now in vim, go to the place where you want to break and press Ctrl + D.
" This inserts a 'debugger' statement, at your current line and launches the file in debug mode.
" You can also combine this with a testing framework like nodeunit:
" Authors: http://vim.wikia.com/wiki/Vim_on_Freenode
" inoremap
" Description: A minimal, but feature rich, example .vimrc. If you are a
" newbie, basing your first .vimrc on this file is a good choice.
" If you're a more advanced user, building your own .vimrc based
" on this file is still a good idea.
"------------------------------------------------------------
" Features {{{1
"
@abuseofnotation
abuseofnotation / d3-react.js
Created February 12, 2016 11:07
Integrating D3 within React
/*
* A function for integrating D3 and React.
*
* Receives a reusable D3 chart as per https://bost.ocks.org/mike/chart/.
*
* Returns a React component which Renders the chart and updates it
* using the 'data' property from its 'props' object.
*/
// IMPLEMENTATION:
@abuseofnotation
abuseofnotation / parser_combinators.js
Created August 9, 2016 13:27
Simple parser combinators implementation in JS
const { dropWhile } = require('lodash')
const parser = {
of: (value) => Parser((_) => ({value, string:''})),
chain(f) {
return Parser((prevString) => {
const { value, string, error} = this.parse(prevString)
return error === undefined ? f(value).parse(string) : {error, value}
})
},
andThen(next) {
const { adt, object } = require('./core')
const Free = adt.data('Free', {
Pure: (value) => ( { value } ),
Impure: (value, func) => ( { value, func } )
}).derive(adt.show)
const { Pure, Impure } = Free
Pure.prototype.chain = function (f) {
//Converts a value to string
const valueAsString = (value) => value.toString()
//Returns true if 'num' is divided without remainder by 'divider'
const dividesBy = (divider) => (num) => num % divider === 0
//Prints 'i' only if it obeys the condition given by 'cond', using 'f' as a formatting function
const printIf = (cond, f) => (i) => cond(i) ? f(i) : ""
//Applies an array of functions to a value and concats the result
/*
* Implementation
*/
// Takes an object containing several effectful functions,
// Returns a similar object where function does not do IO, but just returns an object with its name and args.
const constructDsl = (obj) => Object.keys(obj).reduce((valueObj, fn) => {
valueObj[fn] = (...args) =>({fn, args})
/*
Given a list of objects which form one or many trees, this function builds and returns these trees. Works in linear time.
*/
const idField = 'id';
const parentIdField = 'parent'
const buildTree = (objectList) => {
let mainObjects = [];
let objects = {};