Skip to content

Instantly share code, notes, and snippets.

View ftrain's full-sized avatar

Paul Ford ftrain

View GitHub Profile
@ftrain
ftrain / list->structuredlist.clj
Created December 15, 2013 20:25
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"
#!/bin/bash
#
# _ _ __
# __ _ __ _ _ __| |__ (_)/ _|_ _
# / _` |/ _` | '__| '_ \| | |_| | | |
#| (_| | (_| | | | |_) | | _| |_| |
# \__, |\__,_|_| |_.__/|_|_| \__, |
# |___/ |___/
#
#
;; simplest fold definition
(define (fold f init l)
(if (null? l) init (fold f (f init (car l)) (cdr l))))
;; by this time you are almost certainly comfortable with signatures like:
;; Int -> Int
(fold + 0 '[1 2 3 4 5])
;; => 15
(def date-parsers [nil (java.text.SimpleDateFormat. "yyyy") (java.text.SimpleDateFormat. "yyyy-M") (java.text.SimpleDateFormat. "yyyy-M-dd")])
(def grains [:decade :year :month :day]) ;; what's decade doing?
(map #(let [[_ year month day] (re-find #"(\d{2,4})[-_]([\dx]{2})[-_]([\dx]{2})" %)
date (first (split-with (partial not= "xx") [(if (= (count year) 2) (str "19" year) year) month day]))]
[(.parse (parsers (count date)) (clojure.string/join "-" date)) (grains (count date))])
["thefilehasaname__1939-xx-01.mp3"
"thefilehasaname__39_01_01.mp3"
"thefilehasaname__39-01-01.mp3"