Skip to content

toMap should take a mapping function #600

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

Closed
Daniel15 opened this issue Aug 25, 2015 · 4 comments
Closed

toMap should take a mapping function #600

Daniel15 opened this issue Aug 25, 2015 · 4 comments

Comments

@Daniel15
Copy link

It would be nice if toMap accepted a mapping function to specify what the values should be:

var input = Immutable.List(['a', 's', 'd', 'f']);
var output = input.toMap(x => x.toUpperCase() + '!'); 
// output = Map { 'a': 'A!', 's': 'S!', 'd': 'D!', 'f': 'F!' }

You could do input.toMap().map(x => x.toUpperCase() + '!') at the moment but it's not as nice 😜

.NET LINQ has similar functionality, except it uses the function to specify the key rather than the value: https://msdn.microsoft.com/en-us/library/bb549277(v=vs.95).aspx

@ddaza
Copy link

ddaza commented Aug 25, 2015

The current functionality is done by design, per the documentation of toMap():

Note: This is equivalent to Map(this.toKeyedSeq()), but provided for convenience and to allow for chained expressions.

So the chaining that you're doing is exactly how they meant it to work, I think 😄.

@ChrisMarinos
Copy link

Note, the code you have would actually produce:

"Map { 0: "A!", 1: "S!", 2: "D!", 3: "F!" }" instead of Map { 'a': 'A!', 's': 'S!', 'd': 'D!', 'f': 'F!' }

I'd like a value selector as mentioned above, but I'd actually much prefer a key selector. I find that the lack of a key selector is a much more significant limitation.

@dubrowgn
Copy link

I posted some code for this on #697. Thoughts?

This was referenced Feb 15, 2016
@leebyron
Copy link
Collaborator

leebyron commented Mar 8, 2017

Closing this aging issue.

Trying to shy away from toMap and family, I'd prefer not to complicate them further.

@leebyron leebyron closed this as completed Mar 8, 2017
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

No branches or pull requests

5 participants