Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / fetch.hs
Last active July 5, 2016 06:33
Fetch monad with runFetch
{-# LANGUAGE ApplicativeDo #-}
import Control.Monad
data Fetch a = Done a | Blocked (Fetch a)
instance Functor Fetch where
fmap = liftM
instance Applicative Fetch where
pure = Done
@jooyunghan
jooyunghan / monad-in-java.md
Last active November 17, 2023 04:54
한글번역 - Functor and monad examples in plain Java

Functor and monad examples in plain Java

이 글은 우리가 쓴 책, 'Reactive Programming with RxJava' 의 부록이었다. Reactive programming과 관련이 깊은 주제긴 하지만 모나드를 소개한다는 게 책과 썩 어울리지는 않았다. 그래서 나는 따로 블로그에 올리기로 했다. 프로그래밍을 다루는 블로그에서 *"반은 맞고 반은 틀릴 지 모르는 나만의 모나드 설명"*이란 것이 새로운 *"Hello World"*라는 점을 나도 잘 안다. 하지만 이 글은 펑터(functor)와 모나드(monad)를 자바 자료 구조와 라이브러리라는 각도에서 바라보고 있으며, 이는 공유할 정도의 가치는 있을거라 생각했다.

@jooyunghan
jooyunghan / kth.gen.js
Created August 2, 2016 06:02
LeetCode kth smallest using Generator(JS)
function* gen(g) {
yield* g
}
function kth(k, g) {
while (k-- > 0) g.next() // 문제는 1부터라 0대신 1
return g.next().value
}
function* merge(g1, g2) {
@jooyunghan
jooyunghan / unpack.go
Created October 20, 2016 16:27
// cspbook 4.4 X2 UNPACK >> PACK
package main
import "fmt"
// cspbook 4.4 X2 UNPACK >> PACK
func unpack(in <-chan string, out chan<- rune) {
for s := range in {
for _, c := range s {
out <- c
@jooyunghan
jooyunghan / fibo-babel.js
Last active December 2, 2016 01:59
Babel-transpiled generator(fibo)
"use strict";
var _marked = [fibo].map(regeneratorRuntime.mark);
function fibo() {
var a, b;
return regeneratorRuntime.wrap(function fibo$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0: