All Questions
Tagged with optional or option-type
3,832 questions
0
votes
1
answer
161
views
Passing nullable struct into view in SwiftUI [duplicate]
Note that I do not agree that my question is a duplicate of the linked question, even though the title would appear to imply it is. The accepted answer to that question is to force unwrap Binding<...
3
votes
1
answer
100
views
Why is the getOrElse() function of arrow-kt returning Any?
I am trying to understand the getOrElse() function of arrow-kt. I want to create a simple function that takes a list of strings, filters them, and returns the first matching value as an Option<...
1
vote
1
answer
105
views
Replacing Arrow's deprecated orElse with getOrElse doesn't work
I'm working on migrating some existing Kotlin code from Arrow 1.x to 2.x. The old code looked something like this:
fun filterStrings(incoming: List<String>): Option<String> = incoming
....
0
votes
2
answers
239
views
How is Maybe a monad in Haskell?
A monad, I've been told, is a monoid of X in the category of endofunctors of X, where X is some category.
Maybe is supposedly then a monoid, which means that it is: an object of a category, some ...
1
vote
2
answers
85
views
`Option.defaultValue` not working properly in f# when raising exception
I am trying to combine Option.defaultValue with failWith to get exception for None values:
printf "%A" (Some 1 |> Option.defaultValue (failwith "Error"))
But this code raise ...
7
votes
1
answer
108
views
With &mut self, why can't this method move and replace a field's value?
I'm trying to implement a binary search tree. The parent struct, BinSearchTree, has a pointer to a root node:
pub struct BinSearchTree {
root: Option<Box<Node>>,
size: u32,
}
...
1
vote
1
answer
101
views
How to downcast any List or any Option in rust
I want to add properties window with auto properties based on struct fields. I'am downcasting every field, but for Vec<> and Option<> I have to duplicate all code, how can I automatically ...
0
votes
2
answers
83
views
conditional map fetch efficiently
I have a map which lays down prices of commodities in different currencies
val commpricemap: Map[String , Map[String, Double]] = ???
AN example of an entry for gold is as below:
("AU" -> ...
1
vote
2
answers
101
views
Filter an optional sequence
I have a Option sequence of dates - from which I need to SAFELY extract the first date after a given date( or else return the given date).
The below seems to be a lot of code for such a simple used ...
1
vote
1
answer
148
views
I can't get an optional with (cond) ? value : nullopt ... what should I write instead?
Suppose I'm implementing the function returning an std::optional<T>. In that function, I compute some condition, and then want to return either a value, or a nullopt. I can do it like so:
std::...
0
votes
1
answer
244
views
How can I "merge" two options, keeping one if only one exists? [duplicate]
I find myself regularly encountering the need for logic where two optional values should be "merged" where:
if both are None -> give me None
if only one is Some -> give me Some of ...
0
votes
1
answer
119
views
In Standard ML, are datatype constructors considered values?
My working definition for value is a special kind of expression that evaluates to itself. So 1 is a value, and 1 + 1 is valuable but not a value because it evaluates to 2.
A datatype constructor like ...
3
votes
2
answers
202
views
Why doesn't MonadMaybe exist?
The way I look at MonadState, for instance, is that any type (or set of types, e.g. ReaderT r m a) that implements it, must support get+put (or alternatively just state) in order to behave like the ...
3
votes
1
answer
84
views
Why does Array's contains(_:) method cause an error when comparing an optional value with a non-optional value in Swift?
I’m working with Swift and encountered an issue when using the contains method on an array. The following code works fine:
let result = ["hello", "world"].contains(Optional("...
1
vote
4
answers
92
views
How can I find the only matching element in a Seq[A] as an Option[A], throwing an exception if there is more than one matching element?
In some test code, I have a Seq[A] that I'm filtering down to items matching a given property. There should be at most one such element in the sequence. If it has more, it means I set up something ...
