-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 030de47
Showing
17 changed files
with
7,092 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#r "nuget:FSharp.Data" | ||
|
||
open FSharp.Data | ||
|
||
let getHtml (source: string) = | ||
async { | ||
let! html = HtmlDocument.AsyncLoad(source) | ||
return html | ||
} | ||
|
||
"https://docs.microsoft.com/dotnet/fsharp" | ||
|> getHtml | ||
|> Async.RunSynchronously | ||
|
||
// Run multiple async in parallel | ||
let documents = | ||
[ "https://docs.microsoft.com/dotnet/fsharp" | ||
"https://docs.microsoft.com/dotnet/fsharp/tutorials/asynchronous-and-concurrent-programming/async" | ||
"https://docs.microsoft.com/dotnet/fsharp/language-reference/asynchronous-workflows" ] | ||
|
||
documents | ||
|> List.map getHtml | ||
|> Async.Parallel | ||
|> Async.RunSynchronously | ||
|
||
// Run in order | ||
documents | ||
|> List.map getHtml | ||
|> Async.Sequential | ||
|> Async.RunSynchronously | ||
|
||
(*TASK INTEROP*) | ||
open System.Net.Http | ||
|
||
let getHtmlTask (source: string) = | ||
async { | ||
use client = new HttpClient() | ||
let! html = client.GetStringAsync(source) |> Async.AwaitTask | ||
let parsedHtml = HtmlDocument.Parse html | ||
return parsedHtml | ||
} | ||
|
||
"https://docs.microsoft.com/dotnet/fsharp" | ||
|> getHtmlTask | ||
|> Async.RunSynchronously | ||
|
||
// Merged https://github.com/dotnet/fsharp/pull/6811 | ||
// task { | ||
|
||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Domain = | ||
|
||
type Address = | ||
{ HouseNumber: int | ||
StreetName: string } | ||
|
||
type PhoneNumber = { Code: int; Number: string } | ||
|
||
type ContactMethod = | ||
| PostalMail of Address | ||
| Email of string | ||
| VoiceMail of PhoneNumber | ||
| SMS of PhoneNumber | ||
|
||
module Messenger = | ||
|
||
open Domain | ||
|
||
let sendMessage (message: string) (contactMethod: ContactMethod) = | ||
match contactMethod with | ||
| PostalMail { HouseNumber = h; StreetName = s } -> printfn $"Mailing {message} to {h} {s}" | ||
| Email e -> printfn $"Emailing {message} to {e}" | ||
| VoiceMail { Code = c; Number = n } -> printfn $"Left +{c}{n} a voicemail saying {message}" | ||
|
||
(**MAIN APPLICATION**) | ||
|
||
open Domain | ||
open Messenger | ||
|
||
let messages = | ||
[ "F# is good for the soul", | ||
(PostalMail | ||
{ HouseNumber = 1428 | ||
StreetName = "Elm Street" }) | ||
"Learn F#, this is the way", Email "[email protected]" | ||
"F# is succinct, performant, robust, and practical", VoiceMail { Code = 1; Number = "5555555" } ] | ||
|
||
messages | ||
|> List.iteri | ||
(fun i (message, contactMethod) -> | ||
printf $"{i}. " | ||
sendMessage message contactMethod) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
(** CREATE COLLECTIONS **) | ||
|
||
// List | ||
|
||
// Built-in creation functions | ||
List.init<int> 10 (fun i -> i) | ||
List.init<int> 10 (fun i -> i * 2) | ||
|
||
// Inline initialization | ||
[ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 ] | ||
[ 1 .. 10 ] | ||
|
||
// Array | ||
Array.init<int> 10 (id) | ||
Array2D.init<int> 3 3 (fun x _ -> x) | ||
Array3D.init<int> 3 3 3 (fun _ _ z -> z) | ||
|
||
[| 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 |] | ||
[| 1 .. 10 |] | ||
|
||
// Sequence | ||
Seq.init<int> 10 (fun i -> i) | ||
Seq.initInfinite<int> (fun i -> i) | ||
|
||
seq { | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
} | ||
|
||
seq { 1 .. 10 } | ||
|
||
(**ACCESS ELEMENTS**) | ||
open System | ||
|
||
type Transaction = | ||
{ Date: DateTime | ||
CustomerId: string | ||
Amount: double } | ||
|
||
let transactions = | ||
[ { Date = new DateTime(2021, 1, 1) | ||
CustomerId = "1" | ||
Amount = 23.80 } | ||
{ Date = new DateTime(2021, 2, 1) | ||
CustomerId = "1" | ||
Amount = 120.00 } | ||
{ Date = new DateTime(2021, 1, 5) | ||
CustomerId = "2" | ||
Amount = 46.09 } | ||
{ Date = new DateTime(2021, 3, 15) | ||
CustomerId = "3" | ||
Amount = 15.12 } | ||
{ Date = new DateTime(2021, 2, 15) | ||
CustomerId = "2" | ||
Amount = 3.09 } ] | ||
|
||
// Get first element | ||
transactions.Head | ||
|
||
// Get rest of elements | ||
transactions.Tail | ||
|
||
// Access by index | ||
// This is just for demo purposes. | ||
// Access by index is more efficient on arrays. | ||
transactions.[0] // similar to head | ||
transactions.[1..] // similar to tail | ||
|
||
// Built in collection operations | ||
transactions | ||
|> List.find (fun transaction -> transaction.CustomerId = "1") | ||
|
||
transactions | ||
|> List.find (fun transaction -> transaction.CustomerId = "4") // Throws error | ||
|
||
// Handle missing data using Option type | ||
transactions | ||
|> List.tryFind (fun x -> x.CustomerId = "4") | ||
|
||
transactions | ||
|> List.tryFind (fun x -> x.CustomerId = "1") | ||
|
||
// Append & Prepend | ||
let todoList = [ "Learn F#"; "Create app"; "Profit!" ] | ||
|
||
[ "Repeat" ] |> List.append todoList // Append | ||
todoList |> List.append [ "Make coffee" ] // Prepend | ||
|
||
(** CONVERT COLLECTIONS **) | ||
let transactionArray = transactions |> Array.ofList | ||
let transactionSeq = transactions |> Seq.ofList | ||
|
||
(**OPERATIONS**) | ||
transactions | ||
|> List.map | ||
(fun transaction -> | ||
let taxRate = 0.03 | ||
|
||
{| PreTax = transaction.Amount | ||
Tax = transaction.Amount * taxRate | ||
Total = transaction.Amount * (1.0 + taxRate) |}) // Calculate tax | ||
|
||
transactions | ||
|> List.iter (fun transaction -> printfn $"{transaction.CustomerId}") | ||
|
||
// Total Transactions | ||
transactions | ||
|> List.sumBy (fun transaction -> transaction.Amount) | ||
|
||
// Average amount | ||
transactions | ||
|> List.averageBy (fun transaction -> transaction.Amount) | ||
|
||
// Filter | ||
transactions | ||
|> List.filter (fun transaction -> transaction.Date > DateTime(2021, 1, 31)) | ||
|
||
// Sort | ||
transactions | ||
|> List.sortBy (fun transaction -> transaction.Date) | ||
|
||
transactions | ||
|> List.sortByDescending (fun transaction -> transaction.Date) | ||
|
||
// Pipelines | ||
transactions | ||
|> List.filter(fun transaction -> transaction.Date > DateTime(2021, 1, 31)) | ||
|> List.sortByDescending (fun transaction -> transaction.Date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Conditional expressions | ||
open System.IO | ||
|
||
let processFile (fileName:string) = | ||
let fileExtension = Path.GetExtension(fileName) | ||
|
||
if fileExtension = ".fs" then | ||
printfn "This is a source file" | ||
elif fileExtension = ".fsx" then | ||
printfn "This is a script" | ||
elif fileExtension = ".fsproj" then | ||
printfn "This is a build configuration file" | ||
else printfn "Can't process file" | ||
|
||
processFile "hello.fsx" | ||
processFile "app.fs" | ||
processFile "README.md" | ||
|
||
// Exception Handling | ||
let divide x y = | ||
try | ||
Some(x / y) | ||
with | ||
| :? System.DivideByZeroException -> printfn "Can't divide by zero"; None | ||
| ex -> printfn "Some other exception occurred";None | ||
|
||
divide 10 2 // Some | ||
divide 1 0 //None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// For start to end | ||
for i=1 to 3 do | ||
printfn $"{i}" | ||
|
||
// Reverse | ||
for i=3 downto 1 do | ||
printfn $"{i}" | ||
|
||
// For in | ||
let todoList = [ "Learn F#"; "Create app"; "Profit!" ] | ||
|
||
for todo in todoList do | ||
printfn $"{todo.ToUpper()}" | ||
|
||
// Collection generation | ||
[for todo in todoList do todo.ToUpper()] | ||
|
||
// While loop | ||
open System | ||
|
||
let mutable input = "" | ||
while (input <> "q") do | ||
input <- Console.ReadLine() | ||
printfn $"{input}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
type Address = {HouseNumber:int; StreetName: string} | ||
type PhoneNumber = {Code:int; Number:string} | ||
|
||
type ContactMethod = | ||
| PostalMail of Address | ||
| Email of string | ||
| VoiceMail of PhoneNumber | ||
| SMS of PhoneNumber | ||
|
||
let sendMessage (message:string) (contactMethod:ContactMethod) = | ||
match contactMethod with | ||
| PostalMail {HouseNumber=h;StreetName=s} -> printfn $"Mailing {message} to {h} {s}" | ||
| Email e -> printfn $"Emailing {message} to {e}" | ||
| VoiceMail {Code=c; Number=n} -> printfn $"Left +{c}{n} a voicemail saying {message}" | ||
|
||
let message = "Can't talk now, learning F#!" | ||
|
||
PostalMail {HouseNumber=1428; StreetName="Elm Street"} | ||
|> sendMessage message | ||
|
||
Email "[email protected]" | ||
|> sendMessage message | ||
|
||
VoiceMail {Code=1;Number="5555555"} | ||
|> sendMessage message | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
printfn "Hello World!" | ||
|
||
printfn "Hello F#" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#r "nuget:FSharp.Data,4.1.1" | ||
|
||
open FSharp.Data | ||
|
||
let html = Http.RequestString("https://docs.microsoft.com/dotnet/fsharp/") | ||
|
||
printfn $"{html}" |
Oops, something went wrong.