Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@jooyunghan
jooyunghan / BinTreeTest.scala
Created June 29, 2016 01:43
BinTree example with Free Monad
package bintree
import org.scalatest.{FlatSpec, Matchers}
import scalaz.Free.Trampoline
import scalaz.{-\/, Free, Functor, Trampoline, \/-}
object tree {
case class Pair[A](l:A, r:A)
@jooyunghan
jooyunghan / codensity.hs
Last active June 28, 2016 06:29
Example codes from "Asymptotic improvement of computations over free monads" by Janis
{-# LANGUAGE RankNTypes, MultiParamTypeClasses, FunctionalDependencies, FlexibleContexts, FlexibleInstances, UndecidableInstances, DeriveFunctor #-}
import Prelude hiding (abs)
import Control.Monad
-- Free --
data Free f a = Pure a | Wrap (f (Free f a))
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
@jooyunghan
jooyunghan / Cont.hs
Created June 23, 2016 14:17
Cont type in Haxl is used to improve the performance
data Cont u a
= Cont (GenHaxl u a)
| forall b. Cont u b :>>= (b -> GenHaxl u a)
| forall b. (Cont u (b -> a)) :<*> (Cont u b)
| forall b. (b -> a) :<$> (Cont u b)
@jooyunghan
jooyunghan / Fetch.hs
Last active June 23, 2016 14:10
Monad instance of Fetch from ICFP2014 Haxl paper
instance Monad Fetch where
return a = Fetch $ \ref -> return (Done a)
Fetch m >>= k = Fetch $ \ref -> do
r <- m ref
case r of
Done a -> unFetch (k a) ref
Blocked br c -> return (Blocked br (c >>= k))
Throw e -> return (Throw e)
@jooyunghan
jooyunghan / child.js
Created March 10, 2016 17:27
Make a promise-returning function as non-overlapping one.
process.on('message', function(message) {
console.log("message received", message);
process.send(message.toUpperCase(), function(err) {
console.log("replied");
});
});
@jooyunghan
jooyunghan / regex.js
Created March 10, 2016 15:19
Print look-and-say sequence using Regex/Generator
const ant = function *(s = "1") {
yield s;
yield* ant(s.replace(/(.)\1?\1?/g, g => g.length + g[0]));
}
for (var i=0, a = ant(); i++<10; ) {
console.log(a.next().value)
}
@jooyunghan
jooyunghan / promise-util.js
Created March 9, 2016 04:01
Promise helper which works like mapM
/**
* @template T, S
* @param {[T]} items
* @param {function(T):Promise<S>} f
* @return {Promise<[S]>}
*/
function traverse(items, f) {
if (items.length == 0) {
return Promise.resolve([]);
}
@jooyunghan
jooyunghan / csp.js
Created March 8, 2016 17:59
Yet another look-and-say using js-csp
var csp = require("js-csp");
function Init() {
return csp.go(function* () {
return 1;
});
}
function Next(i) {
var out = csp.chan();
@jooyunghan
jooyunghan / iter.js
Last active June 22, 2016 00:32
Another look-and-say sequence using JS Iterator(Generator)
function* Init() {
yield 1;
}
function* Next(it) {
var prev = it.next().value;
var count = 1;
for (let n of it) {
if (n == prev) {
count++;
@jooyunghan
jooyunghan / child.js
Last active March 7, 2016 20:54
Promise 로 래핑하면서 오히려 실수할만한 내용.
process.on('message', function(message) {
console.log("message received", message);
process.send(message.toUpperCase(), function(err) {
console.log("replied",err);
});
});