Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:43:15+00:00 2026-05-26T12:43:15+00:00

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

  • 0

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

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 to use pattern matching, but it doesn’t work. So I be curious for a good solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:43:16+00:00Added an answer on May 26, 2026 at 12:43 pm

    MIME-Type mapping

    I agree with @glowcoder. I find that at least in this particular case it’s better to have MIME-Types mapping. Here is a 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"
    

    Also, as Rex Kerr noted, you can add default value directly to the mapping 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 first solution, then 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"
    }
    

    Advantage of this solution is that you can can more sophisticated logic in the conditions like for example:

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

    Pattern Matching with Custom Extractor

    There is also another way to use pattern matching. You can write your own extractor for the file extension an 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"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to change this code: @Override public boolean onKeyDown(int keyCode, KeyEvent event)
Is this possible to change bundle structure in Symfony 2, for example: MyBundle EntityName
Is it possible to change PowerPacks.LineShape smoothingMode? I tried to use this code(a class
Using JQuery it possible to clone a Div like this and change add a
Is this possible? example: $('a.change').click(function(){ //code to change p tag to h5 tag });
Possible Duplicate: Can you animate a height change on a UITableViewCell when selected? This
Ok, not sure if this is even possible, but I want to change the
LINQ's AsParallel returns ParallelQuery . I wonder if it's possible to change this behavior
I'm learning scala for a new project, aiming for immutability and functional style wherever
If possible please i'd ask a favor, i've been trying to change this it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.