tidy - use const or let for declarations

This commit is contained in:
alex 2022-02-08 12:36:48 +00:00
parent c4a8180592
commit 44336efb8d

View File

@ -1,6 +1,6 @@
import Fraction from 'fraction.js' import Fraction from 'fraction.js'
var removeUndefineds = function(xs) { const removeUndefineds = function(xs) {
// Removes 'None' values from given list // Removes 'None' values from given list
return xs.filter(x => x != undefined) return xs.filter(x => x != undefined)
} }
@ -9,7 +9,7 @@ function flatten(arr) {
return [].concat(...arr) return [].concat(...arr)
} }
var id = a => a const id = a => a
function curry(func) { function curry(func) {
return function curried(...args) { return function curried(...args) {
@ -82,10 +82,10 @@ class TimeSpan {
} }
get spanCycles() { get spanCycles() {
var spans = [] const spans = []
var begin = this.begin var begin = this.begin
var end = this.end const end = this.end
var end_sam = end.sam() const end_sam = end.sam()
while (end.gt(begin)) { while (end.gt(begin)) {
// If begin and end are in the same cycle, we're done. // If begin and end are in the same cycle, we're done.
@ -94,7 +94,7 @@ class TimeSpan {
break break
} }
// add a timespan up to the next sam // add a timespan up to the next sam
var next_begin = begin.nextSam() const next_begin = begin.nextSam()
spans.push(new TimeSpan(begin, next_begin)) spans.push(new TimeSpan(begin, next_begin))
// continue with the next cycle // continue with the next cycle
@ -110,8 +110,8 @@ class TimeSpan {
intersection(other) { intersection(other) {
// Intersection of two timespans, returns None if they don't intersect. // Intersection of two timespans, returns None if they don't intersect.
var intersect_begin = this.begin.max(other.begin) const intersect_begin = this.begin.max(other.begin)
var intersect_end = this.end.min(other.end) const intersect_end = this.end.min(other.end)
if (intersect_begin.gt(intersect_end)) { if (intersect_begin.gt(intersect_end)) {
return(undefined) return(undefined)
@ -131,7 +131,7 @@ class TimeSpan {
intersection_e(other) { intersection_e(other) {
// Like 'sect', but raises an exception if the timespans don't intersect. // Like 'sect', but raises an exception if the timespans don't intersect.
var result = this.intersection(other) const result = this.intersection(other)
if (result == undefined) { if (result == undefined) {
// TODO - raise exception // TODO - raise exception
// raise ValueError(f'TimeSpan {self} and TimeSpan {other} do not intersect') // raise ValueError(f'TimeSpan {self} and TimeSpan {other} do not intersect')
@ -173,7 +173,7 @@ class Hap {
withSpan(func) { withSpan(func) {
// Returns a new event with the function f applies to the event timespan. // Returns a new event with the function f applies to the event timespan.
var whole = this.whole ? func(this.whole) : undefined const whole = this.whole ? func(this.whole) : undefined
return new Hap(whole, func(this.part), this.value) return new Hap(whole, func(this.part), this.value)
} }
@ -216,10 +216,8 @@ class Pattern {
// Splits queries at cycle boundaries. This makes some calculations // Splits queries at cycle boundaries. This makes some calculations
// easier to express, as all events are then constrained to happen within // easier to express, as all events are then constrained to happen within
// a cycle. // a cycle.
var pat = this const pat = this
var q = function(span) { const q = span => flatten(span.spanCycles.map(subspan => pat.query(subspan)))
return flatten(span.spanCycles.map(subspan => pat.query(subspan)))
}
return new Pattern(q) return new Pattern(q)
} }
@ -280,12 +278,12 @@ class Pattern {
// Assumes 'this' is a pattern of functions, and given a function to // Assumes 'this' is a pattern of functions, and given a function to
// resolve wholes, applies a given pattern of values to that // resolve wholes, applies a given pattern of values to that
// pattern of functions. // pattern of functions.
var pat_func = this const pat_func = this
query = function(span) { query = function(span) {
var event_funcs = pat_func.query(span) const event_funcs = pat_func.query(span)
var event_vals = pat_val.query(span) const event_vals = pat_val.query(span)
apply = function(event_func, event_val) { apply = function(event_func, event_val) {
var s = event_func.part.intersection(event_val.part) const s = event_func.part.intersection(event_val.part)
if (s == undefined) { if (s == undefined) {
return undefined return undefined
} }
@ -298,7 +296,7 @@ class Pattern {
appBoth(pat_val) { appBoth(pat_val) {
// Tidal's <*> // Tidal's <*>
var whole_func = function(span_a, span_b) { const whole_func = function(span_a, span_b) {
if (span_a == undefined || span_B == undefined) { if (span_a == undefined || span_B == undefined) {
return undefined return undefined
} }
@ -308,17 +306,17 @@ class Pattern {
} }
appLeft(pat_val) { appLeft(pat_val) {
var pat_func = this const pat_func = this
var query = function(span) { const query = function(span) {
var haps = [] const haps = []
for (var hap_func of pat_func.query(span)) { for (const hap_func of pat_func.query(span)) {
var event_vals = pat_val.query(hap_func.part) const event_vals = pat_val.query(hap_func.part)
for (var hap_val of event_vals) { for (const hap_val of event_vals) {
var new_whole = hap_func.whole const new_whole = hap_func.whole
var new_part = hap_func.part.intersection_e(hap_val.part) const new_part = hap_func.part.intersection_e(hap_val.part)
var new_value = hap_func.value(hap_val.value) const new_value = hap_func.value(hap_val.value)
var hap = new Hap(new_whole, new_part, new_value) const hap = new Hap(new_whole, new_part, new_value)
haps.push(hap) haps.push(hap)
} }
} }
@ -328,17 +326,17 @@ class Pattern {
} }
appRight(pat_val) { appRight(pat_val) {
var pat_func = this const pat_func = this
var query = function(span) { const query = function(span) {
var haps = [] let haps = []
for (var hap_val of pat_val.query(span)) { for (const hap_val of pat_val.query(span)) {
var hap_funcs = pat_func.query(hap_val.part) const hap_funcs = pat_func.query(hap_val.part)
for (var hap_func of hap_funcs) { for (const hap_func of hap_funcs) {
var new_whole = hap_val.whole const new_whole = hap_val.whole
var new_part = hap_func.part.intersection_e(hap_val.part) const new_part = hap_func.part.intersection_e(hap_val.part)
var new_value = hap_func.value(hap_val.value) const new_value = hap_func.value(hap_val.value)
var hap = new Hap(new_whole, new_part, new_value) const hap = new Hap(new_whole, new_part, new_value)
haps.push(hap) haps.push(hap)
} }
} }
@ -372,14 +370,14 @@ class Pattern {
} }
_bindWhole(choose_whole, func) { _bindWhole(choose_whole, func) {
var pat_val = this const pat_val = this
var query = function(span) { const query = function(span) {
var withWhole = function(a, b) { const withWhole = function(a, b) {
return new Hap(choose_whole(a.whole, b.whole), b.part, return new Hap(choose_whole(a.whole, b.whole), b.part,
b.value b.value
) )
} }
var match = function (a) { const match = function (a) {
return func(a.value).query(a.part).map(b => withWhole(a, b)) return func(a.value).query(a.part).map(b => withWhole(a, b))
} }
return flatten(pat_val.query(span).map(a => match(a))) return flatten(pat_val.query(span).map(a => match(a)))
@ -388,7 +386,7 @@ class Pattern {
} }
bind(func) { bind(func) {
var whole_func = function(a, b) { const whole_func = function(a, b) {
if (a == undefined || b == undefined) { if (a == undefined || b == undefined) {
return undefined return undefined
} }
@ -433,7 +431,7 @@ class Pattern {
} }
_fast(factor) { _fast(factor) {
var fastQuery = this.withQueryTime(t => t.mul(factor)) const fastQuery = this.withQueryTime(t => t.mul(factor))
return fastQuery.withEventTime(t => t.div(factor)) return fastQuery.withEventTime(t => t.div(factor))
} }
@ -471,10 +469,10 @@ class Pattern {
when(binary_pat, func) { when(binary_pat, func) {
//binary_pat = sequence(binary_pat) //binary_pat = sequence(binary_pat)
var true_pat = binary_pat._filterValues(id) const true_pat = binary_pat._filterValues(id)
var false_pat = binary_pat._filterValues(val => !val) const false_pat = binary_pat._filterValues(val => !val)
var with_pat = true_pat.fmap(_ => y => y).appRight(func(this)) const with_pat = true_pat.fmap(_ => y => y).appRight(func(this))
var without_pat = false_pat.fmap(_ => y => y).appRight(this) const without_pat = false_pat.fmap(_ => y => y).appRight(this)
return stack(with_pat, without_pat) return stack(with_pat, without_pat)
} }
@ -483,7 +481,7 @@ class Pattern {
} }
every(n, func) { every(n, func) {
var pats = Array(n-1).fill(this) const pats = Array(n-1).fill(this)
pats.unshift(func(this)) pats.unshift(func(this))
return slowcat(...pats) return slowcat(...pats)
} }
@ -493,19 +491,19 @@ class Pattern {
} }
rev() { rev() {
var pat = this const pat = this
var query = function(span) { const query = function(span) {
var cycle = span.begin.sam() const cycle = span.begin.sam()
var next_cycle = span.begin.nextSam() const next_cycle = span.begin.nextSam()
var reflect = function(to_reflect) { const reflect = function(to_reflect) {
var reflected = to_reflect.withTime(time => cycle.add(next_cycle.sub(time))) const reflected = to_reflect.withTime(time => cycle.add(next_cycle.sub(time)))
// [reflected.begin, reflected.end] = [reflected.end, reflected.begin] -- didn't work // [reflected.begin, reflected.end] = [reflected.end, reflected.begin] -- didn't work
var tmp = reflected.begin const tmp = reflected.begin
reflected.begin = reflected.end reflected.begin = reflected.end
reflected.end = tmp reflected.end = tmp
return reflected return reflected
} }
var haps = pat.query(reflect(span)) const haps = pat.query(reflect(span))
return haps.map(hap => hap.withSpan(reflect)) return haps.map(hap => hap.withSpan(reflect))
} }
return new Pattern(query)._splitQueries() return new Pattern(query)._splitQueries()
@ -513,14 +511,14 @@ class Pattern {
jux(func, by=1) { jux(func, by=1) {
by /= 2 by /= 2
var elem_or = function(dict, key, dflt) { const elem_or = function(dict, key, dflt) {
if (key in dict) { if (key in dict) {
return dict[key] return dict[key]
} }
return dflt return dflt
} }
var left = this.withValue(val => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) - by})) const left = this.withValue(val => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) - by}))
var right = this.withValue(val => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) + by})) const right = this.withValue(val => Object.assign({}, val, {pan: elem_or(val, "pan", 0.5) + by}))
return stack([left,func(right)]) return stack([left,func(right)])
} }
@ -548,9 +546,9 @@ function reify(thing) {
} }
function stack(...pats) { function stack(...pats) {
var pats = pats.map(pat => reify(pat)) const reified = pats.map(pat => reify(pat))
var query = function(span) { const query = function(span) {
return flatten(pats.map(pat => pat.query(span))) return flatten(reified.map(pat => pat.query(span)))
} }
return new Pattern(query) return new Pattern(query)
} }
@ -599,15 +597,15 @@ function sequence(...xs) {
} }
function polymeter(steps=0, ...args) { function polymeter(steps=0, ...args) {
var seqs = args.map(a => _sequenceCount(a)) const seqs = args.map(a => _sequenceCount(a))
if (seqs.length == 0) { if (seqs.length == 0) {
return silence return silence
} }
if (steps == 0) { if (steps == 0) {
steps = seqs[0][1] steps = seqs[0][1]
} }
var pats = [] const pats = []
for (var seq of seqs) { for (const seq of seqs) {
if (seq[1] == 0) { if (seq[1] == 0) {
next next
} }
@ -627,7 +625,7 @@ function pm(args) {
} }
function polyrhythm(...xs) { function polyrhythm(...xs) {
var seqs = xs.map(a => sequence(a)) const seqs = xs.map(a => sequence(a))
if (seqs.length == 0) { if (seqs.length == 0) {
return silence return silence