Change the if-else construct in a more functional way?

Is it possible to change this if-else construct in a more functional Scala style?

def getMIMEType(document: String): String = { if (document.endsWith(".pdf")) { return "application/pdf" } else if (document.endsWith(".dxf")) { return "application/dxf" } else if (document.endsWith(".jpg")) { return "image/jpeg" } else return "application/octet-stream" } 

I tried using pattern matching, but it does not work. Therefore, I am interested to know about a good solution.

Thanks in advance!

Pongo

+6
source share
3 answers

MIME type mapping

I agree with @glowcoder. I believe that, at least in this particular case, it is better to have a MIME type mapping. Here is the Scala version:

 val MimeTypesMapping = Map( ".pdf" -> "application/pdf", ".dxf" -> "application/dxf", ".jpg" -> "image/jpeg" ) def extension(fileName: String) = fileName substring (fileName lastIndexOf ".") def getMIMEType(document: String) = MimeTypesMapping get extension(document) getOrElse "application/octet-stream" 

In addition, as Rex Kerr noted, you can add a default value directly to the display definition:

 val MimeTypesMapping = Map( ".pdf" -> "application/pdf", ".dxf" -> "application/dxf", ".jpg" -> "image/jpeg" ) withDefaultValue "application/octet-stream" 

and then use it like this: MimeTypesMapping(extension(document))

Pattern matching

If you don't like the first solution, you can use pattern matching instead:

 def getMIMEType(document: String) = extension(document) match { case ".pdf" => "application/pdf" case ".dxf" => "application/dxf" case ".jpg" => "image/jpeg" case _ => "application/octet-stream" } 

The advantage of this solution is that you can use more complex logic in conditions such as:

 def getMIMEType(document: String) = extension(document) match { // ... case ".jpg" | ".jpeg" => "image/jpeg" // ... } 

Comparison with custom extractor

There is also another way to use pattern matching. You can write your own extractor to expand the file, and then use it in match :

 object Extension { def unapply(fileName: String): Option[String] = { val idx = fileName lastIndexOf "." if (idx != -1) Some(fileName substring idx) else None } } def getMIMEType(document: String) = document match { case Extension(".pdf") => "application/pdf" case Extension(".dxf") => "application/dxf" case Extension(".jpg") => "image/jpeg" case _ => "application/octet-stream" } 
+35
source

I don’t know exactly what scala built-in structures are, but it looks like you want to get a map. For example, Java uses

 val map = scala.collection.mutable.HashMap[String, String]() map.put(".pdf","application/pdf"); map.put(".dxf","application/dxf"); map.put(".jpg","image/jpeg"); 

Then in your function you use

 def ext = getExtension(document); // assume this exists, eh? if(map.containsKey(ext)) return map.get(ext); return "application/octet-stream"; 

Update: it looks like scala uses a very similar map:

http://www.scala-lang.org/api/rc/scala/collection/mutable/Map.html

+5
source

Along with my previous comment, I would like to optimize it to make it scala actual.

  def getMIMEType(document: String): String = { if (document endsWith ".pdf") { return "application/pdf" } if (document endsWith ".dxf") { return "application/dxf" } if (document endsWith ".jpg") { return "image/jpeg" } "application/octet-stream" } 

I see here that the code cannot satisfy either of the two conditions at a time. Therefore, I would suggest using the above code. In addition, if for the last return statement, that is, "application / octet-stream", you can use the correct "if condition", if you print "if" for the last line, then all the returned data can be deleted even higher, the code above .

Also, you do not need to specify the type of the return value.

  def getMIMEType(document: String): String = {} 

can be written as,

  def getMIMEType(document: String) = {} 

check it out if it works for you.

0
source

Source: https://habr.com/ru/post/900625/


All Articles