F#: What I've learnt so far

1,708 views

Published on

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#.

Published in: Technology, Spiritual
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,708
On SlideShare
0
From Embeds
0
Number of Embeds
297
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

F#: What I've learnt so far

  1. 1. F# What I’ve learnt so far Mark Needham
  2. 2. What is F#?
  3. 3. Another language that runs on the CLR
  4. 4. Interoperable with other .NET languages
  5. 5. Functional...
  6. 6. ...but with Object Oriented features too
  7. 7. Statically typed with type inference
  8. 8. Why did I want to learn F#?
  9. 9. Different mindset
  10. 10. Immutability
  11. 11. Concurrency
  12. 12. How did I go about learning F#?
  13. 13. Functions
  14. 14. Can live on their own
  15. 15. let
add
a
b
=
a
+
b http://intwoplacesatonce.com/?p=9
  16. 16. Type inference
  17. 17. let
add
a
b
=
a
+
b val
add
:
int
‐>
int
‐>
int
  18. 18. let
add
(a:string)
(b:string)
=
a
+
b val
add
:
string
‐>
string
‐>
string
  19. 19. Lots of ways to do the same thing
  20. 20. let
withLinks
(statuses:seq<TwitterStatus>)
=
 Seq.filter
 
(fun
s
‐>
s.Text.Contains("http")) 
statuses
  21. 21. let
withLinks
(statuses:seq<TwitterStatus>)
=
 statuses
|>
 Seq.filter
(fun
s
‐>
s.Text.Contains("http"))
  22. 22. Forward
operator
(|>) 
 ('a
‐>
('a
‐>
'b)
‐>
'b)
  23. 23. 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)



  24. 24. Composing functions
  25. 25. let
statuses
=
seq
{
yield
new
TwitterStatus()
} let
sortedStatuses
=
Seq.sortBy
 







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




 







statuses let
firstStatus
=
Seq.hd
sortedStatuses
  26. 26. seq
{
yield
new
TwitterStatus()
}
 |>
Seq.sortBy
(fun
(s:TwitterStatus)
‐>
s.Id)
 |>
Seq.hd
  27. 27. Functions are cool but lack structure
  28. 28. Refactoring to objects
  29. 29. type
Tweets
=
{ 
 
 
 
 
 
 
 TwitterStatuses:seq<TwitterStatus> 
 
 
 
 
 
 }
  30. 30. let
myTweets
=
{
 
 
 
 TwitterStatuses
=
TwitterService.GetStatuses
100 
 
 
 
 



}
  31. 31. 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 }
  32. 32. Leads to simpler/easier to understand code
  33. 33. Helped by unit testing
  34. 34. xUnit.NET
  35. 35. [<Fact>] let
should_recognise_message_with_link_as_a_link
()
= let
messageWithLink
= (new
MessageBuilder(message
=
"http://www.google.com")).Build() Assert.True(messageWithLink
|>
hasLink)

  36. 36. 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
"")
  37. 37. [<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)
  38. 38. Still doing all the processing at the end
  39. 39. Asynchronous Workflows
  40. 40. “Perform a computation in a background thread without blocking execution on current thread”
  41. 41. Couch Twitter DB Write to Tweets with Tweet Main links Processor Processor Has link?
  42. 42. What am I learning next?
  43. 43. Programming in a more functional way
  44. 44. Loops -> List functions/recursive Objects -> ADTs Classes -> Modules/Signatures/Functors
  45. 45. Quotations
  46. 46. Where can you learn more?
  47. 47. Books
  48. 48. 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/)
  49. 49. Mark Needham mneedham@thoughtworks.com Blog: http://www.markhneedham.com/blog Twitter: markhneedham

×