@shd101wyy/yo 0.1.21 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/std/prelude.yo CHANGED
@@ -5380,6 +5380,55 @@ impl(forall(OkType : Type, ErrorType : Type), where(OkType <: Comptime, ErrorTyp
5380
5380
  )
5381
5381
  );
5382
5382
 
5383
+ /// Clone for `Option(T)` — clones the inner value when `Some`.
5384
+ impl(forall(T : Type), where(T <: Clone), Option(T), Clone(
5385
+ clone : ((self) ->
5386
+ match(self.*,
5387
+ .None => Option(T).None,
5388
+ .Some(v) => Option(T).Some((&v).clone())
5389
+ )
5390
+ )
5391
+ ));
5392
+
5393
+ /// Equality for `Option(T)` — `Some(a) == Some(b)` iff `a == b`; `None == None`.
5394
+ impl(forall(T : Type), where(T <: Eq(T)), Option(T), Eq(Option(T))(
5395
+ (==) : ((lhs, rhs) ->
5396
+ match(lhs,
5397
+ .None => match(rhs, .None => true, .Some(_) => false),
5398
+ .Some(a) => match(rhs,
5399
+ .None => false,
5400
+ .Some(b) => (a == b)
5401
+ )
5402
+ )
5403
+ )
5404
+ ));
5405
+
5406
+ /// Clone for `Result(T, E)` — clones the inner value or error.
5407
+ impl(forall(T : Type, E : Type), where(T <: Clone, E <: Clone), Result(T, E), Clone(
5408
+ clone : ((self) ->
5409
+ match(self.*,
5410
+ .Ok(v) => Result(T, E).Ok((&v).clone()),
5411
+ .Err(e) => Result(T, E).Err((&e).clone())
5412
+ )
5413
+ )
5414
+ ));
5415
+
5416
+ /// Equality for `Result(T, E)` — variants must match and inner values must be equal.
5417
+ impl(forall(T : Type, E : Type), where(T <: Eq(T), E <: Eq(E)), Result(T, E), Eq(Result(T, E))(
5418
+ (==) : ((lhs, rhs) ->
5419
+ match(lhs,
5420
+ .Ok(a) => match(rhs,
5421
+ .Ok(b) => (a == b),
5422
+ .Err(_) => false
5423
+ ),
5424
+ .Err(a) => match(rhs,
5425
+ .Ok(_) => false,
5426
+ .Err(b) => (a == b)
5427
+ )
5428
+ )
5429
+ )
5430
+ ));
5431
+
5383
5432
  export
5384
5433
  Result
5385
5434
  ;
@@ -5419,6 +5468,13 @@ impl(forall(T : Type),
5419
5468
  (==) : ((lhs, rhs) -> (lhs.* == rhs.*))
5420
5469
  )
5421
5470
  );
5471
+ impl(forall(T : Type),
5472
+ where(T <: Clone),
5473
+ Box(T),
5474
+ Clone(
5475
+ clone : ((self) -> box((&(self.*.*)).clone()))
5476
+ )
5477
+ );
5422
5478
 
5423
5479
  export
5424
5480
  Box,
@@ -5701,9 +5757,9 @@ try :: (fn(quote(expr_to_try): Expr) -> unquote(Expr)) {
5701
5757
  quote {
5702
5758
  unquote(temp) := unquote(expr_to_try);
5703
5759
  match(unquote(temp),
5704
- .Ok => unquote(temp).value,
5705
- .Err => {
5706
- return unquote(temp).error;
5760
+ .Ok(value) => value,
5761
+ .Err(error) => {
5762
+ return .Err(error);
5707
5763
  }
5708
5764
  )
5709
5765
  }
@@ -5759,6 +5815,351 @@ for :: (fn(
5759
5815
  };
5760
5816
  export for;
5761
5817
 
5818
+ // =============================================================================
5819
+ // Iterator Combinators
5820
+ // =============================================================================
5821
+
5822
+ /// `IterPair` — a two-element product type used by `enumerate` and `zip`.
5823
+ IterPair :: (fn(comptime(A) : Type, comptime(B) : Type) -> comptime(Type))(
5824
+ struct(_0 : A, _1 : B)
5825
+ );
5826
+ export IterPair;
5827
+
5828
+ // ---------------------------------------------------------------------------
5829
+ // IterMap
5830
+ // ---------------------------------------------------------------------------
5831
+
5832
+ /// Lazy iterator that maps each element through a function `F`.
5833
+ ///
5834
+ /// Created by calling `.map(f)` on any iterator.
5835
+ /// `I` is the source iterator type, `B` is the output element type,
5836
+ /// `F` is the mapper closure type satisfying `Fn(item : A) -> B`.
5837
+ IterMap :: (fn(comptime(I) : Type, comptime(B) : Type, comptime(F) : Type) -> comptime(Type))(
5838
+ struct(_inner : I, _f : F)
5839
+ );
5840
+
5841
+ impl(forall(I : Type, A : Type, B : Type, F : Type),
5842
+ where(I <: Iterator(Item := A), F <: (Fn(item : A) -> B)),
5843
+ IterMap(I, B, F),
5844
+ Iterator(
5845
+ Item : B,
5846
+ next : (fn(self : *(Self)) -> Option(B))(
5847
+ match(&(self._inner).next(),
5848
+ .None => .None,
5849
+ .Some(item) => .Some((self._f)(item))
5850
+ )
5851
+ )
5852
+ )
5853
+ );
5854
+
5855
+ export IterMap;
5856
+
5857
+ // ---------------------------------------------------------------------------
5858
+ // IterFilter
5859
+ // ---------------------------------------------------------------------------
5860
+
5861
+ /// Lazy iterator that skips elements for which the predicate returns `false`.
5862
+ ///
5863
+ /// Created by calling `.filter(pred)` on any iterator.
5864
+ /// `I` is the source iterator type, `F` is the predicate closure type
5865
+ /// satisfying `Fn(item : *(A)) -> bool`.
5866
+ IterFilter :: (fn(comptime(I) : Type, comptime(F) : Type) -> comptime(Type))(
5867
+ struct(_inner : I, _f : F)
5868
+ );
5869
+
5870
+ impl(forall(I : Type, A : Type, F : Type),
5871
+ where(I <: Iterator(Item := A), F <: (Fn(item : *(A)) -> bool)),
5872
+ IterFilter(I, F),
5873
+ Iterator(
5874
+ Item : A,
5875
+ next : (fn(self : *(Self)) -> Option(A))(
5876
+ {
5877
+ (loop_result : Option(A)) = .None;
5878
+ while runtime(true), {
5879
+ candidate := &(self._inner).next();
5880
+ match(candidate,
5881
+ .None => { break; },
5882
+ .Some(item) => {
5883
+ cond(
5884
+ (self._f)(&item) => {
5885
+ loop_result = .Some(item);
5886
+ break;
5887
+ },
5888
+ true => ()
5889
+ );
5890
+ }
5891
+ );
5892
+ };
5893
+ loop_result
5894
+ }
5895
+ )
5896
+ )
5897
+ );
5898
+
5899
+ export IterFilter;
5900
+
5901
+ // ---------------------------------------------------------------------------
5902
+ // IterTake
5903
+ // ---------------------------------------------------------------------------
5904
+
5905
+ /// Lazy iterator that yields at most `n` elements from the source.
5906
+ ///
5907
+ /// Created by calling `.take(n)` on any iterator.
5908
+ IterTake :: (fn(comptime(I) : Type) -> comptime(Type))(
5909
+ struct(_inner : I, _remaining : usize)
5910
+ );
5911
+
5912
+ impl(forall(I : Type, A : Type),
5913
+ where(I <: Iterator(Item := A)),
5914
+ IterTake(I),
5915
+ Iterator(
5916
+ Item : A,
5917
+ next : (fn(self : *(Self)) -> Option(A))(
5918
+ cond(
5919
+ (self._remaining == usize(0)) => .None,
5920
+ true => {
5921
+ self._remaining = (self._remaining - usize(1));
5922
+ &(self._inner).next()
5923
+ }
5924
+ )
5925
+ )
5926
+ )
5927
+ );
5928
+
5929
+ export IterTake;
5930
+
5931
+ // ---------------------------------------------------------------------------
5932
+ // IterSkip
5933
+ // ---------------------------------------------------------------------------
5934
+
5935
+ /// Lazy iterator that skips the first `n` elements of the source, then yields
5936
+ /// all remaining elements.
5937
+ ///
5938
+ /// Created by calling `.skip(n)` on any iterator.
5939
+ IterSkip :: (fn(comptime(I) : Type) -> comptime(Type))(
5940
+ struct(_inner : I, _to_skip : usize)
5941
+ );
5942
+
5943
+ impl(forall(I : Type, A : Type),
5944
+ where(I <: Iterator(Item := A)),
5945
+ IterSkip(I),
5946
+ Iterator(
5947
+ Item : A,
5948
+ next : (fn(self : *(Self)) -> Option(A))(
5949
+ {
5950
+ while (self._to_skip > usize(0)), {
5951
+ result := &(self._inner).next();
5952
+ match(result,
5953
+ .None => {
5954
+ self._to_skip = usize(0);
5955
+ return .None;
5956
+ },
5957
+ .Some(_) => { self._to_skip = (self._to_skip - usize(1)); }
5958
+ );
5959
+ };
5960
+ &(self._inner).next()
5961
+ }
5962
+ )
5963
+ )
5964
+ );
5965
+
5966
+ export IterSkip;
5967
+
5968
+ // ---------------------------------------------------------------------------
5969
+ // IterEnumerate
5970
+ // ---------------------------------------------------------------------------
5971
+
5972
+ /// Lazy iterator that pairs each element with its zero-based index.
5973
+ ///
5974
+ /// Created by calling `.enumerate()` on any iterator.
5975
+ /// Yields `IterPair(usize, A)` values where `_0` is the index and `_1` is
5976
+ /// the original element.
5977
+ IterEnumerate :: (fn(comptime(I) : Type) -> comptime(Type))(
5978
+ struct(_inner : I, _index : usize)
5979
+ );
5980
+
5981
+ impl(forall(I : Type, A : Type),
5982
+ where(I <: Iterator(Item := A)),
5983
+ IterEnumerate(I),
5984
+ Iterator(
5985
+ Item : IterPair(usize, A),
5986
+ next : (fn(self : *(Self)) -> Option(IterPair(usize, A)))(
5987
+ match(&(self._inner).next(),
5988
+ .None => .None,
5989
+ .Some(item) => {
5990
+ idx := self._index;
5991
+ self._index = (self._index + usize(1));
5992
+ .Some(IterPair(usize, A)(_0: idx, _1: item))
5993
+ }
5994
+ )
5995
+ )
5996
+ )
5997
+ );
5998
+
5999
+ export IterEnumerate;
6000
+
6001
+ // ---------------------------------------------------------------------------
6002
+ // IterZip
6003
+ // ---------------------------------------------------------------------------
6004
+
6005
+ /// Lazy iterator that combines two iterators element-by-element into pairs.
6006
+ /// Stops as soon as either iterator is exhausted.
6007
+ ///
6008
+ /// Created by calling `.zip(other)` on any iterator.
6009
+ /// Yields `IterPair(A, B)` values.
6010
+ IterZip :: (fn(comptime(I) : Type, comptime(J) : Type) -> comptime(Type))(
6011
+ struct(_left : I, _right : J)
6012
+ );
6013
+
6014
+ impl(forall(I : Type, J : Type, A : Type, B : Type),
6015
+ where(I <: Iterator(Item := A), J <: Iterator(Item := B)),
6016
+ IterZip(I, J),
6017
+ Iterator(
6018
+ Item : IterPair(A, B),
6019
+ next : (fn(self : *(Self)) -> Option(IterPair(A, B)))(
6020
+ match(&(self._left).next(),
6021
+ .None => .None,
6022
+ .Some(a) => match(&(self._right).next(),
6023
+ .None => .None,
6024
+ .Some(b) => .Some(IterPair(A, B)(_0: a, _1: b))
6025
+ )
6026
+ )
6027
+ )
6028
+ )
6029
+ );
6030
+
6031
+ export IterZip;
6032
+
6033
+ // ---------------------------------------------------------------------------
6034
+ // Blanket methods — added to all types implementing Iterator
6035
+ // ---------------------------------------------------------------------------
6036
+
6037
+ impl(forall(I : Type), where(I <: Iterator), I,
6038
+ /// Transform each element with `f`, producing an `IterMap` iterator.
6039
+ map : (fn(forall(A : Type, B : Type, F : Type), self : Self, f : F,
6040
+ where(Self <: Iterator(Item := A), F <: (Fn(item : A) -> B))) -> IterMap(Self, B, F))(
6041
+ IterMap(Self, B, F)(_inner: self, _f: f)
6042
+ ),
6043
+
6044
+ /// Keep only elements for which `pred` returns `true`, producing an `IterFilter` iterator.
6045
+ filter : (fn(forall(A : Type, F : Type), self : Self, f : F,
6046
+ where(Self <: Iterator(Item := A), F <: (Fn(item : *(A)) -> bool))) -> IterFilter(Self, F))(
6047
+ IterFilter(Self, F)(_inner: self, _f: f)
6048
+ ),
6049
+
6050
+ /// Yield at most `n` elements, producing an `IterTake` iterator.
6051
+ take : (fn(forall(A : Type), self : Self, n : usize,
6052
+ where(Self <: Iterator(Item := A))) -> IterTake(Self))(
6053
+ IterTake(Self)(_inner: self, _remaining: n)
6054
+ ),
6055
+
6056
+ /// Skip the first `n` elements, producing an `IterSkip` iterator.
6057
+ skip : (fn(forall(A : Type), self : Self, n : usize,
6058
+ where(Self <: Iterator(Item := A))) -> IterSkip(Self))(
6059
+ IterSkip(Self)(_inner: self, _to_skip: n)
6060
+ ),
6061
+
6062
+ /// Pair each element with its zero-based index, producing an `IterEnumerate` iterator.
6063
+ enumerate : (fn(forall(A : Type), self : Self,
6064
+ where(Self <: Iterator(Item := A))) -> IterEnumerate(Self))(
6065
+ IterEnumerate(Self)(_inner: self, _index: usize(0))
6066
+ ),
6067
+
6068
+ /// Combine with another iterator element-by-element, producing an `IterZip` iterator.
6069
+ zip : (fn(forall(A : Type, J : Type, B : Type), self : Self, other : J,
6070
+ where(Self <: Iterator(Item := A), J <: Iterator(Item := B))) -> IterZip(Self, J))(
6071
+ IterZip(Self, J)(_left: self, _right: other)
6072
+ ),
6073
+
6074
+ /// Reduce the iterator to a single value by repeatedly applying `f`.
6075
+ fold : (fn(forall(A : Type, Acc : Type, F : Type), self : Self, init : Acc, f : F,
6076
+ where(Self <: Iterator(Item := A), F <: (Fn(acc : Acc, item : A) -> Acc))) -> Acc)(
6077
+ {
6078
+ acc := init;
6079
+ iter := self;
6080
+ while runtime(true), {
6081
+ match((&iter).next(),
6082
+ .None => { break; },
6083
+ .Some(item) => { acc = (f)(acc, item); }
6084
+ );
6085
+ };
6086
+ acc
6087
+ }
6088
+ ),
6089
+
6090
+ /// Apply `f` to each element, consuming the iterator.
6091
+ for_each : (fn(forall(A : Type, F : Type), self : Self, f : F,
6092
+ where(Self <: Iterator(Item := A), F <: (Fn(item : A) -> unit))) -> unit)(
6093
+ {
6094
+ iter := self;
6095
+ while runtime(true), {
6096
+ match((&iter).next(),
6097
+ .None => { break; },
6098
+ .Some(item) => { (f)(item); }
6099
+ );
6100
+ };
6101
+ }
6102
+ ),
6103
+
6104
+ /// Count the number of remaining elements.
6105
+ count : (fn(forall(A : Type), self : Self,
6106
+ where(Self <: Iterator(Item := A))) -> usize)(
6107
+ {
6108
+ n := usize(0);
6109
+ iter := self;
6110
+ while runtime(true), {
6111
+ match((&iter).next(),
6112
+ .None => { break; },
6113
+ .Some(_) => { n = (n + usize(1)); }
6114
+ );
6115
+ };
6116
+ n
6117
+ }
6118
+ ),
6119
+
6120
+ /// Return `true` if any element satisfies `pred`. Short-circuits on first match.
6121
+ any : (fn(forall(A : Type, F : Type), self : Self, pred : F,
6122
+ where(Self <: Iterator(Item := A), F <: (Fn(item : A) -> bool))) -> bool)(
6123
+ {
6124
+ result := false;
6125
+ iter := self;
6126
+ while runtime(true), {
6127
+ match((&iter).next(),
6128
+ .None => { break; },
6129
+ .Some(item) => {
6130
+ cond(
6131
+ (pred)(item) => { result = true; break; },
6132
+ true => ()
6133
+ );
6134
+ }
6135
+ );
6136
+ };
6137
+ result
6138
+ }
6139
+ ),
6140
+
6141
+ /// Return `true` if every element satisfies `pred`. Short-circuits on first failure.
6142
+ all : (fn(forall(A : Type, F : Type), self : Self, pred : F,
6143
+ where(Self <: Iterator(Item := A), F <: (Fn(item : A) -> bool))) -> bool)(
6144
+ {
6145
+ result := true;
6146
+ iter := self;
6147
+ while runtime(true), {
6148
+ match((&iter).next(),
6149
+ .None => { break; },
6150
+ .Some(item) => {
6151
+ cond(
6152
+ !(pred(item)) => { result = false; break; },
6153
+ true => ()
6154
+ );
6155
+ }
6156
+ );
6157
+ };
6158
+ result
6159
+ }
6160
+ )
6161
+ );
6162
+
5762
6163
  /// FutureState — the state of an async `Future`.
5763
6164
  FutureState :: enum(
5764
6165
  /// The future has been created but not yet started.