From da77b41983996e6df899ae37e1df2cfd53411d67 Mon Sep 17 00:00:00 2001 From: IgnatSergeev Date: Fri, 31 May 2024 18:12:12 +0300 Subject: [PATCH 1/2] Finished --- F#/Workflows.Test/Workflows.Test.fsproj | 29 ++++++++++++++++++++++++ F#/Workflows.Test/WorkflowsTest.fs | 29 ++++++++++++++++++++++++ F#/Workflows/Workflows.fs | 30 +++++++++++++++++++++++++ F#/Workflows/Workflows.fsproj | 12 ++++++++++ F#/forSpbu.sln | 12 ++++++++++ 5 files changed, 112 insertions(+) create mode 100644 F#/Workflows.Test/Workflows.Test.fsproj create mode 100644 F#/Workflows.Test/WorkflowsTest.fs create mode 100644 F#/Workflows/Workflows.fs create mode 100644 F#/Workflows/Workflows.fsproj diff --git a/F#/Workflows.Test/Workflows.Test.fsproj b/F#/Workflows.Test/Workflows.Test.fsproj new file mode 100644 index 0000000..1828af3 --- /dev/null +++ b/F#/Workflows.Test/Workflows.Test.fsproj @@ -0,0 +1,29 @@ + + + + net8.0 + + false + true + true + + + + + + + + + + + + + + + + + + + + + diff --git a/F#/Workflows.Test/WorkflowsTest.fs b/F#/Workflows.Test/WorkflowsTest.fs new file mode 100644 index 0000000..52b73be --- /dev/null +++ b/F#/Workflows.Test/WorkflowsTest.fs @@ -0,0 +1,29 @@ +module Workflows.Test + +open NUnit.Framework +open FsUnit + +[] +let roundingTest () = + rounding 3 { + let! a = 2.0 / 12.0 + let! b = 3.5 + return a / b + } |> should equal 0.048 + +[] +let stringCalcTest () = + calculate { + let! a = "1" + let! b = "2" + return a + b + } |> should equal (Some(3)) + +[] +let stringIncorrectCalcTest () = + calculate { + let! a = "1" + let! b = "a" + let z = a + b + return z + } |> should equal None diff --git a/F#/Workflows/Workflows.fs b/F#/Workflows/Workflows.fs new file mode 100644 index 0000000..3a7f583 --- /dev/null +++ b/F#/Workflows/Workflows.fs @@ -0,0 +1,30 @@ +module Workflows + +/// +/// Rounding to a precision workflow builder +/// +/// Workflow precision +type RoundingBuilder(prec: int) = + member this.Bind(x: float, f) = + f (System.Math.Round(x, prec)) + member this.Return(x: float) = System.Math.Round(x, prec) + +/// +/// Builds rounding to a precision workflow +/// +let rounding = RoundingBuilder + +/// +/// String calculation workflow builder +/// +type StringCalculateBuilder() = + member this.Bind(x: string, f) = + try f (x |> int) with + | :? System.FormatException -> None + member this.Return(x: int) = Some(x) + + +/// +/// Build string calculation workflow +/// +let calculate = StringCalculateBuilder() diff --git a/F#/Workflows/Workflows.fsproj b/F#/Workflows/Workflows.fsproj new file mode 100644 index 0000000..80c48ff --- /dev/null +++ b/F#/Workflows/Workflows.fsproj @@ -0,0 +1,12 @@ + + + + net8.0 + true + + + + + + + diff --git a/F#/forSpbu.sln b/F#/forSpbu.sln index 9d54d03..cc81cf6 100644 --- a/F#/forSpbu.sln +++ b/F#/forSpbu.sln @@ -21,6 +21,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "07.03", "07.03", "{5CA9053B EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Homework1", "Homework1\Homework1.fsproj", "{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Workflows", "Workflows\Workflows.fsproj", "{C69807AF-66FC-4C75-811D-FA1242A2219E}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Workflows.Test", "Workflows.Test\Workflows.Test.fsproj", "{94AE559A-A34E-42F2-8C59-F486247239EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,6 +62,14 @@ Global {89A935E8-B5F3-435D-ACC3-A99DD7C66178}.Debug|Any CPU.Build.0 = Debug|Any CPU {89A935E8-B5F3-435D-ACC3-A99DD7C66178}.Release|Any CPU.ActiveCfg = Release|Any CPU {89A935E8-B5F3-435D-ACC3-A99DD7C66178}.Release|Any CPU.Build.0 = Release|Any CPU + {C69807AF-66FC-4C75-811D-FA1242A2219E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C69807AF-66FC-4C75-811D-FA1242A2219E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C69807AF-66FC-4C75-811D-FA1242A2219E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C69807AF-66FC-4C75-811D-FA1242A2219E}.Release|Any CPU.Build.0 = Release|Any CPU + {94AE559A-A34E-42F2-8C59-F486247239EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94AE559A-A34E-42F2-8C59-F486247239EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94AE559A-A34E-42F2-8C59-F486247239EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94AE559A-A34E-42F2-8C59-F486247239EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {7937CDA8-8285-4E23-AD1A-FC0F04FEEFE6} = {91E3BDA2-0836-46C2-95F0-02513FD7F13F} From 43ae532cbb1ae56d9c4bcf3e8142bdd6dd64d725 Mon Sep 17 00:00:00 2001 From: "ignat.sergeev" Date: Sat, 1 Jun 2024 13:46:55 +0300 Subject: [PATCH 2/2] Fixed review --- F#/Workflows/Workflows.fs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/F#/Workflows/Workflows.fs b/F#/Workflows/Workflows.fs index 3a7f583..4b8a609 100644 --- a/F#/Workflows/Workflows.fs +++ b/F#/Workflows/Workflows.fs @@ -19,8 +19,9 @@ let rounding = RoundingBuilder /// type StringCalculateBuilder() = member this.Bind(x: string, f) = - try f (x |> int) with - | :? System.FormatException -> None + let res = 0 + let parsed = System.Int32.TryParse(x, ref res) + if parsed then Some(res) else None member this.Return(x: int) = Some(x)