Last active
August 29, 2015 14:03
-
-
Save Mortimerp9/f07af13a9d012d6886b1 to your computer and use it in GitHub Desktop.
Create ranges from any monoid in scala (with Algebird Monoid)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MonoidRange { | |
case class MonoidRangeBuilder[T](from: T, to: T, incl: Boolean)(implicit mo: Monoid[T], ordering: Ordering[T]) { | |
def by(step: T) = Stream.iterate(from) { previous: T => | |
previous + step | |
}.takeWhile { x => | |
if (incl) { | |
ordering.lteq(x, to) | |
} else { | |
ordering.lt(x, to) | |
} | |
} | |
} | |
implicit class RichMonoidRange[T](wrap: T)(implicit mo: Monoid[T], ordering: Ordering[T]) { | |
def until(incl: T) = MonoidRangeBuilder(wrap, incl, false) | |
def to(incl: T) = MonoidRangeBuilder(wrap, incl, true) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
until: IndexedSeq[com.twitter.util.Duration] = | |
Vector(10.seconds, 12.seconds, 14.seconds, 16.seconds, 18.seconds) | |
to: IndexedSeq[com.twitter.util.Duration] = | |
Vector(10.seconds, 12.seconds, 14.seconds, 16.seconds, 18.seconds, 20.seconds) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MonoidRange._ | |
import com.twitter.util.Duration | |
import java.util.concurrent.TimeUnit | |
implicit val durationMondoid = new Monoid[Duration] { | |
override def zero: Duration = Duration(0, TimeUnit.SECONDS) | |
override def plus(l: Duration, r: Duration): Duration = { | |
l + r | |
} | |
} | |
import com.twitter.conversions.time._ | |
val until = 10.seconds until 20.seconds by 2.seconds toIndexedSeq | |
val to = 10.seconds to 20.seconds by 2.seconds toIndexedSeq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment