Difference between revisions of "FP Homework 3"
Jump to navigation
Jump to search
(Created page with "= Immutable data types = == Immutable Array == <syntaxhighlight lang="Haskell"> data Point = Point Int Int data Shape = Circle Point Int | Rectangle {topLeft:: Poi...") |
|||
| Line 2: | Line 2: | ||
== Immutable Array == | == Immutable Array == | ||
| − | <syntaxhighlight lang=" | + | <syntaxhighlight lang="csharp"> |
| − | data | + | private abstract class Triplet<A> : ITriplet |
| − | data | + | { |
| − | + | private A[] data = new A[3]; | |
| + | public Triplet(A left, A middle, A right) | ||
| + | { | ||
| + | data[0] = left; | ||
| + | data[1] = middle; | ||
| + | data[2] = right; | ||
| + | } | ||
| + | public A this[int index] | ||
| + | { | ||
| + | get => index switch | ||
| + | { | ||
| + | >= 0 and <= 2 => this.data[index], | ||
| + | _ => throw new IndexOutOfRangeException($"Index {index} out of range for a triplet.") | ||
| + | }; | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 10:07, 31 October 2022
Immutable data types
Immutable Array
private abstract class Triplet<A> : ITriplet
{
private A[] data = new A[3];
public Triplet(A left, A middle, A right)
{
data[0] = left;
data[1] = middle;
data[2] = right;
}
public A this[int index]
{
get => index switch
{
>= 0 and <= 2 => this.data[index],
_ => throw new IndexOutOfRangeException($"Index {index} out of range for a triplet.")
};
}
}