![]() ![]() It can of course then be implemented more efficiently like avoiding the boxing and the flat mapping, but any such implementation has to satisfy this equation. We say we only defined flatMap, and that here is the definition of maps. That was an argument why it makes sense to define map that way, but if we argue in a monadic framework, then indeed we turn things around. In other words, boxing in a singleton lists and concatenating the results cancels each other out. Yes, mapping a function f over a list is the same as flat mapping the function that rises from first taking f to the argument and then wrapping the result in a singleton list. With flatMap, we concatenate all the singleton lists. Let's say m.map(f), where m is a list value, we know that lists are monadic values, so that would be the same as m.flatMap where we apply f yielding a single value, and then pass it to unit and that gives us essentially a singleton list of that value. Let's see whether this makes sense with an example. It's implementation is simply taken x of type a, apply first f and then g to x. It takes its left operand, a function from A to B, and it's right operand, a function from B to C, and it yields the composition of those two functions, so that's a function from a to C. It's defined in the standard library, so you can think of andThen as being defined like this. That in turn is the same as m.flatMap(f) andThen unit. Here's the general definition of map: for any monadic value m, m.map(f) is defined to be m.flatMap with a function that takes an x and returns unit of f of x. What about map? Do we need another definition of map? In fact, for monads, we can define map as a combination of flatMap and unit. In each case, the flatMap on a monad is just the flatMap as defined on the type. List is a monad with unit of x as the single element list x, and flatMap being flatMap on lists, set is a monad with the unit element set of x, option is a monad with the unit element sum of x, and generator is a monad with the unit element single of x. If you take this definition, and we see that quite a few of the data structures that we encountered are monads. It can be an extension method or it could also be defined as a regular method in the monad class M. In the literature, flatMap is also called bind. Unit, on the other hand, it takes an x of type T and returns an M of T. It takes an instance of the monad type M of T and a function that takes a T, so what's in the monad to an M of U, and it returns an M of U. FlatMap is typically used as an infix method, so we define it here as an abstract extension method. The shape of these operations looks like this. A monad M is a parametric type, so a type that takes a type parameter, with two operations, flatMap and unit, that have to satisfy some laws. In fact, there's a name that describes this class of data structures together with some algebraic laws that they should have and that's called a monad. An observation is that data Structures with map and flatMap seem to be quite common. In fact, once you have done this session, you probably will find out that you knew monads all along. In fact, there's a zillion of such articles that compare monads to such exotic things as burritos or elephants. If you're at all interested in functional programming, then you probably heard the term already or read an article of what monads are. In this session, we're going to talk about monads. This course is intended to be taken after Functional Programming Principles in Scala. You should have some familiarity with using the command line. Proficiency with Java or C# is ideal, but experience with other languages such as C/C++, Python, Javascript or Ruby is also sufficient. Recommended background: You should have at least one year programming experience. We will also look at purely functional alternatives to mutable state, using infinite data structures or functional reactive programming. ![]() We will explore the consequences of combining functions and state. Several parts of this course deal with the question how functional programming interacts with mutable state. ![]() Finally, you will learn how to leverage the ability of the compiler to infer values from types. You’ll also learn some best practices on how to write good Scala code in the real world. We'll work on larger and more involved examples, from state space exploration to random testing to discrete circuit simulators. ![]() You'll get to know important new functional programming concepts, from lazy evaluation to structuring your libraries using monads. In this course you will learn how to apply the functional programming style in the design of larger Scala applications. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |