Skip to content

Instantly share code, notes, and snippets.

@mvarela
Created February 2, 2020 09:39
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 mvarela/87c8bd7aceef6cb509ff9d4d558b2a38 to your computer and use it in GitHub Desktop.
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
(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