3. “F# is excellent at concisely expressing
business and domain logic.”
“Developers trying to achieve explicit business logic within
an application may opt to
express their domain in F#
with the majority of plumbing code in C#.”
Thoughtworks Tech Radar
March 2012 - Quotes
12. F# Map/Reduce C# Map/Reduce
let map f xs = seq { public static
for x in xs do IEnumerable<R> Map<T, R>
(this IEnumerable<T> xs,
yield f x Func<T, R> f)
} {
foreach (var x in xs)
let reduce f init items = yield return f(x);
let mutable current = init }
for item in items do public static R
current <- f current item Reduce<T, R>
current (this IEnumerable<T> xs,
R init,
Func<R, T, R> f)
{
var current = init;
foreach (var x in xs)
current = f(current, x);
return current;
}
Higher Order Functions
13. F# C#
match day with switch (day) {
| 0 -> "Sunday" case 0: return "Sunday";
case 1: return "Monday";
| 1 -> "Monday" case 2: return "Tuesday";
| 2 -> "Tuesday" case 3: return "Wednesday";
| 3 -> "Wednesday" case 4: return "Thursday";
| 4 -> "Thursday" case 5: return "Friday";
| 5 -> "Friday" case 6: return "Saturday";
default:
| 6 -> "Saturday" throw new
| _ –> ArgumentException("day");
invalidArg "Invalid day" }
Pattern Matching
15. F# C#
type Person(name:string,age:int) = public class Person
{
public Person(string name, int age)
/// Full name {
member person.Name = name _name = name;
_age = age;
/// Age in years }
member person.Age = age
private readonly string _name;
private readonly int _age;
/// <summary>
/// Full name
/// </summary>
public string Name
{
get { return _name; }
}
/// <summary>
/// Age in years
/// </summary>
public int Age
{
get { return _age; }
}
}
Light Syntax: POCOs
16. C# F#
public class VerySimpleStockTraderImpl : IAutomatedStockTrader type VerySimpleStockTraderImpl
{ (analysisService:IStockAnalysisService,
private readonly IStockAnalysisService analysisService;
private readonly IOnlineBrokerageService brokerageService; brokerageService:IOnlineBrokerageService) =
member this.ExecuteTrades() = () // ...
public VerySimpleStockTraderImpl(
IStockAnalysisService analysisService,
IOnlineBrokerageService brokerageService)
{
this.analysisService = analysisService;
this.brokerageService = brokerageService;
}
public void ExecuteTrades()
{
// ...
}
}
Light Syntax: DI
17. F# NUnit C# NUnit
module MathTest = using NUnit.Framework;
open NUnit.Framework [TestFixture]
public class MathTest
{
let [<Test>] ``2 + 2 should equal 4``() =
[Test]
Assert.AreEqual(2 + 2, 4) public void
TwoPlusTwoShouldEqualFour()
{
Assert.AreEqual(2 + 2, 4);
}
}
Unit Testing
20. 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
Units in Cells
24. @rickasaurus
@tomaspetricek
@jonharrop
Meet the F#ers
Twitter #fsharp
25. #chtd Quote from "future of .net"
Have a look at F# for two days
and you will find yourself two
years ahead of your peer .net
devs. #fsharp
@jbandi