Thursday 16 April 2015

Lucee 5 beta: <cfloop> tweak

G'day:
There's another few odds 'n' sods with the Lucee 5 beta that are testable / demoable, but they're pretty trivial / small features, so I'll have to do a series of "misc" blog articles.

There's a bunch of more significant stuff I have not looked at yet because there's no beta documentation for them, so I cannot work out what to do. It's been observed that I am being unhelpful to the Lucee project for making observations like this, interestingly enough. I'm not one to get all biblical (although perhaps I should, but in a different sense), but Matthew 7:3 definitely spring to mind here. But anyway, whatever [gallic shrug].

OK, so they've added a new feature to <cfloop>, anyhow.



<!--- timesLoop.cfm --->
<cfloop times="5">
One thing I don't like is repeating myself for no reason<br>    
</cfloop>

And just to remove all doubt as to what this does:

One thing I don't like is repeating myself for no reason
One thing I don't like is repeating myself for no reason
One thing I don't like is repeating myself for no reason
One thing I don't like is repeating myself for no reason
One thing I don't like is repeating myself for no reason


The conceit here is that a person might want to loop, but might not want an index variable. So it's to save us having to do this:

<cfloop index="i" from="1" to="5">
One thing I don't like is repeating myself for no reason<br>    
</cfloop>

Someone chided me for raising nuisance bug tickets the other day, as I'd be distracting Micha from important work. Work like this, I guess.

Yeah, I'm underwhelmed. This is not a useful feature to be wasting time with. That said, I was talking about this on IRC just now (as I'm typing this) and one of the punters there quite liked it, so I guess it has merit for some people.

There's no native CFScript equivalent to this. One needs to use the tags-in-script-based approach:

loop times=5 {
    echo("One thing I don't like is repeating myself for no reason<br>")
}

I guess the script version ought to have been:

for (5) {
    echo("One thing I don't like is repeating myself for no reason<br>")
}

Whilst thinking about the script solution for this (that one only came to me now, I came up with something else first), I recalled that in Ruby one can do this:

# times.rb
5.times do
    puts "One thing I don't like is repeating myself for no reason\n"
end

That's quite cool. Note that this will also pass in the current iteration value if one needs it:

5.times do |i|
    puts "One thing I don't like is repeating myself for no reason #{i}\n"
end

But as is common in Ruby: if one doesn't need some code, one doesn't need to specify it.

I googled about and Groovy does this too:

// times.groovy
5.times {
    println("One thing I don't like is repeating myself for not reason")
}

So I think a nice approach in CFML might be to have a .times() method too, which takes a callback of what to do n times:

5.times(()->echo("One thing I don't like is repeating myself for not reason<br>"))

I'll raise an E/R for this and update this article: LDEV-289.

Anyway, it's a small feature so this is a small article. It was pleasing to have a mess around with some Ruby and Groovy code again, too.

Once they put some docs for the bigger features up on the beta site, I'll know what they're about and be able to write those up too. I've got another coupla "misc" topics in reserve though, in the mean time.

Righto.

--
Adam