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

improve List.indexedMap example #1008

Closed
stevensonmt opened this issue Dec 8, 2018 · 6 comments
Closed

improve List.indexedMap example #1008

stevensonmt opened this issue Dec 8, 2018 · 6 comments

Comments

@stevensonmt
Copy link

The current example for List.indexedMap is

indexedMap (,) ["Tom","Sue","Bob"] == [ (0,"Tom"), (1,"Sue"), (2,"Bob") ]

The function being applied to the map is not immediately clear in this syntax. I would propose the following alternative example:

indexedMap ( \i n -> i * n ) [ 1, 2, 3, 4 ] == [ 0, 2, 6, 12 ]

Perhaps this could be followed by an example in which the index parameter is inferred, as in the current example.

@timbuckley
Copy link

Personally, I prefer the first example because the relationship between the index argument and the 2 in (2, "Bob") is more obvious than the relationship between 4 and 12 in your example, especially to those who are not quite as math-savvy.

Perhaps we can still improve on the clarity on the first by not using the (,) to-tuple operator by instead using a lambda function which does the same:

indexedMap (\i x -> (i, x)) ["Tom","Sue","Bob"] == [ (0,"Tom"), (1,"Sue"), (2,"Bob") ]

But there is another benefit to the original, which is that it teaches both the (,) operator (a useful function in itself) and indexedMap at the same time. Showing how different functions can be combined together is extremely useful for those learning Elm, IMO.

@stevensonmt
Copy link
Author

That's a fair point. Perhaps using

indexedMap (\i x -> (i, x)) ["Tom","Sue","Bob"] == [ (0,"Tom"), (1,"Sue"), (2,"Bob") ]

and then pointing out that the anonymous function could be replaced by the (,) operator would be the best option.

@drathier
Copy link

drathier commented Jan 3, 2019

Tuple.pair is what it should use.

@frozar
Copy link

frozar commented May 30, 2019

I don't know for you but when I tried the example out of the box (elm 0.19), I get the following output:

> List.indexedMap (,) ["Tom","Sue","Bob"]                                  
-- PARSE ERROR ------------------------------------------------------------- elm

Something went wrong while parsing an expression (in parentheses) in
repl_value_1's definition.

4|   List.indexedMap (,) ["Tom","Sue","Bob"]
                      ^
I was expecting:

  - an expression, like x or 42
  - a closing paren
  - a minus sign (-)
  - an infix operator, like (+) or (==)

So for me, the example is just wrong (or I miss something). Anyway, thank a lot for right examples here that help to undersetand this feature.

@drathier
Copy link

drathier commented Jun 4, 2019

Yeah, the (,) tuple constructor syntax was removed in 0.19.

benkoshy added a commit to benkoshy/core that referenced this issue Nov 19, 2019
# What is the change?

* it adds another example that will hopefully be clearer to users.

# Why the change?

For me, the existing example was not altogether clear. (I am relatively new Elm user.) I did some Googling and found out that other users were facing the exact same issue: elm#1008

# The Solution: Add another example

Hopefully it will be clearer.
@evancz
Copy link
Member

evancz commented Feb 9, 2021

The syntax changed to List.indexedMap Tuple.pair ["Tom","Sue","Bob"] in 0.19.0, but tracking in #1068 for the more explicit example.

@evancz evancz closed this as completed Feb 9, 2021
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