break free with managed functional programming: an introduction to f#

91
Break Free with Managed Functional Programming An Introduction to F#

Upload: indymobilenetdev

Post on 23-Jun-2015

93 views

Category:

Software


1 download

DESCRIPTION

Dave Fancher presenting at the March Indy .NET Mobile Dev meetup: Originally developed by Microsoft Research, Cambridge, F# is an open-source, functional-first language in the ML family. Despite its lofty position as a first-class Visual Studio language for the past two releases and its cross-platform availability it hasn't seen widespread adoption in the business world. These slides take you on an introductory tour of F#, exploring how its constructs and terse syntax can allow you to write more stable, maintainable code while keeping you focused on the problem rather than the plumbing.

TRANSCRIPT

Page 1: Break Free with Managed Functional Programming: An Introduction to F#

Break Free withManaged Functional

ProgrammingAn Introduction to F#

Page 2: Break Free with Managed Functional Programming: An Introduction to F#

A language that doesn't affect the way you think about programming, is not worth knowing.

Alan Perlis, Epigrams on Programming, 1982

Page 3: Break Free with Managed Functional Programming: An Introduction to F#

About Me• Dave Fancher• Blog: http://davefancher.com• Twitter: @davefancher• Email: [email protected]

Page 4: Break Free with Managed Functional Programming: An Introduction to F#

My Background

Page 5: Break Free with Managed Functional Programming: An Introduction to F#
Page 6: Break Free with Managed Functional Programming: An Introduction to F#

Why F#?

Page 7: Break Free with Managed Functional Programming: An Introduction to F#

LINQ: The Functional Gateway Drug• Learned about query syntax• The method syntax• Introduced me to functional principles

Page 8: Break Free with Managed Functional Programming: An Introduction to F#

JavaScript, Too!• Functions are first class citizens• Its most powerful features are enabled by closures

Page 9: Break Free with Managed Functional Programming: An Introduction to F#

Turning Functional• Began applying functional principles in my code• Immutability with readonly fields & properties• More delegation• Coding without side-effects

Page 10: Break Free with Managed Functional Programming: An Introduction to F#

C# Was Getting Frustrating• Verbose and repetitive• Fragile• Ceremonial

Page 11: Break Free with Managed Functional Programming: An Introduction to F#

An Accidental Discovery• Hanselminutes #311• F#

Page 12: Break Free with Managed Functional Programming: An Introduction to F#

So, what is this F# thing?

Page 13: Break Free with Managed Functional Programming: An Introduction to F#

F# is a succinct, expressive, and efficient functional and object-oriented language for Microsoft .NET that helps you write simple code to solve complex problems.

Page 14: Break Free with Managed Functional Programming: An Introduction to F#

Origin• CLR Language• Microsoft Research, Cambridge

Page 15: Break Free with Managed Functional Programming: An Introduction to F#

Lineage

ML OCaml F#

Page 16: Break Free with Managed Functional Programming: An Introduction to F#

Multi-Paradigm• Imperative• Object-oriented• Functional

Page 17: Break Free with Managed Functional Programming: An Introduction to F#

Licensing & Availability• First Class Citizen of Visual Studio since VS2010• Apache 2.0 License• Every major platform• Managed by F# Software Foundation

Page 18: Break Free with Managed Functional Programming: An Introduction to F#

Ok, but why should I care?

Page 19: Break Free with Managed Functional Programming: An Introduction to F#

Gaining Traction

Source: TIOBE Index, May 2014 (http://bit.ly/1cvhJp3)

Page 20: Break Free with Managed Functional Programming: An Introduction to F#

F# Over Time

August 2011

March 2013

March 2014

Source: TIOBE Index, May 2014 (http://bit.ly/1nnmeWK)

Page 21: Break Free with Managed Functional Programming: An Introduction to F#

Functional-First• Favors FP over other styles• Other styles are available if needed

Page 22: Break Free with Managed Functional Programming: An Introduction to F#

Design Principles

Page 23: Break Free with Managed Functional Programming: An Introduction to F#

Terse Syntax• Few keywords• Limited punctuation• Strong type inference• Implicit return values

Page 24: Break Free with Managed Functional Programming: An Introduction to F#

Top-Down Evaluation• File order is significant• Declaration order is, too• Avoids inadvertent mutually recursive definitions• Inferred return values

Page 25: Break Free with Managed Functional Programming: An Introduction to F#

Organization Constructs• Namespaces• Modules

Page 26: Break Free with Managed Functional Programming: An Introduction to F#

Expression-Based• Everything returns a value• Eager evaluation

Page 27: Break Free with Managed Functional Programming: An Introduction to F#

Immutable by Default• 3 Types of bindings• Immutable by default

Page 28: Break Free with Managed Functional Programming: An Introduction to F#

Let Bindings

Page 29: Break Free with Managed Functional Programming: An Introduction to F#

Mutability

Page 30: Break Free with Managed Functional Programming: An Introduction to F#

Reference Cells

Page 31: Break Free with Managed Functional Programming: An Introduction to F#

Using Function & Use Bindings

Page 32: Break Free with Managed Functional Programming: An Introduction to F#

Type system

Page 33: Break Free with Managed Functional Programming: An Introduction to F#

All CLR Types• Int32• String• Double• Float• DateTime• TimeSpan• Types from other assemblies• …

Page 34: Break Free with Managed Functional Programming: An Introduction to F#

Enumerations

Page 35: Break Free with Managed Functional Programming: An Introduction to F#

Functions• Every F# function accepts exactly one input and returns

exactly one output• No concept of void functions• No concept of parameterless functions

Page 36: Break Free with Managed Functional Programming: An Introduction to F#

Unit• Denoted as ()• Indicates no specific value• Can safely be ignored• Usually indicative of a function that has some effect

Page 37: Break Free with Managed Functional Programming: An Introduction to F#

Tuples

Page 38: Break Free with Managed Functional Programming: An Introduction to F#

Tuples for out Parameters

Page 39: Break Free with Managed Functional Programming: An Introduction to F#

Syntactic Tuples

Page 40: Break Free with Managed Functional Programming: An Introduction to F#

Records

Page 41: Break Free with Managed Functional Programming: An Introduction to F#

Discriminated Unions• Immutable• Structural Equality• Resemble enumerations but are more powerful• Versatile

Page 42: Break Free with Managed Functional Programming: An Introduction to F#

DUs as Object Hierarchies

Page 43: Break Free with Managed Functional Programming: An Introduction to F#

DUs as Trees

Page 44: Break Free with Managed Functional Programming: An Introduction to F#

http://bit.ly/1g76AOv

Mitigating Null

Page 45: Break Free with Managed Functional Programming: An Introduction to F#

Options

Page 46: Break Free with Managed Functional Programming: An Introduction to F#

Integrated Units of Measure

Page 47: Break Free with Managed Functional Programming: An Introduction to F#

Measure Types

Page 48: Break Free with Managed Functional Programming: An Introduction to F#

Live Demo: Enforcing Units of Measure

Page 49: Break Free with Managed Functional Programming: An Introduction to F#

Collection Types• Sequences – seq { … }• Arrays – [| … |]• Lists – [ … ]• Maps• Sets

Page 50: Break Free with Managed Functional Programming: An Introduction to F#

Collection Modules• Provide functions for manipulating collections• Many LINQ-like operations

• map -> Select• reduce -> Aggregate• filter -> Where

Page 51: Break Free with Managed Functional Programming: An Introduction to F#

F# Lists• Not List<‘T>• Immutable• [ … ]• List module• Cons operator ::• Concatenation operator @

Page 52: Break Free with Managed Functional Programming: An Introduction to F#

Composing F# Lists With :: and @

Page 53: Break Free with Managed Functional Programming: An Introduction to F#

Object-Oriented

Page 54: Break Free with Managed Functional Programming: An Introduction to F#

Classes

Page 55: Break Free with Managed Functional Programming: An Introduction to F#

Filthy little hobbitses. They stole it from us!

Page 56: Break Free with Managed Functional Programming: An Introduction to F#

Classes

Page 57: Break Free with Managed Functional Programming: An Introduction to F#

Interfaces

Page 58: Break Free with Managed Functional Programming: An Introduction to F#

Implementing Interfaces

Page 59: Break Free with Managed Functional Programming: An Introduction to F#

Inheritance & Virtual Members

Page 60: Break Free with Managed Functional Programming: An Introduction to F#

Object Expressions

Page 61: Break Free with Managed Functional Programming: An Introduction to F#

Functional λ

Page 62: Break Free with Managed Functional Programming: An Introduction to F#

Functional programming is programming without assignment statements.

 Bob Martin, FP Basics, Episode 1 (http://bit.ly/1nnhDnm)

Page 63: Break Free with Managed Functional Programming: An Introduction to F#

Functional Purity• F# is impure• Mutability and side-effects are

allowed

Page 64: Break Free with Managed Functional Programming: An Introduction to F#

Functions as Data• Higher-order functions• Let-bound functions• Lambda expressions

Page 65: Break Free with Managed Functional Programming: An Introduction to F#

Currying• Named for Haskell Curry• Arguments are applied individually• Changes function organization

Page 66: Break Free with Managed Functional Programming: An Introduction to F#

Curried Addition & Expanded Form

Page 67: Break Free with Managed Functional Programming: An Introduction to F#

Partial Application

Page 68: Break Free with Managed Functional Programming: An Introduction to F#

Pipelining

Page 69: Break Free with Managed Functional Programming: An Introduction to F#

Function Composition

Page 70: Break Free with Managed Functional Programming: An Introduction to F#

Recursion• Preferred looping mechanism• Compiler optimizes for tail calls

Page 71: Break Free with Managed Functional Programming: An Introduction to F#

Pattern Matching

Page 72: Break Free with Managed Functional Programming: An Introduction to F#

Basic Pattern Matching

Page 73: Break Free with Managed Functional Programming: An Introduction to F#

Built-in patterns• Null• Variable & Wildcard• Literal• Tuple• Record• Identifier

• Array• List• Cons• As• And• Or

Page 74: Break Free with Managed Functional Programming: An Introduction to F#

Decomposing Tuples

Page 75: Break Free with Managed Functional Programming: An Introduction to F#

Decomposing DUs

Page 76: Break Free with Managed Functional Programming: An Introduction to F#

Active Patterns• Custom patterns• Special type of function called an Active Recognizer• Curried• Maximum number of cases is 7• Each input must map to a named case

Page 77: Break Free with Managed Functional Programming: An Introduction to F#

Partial Active Patterns• Single custom case• Not restricted to 7 active patterns• Return value is option

Page 78: Break Free with Managed Functional Programming: An Introduction to F#

Live Demo: Partial Active Patterns

Page 79: Break Free with Managed Functional Programming: An Introduction to F#

Data Access

Page 80: Break Free with Managed Functional Programming: An Introduction to F#

Language Features• Query Expressions• Type Providers

Page 81: Break Free with Managed Functional Programming: An Introduction to F#

Live Demo: Using the OData Type Provider

Page 82: Break Free with Managed Functional Programming: An Introduction to F#

Async & Parallel Programming

Page 83: Break Free with Managed Functional Programming: An Introduction to F#

Asynchronous Workflows• Conceptually similar to async/await in C#• Works using lightweight callbacks and continuations

Page 84: Break Free with Managed Functional Programming: An Introduction to F#

Agent-based programming• Borrowed from Erlang• In-memory queuing mechanism• Uses MailboxProcessor<‘T> for message passing• Implementation often includes asynchronous workflows

for monitoring

Page 85: Break Free with Managed Functional Programming: An Introduction to F#

Live Demo: Agent-based Calculator

Page 86: Break Free with Managed Functional Programming: An Introduction to F#

Computation Expressions

Page 87: Break Free with Managed Functional Programming: An Introduction to F#

Extending the Language• Basis for several language features• Based on a builder class• Builder class methods map to familiar language

elements

Page 88: Break Free with Managed Functional Programming: An Introduction to F#

Live Demo: Custom Computation Expression

Page 89: Break Free with Managed Functional Programming: An Introduction to F#

In Summary• F# is a powerful, multi-paradigm language• Plays nicely with other CLR languages• Offers numerous constructs to keep you focused on the

problem rather than the plumbing• Simple code for complex problems

Page 90: Break Free with Managed Functional Programming: An Introduction to F#

No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient.

 John Carmack, Functional Programming in C++

Page 91: Break Free with Managed Functional Programming: An Introduction to F#

The More You Know

• The Book of F#http://bit.ly/1hzHV6v

• F# Software Foundationhttp://fsharp.org

• Try F#http://tryfsharp.org

• F# Language Referencehttp://bit.ly/1koEoqK