Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@jooyunghan
jooyunghan / pair.js
Created August 30, 2017 01:39
Pair implementation in Smalltalk-72 style
function pair(left, right) {
return {
left() {
if (arguments.length == 1) left = arguments[0];
return left;
},
right() {
if (arguments.length == 1) right = arguments[0];
return right;
},
function GenIter(c, genf) {
this.c = c;
this.genf = genf;
}
GenIter.prototype.next = function next() {
var f = this.genf;
var result = f(this.c);
if (result === stop) {
return {done: true};
function Context() {
this.next = 0;
}
var stop = {};
Context.prototype.stop = function stop() {
return stop;
}
function fibo2() {
var a, b; // local vars
return wrap(function (c) {
while (1) {
switch (c.next) {
case 0:
a=0, b=1;
case 1: // while begin
if (!true) { // while cond
c.next = 3;
@jooyunghan
jooyunghan / babel-inorder.js
Created December 2, 2016 01:58
Binary inorder traversal using generator (and its babel-transpiled)
var _marked = [inorder].map(regeneratorRuntime.mark);
function inorder(t) {
return regeneratorRuntime.wrap(function inorder$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (t) {
_context2.next = 2;
break;
@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:
@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 / 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 / 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 / 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