Skip to content

Instantly share code, notes, and snippets.

@ftrain
Created December 15, 2013 20:25
Show Gist options
  • Save ftrain/7977681 to your computer and use it in GitHub Desktop.
Save ftrain/7977681 to your computer and use it in GitHub Desktop.
List to structured list scratchpade
;; I have
(def events ["== A ==" "=== A.1 ===" "* One" "* Two" "** Two.A" "** Two.B" "== B =="])
;; (i.e. wikipedia markup) And what I want is:
;; (("== A =="
;; ("=== A.1 ==="
;; ("* One"
;; "* Two"
;; ("** Two.A"
;; "** Two.B"))))
;; ("== B =="))
;; Or possibly even
;; (("A"
;; ("A.1"
;; ("One"
;; "Two"
;; ("Two.A"
;; "Two.B"))))
;; ("B"))
;; So I'm trying to figure it out using partition-by, and my general strategy is
;; do a partition-by using a regexp
;; and then take the results of the partition
;; and partition-by them further until they can't be partition-byed any more
;; and that can work, I'm pretty sure but I keep getting lost
;; doing lots of first/nth calls to the structures
;; because I don't really understand where I am when I'm calling
;; the part function recursively
(defn part [events number-of-equal-signs]
(if (and (> number-of-equal-signs 0)
(or (vector? events) (list? events)))
(partition-by
#(re-find (re-pattern (str "^={" number-of-equal-signs "}[^=]")) %) events)))
;; When I evaluate
(part events 2)
;; What I get is:
; (("== A ==") ("=== A.1 ===" "* One" "* Two" "** Two.A" "** Two.B") ("== B =="))
;; But when I keep running (part) on the ensuing structure I keep getting confused as to whether I'm in a vector, map, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment