Published
Edited
May 8, 2019
Importers
Insert cell
md`# auto-fp
import into your notebook with:
\`\`\`lang-javascript
import {map, reduce, pipe} from '@ada-lovecraft/auto-fp'
\`\`\`
`

Insert cell
min = Math.min
Insert cell
max = Math.max
Insert cell
curry = (arity, fn) => {
const receiver = oldArgs => (...newArgs) => {
const args = [...oldArgs, ...newArgs]
if (args.length >= arity) {
return fn(...args)
}
return receiver(args)
}
return receiver([])
}
Insert cell

autocurry = fn => curry(fn.length, fn)
Insert cell
log = (...args) => console.log(...args)
Insert cell
identity = d => d

Insert cell
pipe = (...fns) => data =>
fns.reduce((value, fn) => fn(value), data)

Insert cell
split = autocurry((delimiter, string) => string.split(delimiter))
Insert cell

join = autocurry((relimiter, arr) => arr.join(relimiter))
Insert cell

trace = autocurry((label, data) => {
log(`**************${label}**************`)
log(data)
log(
`**************${pipe(
split(/./g),
map(a => "*"),
join("")
)(label)}**************`
)
return data
})
Insert cell

map = autocurry((fn, collection) => collection.map(fn))
Insert cell

keys = obj => Object.keys(obj)
Insert cell

values = obj => Object.values(obj)
Insert cell


includedIn = autocurry((arr, d) => arr.includes(d))
Insert cell

includes = autocurry((d, arr) => includedIn(arr, d))
Insert cell

pick = autocurry((key, obj) => obj[key])
Insert cell

pickAll = key => map(pick(key))
Insert cell
Insert cell
tail = collection => {
const [head, ...tail] = collection
return tail
}
Insert cell
sum = collection =>
collection.reduce((total, data) => total + Number(data), 0)
Insert cell
sumBy = autocurry((key, collection) => {
return pipe(
pickAll(key),
sum
)(collection)
})
Insert cell
sort = collection => [...collection].sort()
Insert cell
sortBy = autocurry((key, collection) =>
[...collection].sort((a, b) => a[key] - b[key])
)
Insert cell
reverse = collection => [...collection].reverse()
Insert cell

find = autocurry((fn, collection) => collection.find(fn))
Insert cell

findBy = autocurry((key, value) => find(i => i[key] === value))
Insert cell

filter = autocurry((fn, collection) => collection.filter(fn))
Insert cell

filterBy = autocurry((key, value) => filter(i => i[key] === value))
Insert cell

compress = filter(i => i !== null)
Insert cell
excludeBy = autocurry((key, value) =>
filter(i => i[key] !== value)
)
Insert cell
count = collection => collection.length
Insert cell
uniq = collection =>
collection.reduce(
(uniq, data) => (includedIn(uniq)(data) ? uniq : [...uniq, data]),
[]
)
Insert cell
uniqBy = autocurry((key, collection) => {
collection.reduce((agg, record) => {
if (
pipe(
pickAll(key),
includes(record[key])
)(agg)
) {
return agg
}
return [...agg, record]
}, [])
})
Insert cell
uniqValuesBy = key =>
pipe(
pickAll(key),
uniq
)
Insert cell
uniqCount = key =>
pipe(
uniqValuesBy(key),
count
)
Insert cell
replace = autocurry((needle, replacement, str) => str.replace(needle, replacement))
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more