1. Exploring SharePoint with F# Talbott Crowell (MVP) @Talbott SharePoint Saturday New York City July 30, 2011
2. Learn something new (exercise for the brain) Trend in functional programming Java -> Clojure, Scala Erlang LINQ added to VB and C# Microsoft’s official (and only) functional programming language Why F#
3. Functional programming has been around a long time Not new Long history Functional programming is safe A concern as we head toward manycore and cloud computing Why another language?
4. 1930’s: lambda calculus (roots) 1956: IPL (Information Processing Language) “the first functional language 1958: LISP “a functional flavored language” 1962: APL (A Programming Language) 1973: ML (Meta Language) 1983: SML (Standard ML) 1987: Caml (Categorical Abstract Machine Language ) and Haskell 1996: OCaml (Objective Caml) Functional programming has been around a long time
5. Functional language developed by Microsoft Research By Don Syme and his team, who productized Generics Based on OCaml (influenced by C# and Haskell) History 2002: F# language design started 2005 January: F# 1.0.1 releases to public Not a product. Integration with VS2003 Works in .NET 1.0 through .NET 2.0 beta, Mono 2005 November: F# 1.1.5 with VS 2005 RTM support 2009 October: VS2010 Beta 2, CTP for VS2008 & Non-Windows users 2010: F# is “productized” and baked into VS 2010 What is F#
6. Interactive Scripting Uses REPL (Read Evaluate Print Loop) Good for prototyping Succinct = Less code Type Inference Strongly typed, strict Automatic generalization (generics for free) Few type annotations 1st class functions (currying, lazy evaluations) Pattern matching Key Characteristics of F#
7. Functional first Mutable keyword Functions are first class values Blend of functional and imperative Object oriented capabilities Built on .NET Framework Practical Leverage existing code What I like about F#
8. What is manycore? Lots of processors on one chip Tens or hundreds Intel – Future (Task parallelism) NVIDIA GPU – Today (Data parallelism) 500+ cores on chip Graphics, gaming, 3D rendering Use CUDA for financial or research computing Program in C or C++
9. The Power Wall: CPU Clock Speed Manycore -> Multicore -> Single core -> From Katherine Yelick’s “Multicore: Fallout of a Hardware Revolution”
10. Road to manycore 1970 – 2005 Single core on the “desktop” and laptop 2006 – 2011 Single core on the smartphone/tablet Multi core on the “desktop” Multi core in the cloud 2012 – 2020 Multi core on the smartphone/tablet 2021 Manycore probably will be common on many devices and computers
11. Multicore for Smartphones/Tablets Android 2.2 already supports multicore NVIDIA dual core test with one core shut off 1.5 to 1.6x faster with two cores http://bit.ly/nvidiadualcore NVIDIA quad core on its way Smartphones by holiday season 2011 Faster than 2 GHz notebook Core 2 Duo (T7200) http://bit.ly/eWMOsu Qualcomm quad core SnapDragon Devices expected in 2013 Intel announcing entry into Smartphone market
12. Declarative programming style Easier to introduce parallelism into existing code Immutability by default Can’t introduce race conditions Easier to write lock-free code Functional Programming
13. Type inference Expressions F# Basics let x = 5 let y = 5.0 let files = Directory.GetFiles(@"C:magesriginal") let x = 5 * 5 let y = 5.0 / 3.0 let width = image.Width / 8
15. The |> Combinator “Pipe Forward” Example F# Combinators x |> f is the same as f x let sqr x = x * x sqr 5 5 |> sqr
16. The <| Combinator “Pipe Backward” Example F# Combinators f <| x is the same as f x let sqr x = x * x sqr 5 sqr <| 3 + 2 not same as sqr 3 + 2
17. Similar to operator overloading Example Symbolic Functions let (operatorName) left right = <function> open System.Text.RegularExpressions let (===) str regex = Regex.Match(str, regex).Success
21. For more info on Client Object Model See “Using the SharePoint Foundation 2010 Managed Client Object Model” http://msdn.microsoft.com/en-us/library/ee857094(office.14).aspx
22. Load References FSX Script Open namespace Client Object Model Assemblies #r @"..eferenceAssembliesicrosoft.SharePoint.Client.dll" #r @"..eferenceAssembliesicrosoft.SharePoint.Client.Runtime.dll" open Microsoft.SharePoint.Client
23. Create SharePoint Client Context Pass Credentials Client Context and Credentials let ctx = new ClientContext("http://fsug.org") ctx.Credentials <- new NetworkCredential( user, password, domain)
24. Helper function to Load and Query Retrieve Site Collection Load and Query Context let load(ctx:ClientContext)(item:'a) = ctx.Load(item) ctx.ExecuteQuery() let site = ctx.Site load ctx site
25. Site Collection (Site) and Site (Web) Site Collection and Site let site = ctx.Site load ctx site let web = site.RootWeb load ctx web let title = web.Title
26. Use for loop to iterate through lists Iterate through Lists load ctxweb.Lists for list in web.Lists do print <| "List Title: " + list.Title
27. Query for list items using CAML Use for loop to iterate through list items Iterate through List Items let fsugMeetings = web.Lists.GetByTitle("FSUG Meetings") let query = new CamlQuery() query.ViewXml <- "<View><Query><OrderBy>... let listItems = fsugMeetings.GetItems(query) ctx.Load(fsugMeetings); ctx.Load(listItems); ctx.ExecuteQuery(); for meeting in listItems do print <| "Meeting: " + meeting.["Title"].ToString()
28. Use for loop to iterate through lists Iterate through Lists load ctxweb.Lists for list in web.Lists do print <| "List Title: " + list.Title
32. Thank You References: http://TryFsharp.org Play with F# on browser Tutorial Load and save files http://fsharp.net More information
33. My Info Talbott Crowell F# MVP http://fsug.org New England F# User Group http://twitter.com/talbott @talbott @BASPUG Boston Area SharePoint User Group http://ThirdM.com Third Millennium, Inc. Chief Architect