Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@jooyunghan
jooyunghan / ant.clj
Created March 1, 2016 08:36
Look-and-say sequence in Clojure using Lazy sequence
(defn nn [s] (->> s (partition-by identity) (mapcat (juxt count first))))
(defn ant [] (iterate nn [1]))
(-> (ant) (nth 1000000) (nth 1000000))
@jooyunghan
jooyunghan / ant.go
Created March 1, 2016 08:34 — forked from anonymous/ant.go
Look and say sequence in Go using Chan/Goroutine
package main
import "fmt"
type genf func(chan<- uint8)
func gen(f genf) <-chan uint8 {
c := make(chan uint8)
go f(c)
return c
@jooyunghan
jooyunghan / kmean.hs
Last active February 19, 2016 07:06
K-mean for [(d,d)] - slightly improved for readability
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module Lib
(
kMeans
) where
import GHC.Exts (groupWith)
import Data.List (transpose, sort)
import Data.List.Split (chunksOf)
@jooyunghan
jooyunghan / ant.c
Last active February 2, 2016 09:36
C implementation of look and say sequence
//
// main.c
// ant
//
// Created by Jooyung Han on 1/31/16.
// Copyright © 2016 Jooyung Han. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
@jooyunghan
jooyunghan / buffer.c
Last active January 19, 2016 02:44
Using zer-length array for shared implementation for byte buffer
/*
============================================================================
Name : Buffer.c
Author : Jooyung Han
Version :
Copyright :
Description : Hello World in C, Ansi-style
============================================================================
*/
@jooyunghan
jooyunghan / lazy.ts
Last active December 20, 2015 17:54
lazy in 'even' style.
// http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#lazyinstrict
class Cons<A, B> {
head: A;
tail: B;
constructor(head: A, tail: B) {
this.head = head;
this.tail = tail;
}
}
function cons<A, B>(head: A, tail: B): Cons<A, B> {
@jooyunghan
jooyunghan / ant.js
Created December 15, 2015 18:20
Basic lazy stream in JS
/* base functions */
function apply(f,args) {
return f.apply(undefined, args);
}
var slice = Function.prototype.call.bind(Array.prototype.slice);
/* Stream */
@jooyunghan
jooyunghan / treeparsing.js
Created December 15, 2015 08:55
Tree Parsing with Peg.js
var PEG = require('pegjs');
var parser = PEG.buildParser(`
tree = fork?
fork = name:[a-z] c:children? { return Object.assign({name}, c); }
children = '(' left:tree ',' right:tree ')' {return {left,right}}
`);
var tree = parser.parse('a(b,c(,d))');
console.log(tree);
@jooyunghan
jooyunghan / async.js
Created December 6, 2015 23:37
Async Exercises
/*
Using following two API functions
- deletePage(pageId, callback) : delete a page. if the page has sub-pages then it fails.
- getChildPages(pageId, callback) : get a list of ids of direct child pages.
write deleteTree(pageId, callback) function which
deletes the pages of pageId and sub-pages.
@jooyunghan
jooyunghan / README.md
Last active November 19, 2015 21:20
Sum the even values in Fibonacci series which are less than 4000000
> npm install babel-preset-es2015
> npm install babel-polyfill
> babel-node --presets "es2015" evenfib.js
4613732