Wrap method vs chain method
up vote
0
down vote
favorite
Imagine we have the following:
sealed trait Foo
case class FullFoo[A](foo: A) extends Foo
case object EmptyFoo extends Foo
and
def liftToFoo[A](opt: Option[A]): Foo =
opt.map(a => FullFoo(a)).getOrElse(EmptyFoo)
We can easily do:
liftToFoo(Some(123)) // FullFoo(123)
liftToFoo(None) // EmptyFoo
But I'm curious if there's some "chainable" way to call this (rather than wrapping the method like above):
Some(123).someFunction(liftToFoo) // FullFoo(123)
scala
add a comment |
up vote
0
down vote
favorite
Imagine we have the following:
sealed trait Foo
case class FullFoo[A](foo: A) extends Foo
case object EmptyFoo extends Foo
and
def liftToFoo[A](opt: Option[A]): Foo =
opt.map(a => FullFoo(a)).getOrElse(EmptyFoo)
We can easily do:
liftToFoo(Some(123)) // FullFoo(123)
liftToFoo(None) // EmptyFoo
But I'm curious if there's some "chainable" way to call this (rather than wrapping the method like above):
Some(123).someFunction(liftToFoo) // FullFoo(123)
scala
I'm trying to avoid using an implicit class to achieve this style. Curious if there's something I can use already.
– user451151
Nov 11 at 0:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Imagine we have the following:
sealed trait Foo
case class FullFoo[A](foo: A) extends Foo
case object EmptyFoo extends Foo
and
def liftToFoo[A](opt: Option[A]): Foo =
opt.map(a => FullFoo(a)).getOrElse(EmptyFoo)
We can easily do:
liftToFoo(Some(123)) // FullFoo(123)
liftToFoo(None) // EmptyFoo
But I'm curious if there's some "chainable" way to call this (rather than wrapping the method like above):
Some(123).someFunction(liftToFoo) // FullFoo(123)
scala
Imagine we have the following:
sealed trait Foo
case class FullFoo[A](foo: A) extends Foo
case object EmptyFoo extends Foo
and
def liftToFoo[A](opt: Option[A]): Foo =
opt.map(a => FullFoo(a)).getOrElse(EmptyFoo)
We can easily do:
liftToFoo(Some(123)) // FullFoo(123)
liftToFoo(None) // EmptyFoo
But I'm curious if there's some "chainable" way to call this (rather than wrapping the method like above):
Some(123).someFunction(liftToFoo) // FullFoo(123)
scala
scala
asked Nov 11 at 0:34
user451151
23119
23119
I'm trying to avoid using an implicit class to achieve this style. Curious if there's something I can use already.
– user451151
Nov 11 at 0:41
add a comment |
I'm trying to avoid using an implicit class to achieve this style. Curious if there's something I can use already.
– user451151
Nov 11 at 0:41
I'm trying to avoid using an implicit class to achieve this style. Curious if there's something I can use already.
– user451151
Nov 11 at 0:41
I'm trying to avoid using an implicit class to achieve this style. Curious if there's something I can use already.
– user451151
Nov 11 at 0:41
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
Not exactly what you're looking for but perhaps a little closer than what you've got.
Some(123).fold(EmptyFoo:Foo)(FullFoo(_))
A) Yeah, but you said you were "trying to avoid an implicit class". B)Success()
?Failure()
? That's aTry
, not aFoo
.
– jwvh
Nov 11 at 1:42
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Not exactly what you're looking for but perhaps a little closer than what you've got.
Some(123).fold(EmptyFoo:Foo)(FullFoo(_))
A) Yeah, but you said you were "trying to avoid an implicit class". B)Success()
?Failure()
? That's aTry
, not aFoo
.
– jwvh
Nov 11 at 1:42
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
add a comment |
up vote
2
down vote
Not exactly what you're looking for but perhaps a little closer than what you've got.
Some(123).fold(EmptyFoo:Foo)(FullFoo(_))
A) Yeah, but you said you were "trying to avoid an implicit class". B)Success()
?Failure()
? That's aTry
, not aFoo
.
– jwvh
Nov 11 at 1:42
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
add a comment |
up vote
2
down vote
up vote
2
down vote
Not exactly what you're looking for but perhaps a little closer than what you've got.
Some(123).fold(EmptyFoo:Foo)(FullFoo(_))
Not exactly what you're looking for but perhaps a little closer than what you've got.
Some(123).fold(EmptyFoo:Foo)(FullFoo(_))
answered Nov 11 at 1:33
jwvh
24.8k52038
24.8k52038
A) Yeah, but you said you were "trying to avoid an implicit class". B)Success()
?Failure()
? That's aTry
, not aFoo
.
– jwvh
Nov 11 at 1:42
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
add a comment |
A) Yeah, but you said you were "trying to avoid an implicit class". B)Success()
?Failure()
? That's aTry
, not aFoo
.
– jwvh
Nov 11 at 1:42
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
A) Yeah, but you said you were "trying to avoid an implicit class". B)
Success()
? Failure()
? That's a Try
, not a Foo
.– jwvh
Nov 11 at 1:42
A) Yeah, but you said you were "trying to avoid an implicit class". B)
Success()
? Failure()
? That's a Try
, not a Foo
.– jwvh
Nov 11 at 1:42
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Re: implicits, yeah totally! Lol I hacked together this "Foo" example, but was really thinking of applying it with a Try :P You caught me in some Frankenstein copy/paste :)
– user451151
Nov 11 at 1:51
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
Thanks again for your feedback and help!
– user451151
Nov 11 at 1:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244784%2fwrap-method-vs-chain-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm trying to avoid using an implicit class to achieve this style. Curious if there's something I can use already.
– user451151
Nov 11 at 0:41