3

How do I simplify using the Scala option?

 2 years ago
source link: https://www.codesd.com/item/how-do-i-simplify-using-the-scala-option.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I simplify using the Scala option?

advertisements

I have following code that get parameter and if it is valid ObjectId, converts it to Option[ObjectId] and otherwise, returns None.

I was thinking how it could be simplified, but didn't find anything particularly better.

Note: params.get("desiredImage") is Option[String]

val imageId: Option[ObjectId] = params.get("imageId") match {
  case None => None
  case Some(x) => {
    if (ObjectId.isValid(x)) {
      Some(new ObjectId(x))
    } else {
      None
    }
  }
}


You can use filter:

val imageId: Option[ObjectId] = params.get("imageId")
       .filter (ObjectId.isValid)
       .map(new ObjectId(_))

Tags scala

Related Articles

How do I use the CURLOPT_MAX_RECV_SPEED_LARGE option with pycurl and Python?

I'm trying to use CURL's CURLOPT_MAX_RECV_SPEED_LARGE option with the pycurl library. Here is my test code: import sys import pycurl class Test: def __init__(self): self.contents = '' def body_callback(self, buf): self.contents = self.contents + buf

How do I get jscs to use the --fix option?

Using grunt-jscs, I'd like to have it use the --fix option to automatically reformat code. How can I pass the command-line option to jscs? I've tried adding args & options, but can't get it to work. My gruntfile has: jscs: { all: { src: [ '*.js', ] }

How to use the scala reflection API to get all classes contained

I got this kind of class: trait ThirdParty { def invoke = println("right") } trait WeatherIcon { def invoke = println("wrong") } class MyClass { object objA extends ThirdParty object objB extends WeatherIcon } How can I use the Scala r

How to use the scala distribution library to send a request for publication to the server in the elevator?

I want to use the scala dispatch library to send a post request to the server in the Lift. I want to send a post request to the external server and get some information and then use this information in my web app. How can I do this?The Lift GitHub Wi

How do I prevent the user from using the same option from a drop-down list if they chose it once in java?

I have made a dropdown box and 2 textFields in java, what i want to do is prevent the user from using the same option again, if they have chosen it once already. below is my code for the dropdown menu and textFields. JComboBox comboBox = new JComboBo

Using the 'filter' option in backbone.js paginator requestPager

I currently have a working backbone.js requestPager collection, but would like to use the filter option. I am currently struggling with how to make it work. It works when I just use something link "'filter': 'sam'" in the server_api function, bu

Error 3622 - You must use the dbSeeChanges option with OpenRecordset when accessing a SQL Server table that has an IDENTITY column

I'm running MS Access 2007, connecting to a MS SQL 2008 R2 server. I have a form with a multiselect box that I use to update status for several fields for the servers selected. Currently I'm using the ServerName field in the multiselect box. The prob

Can I use ON DUPLICATE KEY UPDATE with an INSERT query using the SET option?

I've seen the following (using the VALUES option): $query = "INSERT INTO $table (column-1, column-2, column-3) VALUES ('value-1', 'value-2', 'value-3') ON DUPLICATE KEY UPDATE SET column1 = value1, column2 = value2, column3 = value3, ID=LAST_INSERT_I

EF Core Error - No projects were found. Modify the current working directory or use the --project option

I am using Visual Studio 2015 and dotnet core and trying to develop an EF Core Code First project using Sqlite and this documentation / tutorial, which also uses Sqlite => NET Core - New Database When I try to add an initial migration from the comman

Using the -exec option with the Find command in Bash

I am trying to use the -exec option with the find command to find specific files in my massive panoramas directory and move them to a specified location. The command I am using below passes an error argument not found for -exec. Can somebody point ou

How can I not use the application.html.erb file for a particular page in the application?

I want a particular page in the application to have different layout, while all others will have pretty much the same layout. However, the application.html.erb file is rendered for all the pages in application. How do I not use the application.html.e

Can I display the jQuery datepicker more than year when you use the changeYear option?

I am using the jQuery datepicker in an application which will be used, among other things, to insert and update birth dates of employees. I find it cumbersome to have to click several times in order to get to the 70's and 80', decades in which many e

MapReduce: Passing external jar files using the libjars option does not work

My map reduce program needs external jar files. I am using the "-libjars" option to provide those external jar files - I used Tool, Configured and ToolRunner Utilities provided by hadoop. public static void main(String[] args)throws Exception {

How to access and use the stack-trace to help identify a bug

I see a lot of posts from people saying they get error such and such but seem to have no idea how to figure out where the error is coming from. As such I wondered if people know how to access and use the stack trace. SO how do you access it, and what

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK