f# for startups v2

31
F# for Startups Joel Grus @joelgrus Chief Scientist,

Upload: joelgrus

Post on 10-May-2015

299 views

Category:

Technology


2 download

DESCRIPTION

A newer version of F# for Startups that I gave to the F# User Group. This one has a JSON example that works.

TRANSCRIPT

Page 1: F# for startups v2

F# for Startups

Joel Grus@joelgrus

Chief Scientist,

Page 2: F# for startups v2

Hello!

• About me:– Chief Scientist at VoloMetrix– Previously at Decide, Bing, Farecast– Started using F# a little over a year ago, am an

enthusiast/addict but not an expert– Also write a lot of Python, a fair amount of

JavaScript, and a tiny amount of Clojure

Page 3: F# for startups v2

VoloMetrix

“Social Enterprise Intelligence”• Analyze email and calendar data to learn– Who’s connected to whom, and– Where is people’s time going

• In order to help them do their jobs better!• http://www.volometrix.com• We’re hiring

Page 4: F# for startups v2

What is F#?

F# is a succinct, expressive, and efficient functional and object-oriented language for Microsoft .NET that helps you write simple code to solve complex problems.

http://research.microsoft.com/en-us/projects/fsharp/

Page 5: F# for startups v2

My F# Journey• Once upon a time the VoloMetrix application

back-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 and

porting• 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– Analytics mostly written in F#– Plumbing mostly written in C#

Page 6: F# for startups v2

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/

Page 7: F# for startups v2

Functional Programming

• Is fun!• No single definition, but some combo of– First-class functions– Immutable variables– No side-effects– Lazy evaluation

Page 8: F# for startups v2

F# Basics

• Type inference• Functions• Combinators• Tuples

Goal is not to teach you F# tonight, but to prime your brains so that my examples sort of make sense

Page 9: F# for startups v2

Type Inference• F# is strictly typed, but usually you don’t have

to tell it the types

It figures them out like magic!

• If you need to specify types, they go after

val f : x:int -> intval g : x:float -> floatval h : x:string -> string

Page 10: F# for startups v2

Functions• Functions are just objects

• Anonymous functions are easy too

val applyTwice : f:('a -> 'a) -> x:'a -> 'aval square : x:int -> intval fourthPower : (int -> int)

Page 11: F# for startups v2

Combinators

• |> pipes values into functions

• Easy to build elaborate data-processing pipelines

• (Which are difficult to debug)

Take the array [1,2,3] Send it through an “even filter” Send that to a length function

Page 12: F# for startups v2

Tuples

• Easy way of creating compound types

• Available (but wordier + less common) in C# – var pair = new Tuple<int,string>(1,”1”)

Page 13: F# for startups v2

Let’s Do Some Examples

Punchline will always be some variation of“Hey, look how clean and safe and simple my code is and how fast I wrote it!”

Every one of these things is nice in a start-up

Page 14: F# for startups v2

Contrived Example – Discriminated Unions

• Imagine we had no bool type• Could define one in C# using an enum:

Definition is simple enough

You’d hope we’d never get here

This can’t end well

Page 15: F# for startups v2

Contrived Example – Discriminated Unions

• In F# would do the following:

• Punchline: F# version is cleaner and safer

Page 16: F# for startups v2

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”

Page 17: F# for startups v2

Types for Business Logic

Page 18: F# for startups v2

Types for Business Logic

Given a meeting, how many Invitees accepted? How many Invitees were anonymous?

val NumAccepts : m:Meeting -> int

Page 19: F# for startups v2

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

Page 20: F# for startups v2

Discriminated Unions for Business LogicGiven a meeting, how many Invitees accepted? How many Invitees were anonymous?

val NumAccepts2 : m:Meeting -> int

val NumAccepts3 : m:Meeting -> int

Use currying!

Page 21: F# for startups v2

Discriminated Unions for Business Logic

• Punchline:– Types make business logic simple to implement– First-class functions make abstraction and

refactoring quick and easy

Page 22: F# for startups v2

Contrived Example – ValueOrDefault

• Want to get a value out of a dictionary, or a default if the key’s not there

Page 23: F# for startups v2

Contrived Example - ValueOrDefault

• What if we want it generic?

Page 24: F# for startups v2

Contrived Example - ValueOrDefault• Same code in F#

Don’t have to specify types to use generic!

val ValueOrDefault : dict:Dictionary<'a,'b> -> key:'a -> defaultValue:'b -> 'b

Punchline: Takes less code than C#, is more readable (for me)

Page 25: F# for startups v2

Fun Example – JSON Type Provider• Want to get tweets in a lightweight way• Sounds like a job for Python!

Page 26: F# for startups v2

Fun Example – JSON Type Provider

Or for F#!

Page 27: F# for startups v2

Fun Example – JSON Type Provider

• Punchline– Easy to bang out really quick prototypes– Get flexibility of a scripting language like Python

but with type safety– .NET integration means easy to build your

prototypes into full-fledged applications

Page 28: F# for startups v2

Useful Example – SQL Type Provider

This was the most generic database schema I could think of!

Page 29: F# for startups v2

Useful Example – SQL Type Provider

• Punchline: Get to work with typed database objects for free, great for complex analytics (or external libraries) with no SQL equivalent

Page 30: F# for startups v2

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 that

people will mistake you for some sort of guru and then invite you to give talks that are way outside of your comfort zone!

Page 31: F# for startups v2

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• There are some good books out there: Expert F# and

F# Programming are two that I like• Ask me, I know a few things