https://github.com/mtumilowicz/java11-category-theory-reader-functor
Simple implementation of Reader Functor.
https://github.com/mtumilowicz/java11-category-theory-reader-functor
category-theory functors reader reader-functor
Last synced: 5 months ago
JSON representation
Simple implementation of Reader Functor.
- Host: GitHub
- URL: https://github.com/mtumilowicz/java11-category-theory-reader-functor
- Owner: mtumilowicz
- Created: 2019-01-02T16:36:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-16T16:59:09.000Z (almost 7 years ago)
- Last Synced: 2025-04-15T14:14:07.178Z (9 months ago)
- Topics: category-theory, functors, reader, reader-functor
- Language: Java
- Homepage:
- Size: 64.5 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://travis-ci.com/mtumilowicz/java11-category-theory-reader-functor)
# java11-category-theory-reader-functor
_Reference_: https://bartoszmilewski.com/2015/01/20/functors/
# preface
Basic knowledge and intuition about functors is provided in:
https://github.com/mtumilowicz/java11-category-theory-optional-is-not-functor
**Reader functor**:
```
Type constructor: (-> a) // curried (-> a b) or equivallent (a -> b)
map :: (a -> b) -> (r -> a) -> (r -> b)
map f g = (.) f g
```
# proof
1. `map id = id`
```
map id g
= { definition of map }
(.) id g
```
1. `map (g . f) = map g . map f`
```
map (g . f) h
= { definition of map }
(.) (g . f) h
= { associativity of composition }
(.) g (f . h)
= { definition of map }
map g (f h)
= { definition of map }
map g (map f h)
= { definition of composition }
(map g . map f) h
```
# project description
* We provide simple implementation of Reader Functor:
```
@FunctionalInterface
interface Reader extends Function {
default Reader map(Function f) {
return asReader(f.compose(this));
}
default Reader asReader(Function f) {
return f::apply;
}
}
```
* and basic test
```
Reader toInteger = BigDecimal::intValue;
Reader mapped = toInteger.map(String::valueOf);
assertThat(mapped.apply(BigDecimal.TEN), is("10"));
```
# additional info
1. Reader could be extended to monad:
https://github.com/aol/cyclops/blob/master/cyclops-pure/src/main/java/cyclops/control/Reader.java
1. and used for dependency injection:
https://github.com/aol/cyclops/wiki/Reader-:-functional-dependency-injection