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

Scala implementation of letter count

8 replies
wimxa
Joined: 2009-01-02,
User offline. Last seen 42 years 45 weeks ago.

Here are two attempts at doing the count of the letters:

object Main {
def main(args: Array[String]) = {
val letters = List("a", "b", "a", "c", "d", "d", "d", "b")
println(count(letters))
println(count2(letters))
}

def count(letters: List[String]): Map[String, Int] = {
def count(letters: List[String], acc: Map[String, Int]): Map[String,
Int] = {
if(letters.length == 0) acc
else count(letters.tail,
acc + Pair(letters.head, acc.getOrElse(letters.head, 0) + 1))
}
count(letters, Map())
}

def count2(letters: List[String]): Map[String, Int] = {
var map = Map[String, Int]()
for(letter <- letters) map += Pair(letter, map.getOrElse(letter, 0) + 1)
map
}
}

Both of them convert a list of letters (represented as strings) into a map
of (letter, number of occurences).

Here are my comments on the above:
- count is too clumsy
- count2 has side effects

What do you think about the above?

Any solution that is "better" in any sense (one of the above or any other,
like being more Scala in nature or so)?

How would you do it if you needed this in Scala? As a matter of fact, other
languages are OK (Haskell, Java, such).

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala implementation of letter count
val letters = List("a", "b", "a", "c", "d", "d", "d", "b")letters.foldLeft[Map[Char, Int]](Map.empty)((m, c) => m + (c -> (m.getOrElse(c, 0) + 1)))

On Fri, Jan 2, 2009 at 6:44 AM, wimxa <wimxa [at] yahoo [dot] com> wrote:

Here are two attempts at doing the count of the letters:

object Main {
 def main(args: Array[String]) = {
   val letters = List("a", "b", "a", "c", "d", "d", "d", "b")
   println(count(letters))
   println(count2(letters))
 }

 def count(letters: List[String]): Map[String, Int] = {
   def count(letters: List[String], acc: Map[String, Int]): Map[String,
Int] = {
     if(letters.length == 0) acc
     else count(letters.tail,
       acc + Pair(letters.head, acc.getOrElse(letters.head, 0) + 1))
   }
   count(letters, Map())
 }

 def count2(letters: List[String]): Map[String, Int] = {
   var map = Map[String, Int]()
   for(letter <- letters) map += Pair(letter, map.getOrElse(letter, 0) + 1)
   map
 }
}

Both of them convert a list of letters (represented as strings) into a map
of (letter, number of occurences).

Here are my comments on the above:
- count is too clumsy
- count2 has side effects

What do you think about the above?

Any solution that is "better" in any sense (one of the above or any other,
like being more Scala in nature or so)?

How would you do it if you needed this in Scala? As a matter of fact, other
languages are OK (Haskell, Java, such).
--
View this message in context: http://www.nabble.com/Scala-implementation-of-letter-count-tp21252187p21252187.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala implementation of letter count

On Fri, Jan 2, 2009 at 3:44 PM, wimxa wrote:
>

> def count2(letters: List[String]): Map[String, Int] = {
> var map = Map[String, Int]()
> for(letter <- letters) map += Pair(letter, map.getOrElse(letter, 0) + 1)
> map
> }
> }
>
> - count2 has side effects

I don't see that it has any side effects, its all isolated within that
function. Call it with the same list, it will always return the same
result and won't change the state of anything outside of it.

I also find it far easier to follow then count.

just my opinion of course :-)

Chris

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Scala implementation of letter count


On Fri, Jan 2, 2009 at 3:44 PM, wimxa <wimxa [at] yahoo [dot] com> wrote:

Here are two attempts at doing the count of the letters:

object Main {
 def main(args: Array[String]) = {
   val letters = List("a", "b", "a", "c", "d", "d", "d", "b")
   println(count(letters))
   println(count2(letters))
 }

 def count(letters: List[String]): Map[String, Int] = {
   def count(letters: List[String], acc: Map[String, Int]): Map[String,
Int] = {
     if(letters.length == 0) acc
     else count(letters.tail,
       acc + Pair(letters.head, acc.getOrElse(letters.head, 0) + 1))
   }
   count(letters, Map())
 }

 def count2(letters: List[String]): Map[String, Int] = {
   var map = Map[String, Int]()
   for(letter <- letters) map += Pair(letter, map.getOrElse(letter, 0) + 1)
   map
 }
}

Both of them convert a list of letters (represented as strings) into a map
of (letter, number of occurences).

Here are my comments on the above:
- count is too clumsy
- count2 has side effects

What do you think about the above?

Any solution that is "better" in any sense (one of the above or any other,
like being more Scala in nature or so)?

How would you do it if you needed this in Scala? As a matter of fact, other
languages are OK (Haskell, Java, such).
--
View this message in context: http://www.nabble.com/Scala-implementation-of-letter-count-tp21252187p21252187.html
Sent from the Scala - User mailing list archive at Nabble.com.


IMHO count2 isn't side-effecting...

--
Viktor Klang
Senior Systems Analyst
wimxa
Joined: 2009-01-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala implementation of letter count

David Pollak-4 wrote:
>
> val letters = List("a", "b", "a", "c", "d", "d", "d",
> "b")letters.foldLeft[Map[Char,
> Int]](Map.empty)((m, c) => m + (c -> (m.getOrElse(c, 0) + 1)))
>
David, thanks! Exactly what I've been looking for!

Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: Scala implementation of letter count
...in which case there would be no harm in building up the map using a mutable map instead of an immutable one, because the function returns Map (as opposed to s.c.immutable.Map or s.c.mutable.Map) and therefore the calling code cannot tell the difference and cannot treat the returned object as a mutable map without performing a cast.

On Fri, Jan 2, 2009 at 9:57 AM, Chris Twiner <chris [dot] twiner [at] gmail [dot] com> wrote:
On Fri, Jan 2, 2009 at 3:44 PM, wimxa <wimxa [at] yahoo [dot] com> wrote:
>

<snip/>

>  def count2(letters: List[String]): Map[String, Int] = {
>    var map = Map[String, Int]()
>    for(letter <- letters) map += Pair(letter, map.getOrElse(letter, 0) + 1)
>    map
>  }
> }
>
> - count2 has side effects

I don't see that it has any side effects, its all isolated within that
function.  Call it with the same list, it will always return the same
result and won't change the state of anything outside of it.

I also find it far easier to follow then count.

just my opinion of course :-)

Chris



--
http://erikengbrecht.blogspot.com/
James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Scala implementation of letter count
letters.removeDuplicates.length

:-)


On Fri, Jan 2, 2009 at 6:44 AM, wimxa <wimxa [at] yahoo [dot] com> wrote:

Here are two attempts at doing the count of the letters:



How would you do it if you needed this in Scala? As a matter of fact, other
languages are OK (Haskell, Java, such).
--
View this message in context: http://www.nabble.com/Scala-implementation-of-letter-count-tp21252187p21252187.html
Sent from the Scala - User mailing list archive at Nabble.com.


wimxa
Joined: 2009-01-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala implementation of letter count

Viktor Klang wrote:
>
> IMHO count2 isn't side-effecting...
>
Viktor, I agree, this was a more general question. Assume count2 has been
inlined into main or something - then somebody could use the map and mutate
it. Just a stylistic question.

wimxa
Joined: 2009-01-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala implementation of letter count

James Iry-2 wrote:
>
> letters.removeDuplicates.length
>
> :-)
>
James, this would not create a map. The output of the program I posted is:
Map(a -> 2, b -> 2, c -> 1, d -> 3)
Map(a -> 2, b -> 2, c -> 1, d -> 3)

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