"com.mysql.jdbc. Driver This sim..." />

Scala string pattern matching best practice?

In terms of syntax, you can modify just a tiny bit you case statements: case url if url. StartsWith("jdbc:mysql:") => "com.mysql.jdbc. Driver This simply binds the value url to the pattern expression (which is also url ) and adds a guard expression with a test.

That should make the code compile To make it a little bit more scala-like, you can return an OptionString (I removed a couple clause since it's just for illustration): def resolveDriver(url: String) = url match { case you if u. StartsWith("jdbc:mysql:") => Some("com.mysql.jdbc. Driver") case you if u.

StartsWith("jdbc:postgresql:") => Some("org.postgresql. Driver") case _ => None } That is unless you want to manage exceptions.

In terms of syntax, you can modify just a tiny bit you case statements: case url if url. StartsWith("jdbc:mysql:") => "com.mysql.jdbc. Driver" This simply binds the value url to the pattern expression (which is also url) and adds a guard expression with a test.

That should make the code compile. To make it a little bit more scala-like, you can return an OptionString (I removed a couple clause since it's just for illustration): def resolveDriver(url: String) = url match { case you if u. StartsWith("jdbc:mysql:") => Some("com.mysql.jdbc.

Driver") case you if u. StartsWith("jdbc:postgresql:") => Some("org.postgresql. Driver") case _ => None } That is unless you want to manage exceptions.

Thanks! That's exactly what I was looking for! I'm glad I asked the question 'cuz I was already preparing myself to create a case class for that, which smelled like an overcomplication.

Also I thank you for correcting me on the Exception throwing. – mojojojo Oct 8 at 19:21.

Here is an alternate way. Store all the mappings in a map and then use collectFirst method to find the match. Type signature of collectFirst is: def TraversableOnceA.

CollectFirstB(pf: PartialFunctionA, B): OptionB Usage: scala> val urlMappings = Map("jdbc:mysql:" -> "com.mysql.jdbc. Driver", "jdbc:postgresql:" -> "org.postgresql. Driver") urlMappings: scala.collection.immutable.Mapjava.lang.

String,java.lang. String = Map(jdbc:mysql: -> com.mysql.jdbc. Drive r, jdbc:postgresql: -> org.postgresql.

Driver) scala> val url = "jdbc:mysql:somestuff" url: java.lang. String = jdbc:mysql:somestuff scala> urlMappings collectFirst { case(k, v) if url startsWith k => v } res1: Optionjava.lang. String = Some(com.mysql.jdbc.

Driver).

– mojojojo Oct 8 at 19:21 @mojojojo: Not quite. The set of case expressions that follows match constitutes a PartialFunction. CollectFirst is a method that accepts a PartialFunction, loops over the collection, and returns the first match found as wrapped in Some.

(returns None if no match found. ) – missingfaktor Oct 8 at 19:28 @mojojojo: See the source: goo.Gl/Q4UNz – missingfaktor Oct 8 at 19:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions