skedule icon indicating copy to clipboard operation
skedule copied to clipboard

Iterable starting from "same"

Open rocketraman opened this issue 4 years ago • 0 comments

It would be nice if it was possible to call a variation of the iterate method that starts at the "same" instant as the timestamp.

Since I am using Kotlin, and want a Sequence rather than an Iterator I am working around this with the following extensions:

/**
 * Similar to `asSequence` on [ScheduleIterator] but calls `nextOrSame` instead of `next` to obtain the
 * first element.
 */
fun <T> Schedule.ScheduleIterator<T>.asSequenceStartingFromNextOrSame(): Sequence<T> = generateSequence({ nextOrSame() }) {
  next()
}

fun Schedule.asSequence(timestamp: ZonedDateTime, startingFromSame: Boolean = false): Sequence<ZonedDateTime> {
  val scheduleIterator = iterate(timestamp)
  return if(startingFromSame) scheduleIterator.asSequenceStartingFromNextOrSame() else scheduleIterator.asSequence()
}

rocketraman avatar Jan 05 '21 15:01 rocketraman