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

added documentation how to get the index of an array element in a loop #1422

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/coffee/array_with_index_comprehensions.coffee
@@ -0,0 +1,2 @@
for food, index in ['toast', 'cheese', 'wine']
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't think of any realistic examples where it actually made sense to need the index?

Copy link
Author

Choose a reason for hiding this comment

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

for example if you want to set a conference pricing with early bird incentive.
the first ticket is free, the ticket price grows exponentially and stops at 120 USD

participants = ['first participant', 'second participant', 'third participant', 'fourth participant']
for participant, index in participants
alert "#{participant} pays #{Math.min(Math.pow(index, 2), 120)} USD"

I know this is not exactly a good example for adding it to the documentation and It could also be solved by using an ordinary for each loop without index parameter and defining a counter variable instead, but I think solving it in this way is much more elegant.
do you maybe have a better idea for a good example?

cheers, allan

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe something like this?

valueOverTime = [4.9, 4.83, 4.701, 4,5]
for value, time in valueOverTime when time > 0
  change = value/valueOverTime[time-1] - 1
  alert "value changed by #{change*100}%"

Copy link

Choose a reason for hiding this comment

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

how about:

cities = [ { name: "Cairo", position: {lon: 12, lat: 43}},
           { name: "London", position: {lon: -4, lat: -2}},
           { name: "Sydney", position: {lon: 3, lat: 4}},
           { name: "Guadalajara", position: {lon: 7, lat: -3}} ]

distances = for f, index in cities
              for t in cities[index + 1..]
                { from: f.name, to: t.name, distance: computeDistance(f.position,t.position)}

for computing the distance between each pair of cities just once.

Copy link
Collaborator

Choose a reason for hiding this comment

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

alert "#{food}, #{index}"
4 changes: 4 additions & 0 deletions documentation/index.html.erb
Expand Up @@ -505,6 +505,10 @@ Expressions
by adding a meaningful return value, like <tt>true</tt>, or <tt>null</tt>,
to the bottom of your function.
</p>
<p>
To get the array index, you can additionaly add an index variable to the expression:<br />
</p>
<%= code_for('array_with_index_comprehensions') %>
<p>
To step through a range comprehension in fixed-size chunks,
use <tt>by</tt>, for example:<br />
Expand Down