Skip to content

Commit

Permalink
Add burrido wrapper for Do notation computation.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkirov committed Mar 10, 2023
1 parent 20b81e0 commit 0c586f1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,5 @@ I consider this library still a work-in-progress. My next tasks would be:
- [ ] benchmark performance and memory usage. It was not an explicit goal of the current implementation, but there could be benefits of the minimal continuation approach.
- [ ] Make sure there are no mem leaks. I used WeakRef, but didn't test that.
- [ ] add effects, i.e. computations that are recomputed without an explicit .value read.
- [ ] some `async/await`-like synthetic sugar to make this acceptable for the JS developer. Or just wait for `do`-notation to land in ECMAScript. Try to use something like [https://github.com/pelotom/burrido](https://github.com/pelotom/burrido) to use generators?
- [ ] Smarter keeping track of values. `x = 1; x = 2; x = 1` will trigger recomputation of all x dependencies.
- [ ] fix burrido wrapper types if possible.
26 changes: 26 additions & 0 deletions burrido-wrapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Computed} from './burrido-wrapper';
import {input} from '.';

test('burrido-wrapper basic sum', () => {
let a = input(1);
let b = input(2);
let c = Computed.Do(function*(): any {
return (yield a) + (yield b);
});
expect(c.value).toBe(3);
a.value = 2;
expect(c.value).toBe(4);
});

test('burrido-wrapper basic boolean', () => {
let a = input(1);
let b = input(2);
let c = input(true);
let f = Computed.Do(function*(): any {
return (yield c) ? a : b;
});
expect(f.value).toBe(1);

c.value = false;
expect(f.value).toBe(2);
});
7 changes: 7 additions & 0 deletions burrido-wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Monad from 'burrido';
import {input} from '.';

export const Computed = Monad({
pure: (x: any) => x,
bind: (x: any, fn: any) => x.read(fn)
});
3 changes: 3 additions & 0 deletions burrido.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'burrido' {
export default function({pure, bind}: {pure: any, bind: any}): any
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"@types/jest": "^29.4.0",
"burrido": "^1.0.8",
"jest": "^29.4.3",
"typescript": "^4.9.5"
},
Expand Down

0 comments on commit 0c586f1

Please sign in to comment.