해커랭크 사이트의 문제 중 하나 The Tree of Life 를 풀다가 나온 입력 처리입니다.
위의 문제는 이진 트리가 LISP의 S-expression 형식으로 입력됩니다.
이진 트리의 각 노드 값은 . 이나 X 이고,
브랜치는 괄호로 묶어 왼쪽/자신/오른쪽 순으로 표시됩니다.
예를 들어(X . (. X .))는 아래와 같은 트리를 나타냅니다.
| (defn throttle-first [duration keyF] | |
| (operator* | |
| (fn [s] | |
| (let [cache (atom #{})] | |
| (subscriber | |
| s | |
| (fn [s v] | |
| (let [k (keyF v)] | |
| (when-not (@cache k) | |
| (on-next s v) |
| {-# LANGUAGE FlexibleContexts #-} | |
| import Data.Array.IO | |
| import Data.IORef | |
| import Control.Monad (when, forM_) | |
| import Control.Applicative ((<$>)) | |
| readInt :: String -> Int | |
| readInt = read | |
| quicksort (l,r) arr = when (l < r) $ do |
| import Text.Parsec (parse, char, (<|>)) | |
| import Text.Parsec.Char (spaces) | |
| import Text.Parsec.Language (haskell) | |
| import Text.Parsec.String (Parser) | |
| import Text.Parsec.Token (makeTokenParser, integer, parens, whiteSpace, identifier) | |
| data Tree a = Leaf a | Branch (Tree a) a (Tree a) deriving (Show) | |
| int :: Parser Int | |
| int = integer haskell >>= return.fromInteger |
해커랭크 사이트의 문제 중 하나 The Tree of Life 를 풀다가 나온 입력 처리입니다.
위의 문제는 이진 트리가 LISP의 S-expression 형식으로 입력됩니다.
이진 트리의 각 노드 값은 . 이나 X 이고,
브랜치는 괄호로 묶어 왼쪽/자신/오른쪽 순으로 표시됩니다.
예를 들어(X . (. X .))는 아래와 같은 트리를 나타냅니다.
| class Confluence { | |
| constructor(base, username, password) { | |
| this.base = base; | |
| this.username = username; | |
| this.password = password; | |
| } | |
| async getChanges() { | |
| let results = []; | |
| let url = this.base + '/rest/api/content/search?cql=lastModified>now("-1m")'; |
node-postgres라이브러리 위키에는 아래의 예제 코드가 나온다. Transaction으로 insertion을 두 번 하는 코드다.
var Client = require('pg').Client;
var client = new Client(/*your connection info goes here*/);
client.connect();
var rollback = function(client) {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]
| (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 |
| 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 |
| (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)))) |