monad-like pseudo container for single value

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

monad-like pseudo container for single value

Anthony Cros
I often find myself using a container that looks something like this (especially in the context of metaprogramming):

  case class PseudoContainer[A](private val a: A) { // this is probably not a very good implementation of it, this is mostly for illustration purpose
    def get: A = a
    def map[B](f: A => B): PseudoContainer[B] = PseudoContainer(f(a))
    def flatMap[B](f: A => PseudoContainer[B]): PseudoContainer[B] = PseudoContainer(f(a).a)
    def filter(p: A => Boolean): Option[A] = if (p(a)) Some(a) else None
  }


For instance it helps me process a single value, an optional value or a sequence in similar ways.

I was wondering:

1. what people thought about it (beside the horrible name)?
2. if there were a more canonical such container defined elsewhere already?

Thanks!

Anthony

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: monad-like pseudo container for single value

Vlad Patryshev
I'd remove "Pseudo" from its name.

But actually, if you admit that values are not always available, Option would be a better option.

Thanks,
-Vlad

On Fri, Jan 27, 2017 at 12:13 PM, Anthony Cros <[hidden email]> wrote:
I often find myself using a container that looks something like this (especially in the context of metaprogramming):

  case class PseudoContainer[A](private val a: A) { // this is probably not a very good implementation of it, this is mostly for illustration purpose
    def get: A = a
    def map[B](f: A => B): PseudoContainer[B] = PseudoContainer(f(a))
    def flatMap[B](f: A => PseudoContainer[B]): PseudoContainer[B] = PseudoContainer(f(a).a)
    def filter(p: A => Boolean): Option[A] = if (p(a)) Some(a) else None
  }


For instance it helps me process a single value, an optional value or a sequence in similar ways.

I was wondering:

1. what people thought about it (beside the horrible name)?
2. if there were a more canonical such container defined elsewhere already?

Thanks!

Anthony

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: monad-like pseudo container for single value

William Harvey
In reply to this post by Anthony Cros
Would the identity monad from scalaz do the job?  http://eed3si9n.com/learning-scalaz/Id.html

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: monad-like pseudo container for single value

Oliver Ruebenacker

     Hello,

  How about just using scala.Some? What's the reason for using a container?

     Best, Oliver

On Fri, Jan 27, 2017 at 4:30 PM, William Harvey <[hidden email]> wrote:
Would the identity monad from scalaz do the job?  http://eed3si9n.com/learning-scalaz/Id.html

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: monad-like pseudo container for single value

Anthony Cros
Thanks for the responses!

Vlad, that's the thing, in that case I know there will *always* be a value, but i want to be able to process like I do Option/Seq

Oliver, it'd work well if it weren't for the fact that all its operations return an 'Option[A]' and not a 'Some[A]' (but aside from filtering, I don't want to leave my "one-and-only-one-value-container")

William, I still find scalaz pretty intimidating actually, but it looks like it may do the trick indeed! I'll have a closer o

On Friday, January 27, 2017 at 4:32:08 PM UTC-5, Oliver Ruebenacker wrote:

     Hello,

  How about just using scala.Some? What's the reason for using a container?

     Best, Oliver

On Fri, Jan 27, 2017 at 4:30 PM, William Harvey <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZucUj-OmBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">harv...@...> wrote:
Would the identity monad from scalaz do the job?  <a href="http://eed3si9n.com/learning-scalaz/Id.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Feed3si9n.com%2Flearning-scalaz%2FId.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHx6y4dQO6ahoDOw2qQIt4KOljEkw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Feed3si9n.com%2Flearning-scalaz%2FId.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHx6y4dQO6ahoDOw2qQIt4KOljEkw&#39;;return true;">http://eed3si9n.com/learning-scalaz/Id.html

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZucUj-OmBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, <a href="http://www.type2diabetesgenetics.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;">Diabetes Portal, <a href="http://www.broadinstitute.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;">Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: monad-like pseudo container for single value

Vincent Marquez
I agree, Id is a good idea in this case.

Scalaz can definitely be intimidating, but remember that it is a library and not a framework, so it's perfectly fine to ignore the 'confusing' parts and just use what you find useful.  That's definitely what I did for the first year or so I first got into using it.  If you have scalaz specific questions you can always ask me (and other scalaz contributors) here, or on twitter (@runT1ME), or in the #scalaz channel on IRC/Freenode.


http://eed3si9n.com/learning-scalaz/7.0/

--Vincent

On Fri, Jan 27, 2017 at 4:22 PM, Anthony Cros <[hidden email]> wrote:
Thanks for the responses!

Vlad, that's the thing, in that case I know there will *always* be a value, but i want to be able to process like I do Option/Seq

Oliver, it'd work well if it weren't for the fact that all its operations return an 'Option[A]' and not a 'Some[A]' (but aside from filtering, I don't want to leave my "one-and-only-one-value-container")

William, I still find scalaz pretty intimidating actually, but it looks like it may do the trick indeed! I'll have a closer o

On Friday, January 27, 2017 at 4:32:08 PM UTC-5, Oliver Ruebenacker wrote:

     Hello,

  How about just using scala.Some? What's the reason for using a container?

     Best, Oliver

On Fri, Jan 27, 2017 at 4:30 PM, William Harvey <[hidden email]> wrote:
Would the identity monad from scalaz do the job?  http://eed3si9n.com/learning-scalaz/Id.html

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.