Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Tasks/Test/Test.Tests/BinaryTreeTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module BinaryTree.Tests

open NUnit.Framework
open FsUnit

[<Test>]
let testFilter_EmptyTree_GetEmptyList () =
filter None (fun x -> x > 0) |> _.IsEmpty |> should be True

[<Test>]
let testFilter_SingleElementTree_GetTheTip () =
filter (Tip ("abc", None, None)) (fun s -> s.StartsWith "a")
|> should equal ["abc"]

[<Test>]
let testFilter_NestedTree_GetEvenNumbers () =
let tree = Tip (100,
Tip (0,
Tip (-1,
None,
Tip (15, None, None)
),
Tip (-124, None, None)
),
Tip (1000,
Tip (-6, None, None),
None
)
)
filter tree (fun x -> x % 2 = 0)
|> should equal [0; -124; 100; -6; 1000]
20 changes: 20 additions & 0 deletions Tasks/Test/Test.Tests/InfiniteSequenceTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module InfiniteSequence.Tests

open NUnit.Framework
open FsUnit
open FsCheck
open FsCheck.NUnit

[<Property>]
let anyEvenIndexOfSequenceIsPositive (index: NonNegativeInt) =
match index with
| x when int x % 2 = 0 -> Seq.item (int x) signAlternatingSequence > 0
| x -> Seq.item (int x) signAlternatingSequence < 0

[<Test>]
let testSequence_GetFirstNItems () =
signAlternatingSequence
|> Seq.take 12
|> should equal (seq [1; -2; 3; -4; 5; -6; 7; -8; 9; -10; 11; -12])

Check.QuickThrowOnFailure anyEvenIndexOfSequenceIsPositive
4 changes: 4 additions & 0 deletions Tasks/Test/Test.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Program

[<EntryPoint>]
let main _ = 0
30 changes: 30 additions & 0 deletions Tasks/Test/Test.Tests/Test.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="BinaryTreeTests.fs" />
<Compile Include="InfiniteSequenceTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Fscheck.nunit" Version="3.1.0" />
<PackageReference Include="fsunit" Version="7.0.1" />
<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" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Test\Test.fsproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions Tasks/Test/Test.sln
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}") = "Test", "Test\Test.fsproj", "{A1E410C5-287A-40C7-833F-34021FFB093A}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test.Tests", "Test.Tests\Test.Tests.fsproj", "{0BA19CC2-24D0-45D5-A373-30698C147C27}"
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
{A1E410C5-287A-40C7-833F-34021FFB093A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1E410C5-287A-40C7-833F-34021FFB093A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1E410C5-287A-40C7-833F-34021FFB093A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1E410C5-287A-40C7-833F-34021FFB093A}.Release|Any CPU.Build.0 = Release|Any CPU
{0BA19CC2-24D0-45D5-A373-30698C147C27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0BA19CC2-24D0-45D5-A373-30698C147C27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0BA19CC2-24D0-45D5-A373-30698C147C27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0BA19CC2-24D0-45D5-A373-30698C147C27}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions Tasks/Test/Test/BinaryTree.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module BinaryTree

/// Definition of a binary tree.
type Tree<'a> =
| None
| Tip of 'a * Tree<'a> * Tree<'a>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip по-английски означает "кончик", в презентации, где речь ша про деревья, использовался в значении "лист". Правильнее было бы Node или что-то такое


/// Get all of the elements of the given `tree` for which the `condition` is true.
let filter (tree: Tree<'a>) (condition: 'a -> bool) =

/// Traverse the `tree` in CPS.
let rec filterInternal (tree: Tree<'a>) (condition: 'a -> bool) (cont: unit -> 'a list) =
match tree with
| None -> cont ()
| Tip (x, left, right) ->
if condition x then
filterInternal left condition (fun () ->
x :: filterInternal right condition cont
)
else
filterInternal left condition (fun () ->
filterInternal right condition cont
)

filterInternal tree condition (fun () -> [])
18 changes: 18 additions & 0 deletions Tasks/Test/Test/InfiniteSequence.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module InfiniteSequence

/// A sign alternating sequence of ones.
/// (1, -1, 1, -1, ...)
let signAlternatingOnes =
Seq.initInfinite (( + ) 1)
|> Seq.map (fun x ->
match x with
| x when x % 2 = 0 -> -1
| _ -> 1
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Неэлегантно :) Это всё можно было через Seq.unfold в одну строчку сделать


/// A sign alternating sequence with ascending module.
/// (1, -2, 3, -4, ...)
let signAlternatingSequence =
Seq.initInfinite (( + ) 1)
|> Seq.zip signAlternatingOnes
|> Seq.map (fun (x, y) -> x * y)
13 changes: 13 additions & 0 deletions Tasks/Test/Test/Test.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="BinaryTree.fs" />
<Compile Include="InfiniteSequence.fs" />
</ItemGroup>

</Project>
Loading