coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Javascript equivalent of string range (['a'...'z']) is possible, but not working when executed.

Open OnesimusUnbound opened this issue 15 years ago • 10 comments

I've tried the following expression

'a' in ['a'...'z']

and a JavaScript code was generated. However, when I ran it on Try CoffeeScript, it appears to run indefinitely.

OnesimusUnbound avatar Mar 22 '11 07:03 OnesimusUnbound

Ranges with strings as endpoints are not properly supported right now. The range-generating code that is output will keep appending "1" to the starting string. Since generating successive strings is not a trivial task (or standardized), I believe we should just produce errors for ranges with non-integer endpoints. Though this brings back the issue of supporting the currently-working (a for a in [0.1 .. 0.6] by 0.1) syntax. Keeping support would break that integer-only rule, but only for ranges used in comprehensions and for-loops. That is one reason we should rethink adding [0.1 .. 0.6 by 0.1] syntax.

michaelficarra avatar Mar 22 '11 14:03 michaelficarra

Yes, I agree, an error should be thrown when a string is passed in the ranges for now.

However, supposed the type of the arguments in the range is determined in run time, for example

# Working
a = 1; b = 10
1 in [a ... b]

 # Not Working
a = 'a'; b = 'z'
1 in [a ... b]

From my above examples, I think that the error is thrown at run time.

OnesimusUnbound avatar Mar 23 '11 03:03 OnesimusUnbound

There should certainly be a compile-time error for ['a'..'z']. I think it's best to assume that a and b are numbers in [a..b], for efficiency's sake. As a rule, CoffeeScript doesn't throw its own runtime exceptions, and I don't see how else a non-numerical a or b could be sensibly handled if found.

TrevorBurnham avatar Mar 31 '11 18:03 TrevorBurnham

related: satyr/coco@df7672aef19f5d9085bb9850c4a6a29fab0ac517 (character ranges)

michaelficarra avatar Aug 16 '11 20:08 michaelficarra

talking in C terms, the example given here, ['a'..'z'] show a character range more than string range. char range is decently well defined according to me. on the other hand, what a user might mean by something like ['a'..'abc'] is a matter of opinions. what about supporting char ranges?

mfrasca avatar Apr 25 '12 20:04 mfrasca

I had a look at the code in Node.js and here are my temporary conclusions:

if the from and to of the Range are specified by literals, support of length-1-strings is quite doable.

but if the from and of the Range are specified by variables, then the translation of the code would depend on the values of the variables. that is, you cannot translate the code until you start executing it. (or we could define a class that is incrementable and decrementable and that can be initialized with either a number or a length-1- string. but this would change the whole structure of the current implementation of Range.)

mfrasca avatar Apr 26 '12 08:04 mfrasca

please be patient, I never really worked with javascript! what do you think of this Iterator class? according to me it could serve as base for iterating on characters (and according to me it simplifies the code generation task).

function Iterator(start, end, equality) {
  this.start = start;
  this.end = end;
  this.value = this.start;
  this.equality = equality;
  this.increment = (start < end);
}

Iterator.prototype.next = function() {
  if(this.increment) {
    return(++this.value);
  } else {
    return(--this.value);
  }
};

Iterator.prototype.test = function() {
  if(this.increment) {
    return(this.value < this.end || this.equality && this.value == this.end);
  } else {
    return(this.value > this.end || this.equality && this.value == this.end);
  }
};

for(i=new Iterator(0, 4, true), a=i.value; i.test(); a=i.next()) {
  document.write('Hello World' + a);
}

mfrasca avatar Apr 27 '12 06:04 mfrasca

this is still just playing around with code, but this modified class works on characters.

function Iterator(start, end, equality) {
  this.start = start.charCodeAt(0);
  this.end = end.charCodeAt(0);
  this.iter = this.start;
  this.equality = equality;
  this.increment = (this.start < this.end);
  this.value = start;
}

Iterator.prototype.next = function() {
  if(this.increment) {
    return(String.fromCharCode(++this.iter));
  } else {
    return(String.fromCharCode(--this.iter));
  }
};

Iterator.prototype.test = function() {
  if(this.increment) {
    return(this.iter < this.end || this.equality && this.iter == this.end);
  } else {
    return(this.iter > this.end || this.equality && this.iter == this.end);
  }
};

for(i=new Iterator('a', 'e', false), a=i.value; i.test(); a=i.next()) {
  document.write('Hello World' + a);
}

notice the differences in the constructor and in next.

now how do I check whether start and end are character data...

mfrasca avatar Apr 27 '12 06:04 mfrasca

the last one I guess...

function Iterator(start, end, equality, step) {
  this.value = start;
  if(typeof start == "string") {
    this.isStringIterator = true;
    start = start.charCodeAt(0);
    end = end.charCodeAt(0);
  } else {
    this.isStringIterator = false;
  }
  this.start = this.iter = start;
  this.end = end;
  this.equality = equality;
  this.increment = (start < end);
  this.step = step;
}

Iterator.prototype.next = function() {
  if(this.increment) {
    this.iter += this.step;
  } else {
    this.iter -= this.step;
  }
  if (this.isStringIterator) {
    return(String.fromCharCode(this.iter));
  } else {
    return(this.iter);
  }
};

Iterator.prototype.test = function() {
  if(this.increment) {
    return(this.iter < this.end || this.equality && this.iter == this.end);
  } else {
    return(this.iter > this.end || this.equality && this.iter == this.end);
  }
};

this does integer, float and character ranges, increasing and decreasing, by the given step.

mfrasca avatar Apr 27 '12 07:04 mfrasca

This currently gets compiled to:

var i, results,
  indexOf = [].indexOf;

indexOf.call((function() {
  results = [];
  for (var i = 'a'; 'a' <= 'z' ? i < 'z' : i > 'z'; 'a' <= 'z' ? i++ : i--){ results.push(i); }
  return results;
}).apply(this), 'a') >= 0;

I’m not sure what the original poster was expecting, so I can’t tell if this is good or bad. But it doesn’t run forever, at least, so I’m presuming that whatever the issue was has since been fixed.

GeoffreyBooth avatar Apr 26 '17 06:04 GeoffreyBooth