- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
forall
Fri, 2009-03-06, 14:35
Hello everyone,
I'm new to Scala, and I can't figure out why this doesn't work:
def check(as: SortedSet[Double], bs: SortedSet[Double], tol: Double) = {
val list = as.elements.zip(bs.elements)
list.forall(((a,b)) => (a-b)<tol && (b-a)<tol)
}
It gives me an error "not a legal formal parameter" on the tuple (a,b). So I'm left withlist.forall(t => (t._1-t._2)<tol && (t._2-t._1)<tol)
which isn't as legible...
Any idea?
Gilles.
Fri, 2009-03-06, 19:17
#2
Re: forall
This is a beautiful solution! The problem is ubiquitous; I guess this solution should be somehow popularized in something like "Effective Scala" book.
2009/3/6 martin odersky <martin.odersky@epfl.ch>
2009/3/6 martin odersky <martin.odersky@epfl.ch>
On Fri, Mar 6, 2009 at 2:34 PM, Gilles SCOUVART
<Gilles.SCOUVART@n-side.be> wrote:
> Hello everyone,
>
> I'm new to Scala, and I can't figure out why this doesn't work:
>
> def
>
> check(as: SortedSet[Double], bs: SortedSet[Double], tol: Double) = {
>
> val list = as.elements.zip(bs.elements)
>
> list.forall(((a,b)) => (a-b)<tol && (b-a)<tol)
>
> }
>
> It gives me an error "not a legal formal parameter" on the tuple (a,b). So
> I'm left with
>
> list
>
> .forall(t => (t._1-t._2)<tol && (t._2-t._1)<tol)
>
> which isn't as legible...
>
You can pattern match with `case':
list forall { case (a, b) => (a-b)<tol && (b-a)<tol }
Cheers
Fri, 2009-03-06, 19:37
#3
Re: forall
Quoting martin odersky :
> On Fri, Mar 6, 2009 at 2:34 PM, Gilles SCOUVART
>> (...)
>> list.forall(((a,b)) => (a-b)> (...)
>> It gives me an error "not a legal formal parameter" on the tuple (a,b).
>> (...)
> You can pattern match with `case':
>
> list forall { case (a, b) => (a-b) val (a,b) = tuple; // use a,b )
remains to be asked, why is it patterns aren't allowed as formal
parameters? That'd be nice actually.. (although the partial fun is
also nice & legible)
-Martin
Fri, 2009-03-06, 22:27
#4
Re: forall
It is an irrefutable pattern, so effectively it is a full function with pattern parameters.
On 3/6/09, Martin S. Weber <martin.weber@nist.gov> wrote:
On 3/6/09, Martin S. Weber <martin.weber@nist.gov> wrote:
remains to be asked, why is it patterns aren't allowed as formal parameters? That'd be nice actually.. (although the partial fun is also nice & legible)
-Martin
Fri, 2009-03-06, 22:37
#5
Re: forall
Just to drop in my thoughts. Correct me if I am wrong.
The "case" syntax is great for anonymous functions, but doesn't work very well for methods because you have to retype the function parameters which is really boiler platy. For Example,
def foo1(x :(Int,Float)) = x match { case (i, f) => ... }
def foo2(x :(Int,Float)) = { val (i, f) = x; ... }
Unless I am missing something, in both cases an intermediate name is needed and some boiler plate.
-Arthur
On Fri, Mar 6, 2009 at 4:17 PM, Christian Szegedy <christian.szegedy@gmail.com> wrote:
The "case" syntax is great for anonymous functions, but doesn't work very well for methods because you have to retype the function parameters which is really boiler platy. For Example,
def foo1(x :(Int,Float)) = x match { case (i, f) => ... }
def foo2(x :(Int,Float)) = { val (i, f) = x; ... }
Unless I am missing something, in both cases an intermediate name is needed and some boiler plate.
-Arthur
On Fri, Mar 6, 2009 at 4:17 PM, Christian Szegedy <christian.szegedy@gmail.com> wrote:
It is an irrefutable pattern, so effectively it is a full function with pattern parameters.
On 3/6/09, Martin S. Weber <martin.weber@nist.gov> wrote:
remains to be asked, why is it patterns aren't allowed as formal parameters? That'd be nice actually.. (although the partial fun is also nice & legible)
-Martin
Fri, 2009-03-06, 22:57
#6
Re: forall
I guess you can also write,
scala> def foo1: (Int, Int) => Int = { case (a,b) => a+b }
foo1: (Int, Int) => Int
scala> foo1(1,2)
res0: Int = 3
which could just as well be a val... but then you lose some other conveniences of methods such as overloading, etc.
alex
On Fri, Mar 6, 2009 at 1:33 PM, Arthur Peters <arthur.peters@gmail.com> wrote:
scala> def foo1: (Int, Int) => Int = { case (a,b) => a+b }
foo1: (Int, Int) => Int
scala> foo1(1,2)
res0: Int = 3
which could just as well be a val... but then you lose some other conveniences of methods such as overloading, etc.
alex
On Fri, Mar 6, 2009 at 1:33 PM, Arthur Peters <arthur.peters@gmail.com> wrote:
Just to drop in my thoughts. Correct me if I am wrong.
The "case" syntax is great for anonymous functions, but doesn't work very well for methods because you have to retype the function parameters which is really boiler platy. For Example,
def foo1(x :(Int,Float)) = x match { case (i, f) => ... }
def foo2(x :(Int,Float)) = { val (i, f) = x; ... }
Unless I am missing something, in both cases an intermediate name is needed and some boiler plate.
-Arthur
On Fri, Mar 6, 2009 at 4:17 PM, Christian Szegedy <christian.szegedy@gmail.com> wrote:It is an irrefutable pattern, so effectively it is a full function with pattern parameters.
On 3/6/09, Martin S. Weber <martin.weber@nist.gov> wrote:
remains to be asked, why is it patterns aren't allowed as formal parameters? That'd be nice actually.. (although the partial fun is also nice & legible)
-Martin
On Fri, Mar 6, 2009 at 2:34 PM, Gilles SCOUVART
wrote:
> Hello everyone,
>
> I'm new to Scala, and I can't figure out why this doesn't work:
>
> def
>
> check(as: SortedSet[Double], bs: SortedSet[Double], tol: Double) = {
>
> val list = as.elements.zip(bs.elements)
>
> list.forall(((a,b)) => (a-b)
> }
>
> It gives me an error "not a legal formal parameter" on the tuple (a,b). So
> I'm left with
>
> list
>
> .forall(t => (t._1-t._2)
> which isn't as legible...
>
You can pattern match with `case':
list forall { case (a, b) => (a-b)