@@ -4743,10 +4743,26 @@ O.fromPairs = pairs => pairs.reduce((acc, [k, v]) => (acc[k] = v, acc), {});
4743
4743
/* Effect type as a function encoding. Handles the following effects at once:
4744
4744
4745
4745
* exception
4746
+ * non-determinism
4746
4747
* null pointer
4747
- * short circuit
4748
- * non-determinism */
4748
+ * short circuit compuation
4749
+ * defer compuation
4749
4750
4751
+ Usage:
4752
+
4753
+ const k = Eff.Right(2) (Eff.Lazy(() =>
4754
+ Eff.Right(3) (
4755
+ Eff.Right(4) (
4756
+ Eff.Right(5) (
4757
+ Eff.Null)))));
4758
+
4759
+ const k2 = Eff.map(x => x * x) (k)
4760
+
4761
+ // at this point only the first element is evaluated to 4
4762
+
4763
+ // force evaluation
4764
+
4765
+ Eff.cata(); // [4, 9, 16, 25] */
4750
4766
4751
4767
export const Eff = { } ;
4752
4768
@@ -4759,35 +4775,35 @@ export const Eff = {};
4759
4775
Eff . Left = variant ( {
4760
4776
tag : "Eff" ,
4761
4777
cons : "Eff.Left" ,
4762
- keys : [ "left" , "right" , "null" , "short" , "defer " ]
4778
+ keys : [ "left" , "right" , "null" , "short" , "lazy " ]
4763
4779
} ) ;
4764
4780
4765
4781
4766
4782
Eff . Right = variant2 ( {
4767
4783
tag : "Eff" ,
4768
4784
cons : "Eff.Right" ,
4769
- keys : [ "right" , "left" , "null" , "short" , "defer " ]
4785
+ keys : [ "right" , "left" , "null" , "short" , "lazy " ]
4770
4786
} ) ;
4771
4787
4772
4788
4773
4789
Eff . Short = variant ( {
4774
4790
tag : "Eff" ,
4775
4791
cons : "Eff.Short" ,
4776
- keys : [ "short" , "left" , "right" , "null" , "defer " ]
4792
+ keys : [ "short" , "left" , "right" , "null" , "lazy " ]
4777
4793
} ) ;
4778
4794
4779
4795
4780
4796
Eff . Null = variant0 ( {
4781
4797
tag : "Eff" ,
4782
4798
cons : "Eff.Null" ,
4783
- keys : [ "null" , "left" , "right" , "short" , "defer " ]
4799
+ keys : [ "null" , "left" , "right" , "short" , "lazy " ]
4784
4800
} ) ;
4785
4801
4786
4802
4787
- Eff . Defer = variant ( {
4803
+ Eff . Lazy = variant ( {
4788
4804
tag : "Eff" ,
4789
- cons : "Eff.Defer " ,
4790
- keys : [ "defer " , "left" , "right" , "null" , "short" ]
4805
+ cons : "Eff.Lazy " ,
4806
+ keys : [ "lazy " , "left" , "right" , "null" , "short" ]
4791
4807
} ) ;
4792
4808
4793
4809
@@ -4804,7 +4820,17 @@ Eff.map = f => function go(k) { // TODO: make stack-safe
4804
4820
right : head => tail => Eff . Right ( f ( head ) ) ( go ( tail ) ) ,
4805
4821
null : ( ) => Eff . Null ,
4806
4822
short : head => Eff . Short ( f ( head ) ) ,
4807
- defer : thunk => Eff . Defer ( ( ) => go ( thunk ( ) ) )
4823
+
4824
+ get lazy ( ) { // sharing
4825
+ return thunk => {
4826
+ return Eff . Lazy ( ( ) => {
4827
+ const r = thunk ( ) ;
4828
+ delete this . lazy ;
4829
+ this . lazy = ( ) => go ( r ) ;
4830
+ return go ( r ) ;
4831
+ } )
4832
+ }
4833
+ }
4808
4834
} ) ;
4809
4835
} ;
4810
4836
@@ -4820,7 +4846,7 @@ Eff.cata = k => k({ // TODO: make stack-safe
4820
4846
4821
4847
null : ( ) => null ,
4822
4848
short : head => head ,
4823
- defer : thunk => Eff . cata ( thunk ( ) )
4849
+ lazy : thunk => Eff . cata ( thunk ( ) )
4824
4850
} ) ;
4825
4851
4826
4852
0 commit comments