7

I want to create two arrays b and c at the same time. I know two methods which can be able to achieve it. The first method is

b = ([i, i * 2] for i in [0..10])
c = ([i, i * 3] for i in [0..10])

alert "b=#{b}"
alert "c=#{c}"

This method is very handy for creating only one array. I can not be the better way to get the better performance for computation.

The second method is

b = []
c = []
for i in [0..10]
  b.push [i, i*2]
  c.push [i, i*3]

alert "b=#{b}"
alert "c=#{c}"

This method seems good for computation efficiency but two lines b = [] c = [] have to be written first. I don't want to write this 2 lines but I have not find a good idea to have the answer. Without the initialization for the arrays of b and c, we can not use push method.

There exists the existential operator ? in Coffeescript but I don't know hot to use it in this problem. Do you have a better method for creating the arrays of b and c without the explicit initialization?

Thank you!

5
  • maybe b = c = [] will be more suitable? Commented Mar 8, 2013 at 8:28
  • Thanks. You will get the same result for b and c. The answer is wrong. Commented Mar 8, 2013 at 8:32
  • 1
    @varnie: No, the arrays need to be separate, and their contents are different. Commented Mar 8, 2013 at 8:33
  • How about [b, c] = [[],[]]; Commented Mar 9, 2013 at 19:19
  • [b,c] =[[],[]] works but is still trapped in the many parens. Commented Mar 11, 2013 at 0:36

2 Answers 2

4

You can use a little help from underscore (or any other lib that provides zip-like functionality):

[b, c] = _.zip ([[i, i * 2], [i, i * 3]] for i in [0..10])...

After executing it we have:

coffee> b 
[ [ 0, 0 ],
  [ 1, 2 ],
  [ 2, 4 ],
  [ 3, 6 ],
  [ 4, 8 ],
  [ 5, 10 ],
  [ 6, 12 ],
  [ 7, 14 ],
  [ 8, 16 ],
  [ 9, 18 ],
  [ 10, 20 ] ]

coffee> c
[ [ 0, 0 ],
  [ 1, 3 ],
  [ 2, 6 ],
  [ 3, 9 ],
  [ 4, 12 ],
  [ 5, 15 ],
  [ 6, 18 ],
  [ 7, 21 ],
  [ 8, 24 ],
  [ 9, 27 ],
  [ 10, 30 ] ]

See the section about splats in CoffeeScript docs for more details and examples.

Sign up to request clarification or add additional context in comments.

5 Comments

Very interesting but it is really a challenge. I haven't got the right answer with it. Thank you.
I've updated my answer with the result I get. is it incorrect?
Thanks a lot. I have also got the same results. I forgot the three dots at the end of line. I have not really well understand the usage of the three dots. Thank you very much.
From CoffeeScript docs: "The JavaScript arguments object is a useful way to work with functions that accept variable numbers of arguments. CoffeeScript provides splats ..., both for function definition as well as invocation, making variable numbers of arguments a little bit more palatable". See the docs for details and examples.
Thanks you very much for your kind suggestion. It seems that the splats must be used as an argument of function such as _.zip. I will try to practice more with it.
1

How about this using the existential operator:

for i in [0..10]
    b = [] if not b?.push [i, i*2]
    c = [] if not c?.push [i, i*3]

console.log "b=#{b}"
console.log "c=#{c}"

Or to be a bit more understandable:

for i in [0..10]
    (if b? then b else b = []).push [i, i*2]
    (if c? then c else c = []).push [i, i*3]

console.log "b=#{b}"
console.log "c=#{c}"

EDIT: from comments:

OK but you you have to write so many tedious codes. The same reason is also for ` (b = b or []).push [i, i*2]

It is tedious, so we can wrap it in a function (but beware the variables will be global now):

# for node.js
array = (name) -> global[name] = global[name] or []

# for the browser
array = (name) -> window[name] = window[name] or []

for i in [0..10]
    array('b').push [i, i*2]
    array('c').push [i, i*3]

console.log "b=#{b}"
console.log "c=#{c}"

4 Comments

OK but you you have to write so many tedious codes. The same reason is also for ` (b = b or []).push [i, i*2] `
Too complex. Still no better way than initializing them separately with b =[] and c = []. I wonder if there are some ways which can combine the 1st and 2nd methods as I mentioned. We perhaps may need to modify the definition for the push method for the array to make it have a initial value.
I agree that the explicit definition is definitely the simplest, and will be the easiest to understand. I wouldn't want to get into extending the .push functionality, seems even messier.
So do I. Thanks a lot for your kindness suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.