use None or EmptyMyObj in scala? [closed] - scala

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I understand the concept of None
I also understand how nice it is to have MyObj and also MyEmptyObj but i cant figure out when its better to use None instead of MyEmptyObj and when I should be using MyEmptyObj instead of None. Also I tend not to like to see methods that return Option[MyObj] it clutters my code I prefer to return MyObj and then seamlessly call its methods such as MyObj.toJson and thus MyEmptyObj.toJson will know to represent itself rather than having case None: return some empty json what do you think about this whole subject?
To the other side I can say that None goes very well with flatMap etc, so which to choose?
when to None
and when to Empty?

None is great when you have no default value, but if you have a default, by all means use it.
As a very simple example, if you wanted to define a function called amountOwed, you could do this:
def amountOwed(bill: Int, alreadyPaid: Option[Int]): Option[Int] = {
val owed = bill - alreadyPaid.getOrElse(0)
if(owed == 0) None // nothing to pay!
else Some(owed)
}
But that's much more complex (and annoying) than it needs to be, since 0 makes perfect sense as both a "haven't paid anything" and "don't owe anything":
def amountOwed(bill: Int, alreadyPaid: Int): Int = {
bill - alreadyPaid
}

Related

trait good practice to save Scala constants? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
Is a trait a good way to put together all the constants that will be used by certain classes?
Or is there maybe a better way to handle constants, like a package object?
Singleton objects can contain fields, like any other object. If a constant exists on its own, it makes sense to put it in a singleton. Don't make a singleton just for all constants, but if they're logically grouped together, then you could pretty easily make an object with a good name.
object Calendar {
val DaysInYear = 365
val DaysInWeek = 7
val EpochYear = 1970
// ... etc ...
}
Package objects are deprecated in Scala 3 and set to be removed in the future. If you're already using Scala 3, then you can just put individual constants at the top-level of a file. Otherwise, throwing them in a singleton is the most future-proof way to code right now.

Calculating the sum of two numbers with a for in loop [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
i'm extremely new to swift but i have previous experience through college in other programming languages so im familiar with the logistics. however my college professor asked us to do something rather weird that i would have used an Array for, and without one i have no clue how to do what he is asking.
The question is as states:
Create a closure that accepts two Ints and returns an Int (the sum of both int's provided)
but to retrieve the sum, you are supposed to use a For-In Loop?
any help or even a push in the right direction would be greatly appreciated.
my current code to solve this issue is something extremely simple.
var returnSum = { (a: Int, b: Int) -> Int in
var sum: Int
sum = (one + two)
return sum
}
But i feel like if i hand that in im going to be getting a one very bad mark.
Thank you!
The code can simply be written as,
var returnSum = { $0 + $1 }
And since there is nothing sequence like here, a loop makes no sense.

scala get vs match which one to use, given that restult will always be same type? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Given that match will never be None,
Which style is better or can I improve the first?
val tmp = (cols.find(_(0) == id).get)
SomeClass(tmp(0), tmp(1))
cols.find(_(0) == id) match {
case Some(value)=> SomeClass(value(0), value(1))
case None=> NotFound("Given id not found")
}
Since this a question about style, my answer is that neither of these is the best style. Instead, just keep the value in the Option
val opt: Option[SomeClass] = cols.find(_(0) == id).map(v => SomeClass(v(0), v(1)))
Keep processing/testing inside the Option using foreach/exists etc. until you really need the bare value. There is a very rich set of methods on Option that covers most of the things that are needed.
If the rest of the code is structured well, you will likely find that the value never needs to be extracted in a separate operation.

When we should have argument label in Swift function design, and when we should omitting argument label? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
According to https://docs.swift.org/swift-book/LanguageGuide/Functions.html
We can design our function as
With argument label
func someFunction(firstParameterName: Int, secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(firstParameterName: 1, secondParameterName: 2)
Or
Omitting argument label
func someFunction(_ firstParameterName: Int, _ secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(1, 2)
Is there any rule-of-thumb, or best practice we should follow? So that, we know when we should have argument label, and when we should omitting argument label, when come to designing function?
In terms of naming in Swift, you should check out API Design Guidelines. These guidelines will give you a general feel of how to name things in Swift. Fair warning though, these rules are rather theoretical and at the end you are to decide how to name your functions in specific cases. Don't get too hung up on this really, even the most experienced developers have troubles with this, so I've been told.
In your particular situation you definitely shouldn't omit argument labels because type information is very generic and doesn't provide any clue as to what you're passing in.
Hope this helps!

When to use inferred type vs explicit types in swift? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Taking a look at Swift and I was wondering people's opinions on when to take advantage of inferred types? Coming from Obj-C/C I'm used to doing types all the time so that's currently how I'm writing my Swift code but there must be some reasons to use type inference? I've seen this answer, When to exploit type inference in Haskell? but it's about Haskell.
Thanks!
My initial gut tells me that anytime you immediately or shortly declare the value of the variable it's probably preferable to allow type inference to kick in. As a more general rule, I'd say anytime things are made more readable (subjective I know) go for it.
Things like the below I believe greatly improve from type inference.
let hello = "Hello"
vs
let hello: String = "Hello"
and a much better example:
let add = {
(a1: Int, a2: Int) -> Int in
return a1 + a2
}
vs
let add: (Int, Int) -> Int = {
(a1: Int, a2: Int) -> Int in
return a1 + a2
}