Skip to content

Instantly share code, notes, and snippets.

@awto
Last active May 28, 2016 10:12
Show Gist options
  • Select an option

  • Save awto/abb08236e36507df39ea7428c8e24b15 to your computer and use it in GitHub Desktop.

Select an option

Save awto/abb08236e36507df39ea7428c8e24b15 to your computer and use it in GitHub Desktop.
most used with mfjs for do notation
import {from,of,fromPromise,Stream} from 'most'
const M = require('@mfjs/core')
import CC from '@mfjs/cc'
M.profile('regenerator')
const MM = CC.makeMonad(Stream.prototype.chain,of)
let cnt = 0
function updateDb() {
return new Promise(resolve => {
setTimeout(
() => {
console.log(`DB UPDATED ${cnt}`)
resolve(cnt++)
}, 0)
})
}
MM.run(function* () {
const m = yield fromPromise(updateDb())
const x = yield from([1,2])
return `${x} ${m}`
}).observe(function(res) {
console.log(res)
})
/*
outputs:
```
DB UPDATED 0
1 0
2 0
```
*/
let i = 0;
MM.run(function*() {
const x = yield from([1,2])
if (i++) {
const y = yield from(['in then'])
return `then ${x} ${y}`
} else {
const y = yield from(['in else'])
return `else ${x} ${y}`
}
}).observe(function(res) {
console.log(res)
})
/*
outputs:
```
else 1 in else
then 2 in then
```
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment