Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 Program...
About Me
• Dave Fancher
• Blog: http://davefancher.com
• Twitter: @davefancher
• Email: dave@davefancher.com
My Background
Why F#?
LINQ: The Functional Gateway Drug
• Learned about query syntax
• The method syntax
• Introduced me to functional principles
JavaScript, Too!
• Functions are first class citizens
• Its most powerful features are enabled by closures
Turning Functional
• Began applying functional principles in my code
• Immutability with readonly fields & properties
• Mo...
C# Was Getting Frustrating
• Verbose and repetitive
• Fragile
• Ceremonial
An Accidental Discovery
• Hanselminutes #311
• F#
So, what is this F# thing?
F# is a succinct, expressive, and efficient
functional and object-oriented language
for Microsoft .NET that helps you writ...
Origin
• CLR Language
• Microsoft Research, Cambridge
Lineage
ML OCaml F#
Multi-Paradigm
• Imperative
• Object-oriented
• Functional
Licensing & Availability
• First Class Citizen of Visual Studio since VS2010
• Apache 2.0 License
• Every major platform
•...
Ok, but why should I care?
Gaining Traction
Source: TIOBE Index, May 2014 (http://bit.ly/1cvhJp3)
F# Over Time
August 2011
March 2013
March 2014
Source: TIOBE Index, May 2014 (http://bit.ly/1nnmeWK)
Functional-First
• Favors FP over other styles
• Other styles are available if needed
Design Principles
Terse Syntax
• Few keywords
• Limited punctuation
• Strong type inference
• Implicit return values
Top-Down Evaluation
• File order is significant
• Declaration order is, too
• Avoids inadvertent mutually recursive defini...
Organization Constructs
• Namespaces
• Modules
Expression-Based
• Everything returns a value
• Eager evaluation
Immutable by Default
• 3 Types of bindings
• Immutable by default
Let Bindings
Mutability
Reference Cells
Using Function & Use Bindings
Type system
All CLR Types
• Int32
• String
• Double
• Float
• DateTime
• TimeSpan
• Types from other assemblies
• …
Enumerations
Functions
• Every F# function accepts exactly one input and returns exactly one
output
• No concept of void functions
• No...
Unit
• Denoted as ()
• Indicates no specific value
• Can safely be ignored
• Usually indicative of a function that has som...
Tuples
Tuples for out Parameters
Syntactic Tuples
Records
Discriminated Unions
• Immutable
• Structural Equality
• Resemble enumerations but are more powerful
• Versatile
DUs as Object Hierarchies
DUs as Trees
http://bit.ly/1g76AOv
Mitigating Null
Options
Integrated Units of Measure
Measure Types
Live Demo: Enforcing Units of Measure
Collection Types
• Sequences – seq { … }
• Arrays – [| … |]
• Lists – [ … ]
• Maps
• Sets
Collection Modules
• Provide functions for manipulating collections
• Many LINQ-like operations
• map -> Select
• reduce -...
F# Lists
• Not List<‘T>
• Immutable
• [ … ]
• List module
• Cons operator ::
• Concatenation operator @
Composing F# Lists With :: and @
Object-Oriented
Classes
Filthy little
hobbitses.
They stole it
from us!
Classes
Interfaces
Implementing Interfaces
Inheritance & Virtual Members
Object Expressions
Functional
Functional programming is programming without
assignment statements.
Bob Martin, FP Basics, Episode 1 (http://bit.ly/1nnhD...
Functional Purity
• F# is impure
• Mutability and side-effects are
allowed
Functions as Data
• Higher-order functions
• Let-bound functions
• Lambda expressions
Currying
• Named for Haskell Curry
• Arguments are applied individually
• Changes function organization
Curried Addition & Expanded Form
Partial Application
Pipelining
Function Composition
Recursion
• Preferred looping mechanism
• Compiler optimizes for tail calls
Pattern Matching
Basic Pattern Matching
Built-in patterns
• Null
• Variable & Wildcard
• Literal
• Tuple
• Record
• Identifier
• Array
• List
• Cons
• As
• And
• ...
Decomposing Tuples
Decomposing DUs
Active Patterns
• Custom patterns
• Special type of function called an Active Recognizer
• Curried
• Maximum number of cas...
Partial Active Patterns
• Single custom case
• Not restricted to 7 active patterns
• Return value is option
Live Demo: Partial Active Patterns
Data Access
Language Features
• Query Expressions
• Type Providers
Live Demo: Using the OData Type Provider
Async & Parallel Programming
Asynchronous Workflows
• Conceptually similar to async/await in C#
• Works using lightweight callbacks and continuations
Agent-based programming
• Borrowed from Erlang
• In-memory queuing mechanism
• Uses MailboxProcessor<‘T> for message passi...
Live Demo: Agent-based Calculator
Computation Expressions
Extending the Language
• Basis for several language features
• Based on a builder class
• Builder class methods map to fam...
Live Demo: Custom Computation Expression
In Summary
• F# is a powerful, multi-paradigm language
• Plays nicely with other CLR languages
• Offers numerous construct...
No matter what language you work in, programming
in a functional style provides benefits. You should do it
whenever it is ...
The More You Know
• The Book of F#
http://bit.ly/1hzHV6v
• F# Software Foundation
http://fsharp.org
• Try F#
http://tryfsh...
Break Free with Managed Functional Programming: An Introduction to F#
Upcoming SlideShare
Loading in …5
×

2

Share

Download to read offline

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

Download to read offline

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.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

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

  1. 1. Break Free with Managed Functional Programming An Introduction to F#
  2. 2. A language that doesn't affect the way you think about programming, is not worth knowing. Alan Perlis, Epigrams on Programming, 1982
  3. 3. About Me • Dave Fancher • Blog: http://davefancher.com • Twitter: @davefancher • Email: dave@davefancher.com
  4. 4. My Background
  5. 5. Why F#?
  6. 6. LINQ: The Functional Gateway Drug • Learned about query syntax • The method syntax • Introduced me to functional principles
  7. 7. JavaScript, Too! • Functions are first class citizens • Its most powerful features are enabled by closures
  8. 8. Turning Functional • Began applying functional principles in my code • Immutability with readonly fields & properties • More delegation • Coding without side-effects
  9. 9. C# Was Getting Frustrating • Verbose and repetitive • Fragile • Ceremonial
  10. 10. An Accidental Discovery • Hanselminutes #311 • F#
  11. 11. So, what is this F# thing?
  12. 12. 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.
  13. 13. Origin • CLR Language • Microsoft Research, Cambridge
  14. 14. Lineage ML OCaml F#
  15. 15. Multi-Paradigm • Imperative • Object-oriented • Functional
  16. 16. Licensing & Availability • First Class Citizen of Visual Studio since VS2010 • Apache 2.0 License • Every major platform • Managed by F# Software Foundation
  17. 17. Ok, but why should I care?
  18. 18. Gaining Traction Source: TIOBE Index, May 2014 (http://bit.ly/1cvhJp3)
  19. 19. F# Over Time August 2011 March 2013 March 2014 Source: TIOBE Index, May 2014 (http://bit.ly/1nnmeWK)
  20. 20. Functional-First • Favors FP over other styles • Other styles are available if needed
  21. 21. Design Principles
  22. 22. Terse Syntax • Few keywords • Limited punctuation • Strong type inference • Implicit return values
  23. 23. Top-Down Evaluation • File order is significant • Declaration order is, too • Avoids inadvertent mutually recursive definitions • Inferred return values
  24. 24. Organization Constructs • Namespaces • Modules
  25. 25. Expression-Based • Everything returns a value • Eager evaluation
  26. 26. Immutable by Default • 3 Types of bindings • Immutable by default
  27. 27. Let Bindings
  28. 28. Mutability
  29. 29. Reference Cells
  30. 30. Using Function & Use Bindings
  31. 31. Type system
  32. 32. All CLR Types • Int32 • String • Double • Float • DateTime • TimeSpan • Types from other assemblies • …
  33. 33. Enumerations
  34. 34. Functions • Every F# function accepts exactly one input and returns exactly one output • No concept of void functions • No concept of parameterless functions
  35. 35. Unit • Denoted as () • Indicates no specific value • Can safely be ignored • Usually indicative of a function that has some effect
  36. 36. Tuples
  37. 37. Tuples for out Parameters
  38. 38. Syntactic Tuples
  39. 39. Records
  40. 40. Discriminated Unions • Immutable • Structural Equality • Resemble enumerations but are more powerful • Versatile
  41. 41. DUs as Object Hierarchies
  42. 42. DUs as Trees
  43. 43. http://bit.ly/1g76AOv Mitigating Null
  44. 44. Options
  45. 45. Integrated Units of Measure
  46. 46. Measure Types
  47. 47. Live Demo: Enforcing Units of Measure
  48. 48. Collection Types • Sequences – seq { … } • Arrays – [| … |] • Lists – [ … ] • Maps • Sets
  49. 49. Collection Modules • Provide functions for manipulating collections • Many LINQ-like operations • map -> Select • reduce -> Aggregate • filter -> Where
  50. 50. F# Lists • Not List<‘T> • Immutable • [ … ] • List module • Cons operator :: • Concatenation operator @
  51. 51. Composing F# Lists With :: and @
  52. 52. Object-Oriented
  53. 53. Classes
  54. 54. Filthy little hobbitses. They stole it from us!
  55. 55. Classes
  56. 56. Interfaces
  57. 57. Implementing Interfaces
  58. 58. Inheritance & Virtual Members
  59. 59. Object Expressions
  60. 60. Functional
  61. 61. Functional programming is programming without assignment statements. Bob Martin, FP Basics, Episode 1 (http://bit.ly/1nnhDnm)
  62. 62. Functional Purity • F# is impure • Mutability and side-effects are allowed
  63. 63. Functions as Data • Higher-order functions • Let-bound functions • Lambda expressions
  64. 64. Currying • Named for Haskell Curry • Arguments are applied individually • Changes function organization
  65. 65. Curried Addition & Expanded Form
  66. 66. Partial Application
  67. 67. Pipelining
  68. 68. Function Composition
  69. 69. Recursion • Preferred looping mechanism • Compiler optimizes for tail calls
  70. 70. Pattern Matching
  71. 71. Basic Pattern Matching
  72. 72. Built-in patterns • Null • Variable & Wildcard • Literal • Tuple • Record • Identifier • Array • List • Cons • As • And • Or
  73. 73. Decomposing Tuples
  74. 74. Decomposing DUs
  75. 75. 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
  76. 76. Partial Active Patterns • Single custom case • Not restricted to 7 active patterns • Return value is option
  77. 77. Live Demo: Partial Active Patterns
  78. 78. Data Access
  79. 79. Language Features • Query Expressions • Type Providers
  80. 80. Live Demo: Using the OData Type Provider
  81. 81. Async & Parallel Programming
  82. 82. Asynchronous Workflows • Conceptually similar to async/await in C# • Works using lightweight callbacks and continuations
  83. 83. Agent-based programming • Borrowed from Erlang • In-memory queuing mechanism • Uses MailboxProcessor<‘T> for message passing • Implementation often includes asynchronous workflows for monitoring
  84. 84. Live Demo: Agent-based Calculator
  85. 85. Computation Expressions
  86. 86. Extending the Language • Basis for several language features • Based on a builder class • Builder class methods map to familiar language elements
  87. 87. Live Demo: Custom Computation Expression
  88. 88. 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
  89. 89. 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++
  90. 90. The More You Know • The Book of F# http://bit.ly/1hzHV6v • F# Software Foundation http://fsharp.org • Try F# http://tryfsharp.org • F# Language Reference http://bit.ly/1koEoqK
  • BillEubanks

    May. 19, 2019
  • asd125

    Apr. 12, 2015

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.

Views

Total views

1,754

On Slideshare

0

From embeds

0

Number of embeds

687

Actions

Downloads

20

Shares

0

Comments

0

Likes

2

×