From a30545e08e83290ff19ee3ad274dc9584e153c97 Mon Sep 17 00:00:00 2001 From: Danil Date: Tue, 6 Oct 2020 13:15:06 +0300 Subject: [PATCH 01/19] MaldelbrotSet --- Maldelbrot/Maldelbrot app/Form1.Designer.cs | 63 +++++++++++++++++++ Maldelbrot/Maldelbrot app/Form1.cs | 21 +++++++ Maldelbrot/Maldelbrot app/Form1.resx | 60 ++++++++++++++++++ .../Maldelbrot app/Maldelbrot app.csproj | 14 +++++ Maldelbrot/Maldelbrot app/Program.cs | 20 ++++++ Maldelbrot/MaldelbrotSet/Fractal.fs | 55 ++++++++++++++++ Maldelbrot/MaldelbrotSet/Library.fs | 44 +++++++++++++ Maldelbrot/MaldelbrotSet/MaldelbrotSet.fsproj | 13 ++++ Maldelbrot/Mandelbrot set.sln | 31 +++++++++ 9 files changed, 321 insertions(+) create mode 100644 Maldelbrot/Maldelbrot app/Form1.Designer.cs create mode 100644 Maldelbrot/Maldelbrot app/Form1.cs create mode 100644 Maldelbrot/Maldelbrot app/Form1.resx create mode 100644 Maldelbrot/Maldelbrot app/Maldelbrot app.csproj create mode 100644 Maldelbrot/Maldelbrot app/Program.cs create mode 100644 Maldelbrot/MaldelbrotSet/Fractal.fs create mode 100644 Maldelbrot/MaldelbrotSet/Library.fs create mode 100644 Maldelbrot/MaldelbrotSet/MaldelbrotSet.fsproj create mode 100644 Maldelbrot/Mandelbrot set.sln diff --git a/Maldelbrot/Maldelbrot app/Form1.Designer.cs b/Maldelbrot/Maldelbrot app/Form1.Designer.cs new file mode 100644 index 0000000..321f12e --- /dev/null +++ b/Maldelbrot/Maldelbrot app/Form1.Designer.cs @@ -0,0 +1,63 @@ +namespace Maldelbrot_app +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.SuspendLayout(); + // + // pictureBox1 + // + this.pictureBox1.Location = new System.Drawing.Point(35, 31); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(972, 631); + this.pictureBox1.TabIndex = 0; + this.pictureBox1.TabStop = false; + this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1027, 687); + this.Controls.Add(this.pictureBox1); + this.ForeColor = System.Drawing.SystemColors.ControlLightLight; + this.Name = "Form1"; + this.Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.PictureBox pictureBox1; + } +} + diff --git a/Maldelbrot/Maldelbrot app/Form1.cs b/Maldelbrot/Maldelbrot app/Form1.cs new file mode 100644 index 0000000..3c5cb5f --- /dev/null +++ b/Maldelbrot/Maldelbrot app/Form1.cs @@ -0,0 +1,21 @@ +using System.Drawing; +using System.Windows.Forms; + +namespace Maldelbrot_app +{ + public partial class Form1 : Form + { + Bitmap newBitmap; + public Form1() + { + InitializeComponent(); + } + + private void pictureBox1_Click(object sender, System.EventArgs e) + { + var iterations = 1; + pictureBox1.Image= Fractal.createImage(1.5, -1.5, -1.5, 1); + } + + } +} diff --git a/Maldelbrot/Maldelbrot app/Form1.resx b/Maldelbrot/Maldelbrot app/Form1.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Maldelbrot/Maldelbrot app/Form1.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Maldelbrot/Maldelbrot app/Maldelbrot app.csproj b/Maldelbrot/Maldelbrot app/Maldelbrot app.csproj new file mode 100644 index 0000000..1d934a2 --- /dev/null +++ b/Maldelbrot/Maldelbrot app/Maldelbrot app.csproj @@ -0,0 +1,14 @@ + + + + WinExe + netcoreapp3.1 + Maldelbrot_app + true + + + + + + + \ No newline at end of file diff --git a/Maldelbrot/Maldelbrot app/Program.cs b/Maldelbrot/Maldelbrot app/Program.cs new file mode 100644 index 0000000..f1b7be8 --- /dev/null +++ b/Maldelbrot/Maldelbrot app/Program.cs @@ -0,0 +1,20 @@ +using System; +using System.Windows.Forms; + +namespace Maldelbrot_app +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.SetHighDpiMode(HighDpiMode.SystemAware); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/Maldelbrot/MaldelbrotSet/Fractal.fs b/Maldelbrot/MaldelbrotSet/Fractal.fs new file mode 100644 index 0000000..3a971c8 --- /dev/null +++ b/Maldelbrot/MaldelbrotSet/Fractal.fs @@ -0,0 +1,55 @@ +open System.Drawing +open System.Windows.Forms +open System + +type VizualizeForm () = + inherit Form () + override this.OnLoad (e : EventArgs) = + this.DoubleBuffered <- true + +let MapToRange inMin inMax outMin outMax x = + (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin + +let form = new VizualizeForm(MaximizeBox = true, Text = "Mandelbrot Set Visualization", Width = 480, Height = 360) +let mutable size = (double)1.0 +let maxIterations sz = + (int)(100.0 / (sqrt sz)) + +let rec mandelbrotFunction iterations a b ca cb maxIter = + if iterations < maxIter && a*a + b*b < ((double)4.0) then + let asquare = a*a - b*b + let bsquare = ((double)2.0)*a*b + mandelbrotFunction (iterations+1) (asquare + ca) (bsquare + cb) ca cb maxIter + else + iterations + +let mutable startx = -((double)0.73) +let mutable starty = -((double)0.18) +let vizualizeSet (e : PaintEventArgs) = + let g = e.Graphics + g.Clear(Color.Black) + let currentMaxIterations = maxIterations size + for x in 0..form.Width do + for y in 1..form.Height do + let a = startx + MapToRange ((double)0.0) ((double)form.Width) -size size ((double)x) + let b = starty + MapToRange ((double)0.0) ((double)form.Height) -size size ((double)y) + let iterations = mandelbrotFunction 0 a b a b currentMaxIterations + let brightness = + match iterations with + | _ when iterations = currentMaxIterations -> 0 + | _ -> (int)((double)(iterations) |> MapToRange 0.0 ((double)currentMaxIterations) 0.0 255.0) + g.FillRectangle(new SolidBrush(Color.FromArgb((brightness + 10)%255, (brightness * 2)%255, (brightness + 40)%255)), x, y, 1, 1) + + + +form.Paint.Add vizualizeSet + +async { +while true do + do! Async.Sleep(1) + form.Invalidate() + size <- (size * 0.9) + printf "%d " (maxIterations size) +} |> Async.StartImmediate + +Application.Run form \ No newline at end of file diff --git a/Maldelbrot/MaldelbrotSet/Library.fs b/Maldelbrot/MaldelbrotSet/Library.fs new file mode 100644 index 0000000..55186ef --- /dev/null +++ b/Maldelbrot/MaldelbrotSet/Library.fs @@ -0,0 +1,44 @@ +module Fractal +open System.Windows.Forms +open System.Drawing +open System.Numerics + + +let cMax = Complex(1.0, 1.0) +let cMin = Complex (-1.0, -1.0) + +let compare (z: Complex) = cMin.Real < z.Real && z.Real < cMax.Real && cMin.Imaginary < z.Imaginary && z.Imaginary < cMax.Imaginary +let rec isInMandelbrotSet (z, c, iter, count) = + if (compare z) && (count < iter) then + isInMandelbrotSet ( ((z * z) + c), c, iter, (count + 1) ) + else count + +let scalingFactor s = s * 1.0 / 200.0 +let offsetX = -1.0 +let offsetY = -1.0 + +let mapPlane (x, y, s, mx, my) = + let fx = ((float x) * scalingFactor s) + mx + let fy = ((float y) * scalingFactor s) + my + Complex(fx, fy) + +let colorize c = + let r = (4 * c) % 255 + let g = (6 * c) % 255 + let b = (8 * c) % 255 + Color.FromArgb(r,g,b) + +let createImage (s, mx, my, iter) = + let image = new Bitmap(400, 400) + for x = 0 to image.Width - 1 do + for y = 0 to image.Height - 1 do + let count = isInMandelbrotSet( Complex.Zero, (mapPlane (x, y, s, mx, my)), iter, 0) + if count = iter then + image.SetPixel(x,y, Color.Black) + else + image.SetPixel(x,y, colorize( count ) ) + let temp = new Form() in + temp.Paint.Add(fun e -> e.Graphics.DrawImage(image, 0, 0)) + temp + +do Application.Run(createImage (1.5, -1.5, -1.5, 20)) \ No newline at end of file diff --git a/Maldelbrot/MaldelbrotSet/MaldelbrotSet.fsproj b/Maldelbrot/MaldelbrotSet/MaldelbrotSet.fsproj new file mode 100644 index 0000000..e9c243d --- /dev/null +++ b/Maldelbrot/MaldelbrotSet/MaldelbrotSet.fsproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.1 + true + + + + + + + diff --git a/Maldelbrot/Mandelbrot set.sln b/Maldelbrot/Mandelbrot set.sln new file mode 100644 index 0000000..28fab8c --- /dev/null +++ b/Maldelbrot/Mandelbrot set.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30523.141 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Maldelbrot app", "Maldelbrot app\Maldelbrot app.csproj", "{0121F812-5AAA-4388-B6D7-2732FDC786AB}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "MaldelbrotSet", "MaldelbrotSet\MaldelbrotSet.fsproj", "{A0A27C51-BE9C-4B74-95B0-112E666AD724}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0121F812-5AAA-4388-B6D7-2732FDC786AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0121F812-5AAA-4388-B6D7-2732FDC786AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0121F812-5AAA-4388-B6D7-2732FDC786AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0121F812-5AAA-4388-B6D7-2732FDC786AB}.Release|Any CPU.Build.0 = Release|Any CPU + {A0A27C51-BE9C-4B74-95B0-112E666AD724}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0A27C51-BE9C-4B74-95B0-112E666AD724}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0A27C51-BE9C-4B74-95B0-112E666AD724}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0A27C51-BE9C-4B74-95B0-112E666AD724}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DA678E87-B0D9-4E7E-BC56-F37F28A53035} + EndGlobalSection +EndGlobal From 95f44cd3ddc35df868da28790ac4d73bc38f34f7 Mon Sep 17 00:00:00 2001 From: Danil Date: Tue, 6 Oct 2020 13:25:24 +0300 Subject: [PATCH 02/19] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20maybebuilder=20=D0=B2=20main=20=D0=B8=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalculatorF/CalculatorF.sln | 19 ++++++++++++++----- CalculatorF/Program.fs | 15 ++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CalculatorF/CalculatorF.sln b/CalculatorF/CalculatorF.sln index a66da7c..f56e793 100644 --- a/CalculatorF/CalculatorF.sln +++ b/CalculatorF/CalculatorF.sln @@ -1,6 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "CalculatorF", "CalculatorF\CalculatorF.fsproj", "{A55ACE78-975B-4CCD-8176-33831A6D162F}" +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30523.141 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "CalculatorF", "CalculatorF.fsproj", "{BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -8,9 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {A55ACE78-975B-4CCD-8176-33831A6D162F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A55ACE78-975B-4CCD-8176-33831A6D162F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A55ACE78-975B-4CCD-8176-33831A6D162F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A55ACE78-975B-4CCD-8176-33831A6D162F}.Release|Any CPU.Build.0 = Release|Any CPU + {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A2E9BAB3-38AA-4C4C-B45F-0E977F3C2CED} EndGlobalSection EndGlobal diff --git a/CalculatorF/Program.fs b/CalculatorF/Program.fs index aafce82..0ec19f3 100644 --- a/CalculatorF/Program.fs +++ b/CalculatorF/Program.fs @@ -39,10 +39,9 @@ let styles = NumberStyles.AllowDecimalPoint let provider = new CultureInfo("en-US") let output (result : float option) = - if - result = None then Console.WriteLine("Введено неверное число/оператор или попытка деления на нуль") - else - Console.WriteLine(result.Value) + match result with + | None -> Console.WriteLine("Mission Failed we'll get em next time") + | Some result -> Console.WriteLine(result) let getNumber (str:string) = maybe{ let isNumber, number = Double.TryParse(str, styles, provider) @@ -57,9 +56,11 @@ let main argv = let a = getNumber(Console.ReadLine()) let op = Console.ReadLine() let b = getNumber(Console.ReadLine()) - if a = None || b = None then output None else - let result = calculate op a.Value b.Value - output result + let result = maybe{ + let! calc = calculate op a.Value b.Value + return calc + } + output result 0 \ No newline at end of file From 7baa1534e7ab1c752133c6eb497862c3aae4c542 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 7 Oct 2020 18:56:31 +0300 Subject: [PATCH 03/19] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D0=BF=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B8=D0=BB=D0=BE=D1=81=D1=8C=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=BF=D0=B5=D0=BB=D0=B8=D0=B2=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Maldelbrot/Maldelbrot app/Form1.Designer.cs | 28 ++++++-- Maldelbrot/Maldelbrot app/Form1.cs | 47 +++++++++++-- Maldelbrot/MaldelbrotSet/Fractal.fs | 78 ++++++++------------- 3 files changed, 94 insertions(+), 59 deletions(-) diff --git a/Maldelbrot/Maldelbrot app/Form1.Designer.cs b/Maldelbrot/Maldelbrot app/Form1.Designer.cs index 321f12e..6dd5626 100644 --- a/Maldelbrot/Maldelbrot app/Form1.Designer.cs +++ b/Maldelbrot/Maldelbrot app/Form1.Designer.cs @@ -28,24 +28,42 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.button1 = new System.Windows.Forms.Button(); + this.timer1 = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // - this.pictureBox1.Location = new System.Drawing.Point(35, 31); + this.pictureBox1.Location = new System.Drawing.Point(37, 32); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(972, 631); + this.pictureBox1.Size = new System.Drawing.Size(749, 631); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; - this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click); + this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(812, 32); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(121, 57); + this.button1.TabIndex = 1; + this.button1.Text = "button1"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // timer1 + // + this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1027, 687); + this.ClientSize = new System.Drawing.Size(973, 687); + this.Controls.Add(this.button1); this.Controls.Add(this.pictureBox1); this.ForeColor = System.Drawing.SystemColors.ControlLightLight; this.Name = "Form1"; @@ -58,6 +76,8 @@ private void InitializeComponent() #endregion private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Timer timer1; } } diff --git a/Maldelbrot/Maldelbrot app/Form1.cs b/Maldelbrot/Maldelbrot app/Form1.cs index 3c5cb5f..26213bb 100644 --- a/Maldelbrot/Maldelbrot app/Form1.cs +++ b/Maldelbrot/Maldelbrot app/Form1.cs @@ -1,21 +1,56 @@ -using System.Drawing; +using System; +using System.Drawing; using System.Windows.Forms; namespace Maldelbrot_app { public partial class Form1 : Form { - Bitmap newBitmap; + private double hx, hy; + private Bitmap bmp; + private double sizeArea; + + private void button1_Click(object sender, EventArgs e) + { + Draw(); + timer1.Start(); + } + + + private void pictureBox1_MouseClick(object sender, MouseEventArgs e) + { + int X = e.X, + Y = e.Y; + switch (e.Button) + { + case MouseButtons.Left: + hx = Fractal.temp(X, hx, sizeArea, pictureBox1.Width); + hy = Fractal.temp(Y, hy, sizeArea, pictureBox1.Height); + Draw(); + break; + } + } + + private void timer1_Tick_1(object sender, EventArgs e) + { + sizeArea /= 1.1; + Draw(); + } + public Form1() { InitializeComponent(); + + bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); + pictureBox1.Image = bmp; + + sizeArea = 3; } - private void pictureBox1_Click(object sender, System.EventArgs e) + private void Draw() { - var iterations = 1; - pictureBox1.Image= Fractal.createImage(1.5, -1.5, -1.5, 1); + bmp = Fractal.createImage(sizeArea, pictureBox1.Height, pictureBox1.Width, hx, hy); + pictureBox1.Image = bmp; } - } } diff --git a/Maldelbrot/MaldelbrotSet/Fractal.fs b/Maldelbrot/MaldelbrotSet/Fractal.fs index 3a971c8..c05a30b 100644 --- a/Maldelbrot/MaldelbrotSet/Fractal.fs +++ b/Maldelbrot/MaldelbrotSet/Fractal.fs @@ -1,55 +1,35 @@ -open System.Drawing +module Fractal +open System.Drawing open System.Windows.Forms +open System.Numerics open System +let temp c f size k = f - (size / 2.0) + (float c) * size / k -type VizualizeForm () = - inherit Form () - override this.OnLoad (e : EventArgs) = - this.DoubleBuffered <- true - -let MapToRange inMin inMax outMin outMax x = - (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin - -let form = new VizualizeForm(MaximizeBox = true, Text = "Mandelbrot Set Visualization", Width = 480, Height = 360) -let mutable size = (double)1.0 -let maxIterations sz = - (int)(100.0 / (sqrt sz)) - -let rec mandelbrotFunction iterations a b ca cb maxIter = - if iterations < maxIter && a*a + b*b < ((double)4.0) then - let asquare = a*a - b*b - let bsquare = ((double)2.0)*a*b - mandelbrotFunction (iterations+1) (asquare + ca) (bsquare + cb) ca cb maxIter - else - iterations - -let mutable startx = -((double)0.73) -let mutable starty = -((double)0.18) -let vizualizeSet (e : PaintEventArgs) = - let g = e.Graphics - g.Clear(Color.Black) - let currentMaxIterations = maxIterations size - for x in 0..form.Width do - for y in 1..form.Height do - let a = startx + MapToRange ((double)0.0) ((double)form.Width) -size size ((double)x) - let b = starty + MapToRange ((double)0.0) ((double)form.Height) -size size ((double)y) - let iterations = mandelbrotFunction 0 a b a b currentMaxIterations - let brightness = - match iterations with - | _ when iterations = currentMaxIterations -> 0 - | _ -> (int)((double)(iterations) |> MapToRange 0.0 ((double)currentMaxIterations) 0.0 255.0) - g.FillRectangle(new SolidBrush(Color.FromArgb((brightness + 10)%255, (brightness * 2)%255, (brightness + 40)%255)), x, y, 1, 1) - - +let p x y = sqrt(Math.Pow(x-0.25,2.0) + Math.Pow(y,2.0)) + +let q x y = Math.Atan2(y, x- 0.25) + +let pc q = 0.5-0.5*cos q -form.Paint.Add vizualizeSet +let createImage sizeArea heigth wight hx hy = + let image = new Bitmap((int heigth), (int wight)) + for x in 0..image.Width-1 do + for y in 0..image.Height-1 do + let mutable x_ = temp x hx sizeArea wight + let mutable y_ = temp y hy sizeArea heigth + let mutable z = new Complex(0.0, 0.0) + let mutable it = 0 + let mutable Break = false + while (Break = false && it < 100) do + it <- it + 1 + z <- z*z + z <- z + new Complex(x_, y_) + if (z.Magnitude > 2.0) then Break <- true + else + image.SetPixel(x, y, Color.FromArgb(255, it % 8 * 16, it % 4 * 32, it % 2 * 64)) + image -async { -while true do - do! Async.Sleep(1) - form.Invalidate() - size <- (size * 0.9) - printf "%d " (maxIterations size) -} |> Async.StartImmediate -Application.Run form \ No newline at end of file + + + From d8e6c09b9f5bfae89ac0827b183647deb9db21b7 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 7 Oct 2020 23:16:39 +0300 Subject: [PATCH 04/19] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BC=D0=B0=D0=B3=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=87=D0=B8=D1=81=D0=BB=D0=B0=20=D0=B8=20=D1=83?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B9?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Maldelbrot/Maldelbrot app/Form1.cs | 24 +++++++++++------------- Maldelbrot/MaldelbrotSet/Fractal.fs | 6 ------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Maldelbrot/Maldelbrot app/Form1.cs b/Maldelbrot/Maldelbrot app/Form1.cs index 26213bb..fccf5d2 100644 --- a/Maldelbrot/Maldelbrot app/Form1.cs +++ b/Maldelbrot/Maldelbrot app/Form1.cs @@ -6,9 +6,10 @@ namespace Maldelbrot_app { public partial class Form1 : Form { - private double hx, hy; - private Bitmap bmp; - private double sizeArea; + private double _hx, _hy; + private Bitmap _bmp; + private double _sizeArea = 3; + private readonly double _zoom = 1.1; private void button1_Click(object sender, EventArgs e) { @@ -16,7 +17,6 @@ private void button1_Click(object sender, EventArgs e) timer1.Start(); } - private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int X = e.X, @@ -24,8 +24,8 @@ private void pictureBox1_MouseClick(object sender, MouseEventArgs e) switch (e.Button) { case MouseButtons.Left: - hx = Fractal.temp(X, hx, sizeArea, pictureBox1.Width); - hy = Fractal.temp(Y, hy, sizeArea, pictureBox1.Height); + _hx = Fractal.temp(X, _hx, _sizeArea, pictureBox1.Width); + _hy = Fractal.temp(Y, _hy, _sizeArea, pictureBox1.Height); Draw(); break; } @@ -33,7 +33,7 @@ private void pictureBox1_MouseClick(object sender, MouseEventArgs e) private void timer1_Tick_1(object sender, EventArgs e) { - sizeArea /= 1.1; + _sizeArea /= _zoom; Draw(); } @@ -41,16 +41,14 @@ public Form1() { InitializeComponent(); - bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); - pictureBox1.Image = bmp; - - sizeArea = 3; + _bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); + pictureBox1.Image = _bmp; } private void Draw() { - bmp = Fractal.createImage(sizeArea, pictureBox1.Height, pictureBox1.Width, hx, hy); - pictureBox1.Image = bmp; + _bmp = Fractal.createImage(_sizeArea, pictureBox1.Height, pictureBox1.Width, _hx, _hy); + pictureBox1.Image = _bmp; } } } diff --git a/Maldelbrot/MaldelbrotSet/Fractal.fs b/Maldelbrot/MaldelbrotSet/Fractal.fs index c05a30b..dca2611 100644 --- a/Maldelbrot/MaldelbrotSet/Fractal.fs +++ b/Maldelbrot/MaldelbrotSet/Fractal.fs @@ -5,12 +5,6 @@ open System.Numerics open System let temp c f size k = f - (size / 2.0) + (float c) * size / k -let p x y = sqrt(Math.Pow(x-0.25,2.0) + Math.Pow(y,2.0)) - -let q x y = Math.Atan2(y, x- 0.25) - -let pc q = 0.5-0.5*cos q - let createImage sizeArea heigth wight hx hy = let image = new Bitmap((int heigth), (int wight)) for x in 0..image.Width-1 do From 138c1885e7bd3c39f2daa4c451cb74cf9952be66 Mon Sep 17 00:00:00 2001 From: Danil Date: Sun, 11 Oct 2020 11:48:07 +0300 Subject: [PATCH 05/19] CalculatorASPProject --- CalculatorASP/Calculator/Calculator.cs | 35 ++++++++++ CalculatorASP/Calculator/Calculator.csproj | 19 ++++++ CalculatorASP/Calculator/Program.cs | 23 +++++++ CalculatorASP/CalculatorASP.sln | 37 +++++++++++ .../CalculatorASP/CalculatorASP.csproj | 12 ++++ CalculatorASP/CalculatorASP/Program.cs | 26 ++++++++ .../Properties/launchSettings.json | 27 ++++++++ .../appsettings.Development.json | 9 +++ CalculatorASP/CalculatorASP/appsettings.json | 10 +++ CalculatorASP/CalculatorF/CalculatorF.fsproj | 12 ++++ CalculatorASP/CalculatorF/Program.fs | 6 ++ CalculatorF/CalculatorF.fsproj | 12 ---- CalculatorF/CalculatorF.sln | 25 ------- CalculatorF/Program.fs | 66 ------------------- 14 files changed, 216 insertions(+), 103 deletions(-) create mode 100644 CalculatorASP/Calculator/Calculator.cs create mode 100644 CalculatorASP/Calculator/Calculator.csproj create mode 100644 CalculatorASP/Calculator/Program.cs create mode 100644 CalculatorASP/CalculatorASP.sln create mode 100644 CalculatorASP/CalculatorASP/CalculatorASP.csproj create mode 100644 CalculatorASP/CalculatorASP/Program.cs create mode 100644 CalculatorASP/CalculatorASP/Properties/launchSettings.json create mode 100644 CalculatorASP/CalculatorASP/appsettings.Development.json create mode 100644 CalculatorASP/CalculatorASP/appsettings.json create mode 100644 CalculatorASP/CalculatorF/CalculatorF.fsproj create mode 100644 CalculatorASP/CalculatorF/Program.fs delete mode 100644 CalculatorF/CalculatorF.fsproj delete mode 100644 CalculatorF/CalculatorF.sln delete mode 100644 CalculatorF/Program.fs diff --git a/CalculatorASP/Calculator/Calculator.cs b/CalculatorASP/Calculator/Calculator.cs new file mode 100644 index 0000000..cef31ec --- /dev/null +++ b/CalculatorASP/Calculator/Calculator.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace Calculator +{ + public class Calculator + { + private readonly Dictionary> _calculatorOperations = + new Dictionary>() + { + {"+", (a, b) => a + b }, + {"-", (a, b) => a - b }, + {"*", (a, b) => a * b }, + {"/", (a, b) => a / b } + }; + public decimal Calc(decimal arg1, decimal arg2, string oper) + { + return _calculatorOperations[oper](arg1, arg2); + } + public decimal GetNumber(string s) + { + const NumberStyles styles = NumberStyles.AllowDecimalPoint; + var provider = new CultureInfo("en-US"); + return decimal.Parse(s, styles, provider); + } + public decimal Calc(string s) + { + var str = s.Split(); + var arg1 = GetNumber(str[0]); + var arg2 = GetNumber(str[2]); + return Calc(arg1, arg2, str[1]); + } + } +} \ No newline at end of file diff --git a/CalculatorASP/Calculator/Calculator.csproj b/CalculatorASP/Calculator/Calculator.csproj new file mode 100644 index 0000000..65cb1e3 --- /dev/null +++ b/CalculatorASP/Calculator/Calculator.csproj @@ -0,0 +1,19 @@ + + + + Exe + netcoreapp3.1 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/CalculatorASP/Calculator/Program.cs b/CalculatorASP/Calculator/Program.cs new file mode 100644 index 0000000..e6cdaab --- /dev/null +++ b/CalculatorASP/Calculator/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Diagnostics.CodeAnalysis; +namespace Calculator +{ + [ExcludeFromCodeCoverage] + internal static class Program + { + private static void Main() + { + try + { + var calculator = new Calculator(); + var str = Console.ReadLine(); + var value = calculator.Calc(str); + Console.WriteLine(value); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } + } +} diff --git a/CalculatorASP/CalculatorASP.sln b/CalculatorASP/CalculatorASP.sln new file mode 100644 index 0000000..f72c2cf --- /dev/null +++ b/CalculatorASP/CalculatorASP.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30523.141 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculatorASP", "CalculatorASP\CalculatorASP.csproj", "{11D5E5E8-8456-4A3D-9038-496437BF33D0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calculator", "..\Calculator\Calculator.csproj", "{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Calculator.DesignPatterns.Proxy", "CalculatorProxy\Calculator.DesignPatterns.Proxy.fsproj", "{3D2B84F5-1415-405F-B5B8-573751D8BDFD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {11D5E5E8-8456-4A3D-9038-496437BF33D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {11D5E5E8-8456-4A3D-9038-496437BF33D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {11D5E5E8-8456-4A3D-9038-496437BF33D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {11D5E5E8-8456-4A3D-9038-496437BF33D0}.Release|Any CPU.Build.0 = Release|Any CPU + {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.Build.0 = Release|Any CPU + {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B882847A-B94B-491F-BB5A-5E7A7C064526} + EndGlobalSection +EndGlobal diff --git a/CalculatorASP/CalculatorASP/CalculatorASP.csproj b/CalculatorASP/CalculatorASP/CalculatorASP.csproj new file mode 100644 index 0000000..9455cae --- /dev/null +++ b/CalculatorASP/CalculatorASP/CalculatorASP.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/CalculatorASP/CalculatorASP/Program.cs b/CalculatorASP/CalculatorASP/Program.cs new file mode 100644 index 0000000..688d450 --- /dev/null +++ b/CalculatorASP/CalculatorASP/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace CalculatorASP +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/CalculatorASP/CalculatorASP/Properties/launchSettings.json b/CalculatorASP/CalculatorASP/Properties/launchSettings.json new file mode 100644 index 0000000..c71679d --- /dev/null +++ b/CalculatorASP/CalculatorASP/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51963", + "sslPort": 44300 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "CalculatorASP": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CalculatorASP/CalculatorASP/appsettings.Development.json b/CalculatorASP/CalculatorASP/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/CalculatorASP/CalculatorASP/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/CalculatorASP/CalculatorASP/appsettings.json b/CalculatorASP/CalculatorASP/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/CalculatorASP/CalculatorASP/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/CalculatorASP/CalculatorF/CalculatorF.fsproj b/CalculatorASP/CalculatorF/CalculatorF.fsproj new file mode 100644 index 0000000..5e4d540 --- /dev/null +++ b/CalculatorASP/CalculatorF/CalculatorF.fsproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/CalculatorASP/CalculatorF/Program.fs b/CalculatorASP/CalculatorF/Program.fs new file mode 100644 index 0000000..7968e93 --- /dev/null +++ b/CalculatorASP/CalculatorF/Program.fs @@ -0,0 +1,6 @@ + + +[] +let main argv = + printfn "Hello World from F#!" + 0 // return an integer exit code diff --git a/CalculatorF/CalculatorF.fsproj b/CalculatorF/CalculatorF.fsproj deleted file mode 100644 index 05bd285..0000000 --- a/CalculatorF/CalculatorF.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - netcoreapp3.1 - - - - - - - diff --git a/CalculatorF/CalculatorF.sln b/CalculatorF/CalculatorF.sln deleted file mode 100644 index f56e793..0000000 --- a/CalculatorF/CalculatorF.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30523.141 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "CalculatorF", "CalculatorF.fsproj", "{BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BE5C75DA-449E-40E7-B5B5-C4981FABF2D9}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {A2E9BAB3-38AA-4C4C-B45F-0E977F3C2CED} - EndGlobalSection -EndGlobal diff --git a/CalculatorF/Program.fs b/CalculatorF/Program.fs deleted file mode 100644 index 0ec19f3..0000000 --- a/CalculatorF/Program.fs +++ /dev/null @@ -1,66 +0,0 @@ -open System -open System.Globalization - -type MaybeBuilder() = - member this.Bind(x, f) = - match x with - | None -> None - | Some a -> f a - - member this.Return(x) = - Some x - -let maybe = MaybeBuilder() - -let add x y = x + y - -let subtract x y = x - y - -let multiply x y = x * y - -let divide x y = maybe{ - let! x = match y with - | 0.0 -> None - | _ -> Some (x / y) - return x - } - -let calculate op x y = maybe{ - let! x = match op with - | "+" -> Some(add x y) - | "-" -> Some(subtract x y) - | "*" -> Some(multiply x y) - | "/" -> divide x y - | _ -> None - return x - } -let styles = NumberStyles.AllowDecimalPoint - -let provider = new CultureInfo("en-US") - -let output (result : float option) = - match result with - | None -> Console.WriteLine("Mission Failed we'll get em next time") - | Some result -> Console.WriteLine(result) - -let getNumber (str:string) = maybe{ - let isNumber, number = Double.TryParse(str, styles, provider) - let! str = match isNumber with - | true -> Some(number) - | false -> None - return str - } - -[] -let main argv = - let a = getNumber(Console.ReadLine()) - let op = Console.ReadLine() - let b = getNumber(Console.ReadLine()) - let result = maybe{ - let! calc = calculate op a.Value b.Value - return calc - } - output result - 0 - - \ No newline at end of file From 8028afc3a91ad3442a8fd34280a6d17b6750f13a Mon Sep 17 00:00:00 2001 From: Danil Date: Sun, 11 Oct 2020 11:48:47 +0300 Subject: [PATCH 06/19] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20Proxy=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Calculator.DesignPatterns.Proxy.fsproj | 11 +++ .../CalculatorProxy/CalculatorProxy.fsproj | 4 ++ CalculatorASP/CalculatorProxy/Library.fs | 70 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj create mode 100644 CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj create mode 100644 CalculatorASP/CalculatorProxy/Library.fs diff --git a/CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj b/CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj new file mode 100644 index 0000000..c5da923 --- /dev/null +++ b/CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + + + + diff --git a/CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj b/CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/CalculatorASP/CalculatorProxy/Library.fs b/CalculatorASP/CalculatorProxy/Library.fs new file mode 100644 index 0000000..69b0b89 --- /dev/null +++ b/CalculatorASP/CalculatorProxy/Library.fs @@ -0,0 +1,70 @@ +namespace Calculator.DesignPatterns.Proxy + +type MaybeBuilder() = + member this.Bind(x, f) = + match x with + | None -> None + | Some a -> f a + + member this.Return(x) = + Some x + +type AsyncMaybeBuilder () = + member this.Bind(x, f) = + async { + let! x' = x + match x' with + | Some s -> return! f s + | None -> return None + } + + member this.Return(x) = + async{return x} + +open System.Text.RegularExpressions; +open System +open System.Globalization + +module Proxy = + let private pattern = @"-?\d+(?:\.\d+)?)\s*([-+*\/])\s*(-?\d+(?:\.\d+)?" + + let private maybeAsync = new AsyncMaybeBuilder() + + let private maybe = new MaybeBuilder() + + let CheckAccess input = + Regex.IsMatch(input, pattern) + + let private add x y = x + y + + let private subtract x y = x - y + + let private multiply x y = x * y + + let private divide x y = maybe{ + let! x = match y with + | 0.0 -> None + | _ -> Some (x / y) + return x + } + + let calculate op x y = maybe{ + let! x = match op with + | "+" -> Some(add x y) + | "-" -> Some(subtract x y) + | "*" -> Some(multiply x y) + | "/" -> divide x y + | _ -> None + return x + } + let private styles = NumberStyles.AllowDecimalPoint + + let private provider = new CultureInfo("en-US") + + let getNumber (str:string) = maybe{ + let isNumber, number = Double.TryParse(str, styles, provider) + let! str = match isNumber with + | true -> Some(number) + | false -> None + return str + } \ No newline at end of file From c8a293039710b9337d8025cbeb4ac7d93ee9b780 Mon Sep 17 00:00:00 2001 From: Danil Date: Sun, 11 Oct 2020 11:49:25 +0300 Subject: [PATCH 07/19] =?UTF-8?q?ProxyMiddleware=20=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=20=D0=BA=D0=B0=D0=BA=D1=83=D1=8E=20=D1=82?= =?UTF-8?q?=D0=BE=20=D1=84=D0=B8=D0=B3=D0=BD=D1=8E=20=D0=BD=D0=B5=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD=D1=83=D1=8E=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=D0=BE=20=D1=80=D0=B0=D0=B7=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CalculatorASP/CalculatorMiddleware.cs | 29 +++++++++++++++++ .../CalculatorASP/ProxyMiddleware.cs | 26 +++++++++++++++ CalculatorASP/CalculatorASP/Startup.cs | 32 +++++++++++++++++++ CalculatorASP/CalculatorASP/result.cs | 12 +++++++ 4 files changed, 99 insertions(+) create mode 100644 CalculatorASP/CalculatorASP/CalculatorMiddleware.cs create mode 100644 CalculatorASP/CalculatorASP/ProxyMiddleware.cs create mode 100644 CalculatorASP/CalculatorASP/Startup.cs create mode 100644 CalculatorASP/CalculatorASP/result.cs diff --git a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs new file mode 100644 index 0000000..d9dc1b6 --- /dev/null +++ b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using System; +using System.Threading.Tasks; + +namespace CalculatorASP +{ + public class CalculatorMiddleware + { + private readonly RequestDelegate _next; + + public CalculatorMiddleware(RequestDelegate next) => _next = next; + + public async Task InvokeAsync(HttpContext context) + { + var value = context.Request.Query["value"]; + var calclulator = new Calculator.Calculator(); + Result.Value = calclulator.Calc(value); + await _next.Invoke(context); + } + } + public static class TokenExtensions + { + public static IApplicationBuilder UseToken(this IApplicationBuilder builder) + { + return builder.UseMiddleware(); + } + } +} diff --git a/CalculatorASP/CalculatorASP/ProxyMiddleware.cs b/CalculatorASP/CalculatorASP/ProxyMiddleware.cs new file mode 100644 index 0000000..3e2a9f2 --- /dev/null +++ b/CalculatorASP/CalculatorASP/ProxyMiddleware.cs @@ -0,0 +1,26 @@ +using Calculator.DesignPatterns.Proxy; +using Microsoft.AspNetCore.Http; +using System.Threading.Tasks; + +namespace CalculatorASP +{ + public class ProxyMiddleware + { + private readonly RequestDelegate _next; + + public ProxyMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task InvokeAsync(HttpContext context) + { + var value = context.Request.Query["value"]; + var check = Proxy.CheckAccess(value); + if (check) + await _next.Invoke(context); + else + context.Response.StatusCode = 403; + } + } +} diff --git a/CalculatorASP/CalculatorASP/Startup.cs b/CalculatorASP/CalculatorASP/Startup.cs new file mode 100644 index 0000000..1fd30bb --- /dev/null +++ b/CalculatorASP/CalculatorASP/Startup.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace CalculatorASP +{ + +public class Startup + { + public Startup() + { + + } + public void Configure(IApplicationBuilder app) + { + app.UseMiddleware(); + + app.UseToken(); + + app.Run(async (context) => + { + await context.Response.WriteAsync($"{Result.Value}"); + }); + } + } +} diff --git a/CalculatorASP/CalculatorASP/result.cs b/CalculatorASP/CalculatorASP/result.cs new file mode 100644 index 0000000..cd9e1fd --- /dev/null +++ b/CalculatorASP/CalculatorASP/result.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace CalculatorASP +{ + public static class Result + { + public static decimal Value { get; set; } + } +} From ba3ba838e5844e965dcb4ae927afe3a5d025af47 Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 12 Oct 2020 18:28:14 +0300 Subject: [PATCH 08/19] =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalculatorASP/ProxyF/Program.fs | 69 ++++++++++++++++++++++++++++++ CalculatorASP/ProxyF/ProxyF.fsproj | 12 ++++++ 2 files changed, 81 insertions(+) create mode 100644 CalculatorASP/ProxyF/Program.fs create mode 100644 CalculatorASP/ProxyF/ProxyF.fsproj diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs new file mode 100644 index 0000000..40e75ad --- /dev/null +++ b/CalculatorASP/ProxyF/Program.fs @@ -0,0 +1,69 @@ +open System +open System.Net +open System.Text.RegularExpressions; +open System.IO + +type MaybeBuilder() = + member this.Bind(x,f)= + match x with + |None -> None + |Some a -> f a + member this.Return(x)= + Some x + +let maybe = new MaybeBuilder() + +type AsyncMaybeBuilder () = + member this.Bind(x, f) = + async { + let! _x = x + match _x with + | Some s -> return! f s + | None -> return None + } + + member this.Return(x:'a option) = + async{return x} + +let asyncMaybe = new AsyncMaybeBuilder() + +let checkAccess input = + Regex.IsMatch(input, @"(-?\d+(?:\.\d+)?)\s*([-%2B*%2F])\s*(-?\d+(?:\.\d+)?)") + +let output (result : float option) = + match result with + | None -> Console.WriteLine("Mission Failed we'll get em next time") + | Some result -> Console.WriteLine(result) + +let getOper op = + match op with + | "+" -> "%2B" + | "*" -> "*" + | "/" -> "%2F" + | "-" -> "-" + | _ -> "" + +let giveRequest expression = async{ + + let request = WebRequest.Create("http://localhost:51963?value=" + expression, Method = "GET", ContentType = "text/plain") + let response = request.GetResponse() + let stream = response.GetResponseStream(); + let readStream = new StreamReader(stream) + return readStream.ReadToEnd() + } + +let RunCalculator expression = + let result = Async.RunSynchronously(giveRequest expression) + result + +[] +let main argv = + let a = Console.ReadLine() + let op = getOper(Console.ReadLine()) + let b = Console.ReadLine() + let expression = a + " " + op + " " + b; + let result = match checkAccess expression with + | true -> RunCalculator expression + | false -> "Bad request" + printf"%s"result + 0 \ No newline at end of file diff --git a/CalculatorASP/ProxyF/ProxyF.fsproj b/CalculatorASP/ProxyF/ProxyF.fsproj new file mode 100644 index 0000000..5e4d540 --- /dev/null +++ b/CalculatorASP/ProxyF/ProxyF.fsproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + From 34f4cfe9beeb267995c814dcf28a416f00dbea52 Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 12 Oct 2020 18:28:23 +0300 Subject: [PATCH 09/19] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20:)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalculatorASP/CalculatorASP.sln | 6 -- .../CalculatorASP/CalculatorASP.csproj | 1 - .../Properties/launchSettings.json | 14 ++-- .../CalculatorASP/ProxyMiddleware.cs | 26 ------- CalculatorASP/CalculatorASP/Startup.cs | 2 - CalculatorASP/CalculatorF/CalculatorF.fsproj | 12 ---- CalculatorASP/CalculatorF/Program.fs | 6 -- .../Calculator.DesignPatterns.Proxy.fsproj | 11 --- .../CalculatorProxy/CalculatorProxy.fsproj | 4 -- CalculatorASP/CalculatorProxy/Library.fs | 70 ------------------- 10 files changed, 7 insertions(+), 145 deletions(-) delete mode 100644 CalculatorASP/CalculatorASP/ProxyMiddleware.cs delete mode 100644 CalculatorASP/CalculatorF/CalculatorF.fsproj delete mode 100644 CalculatorASP/CalculatorF/Program.fs delete mode 100644 CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj delete mode 100644 CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj delete mode 100644 CalculatorASP/CalculatorProxy/Library.fs diff --git a/CalculatorASP/CalculatorASP.sln b/CalculatorASP/CalculatorASP.sln index f72c2cf..a70d4f0 100644 --- a/CalculatorASP/CalculatorASP.sln +++ b/CalculatorASP/CalculatorASP.sln @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculatorASP", "Calculator EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calculator", "..\Calculator\Calculator.csproj", "{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Calculator.DesignPatterns.Proxy", "CalculatorProxy\Calculator.DesignPatterns.Proxy.fsproj", "{3D2B84F5-1415-405F-B5B8-573751D8BDFD}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,10 +21,6 @@ Global {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Debug|Any CPU.Build.0 = Debug|Any CPU {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.ActiveCfg = Release|Any CPU {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.Build.0 = Release|Any CPU - {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3D2B84F5-1415-405F-B5B8-573751D8BDFD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CalculatorASP/CalculatorASP/CalculatorASP.csproj b/CalculatorASP/CalculatorASP/CalculatorASP.csproj index 9455cae..6af6ab2 100644 --- a/CalculatorASP/CalculatorASP/CalculatorASP.csproj +++ b/CalculatorASP/CalculatorASP/CalculatorASP.csproj @@ -6,7 +6,6 @@ - diff --git a/CalculatorASP/CalculatorASP/Properties/launchSettings.json b/CalculatorASP/CalculatorASP/Properties/launchSettings.json index c71679d..8567cc9 100644 --- a/CalculatorASP/CalculatorASP/Properties/launchSettings.json +++ b/CalculatorASP/CalculatorASP/Properties/launchSettings.json @@ -1,10 +1,10 @@ -{ +{ "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, + "windowsAuthentication": false, + "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:51963", - "sslPort": 44300 + "sslPort": 0 } }, "profiles": { @@ -18,10 +18,10 @@ "CalculatorASP": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" } } -} +} \ No newline at end of file diff --git a/CalculatorASP/CalculatorASP/ProxyMiddleware.cs b/CalculatorASP/CalculatorASP/ProxyMiddleware.cs deleted file mode 100644 index 3e2a9f2..0000000 --- a/CalculatorASP/CalculatorASP/ProxyMiddleware.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Calculator.DesignPatterns.Proxy; -using Microsoft.AspNetCore.Http; -using System.Threading.Tasks; - -namespace CalculatorASP -{ - public class ProxyMiddleware - { - private readonly RequestDelegate _next; - - public ProxyMiddleware(RequestDelegate next) - { - _next = next; - } - - public async Task InvokeAsync(HttpContext context) - { - var value = context.Request.Query["value"]; - var check = Proxy.CheckAccess(value); - if (check) - await _next.Invoke(context); - else - context.Response.StatusCode = 403; - } - } -} diff --git a/CalculatorASP/CalculatorASP/Startup.cs b/CalculatorASP/CalculatorASP/Startup.cs index 1fd30bb..5894e2c 100644 --- a/CalculatorASP/CalculatorASP/Startup.cs +++ b/CalculatorASP/CalculatorASP/Startup.cs @@ -19,8 +19,6 @@ public Startup() } public void Configure(IApplicationBuilder app) { - app.UseMiddleware(); - app.UseToken(); app.Run(async (context) => diff --git a/CalculatorASP/CalculatorF/CalculatorF.fsproj b/CalculatorASP/CalculatorF/CalculatorF.fsproj deleted file mode 100644 index 5e4d540..0000000 --- a/CalculatorASP/CalculatorF/CalculatorF.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - netcoreapp3.1 - - - - - - - diff --git a/CalculatorASP/CalculatorF/Program.fs b/CalculatorASP/CalculatorF/Program.fs deleted file mode 100644 index 7968e93..0000000 --- a/CalculatorASP/CalculatorF/Program.fs +++ /dev/null @@ -1,6 +0,0 @@ - - -[] -let main argv = - printfn "Hello World from F#!" - 0 // return an integer exit code diff --git a/CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj b/CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj deleted file mode 100644 index c5da923..0000000 --- a/CalculatorASP/CalculatorProxy/Calculator.DesignPatterns.Proxy.fsproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - netcoreapp3.1 - - - - - - - diff --git a/CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj b/CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj deleted file mode 100644 index 88a5509..0000000 --- a/CalculatorASP/CalculatorProxy/CalculatorProxy.fsproj +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/CalculatorASP/CalculatorProxy/Library.fs b/CalculatorASP/CalculatorProxy/Library.fs deleted file mode 100644 index 69b0b89..0000000 --- a/CalculatorASP/CalculatorProxy/Library.fs +++ /dev/null @@ -1,70 +0,0 @@ -namespace Calculator.DesignPatterns.Proxy - -type MaybeBuilder() = - member this.Bind(x, f) = - match x with - | None -> None - | Some a -> f a - - member this.Return(x) = - Some x - -type AsyncMaybeBuilder () = - member this.Bind(x, f) = - async { - let! x' = x - match x' with - | Some s -> return! f s - | None -> return None - } - - member this.Return(x) = - async{return x} - -open System.Text.RegularExpressions; -open System -open System.Globalization - -module Proxy = - let private pattern = @"-?\d+(?:\.\d+)?)\s*([-+*\/])\s*(-?\d+(?:\.\d+)?" - - let private maybeAsync = new AsyncMaybeBuilder() - - let private maybe = new MaybeBuilder() - - let CheckAccess input = - Regex.IsMatch(input, pattern) - - let private add x y = x + y - - let private subtract x y = x - y - - let private multiply x y = x * y - - let private divide x y = maybe{ - let! x = match y with - | 0.0 -> None - | _ -> Some (x / y) - return x - } - - let calculate op x y = maybe{ - let! x = match op with - | "+" -> Some(add x y) - | "-" -> Some(subtract x y) - | "*" -> Some(multiply x y) - | "/" -> divide x y - | _ -> None - return x - } - let private styles = NumberStyles.AllowDecimalPoint - - let private provider = new CultureInfo("en-US") - - let getNumber (str:string) = maybe{ - let isNumber, number = Double.TryParse(str, styles, provider) - let! str = match isNumber with - | true -> Some(number) - | false -> None - return str - } \ No newline at end of file From 6b0d5d1a597a88154a1aa9ddad47e1b1a1f36b1c Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 12 Oct 2020 19:56:48 +0300 Subject: [PATCH 10/19] Update Program.fs --- CalculatorASP/ProxyF/Program.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index 40e75ad..668c7bc 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -16,14 +16,14 @@ let maybe = new MaybeBuilder() type AsyncMaybeBuilder () = member this.Bind(x, f) = async { - let! _x = x - match _x with + let! x' = x + match x' with | Some s -> return! f s | None -> return None } - member this.Return(x:'a option) = - async{return x} + member this.Return x' = + async{return x'} let asyncMaybe = new AsyncMaybeBuilder() From cee1a92fcac07cb3f4c605efbe8b17955cf7115c Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 12 Oct 2020 19:57:17 +0300 Subject: [PATCH 11/19] Update Program.fs --- CalculatorASP/ProxyF/Program.fs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index 668c7bc..67f6979 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -29,11 +29,6 @@ let asyncMaybe = new AsyncMaybeBuilder() let checkAccess input = Regex.IsMatch(input, @"(-?\d+(?:\.\d+)?)\s*([-%2B*%2F])\s*(-?\d+(?:\.\d+)?)") - -let output (result : float option) = - match result with - | None -> Console.WriteLine("Mission Failed we'll get em next time") - | Some result -> Console.WriteLine(result) let getOper op = match op with From e609fa160b0d4e8e2db6e8b4aad8dcd50e9a3e11 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 14 Oct 2020 22:48:25 +0300 Subject: [PATCH 12/19] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B2?= =?UTF-8?q?=D1=81=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalculatorASP/CalculatorASP.sln | 6 +++ .../CalculatorASP/CalculatorMiddleware.cs | 3 +- CalculatorASP/ProxyF/Program.fs | 51 +++++++++---------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/CalculatorASP/CalculatorASP.sln b/CalculatorASP/CalculatorASP.sln index a70d4f0..3ac663d 100644 --- a/CalculatorASP/CalculatorASP.sln +++ b/CalculatorASP/CalculatorASP.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculatorASP", "Calculator EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calculator", "..\Calculator\Calculator.csproj", "{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}" EndProject +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "ProxyF", "ProxyF\ProxyF.fsproj", "{69E6A6AB-C19A-4243-8C9C-9B9EAB919092}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Debug|Any CPU.Build.0 = Debug|Any CPU {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.ActiveCfg = Release|Any CPU {84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.Build.0 = Release|Any CPU + {69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Debug|Any CPU.Build.0 = Debug|Any CPU + {69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Release|Any CPU.ActiveCfg = Release|Any CPU + {69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs index d9dc1b6..536dd42 100644 --- a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs +++ b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs @@ -17,8 +17,9 @@ public async Task InvokeAsync(HttpContext context) var calclulator = new Calculator.Calculator(); Result.Value = calclulator.Calc(value); await _next.Invoke(context); - } + } } + public static class TokenExtensions { public static IApplicationBuilder UseToken(this IApplicationBuilder builder) diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index 67f6979..0ce2cac 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -1,17 +1,7 @@ open System -open System.Net open System.Text.RegularExpressions; -open System.IO +open System.Net.Http -type MaybeBuilder() = - member this.Bind(x,f)= - match x with - |None -> None - |Some a -> f a - member this.Return(x)= - Some x - -let maybe = new MaybeBuilder() type AsyncMaybeBuilder () = member this.Bind(x, f) = @@ -22,15 +12,15 @@ type AsyncMaybeBuilder () = | None -> return None } - member this.Return x' = - async{return x'} + member this.Return x = + async{return x} let asyncMaybe = new AsyncMaybeBuilder() let checkAccess input = - Regex.IsMatch(input, @"(-?\d+(?:\.\d+)?)\s*([-%2B*%2F])\s*(-?\d+(?:\.\d+)?)") + Regex.IsMatch(input, @"(-?\d+(?:\.\d+)?)\s*([-]?[*]?[%2B]?[%2F]?)\s(-?\d+(?:\.\d+)?)") -let getOper op = +let GetOper op = match op with | "+" -> "%2B" | "*" -> "*" @@ -38,27 +28,32 @@ let getOper op = | "-" -> "-" | _ -> "" -let giveRequest expression = async{ +let StatusCode (responce: HttpResponseMessage) = + asyncMaybe{ + return + match responce.IsSuccessStatusCode with + |true ->Some (responce.Con) + |false -> None + } + +let GiveRequest expression = async{ - let request = WebRequest.Create("http://localhost:51963?value=" + expression, Method = "GET", ContentType = "text/plain") - let response = request.GetResponse() - let stream = response.GetResponseStream(); - let readStream = new StreamReader(stream) - return readStream.ReadToEnd() + let client = new HttpClient() + let! response = client.GetAsync("http://localhost:51963?value=" + expression) |> Async.AwaitTask + let! statusCode = StatusCode response + return Some(statusCode) } let RunCalculator expression = - let result = Async.RunSynchronously(giveRequest expression) - result + let result = Async.RunSynchronously(GiveRequest expression) + Console.WriteLine(result.Value) [] let main argv = let a = Console.ReadLine() - let op = getOper(Console.ReadLine()) + let op = GetOper(Console.ReadLine()) let b = Console.ReadLine() let expression = a + " " + op + " " + b; - let result = match checkAccess expression with - | true -> RunCalculator expression - | false -> "Bad request" - printf"%s"result + if checkAccess expression then RunCalculator expression + else Console.WriteLine("Bad Request"); 0 \ No newline at end of file From b5a0924c484081f587b612270a1c06b4f338bc48 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 14 Oct 2020 23:08:59 +0300 Subject: [PATCH 13/19] Update Program.fs --- CalculatorASP/ProxyF/Program.fs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index 0ce2cac..d6d8028 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -28,32 +28,29 @@ let GetOper op = | "-" -> "-" | _ -> "" -let StatusCode (responce: HttpResponseMessage) = - asyncMaybe{ - return - match responce.IsSuccessStatusCode with - |true ->Some (responce.Con) - |false -> None - } - let GiveRequest expression = async{ let client = new HttpClient() let! response = client.GetAsync("http://localhost:51963?value=" + expression) |> Async.AwaitTask - let! statusCode = StatusCode response - return Some(statusCode) + let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask + let result = match response.IsSuccessStatusCode with + |true -> Some(content) + |false -> None + return result } -let RunCalculator expression = - let result = Async.RunSynchronously(GiveRequest expression) - Console.WriteLine(result.Value) - +let output (result : string option) = + match result with + | None -> Console.WriteLine("Bad Request") + | Some result -> Console.WriteLine(result) [] let main argv = let a = Console.ReadLine() let op = GetOper(Console.ReadLine()) let b = Console.ReadLine() let expression = a + " " + op + " " + b; - if checkAccess expression then RunCalculator expression - else Console.WriteLine("Bad Request"); - 0 \ No newline at end of file + let result = match checkAccess expression with + | true -> Async.RunSynchronously(GiveRequest expression) |> Some + | false -> None + output result.Value + 0 \ No newline at end of file From 1766153522086ced438e9918f83e1f839e7429f5 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 14 Oct 2020 23:27:08 +0300 Subject: [PATCH 14/19] Update Program.fs --- CalculatorASP/ProxyF/Program.fs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index d6d8028..dec10d5 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -33,7 +33,8 @@ let GiveRequest expression = async{ let client = new HttpClient() let! response = client.GetAsync("http://localhost:51963?value=" + expression) |> Async.AwaitTask let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask - let result = match response.IsSuccessStatusCode with + let result = + match response.IsSuccessStatusCode with |true -> Some(content) |false -> None return result @@ -49,7 +50,8 @@ let main argv = let op = GetOper(Console.ReadLine()) let b = Console.ReadLine() let expression = a + " " + op + " " + b; - let result = match checkAccess expression with + let result = + match checkAccess expression with | true -> Async.RunSynchronously(GiveRequest expression) |> Some | false -> None output result.Value From 33a3e3caab4b3e6186f965a0b3e6a59909b21c99 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 14 Oct 2020 23:31:42 +0300 Subject: [PATCH 15/19] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BB?= =?UTF-8?q?=D0=B8=D1=88=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalculatorASP/CalculatorASP/CalculatorMiddleware.cs | 1 - CalculatorASP/CalculatorASP/Startup.cs | 2 +- CalculatorASP/CalculatorASP/result.cs | 12 ------------ 3 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 CalculatorASP/CalculatorASP/result.cs diff --git a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs index 536dd42..1fca128 100644 --- a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs +++ b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs @@ -15,7 +15,6 @@ public async Task InvokeAsync(HttpContext context) { var value = context.Request.Query["value"]; var calclulator = new Calculator.Calculator(); - Result.Value = calclulator.Calc(value); await _next.Invoke(context); } } diff --git a/CalculatorASP/CalculatorASP/Startup.cs b/CalculatorASP/CalculatorASP/Startup.cs index 5894e2c..20c97fb 100644 --- a/CalculatorASP/CalculatorASP/Startup.cs +++ b/CalculatorASP/CalculatorASP/Startup.cs @@ -23,7 +23,7 @@ public void Configure(IApplicationBuilder app) app.Run(async (context) => { - await context.Response.WriteAsync($"{Result.Value}"); + await context.Response.WriteAsync("HelloWorld"); }); } } diff --git a/CalculatorASP/CalculatorASP/result.cs b/CalculatorASP/CalculatorASP/result.cs deleted file mode 100644 index cd9e1fd..0000000 --- a/CalculatorASP/CalculatorASP/result.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace CalculatorASP -{ - public static class Result - { - public static decimal Value { get; set; } - } -} From 0c36c552c0b5c6c775f8a283fd10c53e2697dcda Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 14 Oct 2020 23:33:15 +0300 Subject: [PATCH 16/19] Update Program.fs --- CalculatorASP/ProxyF/Program.fs | 1 - 1 file changed, 1 deletion(-) diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index dec10d5..e082c32 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -2,7 +2,6 @@ open System.Text.RegularExpressions; open System.Net.Http - type AsyncMaybeBuilder () = member this.Bind(x, f) = async { From 029149e88f45647bb7d0872fd3006f0aca93bf5d Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 26 Oct 2020 17:48:31 +0300 Subject: [PATCH 17/19] =?UTF-8?q?=D0=A7=D0=B5=D1=82=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D1=82=D0=B8=D0=BB=20=D0=BA=D0=BE=D1=81?= =?UTF-8?q?=D1=8F=D0=BA=D0=B0=20=D1=81=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Calculator.cs | 2 +- CalculatorASP/CalculatorASP/CalculatorMiddleware.cs | 5 ++--- CalculatorASP/CalculatorASP/Startup.cs | 5 ----- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Calculator/Calculator.cs b/Calculator/Calculator.cs index cef31ec..1988235 100644 --- a/Calculator/Calculator.cs +++ b/Calculator/Calculator.cs @@ -18,7 +18,7 @@ public decimal Calc(decimal arg1, decimal arg2, string oper) { return _calculatorOperations[oper](arg1, arg2); } - public decimal GetNumber(string s) + private static decimal GetNumber(string s) { const NumberStyles styles = NumberStyles.AllowDecimalPoint; var provider = new CultureInfo("en-US"); diff --git a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs index 1fca128..ea24c1d 100644 --- a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs +++ b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs @@ -7,15 +7,14 @@ namespace CalculatorASP { public class CalculatorMiddleware { - private readonly RequestDelegate _next; - public CalculatorMiddleware(RequestDelegate next) => _next = next; public async Task InvokeAsync(HttpContext context) { var value = context.Request.Query["value"]; var calclulator = new Calculator.Calculator(); - await _next.Invoke(context); + var ans = calclulator.Calc(value); + await context.Response.WriteAsync(ans.ToString()); } } diff --git a/CalculatorASP/CalculatorASP/Startup.cs b/CalculatorASP/CalculatorASP/Startup.cs index 20c97fb..8bdd88f 100644 --- a/CalculatorASP/CalculatorASP/Startup.cs +++ b/CalculatorASP/CalculatorASP/Startup.cs @@ -20,11 +20,6 @@ public Startup() public void Configure(IApplicationBuilder app) { app.UseToken(); - - app.Run(async (context) => - { - await context.Response.WriteAsync("HelloWorld"); - }); } } } From 9799a4578664517881f62f1f5d0052b1d546a9d0 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 28 Oct 2020 20:40:51 +0300 Subject: [PATCH 18/19] Update CalculatorMiddleware.cs --- CalculatorASP/CalculatorASP/CalculatorMiddleware.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs index ea24c1d..2a22d4c 100644 --- a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs +++ b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs @@ -7,6 +7,8 @@ namespace CalculatorASP { public class CalculatorMiddleware { + private readonly RequestDelegate _next; + public CalculatorMiddleware(RequestDelegate next) => _next = next; public async Task InvokeAsync(HttpContext context) From e3e5a339e3871dd45a426655426f5595c63486e1 Mon Sep 17 00:00:00 2001 From: Danil Date: Sun, 1 Nov 2020 22:19:34 +0300 Subject: [PATCH 19/19] =?UTF-8?q?middleware=20=D1=82=D0=B5=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=8C=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalculatorASP/CalculatorASP/CalculatorMiddleware.cs | 9 +-------- CalculatorASP/CalculatorASP/Startup.cs | 2 +- CalculatorASP/ProxyF/Program.fs | 1 + 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs index 2a22d4c..9979d05 100644 --- a/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs +++ b/CalculatorASP/CalculatorASP/CalculatorMiddleware.cs @@ -17,14 +17,7 @@ public async Task InvokeAsync(HttpContext context) var calclulator = new Calculator.Calculator(); var ans = calclulator.Calc(value); await context.Response.WriteAsync(ans.ToString()); + await _next.Invoke(context); } } - - public static class TokenExtensions - { - public static IApplicationBuilder UseToken(this IApplicationBuilder builder) - { - return builder.UseMiddleware(); - } - } } diff --git a/CalculatorASP/CalculatorASP/Startup.cs b/CalculatorASP/CalculatorASP/Startup.cs index 8bdd88f..811aa1e 100644 --- a/CalculatorASP/CalculatorASP/Startup.cs +++ b/CalculatorASP/CalculatorASP/Startup.cs @@ -19,7 +19,7 @@ public Startup() } public void Configure(IApplicationBuilder app) { - app.UseToken(); + app.UseMiddleware(); } } } diff --git a/CalculatorASP/ProxyF/Program.fs b/CalculatorASP/ProxyF/Program.fs index e082c32..9e84071 100644 --- a/CalculatorASP/ProxyF/Program.fs +++ b/CalculatorASP/ProxyF/Program.fs @@ -43,6 +43,7 @@ let output (result : string option) = match result with | None -> Console.WriteLine("Bad Request") | Some result -> Console.WriteLine(result) + [] let main argv = let a = Console.ReadLine()