Skip to content

Instantly share code, notes, and snippets.

View dimitris-papadimitriou-chr's full-sized avatar
📖
Cloud Design Patterns

Dimitris Papadimitriou dimitris-papadimitriou-chr

📖
Cloud Design Patterns
View GitHub Profile
abstract class Shape
{
public abstract string GetShapeDescription();
}
class Rectangle : Shape
{
public int Width { get; set; }
public int Height { get; set; }
public override string GetShapeDescription()
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));
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));
}
abstract class Shape { }
class Rectangle : Shape
{
public int Width { get; set; }
public int Height { get; set; }
}
class Circle : Shape
{
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) {
var weight = new MonoidWeight();
var parsed = Array.from("(()))").map(parse).reduce(weight.concat,weight.empty);
var parse: (c: string) => Balance = (c: string) => {
switch (c) {
case "(": return Balance.Left
case ")": return Balance.Right
default: return Balance.Empty
}
}
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);
}
}
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;
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);