Access Segment Of Input

Examples for reading sections of an Input object without reading all data in the underlying resource

Limited Fold

Demonstrate the limited/controlled fold of LongTraversable.

The limitFold method allows one to fold over a LongTraversable but terminate the traversal at any arbitrary point. This is not as powerful as the Iteree pattern but is easy to get into if one is familiar with fold


    import scalax.io._

    val in:Input = Resource.fromURLString("file://someFile")

    // add bytes until the fifth encounter of a 5 occurs
    val (fives, sum) = in.bytes.limitFold((0,0)) {
      // By returning End the traversal will stop and the resource closed
      case ((5,sum), 5) => End((5, sum))
      // by returning Continue the traversal will continue
      case ((fives,sum), 5) => Continue((fives + 1, sum + 5))
      // by returning Continue the traversal will continue
      case ((fives,sum), next) => Continue((fives, sum + next))
    }