iterate.carbon 915 B

123456789101112131415161718192021222324252627282930
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. package Core library "prelude/iterate";
  5. export import library "prelude/destroy";
  6. export import library "prelude/types";
  7. export import library "prelude/operators";
  8. interface Iterate {
  9. let ElementType:! type;
  10. let CursorType:! type;
  11. fn NewCursor[self: Self]() -> CursorType;
  12. fn Next[self: Self](cursor: CursorType*) -> Optional(ElementType);
  13. }
  14. impl forall [T:! type, N:! IntLiteral()]
  15. array(T, N) as Iterate
  16. where .ElementType = T and .CursorType = i32 {
  17. fn NewCursor[self: Self]() -> i32 { return 0; }
  18. fn Next[self: Self](cursor: i32*) -> Optional(T) {
  19. if (*cursor < N) {
  20. ++*cursor;
  21. return Optional(T).Some(self[*cursor - 1]);
  22. } else {
  23. return Optional(T).None();
  24. }
  25. }
  26. }