Package fj.data
Class IOFunctions
- java.lang.Object
-
- fj.data.IOFunctions
-
public final class IOFunctions extends java.lang.Object
IO monad for processing files, with main methodsenumFileLines(java.io.File, fj.data.Option<java.nio.charset.Charset>, fj.data.Iteratee.IterV<java.lang.String, A>)
,enumFileChars(java.io.File, fj.data.Option<java.nio.charset.Charset>, fj.data.Iteratee.IterV<java.lang.Character, A>)
andenumFileCharChunks(java.io.File, fj.data.Option<java.nio.charset.Charset>, fj.data.Iteratee.IterV<char[], A>)
(the latter one is the fastest as char chunks read from the file are directly passed to the iteratee without indirection in between).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
IOFunctions.CharChunkReader<A>
private static class
IOFunctions.CharChunkReader2<A>
private static class
IOFunctions.LineReader<A>
-
Field Summary
Fields Modifier and Type Field Description static F<java.io.Reader,IO<Unit>>
closeReader
private static int
DEFAULT_BUFFER_SIZE
static IO<Unit>
ioUnit
static java.io.BufferedReader
stdinBufferedReader
-
Constructor Summary
Constructors Modifier Constructor Description private
IOFunctions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <A,B>
IO<B>append(IO<A> io1, IO<B> io2)
static <A,B>
IO<B>apply(IO<A> io, IO<F<A,B>> iof)
static <A,B>
IO<B>as(IO<A> io, B b)
static <A,B>
IO<B>bind(IO<A> io, F<A,IO<B>> f)
static <A,B,C>
IO<C>bracket(IO<A> init, F<A,IO<B>> fin, F<A,IO<C>> body)
static IO<java.io.BufferedReader>
bufferedReader(java.io.File f, Option<java.nio.charset.Charset> encoding)
static <A> F<java.io.Reader,F<Iteratee.IterV<char[],A>,IO<Iteratee.IterV<char[],A>>>>
charChunkReader()
A function that feeds an iteratee with character chunks read from aReader
(char[] of sizeDEFAULT_BUFFER_SIZE
).static <A> F<java.io.Reader,F<Iteratee.IterV<java.lang.Character,A>,IO<Iteratee.IterV<java.lang.Character,A>>>>
charChunkReader2()
A function that feeds an iteratee with characters read from aReader
(chars are read in chunks of sizeDEFAULT_BUFFER_SIZE
).static IO<Unit>
closeReader(java.io.Reader r)
static <A> IO<Iteratee.IterV<char[],A>>
enumFileCharChunks(java.io.File f, Option<java.nio.charset.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.static <A> IO<Iteratee.IterV<java.lang.Character,A>>
enumFileChars(java.io.File f, Option<java.nio.charset.Charset> encoding, Iteratee.IterV<java.lang.Character,A> i)
An IO monad that reads char chunks from the given file and passes single chars to the given iteratee.static <A> IO<Iteratee.IterV<java.lang.String,A>>
enumFileLines(java.io.File f, Option<java.nio.charset.Charset> encoding, Iteratee.IterV<java.lang.String,A> i)
An IO monad that reads lines from the given file (using aBufferedReader
) and passes lines to the provided iteratee.static IO<java.io.Reader>
fileReader(java.io.File f, Option<java.nio.charset.Charset> encoding)
static <A,B>
IO<B>flatMap(IO<A> io, F<A,IO<B>> f)
static <A> IO<A>
fromF(F0<A> p)
static <A> IO<A>
fromTry(Try0<A,? extends java.io.IOException> t)
static IO<LazyString>
getContents()
static IO<Unit>
interact(F<LazyString,LazyString> f)
static IO<Unit>
interactWhile(F<java.lang.String,java.lang.Boolean> condition, F<java.lang.String,java.lang.String> transform)
Read lines from stdin until condition is not met, transforming each line and printing the result to stdout.static <A> IO<A>
join(IO<IO<A>> io1)
static <A> IO<A>
lazy(F<Unit,A> f)
static <A> IO<A>
lazy(F0<A> p)
static <A> SafeIO<A>
lazySafe(F<Unit,A> f)
static <A> SafeIO<A>
lazySafe(F0<A> f)
static <A,B>
IO<A>left(IO<A> io1, IO<B> io2)
static <A,B,C>
IO<C>liftM2(IO<A> ioa, IO<B> iob, F2<A,B,C> f)
static <A> F<java.io.BufferedReader,F<Iteratee.IterV<java.lang.String,A>,IO<Iteratee.IterV<java.lang.String,A>>>>
lineReader()
A function that feeds an iteratee with lines read from aBufferedReader
.static <A,B>
IO<B>map(IO<A> io, F<A,B> f)
static <A> P1<Validation<java.io.IOException,A>>
p(IO<A> io)
static <A> IO<State<java.io.BufferedReader,Validation<java.io.IOException,java.lang.String>>>
readerState()
static <A> IO<List<A>>
replicateM(IO<A> ioa, int n)
static <A> A
runSafe(IO<A> io)
Run io, rethrowing any IOException wrapped in a RuntimeExceptionstatic <A> IO<List<A>>
sequence(List<IO<A>> list)
Evaluate each action in the sequence from left to right, and collect the results.static <A> IO<Stream<A>>
sequence(Stream<IO<A>> stream)
static <A> IO<Stream<A>>
sequenceWhile(Stream<IO<A>> stream, F<A,java.lang.Boolean> f)
static <A> IO<Stream<A>>
sequenceWhileEager(Stream<IO<A>> stream, F<A,java.lang.Boolean> f)
static IO<java.lang.String>
stdinReadLine()
static IO<Unit>
stdoutPrint(java.lang.String s)
static IO<Unit>
stdoutPrintln(java.lang.String s)
static <A> SafeIO<A>
toSafe(IO<A> io)
Convert io to a SafeIO, throwing any IOException wrapped inside a RuntimeExceptionstatic <A> SafeIO<Validation<java.io.IOException,A>>
toSafeValidation(IO<A> io)
static <A> Try0<A,java.io.IOException>
toTry(IO<A> io)
static <A> IO<A>
unit(A a)
static IO<Unit>
unless(java.lang.Boolean b, IO<Unit> io)
static <A> IO<Unit>
voided(IO<A> io)
static IO<Unit>
when(java.lang.Boolean b, IO<Unit> io)
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
stdinBufferedReader
public static final java.io.BufferedReader stdinBufferedReader
-
-
Method Detail
-
p
public static <A> P1<Validation<java.io.IOException,A>> p(IO<A> io)
-
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
-
-
enumFileLines
public static <A> IO<Iteratee.IterV<java.lang.String,A>> enumFileLines(java.io.File f, Option<java.nio.charset.Charset> encoding, Iteratee.IterV<java.lang.String,A> i)
An IO monad that reads lines from the given file (using aBufferedReader
) and passes lines to the provided iteratee. May not be suitable for files with very long lines, consider to useenumFileCharChunks(java.io.File, fj.data.Option<java.nio.charset.Charset>, fj.data.Iteratee.IterV<char[], A>)
orenumFileChars(java.io.File, fj.data.Option<java.nio.charset.Charset>, fj.data.Iteratee.IterV<java.lang.Character, A>)
as an alternative.- Parameters:
f
- the file to read, must not benull
encoding
- the encoding to use,Option.none()
means platform defaulti
- the iteratee that is fed with lines read from the file
-
enumFileCharChunks
public static <A> IO<Iteratee.IterV<char[],A>> enumFileCharChunks(java.io.File f, Option<java.nio.charset.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 benull
encoding
- the encoding to use,Option.none()
means platform defaulti
- the iteratee that is fed with char chunks read from the file
-
enumFileChars
public static <A> IO<Iteratee.IterV<java.lang.Character,A>> enumFileChars(java.io.File f, Option<java.nio.charset.Charset> encoding, Iteratee.IterV<java.lang.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 benull
encoding
- the encoding to use,Option.none()
means platform defaulti
- the iteratee that is fed with chars read from the file
-
bufferedReader
public static IO<java.io.BufferedReader> bufferedReader(java.io.File f, Option<java.nio.charset.Charset> encoding)
-
fileReader
public static IO<java.io.Reader> fileReader(java.io.File f, Option<java.nio.charset.Charset> encoding)
-
unit
public static <A> IO<A> unit(A a)
-
lineReader
public static <A> F<java.io.BufferedReader,F<Iteratee.IterV<java.lang.String,A>,IO<Iteratee.IterV<java.lang.String,A>>>> lineReader()
A function that feeds an iteratee with lines read from aBufferedReader
.
-
charChunkReader
public static <A> F<java.io.Reader,F<Iteratee.IterV<char[],A>,IO<Iteratee.IterV<char[],A>>>> charChunkReader()
A function that feeds an iteratee with character chunks read from aReader
(char[] of sizeDEFAULT_BUFFER_SIZE
).
-
charChunkReader2
public static <A> F<java.io.Reader,F<Iteratee.IterV<java.lang.Character,A>,IO<Iteratee.IterV<java.lang.Character,A>>>> charChunkReader2()
A function that feeds an iteratee with characters read from aReader
(chars are read in chunks of sizeDEFAULT_BUFFER_SIZE
).
-
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.
-
toSafeValidation
public static <A> SafeIO<Validation<java.io.IOException,A>> toSafeValidation(IO<A> io)
-
interactWhile
public static IO<Unit> interactWhile(F<java.lang.String,java.lang.Boolean> condition, F<java.lang.String,java.lang.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 conditiontransform
- Function to change line value
-
sequenceWhileEager
public static <A> IO<Stream<A>> sequenceWhileEager(Stream<IO<A>> stream, F<A,java.lang.Boolean> f)
-
sequenceWhile
public static <A> IO<Stream<A>> sequenceWhile(Stream<IO<A>> stream, F<A,java.lang.Boolean> f)
-
readerState
public static <A> IO<State<java.io.BufferedReader,Validation<java.io.IOException,java.lang.String>>> readerState()
-
stdinReadLine
public static IO<java.lang.String> stdinReadLine()
-
getContents
public static IO<LazyString> getContents()
-
interact
public static IO<Unit> interact(F<LazyString,LazyString> f)
-
-