8

I have looked at these links

http://blog.danielwellman.com/2008/03/using-scalas-op.html

http://blog.tmorris.net/scalaoption-cheat-sheet/

I have a map of [String, Integer] and when I do a map.get("X") I get an option. I would like the following.

val Int count = map.get(key); 
// If the key is there I would like value if it is not I want 0

How do I achieve this in one line? I need to do this several times. It looks a bit inefficient to write a function everytime for doing this. I am sure there is some intelligent one line quirk that I am missing but I really like to get the value into an integer in ONE line :)

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Kannan Ekanath
  • 16,759
  • 22
  • 75
  • 101

3 Answers3

18

Just use getOrElse method:

val count: Int = map.getOrElse(key,0);

Note also, that in Scala you write type after name, not before.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • The problem I got is the type is still Any instead of being an Integer. map.getOrElse(key,0) returns a type of Any – user2441441 Jun 08 '20 at 06:05
6

@om-nom-nom (classic screen name) has the correct answer, but in the interest of providing yet another way

val count = map.get(key) fold(0)(num => num)

Before in-the-know users bash me with, "Option has no fold!", fold has been added to Option in Scala 2.10

getOrElse is of course better in the current case, but in some Some/None scenarios it may be interesting to 1-liner with fold like so (edited complements of @Debiliski who tested against latest 2.10 snapshot):

val count = map.get(k).fold(0)(dao.userlog.count(_))

I suppose in 2.9.2 and under we can already do:

val count = map get(k) map ( dao.userlog.count(_) ) getOrElse(0)

Which is to say, in Scala there is often more than one way to do the same thing: in the linked thread, the OP shows more than 10 alternative means to achieve Option fold ;-)

virtualeyes
  • 11,147
  • 6
  • 56
  • 91
  • yah, I got excited too when I came across the scala-lang thread ;-) Hopefully 2.10 M3 will be released soon after Scala Days conference finishes, a lot to look forward to in Scala.Next – virtualeyes Apr 17 '12 at 11:25
  • 1
    @virtualeyes: That would be `map.get(key).fold(0)(num => num)` in 2.10 (at least going by the current nightly). – Debilski Apr 17 '12 at 12:37
  • @Debilski, right, was going with OP's approach in the scala-lang thread; there was a lot of back & forth over the order (some, none) or (none, some); looks like Paul Phillips went with (none)(some) based on his latest commit "https://github.com/scala/scala/commit/bb4935e92c". I'll edit my answer so at not mislead further ;-) thanks – virtualeyes Apr 17 '12 at 12:59
  • "fold(0)(num => num)" is a bit annoying, "fold(0)(x)" would be wonderfully concise... – virtualeyes Apr 17 '12 at 13:06
  • This makes no sense: `(none => 0, num => num)`. There's no value to pass in the `None` case, so folding it doesn't take a function. – Daniel C. Sobral Apr 17 '12 at 13:44
  • @Debiliski, set that straight, but neglected to fix in the first example, corrected now... – virtualeyes Apr 17 '12 at 13:51
  • @virtualeyes: If you want to set a single `x` in case of a `Some` (regardless of the `Some`s value), you don’t need a `fold` anyway. And if all you have is `fold(0)(x => x)` or `fold(0)(identity)`, then a `getOrElse` is more concise anyway. – Debilski Apr 17 '12 at 16:21
  • indeed, for the getOrElse case. I was more pointing out (or trying to) a possible use case for fold with Option similar to folding over an Either, where one performs an operation based on the left (none) or right (some) condition; that is, vs. simply returning a default value. – virtualeyes Apr 17 '12 at 16:51
  • *in Scala there is often more than one way to do the same thing*. That really scare me sometimes: I don't want Scala to become new Perl. – om-nom-nom Apr 19 '12 at 11:56
  • @om-nom-nom too late, Martin O wants to simplify, but the Scala train has already left the station, all aboard ;-) I would describe Scala as refined Perl with added hieroglyphic symbolism, the overmind (_++_) being one of my favorites. SBT and FP take things to an entirely different level, from the outside looking in, no wonder Scala has the "complex" stigma. A few months into learning Scala, I absolutely love it, but the beginning was chock full of WTFs – virtualeyes Apr 19 '12 at 12:32
3

Yet another way.

import scalaz._, Scalaz._

scala> val m = Map(9 -> 33)
m: scala.collection.immutable.Map[Int,Int] = Map(9 -> 33)

scala> m.get(9).orZero
res3: Int = 33

scala> m.get(8).orZero
res4: Int = 0
missingfaktor
  • 90,905
  • 62
  • 285
  • 365