Why doesn't the compiler discover that the for-loop may be terminated prematurely in (1) and translate the code to (3)?
-Boris
(3) : You can even put the if in the for-comprehension, ( it translates to a call to 'filter' somewhere in the chain call) :
def ls(x: Int, a: Array[Int]): Int = {
for (i <- 0 to a.length -1; if a(i) == x) return i
-1
}
But really, a 'for' is not the way to go here. You don't want a full traversal, so there are methods that handle aborted traversals for you.
On 10.10.2012 13:36, Alex Repain wrote:
(1) : The program does what is expected. In Scala the last value
computed is returned by default. The last computed in the function body
of (1) will always be -1.
I see. This is different to ML where an expression such as 1;3 is illegal.
what is at fault is your algorithm design (i.e. don't use a
for-comprehension in the first place).
As I wrote, I want to make the list scanning explicit by using a loop instead of a HOF such as exists. This way, it is easy to see that the running time is in O(n), which is less obvious if I use HOFs.
for(i <- 0 to a.length -1) if (a(i) == x) println(i) // does this mean
println(x) and return from the function ?
That's a valid point.
Hoever, if for-loops evaluate to (), as Jason wrote, why doesn't give
for(...) i
a type error, since expression i is useless here?
So for-loops (without yield) may only be used for its side-effects?
--
Best regards,
Boris
On Thu, Oct 11, 2012 at 4:42 AM, Boris HollasYou'd be surprised how many side-effecting methods return non-Unit
<hol...@informatik.htw-dresden.de> wrote:
>
> It would suffice to check that i has unit type. The compiler should be able
> to do that, even if it's not obvious from the syntax. In ML, the compiler
> emits a warning:
values. For example:
for (line <- text.lines) sb.append(line) // error,
StringBuilder.append returns StringBuilder
for (word <- line.split("\\s+")) set += word // error if set: mutable.Set
On Thu, Oct 11, 2012 at 5:04 AM, Boris Hollas
<hol...@informatik.htw-dresden.de> wrote:
>
> I don't think it's a good idea to have an expression that looks like a for
> in Java when in fact it isn't. It's a trap to fall in easily. In contrast,
> if I use foreach or map, it is obvious that these are HOFs. For the same
> reason, I don't like the do-notation in Haskell. It is functional code
> disguised as imperative.
Well, please take what I said with a large pinch of salt, but...
Given that for loops and imperative programming are the wrong way of
doing things, why let them have precedence when picking out construct
names?
.
--
Daniel C. Sobral
Well, please take what I said with a large pinch of salt, but...On Thu, Oct 11, 2012 at 5:04 AM, Boris Hollas
<hol...@informatik.htw-dresden.de> wrote:
> On 10.10.2012 21:08, sschaef wrote:
>>
>> I think it is extremely important here to mention that Scala does not
>> have a for-loop! Instead it has a for-comprehension, which is not a loop
>> but a call to a composition of higher order functions - thus, if you
>> wanna use a loop don't use the for-comprehensions, use the while-loop or
>> recursion.
>
>
> Thanks for pointing this out. This wasn't at all obvious to me, even after I
> read the section on for-expressions in "Programming in Scala" by Martin
> Odersky.
>
> I don't think it's a good idea to have an expression that looks like a for
> in Java when in fact it isn't. It's a trap to fall in easily. In contrast,
> if I use foreach or map, it is obvious that these are HOFs. For the same
> reason, I don't like the do-notation in Haskell. It is functional code
> disguised as imperative.
Given that for loops and imperative programming are the wrong way of
doing things, why let them have precedence when picking out construct
names?
On 11.10.2012 20:30, Daniel Sobral wrote:What is wrong if you write
Fluent interfaces.
sb.append("x").append("y")
set += 1 += 2
sb.append("x"); sb.append("y")
set += 1; set += 2
instead? It looks clearer to me.
yes, I'd like that.(2). that your compiler should probably do the following to stay coherent :
val x = {
i // warning or error : unused return value of type Boolean
5
}