DrBoolean / immutable-ext

fantasyland extensions for immutablejs
ISC License
490 stars 20 forks source link

traverse run in reverse order #9

Closed ron-liu closed 6 years ago

ron-liu commented 7 years ago

I tried to do: given a list of functions which return tasks, run given tasks sequentially, and return the task of a list of returned values.

For the following case, I expected to output: 1 2 3, but it outputted: 3 2 1.

const {task, of} = require('folktale/concurrency/task') // using folktale 2.x
const buildTask = (n, ms) => task(({resolve}) => setTimeout( ()=> {
        console.log(n)
        resolve(n)
    }, ms
))

test('sequentially', async () => {
    const {List} = require('immutable-ext')

    await List.of(
        () => buildTask(1, 500),
            () => buildTask(2, 0),
        () => buildTask(3, 1000)
    )
    .traverse(of, f => f())
    .map(x=>x.toArray())
    .run()
    .promise()
})

if I change the following code, it gave me 1 2 3, which is what I expected.

List.prototype.traverse = function(point, f) {
    return this.reduce((ys, x) =>
        // f(x).map(x => y => y.concat([x])).ap(ys), point(this.empty))
        ys.map(x => y => x.concat([y])).ap(f(x)), point(this.empty))
}

Not sure it is a issue, or by design.

DrBoolean commented 7 years ago

Hrmm, this is interesting. Would you mind making a pr? Also, i've never seen run() or promise() yet. Is that part of immutable?

ron-liu commented 7 years ago

I will make a pr soon. The run() and promise() are part of folktale 2, the next version.of data.task. check their homepage.