You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/docs/e6.combinators.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,38 +5,38 @@ slug: combinators
5
5
6
6
## What is a combinator?
7
7
8
-
- One meaning of “combinator” is a more informal sense referring to the **combinator pattern**, a style of organizing libraries centered around the idea of combining things. Usually there is **some type T**, some **functions for constructing “primitive” values of type T**, and some “**combinators**” which can **combine values of type T** in various ways to **build up more complex values of type T**. The other definition is **"function with no free variables"**.
8
+
- One meaning of “combinator” is a more informal sense referring to the combinator pattern, a style of organizing libraries centered around the idea of combining things. Usually there is some type T, some functions for constructing “primitive” values of type T, and some “combinators” which can combine values of type T in various ways to build up more complex values of type T. The other definition is "function with no free variables".
- A combinator is **a function** which **builds program fragments from program fragments**; in a sense the programmer using combinators constructs much of the desired program automatically, rather that writing every detail by hand.
11
+
- A combinator is a function which builds program fragments from program fragments; in a sense the programmer using combinators constructs much of the desired program automatically, rather that writing every detail by hand.
12
12
__ John Hughes—[Generalizing Monads to Arrows](http://www.cse.chalmers.se/~rjmh/Papers/arrows.pdf) via [Functional Programming Concepts](https://github.com/caiorss/Functional-Programming/blob/master/haskell/Functional_Programming_Concepts.org)
13
13
14
14
The exact definition of "combinators" in Rust ecosystem is bit unclear.
15
15
16
16
-`or()`, `and()`, `or_else()`, `and_then()`
17
-
-**Combine two values of type T** and **return same type T**.
17
+
- Combine two values of type T and return same type T.
18
18
19
19
-`xor()` for `Option` types
20
-
-**Combine two values of type T** and **return same type T**, **only if exactly one value is T**
20
+
- Combine two values of type T and return same type T, only if exactly one value is T
21
21
22
22
-`filter()` for `Option` types
23
-
-**Filter type T** by using a closure as a conditional function
24
-
-**Return same type T**
23
+
- Filter type T by using a closure as a conditional function
24
+
- Return same type T
25
25
26
26
-`map()`, `map_err()`
27
-
-**Convert type T by applying a closure**.
28
-
- The **data type of the value inside T can be changed**.
27
+
- Convert type T by applying a closure.
28
+
- The data type of the value inside T can be changed.
29
29
ex. `Some<&str>` can be converted to `Some<usize>` or `Err<&str>` to `Err<isize>` and etc.
30
30
31
31
-`map_or()`, `map_or_else()`
32
-
-**Transform type T by applying a closure** & **return the value inside type T**.
33
-
- For **`None` and `Err`, a default value or another closure** is applied.
32
+
- Transform type T by applying a closure & return the value inside type T.
33
+
- For `None` and `Err`, a default value or another closure is applied.
34
34
35
35
-`ok_or()`, `ok_or_else()` for `Option` types
36
-
-**Transform `Option` type into a `Result` type**.
36
+
- Transform `Option` type into a `Result` type.
37
37
38
38
-`as_ref()`, `as_mut()`
39
-
-**Transform type T into a reference or a mutable reference**.
39
+
- Transform type T into a reference or a mutable reference.
> Usually in programming languages `filter` functions are used with arrays or iterators to create a new array/ iterator by filtering own elements via a function/ closure. Rust also provides [`filter()`**as an iterator adaptor**](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter) to apply a closure on each element of an iterator to transform it into another iterator. However in here we are talking about the functionality of `filter()` with `Option` types.
179
+
> Usually in programming languages `filter` functions are used with arrays or iterators to create a new array/ iterator by filtering own elements via a function/ closure. Rust also provides [`filter()` as an iterator adaptor](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter) to apply a closure on each element of an iterator to transform it into another iterator. However in here we are talking about the functionality of `filter()` with `Option` types.
180
180
181
181
The same `Some` type is returned, only if we pass a `Some` value and the given closure returned true for it. `None` is returned, if `None` type passed or the closure returned false. The closure uses the value inside `Some` as an argument. Still Rust support `filter()` only for `Option` types.
182
182
@@ -195,9 +195,9 @@ assert_eq!(n.filter(fn_is_even), n); // None -> no value -> None
195
195
## map() and map_err()
196
196
197
197
> [!tip]
198
-
> Usually in programming languages `map()` functions are used with arrays or iterators, **to apply a closure on each element** of the array or iterator. Rust also provides [`map()`**as an iterator adaptor**](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map) to apply a closure on each element of an iterator to transform it into another iterator. However in here we are talking about the functionality of `map()` with `Option` and `Result` types.
198
+
> Usually in programming languages `map()` functions are used with arrays or iterators, to apply a closure on each element of the array or iterator. Rust also provides [`map()` as an iterator adaptor](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map) to apply a closure on each element of an iterator to transform it into another iterator. However in here we are talking about the functionality of `map()` with `Option` and `Result` types.
199
199
200
-
-`map()` : Convert type T by applying a closure. **The data type of `Some` or `Ok` blocks can be changed** according to the return type of the closure. Convert `Option<T>` to `Option<U>`, `Result<T, E>` to `Result<U, E>`
200
+
-`map()` : Convert type T by applying a closure. The data type of `Some` or `Ok` blocks can be changed according to the return type of the closure. Convert `Option<T>` to `Option<U>`, `Result<T, E>` to `Result<U, E>`
201
201
202
202
⭐ Via `map()`, only `Some` and `Ok` values are getting changed. No affect to the values inside `Err` (`None` doesn’t contain any value at all).
-`map_err()` for `Result` types : **The data type of `Err` blocks can be changed** according to the return type of the closure. Convert `Result<T, E>` to `Result<T, F>`.
234
+
-`map_err()` for `Result` types : The data type of `Err` blocks can be changed according to the return type of the closure. Convert `Result<T, E>` to `Result<T, F>`.
235
235
236
236
⭐ Via `map_err()`, only `Err` values are getting changed. No affect to the values inside `Ok`.
Hope you remember the functionality of [`unwrap_or()` and `unwrap_or_else()`](/docs/unwrap-and-expect/#unwrap_or-unwrap_or_default-and-unwrap_or_else) functions. These functions also bit similar to them. But `map_or()` and `map_or_else()` apply a closure on `Some` and `Ok` values and **return the value inside type T**.
253
+
Hope you remember the functionality of [`unwrap_or()` and `unwrap_or_else()`](/docs/unwrap-and-expect/#unwrap_or-unwrap_or_default-and-unwrap_or_else) functions. These functions also bit similar to them. But `map_or()` and `map_or_else()` apply a closure on `Some` and `Ok` values and return the value inside type T.
254
254
255
255
-`map_or()` : Support only for `Option` types (not supporting `Result`). Apply the closure to the value inside `Some` and return the output according to the closure. The given default value is returned for `None` types.
0 commit comments