Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming

Functional Programming

A crash course on functional programming for developers coming from a Java or C# background. I go over some high level concepts and talk a little about how you can apply those concepts to existing non-functional languages. I end by briefly talking about why I think Elixir/Erlang is a good solution for creating applications in a functional way.

This talk was presented at a private corporate event on December 14th, 2016.

Matt Chandler

December 14, 2016
Tweet

More Decks by Matt Chandler

Other Decks in Programming

Transcript

  1. A programming paradigm that treats computation as the evaluation of

    mathematical functions and avoids changing‑ state and mutable data.
  2. Lambda calculus A formal system in mathematical logic for expressing

    computation based on function abstraction and application using variable binding and substitution.
  3. The problem with a completely new programming paradigm isnʹt learning

    a new language. [...] The tricky part is to learn to think in a different way. — Neal Ford
  4. Pure functions The function always evaluates the same result value

    given the same argument value(s). Evaluation of the result does not cause any semantically observable side effect or output. A function can perform input or output and still be pure if the sequence of operations on the relevant Input/Output devices is modeled explicitly as both an argument and a result. The I/O monad is a programming idiom typically used to perform input/output in pure functional languages.
  5. Pure functions in C# // string.Concat is an example of

    a pure function in C# string greeting = string.Concat("Hello, ", "Matt")); // The Convert class also contains examples of pure functions int value = Convert.ToInt32("1");
  6. Impure functions in C# // Functions that change the out/ref

    argument int number; bool result = int.TryParse("42", out number);
  7. Composable functions Linq functions can be composed in C# var

    results = stores.Where(s => s.Chain == 200) .OrderBy(s => s.StoreNumber);
  8. An example of a mutable object in C# public class

    Item { public int Quantity { get; set; } public decimal UnitPrice { get; set; } public decimal Total { get { return Quantity * UnitPrice; } } }
  9. An example of an immutable object in C# public class

    Item { public Item(int quantity, decimal unitPrice) { Quantity = quantity; UnitPrice = unitPrice; } public int Quantity { get; private set; } public decimal UnitPrice { get; private set; } public decimal Total { get { return Quantity * UnitPrice; } } }
  10. public class Item { // … public Item WithQuantity(int value)

    { return value == Quantity ? this : new Item(value, UnitPrice); } public Item WithUnitPrice(decimal value) { return value == UnitPrice ? this : new Item(Quantity, value); } } Item frame = new Item(quantity: 1, unitPrice: 19.99M); Item updatedFrame = item.WithQuantity(2);
  11. public class Order { public Order() { Items = new

    List<Item>(); } public List<Item> Items { get; private set; } }
  12. public class Order { public Order(IEnumerable<Item> items) { Items =

    items.ToImmutableList(); } public ImmutableList<Item> Items { get; private set; } public Order WithItems(IEnumerable<Item> value) { return Object.ReferenceEquals(Items, value) ? this : new Order(value } }
  13. public Order AddItem(Item value) { return WithItems(Items.Add(value)); } public Order

    RemoveItem(Item value) { return WithItems(Items.Remove(value)); } public Order ReplaceItem(Item oldValue, Item newValue) { return oldValue == newValue ? this : WithItems(Items.Replace(oldValue, newValue)); }
  14. Item item = new Item(quantity: 1, unitPrice: 2.5m); Order order

    = new Order(ImmutableList.Create(item)); Item addtionalItem = apple.WithDiscount(discount); Order discountedOrder = order.ReplaceLine(apple, discountedApple);
  15. No*

  16. Elixir Functional language designed for building scalable and maintainable applications.

    Leverages the Erlang VM, known for running low‑ latency, distributed, fault‑tolerant systems. All code runs inside lightweight Erlang ʺprocessesʺ, everything is isolated but can communicate via messages. Relies heavily on paീern matching to match values, data structures, functions, and destructure data.