-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue-35] Added orEmpty and orZero functions for Option #44
Conversation
sohum2002
commented
Oct 18, 2017
- Implemented orEmpty helper for Option[String]
- Implemented orEmpty helper for Option[Iterable[Empty]]
- Implemented orEmpty helper for Option[Numeric]
- Created tests accordingly to test all three functions
Thanks for your contribution @sohum2002 . I'll check it ASAP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thing we should be inspired by cats version which use monoid. Simple and functionnal.
WDYT ?
def orEmpty(implicit A: Monoid[A]): A = oa.getOrElse(A.empty)
optInt.orZero shouldEqual 0 | ||
} | ||
"A none element of Option[Float]" should "return 0f" in { | ||
val optInt: Option[Int] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy/paste ? it's the wrong type
optInt.orZero shouldEqual 0f | ||
} | ||
"A none element of Option[Double]" should "return 0d" in { | ||
val optInt: Option[Int] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy/paste ? it's the wrong type
optString.orEmpty shouldEqual "" | ||
} | ||
"An none element of Option[Iterable]" should "return empty iterable" in { | ||
val optList: Option[List[Any]] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any ? Why it's not T ? It must works with all the type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed that in the latest commit :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not seem to be fixed :
https://github.com/sohum2002/hamsters/blob/issue-35/shared/src/test/scala/OptionHelperSpec.scala#L13
@dgouyette I've made some changes as per your comments, thanks for that! Regarding Monoid's, I'm not too sure how they are used in Scala, will look into it. EDIT: I did a quick search on Monoids and don't think it's suitable to implement this for Monoids. |
@@ -0,0 +1,22 @@ | |||
package io.github.hamsters | |||
|
|||
object OptionHelper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be better to avoid *Helper name for objects :)
The name should explain what the object is doing.
Thank you @loicdescotte for your comments to help improve my pull request. I made the changes you recommended, so when you're free please take a look! Looking forward to it :) |
@sohum2002 Monoids provide a zero element. |
@@ -1,6 +1,6 @@ | |||
package io.github.hamsters | |||
|
|||
object OptionHelper { | |||
object OptionDefaults { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it handles empty values, something like EmptyOptionValues would be better IMO.
@@ -1,6 +1,6 @@ | |||
package io.github.hamsters | |||
|
|||
object OptionHelper { | |||
object OptionDefaults { | |||
implicit class StringHelper(optStr: Option[String]){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other Helper should be renamed too :)
@dgouyette Monoids look like an interesting implementation for hamsters project. However, I think this should be separated out into another issue/feature request? Let me know what you think! |
package io.github.hamsters | ||
|
||
object EmptyOptionValues { | ||
implicit class StringOptionValues(optStr: Option[String]){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringOptionValues should not be plural as there is a single default value for empty Option[String].
Also it should reflect the fact that it's a value for an empty option.
(same comment for other implicit classes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of this name: EmptyStringOptionValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest the creation of trait that represents default value :
trait DefaultValue[+A]{ def get : A }
and put typed implementation in object :
`object DefaultValue{
implicit def intDefaultValue = new DefaultValue[Int] {
override def get = 0
}
implicit def stringDefaultValue = new DefaultValue[String] {
override def get = ""
}
}`
So, in the future, it can be replaced by the monoid default value
And creat class for function orEmpty
`implicit class OrEmpty[A](value :Option[A])(implicit defaultValue : DefaultValue[A]) {
def orEmpty : A = value.getOrElse(defaultValue.get)
}`
So the users can easely extend for other type by implementing DefaultValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a brilliant approach. I'll refactor accordingly :)
Nice, thanks :) |
@loicdescotte Squashed and ready to merge :) |
Thanks! |