strand
Concurrency primitives.
Types
| Type | Description |
|---|---|
Key |
Stores a strand-local value. |
Receiver[T] |
Receives values from a channel. As an Iter, it can be used
with for or next(). |
Resource |
Limits concurrent entry into application-defined scopes. |
Sender[T] |
Sends values into a channel. As a Sink, it accepts put. |
Strand[R] |
Handles a background strand created by spawn. |
Stream[R, I, O] |
A Strand with channel-backed input and output, created by
stream. |
Functions
channel[T] … -> Tuple[Sender[T], Receiver[T]]
Creates a channel for communication between strands.
Parameters
| Name | Type | Description |
|---|---|---|
buffer? |
Int |
Buffer capacity (default: 1, unbuffered) |
collect() -> Array[Value]
Collects the strand-local input into a new array.
collect[S @ Sinkable[Value]] target -> S
Collects the strand-local input into a sink, returning the sink.
Parameters
| Name | Type | Description |
|---|---|---|
target |
S |
collection to add to |
each func
Transforms each value of the strand-local input and writes the result to the strand-local output.
Parameters
| Name | Type | Description |
|---|---|---|
func |
((Value) -> Value) |
a function that transforms a single value |
fork[*Rs] *blocks -> Tuple[...Rs]
Runs blocks concurrently and returns their results in block order.
Parameters
| Name | Type | Description |
|---|---|---|
*blocks |
...(() -> Rs) |
functions to execute concurrently |
from value
Writes the values of an iterable to the strand-local output.
Parameters
| Name | Type | Description |
|---|---|---|
value |
Iterable[Value] |
an iterable to emit values from |
map count func -> nil
Applies a function concurrently to values from the strand-local input, writing results to the strand-local output.
Parameters
| Name | Type | Description |
|---|---|---|
count |
Int |
number of worker strands |
func |
((Value) -> Value) |
function applied to each input value |
map[T] count func :input -> nil
Applies a function concurrently to values pulled from an iterable, writing results to the strand-local output.
Parameters
| Name | Type | Description |
|---|---|---|
count |
Int |
number of worker strands |
func |
((T) -> Value) |
function applied to each input value |
:input |
Iterable[T] |
source |
map[U] count func :output -> nil
Applies a function concurrently to values from the strand-local input, writing results to a sink.
Parameters
| Name | Type | Description |
|---|---|---|
count |
Int |
number of worker strands |
func |
((Value) -> U) |
function applied to each input value |
:output |
Sinkable[U] |
destination |
map[T, U] count func :input … -> nil
Applies a function concurrently to values pulled from an iterable, writing results to a sink.
Parameters
| Name | Type | Description |
|---|---|---|
count |
Int |
number of worker strands |
func |
((T) -> U) |
function applied to each input value |
:input |
Iterable[T] |
source |
:output |
Sinkable[U] |
destination |
pipeline[R] ...stages final … -> R
Connects pipeline stages and runs them concurrently, returning the last stage's result.
Parameters
| Name | Type | Description |
|---|---|---|
:input? |
Iterable[Value] |
override strand-local input for the first stage |
:output? |
Sinkable[Value] |
override strand-local output for the last stage |
...stages |
...(() -> Value) |
pipeline stages |
final |
(() -> R) |
final stage |
pool[T] count input func -> nil
Runs a function over an iterator with a fixed number of scoped workers.
Parameters
| Name | Type | Description |
|---|---|---|
count |
Int |
number of worker strands |
input |
Iterable[T] |
iterable of items |
func |
((T) -> Value) |
function applied to each input value |
pool count func -> nil
Runs a function over the strand-local input with a fixed number of scoped workers.
Parameters
| Name | Type | Description |
|---|---|---|
count |
Int |
number of worker strands |
func |
((Value) -> Value) |
function applied to each input value |
put value
Writes a value to the strand-local output.
Parameters
| Name | Type | Description |
|---|---|---|
value |
value to write |
redirect[R] block … -> R
Calls a function with the strand-local input or output replaced, returning its result.
The previous input and output are restored when block returns or raises.
Keyword arguments must precede block.
Parameters
| Name | Type | Description |
|---|---|---|
:input? |
Iterable[Value] |
replacement for the strand-local input |
:output? |
Sinkable[Value] |
replacement for the strand-local output |
block |
(() -> R) |
function to call |
Example
let doubled = []
strand.redirect input: [1, 2, 3] output: $doubled do
strand.each do |x| (x * 2)
assert_eq $doubled [2, 4, 6]
spawn[R] func -> Strand[R]
Runs a function in a background strand.
Parameters
| Name | Type | Description |
|---|---|---|
func |
(() -> R) |
the function to execute |
stream[R] func -> Stream[R, Value, Value]
Runs a function in a background strand with channel-backed endpoints.
Parameters
| Name | Type | Description |
|---|---|---|
func |
(() -> R) |
the function to execute |
where predicate
Writes the values of the strand-local input that satisfy a predicate to the strand-local output.
Parameters
| Name | Type | Description |
|---|---|---|
predicate |
((Value) -> Value) |
a function returning a truthy/falsy value |