We need to create an request query-string, from a case class. The case class contains optional attributes:
case class Example(..., str: Option[String], ..)
We want to create a query-parameter if the option exists, and no query parameter otherwise. Like:
match example.str {
case Some(s) => s"¶m_str=$s"
case _ => ""
}
as this appearing at numerous places I want it to make a bit more generic:
def option2String(optionString: Option[String], template: String) = {
optionString match {
case Some(str) => template.replaceAll("\\$str", str)
case _ => ""
}
But I think it can be done more elegant or scala idiomatic, may be with call-by-name arguments?
I would use fold
example.str.fold("")("¶m_str=" + _)
If you have multiple parameters you can try this:
List(
str1.map("¶m1_str=" + _),
str2.map("¶m2_str=" + _),
str3.map("¶m3_str=" + _)
).flatten.mkString(" ")
Related
I have the following function definition:
private def extractUrl: String => (String, String)
= url =>
url
.split("/")
.toList
.filter(_.startsWith("localhost"))
.flatMap(e => e.split(":").toList)
.foldLeft[(String, String)](("", "")) { (acc, x) =>
acc match {
case ("", "") => (x, "")
case (a, "") => (a, x)
case z => z
}
}
the question is, is there another way to define an empty Tuple instead of ("", "")?
Empty tuple
("", "") is a tuple of empty strings with type (String, String).
Empty is unclear in that context, could be (None, None) or even (null, null) (bad)
You seem to use "" to represents a value that is not present. Try using None and Some[String], both sub types of Option[String], to indicate that a value is not present.
Analysis and comments
Potentially, your method seems not to do what is intended. (execute code below)
Think about using already present functions / methods / libraries for manipulating URLs (also see example below)
Think about using Option
object Fun {
import java.net.URL
def main(args: Array[String]): Unit = {
val url1 = "http://localhost:4000/a/b/c?x=1&y=2#asdf"
val url2 = "http://example.com:4000/a/localhostb/c?x=1&y=2#asdf"
val urls = List(url1, url2)
// your approach
println("Your approach")
urls.map( urlString => extractUrl(urlString ))
.foreach(println)
println("New approach")
urls.map(x => extractUrl2(x))
.filter( x => x.host.startsWith("localhost") )
.foreach(println)
}
case class HostPort(host: String, port: Option[String])
def extractUrl2: String => HostPort = urlString => {
val url = new URL(urlString)
HostPort(url.getHost,
url.getPort match {
case -1 => None
case i => Some(i.toString)
})
}
def extractUrl: String => (String, String) = url =>
url
.split("/")
.toList
.filter(_.startsWith("localhost"))
.flatMap(e => e.split(":").toList)
.foldLeft[(String, String)](("", "")) { (acc, x) =>
acc match {
case ("", "") => (x, "")
case (a, "") => (a, x)
case z => z
}
}
}
yields
Your approach
(localhost,4000)
(localhostb,)
New approach
HostPort(localhost,Some(4000))
I don't think it is possible to define an empty Tuple.
I tried to use (->) but that resolves to a Tuple2.type and not a Tuple2.
If the values of your Tuple are optional, use the type system to express that:
(Option[String], Option[String])
Better yet, you could define a case class for your data structure:
case class HostAndPort(host: Option[String], port: Option[String])
This would provide default values for each type within the tuple:
let my_tuple: (String, usize) = Default::default();
How can rewrite the following to make it more 'Scala way' or use just one match?
case class Foo(bar: Any)
val fooOpt = Some(Foo("bar as String"))
def isValid(p: Any) = p match {
case _ # (_: String | _: Int) => true
case _ => false
}
//Is it possible to check for the type of bar directly in this if statement?
fooOpt match {
case Some(f) if isValid(f.bar) => doSomething
case _ => doSomethingElse
}
One alternative would be using the isInstanceOf.
fooOpt match {
case Some(f) if f.bar.isInstanceOf[String] => doSomething
case Some(f) if f.bar.isInstanceOf[Int] => doSomething //could also rewrite to use just one case
case _ => doSomethingElse
}
Is there other way?
This can all be done in one big pattern match:
fooOpt match {
case Some(Foo(_: Int | _: String)) => doSomething
case _ => doSomethingElse
}
If you want to get the Int or String out, just split that case:
fooOpt match {
case Some(Foo(i: Int)) => doSomething
case Some(Foo(s: String)) => doSomething
case _ => doSomethingElse
}
Is there other way?
Although the solution with one big patten match works(and can be used if you really can't change bar to anything more specific than Any), it is not a proper 'Scala way' of dealing with this situations in general if you have control over Foo.
A better way would be to make Foo generic:
case class Foo[T](bar: T)
And have either a generic doSomething, if it can work with any particular T:
def doSomething[T](foo: Foo[T]): SomeType = ???
or to have different versions of it for different possible T's you have, if it should react on them differently:
def doSomethingWithString(foo: Foo[String]): SomeType = ???
def doSomethingWithInt(foo: Foo[Int]): SomeType = ???
Then you can use it just like this:
val fooOpt = Some(Foo("bar as String"))
fooOpt.map(doSomething).orElse(doSomethingElse)
or like this:
val fooOptString = Some(Foo("bar as String"))
fooOptString.map(doSomethingWithString).orElse(doSomethingElse)
val fooOptInt = Some(Foo(1))
fooOptInt.map(doSomethingWithInt).orElse(doSomethingElse)
So, in this case compiler checks types for you, answering to:
Is it possible to check for the type of bar directly?
And in many situations you can avoid using pattern match at all, using methods like map, orElse, etc. with proper typing. This might be an answer for this:
could also rewrite to use just one case
I have a function that as a parameter takes an object and if it is of the correct type I need to access the last element in an Option[List[Int]]. I have a working solution but it seems clumsy. In the case that there are not any items in obj.listOfThings I will need to have i have the value 0. Is there a better way to achieve this?
val i = foo match {
case obj: Bar =>
obj.listOfInts match {
case Some(ints) =>
ints.last
case _ =>
0
}
case _ =>
0
}
Technically it could return an Option[Int]. I'm still pretty new to Scala and would like to learn better approaches to this sort of problems.
In your case initially it seems that what Ende Neu suggested is the right way to go:
val i = foo match {
case obj: Bar =>
obj.listOfInts.map(_.last /* This throws an exception when the list is empty*/).getOrElse(0)
case _ =>
0
}
But if you look into it you'll see that you have a bug in your code, in the case that that obj.listOfInts is Some(Nil), because in that case you get a NoSuchElementException for trying to call last on an empty List.
Try this code with foo = Bar(Some(Nil)) and see for yourself.
When you use Option[List] think very carefully if this is what you want.
Usually after some thinking you will scrap the Option and just stay with a List because the Option serves no purpose.
I worked with many developers who misuse Option[List] because of not understanding the similarities between Nil and None and usually the 'None' case ends up playing the same role as Some(Nil)
So you end up having to do this:
optionalList match {
case None => // do something
case Some(list) =>
list match {
case Nil => // do the same thing
case head::tail => // do other stuff
}
}
As you can see the None case and the Some(Nil) case are basically the same.
To fix your bug you should do:
case class Bar(listOfInts: Option[List[Int]])
val i = foo match {
case Bar(Some(list)) if list != Nil => list.last
case _ => 0
}
You probably want to use flatMap and lastOption here:
obj.listOfInts.flatMap(_.lastOption)
In case listOfInts is None, or it is Some(Nil), this will return None. Otherwise it will return the last element. If you want to return 0 instead of None, just use getOrElse:
obj.listOfInts.flatMap(_.lastOption).getOrElse(0)
If you wanted to use a match, you could do:
obj.listOfInts match {
case Some(list#(hd::tl)) => list.last
case _ => 0
}
Here, the hd::tl guarantees that list is not empty. Another option is use a conditional match:
obj.listOfInts match {
case Some(list) if list.nonEmpty => list.last
case _ => 0
}
Or to match the None and Some(Nil) cases first:
obj.listOfInts match {
case None | Some(Nil) => 0
case Some(list) => list.last
}
As suggested in the comments, I think the best way to go is:
val i = foo match {
case obj: Bar => obj.listOfInts.map(_.last).getOrElse(0)
case _ => 0
}
More concise way including the instanceof:
scala> case class B(is: Option[List[Int]])
defined class B
scala> def f(x: Any) = Option(x) collect { case b: B => b.is flatMap (_.lastOption) } flatten
f: (x: Any)Option[Int]
scala> f(B(Option(5 to 7 toList)))
res0: Option[Int] = Some(7)
or
scala> import PartialFunction.{ condOpt => when }
import PartialFunction.{condOpt=>when}
scala> def g(x: Any) = when(x) { case b: B => b.is flatMap (_.lastOption) } flatten
g: (x: Any)Option[Int]
scala> g(B(Option(5 to 7 toList)))
res1: Option[Int] = Some(7)
It's probably worth asking why you lost static type info, that you need to pattern match.
Having a List[String] and several parsers I want to pattern match each String from the List to parsers. So it'd look like this (Warning, pseudo-code):
myStringList.map{
case MyParser.keyword => keyword match {
case KeywordParser.keyword1 => //it's special keyword1
case KeywordParser.keyword2 => //special treatment for keyword2
case NotSpecial => //it's a usual command
}
case MyParser.stringValue => //etc...
}
Why would I want to do so?
I'm parsing a simple script line, that contains "strings" and $(keywords). Some of the keywords are special and need to be treated separately.
Currently I have only one special keyword, so I'm using chained parseAll and match, but that doesn't feel right.
So, how it can be done?
It's not really supported, but you can do it like this:
val parseKeyword: PartialFunction[String, ... ] {
case KeywordParser.keyword1 => //it's special keyword1
case KeywordParser.keyword2 => //special treatment for keyword2
case NotSpecial => //it's a usual command
}
myStringList.map{
case x#MyParser.keyword if parseKeyword.isDefinedAt(x) => parseKeyword(x)
case MyParser.stringValue => //etc...
}
Bet I had a short-circuit in my brain, because I woke up next morning and realised, that all I need is a couple of parsers, some case classes and a pair of pattern matchers. So the solution to my problem looks like this (pseudo-code):
case class SpecialKeyword1(value: String)
case class SpecialKeyword2(value: String)
...
case class SpecialKeywordN(value: String)
case class SimpleKeyword(value: String)
object MyParser extends JavaTokenParsers{
def expression: Parser[Any] = ...
def keyword1: Parser[SpecialKeyword1] = ...
def keyword2: ...
...
def simplekeyword: Parser[SimpleKeyword] = ...
}
object Main {
def parseScript(inputScript: String): String = {
MyParser.parseAll(MyParser.expression, inputScript) match {
case Success(result, _) => {
case SpecialKeyword1(val) => ...
case SpecialKeyword2(val) => ...
...
case SimpleKeyword(val) => ...
case str: String => ...
}
case Failure(_, _) => "Parsing Failure"
case Error(_, _) => "Parsing Error"
}
}
}
If I'm doing lots of pattern matching against a (relatively) complex case class, but most of the time I'm only interested in one or two of its fields. Is there a way to abstract away the other fields (perhaps by wrapping the class?)? Here's an example of the type of thing I'm trying to simplify:
def receive = {
case HttpRequest(POST, "foo", _, HttpBody(_, body), _) => // action
case HttpRequest(GET, "bar", _, _, _) => // action
}
I'm only ever really interested in the request type, url and sometimes body so I would ideally like to define a pattern match as case Request(POST, "foo", body) or similar.
Just make your own Request extractor. Here's a simplified example:
case class Complex(a: String, b: Int, c: String)
object Simple {
def unapply(c: Complex): Option[(String, Int)] = Some(c.a, c.b)
}
Complex("B", 2, "x") match {
case Simple("A", i) => println("found A, " + i)
case Simple("B", i) => println("found B, " + i)
}
// prints "found B, 2"