concept functional programming in category f#

appears as: Functional programming, Functional programming, functional programming
F# Deep Dives

This is an excerpt from Manning's book F# Deep Dives.

  • Functional programming is now undeniably a trend in the industry. An increasing number of programming languages support the functional paradigm, including C++, C#, JavaScript, Python, and Java 8. Moreover, the functional approach underlies many successful libraries, including LINQ and Task Parallel Library (TPL) in the .NET world, and also jQuery and Node.js.
  • Table 1. Roles of the individual components

    Component

    Responsibilities

    Collaboration

    Model Holds state  
      The model carries a subset of the view state projected via data binding. Data binding is the key responsible for synchronizing the model with the view, hence the INotifyPropertyChanged constraint in the Mvc.start type signature. Follow best practices of functional programming, and keep the model declarative (dumb, if you prefer this term). Avoid presentation logic in the model—it belongs in the controller.
    View Event source Data-binding target Events Model
      The view hides details of the underlying GUI framework behind a generic and simple IView<'TEvent,'TModel> interface. This interface is an example of both parametric polymorphism (by 'TEvent type, which allows you to define a single generic implementation of the Mvc type) and subtype polymorphism (which uses different IView implementations).
    Controller Event processor Events Model
      The controller is dependent only on events and the model. It makes the architecture loosely coupled (and therefore more testable) yet cohesive (pattern matching ensures that all event cases are handled).
    Mvc Mediator Coordinator Events Model IView Controller
      The Mvc type depends on everything, but it doesn’t matter because it stays fixed. There is no need to extend or change it.
    Real-World Functional Programming with Examples in F# and C#

    This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

    The second snippet consists of two independent lines of code. We said earlier that in functional programming, we can run independent parts of the program in parallel. Now you can see that immutability gives us a great way to spot which parts of the program are independent. Even without knowing any details, we can look at the change that makes these two operations run in parallel. The change to the source code is minimal:

    var hitMonster = Task.Factory.StartNew(() =>
      monster.HitByShooting(gunShot));
    var hitPlayer = Task.Factory.StartNew(() =>
      player.HitByShooting(gunShot));

    The only thing that we did was wrap the computation in a Task type from the Parallel Extensions library. (We’ll talk about Future in detail in chapter 14.) The benefit isn’t only that we have to write less code, but that we have a guarantee that the code is correct. If you did a similar change in an imperative program, you’d have to carefully review the HitByShooting method (and any other method it calls) to find all places where it accesses some mutable state and add locks to protect the code that reads or modifies shared state. In functional programming everything is immutable, so we don’t need to add any locks.

    1.1. What is functional programming?

    Finding a precise definition of functional programming is difficult. Various functional languages exist, and there’s no clear set of features that every functional language must have. Nonetheless, functional languages share common properties and support somewhat different styles of expressing solutions to programming problems. It’s easiest to describe functional programming by comparing it with the most common alternative option: imperative programming.