F# Succinct, Expressive, Functional презентация

Содержание

Topics What is F# about? Some Simple F# Programming A Taste of Parallel/Reactive with F#

Слайд 1F# Succinct, Expressive, Functional

The F# Team
Microsoft Developer Division
Microsoft Research



Слайд 2Topics
What is F# about?

Some Simple F# Programming

A Taste of Parallel/Reactive with

F#


Слайд 3What is F# about?
Or: Why is Microsoft investing in functional programming

anyway?



Слайд 4Simplicity


Слайд 5Economics


Слайд 6Programmer Productivity


Слайд 7Simplicity


Слайд 8Code!
//F#
open System
let a = 2
Console.WriteLine a
//C#
using System;

namespace ConsoleApplication1
{
class Program
{

static int a()
{
return 2;
}
static void Main(string[] args)
{
Console.WriteLine(a);
}
}
}

More Noise
Than Signal!


Слайд 9Pleasure
 
type Command = Command of (Rover -> unit)

let BreakCommand     =

Command(fun rover -> rover.Accelerate(-1.0))

let TurnLeftCommand  =
Command(fun rover -> rover.Rotate(-5.0))
 

Pain

   abstract class Command
    {
        public virtual void Execute();
    }
    abstract class MarsRoverCommand : Command
    {
        protected MarsRover Rover { get; private set; }
 
        public MarsRoverCommand(MarsRover rover)
        {
            this.Rover = rover;
        }
    }
    class BreakCommand : MarsRoverCommand
    {
        public BreakCommand(MarsRover rover)
            : base(rover)
        {
        } 
        public override void Execute()
        {
            Rover.Rotate(-5.0);
        }
    }
class TurnLeftCommand : MarsRoverCommand
    {
        public TurnLeftCommand(MarsRover rover)
            : base(rover)
        {
        }
        public override void Execute()
        {
            Rover.Rotate(-5.0);
        }
    }


Слайд 10Pleasure
type Expr =   
    | True   
    | And of Expr * Expr   
    | Nand of Expr * Expr   
    | Or of Expr * Expr   
    | Xor of Expr * Expr   
    | Not of Expr  
Pain
public abstract class Expr { }   
public abstract class UnaryOp :Expr   
{   
    public Expr First { get; private set; }

  
    public UnaryOp(Expr first)   
    {   
        this.First = first;   
    }   
}   
  
public abstract class BinExpr : Expr   
{   
    public Expr First { get; private set; }   
    public Expr Second { get; private set; }   
  
    public BinExpr(Expr first, Expr second)   
    {   
        this.First = first;   
        this.Second = second;   
    }   
}   
  
public class TrueExpr : Expr { }   
  
public class And : BinExpr   
{   
    public And(Expr first, Expr second) : base(first, second) { }   
}
public class Nand : BinExpr   
{   
    public Nand(Expr first, Expr second) : base(first, second) { }   
}   
  
public class Or : BinExpr   
{   
    public Or(Expr first, Expr second) : base(first, second) { }   
}   
  
public class Xor : BinExpr   
{   
    public Xor(Expr first, Expr second) : base(first, second) { }   
}   
  
public class Not : UnaryOp   
{   
    public Not(Expr first) : base(first) { }   
}  
  
 


http://stepheneasey.wordpress.com/tag/c/


Слайд 11Pleasure
let rotate (x,y,z) = (z,x,y)





let reduce f (x,y,z) =

f x + f y + f z











Pain

Tuple Rotate(Tuple t)
{
return new Tuple(t.Item3,t.Item1,t.Item2);
}

int Reduce(Func f,Tuple t)
{
return f(t.Item1) + f(t.Item2) + f (t.Item3);
}



Слайд 12Economics


Слайд 13Programmer Productivity


Слайд 14People
Love
Programming
In
F#


Слайд 15F#: Influences

Similar core
language

Similar object
model


Слайд 16F#: Combining Paradigms

I've been coding in F# lately, for a production

task. F# allows you to move smoothly in your programming style... I start with pure functional code, shift slightly towards an object-oriented style, and in production code, I sometimes have to do some imperative programming.

I can start with a pure idea, and still finish my project with realistic code. You're never disappointed in any phase of the project!

Julien Laugel, Chief Software Architect, www.eurostocks.com

Слайд 17F#: The Combination Counts!


Слайд 18The Path to Mastering F#


Слайд 19Quick Tour

Comments
// comment

(* comment *)

/// XML doc comment
let x =

1

Слайд 20Quick Tour
Booleans
not expr Boolean negation
expr && expr Boolean “and”
expr || expr Boolean “or”
Overloaded Arithmetic
x

+ y Addition
x - y Subtraction
x * y Multiplication
x / y Division
x % y Remainder/modulus
-x Unary negation

Слайд 21Orthogonal & Unified Constructs

Let “let” simplify your life…
let data = (1,2,3)

let

f(a,b,c) =
let sum = a + b + c
let g(x) = sum + x*x
g(a), g(b), g(c)

Bind a static value

Bind a static function

Bind a local value

Bind a local function

Type inference. The safety of C# with the succinctness of a scripting language


Слайд 22Demo: Let’s WebCrawl…


Слайд 23Orthogonal & Unified Constructs
Functions: like delegates + unified and simple

(fun x

-> x + 1)

let f(x) = x + 1

(f,f)

val f : int -> int

Anonymous
Function value

Declare a
function value

A pair
of function values

predicate = 'a -> bool

send = 'a -> unit

threadStart = unit -> unit

comparer = 'a -> 'a -> int

hasher = 'a -> int

equality = 'a -> 'a -> bool

One simple
mechanism,
many
uses

A function type


Слайд 24F# - Functional
let f x = x+1

let pair x = (x,x)

let

fst (x,y) = x

let data = (Some [1;2;3], Some [4;5;6])

match data with
| Some(nums1), Some(nums2) -> nums1 @ nums2
| None, Some(nums) -> nums
| Some(nums), None -> nums
| None, None -> failwith "missing!"

Слайд 25F# - Functional
List.map Seq.fold

Array.filter Lazy.force Set.union

Map LazyList Events Async...

[

0..1000 ]
[ for x in 0..10 -> (x, x * x) ]
[| for x in 0..10 -> (x, x * x) |]
seq { for x in 0..10 -> (x, x * x) }


Range
Expressions

List via query

Array via query

IEnumerable
via query


Слайд 26Immutability the norm…


Values may not be changed
Data is immutable by default

Not Mutate

✓ Copy & Update


Слайд 27In Praise of Immutability
Immutable objects can be relied upon

Immutable objects can

transfer between threads

Immutable objects can be aliased safely

Immutable objects lead to (different) optimization opportunities

Слайд 28F# - Lists
open System.IO
let rec allFiles(dir) =
[ for file in

Directory.GetFiles(dir) do
yield file
for sub in Directory.GetDirectories(dir) do
yield! allFiles(sub) ]

allFiles(@"C:\Demo")

Generated Lists


Слайд 29F# - Sequences
open System.IO
let rec allFiles(dir) =
seq
{

for file in Directory.GetFiles(dir) do
yield file
for sub in Directory.GetDirectories(dir) do
yield! allFiles(sub) }

allFiles(@"C:\WINDOWS")
|> Seq.take 100
|> show

On-demand sequences

Pipelines


Слайд 30//F#
#light
open System
let a = 2
Console.WriteLine(a)
//C#
using System;

namespace ConsoleApplication1
{
class Program

{
static int a()
{
return 2;
}

static void Main(string[] args)
{
Console.WriteLine(a);
}
}
}

Looks Weakly typed?
Maybe Dynamic?

Weakly Typed? Slow?


Слайд 31F#
Yet rich, dynamic
Yet succinct


Слайд 32Objects

Class Types
type ObjectType(args) =

let internalValue = expr

let internalFunction args = expr
let mutable internalState = expr

member x.Prop1 = expr
member x.Meth2 args = expr

Constructing Objects
new FileInfo(@"c:\misc\test.fs")


Слайд 33F# - Objects + Functional
type Vector2D(dx:double,dy:double) =

member v.DX =

dx

member v.DY = dy

member v.Length = sqrt(dx*dx+dy*dy)

member v.Scale(k) = Vector2D(dx*k,dy*k)

Inputs to object construction

Exported properties

Exported method


Слайд 34F# - Objects + Functional
type Vector2D(dx:double,dy:double) =

let norm2 =

dx*dx+dy*dy

member v.DX = dx

member v.DY = dy

member v.Length = sqrt(norm2)

member v.Norm2 = norm2

Internal (pre-computed) values and functions


Слайд 35F# - Objects + Functional
type HuffmanEncoding(freq:seq) =

...

< 50 lines of beautiful functional code>
...

member x.Encode(input: seq) =
encode(input)

member x.Decode(input: seq) =
decode(input)

Immutable inputs

Internal tables

Publish access


Слайд 36F# - Objects + Functional
type Vector2D(dx:double,dy:double) =

let mutable currDX

= dx

let mutable currDX = dy

member v.DX = currDX

member v.DY = currDY

member v.Move(x,y) =
currDX <- currDX+x
currDY <- currDY+y

Internal state

Publish internal state

Mutate internal state


Слайд 37F# Async/Parallel


Слайд 38The Solution
Good Architecture
Know your techniques
Know your requirements
Know your limits (CPU, disk,

network, latency)

Translate Good Architecture into Good Code with F#
A great platform
A massive increase in isolation and immutability
A massive reduction in mutation

In parallel programming,

F# is a power tool

for good architects and good developers


Слайд 39Async: Simple Examples


async { ... }


Compute 22 and 7 in parallel

Async.Parallel [WebRequest.Async "http://www.live.com";
WebRequest.Async "http://www.yahoo.com";
WebRequest.Async "http://www.google.com" ]

Get these three web pages and wait until all have come back


Слайд 418 Ways to Learn
FSI.exe

Samples Included

Go to definition

Lutz’ Reflector

http://cs.hubfs.net

Codeplex Fsharp Samples

Books

ML


Слайд 42Books about F#
Visit www.fsharp.net


Слайд 43Getting F#
September 2008: CTP released

F# will be a supported language in


Visual Studio 2010

Next stop: Visual Studio 2010 Beta 1

Look for it soon!



Слайд 44Questions & Discussion


Слайд 45© 2007 Microsoft Corporation. All rights reserved. This presentation is for informational

purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.

Обратная связь

Если не удалось найти и скачать презентацию, Вы можете заказать его на нашем сайте. Мы постараемся найти нужный Вам материал и отправим по электронной почте. Не стесняйтесь обращаться к нам, если у вас возникли вопросы или пожелания:

Email: Нажмите что бы посмотреть 

Что такое ThePresentation.ru?

Это сайт презентаций, докладов, проектов, шаблонов в формате PowerPoint. Мы помогаем школьникам, студентам, учителям, преподавателям хранить и обмениваться учебными материалами с другими пользователями.


Для правообладателей

Яндекс.Метрика