This page is no longer maintained — Please continue to the home page at www.scala-lang.org

forall

6 replies
Gilles SCOUVART
Joined: 2009-03-06,
User offline. Last seen 42 years 45 weeks ago.

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...

Any idea?

 

Gilles.

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: forall

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)

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
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>
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

Martin S. Weber
Joined: 2008-12-23,
User offline. Last seen 42 years 45 weeks ago.
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

Christian Szegedy
Joined: 2009-02-08,
User offline. Last seen 42 years 45 weeks ago.
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:


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

Arthur Peters
Joined: 2009-01-09,
User offline. Last seen 42 years 45 weeks ago.
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:
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


Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
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:
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



Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland