score:2
pattern matching is a good option to make dr
a val
, especially if you need to test a
for multiple different values.
val a = 10
val dr = a match {
case 10 => true
case _ => false
}
print(dr)
case 10 => true
this essentially says "if a matches 10, then assign true to dr"
case _ = false
this says "if none of the above case(s) matched a then assign false to dr"
note that dr
will be a boolean
type in the above sample, which is much better than using a string
to represent true/false. if you really need a string for some reason then you would do.
val dr = a match {
case 10 => "true"
case _ => "false"
}
print(dr)
score:1
if you use a val, you're not allowed to assign it a new value, so in your code example above, if you switch the third line with
val fa2 = fa.:+(ra)
then fa can be a val.
score:1
the pattern matching is probably the best style for this case, but if you wanted the if else
you could write is as this. note in scala when the if
statement is evaluated it returns a result like any other code block.
val a = 10
val dr = if (a == 10) {
"true"
}
else {
"false"
}
println("dr: " + dr)
score:1
i'll just add, that perhaps instead of representing dr
as a string it would be better to type it as a boolean
, and then you can simply evaluate it to the result of the condition:
val a = 10
val dr = a == 10
println("dr: " + dr)
score:4
in scala, if/else
is an expression that returns a value.
val dr = if (condition) value1 else value2
Source: stackoverflow.com
Related Query
- Swap out "var" from Scala code and use "val" instead of "var"
- How to use val instead of var in below Scala Code
- scala how to use val instead of var
- Scala - Use DFS for detecting a cycle? My code is buggy, and I can't seem to figure out why
- I want to create variable from dataframe and need to use in spark scala code
- is there a way to write coalesce query to remove null from a file and use in scala code with a filter after extracting from a file?
- Use of def, val, and var in scala
- Scala Process - Capture Standard Out and Exit Code
- How to use Scala varargs from Java code
- Instantiate a Scala class from Java, and use the default parameters of the constructor
- why use def and val in Scala or vice versa
- scala's mutable and immutable set when to use val and var
- Scala 22param limit trying to find a workaround and still use for comprehensions instead of plain SQL in Slick
- What is the best way to use python code from Scala (or Java)?
- Scala Play Websocket - use one out actor to send both: Array[Byte] and String messages
- Scala internals of var and val
- How to use AES Encryption and Decryption from Scala to Python
- Scala val and var related question and scala requiring me to assign a value at declaration
- Access code file and line number from Scala macro?
- Traits: Interop between Scala and Java, accessing val from Java
- Why is the each iteration parameter val and not var in for loop in scala
- Akka: defining an actor in Scala with non-default constructor and creating it from Java code
- How can I use memcached (Amazon elasticache) from both scala and rails simultaneously?
- scala -> use .net (linq) and java code base in the same program?
- Get date out of year and day of year from a value - Scala
- Mimicking C# out and ref in Scala -- ready to use features?
- How to use scala and html code inside single block
- Scala statements and Expression - Var vs Val
- I want to convert all my existing UDTFs in Hive to Scala functions and use it from Spark SQL
- How can I use the Scala REPL to test java code - java and scala REPL giving different outputs
More Query from same tag
- Is it a good practice to use Set instead of Seq every times a sequence without duplicates is needed?
- Literal identifier in back ticks in scala
- How to convert an UUID into a MongoDB oid
- Syntax and meaning of a Scala/Play! code sample
- JSON Schema Parsing to get column names as per the schema order
- Empty partial function in Scala
- Spark: how to iterate over spark arrayType as an expression
- slick: what type do I need get back to iterate though a list in a view
- Scala type bounding - no error being thrown?
- how to write a dataframe from scala to HDFS as csv
- Scala IDE Search Dialog
- How to send custom data beween parent and child akka actor?
- Is it possible/advisable to have a different supervision strategy for different children of an Akka 2 actor?
- Deciphering Scala Errors When Returning Functions From Functions
- Play: migration 2.5 to 2.6: error ProvisionException
- Get http headers with akka routing dsl
- How to run WordCount example from Streaming SQL (throws NoSuchMethodError)?
- Wiki xml parser - org.apache.spark.SparkException: Task not serializable
- Spark to kafka connection using ssl connection
- json deserializer with support for parameterized Case Classes
- How do I get the first letter in each word in scala?
- Transform input stream to key-values pairs stream
- Why doesn't Option inherit from Seq and Set?
- How to get number of null for each row?
- Broadcast Message to Routees in a ClusterRouter in Akka
- Diode. Send more than 1 action in circuit
- Scala regex find/replace with additional formatting
- Macro Paradise 2.11 versus 2.10
- Scala Syntax Partial
- Scala StringLike split method creates extra double quotes for leading spaces