F#: What I've learnt so far

Post on 10-May-2015

1.420 views 2 download

Tags:

description

A talk I gave to the Sydney Alt.NET group on Tuesday 30th June 2009 about some of the stuff I've learnt playing around with F#.

Transcript of F#: What I've learnt so far

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

letaddab=a+b

http://intwoplacesatonce.com/?p=9

Type inference

letaddab=a+b

valadd:int‐>int‐>int

letadd(a:string)(b:string)=a+b

valadd:string‐>string‐>string

Lots of ways to do the same thing

letwithLinks(statuses:seq<TwitterStatus>)=Seq.filter(funs‐>s.Text.Contains("http"))statuses

letwithLinks(statuses:seq<TwitterStatus>)=statuses|>Seq.filter(funs‐>s.Text.Contains("http"))

Forwardoperator(|>)

('a‐>('a‐>'b)‐>'b)

letwithLinks(statuses:seq<TwitterStatus>)=Seq.filter(funs‐>hasLinks)statuses

letwithLinks(statuses:seq<TwitterStatus>)=statuses|>Seq.filter(funs‐>hasLinks)

letwithLinksstatuses=statuses|>Seq.filter(fun(s:TwitterStatus)‐>hasLinks)

letwithLinks(statuses:seq<TwitterStatus>):seq<TwitterStatus>=statuses|>Seq.filter(funs‐>hasLinks)

letwithLinks:seq<TwitterStatus>‐>seq<TwitterStatus>=Seq.filter(funs‐>hasLinks)

Composing functions

letstatuses=seq{yieldnewTwitterStatus()}

letsortedStatuses=Seq.sortBy(fun(s:TwitterStatus)‐>s.Id)statuses

letfirstStatus=Seq.hdsortedStatuses

seq{yieldnewTwitterStatus()}|>Seq.sortBy(fun(s:TwitterStatus)‐>s.Id)|>Seq.hd

Functions are cool but lack structure

Refactoring to objects

typeTweets={ TwitterStatuses:seq<TwitterStatus> }

letmyTweets={ TwitterStatuses=TwitterService.GetStatuses100 }

letwithLinks(statuses:seq<TwitterStatus>)=statuses|>Seq.filter(funs‐>s.Text.Contains("http"))letprint(statuses:seq<TwitterStatus>)=forsinstatusesdoprintfn"[%s]%s"s.User.ScreenNamestatus.Text

typeTweetswithmembert.print()=printt.TwitterStatusesmembert.withLinks()=

{TwitterStatuses=withLinkst.TwitterStatuses}

Leads to simpler/easier to understand code

Helped by unit testing

xUnit.NET

[<Fact>]letshould_recognise_message_with_link_as_a_link()=letmessageWithLink=(newMessageBuilder(message="http://www.google.com")).Build()

Assert.True(messageWithLink|>hasLink)

typeMessageBuilder(?message:string,?user:string)=letbuildMessagemessageuser=

newTwitterStatus(Text=message,User=newTwitterUser(ScreenName=user)

)

membermb.Build()=buildMessage(ifmessage.IsSome

thenmessage.Valueelse"")(ifuser.IsSomethenuser.Valueelse"")

[<Fact>]letshould_not_show_any_tweets_by_me()=

letmessageByMe=(newMessageBuilder(message="",

user="markhneedham")).Build()lettweets=seq{yieldmessageByMe;yieldmessageByMe;yieldmessageByMe}

lettweetsExcludingMe=tweets|>excludeSelfAssert.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”

Main TweetProcessor

Tweets with links

Processor

Has link?

Twitter Couch DB

Write to

What am I learning next?

Programming in a more functional way

Loops -> List functions/recursiveObjects -> ADTsClasses -> Modules/Signatures/Functors

Quotations

Where can you learn more?

Books

On the web

Hub FS (http://cs.hubfs.net)

Robert Pickering’s Wiki(http://www.strangelights.com/FSharp/Wiki/)

Chris Smith’s Blog(http://blogs.msdn.com/chrsmith/)

Matthew Podwysocki’s Blog(http://weblogs.asp.net/Podwysocki/)

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

Tomas Petricek’s Blog(http://tomasp.net/blog/)

Mark Needhammneedham@thoughtworks.com

Blog: http://www.markhneedham.com/blogTwitter: markhneedham