Scala Map#get and the return of Some()
Asked Answered
V

5

30

Im using scala Map#get function, and for every accurate query it returns as Some[String]

IS there an easy way to remove the Some?

Example:

def searchDefs{
    print("What Word would you like defined? ")
    val selection = readLine
    println(selection + ":\n\t" + definitionMap.get(selection))
  }

When I use this method and use the following Input:

What Word would you like defined? Ontology

The returned Value is:

Ontology:
    Some(A set of representational primitives with which to model a domain of knowledge or discourse.)

I would like to remove the Some() around that.

Any tips?

Vanburen answered 22/2, 2012 at 6:7 Comment(2)
See also: #9364750Loo
The wonderful thing about Option is that it forces you to handle the None case.Niigata
O
50

There are a lot of ways to deal with the Option type. First of all, however, do realize how much better it is to have this instead of a potential null reference! Don't try to get rid of it simply because you are used to how Java works.

As someone else recently stated: stick with it for a few weeks and you will moan each time you have to get back to a language which doesn't offer Option types.

Now as for your question, the simplest and riskiest way is this:

mymap.get(something).get

Calling .get on a Some object retrieves the object inside. It does, however, give you a runtime exception if you had a None instead (for example, if the key was not in your map).

A much cleaner way is to use Option.foreach or Option.map like this:

scala> val map = Map(1 -> 2)
map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)

scala> map.get(1).foreach( i => println("Got: " + i))
Got: 2

scala> map.get(2).foreach( i => println("Got: " + i))

scala> 

As you can see, this allows you to execute a statement if and only if you have an actual value. If the Option is None instead, nothing will happen.

Finally, it is also popular to use pattern matching on Option types like this:

scala> map.get(1) match {
     |  case Some(i) => println("Got something")
     |  case None => println("Got nothing")
     | }
Got something
Ominous answered 22/2, 2012 at 6:15 Comment(2)
mymap(something) looks prettier.Tamikotamil
@elbowich: Yes, mymap(something) seems to be a good alternative when you know that something is certainly contained in the map.Elenor
P
3

I personally like using .getOrElse(String) and use something like "None" as a default i.e. .getOrElse("None").

Poss answered 11/1, 2019 at 2:4 Comment(0)
Q
2

In modern scala you can just map(key)

Quassia answered 20/10, 2022 at 9:3 Comment(0)
A
2

The apply method on the Map can be used to retrieve the value without the Option wrapper, throwing an exception if the key does not exist.

val map = Map(1 -> 2)
map(1) // 2
map(2) // NoSuchElementException

def apply(key: K): V
Retrieves the value which is associated with the given key. This method invokes the default method of the map if there is no mapping from the given key to a value. Unless overridden, the default method throws a NoSuchElementException.

Use getOrElse to return a default value when the key is not found (instead of throwing an exception).

val map = Map(1 -> 2)
map.getOrElse(1, -1) // 2
map.getOrElse(2, -1) // -1
Amalamalbena answered 25/9, 2023 at 2:8 Comment(0)
F
1

I faced similar issue, replaced with .Key() to resolve. Solution: definitionMap(selection)

Farkas answered 30/7, 2021 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.