-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntroduction.fsx
497 lines (383 loc) · 8.55 KB
/
Introduction.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
(**
- title : Introduction to F#
- description : Introduction to FSharp
- author : Pierre Irrmann
- theme : Default
- transition : default
***
# Introduction to F#
Pierre Irrmann / @pirrmann
"C# dev, F# addict"
***
![F# Paris User Group](./images/fsharp-ug-paris.png "F# Paris User Group")
@FSharpParis
***
## F# big picture
- Functional-first language that runs on the CLR
- Interoperable with all the exisiting .NET ecosystem
- Very low noise/signal ratio
- Open-source compiler written in F#
- Wonderful community!
---
## Functional?
- Functions and Types over classes
- Purity over mutability
- Composition over inheritance
- Higher order functions over method dispatch
- Options over nulls
[www.notonlyoo.org](http://www.notonlyoo.org)
***
## Why F#?
- Conciseness
- Convenience
- Correctness
- Concurrency
- Completeness
[http://fsharpforfunandprofit.com/why-use-fsharp/](http://fsharpforfunandprofit.com/why-use-fsharp/)
---
### Conciseness
*)
// one-liners
[1..100] |> List.sum |> printfn "sum=%d"
// no curly braces, semicolons or parentheses
let square x = x * x
let sq = square 42
// simple types in one line
type Person = {First:string; Last:string}
// complex types in a few lines
type Employee =
| Worker of Person
| Manager of Employee list
// type inference
let jdoe = {First="John";Last="Doe"}
let worker = Worker jdoe
(**
---
### Convenience
*)
// automatic equality and comparison
let person1 = {First="john"; Last="Doe"}
let person2 = {First="john"; Last="Doe"}
let text = sprintf "Equal? %A" (person1 = person2)
(*** include-value: text ***)
// easy composition of functions
let add2times3 = (+) 2 >> (*) 3
let result = add2times3 5
(*** include-value: result ***)
(**
and the REPL!
***
###Isn't F# just for math & compilers?
No.
***
##Main language constructs
- Expressions
- Lists & collections
- Tuples
- Record types
- Functions
- Discriminated Unions
- Options
- Pattern matching
- Classes & Interfaces (yes, really)
- Sequence expressions...
- ... and more!
***
### Basic syntax
#### Let binding
*)
let aValue = "hello world!" // with a cool comment
(**
---
### Basic syntax
#### Type inference for literals
*)
let anInt = 1
let aDouble = 1.
let aDecimal = 2.5M
(**
---
### Basic syntax
#### Immutability is the default
*)
let a = 1
a = 2 // false
let mutable b = 1
b = 2 // still false, what would you expect?
b <- 2
b = 2 // true, now
(**
*)
// a <- 2 doesn't compile, by the way
(**
***
### List and collections (1)
*)
let aList = [1; 2; 3; 4; 5]
let anotherList = 1 :: 2 :: 3 :: 4 :: 5 :: []
let areListEqual = (aList = anotherList)
(**
Are they equal?
*)
(*** include-value: areListEqual ***)
(**
---
### List and collections (2)
*)
let listSyntax = [1; 2; 3]
let arraySyntax = [|1; 2; 3|]
let sequenceSyntax = seq { yield 1; yield 2; yield 3 }
(**
***
### Tuples
*)
let aTuple = 1, 1., "one"
let tupleFirstVal, tupleSecondVal, tupleThirdVal = aTuple
(*** include-value: tupleFirstVal ***)
(*** include-value: tupleSecondVal ***)
(*** include-value: tupleThirdVal ***)
(**
***
### Record types (1)
*)
type Country = {
Name: string
CapitalCity: string
Population: int
}
let france = {
Name = "France"
CapitalCity = "Paris"
Population = 66600000
}
(**
---
### Record types (2)
*)
// a new instance
let alternateFrance = { france with Name = "AltFrance" }
(*** include-value: alternateFrance ***)
(**
***
### Functions (1)
#### (in a functional language, really?)
*)
let format country =
sprintf "%s has a total poulation of %d" country.Name country.Population
let add x y = x + y
let add2 (x, y) = x + y
(**
---
### Functions (2)
#### Types - currying - partial application
*)
let increasePop increment country =
{ country with Population = country.Population + increment }
let rename newName country =
{ country with Name = newName }
let giveBirth = increasePop 1
let biggerFrance = giveBirth france
(** Welcome to a bigger France! *)
(*** include-value: biggerFrance ***)
(**
---
### Functions (3)
#### Piping - composition
*)
let test1 = france |> increasePop 10000 |> rename "Confédération Française"
(*** include-value: test1 ***)
let composition = increasePop 10000 >> rename "Confédération Française"
let test2 = france |> composition
let areCountryEqual = (test1 = test2)
(**
Are they equal?
*)
(*** include-value: areCountryEqual ***)
(**
***
### Discriminated Unions (1)
#### Like enums...
*)
// This just looks like an enum
type Delivery =
| Cash
| Physical
(**
---
### Discriminated Unions (2)
#### Like enums... on steroids
*)
// You can embed values
type ContactType =
| Phone of string
| Email of string
(**
---
### Discriminated Unions (3)
#### Like enums... on steroids
*)
// You can embed values... even of different types
type Amount = decimal // simplified model, no currency
type Discount =
| Percentage of decimal
| Absolute of Amount
| Limited of discount:Discount * limit:Amount
(**
---
### Discriminated Unions (4)
#### Like enums... on steroids
*)
// And of course you can design trees ;)
type Tree<'T> =
| Node of 'T
| Tree of Tree<'T> * Tree<'T>
let aTree =
Tree(
Node 1,
Tree(
Tree(
Tree(Node 2, Node 3),
Tree(
Tree(Node 4, Node 5),
Node 6)),
Tree(
Node 7,
Tree(Node 8, Node 9))))
(**
***
### Options (1)
#### Just a Discriminated Union
*)
// defined as
type Option<'T> =
| Some of 'T
| None
let aResult = Some "result"
let nothing = None
(**
---
### Options (2)
This might seem simple, but that's the reason why YOU DON'T GET ANY NullReferenceException IN F# !
***
### Pattern-matching (1)
*)
let rec serialize tree =
match tree with
| Tree(left, right) -> sprintf "(%s, %s)" (serialize left) (serialize right)
| Node(value) -> value.ToString()
let serializedTree = serialize aTree
(*** include-value: serializedTree ***)
(**
---
### Pattern-matching (2)
*)
let rec cut tree =
match tree with
| Tree(Node _, right) -> cut right
| Tree(left, Node _) -> cut left
| Tree(left, right) -> Tree(cut left, cut right)
| Node(value) -> Node(value)
let cutTree = aTree |> cut |> serialize
(*** include-value: cutTree ***)
(**
***
### Object-oriented constructs (1)
#### Classes
*)
type ParsedValue<'T> (tag:int, value:'T) =
member x.Tag = tag
member x.Value = value
member x.GetValueType() = typeof<'T>
override x.ToString() = sprintf "{Tag: %d; Value: %A}" tag value
let parsed = new ParsedValue<_>(48, "Z Z4")
(**
---
### Object-oriented constructs (2)
#### Interfaces & object expressions
*)
type IMashable =
abstract Mash: paramName:string -> string
let mashable =
{
new IMashable with
member x.Mash(s:string) = sprintf "***-%s-***" s
}
(**
***
### Sequence expressions (1)
*)
let seq1 = seq {
yield 1
yield 2
yield 3
yield 4
}
let seq1Evaluated = seq1 |> Seq.toArray
(*** include-value: seq1Evaluated ***)
(**
---
### Sequence expressions (2)
*)
let seq2 = seq {
yield 1
yield! seq1 // yield a full sequence
yield 2
}
let seq2Evaluated = seq2 |> Seq.toArray
(*** include-value: seq2Evaluated ***)
(**
---
### Sequence expressions (3)
*)
let products = seq {
for i in seq1 do
for j in seq1 do
yield i, j, i * j
}
(*** include-value: products ***)
let productsEvaluated = products |> Seq.toArray
(*** include-value: productsEvaluated ***)
(**
---
### Sequence expressions (4)
*)
let sortedEvenProducts =
products
|> Seq.filter (fun (_, _, x) -> x % 2 = 0)
|> Seq.sortBy (fun (_, _, x) -> x)
let sortedEvenProductsEvaluated = sortedEvenProducts |> Seq.toArray
(*** include-value: sortedEvenProductsEvaluated ***)
(**
***
##Many more language constructs & features
- Units of measures
- Active patterns
- Quotations
- Async workflows (async on steroids)
- Mailbox processors (actors)
- Computation expressions
- Type providers...
and that's only F# 3.1, wait for 4.0!
***
##Forgotten bullets
- IDE Integration? (it's getting better all the time)
- Testing story (FsUnit, FsCheck, TickSpec)
- GUI / Web dev?
- Cross-platform & Xamarin
- DDD / Event sourcing (@thinkb4coding)
- ALT.NET Coding breakfast
***
## Questions?
Don't be shy
---
##Resources
- [Try F#](http://www.tryfsharp.org/)
- [The F# software foundation](http://fsharp.org/)
- [F# for fun and profit](http://fsharpforfunandprofit.com/)
- [F# Koans](https://github.com/ChrisMarinos/FSharpKoans/)
- [F# Paris User Group](http://www.meetup.com/Functional-Programming-in-F/)
---
##Contact
@pirrmann
[www.pirrmann.net](http://www.pirrmann.net)
*)