projects:fsharp_workshop
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projects:fsharp_workshop [2015/04/17 21:35] – mkucia | projects:fsharp_workshop [2015/05/02 12:45] (current) – [hello(x)] mkucia | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== F# workshop ====== | ====== F# workshop ====== | ||
+ | {{: | ||
+ | ===== Prerequisites ===== | ||
+ | * https:// | ||
+ | <hidden ↘ alternatives> | ||
+ | * Try F# in browser: http:// | ||
+ | * Use Visual Studio https:// | ||
+ | * Get a full distribution: | ||
+ | </ | ||
+ | ===== Agenda ===== | ||
- | * f(x) = Hello_Word | + | * hello(x) |
* Type providers (strongly typed) | * Type providers (strongly typed) | ||
* Matching | * Matching | ||
* Options | * Options | ||
- | ===== How to use? Where to get? ===== | ||
- | Try F# in browser: | ||
- | http:// | ||
- | Want to get a full distribution? | + | ===== hello(x) |
- | http:// | + | |
- | + | ||
- | ===== [Hello] | + | |
<code fsharp> | <code fsharp> | ||
let hello = printf "Hello World" | let hello = printf "Hello World" | ||
+ | hello | ||
</ | </ | ||
- | ===== 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 | + | * Multi platform |
* Modern, rising popularity | * Modern, rising popularity | ||
- | * Lazy evaluation | + | * Lazy evaluated |
* Reusable | * Reusable | ||
- | * Reduced code | + | * Concise (See [[http:// |
* 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 half column box> | <WRAP half column box> | ||
- | Cons: | ||
* Not suitable for real time (unpredictable performance) | * Not suitable for real time (unpredictable performance) | ||
- | * Different | + | * Hard to optimize (Functional languages fundamentally don't model how your computer works) |
+ | * Functional first (different | ||
</ | </ | ||
</ | </ | ||
+ | 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 " | ||
+ | </code> | ||
- | ===== Pitfalls | + | 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 |
+ | " | ||
+ | </ | ||
+ | |||
+ | <hidden pipeline operator definition> | ||
+ | How to define the operator? | ||
+ | <code fsharp> | ||
+ | let inline (|>) x f = f x | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <code fsharp> | ||
+ | let square x = x * x | ||
+ | let subtract x y = x - y | ||
+ | </ | ||
+ | |||
+ | <code fsharp> | ||
+ | let ``complicated stuff`` x = | ||
+ | printfn " | ||
+ | </ | ||
+ | |||
+ | <code fsharp> | ||
+ | let ``another complicated stuff`` x = | ||
+ | x |> square |> subtract 5 |> printfn " | ||
+ | </ | ||
+ | |||
+ | <code fsharp> | ||
+ | let equation x = | ||
+ | x | ||
+ | |> (fun y -> y * y ) | ||
+ | |> (fun y z -> y - z ) 5 | ||
+ | |> printfn " | ||
+ | </ | ||
+ | |||
+ | Note that F# uses indentation to indicate block structure. Find out more [[http:// | ||
+ | |||
+ | ===== Getting data ===== | ||
+ | |||
+ | The following example loads data from webpage | ||
+ | |||
+ | < | ||
+ | <code fsarp> | ||
+ | open System.Net | ||
+ | open System | ||
+ | open System.IO | ||
+ | |||
+ | let myURL = @" | ||
+ | |||
+ | let fetchUrl callback url = | ||
+ | let req | ||
+ | use resp = req.GetResponse() | ||
+ | use stream | ||
+ | use reader = new IO.StreamReader(stream) | ||
+ | callback reader url | ||
+ | |||
+ | let myCallback (reader: | ||
+ | let htmlSource = reader.ReadToEnd() | ||
+ | htmlSource | ||
+ | |||
+ | let getWebpageHTML = fetchUrl myCallback myURL | ||
+ | |||
+ | type FirstOrLast = | ||
+ | | GetFirst | ||
+ | | GetLast | ||
+ | |||
+ | let getDataFromHTML (rawHTML: | ||
+ | let split (split_point: | ||
+ | let array = text.Split([|split_point|], | ||
+ | match firstOrLast with | ||
+ | | GetFirst -> array.First() | ||
+ | | GetLast | ||
+ | rawHTML | ||
+ | |> split @"< | ||
+ | |> split @"</ | ||
+ | |||
+ | let removeAllTags text = | ||
+ | Regex.Replace(text, | ||
+ | |||
+ | let getCSVfromHTML rawHTML = | ||
+ | let mutable (text: | ||
+ | text <- rawHTML | ||
+ | text <- text.Replace(@"</ | ||
+ | text <- text.Replace(@"</ | ||
+ | text <- text.Replace(@"</ | ||
+ | text <- text.Replace(',', ' | ||
+ | text <- text.Replace(' | ||
+ | text <- removeAllTags text | ||
+ | text | ||
+ | |||
+ | let CSVresult = getCSVfromHTML (getDataFromHTML getWebpageHTML) | ||
+ | CSVresult.Dump() | ||
+ | </ | ||
+ | </ | ||
+ | > 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:// | * http:// |
projects/fsharp_workshop.1429299328.txt.gz · Last modified: 2015/04/17 21:35 by mkucia