Skip to content

Commit fe46d15

Browse files
author
Kevin Logan
committed
draw cards
1 parent 4569546 commit fe46d15

File tree

10 files changed

+109
-6
lines changed

10 files changed

+109
-6
lines changed

Readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# F# Code Examples
1+
# F# Code Examples
2+
3+
Blog post:
4+
## Code Examples in examples folder
5+
6+
## Simple Unit Test Example in fsharpTests folder

examples/01HelloWorldRepl.fsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
printfn "Hello World!"

examples/02PipingFunctions.fs

Whitespace-only changes.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// send to F# Interactive with highlight + Alt + Enter
22
// or Open the Command Palette with Ctrl+Shift+P and find FSI: Start command (requires Ionide extension)
33
// https://stackoverflow.com/questions/56425674/how-to-enable-f-interactive-in-visual-studio-code
4-
module fSharpExamples
4+
5+
// referenced in Program.fs
6+
module fSharpExamples.Linq
57

68
let getOddSquares xs =
79
xs
@@ -12,4 +14,3 @@ let getOddSquares xs =
1214
// var xs = new List<int>{ 1, 2, 3, 4, 5, 6};
1315
// var results = xs.Where(x => x % 2 != 0).Select(x => x * x);
1416

15-
printfn "%A" (getOddSquares [1..10])

examples/03PipingFunctions.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// code from https://docs.microsoft.com/en-us/learn/modules/fsharp-functions/
2+
module fSharpExamples.Piping
3+
4+
printfn "03PipingFunctions"
5+
6+
// simple example of functions and piping
7+
let list = [4; 3; 1]
8+
let sort (list: int list) = List.sort list
9+
let print (list: int list)= List.iter(fun x-> printfn "item %i" x) list
10+
11+
let listSortPrint = list |> sort |> print
12+
13+
open Cards.Shuffle
14+
cards |> shuffle |> take 3 |> printAll
15+
16+
open Cards.Draw
17+
printfn "Deck: %A Hand: %A" d h

examples/Program.fs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
// For more information see https://aka.ms/fsharp-console-apps
2+
open System
3+
24
printfn "Hello from F#"
35

4-
printfn "%A" (fSharpExamples.getOddSquares([1..10]))
6+
open fSharpExamples.Linq
7+
printfn "getOddSquares: %A" (getOddSquares([1..10]))
8+
9+
open fSharpExamples.Piping
10+
// once I uncomment this, it will run all the functions in Piping
11+
// let myList = [123; 33; 22]
12+
// printfn "03PipingFunctions in Program"
13+
// listSortPrint
14+

examples/cards/DrawCards.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Cards.Draw
2+
3+
// fill in 0 to 5
4+
let cards = [ 0 .. 5 ]
5+
6+
// let drawCard (list:int list) =
7+
// printfn "%i" list.Head
8+
// list.Tail
9+
10+
// // let result = cards |> drawCard |> drawCard // 0 1
11+
12+
13+
let hand = []
14+
let drawCardToHand (deck: list<int>, draw: list<int>) =
15+
let firstCard = deck.Head
16+
printfn "%i" firstCard
17+
let hand =
18+
draw
19+
|> List.append [firstCard]
20+
(deck.Tail, hand)
21+
22+
let d, h = (cards, hand) |> drawCardToHand |> drawCardToHand

examples/cards/Program.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
open System
2+
3+
4+
// open ShuffleCards
5+
// cards |> shuffle |> take 3 |> printAll
6+
7+
// open DrawCards
8+
// // global type variables, this is the result of the functions
9+
// printfn "Deck: %A Hand: %A" d h

examples/cards/ShuffleCards.fs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://docs.microsoft.com/en-us/learn/modules/fshaarp-functions/5-exercise-patterns
2+
module Cards.Shuffle
3+
4+
let cards = [21; 3; 1; 7; 9; 23]
5+
6+
// cardFace is a function that takes in card which is inferred as an int and the returned as a string
7+
let cardFace card =
8+
let no = card % 13
9+
if no = 1 then "Ace"
10+
elif no = 0 then "King"
11+
elif no = 12 then "Queen"
12+
elif no = 11 then "Jack"
13+
else string no
14+
15+
let suit card =
16+
let no = card / 13
17+
if no = 0 then "Hearts"
18+
elif no = 1 then "Spades"
19+
elif no = 2 then "Diamonds"
20+
else "Clubs"
21+
22+
let shuffle list =
23+
let random = System.Random()
24+
list |> List.sortBy (fun x -> random.Next())
25+
26+
printfn "Cards.Shuffle"
27+
28+
// printCard is a function that takes in card
29+
let printCard card = printfn "%s of %s" (cardFace card) (suit card)
30+
31+
let printAll list = List.iter(fun x -> printCard(x)) list
32+
33+
let take (number:int) (list) = List.take number list

examples/fsharpExamples.fsproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
7-
<Compile Include="01SimilarToLinq.fs" />
7+
<Compile Include="01HelloWorldRepl.fsx" />
8+
<Compile Include="02SimilarToLinqWithPipes.fs" />
9+
<Compile Include="cards/DrawCards.fs" />
10+
<Compile Include="cards/ShuffleCards.fs" />
11+
<Compile Include="03PipingFunctions.fs" />
12+
13+
<!-- needs to be last-->
814
<Compile Include="Program.fs" />
9-
<Compile Include="02PipingFunctions.fs" />
1015
</ItemGroup>
1116
</Project>

0 commit comments

Comments
 (0)