FunVec1T Class

A unified functional 1-dimensional collection. It does not hold the data, rather provides a unified view over different sources of data providing indexed access to collection elements.

Definition

Namespace: Orx.Fun.FunVec
Assembly: Orx.Fun.FunVec (in Orx.Fun.FunVec.dll) Version: 1.0.0
C#
public class FunVec1<T> : IEnumerable<T>, IEnumerable
Inheritance
Object    FunVec1T
Implements
IEnumerableT, IEnumerable

Type Parameters

T
Type of the innermost elements of the collection.

Constructors

FunVec1T(T) 1-dimensional vector, length and values of which are determined by the underlying array.
C#
var array = new char[] { 'a', 'b', 'c' };
FunVec1<char> vec = new(array);
Assert(vec.Length1 == 3);
Assert(vec[2] == 'c');
Assert(vec.Get(0) == Some('a'));
Assert(vec.Get(3).IsNone);
FunVec1T(ListT) 1-dimensional vector, length and values of which are determined by the underlying list.
C#
var list = new List<char> { 'a', 'b', 'c' };
FunVec1<char> vec = new(list);
Assert(vec.Length1 == 3);
Assert(vec[2] == 'c');
Assert(vec.Get(0) == Some('a'));
Assert(vec.Get(3).IsNone);
FunVec1T(FuncInt32, T, OptInt32) 1-dimensional vector with optional length, values of which are determined by the getValueByIndex function.
C#
static int Factorial(int number) { .. }

FunVec1<int> factorials = new(Factorial);
Assert(factorials.Length1 == int.MaxValue); // since length1 is omitted
Assert(factorials[3] == 6);
Assert(factorials[5] == 120);

FunVec1<int> factorialsUpTo4 = new(Factorial, Some(4));
Assert(factorialsUpTo4.Length1 == 4);
Assert(factorialsUpTo4[3] == 6);
// Assert(factorialsUpTo4[5] == 120); // out-of-bounds, throws!
Assert(factorialsUpTo4.Get(5).IsNone);
FunVec1T(T, OptInt32) 1-dimensional vector with optional length, which always yields the same constant value.
C#
var agentSmith = GetSmith();
FunVec1<Agent> vec = new(agentSmith);
Assert(vec.Length1 == int.MaxValue); // since length1 is omitted
Assert(vec[0] == agentSmith);
Assert(vec[42] == agentSmith);
Assert(vec.Get(100) == Some(agentSmith));

FunVec1<Agent> vec = new(agentSmith, Some(50));
Assert(vec.Length1 == 50);
Assert(vec[0] == agentSmith);
Assert(vec[42] == agentSmith);
Assert(vec.Get(100).IsNone);

Properties

HasUnderlyingScalar The unified collection might be constructed with a constant scalar value; hence, returning the scalar for all indices. If this is the case, HasUnderlyingScalar is true; and the field UnderlyingScalar equals to Some of the underlying scalar value.

Otherwise, HasUnderlyingScalar is false and UnderlyingScalar.IsNone.

C#
// vec[i] = 10, for all i.
UniJaggedD1<int> vec = new(10);
Assert(vec[3] == 10 and vec[42] == 10);
Assert(vec.Get(12) == Some(10));

// underlying constant can be obtained by the optional UnderlyingScalar field.
Assert(vec.HasUnderlyingScalar);
Assert(vec.UnderlyingScalar.IsSome);
Assert(vec.UnderlyingScalar == Some(10));
Assert(vec.UnderlyingScalar.Unwrap() == 10);
Item Directly returns the element at the i-th position. Use Get(Int32) for the bound-checked optional version.
C#
var underlyingArray = new int[] { 10, 11, 12 };
FunVec1<int> vec = new(underlyingArray);

Assert(vec[1] == 11);

// var x = vec[-1]; => out-of-bounds, throws!
// var x = vec[3]; => out-of-bounds, throws!

Methods

Equals
(Inherited from Object)
Finalize
(Inherited from Object)
Get Safely gets the element at the i-th position; returns None if the index is invalid.
C#
var underlyingArray = new int[] { 10, 11, 12 };
FunVec1<int> vec = new(underlyingArray);

Assert(jagvecged.Get(1) == Some(11));
Assert(vec.Get(-1).IsNone);
Assert(vec.Get(2).IsNone);
For other methods on the resulting optional, see Opt.
GetEnumerator Returns the enumerator for values in the vector.
GetHashCode
(Inherited from Object)
GetType
(Inherited from Object)
MemberwiseClone
(Inherited from Object)
ToString
(Inherited from Object)

Operators

(FuncInt32, T to FunVec1T) 1-dimensional vector with optional length, values of which are determined by the getValueByIndex function.
C#
static int Factorial(int number) { .. }

FunVec1<int> factorials = new(Factorial);
Assert(factorials.Length1 == int.MaxValue); // since length1 is omitted
Assert(factorials[3] == 6);
Assert(factorials[5] == 120);

FunVec1<int> factorialsUpTo4 = new(Factorial, Some(4));
Assert(factorialsUpTo4.Length1 == 4);
Assert(factorialsUpTo4[3] == 6);
// Assert(factorialsUpTo4[5] == 120); // out-of-bounds, throws!
Assert(factorialsUpTo4.Get(5).IsNone);
(T to FunVec1T) 1-dimensional vector with optional length, which always yields the same constant value.
C#
var agentSmith = GetSmith();
FunVec1<Agent> vec = new(agentSmith);
Assert(vec.Length1 == int.MaxValue); // since length1 is omitted
Assert(vec[0] == agentSmith);
Assert(vec[42] == agentSmith);
Assert(vec.Get(100) == Some(agentSmith));

FunVec1<Agent> vec = new(agentSmith, Some(50));
Assert(vec.Length1 == 50);
Assert(vec[0] == agentSmith);
Assert(vec[42] == agentSmith);
Assert(vec.Get(100).IsNone);
(T to FunVec1T) 1-dimensional vector, length and values of which are determined by the underlying array.
C#
var array = new char[] { 'a', 'b', 'c' };
FunVec1<char> vec = new(array);
Assert(vec.Length1 == 3);
Assert(vec[2] == 'c');
Assert(vec.Get(0) == Some('a'));
Assert(vec.Get(3).IsNone);
(ListT to FunVec1T) 1-dimensional vector, length and values of which are determined by the underlying list.
C#
var list = new List<char> { 'a', 'b', 'c' };
FunVec1<char> vec = new(list);
Assert(vec.Length1 == 3);
Assert(vec[2] == 'c');
Assert(vec.Get(0) == Some('a'));
Assert(vec.Get(3).IsNone);

Fields

Length1 Length of the vector.
C#
var underlyingList = new List<int> { 10, 11, 12 };
FunVec1<int> vec = new(underlyingList);
Assert(vec.Length1 == 3);

Func<int, bool> underlyingFun = i => i % 2 == 0;
vec = new(underlyingFun, length1: Some(4));
Assert(vec.Length1, 4);

Func<int, bool> underlyingFun = i => i % 2 == 0;
vec = new(underlyingFun); // omitted optional argument 'length1' defaults to None -> no limit
Assert(vec.Length1 == int.MaxValue);
UnderlyingScalar The unified collection might be constructed with a constant scalar value; hence, returning the scalar for all indices. If this is the case, HasUnderlyingScalar is true; and the field UnderlyingScalar equals to Some of the underlying scalar value.

Otherwise, HasUnderlyingScalar is false and UnderlyingScalar.IsNone.

C#
// vec[i] = 10, for all i.
UniJaggedD1<int> vec = new(10);
Assert(vec[3] == 10 and vec[42] == 10);
Assert(vec.Get(12) == Some(10));

// underlying constant can be obtained by the optional UnderlyingScalar field.
Assert(vec.HasUnderlyingScalar);
Assert(vec.UnderlyingScalar.IsSome);
Assert(vec.UnderlyingScalar == Some(10));
Assert(vec.UnderlyingScalar.Unwrap() == 10);

See Also