@for can't decrement #691

Closed
ghost opened this Issue Mar 20, 2013 · 9 comments

Comments

Projects
None yet
5 participants
@ghost

ghost commented Mar 20, 2013

I was a bit surprised to see this isn't possible:

@for $i from 7 through 1 {
    @debug $i;
}

The expected output being:

./test.scss:34 DEBUG: 7
./test.scss:34 DEBUG: 6
./test.scss:34 DEBUG: 5
./test.scss:34 DEBUG: 4
./test.scss:34 DEBUG: 3
./test.scss:34 DEBUG: 2
./test.scss:34 DEBUG: 1

The output, however was nothing.
Was this by design? I can't imagine it would have been.

@robwierzbowski

This comment has been minimized.

Show comment
Hide comment
@robwierzbowski

robwierzbowski Mar 22, 2013

Contributor

@for only counts up. Whether this is a bug or not, you can get around it with negative numbers and the abs() (absolute value) function:

@for $i from -7 through -1 {
    @debug abs($i);
}

Result:

/style.scss:3 DEBUG: 7
/style.scss:3 DEBUG: 6
/style.scss:3 DEBUG: 5
/style.scss:3 DEBUG: 4
/style.scss:3 DEBUG: 3
/style.scss:3 DEBUG: 2
/style.scss:3 DEBUG: 1
Contributor

robwierzbowski commented Mar 22, 2013

@for only counts up. Whether this is a bug or not, you can get around it with negative numbers and the abs() (absolute value) function:

@for $i from -7 through -1 {
    @debug abs($i);
}

Result:

/style.scss:3 DEBUG: 7
/style.scss:3 DEBUG: 6
/style.scss:3 DEBUG: 5
/style.scss:3 DEBUG: 4
/style.scss:3 DEBUG: 3
/style.scss:3 DEBUG: 2
/style.scss:3 DEBUG: 1
@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Mar 27, 2013

Thanks @robwierzbowski: Yea, there are various workarounds, they're just less intuitive. I don't know how high of a priority a native implementation would be, but it'd still be nice to see.

ghost commented Mar 27, 2013

Thanks @robwierzbowski: Yea, there are various workarounds, they're just less intuitive. I don't know how high of a priority a native implementation would be, but it'd still be nice to see.

@chriseppstein

This comment has been minimized.

Show comment
Hide comment
@chriseppstein

chriseppstein May 20, 2013

Member

I think adding a by clause to @for would be very nice. Sure it can always be done via math, but it's a nice sugar.

Examples:

@for $v from 0 through 10 by 2 { ... }
@for $v from 10 to 1 by -1 { ... }

Also, Sass can fairly easily change from increment to decrement when the orientation of the values is flipped. This avoids infinite loops. I'm 👍 on this one.

Member

chriseppstein commented May 20, 2013

I think adding a by clause to @for would be very nice. Sure it can always be done via math, but it's a nice sugar.

Examples:

@for $v from 0 through 10 by 2 { ... }
@for $v from 10 to 1 by -1 { ... }

Also, Sass can fairly easily change from increment to decrement when the orientation of the values is flipped. This avoids infinite loops. I'm 👍 on this one.

@nex3

This comment has been minimized.

Show comment
Hide comment
@nex3

nex3 May 21, 2013

Contributor

I'm a little ambivalent about supporting @for ... by. I'm not sure it's widely useful enough to warrant expanding the @for syntax, especially given how easy it is to do by hand.

I do think @for $i from 10 to 1 should work, though.

Contributor

nex3 commented May 21, 2013

I'm a little ambivalent about supporting @for ... by. I'm not sure it's widely useful enough to warrant expanding the @for syntax, especially given how easy it is to do by hand.

I do think @for $i from 10 to 1 should work, though.

@dustinhorton

This comment has been minimized.

Show comment
Hide comment
@dustinhorton

dustinhorton Jul 17, 2013

Just ran into this, so if actual use cases help: I was using a for loop to generate a series of max-width media queries, issue being it was generating larger & larger max-width media queries which would simply override all those prior. Fixed using abs() to get a decrementing @for loop.

Can post code if that'd help, but +1 for @for $i from 10 to 1, or the alternate syntax Chris proposed.

Just ran into this, so if actual use cases help: I was using a for loop to generate a series of max-width media queries, issue being it was generating larger & larger max-width media queries which would simply override all those prior. Fixed using abs() to get a decrementing @for loop.

Can post code if that'd help, but +1 for @for $i from 10 to 1, or the alternate syntax Chris proposed.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jul 17, 2013

The flexibility in the version @chriseppstein posted would be great. As noted; sure, it's easy enough to do most things by hand, but the brevity afforded by some native support ( not to mention the flexibility of something like by in this case ) would be more than welcomed ( and leveraged ) I'm sure.

ghost commented Jul 17, 2013

The flexibility in the version @chriseppstein posted would be great. As noted; sure, it's easy enough to do most things by hand, but the brevity afforded by some native support ( not to mention the flexibility of something like by in this case ) would be more than welcomed ( and leveraged ) I'm sure.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jul 19, 2013

@nex3: If you're going to use the logic that going by 2 is easy to do by hand, going by -1 should be easy enough...

@for $i from 0 through 9 {
  // 10 - $i
}

I think that adding some sort of extension to change the step would be a good thing.

ghost commented Jul 19, 2013

@nex3: If you're going to use the logic that going by 2 is easy to do by hand, going by -1 should be easy enough...

@for $i from 0 through 9 {
  // 10 - $i
}

I think that adding some sort of extension to change the step would be a good thing.

@chriseppstein

This comment has been minimized.

Show comment
Hide comment
@chriseppstein

chriseppstein Jul 19, 2013

Member

I think the by clause is useful, it's obvious what it's doing to an untrained user -- even more so than the math required to accomplish the same result without it and the implementation cost is low.

Member

chriseppstein commented Jul 19, 2013

I think the by clause is useful, it's obvious what it's doing to an untrained user -- even more so than the math required to accomplish the same result without it and the implementation cost is low.

@robinroestenburg

This comment has been minimized.

Show comment
Hide comment
@robinroestenburg

robinroestenburg Sep 17, 2013

Contributor

I added a PR for implementing a decrementing loop (@for $i from 10 to 1 as @nex3 suggested). Sorry I couldn't get hub to add the PR to the issue directly, had a lot of trouble with it - seems there are some problems with Github's new 2FA.

Contributor

robinroestenburg commented Sep 17, 2013

I added a PR for implementing a decrementing loop (@for $i from 10 to 1 as @nex3 suggested). Sorry I couldn't get hub to add the PR to the issue directly, had a lot of trouble with it - seems there are some problems with Github's new 2FA.

nex3 added a commit that referenced this issue Jan 8, 2014

Merge branch 'decrementing-loops'
Conflicts:
	doc-src/SASS_CHANGELOG.md

Closes #923
Closes #691

@nex3 nex3 closed this Jan 8, 2014

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