f# eye for the c# guy - f(by) minsk 2014

26
Phil Trelford, @ptrelford #FuncBY Minsk, 2014

Upload: phillip-trelford

Post on 17-Jul-2015

934 views

Category:

Software


3 download

TRANSCRIPT

Phil Trelford, @ptrelford

#FuncBY Minsk, 2014

Statically Typed

Functional First

Object Orientated

Open Source

.Net language

In Visual Studio

& Xamarin Studio

Kaggle

The fact that F# targets the CLR was also critical

we have a large existing code base in C#,

getting started with F# was an easy decision.

The F# code is

consistently shorter,

easier to read,

easier to refactor and contains far fewer bugs.

…we’ve become

more productive.

Phil Trelford, @ptrelford

#FuncBY Minsk, 2014

F#

type Person(name:string,age:int) =/// Full namemember person.Name = name/// Age in yearsmember person.Age = age

C#

public class Person{

public Person(string name, int age){

_name = name;_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>

F#

type VerySimpleStockTrader

(analysisService:IStockAnalysisService,

brokerageService:IOnlineBrokerageService) =

member this.ExecuteTrades() =

() // ...

C#

public class VerySimpleStockTrader{

private readonlyIStockAnalysisService analysisService;

private readonlyIOnlineBrokerageService brokerageService;

public VerySimpleStockTrader(IStockAnalysisService analysisService,IOnlineBrokerageService brokerageService)

{this.analysisService = analysisService;this.brokerageService = brokerageService;

}

public void ExecuteTrades(){

// ...}

}

F# NUnit

module MathTest =

open NUnit.Framework

let [<Test>] ``2 + 2 should equal 4``() =Assert.AreEqual(2 + 2, 4)

C# NUnit

using NUnit.Framework;

[TestFixture]public class MathTest{

[Test]public void TwoPlusTwoShouldEqualFour(){

Assert.AreEqual(2 + 2, 4);}

}

F# Foq

let ``order sends mail if unfilled``() =// setup datalet order = Order("TALISKER", 51)let mailer = mock()order.SetMailer(mailer)// exerciseorder.Fill(mock())// verify

verify <@ mailer.Send(any()) @> once

C# Moq

public void OrderSendsMailIfUnfilled(){

// setup datavar order = new Order("TALISKER", 51);var mailer = new Mock<MailService>(); order.SetMailer(mailer.Object);// exerciseorder.Fill(Mock.Of<Warehouse>());// verifymailer.Verify(mock =>

mock.Send(It.IsAny<string>()), Times.Once());

}

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

open FSharp.Data

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>

let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """)

Simple.Age

Phil Trelford, @ptrelford

#FuncBY Minsk, 2014

F# Software Foundation

http://www.fsharp.org

software stacks

trainings teaching F# user groups snippets

mac and linux cross-platform books and tutorials

F# community open-source MonoDevelop

contributions research support consultancy mailing list

//---------------------------------------------------------------// About Let//// The let keyword is one of the most fundamental parts of F#.// You'll use it in almost every line of F# code you write, so// let's get to know it well! (no pun intended)//---------------------------------------------------------------[<Koan(Sort = 2)>]module ``about let`` =

[<Koan>]let LetBindsANameToAValue() =

let x = 50

AssertEquality x __

Phil Trelford, @ptrelford

#FuncBY Minsk, 2014