An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

[![Build Status](https://travis-ci.com/mtumilowicz/java11-category-theory-reader-functor.svg?branch=master)](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