All Questions
Tagged with option-type scala
134
questions
1
vote
2
answers
87
views
Create a spark dataset that has additional option fields using an existing dataset
I have a case class as follows:
case class student_address(studentId:String, address:String)
The values from a csv are read into the dataset - studentAddressDS, which is mapped to the above case ...
0
votes
1
answer
99
views
How to flatten if-else-if-statements with caching in Scala 3?
I have the following very ugly nesting of if-statements that involve slow operations (tryFindResult and tryFindOtherResult) that I need to execute as little as possible. Just to give some context, it'...
3
votes
1
answer
95
views
Scala `for - yield` gives Option of Tuple, but need Tuple of Option's
object main {
def main(args: Array[String]) = {
val x: Option[Int] = Some(2)
val y: Option[Int] = Some(3)
val z: Option[Int] = Some(5)
val result1 = for {
x <- x
y &...
1
vote
1
answer
58
views
Scala for-comprehension and Option
object main {
def main(args: Array[String]) = {
val x = Option(2)
val y = None // works with: Option.empty[Int]
val z = Option(5)
val result1 = for {
x <- x
y <- y
...
0
votes
2
answers
64
views
Null safe way to deoption and avoid type mismatch
While giving some readability advice here and checking that my rewrite hadn't broken anything, I discovered it was already broken.
case class Foo (
bars: Seq[Bar]
)
case class Bar(s: String)
def ...
0
votes
2
answers
32
views
Is there a more functional way to transform an Option value of None?
I have an Option[Int] and I want to transform None to Some(0).
I know I can do this with pattern matching. Is there anything more direct or simple?
thanks
2
votes
0
answers
123
views
Scala 3 with strict equality and explicit nulls - Proper way to deal with pattern matching and Option
To make this code compile (Scala 3.2.2 with -language:strictEquality and -Yexplicit-nulls):
val cause: Option[Throwable] = None
cause match
case c: Some[Throwable] => c.value
case None => ...
-1
votes
1
answer
103
views
Scala: What's the best way to filter and flatten Seq[Option[A]]
Let's say I have a val allStrs: Seq[Option[String]] = Seq(Some("A"), Some("B"), None)
And would like to have a result like this: Seq("A", "B"), what's the most ...
0
votes
1
answer
570
views
Scala: do something if get the value in getOrElse
If a variable is an Option[Account], and there is a string field called accountName in the class Account.
e.g:
val allAccounts: Set[Option[Account]] = Set(Some(Account1), Some(Account2), None)
How do ...
0
votes
1
answer
314
views
trying 2 save data cassandra in springboot with optional keyword,as the field may contain null values,its giving me InvalidDataAccessApiUsageException
Exception in thread "Thread-10" org.springframework.dao.InvalidDataAccessApiUsageException: Unknown type [class java.util.Optional] for property [product] in entity [in.happysellers.hdb.gold....
2
votes
1
answer
516
views
What is the advantage of Option/Maybe Monad over Functor?
I understand the advantage of IO monad and List Monad over Functor, however, I don't understand the advantage of Option/Maybe Monad over Functor.
Is that simply the integration of types of the ...
2
votes
0
answers
108
views
Option type overhead for primitive types
Does the Scala Option type incur an overhead of two objects per primitive type value? In other words, is the Option type specialized for primitives, or does it always double-box them (I'm counting ...
1
vote
1
answer
222
views
What is the idiomatic (and fast) way of treating the empty list/Seq as failure in a short-circuiting operation?
I have a situation where I am using functions to model rule applications, with each function returning the actions it would take when applied, or, if the rule cannot be applied, the empty list. I have ...
0
votes
1
answer
80
views
Handle Future[Option[T]] when traversing a List
def getCommentIds(
commentTargetId: Long,
sortOrder: CommentOrderEnum): Future[Seq[CommentStatsBO]]
def getCommentDetail(commentId: Long): Future[Option[CommentDetailDTO]]
def ...
1
vote
2
answers
183
views
Scala Option.fold Behaves differently in and out of function
Take the simple function:
def makeUpper(input: Option[String]): String =
input.fold("b"){ _.toUpperCase }
makeUpper(Some("a")) // A
makeUpper(None) // b
This is as ...