Top Banner
F# AND THE DLR Richard Minerich Senior Researcher at Bayard Rock F# MVP of the Year RichardMinerich.com
31

F# and the DLR

May 10, 2015

Download

Technology

Dev Day for NSWC Dahlgren
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: F# and the DLR

F# AND THE DLRRichard Minerich

Senior Researcher at Bayard Rock F# MVP of the Year

RichardMinerich.com

Page 2: F# and the DLR

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

-- Edsger Dijkstra

Page 3: F# and the DLR

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.

Page 4: F# and the DLR

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

Page 5: F# and the DLR

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

Page 6: F# and the DLR

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

Page 7: F# and the DLR

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

Page 8: F# and the DLR

Functional with Types?

Functional + Types

Page 9: F# and the DLR

F# is great for…

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

Page 10: F# and the DLR

And has the tools to support it.

Page 11: F# and the DLR

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

Page 12: F# and the DLR

F# Programming Paradigms

Recursion Pipelining Matching Function Composition Discriminated Unions

Page 13: F# and the DLR

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

Page 14: F# and the DLR

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]

Page 15: F# and the DLR

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

Page 16: F# and the DLR

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

Page 17: F# and the DLR

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

Page 18: F# and the DLR

And More!

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

Page 19: F# and the DLR

Try writing some AI

http://is.gd/SilverlightAnts

Page 20: F# and the DLR

Try F# at tryfsharp.org

Page 21: F# and the DLR

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

Page 22: F# and the DLR

Read a Book

Page 23: F# and the DLR

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

Page 24: F# and the DLR

The DLR is great for…

Test Driven Development Web Development Using Code from Existing Dynamic

Programs Writing your own Language Embedding in Applications

Page 25: F# and the DLR

Dynamic in C#

Page 26: F# and the DLR

DLR Architecture

Page 27: F# and the DLR

Metaobject Visualization

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

control

createinvoke

Page 28: F# and the DLR

Silverlight and the DLR

Page 29: F# and the DLR

Embedded Languages in Your App

Page 30: F# and the DLR

DLR Books and Resources

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

http://is.gd/DLRFAQ

Page 31: F# and the DLR

Thank You

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

[email protected]