Skip to content

Commit 192bb5b

Browse files
revise effect type
1 parent b5fa5d8 commit 192bb5b

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

scriptum.js

+37-11
Original file line numberDiff line numberDiff line change
@@ -4743,10 +4743,26 @@ O.fromPairs = pairs => pairs.reduce((acc, [k, v]) => (acc[k] = v, acc), {});
47434743
/* Effect type as a function encoding. Handles the following effects at once:
47444744
47454745
* exception
4746+
* non-determinism
47464747
* null pointer
4747-
* short circuit
4748-
* non-determinism */
4748+
* short circuit compuation
4749+
* defer compuation
47494750
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] */
47504766

47514767
export const Eff = {};
47524768

@@ -4759,35 +4775,35 @@ export const Eff = {};
47594775
Eff.Left = variant({
47604776
tag: "Eff",
47614777
cons: "Eff.Left",
4762-
keys: ["left", "right", "null", "short", "defer"]
4778+
keys: ["left", "right", "null", "short", "lazy"]
47634779
});
47644780

47654781

47664782
Eff.Right = variant2({
47674783
tag: "Eff",
47684784
cons: "Eff.Right",
4769-
keys: ["right", "left", "null", "short", "defer"]
4785+
keys: ["right", "left", "null", "short", "lazy"]
47704786
});
47714787

47724788

47734789
Eff.Short = variant({
47744790
tag: "Eff",
47754791
cons: "Eff.Short",
4776-
keys: ["short", "left", "right", "null", "defer"]
4792+
keys: ["short", "left", "right", "null", "lazy"]
47774793
});
47784794

47794795

47804796
Eff.Null = variant0({
47814797
tag: "Eff",
47824798
cons: "Eff.Null",
4783-
keys: ["null", "left", "right", "short", "defer"]
4799+
keys: ["null", "left", "right", "short", "lazy"]
47844800
});
47854801

47864802

4787-
Eff.Defer = variant({
4803+
Eff.Lazy = variant({
47884804
tag: "Eff",
4789-
cons: "Eff.Defer",
4790-
keys: ["defer", "left", "right", "null", "short"]
4805+
cons: "Eff.Lazy",
4806+
keys: ["lazy", "left", "right", "null", "short"]
47914807
});
47924808

47934809

@@ -4804,7 +4820,17 @@ Eff.map = f => function go(k) { // TODO: make stack-safe
48044820
right: head => tail => Eff.Right(f(head)) (go(tail)),
48054821
null: () => Eff.Null,
48064822
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+
}
48084834
});
48094835
};
48104836

@@ -4820,7 +4846,7 @@ Eff.cata = k => k({ // TODO: make stack-safe
48204846

48214847
null: () => null,
48224848
short: head => head,
4823-
defer: thunk => Eff.cata(thunk())
4849+
lazy: thunk => Eff.cata(thunk())
48244850
});
48254851

48264852

0 commit comments

Comments
 (0)