speculative nonfiction
Language ergonomic studies: Summing ranges and most frequent terms
Pennies per day (by day count) in Clojure:
(defn pennyPerDay [numDays]
(reduce + (range 1 (+ numDays 1)))
)
(pennyPerDay 30)
Pennies per day (by day count) in JS:
function pennyPerDay(numDays) {
return Array(++numDays)
.fill(0)
.reduce((sum, _, i) => sum + i, 0);
}
pennyPerDay(30);
Most frequent terms in JS:
const termList = ['term1', 'term2', 'term3', 'term3'];
function mostFrequentTerms(arr) {
return arr.reduce((result, curr) => {
result[curr] = ++result[curr] || 1
return result;
}, {})
}
mostFrequentTerms(termList);
Most frequent terms in a list with Clojure:
(def term-list [:term1 :term2 :term3 :term3])
(defn most-freq-terms \[terms\]
(->> terms
frequencies
(sort-by val)
reverse))
(most-freq-terms term-list)
Monday November 9, 2020