Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More performant compact map #344

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.69.1
- More performant version of `compactMap`.

# 1.69.0
- Add `isPromise` utility

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "futil-js",
"version": "1.69.0",
"version": "1.69.1",
"description": "F(unctional) util(ities). Resistance is futile.",
"main": "lib/futil-js.js",
"scripts": {
Expand Down Expand Up @@ -41,8 +41,7 @@
"trailingComma": "es5"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.22.2",
"babel-cli": "^6.26.0",
warrenv marked this conversation as resolved.
Show resolved Hide resolved
"babel-core": "^6.22.1",
"babel-eslint": "^8.0.0",
"babel-loader": "^7.0.0",
Expand Down
17 changes: 14 additions & 3 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export let insertAtIndex = _.curry((index, val, collection) =>
: insertAtArrayIndex(index, val, collection)
)

export let compactMap = _.curry((fn, collection) =>
_.flow(_.map(fn), _.compact)(collection)
)
export let compactMap = _.curry((iteratee, collection) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a quick benchmark? Lodash supports shortcut fusion which should be effectively the same thing. My first feedback was gonna be to do this with transducers https://runkit.com/daedalus28/transducer-example but lodash does this automatically in some cases - might only be only explicit chain calls though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fusion is only supported in the chain scenario. https://lodash.com/docs/4.17.15#lodash

let fn = _.iteratee(iteratee)
return _.reduce(
(acc, x) => {
let v = fn(x)
if (v) {
acc.push(v)
}
return acc
},
[],
collection
)
})