Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over a string in coffeescript without having to create an array

I have several places in my code where I need to iterate over a string and perform operations char by char. My node.js application needs to do this dozens of times per request and often the length of the strings can be fairly long.

The only way I've seen to convert a javascript like the one below into coffeescript is to create an array based on the length of the string. The problem with this I have is it's an extra thing to do on the hardware side, takes up extra memory, and just seems unnecessary (my node application processes dgrams - up to thousands a second - so all this extra work adds up).

The JavaScript way:

for(var i = 0; i < str.length; i++) { /* Do stuff with str here */ }

The suggested CoffeeScript way

for i in [0..str.length]
  # Do stuff here

Again, I think it's silly to force the creation of an array object when the traditional for loop doesn't have to mess with that step from a hardware perspective.

My only work around I've found is to use while loops like:

i = 0
while i < str.length
  # Do stuff
  i++

While that works, that's far more verbose than even the straight JavaScript way of just using a simple for loop.

Is there a way to use a for loop in CoffeeScript without having to generate superfluous arrays in order to perform basic iterations?

like image 215
Dan Avatar asked Dec 05 '12 15:12

Dan


People also ask

How to iterate over an array in typescript?

Iterating over an array is one of the most commonly faced problem in any programming language. In typescript, we have multiple ways to iterate an array. Using loops and using its inbuilt method forEach, we can iterate through the array elements. In this tutorial, I will show you different ways to do it with examples. Using a for loop :

What is the output of a CoffeeScript array?

On executing, the CoffeeScript file produces the following output. Unlike the Arrays in other programming languages the arrays in CoffeeScript can have multiple types of data i.e. both string and numericals. Here is an example of a CoffeeScript array holding multiple types of data.

What is a string object in CoffeeScript?

The String object lets you work with a series of characters. As in most of the programming languages, the Strings in CoffeeScript are declared using quotes as −

How to concatenate two strings in CoffeeScript?

As in most of the programming languages, the Strings in CoffeeScript are declared using quotes as − On compiling, it will generate the following JavaScript code. We can concatenate two strings using the "+" symbol as shown below. On compiling, it will generate the following JavaScript code.


3 Answers

It doesn't actually create an array if it doesn't have to. Look at the compiled JS. This CoffeeScript:

str = "hello"
for i in [0..(str.length-1)]
  alert(i)

Generates the following JavaScript:

var i, str, _i, _ref;

str = "hello";

for (i = _i = 0, _ref = str.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
  alert(i);
}

No array was actually created.

(Subtracting 1 to .length to avoid an undefined)

like image 156
vcsjones Avatar answered Oct 18 '22 11:10

vcsjones


You may also iterate over the string itself:

for ch, i in str
  # Do stuff here.
like image 32
epidemian Avatar answered Oct 18 '22 12:10

epidemian


In JavaScript (And thus, also CoffeeScript), strings can be accessed like you'd access arrays:

console.log("Hello world".length);       // returns "11"
console.log("Hello world"[6]);           // returns "o"
console.log("Hello world".indexOf("w")); // returns "6"

I don't see you initializing arrays in any of those loops.

You should be able to use plain JavaScript in your CoffeeScript files, though, if that solves the issue.

like image 23
Cerbrus Avatar answered Oct 18 '22 11:10

Cerbrus