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

Add mapNotNullish to collections #1103

Merged
merged 4 commits into from
Aug 7, 2021
Merged

Add mapNotNullish to collections #1103

merged 4 commits into from
Aug 7, 2021

Conversation

LionC
Copy link
Contributor

@LionC LionC commented Aug 5, 2021

This adds mapNotNullish<T, O>(array: Array<T>, transformer: (el: T) => O): Array<NonNullable<O>> to collections. This method helps in common cases like extracting an optional property from objects or mapping and filtering an array at the same time:

Example 1:

const processes = [
    { name: "Building", problem: null },
    { name: "Testing", problem: "Could not load test file" },
    { name: "Benchmarking", problem: "OutOfMemory" },
    { name: "Deploying", problem: undefined },
]

const problems = mapNotNullish(processes, it => it.problem)
// [ "Could not load test file", "OutOfMemory" ]

Example 2:

const numbers = [ 1, 2, 3, 4 ]

const evensSquared = mapNotNullish(numbers, it => it % 2 === 0 ? it*it : null)
// [4, 16]

mapNotNullish(someArray, someFunction) has the same result as

someArray
    .map(someFunction)
    .filter((it) => it !== undefined && it !== null)

but is more efficient regarding runtime and memory (only one iteration, no intermediate array etc), states the intent shorter and more specifically and gets the types right (which filter does not without explicit type guards).

Like a lot of the collections functions, mapNotNullish() is heavily inspired by kotlins .mapNotNull().

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

@LionC LGTM.

As explained in the description, this is optimized version of .map().filter() idiom. I think this has decent use cases, especially when the source array is large.

*
* Example:
*
* ```typescript
Copy link
Member

Choose a reason for hiding this comment

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

We prefer ts tag, but let's address this later.

Copy link
Contributor

Choose a reason for hiding this comment

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

Won't be typechecked, but I'll address that upstream in cli.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry - I keep forgetting that. Should I post a separate PR?

@kt3k kt3k merged commit fb97034 into denoland:main Aug 7, 2021
@kt3k kt3k mentioned this pull request Aug 7, 2021
26 tasks
@LionC LionC deleted the map-not-nullish branch August 7, 2021 09:47
@LionC LionC restored the map-not-nullish branch August 21, 2021 16:10
@LionC LionC deleted the map-not-nullish branch August 23, 2021 10:54
@LionC LionC mentioned this pull request Aug 27, 2021
44 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants