@@ -3788,9 +3788,9 @@ function Seq<T>(params a: array of T): sequence of T;
3788
3788
Result := res;
3789
3789
end ;
3790
3790
3791
- type
3791
+ { type
3792
3792
// Вспомогательный класс для генерации бесконечной последовательности целых, начиная с заданного значения
3793
- { IntNumbersClass = class(SeqBaseInteger,IEnumerable<integer>,IEnumerator<integer>)
3793
+ IntNumbersClass = class(SeqBaseInteger,IEnumerable<integer>,IEnumerator<integer>)
3794
3794
private
3795
3795
first,cur: integer;
3796
3796
public
@@ -3815,7 +3815,7 @@ { IntNumbersClass = class(SeqBaseInteger,IEnumerable<integer>,IEnumerator<integ
3815
3815
end;}
3816
3816
3817
3817
// Вспомогательный класс для генерации рекуррентных последовательностей
3818
- IterateClass<T> = class (SeqBase<T>,IEnumerable<T>,IEnumerator<T>)
3818
+ { IterateClass<T> = class(SeqBase<T>,IEnumerable<T>,IEnumerator<T>)
3819
3819
private
3820
3820
first: T;
3821
3821
cur: T;
@@ -3890,18 +3890,33 @@ Iterate2Class<T> = class(SeqBase<T>,IEnumerable<T>,IEnumerator<T>)
3890
3890
b := second;
3891
3891
isfirst := true;
3892
3892
end;
3893
- end ;
3893
+ end;}
3894
3894
3895
3895
// / Возвращает бесконечную рекуррентную последовательность элементов, задаваемую начальным элементом first и функцией next
3896
3896
function Iterate <T>(first: T; next: T->T): sequence of T;
3897
3897
begin
3898
- Result := IterateClass&<T>.Create(first,next).Select(x->T(x));
3898
+ yield first;
3899
+ while True do
3900
+ begin
3901
+ first := next(first);
3902
+ yield first;
3903
+ end ;
3904
+ // Result := IterateClass&<T>.Create(first,next).Select(x->T(x));
3899
3905
end ;
3900
3906
3901
3907
// / Возвращает бесконечную рекуррентную последовательность элементов, задаваемую начальными элементами first, second и функцией next
3902
3908
function Iterate <T>(first,second: T; next: (T,T)->T): sequence of T;
3903
3909
begin
3904
- Result := Iterate2Class&<T>.Create(first,second,next).Select(x->T(x));
3910
+ yield first;
3911
+ yield second;
3912
+ while True do
3913
+ begin
3914
+ var nxt := next(first,second);
3915
+ yield nxt;
3916
+ first := second;
3917
+ second := nxt;
3918
+ end ;
3919
+ // Result := Iterate2Class&<T>.Create(first,second,next).Select(x->T(x));
3905
3920
end ;
3906
3921
3907
3922
function SeqGen <T>(count: integer; first: T; next: T -> T): sequence of T;
@@ -7911,60 +7926,49 @@ function Iterate<T>(Self,second: T; next: (T,T) -> T): sequence of T; extensionm
7911
7926
// / Возвращает бесконечную последовательность целых от текущего значения с шагом 1
7912
7927
function Step (Self: integer): sequence of integer; extensionmethod;
7913
7928
begin
7914
- // Result := IntNumbersClass.Create(Self);
7915
- var s := Self;
7916
7929
while True do
7917
7930
begin
7918
- yield s ;
7919
- s += 1 ;
7931
+ yield Self ;
7932
+ Self += 1 ;
7920
7933
end ;
7921
7934
end ;
7922
7935
7923
7936
// / Возвращает бесконечную последовательность целых от текущего значения с шагом step
7924
7937
function Step (Self: integer; step: integer): sequence of integer; extensionmethod;
7925
7938
begin
7926
- { var slf := Self;
7927
- Result := IntNumbersClass.Create().Select(x->x*step+slf);}
7928
- var s := Self;
7929
7939
while True do
7930
7940
begin
7931
- yield s ;
7932
- s += step;
7941
+ yield Self ;
7942
+ Self += step;
7933
7943
end ;
7934
7944
end ;
7935
7945
7936
7946
// / Возвращает бесконечную последовательность вещественных от текущего значения с шагом step
7937
7947
function Step (Self: real; step: real): sequence of real; extensionmethod;
7938
7948
begin
7939
- { var slf := Self;
7940
- Result := IntNumbersClass.Create().Select(x->x*step+slf);}
7941
- var s := Self;
7942
7949
while True do
7943
7950
begin
7944
- yield s ;
7945
- s += step;
7951
+ yield Self ;
7952
+ Self += step;
7946
7953
end ;
7947
7954
end ;
7948
7955
7949
7956
// Возвращает бесконечную последовательность элементов, совпадающих с данным
7950
7957
// /--
7951
7958
function &Repeat <T>(Self: T): sequence of T; extensionmethod;
7952
7959
begin
7953
- var s := Self;
7954
7960
while True do
7955
- yield s ;
7961
+ yield Self ;
7956
7962
end ;
7957
7963
7958
7964
// / Повторяет последовательность бесконечное число раз
7959
7965
function Cycle <T>(Self: sequence of T): sequence of T; extensionmethod;
7960
7966
begin
7961
- var s := Self;
7962
7967
while True do
7963
7968
begin
7964
- foreach var x in s do
7969
+ foreach var x in Self do
7965
7970
yield x;
7966
7971
end ;
7967
- // Result := SeqFill(MaxInt,Self).SelectMany(x->x);
7968
7972
end ;
7969
7973
7970
7974
// ------------------------------------------------------------------------------
@@ -7986,7 +7990,10 @@ function Print<T>(Self: sequence of T; delim: string): sequence of T; extensionm
7986
7990
// / Выводит последовательность на экран, используя пробел в качестве разделителя
7987
7991
function Print <T>(Self: sequence of T): sequence of T; extensionmethod;
7988
7992
begin
7989
- Result := Self.Print(PrintDelimDefault);
7993
+ if typeof(T)=typeof(char) then
7994
+ Result := Self.Print(' ' )
7995
+ else
7996
+ Result := Self.Print(PrintDelimDefault);
7990
7997
end ;
7991
7998
7992
7999
// / Выводит последовательность на экран, используя delim в качестве разделителя, и переходит на новую строку
@@ -8000,7 +8007,10 @@ function Println<T>(Self: sequence of T; delim: string): sequence of T; extensio
8000
8007
// / Выводит последовательность на экран, используя пробел качестве разделителя, и переходит на новую строку
8001
8008
function Println <T>(Self: sequence of T): sequence of T; extensionmethod;
8002
8009
begin
8003
- Result := Self.Println(PrintDelimDefault);
8010
+ if typeof(T)=typeof(char) then
8011
+ Result := Self.Println(' ' )
8012
+ else
8013
+ Result := Self.Println(PrintDelimDefault);
8004
8014
end ;
8005
8015
8006
8016
// / Выводит последовательность строк в файл
@@ -8025,7 +8035,9 @@ function JoinIntoString<T>(Self: sequence of T; delim: string): string; extensio
8025
8035
// / Преобразует элементы последовательности в строковое представление, после чего объединяет их в строку, используя пробел в качестве разделителя
8026
8036
function JoinIntoString <T>(Self: sequence of T): string; extensionmethod;
8027
8037
begin
8028
- Result := Self.JoinIntoString(' ' );
8038
+ if typeof(T) = typeof(char) then
8039
+ Result := Self.JoinIntoString(' ' )
8040
+ else Result := Self.JoinIntoString(' ' );
8029
8041
end ;
8030
8042
8031
8043
// / Применяет действие к каждому элементу последовательности
@@ -8144,15 +8156,25 @@ function Cartesian<T,T1>(Self: sequence of T; b: sequence of T1): sequence of (T
8144
8156
begin
8145
8157
if b=nil then
8146
8158
raise new System.ArgumentNullException(' b' );
8147
- Result := Self.Select(x->b.Select(y->(x,y))).SelectMany(x->x);
8159
+
8160
+ foreach var x in Self do
8161
+ foreach var y in b do
8162
+ yield (x,y)
8163
+
8164
+ // Result := Self.Select(x->b.Select(y->(x,y))).SelectMany(x->x);
8148
8165
end ;
8149
8166
8150
8167
// / Декартово произведение последовательностей
8151
8168
function Cartesian <T,T1,T2>(Self: sequence of T; b: sequence of T1; func: (T,T1)->T2): sequence of T2; extensionmethod;
8152
8169
begin
8153
8170
if b=nil then
8154
8171
raise new System.ArgumentNullException(' b' );
8155
- Result := Self.Select(x->b.Select(y->(x,y))).SelectMany(x->x).Select(x->func(x[0 ],x[1 ]));
8172
+
8173
+ foreach var x in Self do
8174
+ foreach var y in b do
8175
+ yield func(x,y)
8176
+
8177
+ // Result := Self.Select(x->b.Select(y->(x,y))).SelectMany(x->x).Select(x->func(x[0],x[1]));
8156
8178
end ;
8157
8179
8158
8180
// / Разбивает последовательности на две в позиции ind
@@ -8290,11 +8312,20 @@ function Pairwise<T>(Self: sequence of T): sequence of (T,T); extensionmethod;
8290
8312
end
8291
8313
end ;
8292
8314
8293
-
8294
8315
// / Превращает последовательность в последовательность пар соседних элементов, применяет func к каждой паре полученных элементов и получает новую последовательность
8295
8316
function Pairwise <T,Res>(Self: sequence of T; func:(T,T)->Res): sequence of Res; extensionmethod;
8296
8317
begin
8297
- Result := Self.ZipTuple(Self.Skip(1 )).Select(x->func(x[0 ],x[1 ]));
8318
+ var previous: T;
8319
+ var it := Self.GetEnumerator();
8320
+ if (it.MoveNext()) then
8321
+ previous := it.Current;
8322
+
8323
+ while (it.MoveNext()) do
8324
+ begin
8325
+ yield func(previous,it.Current);
8326
+ previous := it.Current;
8327
+ end
8328
+ // Result := Self.ZipTuple(Self.Skip(1)).Select(x->func(x[0],x[1]));
8298
8329
end ;
8299
8330
8300
8331
// / Разбивает последовательность на серии длины size
0 commit comments