-
Notifications
You must be signed in to change notification settings - Fork 51
Type Classes : Applicative
johnmcclean-aol edited this page Nov 25, 2016
·
1 revision
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
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]
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);