All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refuel.lang.collections.Sliceable.scala Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
package refuel.lang.collections

object Sliceable {

  implicit class ExpandedCollection[T, +C[_] <: Iterable[_]](value: C[T]) {

    /**
      * Decompose the collection into N pieces.
      * When 100 collections are divided into three,
      * {{{
      * sliceDevideBy(3)
      * }}}
      * 100 / 3 + 1 = Process 34 cases at a time
      *
      * @param size denominator.
      * @param func applyment function.
      * @tparam R Result type.
      * @return
      */
    def sliceDivideApply[R](size: Int)(func: Iterable[T] => R): Iterator[R] = {
      val split = value.size / size + 1
      sliceApply(split)(func)
    }

    /**
      * Disassemble N collections.
      *
      * When 100 collections are divided into three,
      * {{{
      *   sliceApply(33)
      * }}}
      *
      * each 33, 33, 33, 1
      *
      * @param size      Cut size.
      * @param applyment applyment function
      * @tparam R Result type.
      * @return
      */
    def sliceApply[R](size: Int)(applyment: Iterable[T] => R): Iterator[R] = {
      value.sliding(size).map { x => applyment(x.toSeq.asInstanceOf[Seq[T]]) }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy