Product для последовательностей

This commit is contained in:
Mikhalkovich Stanislav 2018-12-10 18:33:48 +03:00
parent 38d94f702d
commit 16970b7b3d
4 changed files with 38 additions and 10 deletions

View file

@ -1,5 +1,5 @@
begin
var a := new List<integer>(arr(1, 2));
var a: object := new List<integer>(arr(1, 2));
match a with
integer(var c): write(1);
boolean(var s): write(2);

3
TestSuite/ArrProduct.pas Normal file
View file

@ -0,0 +1,3 @@
begin
Assert(Arr(1,2,3).Product = 6);
end.

View file

@ -3540,13 +3540,6 @@ namespace PascalABCCompiler.TreeConverter
public override void visit(SyntaxTree.class_definition _class_definition)
{
/*var tt = _class_definition.DescendantNodes().OfType<class_definition>();
if (tt.Count() > 0)
{
AddWarning(new Errors.CompilerWarning("bs", _class_definition.source_context.FileName));
AddError(get_location(tt.First()),"NESTED_RECORD_DEFINITIONS_ARE_FORBIDDEN");
}*/
var testIsTypeclassInstance = context._ctn?.Attributes?.Any(a => a.AttributeType.name == "__TypeclassInstanceAttribute");
if (testIsTypeclassInstance.HasValue && testIsTypeclassInstance.Value)
{

View file

@ -8581,6 +8581,22 @@ begin
end;
end;
/// Возвращает произведение элементов последовательности
function Product(Self: sequence of real): real; extensionmethod;
begin
Result := 1.0;
foreach var x in Self do
Result *= x;
end;
/// Возвращает произведение элементов последовательности
function Product(Self: sequence of integer): integer; extensionmethod;
begin
Result := 1;
foreach var x in Self do
Result *= x;
end;
/// Возвращает отсортированную по возрастанию последовательность
function Sorted<T>(Self: sequence of T): sequence of T; extensionmethod;
begin
@ -9602,6 +9618,22 @@ begin
Self[i, j] := f(i, j);
end;
/// Применяет действие к каждому элементу двумерного массива
procedure &ForEach<T>(Self: array [,] of T; act: T -> ()); extensionmethod;
begin
for var i := 0 to Self.RowCount - 1 do
for var j := 0 to Self.ColCount - 1 do
act(Self[i, j]);
end;
/// Применяет действие к каждому элементу двумерного массива
procedure &ForEach<T>(Self: array [,] of T; act: (T,integer,integer) -> ()); extensionmethod;
begin
for var i := 0 to Self.RowCount - 1 do
for var j := 0 to Self.ColCount - 1 do
act(Self[i, j],i,j);
end;
// -----------------------------------------------------
//>> Фиктивная секция YYY - не удалять! # YYY
// -----------------------------------------------------
@ -9730,7 +9762,7 @@ begin
end;}
/// Возвращает минимальный элемент
function Min<T>(Self: array of T): T; extensionmethod;where T: System.IComparable<T>;
function Min<T>(Self: array of T): T; extensionmethod; where T: System.IComparable<T>;
begin
Result := Self[0];
for var i := 1 to Self.Length - 1 do
@ -9739,7 +9771,7 @@ begin
end;
/// Возвращает максимальный элемент
function Max<T>(Self: array of T): T; extensionmethod;where T: System.IComparable<T>;
function Max<T>(Self: array of T): T; extensionmethod; where T: System.IComparable<T>;
begin
Result := Self[0];
for var i := 1 to Self.Length - 1 do