2
function TSomething.Concat<T>(const E: IEnumerable<IEnumerable<T>>): IEnumerable<T>;
begin
  Result := TConcatIterator<T>.Create(E) as IEnumerable<T>;
end;

That doesn't compile, because TConcatIterator<T> can only concat two enumerables, but what I need is an iterator that concats an enumerable of enumerables.

Haskell has the concat function that does this:

concat [[1, 2, 3], [4,5]] => [1, 2, 3, 4, 5]

The Delphi version would look like this:

class function TForm1.Concat<T>(
  const AEnumerable: IEnumerable<IEnumerable<T>>): IEnumerable<T>;
begin
  // ???
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  InnerList1: IList<Integer>;
  InnerList2: IList<Integer>;
  OuterList: IList<IEnumerable<Integer>>;
  Concated: IEnumerable<Integer>;
begin
  InnerList1 := TCollections.CreateList<Integer>([1, 2, 3]);
  InnerList2 := TCollections.CreateList<Integer>([4, 5]);
  OuterList := TCollections.CreateList<IEnumerable<Integer>>([InnerList1, InnerList2]);
  Concated := Concat<Integer>(OuterList);
end;

How can I implement this in spring4d?

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113

2 Answers2

4

Since the Spring4D collections are modelled after those in .Net the operation/method you are looking for is called SelectMany.

In 1.2 we have a static method for that in the TEnumerable type so the code would look like that:

Concated := TEnumerable.SelectMany<IEnumerable<Integer>, Integer>(OuterList,
  function(x: IEnumerable<Integer>): IEnumerable<Integer>
  begin
    Result := x;
  end);

However this is a bit verbose so you can easily write a method to handle the special case of flattening an IEnumerable<IEnumerable<T>> to an IEnumerable<T>:

type
  TEnumerableHelper = class helper for TEnumerable
    class function SelectMany<T>(const source: IEnumerable<IEnumerable<T>>): IEnumerable<T>; overload; static;
  end;

class function TEnumerableHelper.SelectMany<T>(
  const source: IEnumerable<IEnumerable<T>>): IEnumerable<T>;
begin
  Result := TSelectManyIterator<IEnumerable<T>, T>.Create(source,
    function(x: IEnumerable<T>): IEnumerable<T>
    begin
      Result := x;
    end);
end;

and use this just easily like this:

Concated := TEnumerable.SelectMany<Integer>(OuterList);

The SelectMany operation is deferred executed and lazy evaluated.

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102
1

A strict (= non lazy) version might look like this:

class function TForm1.Concat<T>(
  const AEnumerable: IEnumerable<IEnumerable<T>>): IEnumerable<T>;
var
  L: IList<T>;
begin
  L := TCollections.CreateList<T>;
  AEnumerable.ForEach(
    procedure(const AInnerEnum: IEnumerable<T>)
    begin
      L.AddRange(AInnerEnum);
    end);
  Result := L as IEnumerable<T>;
end;
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113