import scalaz._
import scalaz.Scalaz._
import scalaz.effect._
import scalaz.iteratee.Iteratee._
import scala.compat.Platform.EOL
import java.io.IOException
object Split {
type IoEx[A] = IOException \/ A
def chunk(size: Long) = {
type E = ((IoEx[Char], Long), Long)
for (x <- takeUntil[E, List] {
case ((c, _), n) => n > size &&
c.fold(_ => false, _ === EOL(0))
}; _ <- drop[E, Id](EOL.length))
yield x.unzip._1.unzip.bimap(
_.sequenceU.map(_.mkString),
_.reverse.head)
} %= zipWithIndex[(IoEx[Char], Long), Id]
def file[A, B](size: Long, name: String)(
lines: String => A = identity _,
pos: Long => B = identity _) =
(collect[(IoEx[A], B), List] %= chunk(size).map(
_.bimap(_.map(lines), pos)).sequenceI).up[IO] &=
enumReader[IO](io.Source.fromFile(name).reader)
.map(_.fold(_.left, _.right)).zipWithIndex
}
object SplitSample extends App {
Split.file(10, "src/test/resources/test.txt")(
println, println).run.unsafePerformIO()
}