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

Why would a 'for' require a definition of 'map'?

6 replies
Kenneth McDonald
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.

Here's the error message:

MBP:scalaapplication1 Ken$ scalac rex.scala
rex.scala:265: error: value map is not a member of
rex.MatchResultIterator
val sequence = for(m <- Lit("a").findAllMatchesIn("aabbaba"))
yield m.group(0)

and here is the definition of MatchResultIterator:

class MatchResultIterator(val matches:Iterator[Regex.Match], val
matcher:Matcher) {
var lastEnd = 0
def hasNext = matches.hasNext
def next = matches.next
}

I'm probably just being obtuse, but I can't see the need for a 'map'
definition here.

Thanks,
Ken

Tony Morris
Joined: 2008-12-19,
User offline. Last seen 30 weeks 4 days ago.
Re: Why would a 'for' require a definition of 'map'?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Your code is expanded to:

Lit("a").findAllMatchesIn("aabbaba").map(m => m.group(0))

A more thorough explanation about lifting into a monad might help, but
it might not, so I won't.

Kenneth McDonald wrote:
> Here's the error message:
>
> MBP:scalaapplication1 Ken$ scalac rex.scala
> rex.scala:265: error: value map is not a member of
> rex.MatchResultIterator
> val sequence = for(m <-
> Lit("a").findAllMatchesIn("aabbaba")) yield m.group(0)
>
> and here is the definition of MatchResultIterator:
>
> class MatchResultIterator(val matches:Iterator[Regex.Match], val
> matcher:Matcher) {
> var lastEnd = 0
> def hasNext = matches.hasNext
> def next = matches.next
> }
>
> I'm probably just being obtuse, but I can't see the need for a 'map'
> definition here.
>
> Thanks,
> Ken
>
>
>

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkl7s3wACgkQmnpgrYe6r61ZWQCdHLZHG1QrUhc5TKu6BO+TeiZn
z90AoJdWZAECI/eyvdq9ZoJRIS3LB3Ul
=K4kB
-----END PGP SIGNATURE-----

Florian Hars
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Why would a 'for' require a definition of 'map'?

Kenneth McDonald schrieb:
> I'm probably just being obtuse, but I can't see the need for a 'map'
> definition here.

Because for is defined that way. To use for, you must implement
map, flatMap and filter. In scala for is not a loop, but a
comprehension: http://www.scala-lang.org/node/111

- Florian.

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: Why would a 'for' require a definition of 'map'?

Florian Hars wrote:
> Kenneth McDonald schrieb:
>> I'm probably just being obtuse, but I can't see the need for a 'map'
>> definition here.
>
> Because for is defined that way. To use for, you must implement
> map, flatMap and filter. In scala for is not a loop, but a
> comprehension: http://www.scala-lang.org/node/111

Note you won't need to explicitly implement map, filter, flatMap,
foreach if your iterator inherits from Iterator. You've already
implemented its only abstract methods (hasNext, next).

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Re: Why would a 'for' require a definition of 'map'?
Perhaps it's java.util.Iterator and not scala.Iterator?

On Sun, Jan 25, 2009 at 4:44 AM, Eric Willigers <ewilligers [at] gmail [dot] com> wrote:
Florian Hars wrote:
Kenneth McDonald schrieb:
I'm probably just being obtuse, but I can't see the need for a 'map' definition here.

Because for is defined that way. To use for, you must implement
map, flatMap and filter. In scala for is not a loop, but a comprehension: http://www.scala-lang.org/node/111

Note you won't need to explicitly implement map, filter, flatMap, foreach if your iterator inherits from Iterator. You've already implemented its only abstract methods (hasNext, next).




--
Viktor Klang
Senior Systems Analyst
Florian Hars
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Why would a 'for' require a definition of 'map'?

Viktor Klang schrieb:
> Perhaps it's java.util.Iterator and not scala.Iterator?

He just called his class SomethingIterator without extending anything,
so he didn't inherit any methods. In effect, all that's missing is an
"extends Iterator" in his class definition.

- Florian

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Re: Why would a 'for' require a definition of 'map'?
Haha,

marvellous,

That's what I get for wrongly assuming the most basic stuff is already present. ;D


On Sun, Jan 25, 2009 at 10:02 AM, Florian Hars <hars [at] bik-gmbh [dot] de> wrote:
Viktor Klang schrieb:
Perhaps it's java.util.Iterator and not scala.Iterator?

He just called his class SomethingIterator without extending anything,
so he didn't inherit any methods. In effect, all that's missing is an "extends Iterator" in his class definition.

- Florian



--
Viktor Klang
Senior Systems Analyst

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