xacid: (Default)
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()
}

Profile

xacid: (Default)
xacid

August 2017

S M T W T F S
  123 45
67891011 12
13 141516171819
20212223242526
2728293031  

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 19th, 2017 05:09 pm
Powered by Dreamwidth Studios