Upload
alfonso-garcia-caro
View
575
Download
4
Embed Size (px)
Citation preview
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
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
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.
FunScript F# to JavaScript with type providersMADRID F# MEETUP
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
MADRID F# MEETUP
Model Problem: A contact must havean email or a postal address
Naïve solution
Solution withunion types
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
F# types: Records
MADRID F# MEETUP
• Like tuples with labels• Structural equality by default• Can have members
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
F# types: Discriminated Unions
MADRID F# MEETUP
Construction
Pattern matching:Check case anddeconstruct all-in-one
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
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...
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
FunScript F# to JavaScript with type providersMADRID F# MEETUP
YOU SAY STATIC TYPING
I HEAR BOILERPLATE
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#
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
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
Pattern Matching
MADRID F# MEETUP
Define your own active patterns
Syntac
tic
sugar
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
Thinking sequentially (2)
MADRID F# MEETUP
Define your own workflows easily
Can return Som
e(value)
or None
... and without
With...
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
Domain Driven Design
MADRID F# MEETUP
Programmers and non-programmers can speak the same language
Believe it!
MADRID F# MEETUP
Source
Scripting
MADRID F# MEETUP
With F# you can write both complex project and simple scripts
There is a REPL too
FunScript F# to JavaScript with type providersMADRID F# MEETUP
IALREADY
KNEWALL
THAT
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
FunScript F# to JavaScript with type providersMADRID F# MEETUP
There are morekiller featuresbut let’s godirectly to
the crown jewel
Killer Features: Type Providers
MADRID F# MEETUP
Static typing on demand!
Fsharp.DataSergey Tihon’s blog
FunScript F# to JavaScript with type providersMADRID F# MEETUP
I WANT NOACADEMIC STUFF
I WANNA MAKE REAL APPS!
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
FunScript F# to JavaScript with type providersMADRID F# MEETUP
DEMO TIME
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!