SlideShare a Scribd company logo
1 of 51
Download to read offline
F#
What I’ve learnt so far
       Mark Needham
What is F#?
Another language that
  runs on the CLR
Interoperable with
other .NET languages
Functional...
...but with Object
Oriented features too
Statically typed with type
         inference
Why did I want to learn F#?
Different mindset
Immutability
Concurrency
How did I go about learning
           F#?
Functions
Can live on their own
let
add
a
b
=
a
+
b




http://intwoplacesatonce.com/?p=9
Type inference
let
add
a
b
=
a
+
b

val
add
:
int
‐>
int
‐>
int
let
add
(a:string)
(b:string)
=
a
+
b

val
add
:
string
‐>
string
‐>
string
Lots of ways to do the same thing
let
withLinks
(statuses:seq<TwitterStatus>)
=

 Seq.filter

   
(fun
s
‐>
s.Text.Contains("http"))
   
statuses
let
withLinks
(statuses:seq<TwitterStatus>)
=

 statuses
|>

 Seq.filter
(fun
s
‐>
s.Text.Contains("http"))
Forward
operator
(|>)
            

('a
‐>
('a
‐>
'b)
‐>
'b)
let
withLinks
(statuses:seq<TwitterStatus>)
=

  Seq.filter
(fun
s
‐>
hasLink
s)
statuses

let
withLinks
(statuses:seq<TwitterStatus>)
=

  statuses
|>
Seq.filter
(fun
s
‐>
hasLink
s)

let
withLinks
statuses
=

  statuses
|>
Seq.filter
(fun
(s:TwitterStatus)
‐>
hasLink
s)




let
withLinks
(statuses:seq<TwitterStatus>):seq<TwitterStatus>
=

  statuses
|>
Seq.filter
(fun
s
‐>
hasLink
s)

let
withLinks

:
seq<TwitterStatus>
‐>
seq<TwitterStatus>
=

  Seq.filter
(fun
s
‐>
hasLink
s)



Composing functions
let
statuses
=
seq
{
yield
new
TwitterStatus()
}

let
sortedStatuses
=
Seq.sortBy

             







(fun
(s:TwitterStatus)
‐>
s.Id)





             







statuses

let
firstStatus
=
Seq.hd
sortedStatuses
seq
{
yield
new
TwitterStatus()
}

|>
Seq.sortBy
(fun
(s:TwitterStatus)
‐>
s.Id)

|>
Seq.hd
Functions are cool but
    lack structure
Refactoring to objects
type
Tweets
=
{

 
 
 
 
 
 
 TwitterStatuses:seq<TwitterStatus>

 
 
 
 
 
 }
let
myTweets
=
{


 
 
 TwitterStatuses
=
TwitterService.GetStatuses
100

 
 
 
 



}
let
withLinks
(statuses:seq<TwitterStatus>)
=





statuses
|>
Seq.filter
(fun
s
‐>
s.Text.Contains("http"))


let
print
(statuses:seq<TwitterStatus>)
=




for
s
in
statuses
do








printfn
"[%s]
%s"
s.User.ScreenName
status.Text

type
Tweets
with




member
t.print()
=
print
t.TwitterStatuses




member
t.withLinks()
=

              {

              


TwitterStatuses
=
withLinks
t.TwitterStatuses
              }
Leads to simpler/easier to
    understand code
Helped by unit testing
xUnit.NET
[<Fact>]
let
should_recognise_message_with_link_as_a_link
()
=
  let
messageWithLink
=
    (new
MessageBuilder(message
=
"http://www.google.com")).Build()
  Assert.True(messageWithLink
|>
hasLink)

type
MessageBuilder(?message:string,
?user:string)
=









                                                            

  let
buildMessage
message
user
=

     new
TwitterStatus(
             Text
=
message,

             User
=
new
TwitterUser(ScreenName
=
user)
     )

 member
mb.Build()
=

 

buildMessage
(if
message.IsSome

            




then
message.Value

            




else
"")

            



(if
user.IsSome

            




then
user.Value

            




else
"")
[<Fact>]


let
should_not_show_any_tweets_by_me
()
=
    let
messageByMe
=

       (new
MessageBuilder(message
=
"",

              












user
=
"markhneedham")).Build()




                                                             










  let
tweets
=
seq
{
yield
messageByMe;

    
















yield
messageByMe;

    
















yield
messageByMe
}



let
tweetsExcludingMe
=
tweets
|>
excludeSelf











Assert.Equal(0,
tweetsExcludingMe
|>
Seq.length)
Still doing all the processing at
             the end
Asynchronous Workflows
“Perform a computation in a
  background thread without
blocking execution on current
           thread”
Couch
Twitter                   DB
                        Write to




                      Tweets with
            Tweet
 Main                    links
          Processor
                       Processor



          Has link?
What am I learning next?
Programming in a more
    functional way
Loops -> List functions/recursive
Objects -> ADTs
Classes -> Modules/Signatures/Functors
Quotations
Where can you learn more?
Books
Hub FS (http://cs.hubfs.net)
                                          Chris Smith’s Blog
                                 (http://blogs.msdn.com/chrsmith/)

              Wes Dyer’s Blog
    (http://blogs.msdn.com/wesdyer/)

                     On the web
                                           Tomas Petricek’s Blog
     Matthew Podwysocki’s Blog            (http://tomasp.net/blog/)
(http://weblogs.asp.net/Podwysocki/)

                           Robert Pickering’s Wiki
                 (http://www.strangelights.com/FSharp/Wiki/)
Mark Needham
mneedham@thoughtworks.com

Blog: http://www.markhneedham.com/blog
Twitter: markhneedham

More Related Content

Similar to F#: What I've learnt so far

The Open Source... Behind the Tweets
The Open Source... Behind the TweetsThe Open Source... Behind the Tweets
The Open Source... Behind the Tweets
Chris Aniszczyk
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf
hamzadamani7
 
Developing components and extensions for ext js
Developing components and extensions for ext jsDeveloping components and extensions for ext js
Developing components and extensions for ext js
Mats Bryntse
 
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mohammad Shaker
 

Similar to F#: What I've learnt so far (20)

Mobile Fest 2018. Fernando Cejas. What Mom Never Told You About Multi-threading
Mobile Fest 2018. Fernando Cejas. What Mom Never Told You About Multi-threadingMobile Fest 2018. Fernando Cejas. What Mom Never Told You About Multi-threading
Mobile Fest 2018. Fernando Cejas. What Mom Never Told You About Multi-threading
 
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
 
OpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in PythonOpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in Python
 
Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)
 
The Open Source... Behind the Tweets
The Open Source... Behind the TweetsThe Open Source... Behind the Tweets
The Open Source... Behind the Tweets
 
Create a meteor chat app in 30 minutes
Create a meteor chat app in 30 minutesCreate a meteor chat app in 30 minutes
Create a meteor chat app in 30 minutes
 
Meebo performance ny_web_performance
Meebo performance ny_web_performanceMeebo performance ny_web_performance
Meebo performance ny_web_performance
 
Claim Academy Intro to Programming
Claim Academy Intro to ProgrammingClaim Academy Intro to Programming
Claim Academy Intro to Programming
 
PowerShell for SharePoint Developers
PowerShell for SharePoint DevelopersPowerShell for SharePoint Developers
PowerShell for SharePoint Developers
 
Apache Utilities At Work V5
Apache Utilities At Work   V5Apache Utilities At Work   V5
Apache Utilities At Work V5
 
Client Building Functional webapps.
Client   Building Functional webapps.Client   Building Functional webapps.
Client Building Functional webapps.
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf
 
dotnetConf2019 meetup in AICHI / Elmish
dotnetConf2019 meetup in AICHI / ElmishdotnetConf2019 meetup in AICHI / Elmish
dotnetConf2019 meetup in AICHI / Elmish
 
SenchaCon 2010: Developing components and extensions for ext js
SenchaCon 2010: Developing components and extensions for ext jsSenchaCon 2010: Developing components and extensions for ext js
SenchaCon 2010: Developing components and extensions for ext js
 
Developing components and extensions for ext js
Developing components and extensions for ext jsDeveloping components and extensions for ext js
Developing components and extensions for ext js
 
Creating a New iSites Tool
Creating a New iSites ToolCreating a New iSites Tool
Creating a New iSites Tool
 
Scripting as a Second Language
Scripting as a Second LanguageScripting as a Second Language
Scripting as a Second Language
 
Choosing A Web Cms And Intro To Modx
Choosing A Web Cms And Intro To ModxChoosing A Web Cms And Intro To Modx
Choosing A Web Cms And Intro To Modx
 
Ruby and Rails by Example (GeekCamp edition)
Ruby and Rails by Example (GeekCamp edition)Ruby and Rails by Example (GeekCamp edition)
Ruby and Rails by Example (GeekCamp edition)
 
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhone
 

More from Mark Needham

Optimizing cypher queries in neo4j
Optimizing cypher queries in neo4jOptimizing cypher queries in neo4j
Optimizing cypher queries in neo4j
Mark Needham
 
Football graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueFootball graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier League
Mark Needham
 
The Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier LeagueThe Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier League
Mark Needham
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
Mark Needham
 

More from Mark Needham (11)

Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and OperationsNeo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
 
This week in Neo4j - 3rd February 2018
This week in Neo4j - 3rd February 2018This week in Neo4j - 3rd February 2018
This week in Neo4j - 3rd February 2018
 
Building a recommendation engine with python and neo4j
Building a recommendation engine with python and neo4jBuilding a recommendation engine with python and neo4j
Building a recommendation engine with python and neo4j
 
Graph Connect: Tuning Cypher
Graph Connect: Tuning CypherGraph Connect: Tuning Cypher
Graph Connect: Tuning Cypher
 
Optimizing cypher queries in neo4j
Optimizing cypher queries in neo4jOptimizing cypher queries in neo4j
Optimizing cypher queries in neo4j
 
Football graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueFootball graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier League
 
The Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier LeagueThe Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier League
 
Scala: An experience report
Scala: An experience reportScala: An experience report
Scala: An experience report
 
Visualisations
VisualisationsVisualisations
Visualisations
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

F#: What I've learnt so far