Great functional code of the day

Great functional code of the day

type
  TSyntaxNodeHelper = class helper for TSyntaxNode
    function FlattenHierarchy: IEnumerable;
  end;

function TSyntaxNodeHelper.FlattenHierarchy: IEnumerable;
begin
  Result := TGenerator.Create(
    procedure
    var
      child, node: TSyntaxNode;
    begin
      Yield(Self);
      for child in Self.ChildNodes do
        for node in child.FlattenHierarchy do
          Yield(node);
    end);
end;

Comments