-
Notifications
You must be signed in to change notification settings - Fork 0
Продолжение про F# #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6dbf08
add solutions
bygu4 d2b1b36
refactor
bygu4 437c0eb
add tests for even numbers functions
bygu4 5feba7b
add tests
bygu4 42902b2
use cps with trees, update tests
bygu4 888e3e3
fix tests
bygu4 81170d6
minor changes
bygu4 2c44182
enable tail call optimization in props, update tests
bygu4 90dc39c
correct Expression type definition
bygu4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Tasks/Continuation/Continuation.Tests/Continuation.Tests.fsproj
This file contains hidden or 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,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>false</GenerateProgramFile> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="PrimesTests.fs" /> | ||
| <Compile Include="EvenNumbersTests.fs" /> | ||
| <Compile Include="ParseTreeTests.fs" /> | ||
| <Compile Include="TreeMapTests.fs" /> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
| <PackageReference Include="NUnit" Version="4.2.2" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="4.4.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
| <PackageReference Include="FsUnit" Version="7.0.1" /> | ||
| <PackageReference Include="FsCheck.NUnit" Version="3.1.0" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Continuation\Continuation.fsproj" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or 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 @@ | ||
| module EvenNumbersTests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open FsCheck | ||
| open FsCheck.NUnit | ||
|
|
||
| open EvenNumbers | ||
|
|
||
| [<Test>] | ||
| let testEvenNumbers_Map () = | ||
| countEvenNumbers_Map [] |> should equal 0 | ||
| countEvenNumbers_Map [1; 5; -3; 5; 77; 9] |> should equal 0 | ||
| countEvenNumbers_Map [4; 6; 8; 8] |> should equal 4 | ||
| countEvenNumbers_Map [0; 0; 1; 2; 3] |> should equal 3 | ||
| countEvenNumbers_Map [56592; -321321; 898934; 90901; -137] |> should equal 2 | ||
| countEvenNumbers_Map [2147483647] |> should equal 0 | ||
|
|
||
| [<Property>] | ||
| let areEquivalent_MapAndFilter (list: list<int>) = | ||
| countEvenNumbers_Map list = countEvenNumbers_Filter list | ||
|
|
||
| [<Property>] | ||
| let areEquivalent_FilterAndFold (list: list<int>) = | ||
| countEvenNumbers_Filter list = countEvenNumbers_Fold list | ||
|
|
||
| Check.QuickThrowOnFailure areEquivalent_MapAndFilter | ||
| Check.QuickThrowOnFailure areEquivalent_FilterAndFold |
This file contains hidden or 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,40 @@ | ||
| module ParseTreeTests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open System | ||
|
|
||
| open ParseTree | ||
|
|
||
| [<Test>] | ||
| let testEvaluate () = | ||
| let ``1 + 1`` = Operator (Sum, Const 1, Const 1) | ||
| let ``25 - 100`` = Operator (Difference, Const 25, Const 100) | ||
| let ``8 * 5`` = Operator (Product, Const 8, Const 5) | ||
| let ``8 * 5 + 25 - 100`` = Operator (Sum, ``8 * 5``, ``25 - 100``) | ||
| let ``14 / (1 + 1)`` = Operator (Ratio, Const 14, ``1 + 1``) | ||
| let ``14 / (1 + 1) - 9`` = Operator (Difference, ``14 / (1 + 1)``, Const 9) | ||
| let ``3 * (14 / (1 + 1) - 9)`` = Operator (Product, Const 3, ``14 / (1 + 1) - 9``) | ||
| let ``321 * 0`` = Operator (Product, Const 321, Const 0) | ||
| let ``0 / 0`` = Operator (Ratio, Const 0, Const 0) | ||
| let ``(1 + 1) / (321 * 0)`` = Operator (Ratio, ``1 + 1``, ``321 * 0``) | ||
|
|
||
| evaluate (Const 0) |> should equal 0 | ||
| evaluate ``1 + 1`` |> should equal 2 | ||
| evaluate ``25 - 100`` |> should equal -75 | ||
| evaluate ``8 * 5`` |> should equal 40 | ||
| evaluate ``8 * 5 + 25 - 100`` |> should equal -35 | ||
| evaluate ``14 / (1 + 1)`` |> should equal 7 | ||
| evaluate ``14 / (1 + 1) - 9`` |> should equal -2 | ||
| evaluate ``3 * (14 / (1 + 1) - 9)`` |> should equal -6 | ||
| evaluate ``321 * 0`` |> should equal 0 | ||
| (fun () -> evaluate ``0 / 0`` |> ignore) |> should throw typeof<DivideByZeroException> | ||
| (fun () -> evaluate ``(1 + 1) / (321 * 0)`` |> ignore) |> should throw typeof<DivideByZeroException> | ||
|
|
||
| [<Test>] | ||
| let testEvaluateWithLargeExpression () = | ||
| let treeDepth = 1000000 | ||
| { 1 .. treeDepth } | ||
| |> Seq.fold (fun node _ -> Operator (Sum, node, Const 1)) (Const 1) | ||
| |> evaluate | ||
| |> should equal (treeDepth + 1) |
This file contains hidden or 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 @@ | ||
| module PrimesTests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
|
|
||
| open Primes | ||
|
|
||
| [<Test>] | ||
| let testIsPrime () = | ||
| isPrime 0 |> should be False | ||
| isPrime 1 |> should be False | ||
| isPrime 2 |> should be True | ||
| isPrime 3 |> should be True | ||
| isPrime 73 |> should be True | ||
| isPrime 32132 |> should be False | ||
| isPrime -9091 |> should be False | ||
| isPrime 115249 |> should be True | ||
|
|
||
| [<Test>] | ||
| let testPrimes () = | ||
| let primes = primes () | ||
| primes |> Seq.take 10 |> should equal (seq { 2; 3; 5; 7; 11; 13; 17; 19; 23; 29 }) | ||
| primes |> Seq.item 41 |> should equal 181 | ||
| primes |> Seq.item 978 |> should equal 7717 | ||
| let primes = Seq.take 100000 primes | ||
| primes |> should be ascending | ||
| primes |> should be unique | ||
| primes |> Seq.filter isPrime |> should equal primes |
This file contains hidden or 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,4 @@ | ||
| module Program | ||
|
|
||
| [<EntryPoint>] | ||
| let main _ = 0 |
This file contains hidden or 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 TreeMapTests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
|
|
||
| open TreeMap | ||
|
|
||
| [<Test>] | ||
| let testTreeMap () = | ||
| let strTree = | ||
| Node ("abc", | ||
| Node ("", Empty, Node ("21321", Empty, Empty)), | ||
| Node ("ololo332", Empty, Empty)) | ||
| let lenTree = | ||
| Node (3, | ||
| Node (0, Empty, Node (5, Empty, Empty)), | ||
| Node (8, Empty, Empty)) | ||
| let intTree = | ||
| Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Empty)))) | ||
| let squaresTree = | ||
| Node (1, Empty, Node (4, Empty, Node (9, Empty, Node (16, Empty, Empty)))) | ||
|
|
||
| map (( * ) 5) Empty |> should equal Tree<int>.Empty | ||
| map (( = ) 0) (Node (0, Empty, Empty)) |> should equal (Node (true, Empty, Empty)) | ||
| map sqrt (Node (9.0, Empty, Empty)) |> should equal (Node (3.0, Empty, Empty)) | ||
| map String.length strTree |> should equal lenTree | ||
| map (fun x -> x * x) intTree |> should equal squaresTree | ||
|
|
||
| [<Test>] | ||
| let testMapWithLargeTree () = | ||
| let treeDepth = 1000000 | ||
| let sourceTree = | ||
| { 1 .. treeDepth } | ||
| |> Seq.fold (fun node _ -> Node (1, node, Empty)) Empty | ||
| let resultTree = | ||
| { 1 .. treeDepth } | ||
| |> Seq.fold (fun node _ -> Node (8, node, Empty)) Empty | ||
|
|
||
| sourceTree | ||
| |> map (( + ) 7) | ||
| |> areEqual resultTree | ||
| |> should be True | ||
This file contains hidden or 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 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.0.31903.59 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Continuation", "Continuation\Continuation.fsproj", "{F1533830-EECE-4FFF-AA1F-640C75003521}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Continuation.Tests", "Continuation.Tests\Continuation.Tests.fsproj", "{06E91877-A8A4-4068-A038-566B457A95E2}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {F1533830-EECE-4FFF-AA1F-640C75003521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {F1533830-EECE-4FFF-AA1F-640C75003521}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {F1533830-EECE-4FFF-AA1F-640C75003521}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {F1533830-EECE-4FFF-AA1F-640C75003521}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {06E91877-A8A4-4068-A038-566B457A95E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {06E91877-A8A4-4068-A038-566B457A95E2}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {06E91877-A8A4-4068-A038-566B457A95E2}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {06E91877-A8A4-4068-A038-566B457A95E2}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or 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,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| <Tailcalls>true</Tailcalls> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="EvenNumbers.fs" /> | ||
| <Compile Include="Primes.fs" /> | ||
| <Compile Include="TreeMap.fs" /> | ||
| <Compile Include="ParseTree.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,14 @@ | ||
| module EvenNumbers | ||
|
|
||
| let ( %% ) x y = | ||
| let rem = x % y | ||
| if rem >= 0 then rem else rem + y | ||
|
|
||
| let countEvenNumbers_Map: list<int> -> int = | ||
| Seq.map (fun x -> (x + 1) %% 2) >> Seq.sum | ||
|
|
||
| let countEvenNumbers_Filter: list<int> -> int = | ||
| Seq.filter (fun x -> x % 2 = 0) >> Seq.length | ||
|
|
||
| let countEvenNumbers_Fold: list<int> -> int = | ||
| Seq.fold (fun acc x -> acc + (x + 1) %% 2) 0 |
This file contains hidden or 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,31 @@ | ||
| module ParseTree | ||
|
|
||
| type Operation = | ||
| | Sum | ||
| | Difference | ||
| | Product | ||
| | Ratio | ||
|
|
||
| type Expression = | ||
| | Const of int | ||
| | Operator of Operation * Expression * Expression | ||
|
|
||
| let operation op = | ||
| match op with | ||
| | Sum -> ( + ) | ||
| | Difference -> ( - ) | ||
| | Product -> ( * ) | ||
| | Ratio -> ( / ) | ||
|
|
||
| [<TailCall>] | ||
| let rec evaluateCPS expr cont = | ||
| match expr with | ||
| | Const x -> cont x | ||
| | Operator (op, left, right) -> | ||
| evaluateCPS left (fun leftValue -> | ||
| evaluateCPS right (fun rightValue -> | ||
| operation op leftValue rightValue |> cont | ||
| ) | ||
| ) | ||
|
|
||
| let evaluate expr = evaluateCPS expr id |
This file contains hidden or 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,13 @@ | ||
| module Primes | ||
|
|
||
| let isPrime n = | ||
| if n >= 2 then | ||
| { 2 .. int (sqrt (float n)) } | ||
| |> Seq.exists (fun x -> n % x = 0) | ||
| |> not | ||
| else false | ||
|
|
||
| let primes () = | ||
| ( + ) 2 | ||
| |> Seq.initInfinite | ||
| |> Seq.filter isPrime |
This file contains hidden or 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,33 @@ | ||
| module TreeMap | ||
|
|
||
| type Tree<'a> = | ||
| | Empty | ||
| | Node of 'a * Tree<'a> * Tree<'a> | ||
|
|
||
| [<TailCall>] | ||
| let rec private mapCPS mapping tree cont = | ||
| match tree with | ||
| | Empty -> cont Empty | ||
| | Node (value, left, right) -> | ||
| mapCPS mapping left (fun mappedLeft -> | ||
| mapCPS mapping right (fun mappedRight -> | ||
| Node (mapping value, mappedLeft, mappedRight) |> cont | ||
| ) | ||
| ) | ||
|
|
||
| let map mapping tree = mapCPS mapping tree id | ||
|
|
||
| [<TailCall>] | ||
| let rec private areEqualCPS tree1 tree2 cont = | ||
| match tree1, tree2 with | ||
| | Empty, Empty -> cont true | ||
| | Empty, Node _ | Node _, Empty -> cont false | ||
| | Node (val1, _, _), Node (val2, _, _) when val1 <> val2 -> cont false | ||
| | Node (_, left1, right1), Node (_, left2, right2) -> | ||
| areEqualCPS left1 left2 (fun leftAreEqual -> | ||
| areEqualCPS right1 right2 (fun rightAreEqual -> | ||
| leftAreEqual && rightAreEqual |> cont | ||
| ) | ||
| ) | ||
|
|
||
| let areEqual tree1 tree2 = areEqualCPS tree1 tree2 id |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я как-то больше привык писать
(*), а не( * )