F# for startups


Published on

a talk on F# I gave to the local ".NET startup" meetup group in June 2013

Published in: Technology
  • Thanks, here's a newer version that uses the Github API instead:

    Are you sure you want to  Yes  No
    Your message goes here
  • Please note that the Twitter Search example (slide 25) needs to be updated since Twitter has removed support for the V1 APIs.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • currying
  • Sequence expressions
  • Anonymous Functions, Pattern Matching, Type Inference
  • Static v dynamic typing
  • Explain what the type provider does
  • Explain what the type provider does
  • More about sequence expressions, group by, IQueryable
  • F# for startups

    1. 1. F# for StartupsJoel Grus@joelgrusChief Scientist,
    2. 2. Hello!• About me:– Chief Scientist at VoloMetrixhttp://www.volometrix.comWe’re hiring – Previously at Decide, Bing, Farecast– Started using F# a little over a year ago, am anenthusiast/addict but not an expert
    3. 3. What is F#?F# is a succinct, expressive, andefficient functional and object-oriented language for Microsoft .NETthat helps you write simple code tosolve complex problems.http://research.microsoft.com/en-us/projects/fsharp/
    4. 4. My F# Journey• Once upon a time the VoloMetrix applicationback-end was written entirely in C#• One day I used Ruby (ick!) to prototype very“functional” (and slow) analytics platform• Feel was very F#-y, so started learning andporting• Can develop in F# a lot faster than in C#• Can develop in F# a lot happier than in C#• Today application is a mix of F# and C#projects
    5. 5. Some nice things about F#• Conciseness– Whitespace formatting– Type inference• Convenience– First-class functions– Interactive shell• Correctness– No NULLs (in the normal course of things)– Immutable values• Concurrency– I don’t typically use this, so I’m not going to talk about it!• Completeness– Access to .NET libraries + Visual Studio– Can mutate/iterate when necessaryI stole this list from http://fsharpforfunandprofit.com/
    6. 6. Functional Programming• Is fun!• No single definition, but some combo of– Immutable variables– No side-effects– First-class functions– Lazy evaluation
    7. 7. F# Basics• Type inference• Functions• Combinators• TuplesGoal is not to teach you F# tonight, but to prime yourbrains so that my examples sort of make sense
    8. 8. Type Inference• F# is strictly typed, but usually you don’t haveto tell it the typesIt figures them out like magic!• If you need to specify types, they go afterval f : x:int -> intval g : x:float -> floatval h : x:string -> string
    9. 9. Functions• Functions are just objects• Anonymous functions are easy tooval applyTwice : f:(a -> a) -> x:a -> aval square : x:int -> intval fourthPower : (int -> int)
    10. 10. Combinators• |> pipes values into functionsTake the array [1,2,3] Send it through an “even filter” Send that to a length function
    11. 11. Tuples• Easy way of creating compound types
    12. 12. Let’s Do Some ExamplesPunchline will always be some variation of“Hey, look how clean and safe and simple my code isand how fast I wrote it!”Every one of these things is nice in a start-up
    13. 13. Contrived Example – Discriminated Unions• Imagine we had no bool type• Could define one in C# using an enum:Definition is simpleenoughYou’d hope we’dnever get hereThis can’t end well
    14. 14. Contrived Example – Discriminated Unions• In F# would do the following:• Punchline: F# version is cleaner and safer
    15. 15. Types for Business Logic• Want to represent “meetings”• A meeting has– Start Date– End Date– Subject– Invitees• Each invitee is a Person, and a Response• A Person can be have a Name or be Anonymous• A Response can be “Accept” or “Decline”
    16. 16. Types for Business Logic
    17. 17. Types for Business LogicGiven a meeting, how many Invitees accepted?How many Invitees were anonymous?val NumAccepts : m:Meeting -> int
    18. 18. Discriminated Unions for Business LogicGiven a meeting, how many Invitees accepted?How many Invitees were anonymous?What can we factor out?val CountInvitees : predicate:(Invitee -> bool) -> m:Meeting -> int
    19. 19. Discriminated Unions for Business LogicGiven a meeting, how many Invitees accepted?How many Invitees were anonymous?val NumAccepts2 : m:Meeting -> intval NumAccepts3 : m:Meeting -> intUse currying!
    20. 20. Discriminated Unions for Business Logic• Punchline:– Types make business logic simple to implement– First-class functions make abstraction andrefactoring quick and easy
    21. 21. Contrived Example – ValueOrDefault• Want to get a value out of a dictionary, or adefault if the key’s not there
    22. 22. Contrived Example - ValueOrDefault• What if we want it generic?
    23. 23. Contrived Example - ValueOrDefault• Same code in F#Don’t have to specify types touse generic!val ValueOrDefault : dict:Dictionary<a,b> -> key:a -> defaultValue:b -> bPunchline: Takes less code than C#, is morereadable (for me)
    24. 24. Fun Example – JSON Type Provider• Want to get tweets in a lightweight way• Sounds like a job for Python!15F# ( Bot) F# is a simple, succinct, efficient functional programming language for.NET...Igor Brejc RT @dsyme: If youre into open/cross-platform .NET, F# or C# but mainly usewindows, please read this testing/dev technique http://t.co/poTiUcSsXeF# ( Bot) F# lets users concentrate on the problems they are solving rather thangetting lost in programming details.Onorio Catenacci RT @cdrnet: Blog: Test your #csharp/#fsharp code on mono 3.0.10/debianusing #vagrant without leaving Windows: http://t.co/CtGnVn06Cd…
    25. 25. Fun Example – JSON Type ProviderOr for F#!
    26. 26. Fun Example – JSON Type Provider• Punchline– Easy to bang out really quick prototypes– Get flexibility of a scripting language like Pythonbut with type safety– .NET integration means easy to build yourprototypes into full-fledged applications
    27. 27. Useful Example – SQL Type ProviderThis was the most generic database schema I could think of!
    28. 28. Useful Example – SQL Type Provider• Punchline: Get to work with typed databaseobjects for free, great for complex analytics(or external libraries) with no SQL equivalent
    29. 29. F# is not Perfect• Life is dull without NullReferenceException• Tooling is not on par with C#• Hard to organize projects, file order matters• Everyone knows C#, no one knows F#• P(zealot | knows F#) is very high!• Your code will be so unexpectedly good thatpeople will mistake you for some sort of guruand then invite you to give talks that are wayoutside of your comfort zone!
    30. 30. Resources• http://www.tryfsharp.org/• http://fsharp.org• http://fsharpforfunandprofit.com/• http://en.wikibooks.org/wiki/F_Sharp_Programming• Lots of F# people on Twitter, modify my code to findthem!• There are some good books out there: Expert F# andF# Programming are two that I like• Ask me, I know a few things