concept Apply in category .net

appears as: Apply, Apply
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

In practice, the function that takes more than one argument, in this case ToThumbnail, is curried and lifted into the Task type using the Task Pure extension method. Then, the resulting Task<Func<Image, Func<int, Image>>> is passed over the applicative functor Apply, which injects its output, Task<Image>, into the next function applied over DownloadImageAsync.

Ultimately, the last applicative functor operator Apply handles the transient parameter maxPixels elevated using the Pure extension method. From the perspective of the functor map operator, the curried function ToThumbnailCurried is partially applied and is exercised against an image argument and then wrapped into the task. Therefore, conceptually, the signature is

Let’s analyze the functions Pure and Apply. An applicative functor is a pattern implemented by two operations, defined here, where AF represents any elevated type (in bold):

Pure : T -> AF<R> 
Apply : AF<T -> R> -> AF<T> -> F<R>

!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":5}]]"}
!@%STYLE%@!

Intuitively, the Pure operator lifts a value into an elevated domain, and it’s equivalent to the Return monadic operator. The name Pure is a convention for an applicative functor definition. But in the case of applicative functions, this operator elevates a function. The Apply operator is a two-parameter function, both part of the same elevated domain.

Figure 10.8 The Apply operator implements the function wrapped inside an elevated type to a value in the context. The process triggers the unwrapping of both values; then, because the first value is a function, it’s applied automatically to the second value. Finally, the output is wrapped back inside the context of the elevated type.

c10-08.png