package flux

  1. Overview
  2. Docs

Module Flux.SourceSource

Sourceval unfold : 's -> ('s -> ('a * 's) option) -> 'a source

unfold seed next is a finite source created from a seed state and a function that produces elements and an updated state.

Sourceval empty : 'a source

An empty source.

Sourceval list : 'a list -> 'a source

list items is a source with all elements from the items list.

Sourceval seq : 'a Seq.t -> 'a source

seq items is a source with all elements from the items sequence.

Sourceval array : 'a array -> 'a source

array items is a source with all elements from the items array.

Sourceval string : string -> char source

string str is a source with all characters from the str string.

Sourceval queue : 'a Queue.t -> 'a source

queue q is a source with all elements from the q queue.

Sourceval bqueue : ?stop:[ `Ignore | `Halt | `Close ] -> ('a, 'a option) Bqueue.t -> 'a source

bqueue ?stop q is a source with all iterms from the q bounded-queue.

The user can choose how to dispose the given bounded-queue q:

NOTE: `Halt a bounded-queue created with Bqueue.with_close also closes the given bounded-queue.

Sourceval file : filename:string -> int -> string source

file ~filename len reads at most len bytes of the file filename until the end.

Sourceval in_channel : ?close:bool -> in_channel -> string source

in_channel ic reads strings from the given in_channel ic. If the given ic is not stdin and ?close is true (by default), dispose closes it properly.

Transforming a source.

NOTE: Instead ofapplying the transformation functions at the source, consider using Stream.from or defining your computation as a flow to make it reusable.

Sourceval map : ('a -> 'b) -> 'a source -> 'b source

A source with all elements transformed with a mapping function.

Consuming a source.

Sourceval each : ('a -> unit) -> 'a source -> unit

each fn src applies an effectful function fn to all elements in src.

Sourceval next : 'a source -> ('a * 'a source) option

next src is Some (x, rest) where x is the first element of src and rest is src without x; or None, if src is empty.

NOTE: If rest is produced, it is required to either consume it or manually dispose its resources. Not doing so might lead to resource leaks.

Resource handling.

Sourceval dispose : 'a source -> unit

dispose src forces the termination of the source data. This function is useful in situations when a leftover source is produced in Stream.run.

NOTE: If the source is not already initialized, calling this function will first initialize its state before it is terminated.

Sourceval resource : finally:('r -> unit) -> ('r -> 'a option) -> 'r -> 'a source

resource ~finally pull value creates a new resource from a pull function and an initial value. resource uses Miou's ownership mechanism so that if the task consuming the resource is cancelled, the resource is properly released (and finally is executed).

This also requires the user to dispose the source at the end of the task, otherwise Miou's rules will be violated.

NOTE: The resource passed to the pull function is physically the same as init.

Sourcetype 'a task = ('a, 'a option) Bqueue.t -> unit

Type of tasks which should fill a given bounded-queue.

Sourceval with_task : ?parallel:bool -> ?halt:bool -> size:int -> 'a task -> 'a source

with_task ?halt ~size producer returns a source that is linked to a task producer producing elements of type 'a. The transfer of elements between the task and the source consumer is limited in memory by size elements at a time.

The source is not effective and may potentially block if the shared queue is empty and an attempt is made to consume it (via next, for example). The shared queue is automatically closed/halted (depending on ?halt, defaults to false) as soon as the producer terminates.

For more details on the difference between halt and close, please refer to the Bqueue module.

Sourceval with_formatter : ?halt:bool -> size:int -> (Format.formatter -> unit) -> string source

with_formatter ?halt ~size producer is a specialisation of with_task with a Format.formatter. Everything written in the formatter is transmitted to the consumer.