User Tools

Site Tools


projects:fsharp_workshop

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
projects:fsharp_workshop [2015/03/14 18:11] mkuciaprojects:fsharp_workshop [2015/05/02 12:45] (current) – [hello(x)] mkucia
Line 1: Line 1:
 ====== F# workshop ====== ====== F# workshop ======
 +{{:projects:fsharp_logo.png?nolink|}}
 +===== Prerequisites =====
 +    * https://www.linqpad.net/ or anything that can execute a REPL((Read, Evaluate, Print Loop))
 +<hidden ↘ alternatives>
 +    * Try F# in browser: http://www.tryfsharp.org/Create
 +    * Use Visual Studio https://www.visualstudio.com/
 +    * Get a full distribution: http://fsharp.org/use/windows/ OR http://fsharp.org/use/linux/
 +</hidden>
  
 +===== Agenda =====
  
-  * f(x) = Hello_Word +  * hello(x) 
-  * On Testing and +  * Type providers (strongly typed) 
 +  * Matching 
 +  * Options
  
-===== How to use? Where to get? ===== 
-Try F# in browser: 
-http://www.tryfsharp.org/Create 
  
-Want to get a full distribution?  +===== hello(x) ===== 
-http://fsharp.org/ → Use F# +<code fsharp>
- +
-===== [Hello] ===== +
-<code>+
 let hello = printf "Hello World" let hello = printf "Hello World"
 +hello
 </code> </code>
  
-===== Why? ===== +F# (pronounced “F Sharp”) is 
- +
- +
  
 <WRAP group> <WRAP group>
 <WRAP half column box> <WRAP half column box>
-Pros: 
   * Open Source   * Open Source
-  * Multi platform (Mono, .NET core)+  * Multi platform
   * Modern, rising popularity   * Modern, rising popularity
-  * Lazy evaluation+  * Lazy evaluated
   * Reusable   * Reusable
-  * Reduced code+  * Concise (See [[http://theburningmonk.com/2014/12/seven-ineffective-coding-habits-many-f-programmers-dont-have/|signal-to-noise ratio]])
   * Strongly typed   * Strongly typed
 +  * Functional first
 +    * Use functional to write easy to test and understand code
 +    * Use imperative for performance and compatibility
 +    * Use objective for organization and encapsulation
 +  * Compiled
 </WRAP> </WRAP>
  
 <WRAP half column box> <WRAP half column box>
-Cons: 
   * Not suitable for real time (unpredictable performance)   * Not suitable for real time (unpredictable performance)
-  * Different thinking process (you need to re-learn)+  * Hard to optimize (Functional languages fundamentally don't model how your computer works) 
 +  * Functional first (different thinking processyou will need to re-learn)
 </WRAP> </WRAP>
 </WRAP> </WRAP>
 +programming language for writing simple code to solve complex problems. 
  
-<hidden ↘ C# equivalent>Since you want to know more, here is an explanation with more details […]</hidden>+<code fsharp> 
 +let hello x = printfn "Hello %s" x 
 +hello "world" 
 +</code>
  
-===== Pitfalls and drawbacks ===== +Introducing pipeline operator: 
-Functional languages fundamentally don't model how your computer works+<code fsharp> 
-Harder to optimize.+let hello x printfn "Hello %s" x 
 +"World" |> hello 
 +</code> 
 + 
 +<hidden pipeline operator definition> 
 +How to define the operator?  
 +<code fsharp> 
 +let inline (|>) x f f x 
 +</code> 
 +</hidden> 
 + 
 +<code fsharp> 
 +let square x x * x 
 +let subtract x y x - y 
 +</code> 
 + 
 +<code fsharp> 
 +let ``complicated stuff`` x = 
 +    printfn "%d" (add 5 (square x)) 
 +</code> 
 + 
 +<code fsharp> 
 +let ``another complicated stuff`` x = 
 +    x |> square |> subtract 5 |> printfn "%d" 
 +</code> 
 + 
 +<code fsharp> 
 +let equation x = 
 + x  
 + |> (fun y -> y * y )  
 + |> (fun y z -> y - z ) 5 
 + |> printfn "%d" 
 +</code> 
 + 
 +Note that F# uses indentation to indicate block structure. Find out more [[http://fsharpforfunandprofit.com/posts/fsharp-syntax/|here]]. 
 + 
 +===== Getting data ===== 
 + 
 +The following example loads data from webpage and stores it in CSV file. 
 + 
 +<hidden> 
 +<code fsarp> 
 +open System.Net 
 +open System 
 +open System.IO 
 + 
 +let myURL @"..." 
 + 
 +let fetchUrl callback url         
 +    let req    WebRequest.Create(Uri(url))  
 +    use resp   req.GetResponse()  
 +    use stream resp.GetResponseStream()  
 +    use reader = new IO.StreamReader(stream)  
 +    callback reader url 
 +  
 +let myCallback (reader:IO.StreamReader) url =  
 +    let htmlSource = reader.ReadToEnd() 
 +    htmlSource 
 + 
 +let getWebpageHTML = fetchUrl myCallback myURL 
 + 
 +type FirstOrLast =  
 + | GetFirst 
 + | GetLast 
 + 
 +let getDataFromHTML (rawHTML:String) =  
 + let split (split_point:String) (firstOrLast:FirstOrLast) (text:String) =  
 + let array = text.Split([|split_point|], 2, StringSplitOptions.RemoveEmptyEntries) 
 + match firstOrLast with 
 + | GetFirst -> array.First() 
 + | GetLast  -> array.Last() 
 + rawHTML 
 + |> split @"<table id=""t2"">" GetLast 
 + |> split @"</table>"          GetFirst 
 +  
 +let removeAllTags text = 
 +    Regex.Replace(text, "<.*?>", String.Empty); 
 + 
 +let getCSVfromHTML rawHTML =  
 + let mutable (text:String) = String.Empty 
 + text <- rawHTML 
 + text <- text.Replace(@"</th>", ";"); 
 + text <- text.Replace(@"</td>", ";"); 
 + text <- text.Replace(@"</tr>", Environment.NewLine); 
 + text <- text.Replace(',', '|'); 
 + text <- text.Replace('.', ','); 
 + text <- removeAllTags text 
 + text 
 + 
 +let CSVresult = getCSVfromHTML (getDataFromHTML getWebpageHTML) 
 +CSVresult.Dump() 
 +</code> 
 +</hidden>
  
 +> An F# **type provider** is a component that provides types, properties, and methods for use in your program. 
 +> The key to information-rich programming is to eliminate barriers to working with diverse information sources. One significant barrier to including a source of information into a program is the need to represent that information as types, properties, and methods for use in a programming language environment. 
 +> Writing these types manually is very time-consuming and difficult to maintain. A common alternative is to use a code generator which adds files to your project; however, the conventional types of code generation do not integrate well into exploratory modes of programming supported by F# because the generated code must be replaced each time a service reference is adjusted.
 ===== References ===== ===== References =====
   * http://fsharp.org/   * http://fsharp.org/
 +  * http://fsharp3sample.codeplex.com/wikipage?Title=MicroSamples
   * http://en.wikibooks.org/wiki/F_Sharp_Programming   * http://en.wikibooks.org/wiki/F_Sharp_Programming
   * http://fsharp.org/specs/language-spec/3.0/FSharpSpec-3.0-final.pdf   * http://fsharp.org/specs/language-spec/3.0/FSharpSpec-3.0-final.pdf
Line 57: Line 164:
   * http://fsharpforfunandprofit.com/site-contents/   * http://fsharpforfunandprofit.com/site-contents/
   * http://research.microsoft.com/en-us/projects/kiwi/   * http://research.microsoft.com/en-us/projects/kiwi/
 +  * [[https://www.youtube.com/watch?v=hMjv8m--W28|Ten things F# can do, and C# can't - Liam McLennan @ DDD Brisbane 2013]]
projects/fsharp_workshop.1426353069.txt.gz · Last modified: 2015/03/14 18:11 by mkucia