mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 20:34:44 +00:00
32 lines
981 B
Markdown
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='./31_error_handling.md'>Previous</a> | <a href='./33_integration_with_Python.md'>Next</a>
|
|
</p>
|