Why does foldLeft not support operator syntax?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does foldLeft not support operator syntax?

boris.hollas
Scala supports operator syntax for methods of arity 1, so you can write
List(1,2,3) reduce (_+_)
while
List(1,2,3) foldLeft (0)(_+_)
fails with "error: Int(0) does not take parameters".

According to the style guide, you should use operator syntax for infix operators. However, the problem with dot-notation as explained in the style guide also applies to foldLeft:
List("a","b").foldLeft("") (_+_).toUpperCase

Also, why does foldLeft have curried parameters? This way, we can't write
List(1,2,3) foldLeft (0, _+_)

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why does foldLeft not support operator syntax?

Kevin Wright-3
It’s all about type inference.

If I have method[T](a: T, b: T) and call it with two values a and b of different types then it’ll infer T to be the least upper bound of those two types.

If I have method[T](a: T)(b: T) and call it with two values a and b of different types then it’ll infer T to be the type of a, and will refuse to compile if the type of b doesn’t conform.

By having two parameter blocks, you can steer the compiler towards the type that you’re expecting it to return, and it can warn you if that won’t happen (instead of, e.g. inferring something useless like Any)



On 31 March 2016 at 11:18, <[hidden email]> wrote:
Scala supports operator syntax for methods of arity 1, so you can write
List(1,2,3) reduce (_+_)
while
List(1,2,3) foldLeft (0)(_+_)
fails with "error: Int(0) does not take parameters".

According to the style guide, you should use operator syntax for infix operators. However, the problem with dot-notation as explained in the style guide also applies to foldLeft:
List("a","b").foldLeft("") (_+_).toUpperCase

Also, why does foldLeft have curried parameters? This way, we can't write
List(1,2,3) foldLeft (0, _+_)

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why does foldLeft not support operator syntax?

Simon Schäfer
In reply to this post by boris.hollas


On 03/31/2016 12:18 PM, [hidden email] wrote:
Scala supports operator syntax for methods of arity 1, so you can write
List(1,2,3) reduce (_+_)
while
List(1,2,3) foldLeft (0)(_+_)
fails with "error: Int(0) does not take parameters".

According to the style guide, you should use operator syntax for infix operators. However, the problem with dot-notation as explained in the style guide also applies to foldLeft:
List("a","b").foldLeft("") (_+_).toUpperCase
Google search for "scala foldleft infix" reveals http://stackoverflow.com/questions/12904978 which should answer your question.

Also, why does foldLeft have curried parameters? This way, we can't write
List(1,2,3) foldLeft (0, _+_)
Google search for "scala multiple parameter lists" reveals http://stackoverflow.com/questions/4684185 which once again should answer your question.
--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why does foldLeft not support operator syntax?

boris.hollas
Am Donnerstag, 31. März 2016 12:31:45 UTC+2 schrieb Simon Schäfer:

Ich habe nicht gefragt, warum ich ein . schreiben muß, sondern warum dieses Design gewählt wurde.

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why does foldLeft not support operator syntax?

boris.hollas
In reply to this post by Kevin Wright-3
I see. However, the problem could be mitigated with explicit type annotation if the compiler can't infer a reasonable type.

Am Donnerstag, 31. März 2016 12:29:41 UTC+2 schrieb Kevin Wright:
It’s all about type inference.

If I have method[T](a: T, b: T) and call it with two values a and b of different types then it’ll infer T to be the least upper bound of those two types.

If I have method[T](a: T)(b: T) and call it with two values a and b of different types then it’ll infer T to be the type of a, and will refuse to compile if the type of b doesn’t conform.

By having two parameter blocks, you can steer the compiler towards the type that you’re expecting it to return, and it can warn you if that won’t happen (instead of, e.g. inferring something useless like Any)



On 31 March 2016 at 11:18, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="feeOK09XEQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">boris....@...> wrote:
Scala supports operator syntax for methods of arity 1, so you can write
List(1,2,3) reduce (_+_)
while
List(1,2,3) foldLeft (0)(_+_)
fails with "error: Int(0) does not take parameters".

According to the style guide, you should use operator syntax for infix operators. However, the problem with dot-notation as explained in the style guide also applies to foldLeft:
List("a","b").foldLeft("") (_+_).toUpperCase

Also, why does foldLeft have curried parameters? This way, we can't write
List(1,2,3) foldLeft (0, _+_)

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="feeOK09XEQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-debate...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.