Skip to content

Commit

Permalink
fix details
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Feb 28, 2025
1 parent d17b139 commit 5368b18
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
55 changes: 16 additions & 39 deletions mojo/stdlib/src/builtin/range.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -686,67 +686,44 @@ fn iter[

@always_inline
fn iter[
K: KeyElement, V: CollectionElement
](ref [_]value: Dict[K, V]) -> _DictKeyIter[K, V, __origin_of(value)]:
"""Get an iterator of the input dict.
T: CollectionElement
](value: Span[T]) -> _SpanIter[
T,
origin,
address_space=address_space,
alignment=alignment,
]:
"""Return an iterator.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
T: The type that the iterator yields.
Args:
value: The dict to get the iterator of.
value: The iterable value.
Returns:
The iterator of the dict keys.
The type's Iterator.
"""
return value.__iter__()


@always_inline
fn iter[
K: KeyElement, V: CollectionElement
](
ref [_]value: _DictValueIter[K, V, *_],
out output: _DictValueIter[K, V, __type_of(value).dict_origin],
):
"""Get an iterator of the input dict values.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
Args:
value: The dict values to get the iterator of.
Returns:
The iterator of the dict values.
"""
output = rebind[__type_of(output)](value.__iter__())


@always_inline
fn iter[
K: KeyElement,
V: CollectionElement,
](
ref [_]value: _DictEntryIter[K, V, *_],
out output: _DictEntryIter[K, V, __type_of(value).dict_origin],
):
"""Get an iterator of the input dict items.
](ref [_]value: Dict[K, V]) -> _DictKeyIter[K, V, __origin_of(value)]:
"""Get an iterator of the input dict.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
Args:
value: The dict items to get the iterator of.
value: The dict to get the iterator of.
Returns:
The iterator of the dict items.
The iterator of the dict keys.
"""
var src = value.src
output = __type_of(output)(src[]._reserved() - 1, 0, src)
return value.__iter__()


@always_inline
Expand Down
24 changes: 23 additions & 1 deletion mojo/stdlib/src/builtin/reversed.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ trait ReversibleRange:
fn reversed[T: ReversibleRange](value: T) -> _StridedRange:
"""Get a reversed iterator of the input range.
**Note**: iterators are currently non-raising.
Parameters:
T: The type conforming to ReversibleRange.
Expand All @@ -87,6 +89,8 @@ fn reversed[
]:
"""Get a reversed iterator of the input list.
**Note**: iterators are currently non-raising.
Parameters:
T: The type of the elements in the list.
Expand Down Expand Up @@ -126,6 +130,8 @@ fn reversed[
](ref value: Dict[K, V],) -> _DictKeyIter[K, V, __origin_of(value), False]:
"""Get a reversed iterator of the input dict.
**Note**: iterators are currently non-raising.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
Expand All @@ -150,6 +156,8 @@ fn reversed[
]:
"""Get a reversed iterator of the input dict values.
**Note**: iterators are currently non-raising.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
Expand All @@ -176,6 +184,8 @@ fn reversed[
]:
"""Get a reversed iterator of the input dict items.
**Note**: iterators are currently non-raising.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
Expand All @@ -197,7 +207,13 @@ fn reversed[
@always_inline
fn reversed[
T: CollectionElement
](value: Span[T]) -> _SpanIter[T, value.origin, forward=False]:
](value: Span[T]) -> _SpanIter[
T,
origin,
forward=False,
address_space=address_space,
alignment=alignment,
]:
"""Get a reversed iterator of the input Span.
**Note**: iterators are currently non-raising.
Expand All @@ -221,6 +237,8 @@ alias _S = CodepointSliceIter[_, forward=False]
fn reversed(ref value: String) -> _S[__origin_of(value)]:
"""Return a reversed iterator.
**Note**: iterators are currently non-raising.
Args:
value: The iterable value.
Expand All @@ -234,6 +252,8 @@ fn reversed(ref value: String) -> _S[__origin_of(value)]:
fn reversed(value: StringLiteral) -> _S[StaticConstantOrigin]:
"""Return a reversed iterator.
**Note**: iterators are currently non-raising.
Args:
value: The iterable value.
Expand All @@ -247,6 +267,8 @@ fn reversed(value: StringLiteral) -> _S[StaticConstantOrigin]:
fn reversed(value: StringSlice) -> _S[__type_of(value).origin]:
"""Return a reversed iterator.
**Note**: iterators are currently non-raising.
Args:
value: The iterable value.
Expand Down
2 changes: 1 addition & 1 deletion mojo/stdlib/test/builtin/test_string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def test_iter():

var idx = -1
vs = "mojo🔥"
var iterator = vs.__iter__()
var iterator = iter(vs)
assert_equal(5, len(iterator))
var item = next(iterator)
assert_equal(String("m"), String(item))
Expand Down

0 comments on commit 5368b18

Please sign in to comment.