Skip to content

Commit

Permalink
Optimize: return type of iter trait
Browse files Browse the repository at this point in the history
  • Loading branch information
illuxiza committed Dec 12, 2024
1 parent 3d3c0c7 commit 01f7019
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rustable",
"version": "0.1.2",
"version": "0.1.3",
"workspaces": [
"packages/*"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/coll/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustable/coll",
"version": "0.1.2",
"version": "0.1.3",
"description": "Rust-inspired collection types for TypeScript: HashMap, HashSet, and Vec with type safety and efficient implementations",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion packages/enum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustable/enum",
"version": "0.1.2",
"version": "0.1.3",
"description": "Rust-inspired pattern matching and type-safe error handling for TypeScript. Includes Option<T> for null-safety and Result<T, E> for error handling, with comprehensive pattern matching support",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion packages/iter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustable/iter",
"version": "0.1.2",
"version": "0.1.3",
"description": "Rust-style iterator adapters for TypeScript, providing a rich set of functional operations for iterating over collections",
"keywords": [
"iterator",
Expand Down
2 changes: 2 additions & 0 deletions packages/iter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function iter<T>(items: Iterable<T>): IterImpl<T> {
return IterImpl.from(items);
}

export type RustIter<T> = IterImpl<T>;

/**
* Creates a new range iterator
* @param start Starting value (inclusive)
Expand Down
2 changes: 1 addition & 1 deletion packages/trait-impls/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustable/trait-impls",
"version": "0.1.2",
"version": "0.1.3",
"description": "A TypeScript implementation of Rust-like traits system, providing Clone, Eq, and From traits for type-safe operations. Zero dependencies, fully type-safe.",
"keywords": [
"typescript",
Expand Down
50 changes: 47 additions & 3 deletions packages/trait-impls/src/iter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
import { HashMap, HashSet, Vec } from '@rustable/coll';
import { iter } from '@rustable/iter';
import { iter, RustIter } from '@rustable/iter';
import { implTrait, trait } from '@rustable/trait';

@trait
export class Iter<T> implements Iterable<T> {
[Symbol.iterator](): IterableIterator<T> {
throw new Error('Method not implemented.');
}
iter() {
iter(): RustIter<T> {
/**
* Creates a new iterator from an iterable
* @param items Source iterable to create iterator from
* @returns A new iterator with extended functionality
*
* @example
* ```ts
* // Basic iteration
* iter([1, 2, 3])
* .map(x => x * 2)
* .filter(x => x > 4)
* .collect() // [6]
*
* // String iteration
* iter('hello')
* .enumerate()
* .collect() // [[0, 'h'], [1, 'e'], [2, 'l'], [3, 'l'], [4, 'o']]
* ```
*/
return iter(this);
}
enumerate() {
/**
* Creates an iterator that yields pairs of index and value
* The index starts at 0 and increments by 1 for each element
* @returns A new iterator yielding [index, value] pairs
*
* @example
* ```ts
* iter(['a', 'b', 'c'])
* .enumerate()
* .collect() // [[0, 'a'], [1, 'b'], [2, 'c']]
*
* // Useful for finding element positions
* iter(['x', 'y', 'z'])
* .enumerate()
* .find(([_, val]) => val === 'y') // Some([1, 'y'])
*
* // Convert to Map of index -> value
* iter(['a', 'b', 'c'])
* .enumerate()
* .collectInto(Collector.toMap(
* ([idx, _]) => idx,
* ([_, val]) => val
* )) // Map { 0: 'a', 1: 'b', 2: 'c' }
* ```
*/
enumerate(): RustIter<[number, T]> {
return this.iter().enumerate();
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/trait/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustable/trait",
"version": "0.1.2",
"version": "0.1.3",
"description": "Powerful TypeScript implementation of Rust-like traits. Features type-safe interface definitions, generic traits, multiple implementations, default methods, and runtime trait checking. Perfect for building flexible and maintainable TypeScript applications with Rust-inspired patterns.",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustable/utils",
"version": "0.1.2",
"version": "0.1.3",
"description": "Utility TypeScript utilities inspired by Rust, providing type-safe implementations of HashMap, TypeId, deep cloning, hashing, and equality comparison",
"keywords": [
"typescript",
Expand Down

0 comments on commit 01f7019

Please sign in to comment.