Class IOFunctions

java.lang.Object
fj.data.IOFunctions

public final class IOFunctions extends Object
IO monad for processing files, with main methods enumFileLines(File, Option, Iteratee.IterV), enumFileChars(File, Option, Iteratee.IterV) and enumFileCharChunks(File, Option, Iteratee.IterV) (the latter one is the fastest as char chunks read from the file are directly passed to the iteratee without indirection in between).
  • Field Details

    • DEFAULT_BUFFER_SIZE

      private static final int DEFAULT_BUFFER_SIZE
      See Also:
    • closeReader

      public static final F<Reader,IO<Unit>> closeReader
    • ioUnit

      public static final IO<Unit> ioUnit
    • stdinBufferedReader

      public static final BufferedReader stdinBufferedReader
  • Constructor Details

    • IOFunctions

      private IOFunctions()
  • Method Details

    • toTry

      public static <A> Try0<A, IOException> toTry(IO<A> io)
    • p

      public static <A> P1<Validation<IOException, A>> p(IO<A> io)
    • fromF

      public static <A> IO<A> fromF(F0<A> p)
    • fromTry

      public static <A> IO<A> fromTry(Try0<A, ? extends IOException> t)
    • toSafe

      public static <A> SafeIO<A> toSafe(IO<A> io)
      Convert io to a SafeIO, throwing any IOException wrapped inside a RuntimeException
      Parameters:
      io -
    • runSafe

      public static <A> A runSafe(IO<A> io)
      Run io, rethrowing any IOException wrapped in a RuntimeException
      Parameters:
      io -
    • closeReader

      public static IO<Unit> closeReader(Reader r)
    • enumFileLines

      public static <A> IO<Iteratee.IterV<String,A>> enumFileLines(File f, Option<Charset> encoding, Iteratee.IterV<String,A> i)
      An IO monad that reads lines from the given file (using a BufferedReader) and passes lines to the provided iteratee. May not be suitable for files with very long lines, consider to use enumFileCharChunks(File, Option, Iteratee.IterV) or enumFileChars(File, Option, Iteratee.IterV) as an alternative.
      Parameters:
      f - the file to read, must not be null
      encoding - the encoding to use, Option.none() means platform default
      i - the iteratee that is fed with lines read from the file
    • enumFileCharChunks

      public static <A> IO<Iteratee.IterV<char[],A>> enumFileCharChunks(File f, Option<Charset> encoding, Iteratee.IterV<char[],A> i)
      An IO monad that reads char chunks from the given file and passes them to the given iteratee.
      Parameters:
      f - the file to read, must not be null
      encoding - the encoding to use, Option.none() means platform default
      i - the iteratee that is fed with char chunks read from the file
    • enumFileChars

      public static <A> IO<Iteratee.IterV<Character, A>> enumFileChars(File f, Option<Charset> encoding, Iteratee.IterV<Character, A> i)
      An IO monad that reads char chunks from the given file and passes single chars to the given iteratee.
      Parameters:
      f - the file to read, must not be null
      encoding - the encoding to use, Option.none() means platform default
      i - the iteratee that is fed with chars read from the file
    • bufferedReader

      public static IO<BufferedReader> bufferedReader(File f, Option<Charset> encoding)
    • fileReader

      public static IO<Reader> fileReader(File f, Option<Charset> encoding)
    • bracket

      public static <A,B,C> IO<C> bracket(IO<A> init, F<A,IO<B>> fin, F<A,IO<C>> body)
    • unit

      public static <A> IO<A> unit(A a)
    • lazy

      public static <A> IO<A> lazy(F0<A> p)
    • lazy

      public static <A> IO<A> lazy(F<Unit,A> f)
    • lazySafe

      public static <A> SafeIO<A> lazySafe(F<Unit,A> f)
    • lazySafe

      public static <A> SafeIO<A> lazySafe(F0<A> f)
    • lineReader

      public static <A> F<BufferedReader, F<Iteratee.IterV<String,A>, IO<Iteratee.IterV<String,A>>>> lineReader()
      A function that feeds an iteratee with lines read from a BufferedReader.
    • charChunkReader

      public static <A> F<Reader, F<Iteratee.IterV<char[],A>, IO<Iteratee.IterV<char[],A>>>> charChunkReader()
      A function that feeds an iteratee with character chunks read from a Reader (char[] of size DEFAULT_BUFFER_SIZE).
    • charChunkReader2

      public static <A> F<Reader, F<Iteratee.IterV<Character, A>, IO<Iteratee.IterV<Character, A>>>> charChunkReader2()
      A function that feeds an iteratee with characters read from a Reader (chars are read in chunks of size DEFAULT_BUFFER_SIZE).
    • map

      public static <A,B> IO<B> map(IO<A> io, F<A,B> f)
    • as

      public static <A,B> IO<B> as(IO<A> io, B b)
    • voided

      public static <A> IO<Unit> voided(IO<A> io)
    • bind

      public static <A,B> IO<B> bind(IO<A> io, F<A,IO<B>> f)
    • when

      public static IO<Unit> when(Boolean b, IO<Unit> io)
    • unless

      public static IO<Unit> unless(Boolean b, IO<Unit> io)
    • sequence

      public static <A> IO<List<A>> sequence(List<IO<A>> list)
      Evaluate each action in the sequence from left to right, and collect the results.
    • sequence

      public static <A> IO<Stream<A>> sequence(Stream<IO<A>> stream)
    • join

      public static <A> IO<A> join(IO<IO<A>> io1)
    • toSafeValidation

      public static <A> SafeIO<Validation<IOException, A>> toSafeValidation(IO<A> io)
    • append

      public static <A,B> IO<B> append(IO<A> io1, IO<B> io2)
    • left

      public static <A,B> IO<A> left(IO<A> io1, IO<B> io2)
    • flatMap

      public static <A,B> IO<B> flatMap(IO<A> io, F<A,IO<B>> f)
    • interactWhile

      public static IO<Unit> interactWhile(F<String,Boolean> condition, F<String,String> transform)
      Read lines from stdin until condition is not met, transforming each line and printing the result to stdout.
      Parameters:
      condition - Read lines until a line does not satisfy condition
      transform - Function to change line value
    • sequenceWhileEager

      public static <A> IO<Stream<A>> sequenceWhileEager(Stream<IO<A>> stream, F<A,Boolean> f)
    • sequenceWhile

      public static <A> IO<Stream<A>> sequenceWhile(Stream<IO<A>> stream, F<A,Boolean> f)
    • apply

      public static <A,B> IO<B> apply(IO<A> io, IO<F<A,B>> iof)
    • liftM2

      public static <A,B,C> IO<C> liftM2(IO<A> ioa, IO<B> iob, F2<A,B,C> f)
    • replicateM

      public static <A> IO<List<A>> replicateM(IO<A> ioa, int n)
    • readerState

      public static <A> IO<State<BufferedReader, Validation<IOException, String>>> readerState()
    • stdinReadLine

      public static IO<String> stdinReadLine()
    • stdoutPrintln

      public static IO<Unit> stdoutPrintln(String s)
    • stdoutPrint

      public static IO<Unit> stdoutPrint(String s)
    • getContents

      public static IO<LazyString> getContents()
    • interact

      public static IO<Unit> interact(F<LazyString, LazyString> f)