Skip to content

Loading…

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

Closed
wants to merge 1 commit into from

6 participants

@allanberger
  • added single description line in the documentation
  • added code example
@michaelficarra michaelficarra commented on the diff
...ntation/coffee/array_with_index_comprehensions.coffee
@@ -0,0 +1,2 @@
+for food, index in ['toast', 'cheese', 'wine']
@michaelficarra Collaborator

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

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

@thejh
thejh added a note

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}%"
@uu1101
uu1101 added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@davidbau

Today I had a teacher who was puzzled by this construct because he couldn't find any documentation of it. I actually cannot find a Coffeescript reference that notes that it exists, presumably because the authors don't know, presumably because it's not described here.

Can we merge this addition?

@jashkenas
Owner

Comprehensions replace (and compile into) for loops, with optional guard clauses and the value of the current array index.

courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
menu i + 1, dish for dish, i in courses

@jashkenas jashkenas closed this
@jashkenas jashkenas added the duplicate label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 7, 2011
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 0 deletions.
  1. +2 −0 documentation/coffee/array_with_index_comprehensions.coffee
  2. +4 −0 documentation/index.html.erb
View
2 documentation/coffee/array_with_index_comprehensions.coffee
@@ -0,0 +1,2 @@
+for food, index in ['toast', 'cheese', 'wine']
@michaelficarra Collaborator

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

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

@thejh
thejh added a note

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}%"
@uu1101
uu1101 added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ alert "#{food}, #{index}"
View
4 documentation/index.html.erb
@@ -506,6 +506,10 @@ Expressions
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 />
<tt>evens = (x for x in [0..10] by 2)</tt>
Something went wrong with that request. Please try again.