5. I have 3 catsContact me: robert@strangelights.com http://strangelights.com/blog
6. What is a Combinator? A combinator is a higher-order function that uses only function application and earlier defined combinators to define a result from its arguments. Source: Wikipedia, http://en.wikipedia.org/wiki/Combinatory_Logic
7. Combinatory Logic in Computing In computer science, combinatory logic is used as a simplified model of computation, used in computability theory and proof theory. Despite its simplicity, combinatory logic captures many essential features of computation. Source: Wikipedia, http://en.wikipedia.org/wiki/Combinatory_Logic
8. Combinator Library "A combinator library offers functions (the combinators) that combine functions together to make bigger functions"[1]. These kinds of libraries are particularly useful for allowing domain-specific programming languages to be easily embedded into a general purpose language by defining a few primitive functions for the given domain. Souce: Wikipedia http://en.wikipedia.org/wiki/Combinator_library [1] “A History of Haskell” Hudak, Hughes, Peyton Jones, Wadler
9. History of Haskell: Combinator Libraries What is a combinatorlibrary? The reader will search in vain for a definition of this heavily used term, but the key idea is this: a combinator library offers functions (the combinators) that combine functions together to make bigger functions.
10. History of Haskell: Combinator Libraries What is a combinatorlibrary?The reader will search in vain for a definition of this heavily used term, but the key idea is this: a combinator library offers functions (the combinators) that combine functions together to make bigger functions.
11. History of Haskell: Combinator Libraries Another productive way to think of a combinator library is as a domain-specific language (DSL) for describing values of a particular type.
12. What is a Domain Specific Language? A programming language tailored for a particular application domain, which captures precisely the semantics of the application domain -- no more, no less. A DSL allows one to develop software for a particular application domain quickly, and effectively, yielding programs that are easy to understand, reason about, and maintain. Hudak
43. DSLs for Working with HTML #light – DSL for working with HTML, by SadekDrobi: http://sharplight.codeplex.com/ F# Web Tools – Tool kit for Ajax programing, by TomášPetříček: http://www.codeplex.com/fswebtools WebSharper – DSL for HTML with F# to JavaScript translator, by IntelliFactory: http://www.intellifactory.com/products/wsp
44. Hello world Paragraph Element [<JavaScript>] let Main () = let welcome = P [Text "Welcome"] Div [ welcome Input [Type "Button"; Value "Click me!"] |> On Events.Click (fun e -> welcome.Text <- "Hello, world!") ] Input Button Element Client Side Event Handler
45. How Does it Work? At compile time an extra step takes place: Functions & types are translated into JavaScript Compiler attempts to optimize JavaScript code Compiler tries to preserve binding names F# functions are compiled using the F# quotations system
46. Aside: Quotations in F# openMicrosoft.FSharp.Quotations.Patterns openMicrosoft.FSharp.Quotations.DerivedPatterns let quotation = <@ 1 + 1 @> // a function to interpret very simple quotations letrecinterpretQuotationexp = matchexpwith | Value (x, typ) whentyp = typeof<int> ->printfn"%d" (x :?> int) | SpecificCall <@ (+) @> (_, _, [l;r]) ->interpretQuotation l printfn"+" interpretQuotation r | _ ->printfn"not supported" // test the function interpretQuotation quotation
47. Formlets– Compositional Forms Text Input Element [<JavaScript>] let input label err = Controls.Input"" |> Validator.IsNotEmpty err |> Controls.Enhance.WithLabel label [<JavaScript>] letinputInt label err = Controls.Input"" |> Validator.IsInt err |> Controls.Enhance.WithLabellabel Add Validation Element Add Label Element
48. Formlets – Compositional Forms [<JavaScriptType>] typeBasicInfo = { Name : string; Age : int } [<JavaScript>] letBasicInfoForm () : Formlet<BasicInfo> = Formlet.Yield (fun name age -> { Name = name; Age = age |> int }) <*> input "Name""Please enter your name" <*> inputInt"Age""Please enter a valid age" Construct form result Construct and compose input elements
49. Formlets – Compositional Forms Configuration options letconf = { Render.FormConfiguration.Defaultwith Header = "Sign up" } Render.FormThenResponse conf (ContactForms.BasicInfoForm()) (fun info ->Div [ P [ "Thanks " + info.Name ] P [ "You are " + string info.Age + " years old" ] ]) Form and config Function that constructs result
52. Flowlets – Composing Formlets Flowlet.Run (fun (info, contact) -> letresult = matchcontact with | ContactForms.ContactVia.Address address -> "the address: " + address.Street + ", " + address.City + ", " + address.Country | ContactForms.ContactVia.Phone phone -> "the phone number: " + phone letresults = FieldSet [ Legend [Text "Sign-up summary"] P ["Hi " + info.Name + "!" |> Text] P ["You are " + string info.Age + " years old" |> Text] P ["Your preferred contact method is via " + result |> Text] ] steps -< [ results ] |> ignore)
53. Wrapping It Up Combinator libraries/DSL are an excellent way of tackling many problems Especially if that problem involves some kind of tree manipulation F# provides the features you need to implement combinator libraries
54. Further F# Info MSDN Center: http://msdn.microsoft.com/fsharp/ User’s forums: http://cs.hubfs.net/forums http://stackoverflow.com/tags/F%23 Blogs (there are lots of others) : http://blogs.msnd.com/dsyme http://strangelights.com/blog Websites : http://code.msdn.microsoft.com/fsharpsamples http://code.google.com/hosting/search?q=label:fsharp http://codeplex.com/Project/ProjectDirectory.aspx?TagName=F%23