I just started working through Tamizhvendan S' book F# Applied and came across this code snippet:
type WebPart = Context -> Async<Context option>
I've been looking through Microsoft's F# docs as well as my favorite F# site, Scott Wlaschin's F# for Fun and Profit
but have been unable to find any reference to this kind of type. It doesn't seem like a record type. It almost seems like a plain old function. So what is it?
Thanks for any help gals and guys.
One of the main differences between kind and type is that type is used to show sub-division or category while kind is used in the sense of 'sort'.
In formal English, “kind of” means “one category of,” or “one example of” a given category. It's the most formal, “correct” use of the expression. For example, you can say: The bald eagle is a kind of bird.
The word 'Type' is used to show the category or sub-division within a bigger or complete thing. The use of preposition 'of' after the word 'kind' tells us that the sentence have complete meaning. Type refers to clearly distinct and essential traits shared by members of a group. Its root meaning is “impression”.
Kind is a countable noun. After words like all and many, you use kinds, not 'kind'.
The WebPart
type you're looking at comes from Suave. You can read more about it at https://suave.io/async.html, but I'll summarize. You're correct: it is a type for a function. Specifically, it is a function that takes a Context
(which in Suave is a combination of an HTTP request record and a response record), and returns a Context option
asynchronously. That is, since some requests can fail, the function has the option of returning None
instead of a value. And since some requests can take a long time, all requests are treated as returning asynchronously, hence the Async
.
To chain two requests together, Suave provides the binding operator >=>
so that you don't have to go through the boilerplate of typing async { match result1 with None -> None | Some x -> return! request2 x
all the time; instead, you can just type request1 >=> request2
for the same effect.
If you need more help and reading through the Suave documentation hasn't provided the help you need, let us know and we'll try to explain further.
This is too long to be a comment. And I have never used Suave. However, I think that I can guess from the monadic properties of the types involved that >=>
is not a bind operator, but the Kleisli compositional operator. Without observing monad laws or trying to understand category theory, just by presuming the signatures to be:
val ( >>= ) : Async<'a option> -> ('a -> Async<'b option>) -> Async<'b option> val ( >=> ) : ('a -> Async<'b option>) -> ('b -> Async<'c option>) -> 'a -> Async<'c option>
The bind operator takes an Async<'a option>
and transforms it by way of a binder function 'a -> Async<'b option>
into an Async<'b option>
. The Kleisli operator composes two binder functions into a third.
These operators are working on monadic types and functions that are more generic than the concrete types described by a type abbreviation like type WebPart = Context -> Async<Context option>
. An implementation of the operators follows almost naturally.
let (>>=) request1 binder = async {
let! result1 = request1
match result1 with
| None -> return None
| Some x -> return! binder x }
let (>=>) f g a = f a >>= g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With