SlideShare a Scribd company logo
F# and SilverlightTalbott CrowellF# User Group, April 5, 2010http://fsug.org
QuickIntro to F#Calculating Moving Average in F#Quick Intro to SilverlightVisualizing F# Using Silverlight ToolkitAgenda
Quick Intro to F#
Functional language developed by Microsoft Research2002 language design started by Don Syme2005 F# 1.0.1 public release (integration w/ VS2003)2010 F# is “productized” and baked into VS2010Multi-paradigmFunctional/Imperative/OO/Language OrientedKey characteristicsSuccinct, Type Inference (strongly typed), 1st class functionsWhat is F#
Type inferencingStrong typedFunctions as valuesF# Introb is a floatlet a = 5let b = 6.0let c = “7 feet”let d x = x * xlet e = d bd is a functionWhat data type is e?
The |> Combinator “Pipe Forward” ExampleF# Combinators x |> f       is the same as       f x  let sqr x = x * xsqr 5  5 |> sqr
Moving Average in F#
Start with a list of numbersMoving Average step 1let testdata = [1.0 .. 10.0]testdata is a list of floats
Create windows of the seriesMoving Average step 2Seq.windowed 4 testdatait is a sequence of float arrays
Average the values in each arrayMoving Average step 3Array.average [|1.0; 2.0; 3.0; 4.0|]
Use Seq.map to calculate average of all arrays in the sequenceMoving Average step 4let windowedData = Seq.windowed period dataSeq.map Array.averagewindowedDatathe first argument is the function to apply to each item in the sequencethe second argument is the sequence
Use the pipe forward operator to put it together and omit the letMoving Average step 5Seq.windowed period data |> Seq.map Array.average
Put our algorithm into a function and test itMoving Average functionlet MovingAverage period data =Seq.windowed period data     |> Seq.map Array.averagelet testdata = [1.0 .. 10.0]let test = MovingAverage 4 testdata
Let’s generate some random dataopen Systemlet GenerateData offset variance count =     let rnd = new Random()    let randomDouble variance = rnd.NextDouble() * variance    let indexes = Seq.ofList [0..(count-1)]    let genValuei =         let value = float i + offset + randomDouble variance        value    Seq.map genValue indexeslet dataGenerator = GenerateData 50.0 100.0 200
Putting it togetherlet data1 = List.ofSeqdataGeneratorlet data2 = List.ofSeq <| MovingAverage 10 data1
Time to visualize…
Quick Intro to Silverlight
What is Silverlight?Microsoft RIA plug-in for browsers2007 Version 1.0 JavaScript based2008 Version 2.0 .NET based2009 Version 3.0 more controls, out of browser support (offline)2010 Version 4.0 printing, COM, Multi-touchRuns on multiple browsers/OSIE, Safari, Firefox, Chrome / Win, MacOS, Linux*via Mono Moonlight implementation
WPF/E (original name)VS 2008Microsoft Silverlight 3 Tools for Visual Studio 2008 SP1VS 2010Built in support for Silverlight 3Current RC requires “Silverlight 4 Tools for VS 2010”http://www.silverlight.net/getstarted/silverlight-4/RTM expected to include Silverlight 3 and 4 tools built inVisual Studio Integration
Visual Studio 2008Using Luke Hoban’s F# Silverlight templatesVisual Studio 2010 RCBuilt-inF# Support for Silverlight in VShttp://code.msdn.microsoft.com/fsharpsilverlight
Silverlight Toolkithttp://silverlight.codeplex.com/Silverlight SDKSilverlight 3 SDKSilverlight 4 SDK BetaGraphing Controls
Visualizing F# using Silverlight Toolkit
Use the “Silverlight Application” template in Visual StudioCreates Silverlight Application (produces XAP)Creates Web Application (hosts web site and XAP)Create a Silverlight Application
 Initial UserControlNotice XML Namespaces (need to add more)XAML
Add References
Add namespaces to XAML to support Silverlight Toolkitxmlns:controls="clr-namespace: System.Windows.Controls; assembly=System.Windows.Controls"xmlns:controlsToolkit="clr-namespace: System.Windows.Controls; assembly=System.Windows.Controls.Toolkit"xmlns:chartingToolkit="clr-namespace: System.Windows.Controls.DataVisualization.Charting; assembly=System.Windows.Controls.DataVisualization.Toolkit"xmlns:chartingPrimitivesToolkit="clr-namespace: System.Windows.Controls.DataVisualization.Charting.Primitives; assembly=System.Windows.Controls.DataVisualization.Toolkit"
Sample Series comes from Static Resource.Define Static Resource in App.xamlAdd a chart to MainPage.xaml
add namespaceadd sample datasetStatic Resource App.xaml
Similar to a Class Library project except it compiles with special options to target the Silverlight CLRUse file links to share F# files between Class Library and Silverlight LibraryAdd Existing FileChoose “Add As Link” from dropdownCreate an F# Silverlight Library
SeriesDataPoint is a class (type) that has two members, Index and ValueUsed for series dataAdd Chart Helpertype SeriesDataPoint(index, value) =    member this.Index with get() = index    member this.Value with get() = value
Used by XAML Designer to bind at design timeAdd Sample Data Settype SampleDataSet() =    static member SampleSeries1 =        let data = new ObjectCollection()data.Add(new SeriesDataPoint(0, 124.1))data.Add(new SeriesDataPoint(1, 124.3))data.Add(new SeriesDataPoint(2, 125.7))data.Add(new SeriesDataPoint(3, 115.4))data.Add(new SeriesDataPoint(4, 115.9))data.Add(new SeriesDataPoint(5, 125.0))data.Add(new SeriesDataPoint(6, 133.6))data.Add(new SeriesDataPoint(7, 131.9))data.Add(new SeriesDataPoint(8, 127.3))data.Add(new SeriesDataPoint(9, 137.3))        data
Designer is now binding with F#
Code behind for MainPage.xamlUse constructor to wire up eventsAdd code behind for MainPagepublic partial class MainPage : UserControl{   public MainPage()   {InitializeComponent();SilverlightEvents.RegisterHandlers(this.LayoutRoot);   }}
Event handling code for application in F#SilverlightEvents in F#
Sample Button Click EventbuttonGenerateSampleData.Click.Add(fun(_) ->   let data = GenerateDataPoints 125.0 10.0 10   let series = DataConverter.ConvertDataPointsToLineSeries       "Sample Series 3" datachart.Series.Add(series)   )
Slider Events - Mouseslider.MouseLeftButtonUp.Add(fun(_) -> model.UpdateSeries())member internal this.UpdateSeries() =m_range <- (intslider.Value)   let movingAverage = MovingAveragem_rangem_data   let newSeries = this.GetMovingAverageSeriesmovingAveragechart.Series.[1] <- newSeries   ()
Slider Events – Value Changedslider.ValueChanged.Add(fun(callback) ->model.UpdateMovingAverage(callback))member this.UpdateMovingAverage(args:RoutedPropertyChangedEventArgs<float>) =        let oldVal = intargs.OldValue        let newVal = intargs.NewValue        if oldVal = newVal then            ()elif (Math.Abs(oldVal - newVal) > 4) thenm_range <- newValthis.UpdateSeries()            ()        elsem_range <- newVal            ()
Generate Data and Moving Average
Getting Startedhttp://www.silverlight.net/getstarted/silverlight-4/Tim Heuer’s Bloghttp://timheuer.com/blog/archive/2010/03/15/whats-new-in-silverlight-4-rc-mix10.aspxSilverlight 4 and Visual Studio 2010 RC
Bart Czernicki’s Silverlight Hack Bloghttp://www.silverlighthack.com/post/2009/11/04/Silverlight-3-and-FSharp-Support-in-Visual-Studio-2010.aspxLuke Hoban’s WebLoghttp://blogs.msdn.com/lukeh/archive/2009/06/26/f-in-silverlight.aspxsupport for F# in VS 2008http://code.msdn.microsoft.com/fsharpsilverlightF# and Silverlight 2.0http://jyliao.blogspot.com/2008/11/f-and-silverlight-20.htmlOther Helpful Links
http://fsug.orgThank you. Questions?F# and SilverlightTalbott CrowellThirdM.comhttp://talbottc.spaces.live.comTwitter: @talbottand @fsug

More Related Content

PPTX
Stack and its applications
PDF
Oot practical
PDF
JavaOne 2013: Java 8 - The Good Parts
PDF
PPTX
Awt - Swings-- Applets In java
PPT
Stack a Data Structure
PDF
cats.effect.IO - Scala Vienna Meetup February 2019
PPTX
Typescript barcelona
Stack and its applications
Oot practical
JavaOne 2013: Java 8 - The Good Parts
Awt - Swings-- Applets In java
Stack a Data Structure
cats.effect.IO - Scala Vienna Meetup February 2019
Typescript barcelona

What's hot (18)

PPTX
Project of data structure
PPTX
Stacks in Data Structure
PPSX
Data Structure (Stack)
PPTX
Sql FUNCTIONS
PDF
Stack push pop
PPT
Stacks overview with its applications
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PDF
Controllers and context programming
PDF
React table tutorial use filter (part 2)
PPTX
Introduction To Stack
PPT
Conversion of Infix To Postfix Expressions
PPT
data structure, stack, stack data structure
PDF
QuirksofR
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
PDF
How to instantiate any view controller for free
PDF
Stacks
PPTX
Stack data structure
Project of data structure
Stacks in Data Structure
Data Structure (Stack)
Sql FUNCTIONS
Stack push pop
Stacks overview with its applications
STACK ( LIFO STRUCTURE) - Data Structure
Controllers and context programming
React table tutorial use filter (part 2)
Introduction To Stack
Conversion of Infix To Postfix Expressions
data structure, stack, stack data structure
QuirksofR
The Ring programming language version 1.5.1 book - Part 20 of 180
How to instantiate any view controller for free
Stacks
Stack data structure
Ad

Similar to F# And Silverlight (20)

PPTX
Silverlight Developer Introduction
PPTX
Introduction to F#
PPTX
Building appsinsilverlight4 part_1
PPTX
Creating Domain Specific Languages in F#
PPT
Microsoft silverlight
PPTX
WP7 HUB_Introducción a Silverlight
PPTX
Reactive fsharp
PPTX
Building a Twitter App with Silverlight 3 - Part 1
PPTX
Exploring SharePoint with F#
PDF
Silverlight 2 For Developers
PPTX
Parallel minds silverlight
PPTX
Advanced Silverlight
PPT
Silverlight Development & The Model-View-ViewModel Pattern
PPT
From Windows Presentation Foundation To Silverlight
PPTX
NH .Net Code Camp 2010 - An Introduction to Silverlight Development
PPTX
Windows phone and azure
PPTX
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
PDF
Mike Taulty MIX10 Silverlight 4 Accelerated Fundamentals
PPTX
Optimizing Application Architecture (.NET/Java topics)
PDF
The F# Path to Relaxation
Silverlight Developer Introduction
Introduction to F#
Building appsinsilverlight4 part_1
Creating Domain Specific Languages in F#
Microsoft silverlight
WP7 HUB_Introducción a Silverlight
Reactive fsharp
Building a Twitter App with Silverlight 3 - Part 1
Exploring SharePoint with F#
Silverlight 2 For Developers
Parallel minds silverlight
Advanced Silverlight
Silverlight Development & The Model-View-ViewModel Pattern
From Windows Presentation Foundation To Silverlight
NH .Net Code Camp 2010 - An Introduction to Silverlight Development
Windows phone and azure
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
Mike Taulty MIX10 Silverlight 4 Accelerated Fundamentals
Optimizing Application Architecture (.NET/Java topics)
The F# Path to Relaxation
Ad

More from Talbott Crowell (17)

PPTX
Talbott's brief History of Computers for CollabDays Hamburg 2025
PPTX
Top 7 mistakes
PPTX
Top 3 Mistakes when Building
PPTX
Building high performance and scalable share point applications
PPTX
Road to the Cloud - Extending your reach with SharePoint and Office 365
PPTX
Custom Development for SharePoint
PPTX
Custom Development in SharePoint – What are my options now?
PPTX
Developing a Provider Hosted SharePoint app
PPTX
Developing a provider hosted share point app
PPTX
Introduction to F# 3.0
PPTX
PowerShell and SharePoint @spsnyc July 2012
PPTX
PowerShell and SharePoint
PPTX
Welcome to windows 8
PPTX
Automating PowerShell with SharePoint
PPTX
SharePoint Saturday Boston 2010
PPT
Automating SQL Server Database Creation for SharePoint
PPTX
Architecting Solutions for the Manycore Future
Talbott's brief History of Computers for CollabDays Hamburg 2025
Top 7 mistakes
Top 3 Mistakes when Building
Building high performance and scalable share point applications
Road to the Cloud - Extending your reach with SharePoint and Office 365
Custom Development for SharePoint
Custom Development in SharePoint – What are my options now?
Developing a Provider Hosted SharePoint app
Developing a provider hosted share point app
Introduction to F# 3.0
PowerShell and SharePoint @spsnyc July 2012
PowerShell and SharePoint
Welcome to windows 8
Automating PowerShell with SharePoint
SharePoint Saturday Boston 2010
Automating SQL Server Database Creation for SharePoint
Architecting Solutions for the Manycore Future

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
sap open course for s4hana steps from ECC to s4

F# And Silverlight

  • 1. F# and SilverlightTalbott CrowellF# User Group, April 5, 2010http://fsug.org
  • 2. QuickIntro to F#Calculating Moving Average in F#Quick Intro to SilverlightVisualizing F# Using Silverlight ToolkitAgenda
  • 4. Functional language developed by Microsoft Research2002 language design started by Don Syme2005 F# 1.0.1 public release (integration w/ VS2003)2010 F# is “productized” and baked into VS2010Multi-paradigmFunctional/Imperative/OO/Language OrientedKey characteristicsSuccinct, Type Inference (strongly typed), 1st class functionsWhat is F#
  • 5. Type inferencingStrong typedFunctions as valuesF# Introb is a floatlet a = 5let b = 6.0let c = “7 feet”let d x = x * xlet e = d bd is a functionWhat data type is e?
  • 6. The |> Combinator “Pipe Forward” ExampleF# Combinators x |> f is the same as f x let sqr x = x * xsqr 5 5 |> sqr
  • 8. Start with a list of numbersMoving Average step 1let testdata = [1.0 .. 10.0]testdata is a list of floats
  • 9. Create windows of the seriesMoving Average step 2Seq.windowed 4 testdatait is a sequence of float arrays
  • 10. Average the values in each arrayMoving Average step 3Array.average [|1.0; 2.0; 3.0; 4.0|]
  • 11. Use Seq.map to calculate average of all arrays in the sequenceMoving Average step 4let windowedData = Seq.windowed period dataSeq.map Array.averagewindowedDatathe first argument is the function to apply to each item in the sequencethe second argument is the sequence
  • 12. Use the pipe forward operator to put it together and omit the letMoving Average step 5Seq.windowed period data |> Seq.map Array.average
  • 13. Put our algorithm into a function and test itMoving Average functionlet MovingAverage period data =Seq.windowed period data |> Seq.map Array.averagelet testdata = [1.0 .. 10.0]let test = MovingAverage 4 testdata
  • 14. Let’s generate some random dataopen Systemlet GenerateData offset variance count = let rnd = new Random() let randomDouble variance = rnd.NextDouble() * variance let indexes = Seq.ofList [0..(count-1)] let genValuei = let value = float i + offset + randomDouble variance value Seq.map genValue indexeslet dataGenerator = GenerateData 50.0 100.0 200
  • 15. Putting it togetherlet data1 = List.ofSeqdataGeneratorlet data2 = List.ofSeq <| MovingAverage 10 data1
  • 17. Quick Intro to Silverlight
  • 18. What is Silverlight?Microsoft RIA plug-in for browsers2007 Version 1.0 JavaScript based2008 Version 2.0 .NET based2009 Version 3.0 more controls, out of browser support (offline)2010 Version 4.0 printing, COM, Multi-touchRuns on multiple browsers/OSIE, Safari, Firefox, Chrome / Win, MacOS, Linux*via Mono Moonlight implementation
  • 19. WPF/E (original name)VS 2008Microsoft Silverlight 3 Tools for Visual Studio 2008 SP1VS 2010Built in support for Silverlight 3Current RC requires “Silverlight 4 Tools for VS 2010”http://www.silverlight.net/getstarted/silverlight-4/RTM expected to include Silverlight 3 and 4 tools built inVisual Studio Integration
  • 20. Visual Studio 2008Using Luke Hoban’s F# Silverlight templatesVisual Studio 2010 RCBuilt-inF# Support for Silverlight in VShttp://code.msdn.microsoft.com/fsharpsilverlight
  • 22. Visualizing F# using Silverlight Toolkit
  • 23. Use the “Silverlight Application” template in Visual StudioCreates Silverlight Application (produces XAP)Creates Web Application (hosts web site and XAP)Create a Silverlight Application
  • 24. Initial UserControlNotice XML Namespaces (need to add more)XAML
  • 26. Add namespaces to XAML to support Silverlight Toolkitxmlns:controls="clr-namespace: System.Windows.Controls; assembly=System.Windows.Controls"xmlns:controlsToolkit="clr-namespace: System.Windows.Controls; assembly=System.Windows.Controls.Toolkit"xmlns:chartingToolkit="clr-namespace: System.Windows.Controls.DataVisualization.Charting; assembly=System.Windows.Controls.DataVisualization.Toolkit"xmlns:chartingPrimitivesToolkit="clr-namespace: System.Windows.Controls.DataVisualization.Charting.Primitives; assembly=System.Windows.Controls.DataVisualization.Toolkit"
  • 27. Sample Series comes from Static Resource.Define Static Resource in App.xamlAdd a chart to MainPage.xaml
  • 28. add namespaceadd sample datasetStatic Resource App.xaml
  • 29. Similar to a Class Library project except it compiles with special options to target the Silverlight CLRUse file links to share F# files between Class Library and Silverlight LibraryAdd Existing FileChoose “Add As Link” from dropdownCreate an F# Silverlight Library
  • 30. SeriesDataPoint is a class (type) that has two members, Index and ValueUsed for series dataAdd Chart Helpertype SeriesDataPoint(index, value) = member this.Index with get() = index member this.Value with get() = value
  • 31. Used by XAML Designer to bind at design timeAdd Sample Data Settype SampleDataSet() = static member SampleSeries1 = let data = new ObjectCollection()data.Add(new SeriesDataPoint(0, 124.1))data.Add(new SeriesDataPoint(1, 124.3))data.Add(new SeriesDataPoint(2, 125.7))data.Add(new SeriesDataPoint(3, 115.4))data.Add(new SeriesDataPoint(4, 115.9))data.Add(new SeriesDataPoint(5, 125.0))data.Add(new SeriesDataPoint(6, 133.6))data.Add(new SeriesDataPoint(7, 131.9))data.Add(new SeriesDataPoint(8, 127.3))data.Add(new SeriesDataPoint(9, 137.3)) data
  • 32. Designer is now binding with F#
  • 33. Code behind for MainPage.xamlUse constructor to wire up eventsAdd code behind for MainPagepublic partial class MainPage : UserControl{ public MainPage() {InitializeComponent();SilverlightEvents.RegisterHandlers(this.LayoutRoot); }}
  • 34. Event handling code for application in F#SilverlightEvents in F#
  • 35. Sample Button Click EventbuttonGenerateSampleData.Click.Add(fun(_) -> let data = GenerateDataPoints 125.0 10.0 10 let series = DataConverter.ConvertDataPointsToLineSeries "Sample Series 3" datachart.Series.Add(series) )
  • 36. Slider Events - Mouseslider.MouseLeftButtonUp.Add(fun(_) -> model.UpdateSeries())member internal this.UpdateSeries() =m_range <- (intslider.Value) let movingAverage = MovingAveragem_rangem_data let newSeries = this.GetMovingAverageSeriesmovingAveragechart.Series.[1] <- newSeries ()
  • 37. Slider Events – Value Changedslider.ValueChanged.Add(fun(callback) ->model.UpdateMovingAverage(callback))member this.UpdateMovingAverage(args:RoutedPropertyChangedEventArgs<float>) = let oldVal = intargs.OldValue let newVal = intargs.NewValue if oldVal = newVal then ()elif (Math.Abs(oldVal - newVal) > 4) thenm_range <- newValthis.UpdateSeries() () elsem_range <- newVal ()
  • 38. Generate Data and Moving Average
  • 39. Getting Startedhttp://www.silverlight.net/getstarted/silverlight-4/Tim Heuer’s Bloghttp://timheuer.com/blog/archive/2010/03/15/whats-new-in-silverlight-4-rc-mix10.aspxSilverlight 4 and Visual Studio 2010 RC
  • 40. Bart Czernicki’s Silverlight Hack Bloghttp://www.silverlighthack.com/post/2009/11/04/Silverlight-3-and-FSharp-Support-in-Visual-Studio-2010.aspxLuke Hoban’s WebLoghttp://blogs.msdn.com/lukeh/archive/2009/06/26/f-in-silverlight.aspxsupport for F# in VS 2008http://code.msdn.microsoft.com/fsharpsilverlightF# and Silverlight 2.0http://jyliao.blogspot.com/2008/11/f-and-silverlight-20.htmlOther Helpful Links
  • 41. http://fsug.orgThank you. Questions?F# and SilverlightTalbott CrowellThirdM.comhttp://talbottc.spaces.live.comTwitter: @talbottand @fsug