Skip to content

Instantly share code, notes, and snippets.

@joelittlejohn
Created July 31, 2013 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelittlejohn/6125072 to your computer and use it in GitHub Desktop.
Save joelittlejohn/6125072 to your computer and use it in GitHub Desktop.
Merge two ascii (art) files like image layers, treating spaces as transparent.
(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