@shd101wyy/yo 0.1.4 → 0.1.5

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shd101wyy/yo",
3
3
  "displayName": "Yo",
4
- "version": "0.1.4",
4
+ "version": "0.1.5",
5
5
  "main": "./out/cjs/index.cjs",
6
6
  "module": "./out/esm/index.mjs",
7
7
  "types": "./out/types/src/index.d.ts",
package/std/prelude.yo CHANGED
@@ -110,13 +110,7 @@ extern "Yo",
110
110
  ;
111
111
  export __yo_as;
112
112
 
113
- assert :: (fn(flag : bool, (comptime(msg) : comptime_string) ?= "Assertion failed.") -> unit)
114
- cond(
115
- flag => (),
116
- true => panic(msg)
117
- )
118
- ;
119
- export assert;
113
+
120
114
 
121
115
  // unsafe module
122
116
  unsafe :: impl {
@@ -3271,6 +3265,14 @@ impl(str, Ord(str)(
3271
3265
 
3272
3266
  export str;
3273
3267
 
3268
+ assert :: (fn(flag : bool, (msg : str) ?= "Assertion failed.") -> unit)
3269
+ cond(
3270
+ flag => (),
3271
+ true => panic(msg)
3272
+ )
3273
+ ;
3274
+ export assert;
3275
+
3274
3276
 
3275
3277
  /// ComptimeList
3276
3278
  impl(forall(T : Type), ComptimeList(T), Comptime());
@@ -3419,6 +3421,85 @@ impl(forall(T : Type), Option(T),
3419
3421
  )
3420
3422
  );
3421
3423
 
3424
+ // Functional combinators for Option(T)
3425
+ impl(forall(T : Type), Option(T),
3426
+ map : (fn(forall(B : Type), self : Self, f : Impl(Fn(a : T) -> B)) -> Option(B))(
3427
+ match(self,
3428
+ .Some(value) => Option(B).Some(f(value)),
3429
+ .None => Option(B).None
3430
+ )
3431
+ ),
3432
+
3433
+ and_then : (fn(forall(B : Type), self : Self, f : Impl(Fn(a : T) -> Option(B))) -> Option(B))(
3434
+ match(self,
3435
+ .Some(value) => f(value),
3436
+ .None => Option(B).None
3437
+ )
3438
+ ),
3439
+
3440
+ filter : (fn(self : Self, predicate : Impl(Fn(a : T) -> bool)) -> Option(T))(
3441
+ match(self,
3442
+ .Some(value) => cond(
3443
+ predicate(value) => Option(T).Some(value),
3444
+ true => Option(T).None
3445
+ ),
3446
+ .None => Option(T).None
3447
+ )
3448
+ ),
3449
+
3450
+ or_else : (fn(self : Self, f : Impl(Fn() -> Option(T))) -> Option(T))(
3451
+ match(self,
3452
+ .Some(value) => Option(T).Some(value),
3453
+ .None => f()
3454
+ )
3455
+ ),
3456
+
3457
+ map_or : (fn(forall(B : Type), self : Self, default_val : B, f : Impl(Fn(a : T) -> B)) -> B)(
3458
+ match(self,
3459
+ .Some(value) => f(value),
3460
+ .None => default_val
3461
+ )
3462
+ ),
3463
+
3464
+ map_or_else : (fn(forall(B : Type), self : Self, default_fn : Impl(Fn() -> B), f : Impl(Fn(a : T) -> B)) -> B)(
3465
+ match(self,
3466
+ .Some(value) => f(value),
3467
+ .None => default_fn()
3468
+ )
3469
+ ),
3470
+
3471
+ and : (fn(forall(B : Type), self : Self, optb : Option(B)) -> Option(B))(
3472
+ match(self,
3473
+ .Some(_) => optb,
3474
+ .None => Option(B).None
3475
+ )
3476
+ ),
3477
+
3478
+ or : (fn(self : Self, optb : Option(T)) -> Option(T))(
3479
+ match(self,
3480
+ .Some(value) => Option(T).Some(value),
3481
+ .None => optb
3482
+ )
3483
+ ),
3484
+
3485
+ unwrap_or_else : (fn(self : Self, f : Impl(Fn() -> T)) -> T)(
3486
+ match(self,
3487
+ .Some(value) => value,
3488
+ .None => f()
3489
+ )
3490
+ )
3491
+ );
3492
+
3493
+ // Flatten for nested Option
3494
+ impl(forall(T : Type), Option(Option(T)),
3495
+ flatten : (fn(self : Self) -> Option(T))(
3496
+ match(self,
3497
+ .Some(inner) => inner,
3498
+ .None => Option(T).None
3499
+ )
3500
+ )
3501
+ );
3502
+
3422
3503
  // Option(T) implements Comptime when T implements Comptime
3423
3504
  impl(forall(T : Type), where(T <: Comptime), Option(T), Comptime());
3424
3505
 
@@ -3503,6 +3584,103 @@ impl(forall(OkType : Type, ErrorType : Type), Result(OkType, ErrorType),
3503
3584
  )
3504
3585
  );
3505
3586
 
3587
+ // Functional combinators for Result(OkType, ErrorType)
3588
+ impl(forall(OkType : Type, ErrorType : Type), Result(OkType, ErrorType),
3589
+ map : (fn(forall(B : Type), self : Self, f : Impl(Fn(a : OkType) -> B)) -> Result(B, ErrorType))(
3590
+ match(self,
3591
+ .Ok(value) => Result(B, ErrorType).Ok(f(value)),
3592
+ .Err(error) => Result(B, ErrorType).Err(error)
3593
+ )
3594
+ ),
3595
+
3596
+ map_err : (fn(forall(F : Type), self : Self, f : Impl(Fn(a : ErrorType) -> F)) -> Result(OkType, F))(
3597
+ match(self,
3598
+ .Ok(value) => Result(OkType, F).Ok(value),
3599
+ .Err(error) => Result(OkType, F).Err(f(error))
3600
+ )
3601
+ ),
3602
+
3603
+ and_then : (fn(forall(B : Type), self : Self, f : Impl(Fn(a : OkType) -> Result(B, ErrorType))) -> Result(B, ErrorType))(
3604
+ match(self,
3605
+ .Ok(value) => f(value),
3606
+ .Err(error) => Result(B, ErrorType).Err(error)
3607
+ )
3608
+ ),
3609
+
3610
+ or_else : (fn(forall(F : Type), self : Self, f : Impl(Fn(a : ErrorType) -> Result(OkType, F))) -> Result(OkType, F))(
3611
+ match(self,
3612
+ .Ok(value) => Result(OkType, F).Ok(value),
3613
+ .Err(error) => f(error)
3614
+ )
3615
+ ),
3616
+
3617
+ and : (fn(forall(B : Type), self : Self, res : Result(B, ErrorType)) -> Result(B, ErrorType))(
3618
+ match(self,
3619
+ .Ok(_) => res,
3620
+ .Err(error) => Result(B, ErrorType).Err(error)
3621
+ )
3622
+ ),
3623
+
3624
+ or : (fn(forall(F : Type), self : Self, res : Result(OkType, F)) -> Result(OkType, F))(
3625
+ match(self,
3626
+ .Ok(value) => Result(OkType, F).Ok(value),
3627
+ .Err(_) => res
3628
+ )
3629
+ ),
3630
+
3631
+ ok : (fn(self : Self) -> Option(OkType))(
3632
+ match(self,
3633
+ .Ok(value) => Option(OkType).Some(value),
3634
+ .Err(_) => Option(OkType).None
3635
+ )
3636
+ ),
3637
+
3638
+ err : (fn(self : Self) -> Option(ErrorType))(
3639
+ match(self,
3640
+ .Ok(_) => Option(ErrorType).None,
3641
+ .Err(error) => Option(ErrorType).Some(error)
3642
+ )
3643
+ ),
3644
+
3645
+ map_or : (fn(forall(B : Type), self : Self, default_val : B, f : Impl(Fn(a : OkType) -> B)) -> B)(
3646
+ match(self,
3647
+ .Ok(value) => f(value),
3648
+ .Err(_) => default_val
3649
+ )
3650
+ ),
3651
+
3652
+ map_or_else : (fn(forall(B : Type), self : Self, default_fn : Impl(Fn(a : ErrorType) -> B), f : Impl(Fn(a : OkType) -> B)) -> B)(
3653
+ match(self,
3654
+ .Ok(value) => f(value),
3655
+ .Err(error) => default_fn(error)
3656
+ )
3657
+ ),
3658
+
3659
+ unwrap_or_else : (fn(self : Self, f : Impl(Fn(a : ErrorType) -> OkType)) -> OkType)(
3660
+ match(self,
3661
+ .Ok(value) => value,
3662
+ .Err(error) => f(error)
3663
+ )
3664
+ )
3665
+ );
3666
+
3667
+ // Option -> Result conversion (defined after Result)
3668
+ impl(forall(T : Type), Option(T),
3669
+ ok_or : (fn(forall(E : Type), self : Self, err : E) -> Result(T, E))(
3670
+ match(self,
3671
+ .Some(value) => Result(T, E).Ok(value),
3672
+ .None => Result(T, E).Err(err)
3673
+ )
3674
+ ),
3675
+
3676
+ ok_or_else : (fn(forall(E : Type), self : Self, err_fn : Impl(Fn() -> E)) -> Result(T, E))(
3677
+ match(self,
3678
+ .Some(value) => Result(T, E).Ok(value),
3679
+ .None => Result(T, E).Err(err_fn())
3680
+ )
3681
+ )
3682
+ );
3683
+
3506
3684
  // Result(OkType, ErrorType) implements Comptime when both types implement Comptime
3507
3685
  impl(forall(OkType : Type, ErrorType : Type), where(OkType <: Comptime, ErrorType <: Comptime), Result(OkType, ErrorType), Comptime());
3508
3686