33
MADRID F# MEETUP Introduction to F#

Madrid F# Meetup: Introduction to F#

Embed Size (px)

Citation preview

Page 1: Madrid F# Meetup: Introduction to F#

MADRID F# MEETUP

Introduction to F#

Page 2: Madrid F# Meetup: Introduction to F#

MADRID F# MEETUP

About meAlfonso García-CaroIndependent DeveloperStudied IT but got degree in LinguisticsJapanese speaker.NET fanboyStarted F# recentlyMore pragmatic than theoretical

@alfonsogcnunezgithub.com/alfonsogarciacaro

Page 3: Madrid F# Meetup: Introduction to F#

MADRID F# MEETUP

About this talkNot official nor comprehensiveMostly based in personal experienceNot an introduction to functional programmingPlease ask if you need more detailWill “borrow” from other F#ersParticularly @ScottWlaschin

fsharp.orgfsharpforfunandprofit.com

Page 4: Madrid F# Meetup: Introduction to F#

What is F#?

MADRID F# MEETUP

F# is an open source, cross-platform, strongly-typed, functional-first programming language. It offers interoperability with all .NET languages and libraries and data-rich analytical services like type providers and LINQ queries.

Page 5: Madrid F# Meetup: Introduction to F#

FunScript F# to JavaScript with type providersMADRID F# MEETUP

Page 6: Madrid F# Meetup: Introduction to F#

F# is... robust

MADRID F# MEETUP

Its powerful type system prevents many common errors (null references) and allows you to encode business logic to make illegal states unrepresentable.

Yaron minsky via

fsharpforfunandprofit.com

Page 7: Madrid F# Meetup: Introduction to F#

MADRID F# MEETUP

Model Problem: A contact must havean email or a postal address

Naïve solution

Solution withunion types

Page 8: Madrid F# Meetup: Introduction to F#

F# types: Tuples

MADRID F# MEETUP

• No explicit names• Can have any number of components• Components can be generic o complex types• Most often used to return multiple values from private functions

Page 9: Madrid F# Meetup: Introduction to F#

F# types: Records

MADRID F# MEETUP

• Like tuples with labels• Structural equality by default• Can have members

Page 10: Madrid F# Meetup: Introduction to F#

F# types: Discriminated Unions

MADRID F# MEETUP

• Mostly used with pattern matching• Can be recursive• Can have field names (tuple labels)• Empty cases possible• Option union instead of null

Page 11: Madrid F# Meetup: Introduction to F#

F# types: Discriminated Unions

MADRID F# MEETUP

Construction

Pattern matching:Check case anddeconstruct all-in-one

Page 12: Madrid F# Meetup: Introduction to F#

F# types: unit

MADRID F# MEETUP

F# types: AbbreviationsUse an alias for convenience (no type-safe)

• No void functions• Single value: ()• Automatically ignored

These ones are built-in

Page 13: Madrid F# Meetup: Introduction to F#

F# types: Collections

MADRID F# MEETUP

• ImmutableoRecursive listsoSet and mapsoLazy sequences

• MutableoArrayoBuilt-in .NET collections: Lists, Dictionaries...

• Also ranges and comprehensions All collect

ions

are gene

ric

Many built-in functionsto deal with sequences:map, reduce, fold, find...

Page 14: Madrid F# Meetup: Introduction to F#

F# types: Built-in .NET Types

MADRID F# MEETUP

• Built-in value types: int, bool...• Built-in reference types: string...• User-defined value types: enum and struct• Classes and interfaces• Delegates

Not the same asF# functions butthey can be convertedAlso pointers,

but they

are not usually needed

Page 15: Madrid F# Meetup: Introduction to F#

FunScript F# to JavaScript with type providersMADRID F# MEETUP

YOU SAY STATIC TYPING

I HEAR BOILERPLATE

Page 16: Madrid F# Meetup: Introduction to F#

F# is... agile

MADRID F# MEETUP

Type inference reduces the number of type annotations, bringing together the benefits of static typing and the conciseness of dynamic typing.

C#F#

Page 17: Madrid F# Meetup: Introduction to F#

F# reduces code “noise”

MADRID F# MEETUP

• No curly braces for scope• No semicolons• Meaningful whitespace like Python• No return keyword

But you can use themif necessary

C# F#

Forward pipe operator

Page 18: Madrid F# Meetup: Introduction to F#

Pattern Matching

MADRID F# MEETUP

Match and deconstruct complex patterns with no hassle

Wildcard

ConstantEmpty

list

Guard

The compiler

warns you if

you don’t cover

all cases

Page 19: Madrid F# Meetup: Introduction to F#

Pattern Matching

MADRID F# MEETUP

Define your own active patterns

Syntac

tic

sugar

Page 20: Madrid F# Meetup: Introduction to F#

Thinking sequentially (1)

MADRID F# MEETUP

Use computation expressions (aka monads) to simplify complex programming workflows and simulate state machines Perform

asynchronousoperations

as ifthey weresynchronous

Page 21: Madrid F# Meetup: Introduction to F#

Thinking sequentially (2)

MADRID F# MEETUP

Define your own workflows easily

Can return Som

e(value)

or None

... and without

With...

Page 22: Madrid F# Meetup: Introduction to F#

Thinking sequentially (and 3)

MADRID F# MEETUP

Parallelism made easy thanks to immutability and Async

Erlang-like agentsalso available

Parallel I

/O

Parallel C

PU

Page 23: Madrid F# Meetup: Introduction to F#

Domain Driven Design

MADRID F# MEETUP

Programmers and non-programmers can speak the same language

Believe it!

Page 25: Madrid F# Meetup: Introduction to F#

Scripting

MADRID F# MEETUP

With F# you can write both complex project and simple scripts

There is a REPL too

Page 26: Madrid F# Meetup: Introduction to F#

FunScript F# to JavaScript with type providersMADRID F# MEETUP

IALREADY

KNEWALL

THAT

Page 27: Madrid F# Meetup: Introduction to F#

Killer Features: Measure Units

MADRID F# MEETUP

Protect your number values for free!

Combine them with other types

Use generics

The compiler prevents mistakes

The measure annotationsdisappear after compilation sothey have no performance penalty

Page 28: Madrid F# Meetup: Introduction to F#

FunScript F# to JavaScript with type providersMADRID F# MEETUP

There are morekiller featuresbut let’s godirectly to

the crown jewel

Page 30: Madrid F# Meetup: Introduction to F#

FunScript F# to JavaScript with type providersMADRID F# MEETUP

I WANT NOACADEMIC STUFF

I WANNA MAKE REAL APPS!

Page 31: Madrid F# Meetup: Introduction to F#

F# is multiplatform

MADRID F# MEETUP

• .NET: Windows Desktop and Store, Windows Phone• Mono: Mac and Linux• Xamarin: Android and iOS• ASP.NET: Server• FunScript and WebSharper: Browser• GPU, FreeBSD...

more at fsharp.org

Page 32: Madrid F# Meetup: Introduction to F#

FunScript F# to JavaScript with type providersMADRID F# MEETUP

DEMO TIME

Page 33: Madrid F# Meetup: Introduction to F#

Why F#? Why now?

MADRID F# MEETUP

• Functional programming is booming• Great community, really open to newcomers• Everybody can contribute to the evolution of the

language• Open source but backed by big companiesoMicrosoftoXamarin

• fun is a keyword!