Skip to content

Type Classes : Applicative

johnmcclean-aol edited this page Nov 25, 2016 · 1 revision

Applicative Type Class

Learn you a Haskell reference

The Applicative type class in cyclops-typeclasses defines 'ap' methods that accept two HKT encoded Applicative instances, one of which contains a function and another a value. The ap method will apply the function to the value and return the resultant applicative. If that is too abstract (or poorly described to make sense) - let's look at a concrete example

Example

Without HKT encoding for simplicity

Optional<Function<Integer,Integer>> optAdd2 = Optional.of(i->i2);
Optional<Integer> optFive = Optional.of(5);

Optional<Integer> result = OptionalInstances.ap(optAdd2,optFive);

What should result be?

5 + 2 = 7

Optional[7]

With HKT encoding

OptionalType<Function<Integer,Integer>> hktAdd2 = OptionalType.widen(optAdd2);
OptionalType<Integer> hktFive = OptionalType.widen(optFive);

OptionalType<Integer> result  = OptionalInstances.applicative()
                                                 .ap(hktAdd2,hktFive);

Optional<Integer> opt = OptionalType.narrow(result);
Clone this wiki locally