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
| abstract class Shape | |
| { | |
| public abstract string GetShapeDescription(); | |
| } | |
| class Rectangle : Shape | |
| { | |
| public int Width { get; set; } | |
| public int Height { get; set; } | |
| public override string GetShapeDescription() |
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
| string GetShapeDescription(Shape shape) | |
| { | |
| switch (shape) | |
| { | |
| case Circle c: | |
| return $"found a circle with radius {c.Radius}"; | |
| case Rectangle s: | |
| return $"Found {s.Height }x {s.Width} rectangle"; | |
| default: | |
| throw new System.Exception(nameof(shape)); |
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
| string GetShapeDescription1(Shape shape) | |
| { | |
| if (shape is Circle) | |
| return $"found a circle with radius {(shape as Circle).Radius}"; | |
| if (shape is Rectangle) | |
| return $"Found {(shape as Rectangle).Height }x {(shape as Rectangle).Width} rectangle"; | |
| throw new System.Exception(nameof(shape)); | |
| } |
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
| abstract class Shape { } | |
| class Rectangle : Shape | |
| { | |
| public int Width { get; set; } | |
| public int Height { get; set; } | |
| } | |
| class Circle : Shape | |
| { |
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
| class Balance { | |
| static Left = new Balance(0, 1) | |
| static Right = new Balance(1, 0) | |
| static Empty = new Balance(0, 0) | |
| L: number | |
| R: number | |
| constructor(l: number, r: number) { |
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
| var weight = new MonoidWeight(); | |
| var parsed = Array.from("(()))").map(parse).reduce(weight.concat,weight.empty); |
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
| var parse: (c: string) => Balance = (c: string) => { | |
| switch (c) { | |
| case "(": return Balance.Left | |
| case ")": return Balance.Right | |
| default: return Balance.Empty | |
| } | |
| } |
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
| class MonoidWeight implements monoid<Balance> { | |
| empty: Balance = Balance.Empty; | |
| concat(x: Balance, y: Balance): Balance { | |
| if (x.R < y.L) | |
| return new Balance(x.L + y.L - x.R, y.R); | |
| else | |
| return new Balance(x.L, y.R + x.R - y.L); | |
| } | |
| } |
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
| class Balance { | |
| static Left = new Balance(0, 1) | |
| static Right = new Balance(1, 0) | |
| static Empty = new Balance(0, 0) | |
| L: number | |
| R: number | |
| constructor(l: number, r: number) { | |
| this.L = l; |
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
| var sum = fold([2, 3, 4], Sum.empty, Sum.concat); | |
| var product = fold([2, 3, 4], 1, (x, y) => x * y); | |
| var max = fold([2, 3, 4], Number.MIN_SAFE_INTEGER, (x, y) => x > y ? x : y); |