Skip to content

#[mixable] Sinkable[T]

Abstract type for values that sinks can be obtained from, whose instances have the methods below. An Array is Sinkable; a class that only implements (sink) is not.

Implements: BaseSinkable[T]

Methods

(sink)() -> Sink[T]

Returns a sink that writes to the value.

prechomp() -> Sink[Str] overload 1 of 2

Returns a sink that removes one trailing line terminator (\n or \r\n) from each value, if present, before writing it.

Parameters

NameTypeDescription
self Sinkable[Str] Receiver.

Errors

Putting a value raises TypeError if it is neither a Str nor a Bin.

Example

let acc = []
let out = acc.prechomp()
out.put "line\n"
assert_eq $acc ["line"]

prechomp() -> Sink[Bin] overload 2 of 2

Returns a sink that removes one trailing line terminator (\n or \r\n) from each value, if present, before writing it.

Parameters

NameTypeDescription
self Sinkable[Bin] Receiver.

Errors

Putting a value raises TypeError if it is neither a Str nor a Bin.

Example

let acc = []
let out = acc.prechomp()
out.put "line\n"
assert_eq $acc ["line"]

precrimp … -> Sink[Str] overload 1 of 2

Returns a sink that appends a line terminator to each value before writing it. The append is unconditional, so a value that already ends in one gets a second.

Parameters

NameTypeDescription
self Sinkable[Str] Receiver.
terminator? (Str | Bin)

Errors

Raises TypeError for a value, or a terminator, that is neither a Str nor a Bin, or if a Bin terminator would leave a Str value invalid UTF-8.

Example

let out = shell.stdout.precrimp()
out.put "hello"
# writes "hello\n"

open $path w do |file|
  file.precrimp(shell.line_ending()).put "native"

precrimp … -> Sink[Bin] overload 2 of 2

Returns a sink that appends a line terminator to each value before writing it. The append is unconditional, so a value that already ends in one gets a second.

Parameters

NameTypeDescription
self Sinkable[Bin] Receiver.
terminator? (Str | Bin)

Errors

Raises TypeError for a value, or a terminator, that is neither a Str nor a Bin, or if a Bin terminator would leave a Str value invalid UTF-8.

Example

let out = shell.stdout.precrimp()
out.put "hello"
# writes "hello\n"

open $path w do |file|
  file.precrimp(shell.line_ending()).put "native"

prefilter[U @ T] pred -> Sink[U]

Returns a sink that writes only the values for which pred is truthy.

Because each wrapper transforms values on the way in, the outermost wrapper sees a value first — the reverse of an iterator chain.

Parameters

NameTypeDescription
pred ((U, <Value, >Value) -> Value)

Example

let acc = []
let out = acc.prefilter (do |x| x % 2 == 0)
out.put 1
out.put 2
assert_eq $acc [2]

premap[U] func -> Sink[U]

Returns a sink that transforms each value with func before writing it.

The pre prefix marks the direction: unlike map, which transforms values on their way out, premap transforms values on their way into the sink. A value that is both Iterable and Sinkable — an Array, say — therefore offers map and premap as separate methods running in opposite directions.

Parameters

NameTypeDescription
func ((U, <Value, >Value) -> T)

Example

let acc = []
let out = acc.premap (do |x| x * 2)
out.put 3
assert_eq $acc [6]

put value

Writes a value to the sink.

Parameters

NameTypeDescription
value T

sink() -> Sink[T]

Returns a sink over the value.