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

flat/flatMap not available #7397

Closed
gajus opened this issue Jan 26, 2019 · 8 comments
Closed

flat/flatMap not available #7397

gajus opened this issue Jan 26, 2019 · 8 comments
Labels
enhancement Library definitions Issues or pull requests about core library definitions

Comments

@gajus
Copy link

gajus commented Jan 26, 2019

There is currently no open issue describing the lack of flatMap.

As this is a stage 3 proposal, it should be supported by Flow.

@lll000111
Copy link
Contributor

Reopened #6602 which has more info.

@gajus
Copy link
Author

gajus commented Jan 26, 2019

Reopened #6602 which has more info.

#6602 asks how to implement such types in user-land instead of raising the issue that types for Array#flat/ Arrray#flatMap are missing in the standard Flow type definitions. The two are related, but different issues.

@lll000111
Copy link
Contributor

lll000111 commented Jan 26, 2019

They are not different, Flow types are Flow types. It's really obvious that it should be in the Flow lib since the question is for an official JS function (the linked issue even links to the standard — well, to the proposal, it isn't in https://tc39.github.io/ecma262/ yet, it still is stage 3). The linked issue has a suggestion how to actually do it (better than any I'd say?):

declare export function flatten<T, X>(array?: ?Array<Array<T> | X>): Array<T | X>;

Anyway, I only linked it for those suggestions, they can be used as-is exactly, and yes I mean putting them into the Flow lib definitions.

PS: For a long moment I confused this with the far, far more onerous Flow type for extracting function arguments which covers almost ten lines of Flow definition for one thing.... but if the above line does not work for everybody that's probably the route for flatMap too, describing several levels and concatenating them with &

@jbrown215 jbrown215 added Library definitions Issues or pull requests about core library definitions enhancement labels Jan 28, 2019
@wchargin
Copy link
Contributor

See #6948 for an attempt at this.

facebook-github-bot pushed a commit that referenced this issue Jul 2, 2019
Summary:
<!--
  If this is a change to library defintions, please include links to relevant documentation.
  If this is a documentation change, please prefix the title with [DOCS].

  If this is neither, ensure you opened a discussion issue and link it in the PR description.
-->

Fixes part of #7397
Pull Request resolved: #7854

Reviewed By: dsainati1

Differential Revision: D16091488

Pulled By: nmote

fbshipit-source-id: 72af9dd2d13f604cae58135e0869712f2b20abfe
@amiralies
Copy link

anyone progress on flat?

@amiralies
Copy link

amiralies commented Aug 22, 2019

as a workaroung im using identity function as mapper in flatMap

const nums: Array<number> = [[1], [2], [3]].flatMap(x =>x);

@gkz
Copy link
Member

gkz commented Jun 1, 2020

I added support for common patterns (that are possible to type) for .flat:

https://flow.org/try/#0MYewdgzgLgBGCuBbCAuGBBATpghgTwB4FEAjAU0wD4YBeGAbXoEYBdAGgYCZ2GBmFlgDoAZgBscUABQBKANxA

@gkz gkz closed this as completed Jun 1, 2020
@callumgare
Copy link

This doesn't seem to deal with arrays of different levels of nesting https://flow.org/try/#0MYewdgzgLgBGCuBbCAuGBBATpghgTwB4FEAjAU0wD4YBeGAbXoEYBdAGgYCZ2GBmHgCwsAdADMANjigAKAJQBuIA

That seems like a bit of a major issue to me since a key use case of .flat() is to take input with some varied level of array nesting and flatten it out so all elements are consistent. It's the very first example given in the MDN entry https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Library definitions Issues or pull requests about core library definitions
Projects
None yet
Development

No branches or pull requests

7 participants