Your SlideShare is downloading. ×
0
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
F# And Silverlight
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

F# And Silverlight

2,459

Published on

This talk describes building Silverlight 3 applications using F#. Both Visual Studio 2008 and 2010 RC are demonstrated. This talk was given by Talbott Crowell at the F# User Group meeting on April 4, …

This talk describes building Silverlight 3 applications using F#. Both Visual Studio 2008 and 2010 RC are demonstrated. This talk was given by Talbott Crowell at the F# User Group meeting on April 4, 2010 at Microsoft, Cambridge, MA.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,459
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. F# and Silverlight<br />Talbott Crowell<br />F# User Group, April 5, 2010http://fsug.org<br />
  • 2. QuickIntro to F#<br />Calculating Moving Average in F#<br />Quick Intro to Silverlight<br />Visualizing F# Using Silverlight Toolkit<br />Agenda<br />
  • 3. Quick Intro to F#<br />
  • 4. Functional language developed by Microsoft Research<br />2002 language design started by Don Syme<br />2005 F# 1.0.1 public release (integration w/ VS2003)<br />2010 F# is “productized” and baked into VS2010<br />Multi-paradigm<br />Functional/Imperative/OO/Language Oriented<br />Key characteristics<br />Succinct, Type Inference (strongly typed), 1st class functions<br />What is F#<br />
  • 5. Type inferencing<br />Strong typed<br />Functions as values<br />F# Intro<br />b is a float<br />let a = 5<br />let b = 6.0<br />let c = “7 feet”<br />let d x = x * x<br />let e = d b<br />d is a function<br />What data type is e?<br />
  • 6. The |> Combinator “Pipe Forward”<br /> Example<br />F# Combinators<br /> x |> f is the same as f x<br /> let sqr x = x * x<br />sqr 5<br /> 5 |> sqr<br />
  • 7. Moving Average in F#<br />
  • 8. Start with a list of numbers<br />Moving Average step 1<br />let testdata = [1.0 .. 10.0]<br />testdata is a list of floats<br />
  • 9. Create windows of the series<br />Moving Average step 2<br />Seq.windowed 4 testdata<br />it is a sequence of float arrays<br />
  • 10. Average the values in each array<br />Moving Average step 3<br />Array.average [|1.0; 2.0; 3.0; 4.0|]<br />
  • 11. Use Seq.map to calculate average of all arrays in the sequence<br />Moving Average step 4<br />let windowedData = Seq.windowed period data<br />Seq.map Array.averagewindowedData<br />the first argument is the function to apply to each item in the sequence<br />the second argument is the sequence<br />
  • 12. Use the pipe forward operator to put it together and omit the let<br />Moving Average step 5<br />Seq.windowed period data<br /> |> Seq.map Array.average<br />
  • 13. Put our algorithm into a function and test it<br />Moving Average function<br />let MovingAverage period data =<br />Seq.windowed period data<br /> |> Seq.map Array.average<br />let testdata = [1.0 .. 10.0]<br />let test = MovingAverage 4 testdata<br />
  • 14. Let’s generate some random data<br />open System<br />let GenerateData offset variance count = <br /> let rnd = new Random()<br /> let randomDouble variance = rnd.NextDouble() * variance<br /> let indexes = Seq.ofList [0..(count-1)]<br /> let genValuei = <br /> let value = float i + offset + randomDouble variance<br /> value<br /> Seq.map genValue indexes<br />let dataGenerator = GenerateData 50.0 100.0 200<br />
  • 15. Putting it together<br />let data1 = List.ofSeqdataGenerator<br />let data2 = List.ofSeq <| MovingAverage 10 data1<br />
  • 16. Time to visualize…<br />
  • 17. Quick Intro to Silverlight<br />
  • 18. What is Silverlight?<br />Microsoft RIA plug-in for browsers<br />2007 Version 1.0 JavaScript based<br />2008 Version 2.0 .NET based<br />2009 Version 3.0 more controls, out of browser support (offline)<br />2010 Version 4.0 printing, COM, Multi-touch<br />Runs on multiple browsers/OS<br />IE, Safari, Firefox, Chrome / Win, MacOS, Linux*<br /><ul><li>via Mono Moonlight implementation
  • 19. WPF/E (original name)</li></li></ul><li>VS 2008<br />Microsoft Silverlight 3 Tools for Visual Studio 2008 SP1<br />VS 2010<br />Built in support for Silverlight 3<br />Current RC requires “Silverlight 4 Tools for VS 2010”<br />http://www.silverlight.net/getstarted/silverlight-4/<br />RTM expected to include Silverlight 3 and 4 tools built in<br />Visual Studio Integration<br />
  • 20. Visual Studio 2008<br />Using Luke Hoban’s F# Silverlight templates<br />Visual Studio 2010 RC<br />Built-in<br />F# Support for Silverlight in VS<br />http://code.msdn.microsoft.com/fsharpsilverlight<br />
  • 21. Silverlight Toolkit<br />http://silverlight.codeplex.com/<br />Silverlight SDK<br />Silverlight 3 SDK<br />Silverlight 4 SDK Beta<br />Graphing Controls<br />
  • 22. Visualizing F# using Silverlight Toolkit<br />
  • 23. Use the “Silverlight Application” template in Visual Studio<br />Creates Silverlight Application (produces XAP)<br />Creates Web Application (hosts web site and XAP)<br />Create a Silverlight Application<br />
  • 24. Initial UserControl<br />Notice XML Namespaces (need to add more)<br />XAML<br />
  • 25. Add References<br />
  • 26. Add namespaces to XAML to support Silverlight Toolkit<br />xmlns:controls="clr-namespace: System.Windows.Controls; assembly=System.Windows.Controls"<br />xmlns:controlsToolkit="clr-namespace: System.Windows.Controls; assembly=System.Windows.Controls.Toolkit"<br />xmlns:chartingToolkit="clr-namespace: System.Windows.Controls.DataVisualization.Charting; assembly=System.Windows.Controls.DataVisualization.Toolkit"<br />xmlns:chartingPrimitivesToolkit="clr-namespace: System.Windows.Controls.DataVisualization.Charting.Primitives; assembly=System.Windows.Controls.DataVisualization.Toolkit"<br />
  • 27. Sample Series comes from Static Resource.<br />Define Static Resource in App.xaml<br />Add a chart to MainPage.xaml<br />
  • 28. add namespace<br />add sample dataset<br />Static Resource App.xaml<br />
  • 29. Similar to a Class Library project except it compiles with special options to target the Silverlight CLR<br />Use file links to share F# files between Class Library and Silverlight Library<br />Add Existing File<br />Choose “Add As Link” from dropdown<br />Create an F# Silverlight Library<br />
  • 30. SeriesDataPoint is a class (type) that has two members, Index and Value<br />Used for series data<br />Add Chart Helper<br />type SeriesDataPoint(index, value) =<br /> member this.Index with get() = index<br /> member this.Value with get() = value<br />
  • 31. Used by XAML Designer to bind at design time<br />Add Sample Data Set<br />type SampleDataSet() =<br /> static member SampleSeries1 =<br /> let data = new ObjectCollection()<br />data.Add(new SeriesDataPoint(0, 124.1))<br />data.Add(new SeriesDataPoint(1, 124.3))<br />data.Add(new SeriesDataPoint(2, 125.7))<br />data.Add(new SeriesDataPoint(3, 115.4))<br />data.Add(new SeriesDataPoint(4, 115.9))<br />data.Add(new SeriesDataPoint(5, 125.0))<br />data.Add(new SeriesDataPoint(6, 133.6))<br />data.Add(new SeriesDataPoint(7, 131.9))<br />data.Add(new SeriesDataPoint(8, 127.3))<br />data.Add(new SeriesDataPoint(9, 137.3))<br /> data<br />
  • 32. Designer is now binding with F#<br />
  • 33. Code behind for MainPage.xaml<br />Use constructor to wire up events<br />Add code behind for MainPage<br />public partial class MainPage : UserControl<br />{<br /> public MainPage()<br /> {<br />InitializeComponent();<br />SilverlightEvents.RegisterHandlers(this.LayoutRoot);<br /> }<br />}<br />
  • 34. Event handling code for application in F#<br />SilverlightEvents in F#<br />
  • 35. Sample Button Click Event<br />buttonGenerateSampleData.Click.Add(fun(_) -><br /> let data = GenerateDataPoints 125.0 10.0 10<br /> let series = DataConverter.ConvertDataPointsToLineSeries<br /> "Sample Series 3" data<br />chart.Series.Add(series)<br /> )<br />
  • 36. Slider Events - Mouse<br />slider.MouseLeftButtonUp.Add(fun(_) -> model.UpdateSeries())<br />member internal this.UpdateSeries() =<br />m_range <- (intslider.Value)<br /> let movingAverage = MovingAveragem_rangem_data<br /> let newSeries = this.GetMovingAverageSeriesmovingAverage<br />chart.Series.[1] <- newSeries<br /> ()<br />
  • 37. Slider Events – Value Changed<br />slider.ValueChanged.Add(fun(callback) -><br />model.UpdateMovingAverage(callback))<br />member this.UpdateMovingAverage(args:RoutedPropertyChangedEventArgs<float>) =<br /> let oldVal = intargs.OldValue<br /> let newVal = intargs.NewValue<br /> if oldVal = newVal then<br /> ()<br />elif (Math.Abs(oldVal - newVal) > 4) then<br />m_range <- newVal<br />this.UpdateSeries()<br /> ()<br /> else<br />m_range <- newVal<br /> ()<br />
  • 38. Generate Data and Moving Average<br />
  • 39. Getting Started<br />http://www.silverlight.net/getstarted/silverlight-4/<br />Tim Heuer’s Blog<br />http://timheuer.com/blog/archive/2010/03/15/whats-new-in-silverlight-4-rc-mix10.aspx<br />Silverlight 4 and Visual Studio 2010 RC<br />
  • 40. Bart Czernicki’s Silverlight Hack Blog<br />http://www.silverlighthack.com/post/2009/11/04/Silverlight-3-and-FSharp-Support-in-Visual-Studio-2010.aspx<br />Luke Hoban’s WebLog<br />http://blogs.msdn.com/lukeh/archive/2009/06/26/f-in-silverlight.aspx<br />support for F# in VS 2008<br />http://code.msdn.microsoft.com/fsharpsilverlight<br />F# and Silverlight 2.0<br />http://jyliao.blogspot.com/2008/11/f-and-silverlight-20.html<br />Other Helpful Links<br />
  • 41. http://fsug.org<br />Thank you. Questions?F# and Silverlight<br />Talbott Crowell<br />ThirdM.com<br />http://talbottc.spaces.live.com<br />Twitter: @talbottand @fsug<br />

×