8000 Update README.md · jgarte/functional-jargon-python@93fc69e · GitHub
[go: up one dir, main page]

Skip to content

Commit 93fc69e

Browse files
authored
Update README.md
1 parent 6433f2a commit 93fc69e

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

README.md

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ __Table of Contents__
3434
* [Lambda (TODO)](#lambda-todo)
3535
* [Lambda Calculus (TODO)](#lambda-calculus-todo)
3636
* [Lazy evaluation (TODO)](#lazy-evaluation-todo)
37-
* [Functor (TODO)](#functor-todo)
38-
* [Applicative Functor (TODO)](#applicative-functor-todo)
37+
* [Functor](#functor)
38+
* [Applicative Functor](#applicative-functor)
3939
* [Monoid](#monoid)
4040
* [Monad (TODO)](#monad-todo)
4141
* [Comonad (TODO)](#comonad-todo)
@@ -579,28 +579,61 @@ Lazy evaluation is a call-by-need evaluation mechanism that delays the evaluatio
579579
# TODO
580580
```
581581

582-
## Functor (TODO)
582+
## Functor
583583

584-
An object that implements a `map` function which, while running over each value in the object to produce a new object, adheres to two rules:
584+
An object that implements a `map` method which, while running over each value in the object to produce a new object, adheres to two rules:
585585

586-
__Preserves identity__
587-
```
588-
object.map(x => x) ≍ object
586+
__Identity law__
587+
588+
```python
589+
functor.map(lambda x: x) == functor
589590
```
590591

591-
__Composable__
592+
__Associative law__
592593

593-
```
594-
object.map(compose(f, g)) ≍ object.map(g).map(f)
594+
```python
595+
functor.map(compose(f, g)) == functor.map(g).map(f)
595596
```
596597

598+
Sometimes `Functor` can be called `Mappable` to its `.map` method.
599+
You can have a look at the real-life [`Functor` interface](https://github.com/dry-python/returns/blob/master/returns/interfaces/mappable.py):
600+
597601
```python
598-
# TODO
602+
>>> from typing import Callable, TypeVar
603+
>>> from returns.interfaces.mappable import Mappable1 as Functor
604+
>>> from returns.primitives.hkt import SupportsKind1
605+
606+
>>> _FirstType = TypeVar('_FirstType')
607+
>>> _NewFirstType = TypeVar('_NewFirstType')
608+
609+
>>> class Box(SupportKind1['Box', _FirstType], Functor[_FirstType]):
610+
... def __init__(self, inner_value: _FirstType) -> None:
611+
... self._inner_value = inner_value
612+
613+
>>> def map(
614+
... self,
615+
... function: Callable[[_FirstType], _NewFirstType],
616+
... ) -> 'Box[_NewFirstType]':
617+
... return Box(function(self._inner_value))
618+
619+
>>> assert Box(-5).map(abs) == Box(5)
620+
>>>
599621
```
600622

601-
## Pointed Functor (TODO)
623+
__Further reading:__
602624

603-
## Applicative Functor (TODO)
625+
- [Functor interface docs](https://returns.readthedocs.io/en/latest/pages/interfaces.html#mappable)
626+
627+
628+
## Applicative Functor
629+
630+
An Applicative Functor is an object with `apply` and `.from_value` methods:
631+
- `.apply` applies a function in the object to a value in another object of the same type. Somethimes this method is also called `ap`
632+
- `.from_value` creates a new Applicative Functor from a pure value. Sometimes this method is also called `pure`
633+
634+
All Applicative Functors must also follow [a bunch of laws](https://returns.readthedocs.io/en/latest/pages/interfaces.html#applicative).
635+
636+
You can have a look at the real-life [`Applicative Functor` interface](https://github.com/dry-python/returns/blob/master/returns/interfaces/applicative.py).
604637

605638

606639
## Monoid

0 commit comments

Comments
 (0)
0