Iterant

object Iterant extends IterantInstances

Defines the standard Iterant builders.

Defines the standard Iterant builders.

Companion
class
trait Sum
trait Mirror
trait IterantInstances
class Object
trait Matchable
class Any

Type members

Classlikes

final case class Concat[F[_], A](lh: F[Iterant[F, A]], rh: F[Iterant[F, A]]) extends Iterant[F, A]

The Concat state of the Iterant represents a state that specifies the concatenation of two streams.

The Concat state of the Iterant represents a state that specifies the concatenation of two streams.

Value Params
lh

is the left hand side of the concatenation, to be processed before the right-hand side

rh

is the rest of the stream, to be processed after the left-hand side is

class Deprecated[F[_], A](val self: Iterant[F, A]) extends Extensions[F, A]

Extension methods for deprecated methods.

Extension methods for deprecated methods.

final case class Halt[F[_], A](e: Option[Throwable]) extends Iterant[F, A]

The Halt state of the Iterant represents the completion state of a stream, with an optional exception if an error happened.

The Halt state of the Iterant represents the completion state of a stream, with an optional exception if an error happened.

   `Halt` is received as a final state in the iteration process.
   This state cannot be followed by any other element and
   represents the end of the stream.
Value Params
e

is an error to signal at the end of the stream, or None in case the stream has completed normally

final case class Last[F[_], A](item: A) extends Iterant[F, A]

The Last state of the Iterant represents a completion state as an alternative to Halt(None), describing one last element.

The Last state of the Iterant represents a completion state as an alternative to Halt(None), describing one last element.

   It is introduced as an optimization, being equivalent to
   `Next(item, F.pure(Halt(None)), F.unit)`, to avoid extra processing
   in the monadic `F[_]` and to short-circuit operations such as
   concatenation and `flatMap`.
Value Params
item

is the last element being signaled, after which the consumer can stop the iteration

final case class Next[F[_], A](item: A, rest: F[Iterant[F, A]]) extends Iterant[F, A]

The Next state of the Iterant represents a item / rest cons pair, where the head item is a strict value.

The Next state of the Iterant represents a item / rest cons pair, where the head item is a strict value.

   Note that `item` being a strict value means that it is
   already known, whereas the `rest` is meant to be lazy and
   can have asynchronous behavior as well, depending on the `F`
   type used.

   See [[monix.tail.Iterant.NextCursor NextCursor]]
   for a state where the head is a strict immutable list.
Value Params
item

is the current element to be signaled

rest

is the next state in the sequence that will produce the rest of the stream when evaluated

final case class NextBatch[F[_], A](batch: Batch[A], rest: F[Iterant[F, A]]) extends Iterant[F, A]

The NextBatch state of the Iterant represents an batch / rest cons pair, where batch is an Iterable type that can generate a whole batch of elements.

The NextBatch state of the Iterant represents an batch / rest cons pair, where batch is an Iterable type that can generate a whole batch of elements.

Value Params
batch

is a Iterable type that can generate elements by traversing a collection, a standard array or any Iterable

rest

is the next state in the sequence that will produce the rest of the stream when evaluated

final case class NextCursor[F[_], A](cursor: BatchCursor[A], rest: F[Iterant[F, A]]) extends Iterant[F, A]

The NextCursor state of the Iterant represents an batch / rest cons pair, where batch is an Iterator type that can generate a whole batch of elements.

The NextCursor state of the Iterant represents an batch / rest cons pair, where batch is an Iterator type that can generate a whole batch of elements.

   Useful for doing buffering, or by giving it an empty iterator,
   useful to postpone the evaluation of the next element.
Value Params
cursor

is an Iterator type that can generate elements by traversing a collection, a standard array or any Iterator

rest

is the next state in the sequence that will produce the rest of the stream when evaluated

final case class Scope[F[_], A, B](acquire: F[A], use: A => F[Iterant[F, B]], release: (A, ExitCase[Throwable]) => F[Unit]) extends Iterant[F, B]

The Scope state of the Iterant represents a stream that is able to specify the acquisition and release of a resource, to be used in generating stream events.

The Scope state of the Iterant represents a stream that is able to specify the acquisition and release of a resource, to be used in generating stream events.

   `Scope` is effectively the encoding of
   [[https://typelevel.org/cats-effect/typeclasses/bracket.html Bracket]],
   necessary for safe handling of resources. The `use`
   parameter is supposed to trigger a side effectful action
   that allocates resources, which are then used via `use`
   and released via `close`.

   Note that this is often used in combination with
   [[Iterant.Suspend Suspend]] and data types like
   [[https://typelevel.org/cats-effect/concurrency/ref.html cats.effect.concurrent.Ref]]
   in order to communicate the acquired resources between
   `open`, `use` and `close`.
Value Params
acquire

is an effect that should allocate necessary resources to be used in use and released in close

release

is an effect that should deallocate acquired resources via open and that will be executed no matter what

use

is the stream created via this scope

final case class Suspend[F[_], A](rest: F[Iterant[F, A]]) extends Iterant[F, A]

Builds a stream state equivalent with Iterant.NextCursor.

Builds a stream state equivalent with Iterant.NextCursor.

The Suspend state of the Iterant represents a suspended stream to be evaluated in the F context. It is useful to delay the evaluation of a stream by deferring to F.

Value Params
rest

is the next state in the sequence that will produce the rest of the stream when evaluated

abstract class Visitor[F[_], A, R] extends Iterant[F, A] => R

Implements the Visitor Pattern for interpreting the Iterant data structure.

Implements the Visitor Pattern for interpreting the Iterant data structure.

This can be used as an alternative to pattern matching and is used in the implementation of Iterant for performance reasons.

WARN: this being a class instead of a recursive function, it means that it often has to keep "shared state". Keeping shared state is great for performance, but breaks referential transparency, so use with care.

Inherited classlikes

class CatsSyncInstances[F[_]](F: Sync[F])

Provides the cats.effect.Sync instance for Iterant.

Provides the cats.effect.Sync instance for Iterant.

Inherited from
IterantInstances

Types

type Channel[F[_], A] = ChannelF[F, Option[Throwable], A]

Alias for monix.catnap.ChannelF, using Option[Throwable] as the completion event, to be compatible with Iterant.

Alias for monix.catnap.ChannelF, using Option[Throwable] as the completion event, to be compatible with Iterant.

See also

the docs for ChannelF

type Consumer[F[_], A] = ConsumerF[F, Option[Throwable], A]

Alias for monix.catnap.ConsumerF, using Option[Throwable] as the completion event, to be compatible with Iterant.

Alias for monix.catnap.ConsumerF, using Option[Throwable] as the completion event, to be compatible with Iterant.

See also

the docs for ConsumerF

type Producer[F[_], A] = ProducerF[F, Option[Throwable], A]

Alias for monix.catnap.ProducerF, using Option[Throwable] as the completion event, to be compatible with Iterant.

Alias for monix.catnap.ProducerF, using Option[Throwable] as the completion event, to be compatible with Iterant.

See also

the docs for ProducerF

Inherited types

type MirroredElemLabels <: Tuple

The names of the product elements

The names of the product elements

Inherited from
Mirror
type MirroredLabel <: String

The name of the type

The name of the type

Inherited from
Mirror

Value members

Concrete methods

def apply[F[_]]: Apply[F]

Returns an IterantBuilders instance for the specified F monadic type that can be used to build Iterant instances.

Returns an IterantBuilders instance for the specified F monadic type that can be used to build Iterant instances.

This is used to achieve the Partially-Applied Type technique.

Example:

 import monix.eval.Task

 Iterant[Task].range(0, 10)
def channel[F[_], A](bufferCapacity: BufferCapacity, maxBatchSize: Int, producerType: ProducerSide)(F: Concurrent[F], cs: ContextShift[F]): F[(Producer[F, A], Iterant[F, A])]

Returns a ProducerF instance, along with an Iterant connected to it.

Returns a ProducerF instance, along with an Iterant connected to it.

Internally a ConcurrentChannel is used, the paired Iterant acting as a ConsumerF, connecting via ConcurrentChannel.consume.

Value Params
bufferCapacity

is the capacity of the internal buffer being created per evaluated Iterant stream

maxBatchSize

is the maximum size of the Iterant.NextBatch nodes being emitted; this determines the maximum number of events being processed at any one time

producerType

(UNSAFE) specifies if there are multiple concurrent producers that will push events on the channel, or not; MultiProducer is the sane, default choice; only use SingleProducer for optimization purposes, for when you know what you're doing

def concat[F[_], A](xs: Iterant[F, A]*)(F: Sync[F]): Iterant[F, A]

Concatenates list of Iterants into a single stream

Concatenates list of Iterants into a single stream

def concatS[F[_], A](lh: F[Iterant[F, A]], rh: F[Iterant[F, A]]): Iterant[F, A]

Builds a stream state equivalent with Iterant.Concat.

Builds a stream state equivalent with Iterant.Concat.

The Concat state of the Iterant represents a state that specifies the concatenation of two streams.

Value Params
lh

is the left hand side of the concatenation, to be processed before the right-hand side

rh

is the rest of the stream, to be processed after the left-hand side is

def defer[F[_], A](fa: => Iterant[F, A])(F: Sync[F]): Iterant[F, A]

Alias for suspend.

Alias for suspend.

Promote a non-strict value representing a stream to a stream of the same type, effectively delaying its initialisation.

Value Params
fa

is the by-name parameter that will generate the stream when evaluated

def delay[F[_], A](a: => A)(F: Sync[F]): Iterant[F, A]

Alias for eval.

Alias for eval.

def empty[F[_], A]: Iterant[F, A]

Returns an empty stream.

Returns an empty stream.

def eval[F[_], A](a: => A)(F: Sync[F]): Iterant[F, A]

Lifts a non-strict value into the stream context, returning a stream of one element that is lazily evaluated.

Lifts a non-strict value into the stream context, returning a stream of one element that is lazily evaluated.

def fromArray[F[_], A](xs: Array[A])(F: Applicative[F]): Iterant[F, A]

Converts any standard Array into a stream.

Converts any standard Array into a stream.

def fromBatch[F[_], A](xs: Batch[A])(F: Applicative[F]): Iterant[F, A]

Converts a Batch into a stream.

Converts a Batch into a stream.

def fromBatchCursor[F[_], A](xs: BatchCursor[A])(F: Applicative[F]): Iterant[F, A]

Converts a BatchCursor into a stream.

Converts a BatchCursor into a stream.

def fromChannel[F[_], A](channel: Channel[F, A], bufferCapacity: BufferCapacity, maxBatchSize: Int)(F: Async[F]): Iterant[F, A]

Transforms any monix.catnap.ChannelF into an Iterant stream.

Transforms any monix.catnap.ChannelF into an Iterant stream.

This allows for example consuming from a ConcurrentChannel.

Value Params
bufferCapacity

is the capacity of the internal buffer being created; it can be either of limited capacity or unbounded

channel

is the monix.catnap.ChannelF value from which the created stream will consume events

maxBatchSize

is the maximum size of the emitted Iterant.NextBatch nodes, effectively specifying how many items can be pulled from the queue and processed in batches

def fromConsumer[F[_], A](consumer: Consumer[F, A], maxBatchSize: Int)(F: Async[F]): Iterant[F, A]

Transforms any monix.catnap.ConsumerF into an Iterant stream.

Transforms any monix.catnap.ConsumerF into an Iterant stream.

This allows for example consuming from a ConcurrentChannel.

Value Params
consumer

is the monix.catnap.ConsumerF value to transform into an Iterant

maxBatchSize

is the maximum size of the emitted Iterant.NextBatch nodes, effectively specifying how many items can be pulled from the queue and processed in batches

def fromIndexedSeq[F[_], A](xs: IndexedSeq[A])(F: Applicative[F]): Iterant[F, A]

Converts any Scala collection.IndexedSeq into a stream (e.g. Vector).

Converts any Scala collection.IndexedSeq into a stream (e.g. Vector).

def fromIterable[F[_], A](xs: Iterable[A])(F: Applicative[F]): Iterant[F, A]

Converts a scala.collection.Iterable into a stream.

Converts a scala.collection.Iterable into a stream.

def fromIterator[F[_], A](xs: Iterator[A])(F: Applicative[F]): Iterant[F, A]

Converts a scala.collection.Iterator into a stream.

Converts a scala.collection.Iterator into a stream.

def fromLazyStateAction[F[_], S, A](f: S => F[(A, S)])(seed: => F[S])(F: Sync[F]): Iterant[F, A]

Given an initial state and a generator function that produces the next state and the next element in the sequence in F[_] context, creates an Iterant that keeps generating Next items produced by our generator function.

Given an initial state and a generator function that produces the next state and the next element in the sequence in F[_] context, creates an Iterant that keeps generating Next items produced by our generator function.

Example:

 import monix.eval.Task

 val f = (x: Int) => Task((x + 1, x * 2))
 val seed = Task.pure(1)
 val stream = Iterant.fromLazyStateAction[Task, Int, Int](f)(seed)

 // Yields 2, 3, 5, 9
 stream.take(5)
See also

fromStateAction for version without F[_] context which generates NextBatch items

def fromList[F[_], A](xs: LinearSeq[A])(F: Applicative[F]): Iterant[F, A]

Converts any Scala collection.immutable.LinearSeq into a stream.

Converts any Scala collection.immutable.LinearSeq into a stream.

def fromReactivePublisher[F[_], A](publisher: Publisher[A], requestCount: Int, eagerBuffer: Boolean)(F: Async[F]): Iterant[F, A]

Given an org.reactivestreams.Publisher, converts it into an Iterant.

Given an org.reactivestreams.Publisher, converts it into an Iterant.

Value Params
eagerBuffer

can activate or deactivate the "eager buffer" mode in which the buffer gets pre-filled before the Iterant's consumer is ready to process it — this prevents having pauses due to back-pressuring the Subscription.request(n) calls

publisher

is the org.reactivestreams.Publisher reference to wrap into an Iterant

requestCount

a strictly positive number, representing the size of the buffer used and the number of elements requested on each cycle when communicating demand, compliant with the reactive streams specification. If Int.MaxValue is given, then no back-pressuring logic will be applied (e.g. an unbounded buffer is used and the source has a license to stream as many events as it wants)

See also

Iterant.toReactivePublisher for converting an Iterant to a reactive publisher.

def fromResource[F[_], A](r: Resource[F, A])(F: Sync[F]): Iterant[F, A]

Transforms any cats.effect.Resource into an Iterant.

Transforms any cats.effect.Resource into an Iterant.

See the documentation for Resource.

 import cats.effect.Resource
 import cats.effect.IO
 import java.io._

 def openFileAsResource(file: File): Resource[IO, FileInputStream] =
   Resource.make(IO(new FileInputStream(file)))(h => IO(h.close()))

 def openFileAsStream(file: File): Iterant[IO, FileInputStream] =
   Iterant[IO].fromResource(openFileAsResource(file))

This example would be equivalent with usage of Iterant.resource:

 def openFileAsResource2(file: File): Iterant[IO, FileInputStream] = {
   Iterant.resource(IO(new FileInputStream(file)))(h => IO(h.close()))
 }

This means that flatMap is safe to use:

 def readLines(file: File): Iterant[IO, String] =
   openFileAsStream(file).flatMap { in =>
     val buf = new BufferedReader(new InputStreamReader(in, "utf-8"))
     Iterant[IO].repeatEval(buf.readLine())
       .takeWhile(_ != null)
   }
def fromSeq[F[_], A](xs: Seq[A])(F: Applicative[F]): Iterant[F, A]

Converts any scala.collection.Seq into a stream.

Converts any scala.collection.Seq into a stream.

def fromStateAction[F[_], S, A](f: S => (A, S))(seed: => S)(F: Sync[F]): Iterant[F, A]

Given an initial state and a generator function that produces the next state and the next element in the sequence, creates an Iterant that keeps generating NextBatch items produced by our generator function with default recommendedBatchSize.

Given an initial state and a generator function that produces the next state and the next element in the sequence, creates an Iterant that keeps generating NextBatch items produced by our generator function with default recommendedBatchSize.

Example:

 import monix.eval.Task

 val f = (x: Int) => (x + 1, x * 2)
 val seed = 1
 val stream = Iterant.fromStateAction[Task, Int, Int](f)(seed)

 // Yields 2, 3, 5, 9
 stream.take(5)
See also

fromLazyStateAction for version supporting F[_] in result of generator function and seed element

def haltS[F[_], A](e: Option[Throwable]): Iterant[F, A]

Data constructor for building a Iterant.Halt value.

Data constructor for building a Iterant.Halt value.

The Halt state of the Iterant represents the completion state of a stream, with an optional exception if an error happened.

   `Halt` is received as a final state in the iteration process.
   This state cannot be followed by any other element and
   represents the end of the stream.
Value Params
e

is an error to signal at the end of the stream, or None in case the stream has completed normally

def intervalAtFixedRate[F[_]](period: FiniteDuration)(F: Async[F], timer: Timer[F]): Iterant[F, Long]

Creates an iterant that emits auto-incremented natural numbers (longs). at a fixed rate, as given by the specified period. The amount of time it takes to process an incoming value gets subtracted from provided period, thus created iterant tries to emit events spaced by the given time interval, regardless of how long further processing takes

Creates an iterant that emits auto-incremented natural numbers (longs). at a fixed rate, as given by the specified period. The amount of time it takes to process an incoming value gets subtracted from provided period, thus created iterant tries to emit events spaced by the given time interval, regardless of how long further processing takes

Value Params
period

period between 2 successive emitted values

timer

is the timer implementation used to generate delays and to fetch the current time

def intervalAtFixedRate[F[_]](initialDelay: FiniteDuration, period: FiniteDuration)(F: Async[F], timer: Timer[F]): Iterant[F, Long]

Creates an iterant that emits auto-incremented natural numbers (longs). at a fixed rate, as given by the specified period. The amount of time it takes to process an incoming value gets subtracted from provided period, thus created iterant tries to emit events spaced by the given time interval, regardless of how long further processing takes

Creates an iterant that emits auto-incremented natural numbers (longs). at a fixed rate, as given by the specified period. The amount of time it takes to process an incoming value gets subtracted from provided period, thus created iterant tries to emit events spaced by the given time interval, regardless of how long further processing takes

This version of the intervalAtFixedRate allows specifying an initialDelay before first value is emitted

Value Params
initialDelay

initial delay before emitting the first value

period

period between 2 successive emitted values

timer

is the timer implementation used to generate delays and to fetch the current time

def intervalWithFixedDelay[F[_]](delay: FiniteDuration)(F: Async[F], timer: Timer[F]): Iterant[F, Long]

Creates an iterant that emits auto-incremented natural numbers (longs) spaced by a given time interval. Starts from 0 with no delay, after which it emits incremented numbers spaced by the period of time. The given period of time acts as a fixed delay between successive events.

Creates an iterant that emits auto-incremented natural numbers (longs) spaced by a given time interval. Starts from 0 with no delay, after which it emits incremented numbers spaced by the period of time. The given period of time acts as a fixed delay between successive events.

Without having an initial delay specified, this overload will immediately emit the first item, without any delays.

Value Params
delay

the time to wait between 2 successive events

timer

is the timer implementation used to generate delays and to fetch the current time

def intervalWithFixedDelay[F[_]](initialDelay: FiniteDuration, delay: FiniteDuration)(F: Async[F], timer: Timer[F]): Iterant[F, Long]

Creates an iterant that emits auto-incremented natural numbers (longs) spaced by a given time interval. Starts from 0 with no delay, after which it emits incremented numbers spaced by the period of time. The given period of time acts as a fixed delay between successive events.

Creates an iterant that emits auto-incremented natural numbers (longs) spaced by a given time interval. Starts from 0 with no delay, after which it emits incremented numbers spaced by the period of time. The given period of time acts as a fixed delay between successive events.

Value Params
delay

the time to wait between 2 successive events

initialDelay

is the delay to wait before emitting the first event

timer

is the timer implementation used to generate delays and to fetch the current time

def lastS[F[_], A](item: A): Iterant[F, A]

Builds a stream state equivalent with Iterant.Last.

Builds a stream state equivalent with Iterant.Last.

The Last state of the Iterant represents a completion state as an alternative to Halt(None), describing one last element.

   It is introduced as an optimization, being equivalent to
   `Next(item, F.pure(Halt(None)), F.unit)`, to avoid extra processing
   in the monadic `F[_]` and to short-circuit operations such as
   concatenation and `flatMap`.
Value Params
item

is the last element being signaled, after which the consumer can stop the iteration

def liftF[F[_], A](fa: F[A])(F: Functor[F]): Iterant[F, A]

Lifts a value from monadic context into the stream context, returning a stream of one element

Lifts a value from monadic context into the stream context, returning a stream of one element

def never[F[_], A](F: Async[F]): Iterant[F, A]

Returns a stream that never emits any event and never completes.

Returns a stream that never emits any event and never completes.

def nextBatchS[F[_], A](items: Batch[A], rest: F[Iterant[F, A]]): Iterant[F, A]

Data constructor for building a Iterant.NextBatch value.

Data constructor for building a Iterant.NextBatch value.

The NextBatch state of the Iterant represents an batch / rest cons pair, where batch is an Iterable type that can generate a whole batch of elements.

Value Params
items

is a Iterable type that can generate elements by traversing a collection, a standard array or any Iterable

rest

is the next state in the sequence that will produce the rest of the stream when evaluated

def nextCursorS[F[_], A](items: BatchCursor[A], rest: F[Iterant[F, A]]): Iterant[F, A]

Data constructor for building a Iterant.NextCursor value.

Data constructor for building a Iterant.NextCursor value.

The NextCursor state of the Iterant represents an batch / rest cons pair, where batch is an Iterator type that can generate a whole batch of elements.

   Useful for doing buffering, or by giving it an empty iterator,
   useful to postpone the evaluation of the next element.
Value Params
items

is an Iterator type that can generate elements by traversing a collection, a standard array or any Iterator

rest

is the next state in the sequence that will produce the rest of the stream when evaluated

def nextS[F[_], A](item: A, rest: F[Iterant[F, A]]): Iterant[F, A]

Data constructor for building a Iterant.Next value.

Data constructor for building a Iterant.Next value.

The Next state of the Iterant represents a item / rest cons pair, where the head item is a strict value.

   Note that `item` being a strict value means that it is
   already known, whereas the `rest` is meant to be lazy and
   can have asynchronous behavior as well, depending on the `F`
   type used.

   See [[monix.tail.Iterant.NextCursor NextCursor]]
   for a state where the head is a strict immutable list.
Value Params
item

is the current element to be signaled

rest

is the next state in the sequence that will produce the rest of the stream when evaluated

def now[F[_], A](a: A): Iterant[F, A]

Lifts a strict value into the stream context, returning a stream of one element.

Lifts a strict value into the stream context, returning a stream of one element.

def pure[F[_], A](a: A): Iterant[F, A]

Alias for now.

Alias for now.

def raiseError[F[_], A](ex: Throwable): Iterant[F, A]

Returns an empty stream that ends with an error.

Returns an empty stream that ends with an error.

def range[F[_]](from: Int, until: Int, step: Int)(F: Applicative[F]): Iterant[F, Int]

Builds a stream that on evaluation will produce equally spaced values in some integer interval.

Builds a stream that on evaluation will produce equally spaced values in some integer interval.

Value Params
from

the start value of the stream

step

the increment value of the tail (must be positive or negative)

until

the end value of the stream (exclusive from the stream)

Returns

the tail producing values from, from + step, ... up to, but excluding until

def repeat[F[_], A](elems: A*)(F: Sync[F]): Iterant[F, A]

Builds a stream that repeats the items provided in argument.

Builds a stream that repeats the items provided in argument.

It terminates either on error or if the source is empty.

def repeatEval[F[_], A](thunk: => A)(F: Sync[F]): Iterant[F, A]

Builds a stream that suspends provided thunk and evaluates it indefinitely on-demand.

Builds a stream that suspends provided thunk and evaluates it indefinitely on-demand.

The stream will only terminate if evaluation throws an exception

Referentially transparent alternative to Iterator.continually

Example: infinite sequence of random numbers

 import monix.eval.Coeval
 import scala.util.Random

 val randomInts = Iterant[Coeval].repeatEval(Random.nextInt())
def repeatEvalF[F[_], A](fa: F[A])(F: Sync[F]): Iterant[F, A]

Builds a stream that evaluates provided effectful values indefinitely.

Builds a stream that evaluates provided effectful values indefinitely.

The stream will only terminate if an error is raised in F context

def resource[F[_], A](acquire: F[A])(release: A => F[Unit])(F: Sync[F]): Iterant[F, A]

Creates a stream that depends on resource allocated by a monadic value, ensuring the resource is released.

Creates a stream that depends on resource allocated by a monadic value, ensuring the resource is released.

Typical use-cases are working with files or network sockets

Example:

 import cats.implicits._
 import cats.effect.IO
 import java.io.PrintWriter

 val printer =
   Iterant.resource {
     IO(new PrintWriter("./lines.txt"))
   } { writer =>
     IO(writer.close())
   }

 // Safely use the resource, because the release is
 // scheduled to happen afterwards
 val writeLines = printer.flatMap { writer =>
   Iterant[IO]
     .fromIterator(Iterator.from(1))
     .mapEval(i => IO { writer.println(s"Line #$$i") })
 }

 // Write 100 numbered lines to the file
 // closing the writer when finished
 writeLines.take(100).completedL
Value Params
acquire

resource to acquire at the start of the stream

release

function that releases the acquired resource

def resourceCase[F[_], A](acquire: F[A])(release: (A, ExitCase[Throwable]) => F[Unit])(F: Sync[F]): Iterant[F, A]

Creates a stream that depends on resource allocated by a monadic value, ensuring the resource is released.

Creates a stream that depends on resource allocated by a monadic value, ensuring the resource is released.

Typical use-cases are working with files or network sockets

Example:

 import cats.effect._
 import java.io.PrintWriter

 val printer =
   Iterant.resource {
     IO(new PrintWriter("./lines.txt"))
   } { writer =>
     IO(writer.close())
   }

 // Safely use the resource, because the release is
 // scheduled to happen afterwards
 val writeLines = printer.flatMap { writer =>
   Iterant[IO]
     .fromIterator(Iterator.from(1))
     .mapEval(i => IO { writer.println(s"Line #$$i") })
 }

 // Write 100 numbered lines to the file
 // closing the writer when finished
 writeLines.take(100).completedL
Value Params
acquire

an effect that acquires an expensive resource

release

function that releases the acquired resource

def scopeS[F[_], A, B](acquire: F[A], use: A => F[Iterant[F, B]], release: (A, ExitCase[Throwable]) => F[Unit]): Iterant[F, B]

Builds a stream state equivalent with Iterant.Scope.

Builds a stream state equivalent with Iterant.Scope.

The Scope state of the Iterant represents a stream that is able to specify the acquisition and release of a resource, to be used in generating stream events.

   `Scope` is effectively the encoding of
   [[https://typelevel.org/cats-effect/typeclasses/bracket.html Bracket]],
   necessary for safe handling of resources. The `use`
   parameter is supposed to trigger a side effectful action
   that allocates resources, which are then used via `use`
   and released via `close`.

   Note that this is often used in combination with
   [[Iterant.Suspend Suspend]] and data types like
   [[https://typelevel.org/cats-effect/concurrency/ref.html cats.effect.concurrent.Ref]]
   in order to communicate the acquired resources between
   `open`, `use` and `close`.
Value Params
acquire

is an effect that should allocate necessary resources to be used in use and released in close

release

is an effect that should deallocate acquired resources via open and that will be executed no matter what

use

is the stream created via this scope

def suspend[F[_], A](fa: => Iterant[F, A])(F: Sync[F]): Iterant[F, A]

Promote a non-strict value representing a stream to a stream of the same type, effectively delaying its initialisation.

Promote a non-strict value representing a stream to a stream of the same type, effectively delaying its initialisation.

Value Params
fa

is the by-name parameter that will generate the stream when evaluated

def suspend[F[_], A](rest: F[Iterant[F, A]]): Iterant[F, A]

Defers the stream generation to the underlying evaluation context (e.g. Task, Coeval, IO, etc), building a reference equivalent with Iterant.Suspend.

Defers the stream generation to the underlying evaluation context (e.g. Task, Coeval, IO, etc), building a reference equivalent with Iterant.Suspend.

The Suspend state of the Iterant represents a suspended stream to be evaluated in the F context. It is useful to delay the evaluation of a stream by deferring to F.

Value Params
rest

is the next state in the sequence that will produce the rest of the stream when evaluated

def suspendS[F[_], A](rest: F[Iterant[F, A]]): Iterant[F, A]

Builds a stream state equivalent with Iterant.NextCursor.

Builds a stream state equivalent with Iterant.NextCursor.

The Suspend state of the Iterant represents a suspended stream to be evaluated in the F context. It is useful to delay the evaluation of a stream by deferring to F.

Value Params
rest

is the next state in the sequence that will produce the rest of the stream when evaluated

def tailRecM[F[_], A, B](a: A)(f: A => Iterant[F, Either[A, B]])(F: Sync[F]): Iterant[F, B]

Keeps calling f and concatenating the resulting iterants for each scala.util.Left event emitted by the source, concatenating the resulting iterants and generating events out of scala.util.Right[B] values.

Keeps calling f and concatenating the resulting iterants for each scala.util.Left event emitted by the source, concatenating the resulting iterants and generating events out of scala.util.Right[B] values.

Based on Phil Freeman's Stack Safety for Free.

Implicits

Inherited implicits

implicit def catsSyncInstances[F[_]](F: Sync[F]): CatsSyncInstances[F]

Provides the cats.effect.Sync instance for Iterant.

Provides the cats.effect.Sync instance for Iterant.

Inherited from
IterantInstances