Coffescript question (includes answer!)

55 views
Skip to first unread message

metageoff

unread,
Apr 27, 2011, 9:49:54 AM4/27/11
to OGRE
I should have posted this here in the first place. My Question follows
with the Answer from Christopher.

Geoff

On Wed, Apr 27, 2011 at 9:19 AM, Geoff Longman <glon...@gmail.com>
wrote:
Dude,

Was checking it out a little more last night and I can't figure out
how to write the following in Coffescript:

(contrived example)

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9. 10, 11, 12]

var result = false
for (i=0, len = a.length, i < len; i++) {
if (a[i] == 3) {
result = true;
break; // stop looping
}
}

I can't seem to figure out the "stop looping" case in CS syntax.
If that array had 10,000 entries I dont want to loop 10,000 times if
the 3rd entry matched.
CS comprehensions appear to want to iterate over all entries and
collect the results.


Any suggestions???

--
Geoff


Chris Says
=========

a = [1..12]

for x in a
if x == 3
result = true
break

if result?
console.log "woohoo"

Note: Since coffeescript defines all your variables at the top you can
pull this off. Also this is a truthiness test, so in the if result?
case where we couldn't find x == 3, result would be undefined/null
which technically counts as false.

--
Christopher Saunders
http://christophersaunders.ca/

Willem van Bergen

unread,
Apr 27, 2011, 9:53:26 AM4/27/11
to ogre...@googlegroups.com
I think this is the equivalent of your javascript snippet.

a = [1,2,3,4,5,6,7,8]
result = false
for i in a
if i == 3
result = true
break

> --
> You received this message because you are subscribed to the Ottawa Group of Ruby Enthusiasts [OGRE].
> To post to this group, send email to ogre...@googlegroups.com
> To unsubscribe from this group, send email to
> ogre-list+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ogre-list?hl=en

Wayne Larsen

unread,
Apr 28, 2011, 2:20:11 AM4/28/11
to OGRE
On Apr 27, 9:49 am, metageoff <glong...@gmail.com> wrote:
>
> a = [1..12]
>
> for x in a
>   if x == 3
>     result = true
>     break

I don't mean to code golf this solution, but this may be helpful. I've
found a lot of utility in underscore[1] -- a great library that
provides the functional utilities that are missing from the javascript
stdlib.

_ = require 'underscore'
result = _.detect [1..12], (x) -> x==3

[1]: http://documentcloud.github.com/underscore

Wayne

metageoff

unread,
Apr 28, 2011, 1:07:14 PM4/28/11
to OGRE
and your code snippet, Willem, does not seem to parse.

as soon as I type the 'r' in 'for'...

a = [1,2,3,4,5,6,7,8]
result = false
for

I get:

PARSE ERROR ON LINE 3: UNEXPECTED 'TERMINATOR

could this be a bug in the online tool?

Geoff


On Apr 27, 9:53 am, Willem van Bergen <wil...@vanbergen.org> wrote:
> I think this is the equivalent of your javascript snippet.
>
> a = [1,2,3,4,5,6,7,8]
> result = false
> for i in a
>   if i == 3
>     result = true
>     break
>
> On 2011-04-27, at 9:49 AM, metageoff wrote:
>
>
>
>
>
>
>
> > I should have posted this here in the first place. My Question follows
> > with the Answer from Christopher.
>
> > Geoff
>
> > On Wed, Apr 27, 2011 at 9:19 AM, Geoff Longman <glong...@gmail.com>

Christopher Saunders

unread,
Apr 28, 2011, 1:27:05 PM4/28/11
to ogre...@googlegroups.com
The online tool is kinda funky.  I've found ignoring the error until you think you are done is the way to go.

for a in x
  console.log 'pew pew'

metageoff

unread,
Apr 28, 2011, 4:54:36 PM4/28/11
to OGRE
ya that snippet didn't parse either.
I guess I'll have to install it. node, npm, coffeescript.... I'm
losing interest ;)

On Apr 28, 1:27 pm, Christopher Saunders <c.saunders...@gmail.com>
wrote:

Willem van Bergen

unread,
Apr 28, 2011, 4:55:42 PM4/28/11
to ogre...@googlegroups.com
Don't install, use http://jashkenas.github.com/coffee-script/
and press try coffeescript. Live compilation to javascript.

Willem

metageoff

unread,
Apr 28, 2011, 10:01:50 PM4/28/11
to OGRE

Cool. I'll give that a shot.

On Apr 28, 4:55 pm, Willem van Bergen <wil...@vanbergen.org> wrote:
> Don't install, usehttp://jashkenas.github.com/coffee-script/

metageoff

unread,
Apr 28, 2011, 10:03:26 PM4/28/11
to OGRE
Hold on. That's the tool I've been getting the parse errors :)
Reply all
Reply to author
Forward
0 new messages