Skip to content
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

Merged
merged 1 commit into from
Oct 29, 2017

Conversation

sohum2002
Copy link

  • 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

@dgouyette
Copy link
Collaborator

dgouyette commented Oct 18, 2017

Thanks for your contribution @sohum2002 . I'll check it ASAP

Copy link
Collaborator

@dgouyette dgouyette left a 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
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Collaborator

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

Copy link
Author

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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sohum2002
Copy link
Author

sohum2002 commented Oct 18, 2017

@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 {
Copy link
Contributor

@loicdescotte loicdescotte Oct 21, 2017

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.

@sohum2002
Copy link
Author

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 :)

@dgouyette
Copy link
Collaborator

dgouyette commented Oct 21, 2017

@sohum2002 Monoids provide a zero element.
So i think , that would help http://eed3si9n.com/learning-scalaz/Monoid.html because orEmpty give us a zero element for a given type

@@ -1,6 +1,6 @@
package io.github.hamsters

object OptionHelper {
object OptionDefaults {
Copy link
Contributor

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]){
Copy link
Contributor

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 :)

@sohum2002
Copy link
Author

@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]){
Copy link
Contributor

@loicdescotte loicdescotte Oct 22, 2017

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)

Copy link
Author

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

Copy link
Collaborator

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Author

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 :)

@loicdescotte
Copy link
Contributor

loicdescotte commented Oct 27, 2017

Nice, thanks :)
Can you git rebase (squash) your branch before the merge, so we have a single commit for this ?

@sohum2002
Copy link
Author

@loicdescotte Squashed and ready to merge :)

@loicdescotte
Copy link
Contributor

Thanks!

@loicdescotte loicdescotte merged commit 24021de into scala-hamsters:master Oct 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants