This document summarizes an introduction to F# presentation given at a Madrid F# meetup. The presenter introduces themselves and explains that the talk will provide an overview of F# based on their personal experience, and will focus more on practical than theoretical aspects. They then discuss what F# is, its type system including tuples, records, discriminated unions and built-in types, as well as features like pattern matching, units of measure, and type providers. Real-world applications and platforms that F# supports are also mentioned.
2. MADRID F# MEETUP
About me
Alfonso García-Caro
Independent Developer
Studied IT but got degree in Linguistics
Japanese speaker
.NET fanboy
Started F# recently
More pragmatic than theoretical
@alfonsogcnunez
github.com/alfonsogarciacaro
3. MADRID F# MEETUP
About this talk
Not official nor comprehensive
Mostly based in personal experience
Not an introduction to functional programming
Please ask if you need more detail
Will “borrow” from other F#ers
Particularly @ScottWlaschin
fsharp.org
fsharpforfunandprofit.com
4. MADRID F# MEETUP
What is F#?
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.
5. FunScript
F# to JavaScript with type providers MADRID F# MEETUP
6. MADRID F# MEETUP
F# is... robust
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
7. MADRID F# MEETUP Model Problem: A contact must have
an email or a postal address
Naïve solution
Solution with
union types
8. MADRID F# MEETUP
F# types: Tuples
• 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
9. MADRID F# MEETUP
F# types: Records
• Like tuples with labels
• Structural equality by default
• Can have members
10. MADRID F# MEETUP
F# types: Discriminated Unions
• Mostly used with pattern matching
• Can be recursive
• Can have field names (tuple labels)
• Empty cases possible
• Option union instead of null
11. MADRID F# MEETUP
F# types: Discriminated Unions
Construction
Pattern matching:
Check case and
deconstruct all-in-one
12. MADRID F# MEETUP
F# types: unit
• No void functions
• Single value: ()
• Automatically ignored
F# types: Abbreviations
Use an alias for convenience (no type-safe)
These ones are built-in
13. MADRID F# MEETUP
F# types: Collections
Many built-in functions
to deal with sequences:
map, reduce, fold, find...
• Immutable
oRecursive lists
oSet and maps
oLazy sequences
• Mutable
oArray
oBuilt-in .NET collections: Lists, Dictionaries...
• Also ranges and comprehensions
All collections
are generic
14. MADRID F# MEETUP
F# types: Built-in .NET Types
• 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 as
F# functions but
they can be converted Also pointers, but they
are not usually needed
15. FunScript
F# to JavaScript with type providers MADRID F# MEETUP
YOU SAY STATIC TYPING
I HEAR BOILERPLATE
16. MADRID F# MEETUP
F# is... agile
Type inference reduces the number of type
annotations, bringing together the benefits of static
typing and the conciseness of dynamic typing. C#
F#
17. MADRID F# MEETUP
F# reduces code “noise”
But you can use them
• No curly braces for scope
• No semicolons
• Meaningful whitespace like Python
• No return keyword
if necessary
C#
F#
Forward pipe operator
18. MADRID F# MEETUP
Pattern Matching
Match and deconstruct complex patterns with no hassle
Wildcard
Constant
Empty
list
Guard
The compiler
warns you if
you don’t cover
all cases
19. MADRID F# MEETUP
Pattern Matching
Define your own active patterns
Syntactic
sugar
20. MADRID F# MEETUP
Thinking sequentially (1)
Use computation expressions (aka monads) to simplify
complex programming workflows and simulate state machines
Perform
asynchronous
operations
as if
they were
synchronous
21. MADRID F# MEETUP
Thinking sequentially (2)
Define your own workflows easily
Can return Some(value)
or None
... and without
With...
22. MADRID F# MEETUP
Thinking sequentially (and 3)
Parallelism made easy thanks to immutability and Async
Erlang-like agents
also available
Parallel I/O
Parallel CPU
23. MADRID F# MEETUP
Domain Driven Design
Programmers and non-programmers can
Believe it!
speak the same language
25. MADRID F# MEETUP
Scripting
With F# you can write both complex project
and simple scripts
There is a REPL too
26. FunScript
F# to JavaScript with type providers MADRID F# MEETUP
I
ALREADY
KNEW
ALL
THAT
27. MADRID F# MEETUP
Killer Features: Measure Units
Protect your number values for free!
Combine them with other types
Use generics
The measure annotations
disappear after compilation so
they have no performance penalty
The compiler prevents mistakes
28. FunScript
F# to JavaScript with type providers MADRID F# MEETUP
There are more
killer features
but let’s go
directly to
the crown jewel
29. MADRID F# MEETUP
Killer Features: Type Providers
Static typing on demand!
Fsharp.Data
Sergey Tihon’s blog
30. FunScript
F# to JavaScript with type providers MADRID F# MEETUP
I WANT NO
ACADEMIC STUFF
I WANNA MAKE REAL APPS!
31. MADRID F# MEETUP
F# is multiplatform
• .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
32. FunScript
F# to JavaScript with type providers MADRID F# MEETUP
DEMO TIME
33. MADRID F# MEETUP
Why F#? Why now?
• 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 companies
oMicrosoft
oXamarin
• fun is a keyword!