Created
July 31, 2013 19:02
-
-
Save joelittlejohn/6125072 to your computer and use it in GitHub Desktop.
Merge two ascii (art) files like image layers, treating spaces as transparent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns merge-ascii | |
(:require [clojure.java.io :refer [reader writer]])) | |
(defn choose-char [a b] | |
(cond (nil? a) b | |
(nil? b) a | |
(not= b \space) b | |
:else a)) | |
(defn merge [a b out] | |
(with-open [read-a (reader a) | |
read-b (reader b) | |
w (writer out)] | |
(let [lines-a (line-seq read-a) | |
lines-b (line-seq read-b)] | |
(loop [[al & als] lines-a | |
[bl & bls] lines-b] | |
(when (or al bl) | |
(loop [[ac & acs] al | |
[bc & bcs] bl] | |
(when (or ac bc) | |
(.write w (str (choose-char ac bc))) | |
(recur acs bcs))) | |
(.newLine w) | |
(recur als bls)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment