erg/doc/EN/syntax/33_pipeline.md
2023-03-29 22:15:39 +09:00

32 lines
981 B
Markdown

# Pipeline operator
Pipeline operators are used like this:
```python
assert f(g(x)) == (x |> g |> f())
assert f(g(x, y)) == (x |> g(y) |> f())
```
In other words, the order `Callable(object)` can be changed to `object |> Callable()`.
The pipeline operator can also be used on methods. For methods, `object.method(args)` changes to `object |>.method(args)`.
It looks like just more `|>`, but since the bond strength is low, you may be able to reduce the amount of `()`.
```python
rand = -1.0..1.0 |>.sample!()
log rand # 0.2597...
1+1*2 |>.times do log("a", end := "") # aaa
evens = 1..100 |>.iter() |>.filter i -> i % 2 == 0 |>.collect Array
# When implemented without the pipeline operator,
_evens = (1..100).iter().filter(i -> i % 2 == 0).collect(Array)
# or
__evens = 1..100 \
.iter() \
.filter i -> i % 2 == 0 \
.collect Array
```
<p align='center'>
<a href='./32_error_handling.md'>Previous</a> | <a href='./34_integration_with_Python.md'>Next</a>
</p>