8. SUMMARY
I can fit the whole F# solution in the blank lines of the C# solution
with 7,000 lines to spare.
WTF C#! OMG F#!
I have spent most of my life writing buggy, bloated software that was
impossible to reason about. What was I thinking?
11. CURRENCY CONVERSIONS
[<Measure>] type EUR
[<Measure>] type GBP
let rateEurGbp = 0.783M<GBP/EUR>
// Converts amount in EUR to GBP
let euroToPounds (eur:decimal<EUR>) = eur * rateEurGbp
12. UNITS IN CELLS
type formula =
| Neg of formula
| Exp of formula * formula
| ArithmeticOp of
formula * arithmetic * formula
| LogicalOp of
formula * logical * formula
| Num of UnitValue
| Ref of int * int
| Range of int * int * int * int
| Fun of string * formula list
15. AUTOMATING 2048 WITH CANOPY
start firefox
url "http://gabrielecirulli.github.io/2048/
press up
press left
press right
let score = element ".score-container"
printfn "Score %s" score.Text
18. FUNCTIONAL C# - MAP/REDUCE
using System;
using System.Collections.Generic;
public class Enumerable
{
public static IEnumerable<TResult> Map<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> func)
{
foreach(var item in source)
{
yield return func(item);
}
}
public static TAccumulate Reduce<TSource,TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func)
{
var current = seed;
foreach(var item in source)
{
current = func(current, item);
}
return current;
}
}
19. HIGHER ORDER FUNCTIONS – C#
MAP
public static IEnumerable<TResult> Map<TSource, TResult>(this IEnum
{
foreach(var item in source)
{
yield return func(item);
}
}
20. HIGHER ORDER FUNCTIONS – F#
MAP/REDUCE
let map func items =
for item in items do
func item
let reduce func seed items =
let mutable acc = seed
for item in items do
acc <- func acc
acc
23. C#/F# INTEROP (MOSTLY IT JUST
WORKS)
C# friendly
Classes
Interfaces
Modules
Records
Tuples(*)
F# specific
Discriminated unions
Function arguments
Curried arguments
Option
Async
27. F# KOANS
[<Koan>]
let SquareEvenNumbersWithPipelineOperator() =
(* In F#, you can use the pipeline operator to get the benefit of
the parens style with the readability of the statement style. *)
let result =
[0..5]
|> List.filter isEven
|> List.map square
AssertEquality result __