Created
February 2, 2020 09:39
-
-
Save mvarela/87c8bd7aceef6cb509ff9d4d558b2a38 to your computer and use it in GitHub Desktop.
A solution for the "Ibid." problem proposed by Gene Kim here: https://twitter.com/RealGeneKim/status/1201922587346866176?ref_src=twsrc%5Etfw. We avoid explicit recursion
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
(defn replace-ibids [coll] | |
(letfn [(go [{:keys [acc latest]} e] | |
(if (and (= e "Ibid.") | |
(some? latest)) | |
{:acc (conj acc latest) :latest latest} | |
{:acc (conj acc e) :latest e}))] | |
(if (= "Ibid." (first coll)) | |
(ex-info "Invalid ibid" {}) | |
(-> (reduce go {:acc []} coll) | |
:acc)))) | |
(def authors ["toto" "Ibid." "tato" "Ibid." "Ibid." "tito" "Ibid."]) | |
(replace-ibids authors) | |
;; => ["toto" "toto" "tato" "tato" "tato" "tito" "tito"] | |
(replace-ibids []) | |
;; => [] | |
(replace-ibids ["toto"]) | |
;; => ["toto"] | |
(replace-ibids ["Ibid."]) | |
;; => #error { | |
;; :cause "Invalid ibid" | |
;; :data {} | |
;; ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment