SlideShare a Scribd company logo
1 of 22
Download to read offline
Get Functional on the CLR:
Intro to Functional Programming with
F#
David Alpert | @davidalpert | http://about.me/davidalpert
Learning Goals
• Disambiguate functional programming
• Demystify F# and F# syntax
• Demonstrate .NET interoperability
• Experiment with F# syntax
• Create a new F# library project
• Write basic functions and data structures in F#
• Call F# from C#
So that you will be able to:
Put yourself in “I have no idea
what I’m doing” situations more
often. Your growth will be
exponential. - @derekbailey
https://twitter.com/derickbailey/status/452832022860292096
Woody Zuill
“Question Everything – put
special focus on our deepest
held beliefs. What we trust most
hides our biggest problems.”
Agile Maxim #2
http://zuill.us/WoodyZuill/2012/09/16/the-8-agile-maxims-of-woody-zuill/
We are addicted to complexity
We are addicted to
complexity
We are addicted to accidental
complexity
We are addicted to
accidental complexity
We are addicted to manipulating
state
We are addicted to
manipulating state
#noManipulatingState is like
#noDefects or #NoEstimates
#noManipulatingState
is like
#noDefects
or
#noEstimates
What is Functional Programming?
It is programming... with functions!
• Pure – Referentially Transparent
• First Class and Higher-Order functions
• Partial application & Currying
What is Functional Programming?
(function ($) {
$(document).ready(function () {
$('dt').click(function (ev) {
// do something interesting
});
function doSomethingElse(ev) {
// something else again
}
$('a').click(doSomethingElse);
});
})(jQuery);
Func<int, bool> isEven = x => x%2 == 0;
public static MvcHtmlString LabelFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression)
{
// ...
}
var someString = @Html.TextBoxFor(model => model.Rating)
Why F#?
• Concise
• Convenient
• Correctness
• Concurrency
• Complete
Concise
Convenient
Correctness
Concurrency
Complete
The “Billion Dollar Mistake”
http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
Sir Charles Antony Richard Hoare, commonly
known as Tony Hoare, is a British computer
scientist, probably best known for the
development in 1960, at age 26, of Quicksort.
He also developed Hoare logic, the formal
language Communicating Sequential Processes
(CSP), and inspired the Occam programming
language.
Tony Hoare introduced Null references in
ALGOL W back in 1965 “simply because it was
so easy to implement”, says Mr. Hoare. He talks
about that decision considering it “my billion-
dollar mistake”.
We are addicted to accidental
complexity
We are addicted to
accidental complexity
From C# syntax to F# syntax
C# F#
Curly braces denote scope Indentation (i.e. Whitespace) denotes scope
Brackets and commas in argument list No brackets or commas
// single line comment // single line comment
/* multi-line comment */ (* multi-line comment *)
Explicit return Implicit return (value of the last expression)
public int Add(int x, int y)
{
return x + y;
}
let add x y = x + y
IEnumerable<T> seq<‘T>
IList<T> ‘T list
Null None ( an Option value)
Some more F# syntax
Description Syntax Evaluates to
Declare a list (of int) [2; 3; 4] int list = [2; 3; 4]
Prepend to a list 1 :: [2; 3; 4] int list = [1; 2; 3; 4]
Contact two lists [0; 1] @ [2; 3; 4] int list = [0; 1; 2; 3; 4]
Declare a tuple of int (1, 2, 3) int * int * int = (1, 2, 3)
Declare an optional value let x = Some(1) val x : int option = Some 1
Declare a ‘null’ value Let z = None val z : 'a option
Pipe a list into a function
(like Linq’s Select)
[1; 2; 3]
|> (fun x -> x ^ 2) int list = [1; 4; 9]
Functional Type Notation
let add1 x = x + 1 int -> int
let add x y = x + y int -> int -> int
Basic List Functions
List.filter : ('T -> bool) -> 'T list -> 'T list
List.map : ('T -> 'U) -> 'T list -> 'U list
List.fold : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State
predicate items result
projection items result
accumulator items final
state
initial
state
+ new item = final
state
initial
state
Basic List Functions
predicateitemsresult
projectionitemsresult
items
final
state initial
state
accumulator
+ new item = final
state
initial
state
public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
public static IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, TResult> selector )
public static TAccumulate Aggregate<TSource, TAccumulate>( this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func )
Demo: Rapid Prototyping with FSI
• Rapid prototyping with the FSI REPL
and the F# Interactive Window
Demo:
Rapid Prototyping w/ FSI REPL
& the F# Interactive window
Demo: File -> New F# Project
• Create a new F# library project
• Create a new C# library project w/ Nunit
• Add references and test F# code from C#Demo:
File -> New F# Project
Demo: Other goodies
• Parsing with FParsec
(a parser-combinator approach)
• Strongly-typed Units of Measure
• Domain Modelling (i.e. Lightweight DDD)
• Automated browser testing with Canopy
Demo:
Other F# goodies
References & Resources
Get Functional on the CLR:
Intro to Functional Programming with
F#
David Alpert | @davidalpert | http://about.me/davidalpert
Reference
• http://en.wikipedia.org/wiki/Functional_programming
• http://fsharpforfunandprofit.com/why-use-fsharp/
• http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
• http://www.tryfsharp.org/Create
• http://fsharpforfunandprofit.com/posts/fsharp-in-60-seconds/
• http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/
• http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/fsharp-component-design-
guidelines.pdf [ Component design guidelines ]
• http://visualstudiogallery.msdn.microsoft.com/07636c36-52be-4dce-9f2e-3c56b8329e33 [ depth colorizer ]
Examples
• http://www.quanttec.com/fparsec/ [ A parser-combinator library ]
• https://github.com/davidalpert/furlstrong [ F# based url parsing with FParsec ]
• http://nuggle.me/ [ DC Circuits in F# ]
• http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-
units.aspx [ Custom units of measure in F# ]
• http://www.slideshare.net/ScottWlaschin/ddd-with-fsharptypesystemlondonndc2013 [ DDD & F# Types ]
• http://lefthandedgoat.github.io/canopy/ [ Browser automation DSL with F# ]

More Related Content

What's hot

仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
bleis tift
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
mrkurt
 
F# Eye 4 the C# Guy - DDD Cambridge Nights 2014
F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014
F# Eye 4 the C# Guy - DDD Cambridge Nights 2014
Phillip Trelford
 

What's hot (20)

Rx workshop
Rx workshopRx workshop
Rx workshop
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programming
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
Time for Functions
Time for FunctionsTime for Functions
Time for Functions
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
F# Eye 4 the C# Guy - DDD Cambridge Nights 2014
F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014
F# Eye 4 the C# Guy - DDD Cambridge Nights 2014
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
Unit2 input output
Unit2 input outputUnit2 input output
Unit2 input output
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabus
 
Write Your Own Compiler in 24 Hours
Write Your Own Compiler in 24 HoursWrite Your Own Compiler in 24 Hours
Write Your Own Compiler in 24 Hours
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - Kalamassery
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Chapter 2 Decision Making (Python Programming Lecture)
Chapter 2 Decision Making (Python Programming Lecture)Chapter 2 Decision Making (Python Programming Lecture)
Chapter 2 Decision Making (Python Programming Lecture)
 

Viewers also liked

Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...
Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...
Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...
Dimitris Dranidis
 
Swift and Kotlin Presentation
Swift and Kotlin PresentationSwift and Kotlin Presentation
Swift and Kotlin Presentation
Andrzej Sitek
 
An Empirical Study of Goto in C Code from GitHub Repositories
An Empirical Study of Goto in C Code from GitHub RepositoriesAn Empirical Study of Goto in C Code from GitHub Repositories
An Empirical Study of Goto in C Code from GitHub Repositories
SAIL_QU
 
Software reliability engineering
Software reliability engineeringSoftware reliability engineering
Software reliability engineering
Mark Turner CRP
 

Viewers also liked (14)

Functional programming ruby mty
Functional programming   ruby mtyFunctional programming   ruby mty
Functional programming ruby mty
 
The Rise of Functional Programming
The Rise of Functional ProgrammingThe Rise of Functional Programming
The Rise of Functional Programming
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Eclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India 2015 - Keynote - Stephan HerrmannEclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India 2015 - Keynote - Stephan Herrmann
 
Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...
Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...
Get Ready for Agile Methods: How to manage constant and rapid change in IT pr...
 
No silver bullet essence and accidents of software engineering
No silver bullet essence and accidents of software engineeringNo silver bullet essence and accidents of software engineering
No silver bullet essence and accidents of software engineering
 
No silver bullet
No silver bulletNo silver bullet
No silver bullet
 
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
C# Async on iOS and Android - Miguel de Icaza, CTO of XamarinC# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
 
Swift and Kotlin Presentation
Swift and Kotlin PresentationSwift and Kotlin Presentation
Swift and Kotlin Presentation
 
An Empirical Study of Goto in C Code from GitHub Repositories
An Empirical Study of Goto in C Code from GitHub RepositoriesAn Empirical Study of Goto in C Code from GitHub Repositories
An Empirical Study of Goto in C Code from GitHub Repositories
 
Software reliability engineering
Software reliability engineeringSoftware reliability engineering
Software reliability engineering
 
Chapter 7 software reliability
Chapter 7 software reliabilityChapter 7 software reliability
Chapter 7 software reliability
 
Agile Is the New Waterfall
Agile Is the New WaterfallAgile Is the New Waterfall
Agile Is the New Waterfall
 
Kotlin
KotlinKotlin
Kotlin
 

Similar to Get Functional on the CLR: Intro to Functional Programming with F#

F# for C# devs - NDC Oslo 2015
F# for C# devs - NDC Oslo 2015F# for C# devs - NDC Oslo 2015
F# for C# devs - NDC Oslo 2015
Phillip Trelford
 
F# for C# devs - Leeds Sharp 2015
F# for C# devs -  Leeds Sharp 2015F# for C# devs -  Leeds Sharp 2015
F# for C# devs - Leeds Sharp 2015
Phillip Trelford
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
ezonesolutions
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
Paulo Morgado
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docx
jeanettehully
 

Similar to Get Functional on the CLR: Intro to Functional Programming with F# (20)

F# for C# devs - NDC Oslo 2015
F# for C# devs - NDC Oslo 2015F# for C# devs - NDC Oslo 2015
F# for C# devs - NDC Oslo 2015
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
 
Lesson11
Lesson11Lesson11
Lesson11
 
F# for C# devs - Leeds Sharp 2015
F# for C# devs -  Leeds Sharp 2015F# for C# devs -  Leeds Sharp 2015
F# for C# devs - Leeds Sharp 2015
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
F# for C# devs - SDD 2015
F# for C# devs - SDD 2015F# for C# devs - SDD 2015
F# for C# devs - SDD 2015
 
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 
Testing for share
Testing for share Testing for share
Testing for share
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docx
 
Pythonppt28 11-18
Pythonppt28 11-18Pythonppt28 11-18
Pythonppt28 11-18
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Python basic
Python basicPython basic
Python basic
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Get Functional on the CLR: Intro to Functional Programming with F#

  • 1. Get Functional on the CLR: Intro to Functional Programming with F# David Alpert | @davidalpert | http://about.me/davidalpert
  • 2. Learning Goals • Disambiguate functional programming • Demystify F# and F# syntax • Demonstrate .NET interoperability • Experiment with F# syntax • Create a new F# library project • Write basic functions and data structures in F# • Call F# from C# So that you will be able to:
  • 3. Put yourself in “I have no idea what I’m doing” situations more often. Your growth will be exponential. - @derekbailey https://twitter.com/derickbailey/status/452832022860292096
  • 4. Woody Zuill “Question Everything – put special focus on our deepest held beliefs. What we trust most hides our biggest problems.” Agile Maxim #2 http://zuill.us/WoodyZuill/2012/09/16/the-8-agile-maxims-of-woody-zuill/
  • 5. We are addicted to complexity We are addicted to complexity
  • 6. We are addicted to accidental complexity We are addicted to accidental complexity
  • 7. We are addicted to manipulating state We are addicted to manipulating state
  • 8. #noManipulatingState is like #noDefects or #NoEstimates #noManipulatingState is like #noDefects or #noEstimates
  • 9. What is Functional Programming? It is programming... with functions! • Pure – Referentially Transparent • First Class and Higher-Order functions • Partial application & Currying
  • 10. What is Functional Programming? (function ($) { $(document).ready(function () { $('dt').click(function (ev) { // do something interesting }); function doSomethingElse(ev) { // something else again } $('a').click(doSomethingElse); }); })(jQuery); Func<int, bool> isEven = x => x%2 == 0; public static MvcHtmlString LabelFor<TModel, TValue>( this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { // ... } var someString = @Html.TextBoxFor(model => model.Rating)
  • 11. Why F#? • Concise • Convenient • Correctness • Concurrency • Complete Concise Convenient Correctness Concurrency Complete
  • 12. The “Billion Dollar Mistake” http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare Sir Charles Antony Richard Hoare, commonly known as Tony Hoare, is a British computer scientist, probably best known for the development in 1960, at age 26, of Quicksort. He also developed Hoare logic, the formal language Communicating Sequential Processes (CSP), and inspired the Occam programming language. Tony Hoare introduced Null references in ALGOL W back in 1965 “simply because it was so easy to implement”, says Mr. Hoare. He talks about that decision considering it “my billion- dollar mistake”.
  • 13. We are addicted to accidental complexity We are addicted to accidental complexity
  • 14. From C# syntax to F# syntax C# F# Curly braces denote scope Indentation (i.e. Whitespace) denotes scope Brackets and commas in argument list No brackets or commas // single line comment // single line comment /* multi-line comment */ (* multi-line comment *) Explicit return Implicit return (value of the last expression) public int Add(int x, int y) { return x + y; } let add x y = x + y IEnumerable<T> seq<‘T> IList<T> ‘T list Null None ( an Option value)
  • 15. Some more F# syntax Description Syntax Evaluates to Declare a list (of int) [2; 3; 4] int list = [2; 3; 4] Prepend to a list 1 :: [2; 3; 4] int list = [1; 2; 3; 4] Contact two lists [0; 1] @ [2; 3; 4] int list = [0; 1; 2; 3; 4] Declare a tuple of int (1, 2, 3) int * int * int = (1, 2, 3) Declare an optional value let x = Some(1) val x : int option = Some 1 Declare a ‘null’ value Let z = None val z : 'a option Pipe a list into a function (like Linq’s Select) [1; 2; 3] |> (fun x -> x ^ 2) int list = [1; 4; 9]
  • 16. Functional Type Notation let add1 x = x + 1 int -> int let add x y = x + y int -> int -> int
  • 17. Basic List Functions List.filter : ('T -> bool) -> 'T list -> 'T list List.map : ('T -> 'U) -> 'T list -> 'U list List.fold : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State predicate items result projection items result accumulator items final state initial state + new item = final state initial state
  • 18. Basic List Functions predicateitemsresult projectionitemsresult items final state initial state accumulator + new item = final state initial state public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate ) public static IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, TResult> selector ) public static TAccumulate Aggregate<TSource, TAccumulate>( this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func )
  • 19. Demo: Rapid Prototyping with FSI • Rapid prototyping with the FSI REPL and the F# Interactive Window Demo: Rapid Prototyping w/ FSI REPL & the F# Interactive window
  • 20. Demo: File -> New F# Project • Create a new F# library project • Create a new C# library project w/ Nunit • Add references and test F# code from C#Demo: File -> New F# Project
  • 21. Demo: Other goodies • Parsing with FParsec (a parser-combinator approach) • Strongly-typed Units of Measure • Domain Modelling (i.e. Lightweight DDD) • Automated browser testing with Canopy Demo: Other F# goodies
  • 22. References & Resources Get Functional on the CLR: Intro to Functional Programming with F# David Alpert | @davidalpert | http://about.me/davidalpert Reference • http://en.wikipedia.org/wiki/Functional_programming • http://fsharpforfunandprofit.com/why-use-fsharp/ • http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare • http://www.tryfsharp.org/Create • http://fsharpforfunandprofit.com/posts/fsharp-in-60-seconds/ • http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ • http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/fsharp-component-design- guidelines.pdf [ Component design guidelines ] • http://visualstudiogallery.msdn.microsoft.com/07636c36-52be-4dce-9f2e-3c56b8329e33 [ depth colorizer ] Examples • http://www.quanttec.com/fparsec/ [ A parser-combinator library ] • https://github.com/davidalpert/furlstrong [ F# based url parsing with FParsec ] • http://nuggle.me/ [ DC Circuits in F# ] • http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing- units.aspx [ Custom units of measure in F# ] • http://www.slideshare.net/ScottWlaschin/ddd-with-fsharptypesystemlondonndc2013 [ DDD & F# Types ] • http://lefthandedgoat.github.io/canopy/ [ Browser automation DSL with F# ]