prelude-ls icon indicating copy to clipboard operation
prelude-ls copied to clipboard

Call function arguments in List functions with multiple arguments (element, index, array)

Open Daiz opened this issue 11 years ago • 9 comments

Native JS [].map and [].forEach call the given function with function(element, index, array). I think the List functions that take functions in prelude would benefit from the same thing - right now they only call the given functions with function(element) in most cases. Providing the index and array would give a lot more flexibility.

Daiz avatar Aug 03 '13 15:08 Daiz

+1, it seems like this is a must-have for prelude's list functions to be an alternative to underscore

sbking avatar Sep 29 '13 02:09 sbking

The issue is with using map on a list of curried functions to create new functions, but maybe that is a small use case.

gkz avatar Sep 29 '13 19:09 gkz

Could you give an example of how to use zips to achieve this effect, as mentioned in #21? I'm confused how to create a readable pattern for this, both with list functions and object functions.

I would think that use case is less common than mapping through a list or object of non-function values or non-curried functions. Maybe my head is just stuck in my ways, but I'm trying to achieve something with similar readability to:

map ((val, key) -> "#key is #val"), <[ foo bar baz ]> #=> ["0 is foo", "1 is bar", "2 is baz"]

sbking avatar Sep 29 '13 19:09 sbking

I'm leaning toward changing it. As the change will not be backwards compatible, it would be with prelude.ls 2.0.0

gkz avatar Sep 29 '13 20:09 gkz

Right, for now I'm just using Daiz's versions renamed to map-at, filter-at, etc.

sbking avatar Sep 29 '13 21:09 sbking

Could you give an example of how to use zips to achieve this effect, as mentioned in #21? I'm confused how to create a readable pattern for this, both with list functions and object functions.

I would think that use case is less common than mapping through a list or object of non-function values or non-curried functions. Maybe my head is just stuck in my ways, but I'm trying to achieve something with similar readability to:

map ((val, key) -> "#key is #val"), <[ foo bar baz ]> #=> ["0 is foo", "1 is bar",

zip-with ((i, val) -> "#i is #val") [1 to 3] <[ foo bar baz ]>

joneshf avatar Mar 27 '14 02:03 joneshf

Also, I think it'd be better to not break compatibility. and just offer additional functions if you need an index.

joneshf avatar Mar 27 '14 02:03 joneshf

If you want to get the index, you can use obj-to-pairs on an array (this will get the index as a string):

map (([key, val]) -> "#key is #val"), obj-to-pairs <[ foo bar baz ]>

gkz avatar Mar 27 '14 03:03 gkz

Adding key access would also be a welcomed addition for Obj.* versions such as Obj.map and Obj.each

One benefit is easier migration from other toolbelt like lodash.

Delapouite avatar Mar 31 '14 09:03 Delapouite