Skip to content

Commit

Permalink
refactor(pointfree): update/rename storeat => setat, update tests
Browse files Browse the repository at this point in the history
- change behavior to keep obj on stack
  • Loading branch information
postspectacular committed Apr 1, 2018
1 parent 68a8dba commit 92d2d68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/pointfree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ at word construction time and return a pre-configured stack function.
| `pull3` | `( arr -- x y z arr )` | short for: `[pull2, pull]` |
| `pull4` | `( arr -- a b c d arr )` | short for: `[pull2, pull2]` |
| `split` | `( arr x -- [...] [...] )` | split array at index `x` |
| `storeat` | `( val obj k -- )` | `obj` can be array/obj |
| `setat` | `( val obj k -- obj )` | `obj` can be array/obj |
| `tuple(n)` | `( ... -- [...] )` | HOF, like `collect`, but w/ predefined size |
| `vec2` | `( x y -- [x, y] )` | same as `tuple(2)` |
| `vec3` | `( x y z -- [x, y, z] )` | same as `tuple(3)` |
Expand Down
7 changes: 4 additions & 3 deletions packages/pointfree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,16 +1563,17 @@ export const at = op2((b, a) => a[b]);
/**
* Writes `val` at key/index in object/array.
*
* ( val obj k -- )
* ( val obj k -- obj )
*
* @param ctx
*/
export const storeat = (ctx: StackContext) => {
export const setat = (ctx: StackContext) => {
const stack = ctx[0];
const n = stack.length - 3;
$n(n, 0);
stack[n + 1][stack[n + 2]] = stack[n];
stack.length = n;
stack[n] = stack[n + 1];
stack.length -= 2;
return ctx;
};

Expand Down
10 changes: 5 additions & 5 deletions packages/pointfree/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,16 @@ describe("pointfree", () => {
assert.deepEqual(pf.at($([{ id: 42 }, "id"]))[0], [42]);
});

it("storeat", () => {
assert.throws(() => pf.storeat($([1, 2])));
it("setat", () => {
assert.throws(() => pf.setat($([1, 2])));
let a: any = [10, 20];
assert.deepEqual(pf.storeat($([30, a, 0]))[0], []);
assert.deepEqual(pf.setat($([30, a, 0]))[0], [a]);
assert.deepEqual(a, [30, 20]);
a = [10, 20];
assert.deepEqual(pf.storeat($([30, a, 3]))[0], []);
assert.deepEqual(pf.setat($([30, a, 3]))[0], [a]);
assert.deepEqual(a, [10, 20, , 30]);
a = {};
assert.deepEqual(pf.storeat($([30, a, "a"]))[0], []);
assert.deepEqual(pf.setat($([30, a, "a"]))[0], [a]);
assert.deepEqual(a, { a: 30 });
});

Expand Down

0 comments on commit 92d2d68

Please sign in to comment.