F# and the DLR

Preview:

DESCRIPTION

Dev Day for NSWC Dahlgren

Citation preview

F# AND THE DLRRichard Minerich

Senior Researcher at Bayard Rock F# MVP of the Year

RichardMinerich.com

The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.

-- Edsger Dijkstra

Problems in Modern Programming Adding functionality to programs is difficult Exploring an existing code base is very

difficult Architecture is hard Multi-threading is very hard Algorithms are difficult to both write and read

later

Why? Our Object-Oriented heritage.

F# vs Dynamic

F# Dynamic Languages

Constructs conform to types and are fixed at runtime

Types conform to constructs and change at runtime

Data is immutable by default Generally data is mutable by default

Protection from many kinds of mistakes

No protection, even from typos

Native interop with existing .NET Programs and Libraries

Some work required for interop, but it’s not difficult

Somewhat difficult to learn Very easy to learn

What is F#?

F# is a Multi-Paradigm Language Functional with Some Type Extensions Allows for Object Oriented and Imperative

Style Code F# is Interactive

Command Line, Compile or Interactive Window F# is a .NET CLR Language

In the box with Visual Studio 2010 Native Interop with every other .NET Language

F# is Open Source But maintained and supported by Microsoft

If you love to code, you’ll love F#.Immutable Data Structures

Tail RecursionDiscriminated Unions

Partial Application

Function Composition

QuotationsComputation Expressions

Statically Resolved Type Parameters

If you hate to code, you’ll love F#

Functional with Types?

Functional + Types

F# is great for…

Data Processing Technical Computing Algorithmic Programming Experimenting with Frameworks Implementing Critical Processes

And has the tools to support it.

Functional Programming in F# All statements return somethinglet x = if (y > 10) then 10 else y

Everything is immutable by defaultlet rootList = [ 1; 2; 3 ]

rootList @ [ 4 ]

Chains, instead of discrete statementsurls |> List.map download |> Async.Parallel

F# Programming Paradigms

Recursion Pipelining Matching Function Composition Discriminated Unions

Recursion and TCO

Using functions which call themselves.

Function

> let rec badfib n = if n < 3 then 1 else badfib (n - 1) + badfib (n - 2);;val badfib : int -> int

> let rec fib n1 n2 c = if c = 1 then n2 else fib n2 (n1 + n2) (c - 1);;val fib: int -> int -> int -> int

Pipelining

Push data through a series of functions.

Data

function

function

function

|>

|>

|> lambda

lambda

> [1; 2; 3; 4; 5]|> List.map (fun x -> x * 2)|> List.filter (fun x -> x % 3 <> 0);;[2; 4; 8; 10]

Matching

A switch statement on steroids.

match num with| 1 -> "One"| x when x = 2 -> "Two"| _ -> "Higher than I can count“

matchexact case

when

|

|

->

variable

with

function

-> function

-> function

name

| -> function

name

predicate

|active pattern

Function Composition

Combine functions at runtime

‘a -> ‘b ‘b -> ‘c ‘a -> ‘c>>

=

let addOne x = x + 1let addTwo x = x + 2let addThree x = x + 3let addSix = addOne >> addTwo >> addThree

Discriminated Unions

A type that can be one of several other types

typenam

e|

signature

name

=

name

|signatur

e

type Option<'a> = | Some of 'a | None

let printOutput opt = match opt with | Some (val) -> printfn “%A” val | None -> printfn “No Value”

of

of

And More!

Statically Resolved Type Parameters Special Record Types Object Expressions Comprehensions Computation Expressions Quotations

Try writing some AI

http://is.gd/SilverlightAnts

Try F# at tryfsharp.org

Find Some Functional Friends

The Community For F# Onlinewww.communityforfsharp.net

The NYC F# User Groupwww.meetup.com/nyc-fsharp

The Cambridge F# User Groupfsug.org

The San Francisco Bay Area F# User Groupwww.sfsharp.org

F#unctional Londonerswww.meetup.com/FSharpLondon

Read a Book

What is the DLR?

The DLR is a layer on top of the CLR Designed for dynamic languages Enables interop with running CLR programs and

.NET libraries Compatible Implementations

Python, Ruby, Javascript, and Scheme It’s Fast

IronPython is about 2x the speed of Python The Languages are Open Source

Community maintained and not directly supported

The DLR is great for…

Test Driven Development Web Development Using Code from Existing Dynamic

Programs Writing your own Language Embedding in Applications

Dynamic in C#

DLR Architecture

Metaobject Visualization

Users and programs interact with metaobjects, not directly with their backing objects as in C#, VB.NET or F#.

control

createinvoke

Silverlight and the DLR

Embedded Languages in Your App

DLR Books and Resources

http://www.silverlight.net/learn/dynamic-languages/

http://is.gd/DLRFAQ

Thank You

Slides and code will be available on RichardMinerich.com later today.

richard.minerich@gmail.com

Recommended