That formatted well :-(

object EnumTest extends App {
  sealed trait Num { def v: Int}
  case object One extends Num{ val v = 1 }
  case object Two extends Num{ val v = 2 }
  case object Three extends Num{ val v = 3 }

  object Num {
    implicit val NumEnum: scalaz.Enum[Num] = new scalaz.Enum[Num] {
      override def succ(a: Num) = a match { case One => Two case Two =>
Three case Three => One}
      override def pred(a: Num) = a match { case One => Three case Two =>
One case Three => Two}

      override def order(x: Num, y: Num) =
scalaz.Ordering.fromInt(java.lang.Integer.compare(x.v, y.v))

      override val min = Some(One)
      override val max = Some(Three)
    }
  }
  import scalaz.syntax.equal._; import scalaz.std.anyVal._

  def sumVFrom(n: Num): Int = ???

  assert(sumVFrom(One)   === 6)
  assert(sumVFrom(Two)   === 5)
  assert(sumVFrom(Three) === 3)


}



On Tue, Nov 8, 2016 at 10:33 AM, oxbow_lakes <[email protected]> wrote:

> Suppose I have some enumeration with a definite min and max:
>
>
>
> sealed trait Num { def v: Int}
>
> case object One   extends Num { val v = 1 }
>
> case object Two   extends Num { val v = 2 }
>
> case object Three extends Num { val v = 3 }
>
>
>
> I might define the enum instance as follows:
>
>
>
>  object Num {
>
>   implicit val NumEnum: scalaz.Enum[Num] = new scalaz.Enum[Num] {
>
>     override def succ(a: Num) = a match { case One => Two   case Two => Three 
> case Three => One}
>
>     override def pred(a: Num) = a match { case One => Three case Two => One   
> case Three => Two}
>
>
>     override def order(x: Num, y: Num) = 
> scalaz.Ordering.fromInt(java.lang.Integer.compare(x.v, y.v))
>
>
>     override val min = Some(One)
>
>     override val max = Some(Three)
>
>   }
>
>  }
>
>
>
> I wish to perform some state transition (or fold) through the values of
> the enumeration. Let's say I wish to sum the numeric values of the above
> from some given start. How do I do this using the *succState* methods on
> Enum? The comments say that these methods are useful for looping but I'm
> struggling to figure out how.
>
>
>
>  def sumVFrom(n: Num): Int = ??? //TODO: How to implement this?
>
>
>  import scalaz.syntax.equal._; import scalaz.std.anyVal._
>
>  assert(sumVFrom(One)   === 6)
>
>  assert(sumVFrom(Two)   === 5)
>
>  assert(sumVFrom(Three) === 3)
>
>
>
> Any help appreciated!
>
> Cheers,
> Chris
>
> --
> You received this message because you are subscribed to the Google Groups
> "scalaz" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/scalaz.
> For more options, visit https://groups.google.com/d/optout.
>

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

Reply via email to