Skip to content

Instantly share code, notes, and snippets.

@dszakallas
dszakallas / coroutine.js
Last active February 1, 2018 20:35
Javascript Snippets
// Turn a generator function into a coroutine
const coroutine = (f) => (...args) => {
const g = f(...args) // instantiate the generator
const iter = ({ done, value }) => done // iterate over suspensions
? value
: value.then(
(d) => iter(g.next(d)),
(e) => iter(g.throw(e))
)
return Promise.resolve().then(() => iter(g.next())) // start iterating asynchronously
@dszakallas
dszakallas / rng.hs
Created February 11, 2017 18:49
randomNumberGenerator
{-# LANGUAGE NoImplicitPrelude #-}
module Rng where
import GHC.Show (Show)
import GHC.Base (Eq, ($))
import GHC.Num ((*), (+), Integer)
import GHC.Float (Float, Double)
import GHC.Real (fromIntegral, (/))
import GHC.Enum (maxBound)
import Data.Int (Int64, Int32, Int16, Int8)
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
deriving (Eq, Ord, Show, Read, Bounded, Enum)
-- and that's it. we have everything we need
-- david@mac:~% stack ghci
-- λ> Monday == Tuesday
-- False
-- λ> Monday == Monday
-- True
-- λ> Tuesday > Monday
@dszakallas
dszakallas / dbVerify.js
Created February 1, 2017 12:11
avoiding callback pyramids
#!/usr/bin/env node
'use strict'
var mysql = require('mysql')
var fs = require('fs')
var path = require('path')
function exitOnError (f) {
return function (err) {
@dszakallas
dszakallas / hashes.js
Created January 27, 2017 22:20
benchmark hashes
'use strict'
var crypto = require('crypto')
var promise = Promise.resolve()
var data = []
for (var i = 0; i < 1e5; ++i) {
promise = promise.then(function () {
return new Promise(function (resolve, reject) {
@dszakallas
dszakallas / fib.md
Last active April 3, 2018 23:54
Evolving a Fibonacci

We all know about the Fibonacci sequence. Some of us also know a song that uses it to achieve a spiraling feeling (yes, Tool fans!).

In Haskell:

fib 1 = 0
fib 2 = 1
fib x = fib (x - 1) (x - 2)
@dszakallas
dszakallas / ShortestPaths.md
Last active April 3, 2018 23:44
ShortestPaths.md

As a rookie Haskeller coming from imperative languages I am still often shocked how elegantly mathematical problems can be expressed in this great language. Last time a wrote a blogpost about recursion. As an example, I presented the Relation type and implemented an operation to it I called join. If you are familiar with relational algebra you might have recognized that this is a specific case of equijoin, where you join pairs so that the first pair's second element matches the second pair's first element. I was thinking about a more complex problem I could solve with this toolkit, one

@dszakallas
dszakallas / fp4_recursion.md
Last active October 8, 2016 09:56
FP Training

FP Training 4: Recursion

Agenda

  • Example 1: factorial
  • Example 2: map
  • Example 3: join

1. We implemented the factorial example in JavaScript imperatively:

#/usr/bin/env sh
function killport() {
lsof -i :$1 | tail -n 1 | awk '{ printf $2 }' | xargs kill -9
}
@dszakallas
dszakallas / naive-dfs.js
Last active May 30, 2016 19:21
naive dfs
function dfs (vertex, visited) {
if (visited.indexOf(vertex.id) !== -1) { // cut visited nodes
return null
}
if (!vertex.children) { // leaf, return its value
return { id: vertex.id, sum: vertex.value }
} else { // inner node, traverse leaves and aggregate their values
var sum = 0
var childSums = []
for (var child in vertex.children) {