Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
esbudylin committed Oct 8, 2024
1 parent 18da31c commit 2675672
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
19 changes: 9 additions & 10 deletions modest/chord.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(require :modest.basics))

(local {: map : flatten-nested : sort : apply : safe-cons
: index-by : vals : remove-keys : copy : conj}
: index-by : vals : remove-keys : copy : conj : reduce}
(require :modest.utils))

(fn build-triad [{: triad}]
Expand Down Expand Up @@ -38,7 +38,7 @@
(if alterations
(let [alt-map (collect [_ [acc interval-size] (ipairs alterations)]
(values interval-size acc))
interval-map (index-by intervals #(. $1 :size))]
interval-map (index-by intervals #(. $ :size))]
(each [size alt (pairs alt-map)]
(let [{: quality} (or (. interval-map size) {})]
(tset interval-map size (Interval.new size (+ (or quality 0) alt)))))
Expand All @@ -51,7 +51,7 @@

(fn transpose [v root]
(let [inter (semitone-interval (Note.new :C) root)]
(map #(+ inter $1) v)))
(map #(+ inter $) v)))

(fn root [] [1])

Expand Down Expand Up @@ -79,7 +79,7 @@
(let [alterations (-> (or alterations [])
(copy)
(conj (when (= triad :half-dim) [-1 5]))
(sort #(. $1 2)))
(sort #(. $ 2)))
alteration-string (accumulate [res ""
_ [acc interval-size] (ipairs alterations)]
(.. res (accidental-to-string acc ascii) interval-size))]
Expand All @@ -94,10 +94,9 @@
;; transforms a parsed chord suffix (e.g. mM7, aug, dim7) into a string
(fn suffix-to-string [suffix ascii]
(let [foos [quality-to-string ext-to-string
add-to-string #(alterations-to-string $1 ascii)]
strings (map #($1 suffix) foos)]
(accumulate [res "" _ s (ipairs strings)]
(.. res s))))
add-to-string #(alterations-to-string $ ascii)]
strings (map #($ suffix) foos)]
(reduce #(.. $ $2) strings "")))

(local Chord {})

Expand All @@ -124,7 +123,7 @@
(chord-transpose-util self interval -1))

(fn Chord.tostring [{: root : bass : suffix} ascii]
(local str-func #(Note.tostring $1 ascii))
(local str-func #(Note.tostring $ ascii))
(.. (str-func root)
(suffix-to-string suffix ascii)
(if bass (.. "/" (str-func bass)) "")))
Expand All @@ -134,7 +133,7 @@
(fn Chord.transform [t]
(let [foos [root build-triad add-7 extend added]
intervals (map (partial apply Interval.new)
(flatten-nested (map #($1 t) foos)))
(flatten-nested (map #($ t) foos)))
alterated (sort (alterate intervals t) Interval.semitones)
chord {:intervals alterated
:bass t.bass
Expand Down
12 changes: 5 additions & 7 deletions modest/utils.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
(= (type v) :table))

(fn find [coll el acc]
(let-default
[acc 1]
(let-default [acc 1]
(if (empty? coll) nil
(= (car coll) el) acc
(find (cdr coll) el (+ acc 1)))))
Expand All @@ -71,12 +70,11 @@
(not= (find coll el) nil))

(fn sort [coll comp]
(table.sort coll (when comp #(< (comp $1) (comp $2))))
(table.sort coll (when comp #(< (comp $) (comp $2))))
coll)

(fn flatten [v fcond acc]
(let-default
[acc [] fcond (fn [] true)]
(let-default [acc [] fcond (fn [] true)]
(if (empty? v) acc
(and (table? (car v)) (fcond (car v)))
(flatten (cdr v)
Expand All @@ -87,10 +85,10 @@
(conj acc (car v))))))

(fn nested? [v]
(not (empty? (filter #(= (type $1) :table) v))))
(not (empty? (filter table? v))))

(fn flatten-nested [lol]
(flatten lol #(nested? $1)))
(flatten lol nested?))

(fn circular [t]
(local n (length t))
Expand Down

0 comments on commit 2675672

Please sign in to comment.