Skip to content

Commit

Permalink
Fix bind using function param. Add Exe.
Browse files Browse the repository at this point in the history
  • Loading branch information
FbN committed Jan 26, 2021
1 parent f60a2e8 commit a0a680e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 88 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ ArrayDo([
]) // [3, 4, 6, 8]
```
## Scope init
By default Do start the computation with an empty scope {}.
It's possibile to start computation with an initial scope using Exe.
```js
import {Exe, bind, returns} from 'yado-js'

const ArrayDo = Exe({
pure: x => [x],
bind: xs => f => xs.map(f).reduce((a, b) => a.concat(b)
})

ArrayDo([
bind('x')([1, 2]),
bind('y')([3, 4]),
returns(s => s.x * s.y + s.k)
])({k: 1}) // [4, 5, 7, 9]
```
**See below for more example and use cases**
<!-- STATEMENTS -->
Expand Down
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const isFunction = statement => typeof statement === 'function'
const resultOrChain = chain => scope =>
scope[RESULT] !== undefined ? scope[RESULT] : chain(scope)

const resolveArgument = (argument, scope) =>
isFunction(argument) ? argument(scope) : argument

const tokenChain = ({ pure, bind }) => (chain, [token, argument]) => {
if (token === 'return') {
return scope =>
chain({
...scope,
[RESULT]: pure(
isFunction(argument) ? argument(scope) : argument
)
[RESULT]: pure(resolveArgument(argument, scope))
})
}
return scope => bind(argument)(v => chain({ ...scope, [token]: v }))
return scope =>
bind(resolveArgument(argument, scope))(v =>
chain({ ...scope, [token]: v })
)
}

const blockResolve = Monad => {
Expand Down Expand Up @@ -54,8 +58,10 @@ const getMonad = MonadDef =>
}
: MonadDef

const Do = MonadDef => statemens =>
blockResolve(getMonad(MonadDef))(statemens)(scope => scope[RESULT])({})
const Exe = MonadDef => statemens =>
blockResolve(getMonad(MonadDef))(statemens)(scope => scope[RESULT])

const Do = MonadDef => statemens => Exe(MonadDef)(statemens)({})

// Command utility
const bind = identifier => argument => ({ [identifier]: argument })
Expand All @@ -69,4 +75,4 @@ Do.bind = bind
Do.returns = returns
Do.to = to

export { Do }
export { Do, Exe }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yado-js",
"version": "0.0.1",
"version": "0.0.3",
"description": "Yet Another DO notation for Javascript",
"main": "index.js",
"repository": "https://github.com/FbN/yado-js",
Expand Down
18 changes: 18 additions & 0 deletions tests/features.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ test('with scope', t => {
}
t.deepEqual(BurridoArrayDo(burr), ArrayDo(yado)) // [ 4, 5, 7, 9 ]
})

test('by function', t => {
const yado = [
{ x: () => [1, 2] },
s => ({ ...s, z: 1 }),
{ y: [3, 4] },
{
return: s => s.x * s.y + s.z
}
]
const burr = function * () {
const x = yield [1, 2]
const z = 1
const y = yield [3, 4]
return x * y + z
}
t.deepEqual(BurridoArrayDo(burr), ArrayDo(yado)) // [ 4, 5, 7, 9 ]
})
Loading

0 comments on commit a0a680e

Please sign in to comment.