Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Map#get and the return of Some()

Tags:

map

scala

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?

like image 624
meriley Avatar asked Feb 22 '12 06:02

meriley


People also ask

What is a Scala map?

Scala map is a collection of key/value pairs. Any value can be retrieved based on its key. Keys are unique in the Map, but values need not be unique. Maps are also called Hash tables. There are two kinds of Maps, the immutable and the mutable.

What map function does in Scala?

The map function is applicable to both Scala's Mutable and Immutable collection data structures. The map method takes a predicate function and applies it to every element in the collection. It creates a new collection with the result of the predicate function applied to each and every element of the collection.

Is Scala map a HashMap?

Scala HashMap is used to store objects and it take the object in the form of key value pair. For every value there should be one key associated with it. Scala collection contains this Hashmap and it is the implementation of MAP.

How do I read a Scala map?

Scala Map get() method with exampleThe get() method is utilized to give the value associated with the keys of the map. The values are returned here as an Option i.e, either in form of Some or None. Return Type: It returns the keys corresponding to the values given in the method as argument.


2 Answers

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 
like image 161
Frank Avatar answered Sep 23 '22 11:09

Frank


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

like image 21
Albaro Pereyra Avatar answered Sep 20 '22 11:09

Albaro Pereyra