Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
-- euler67
-- input file: https://projecteuler.net/project/resources/p067_triangle.txt
readInput = map (map read . words) . lines
solve = foldr1 merge
where merge xs ys = zipWith (+) xs $ zipWith max ys (tail ys)
main = do
content <- readFile "p067_triangle.txt"
@jooyunghan
jooyunghan / SeminarTest.java
Created March 26, 2015 04:06
TotallyLazy exercises with problems on Project Euler
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Strings;
import com.googlecode.totallylazy.numbers.Integers;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static com.googlecode.totallylazy.Pair.functions.first;
import static com.googlecode.totallylazy.Pair.pair;
@jooyunghan
jooyunghan / flappy.html
Created March 30, 2015 08:40
Functional Flappy v1
<!DOCTYPE html>
<html>
<head>
<title>Functional Flappy</title>
</head>
<style type="text/css">
#playground {
width: 500px;
height: 500px;
border: 1px solid red;
@jooyunghan
jooyunghan / minimal fp
Created April 23, 2015 17:41
solve euler project #18
@Test
public void no18_2() throws Exception {
String input = "75\n" +
"95 64\n" +
"17 47 82\n" +
"18 35 87 10\n" +
"20 04 82 47 65\n" +
"19 01 23 75 03 34\n" +
"88 02 77 73 07 63 67\n" +
"99 65 04 28 06 16 70 92\n" +
@jooyunghan
jooyunghan / FunctionalJavaExample.java
Created May 12, 2015 20:51
Some examples using FunctionalJava
package com.jooyunghan;
import fj.P2;
import fj.data.List;
import fj.data.Stream;
import static fj.P.p;
import static fj.Show.*;
import static fj.data.List.list;
import static fj.data.Stream.iterate;
@jooyunghan
jooyunghan / coder.clj
Created September 14, 2015 07:14
Alphabet Cipher
(ns alphabet-cipher.coder)
(defn to-char [n]
(char (+ (int \a) (mod n 26))))
(defn to-int [c]
(- (int c) (int \a)))
(defn m [k s]
(to-char (+ (to-int k)(to-int s))))
import Graphics.Collage exposing (filled, rect, collage, toForm)
import Graphics.Element exposing (image, beside, below, above, empty, Element, spacer, color)
import Graphics.Input exposing (customButton, button)
import Color exposing (red, yellow)
import Signal exposing ((<~))
import Markdown
off_bulb = collage 30 30 [
toForm (image 28 28 "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEBUSEhQQFBUSEhAQFBcQEBARFRQSFBQaFhQVFRQYHCggGBolHRYVITEhJSkrLi4uGR8zODMsNygtLisBCgoKDQcGDg8ODisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIAMwAzAMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAABgIDBAUHAQj/xABDEAABAwIDAwYLBgUDBQAAAAABAAIDBBEFEiEGMUETIlFhcYEHIzJCUnKRobHB0RRzgpKysyQzQ2KiU4PxFlSTwtL/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8A7iiIgIiICIiAiK3UTtjaXvcGtaLkuNgAguLCxLFYYBeWRregE849jRqVC8c22fITHSjK3dyhHOPqt4DrPuWjpcKfK+7sznONze7nHtQSiu8IDRpDC5/90jsg9gBJ9y1M22VY/wAkRs9Vhd7yVtKDY9292Vvrc4+wLbRbMxje5x7A1o+aCHHaGuP9Q9zGfRVM2nrm+eD60bT8F
@jooyunghan
jooyunghan / puzzle.clj
Created September 18, 2015 16:46
fox/goose/corn puzzle from Living Clojure Kata
(ns fox-goose-bag-of-corn.puzzle)
(def start-pos [[[:fox :goose :corn :you] [:boat] []]])
(defn end? [state]
(= state [#{} #{:boat} #{:you :goose :fox :corn}]))
(defn next-moves' [[left boat right]]
(cond (left :you)
(into [[(disj left :you) (conj boat :you) right]] ;; move alone
@jooyunghan
jooyunghan / async.md
Last active October 7, 2015 01:18
async

async 라이브러리로 JavaScript의 콜백헬을 조금이나마 피할 수 있는 모양이다.

뒤늦게 async를 살펴보았다. API들이 잘 이해가 안되었는데, 알고보니 비교적 단순한 원리로 만들어진 것들이었다. sync버전의 대응함수와 비교해보면, 함수 그자체와 iterator 함수들을 모두 async로 바꾼것에 불과하다.

each :: Array[A] -> (A->Unit) -> Unit
async.each :: Array[A] -> (A->(Unit->Unit)->Unit) ->(Unit->Unit)->Unit 

map :: Array[A] -> (A->B) -> Array[B]

node-postgres라이브러리 위키에는 아래의 예제 코드가 나온다. Transaction으로 insertion을 두 번 하는 코드다.

var Client = require('pg').Client;

var client = new Client(/*your connection info goes here*/);
client.connect();

var rollback = function(client) {