Skip to content

sampleCombine bug-like behavior #324

@nukisman

Description

@nukisman

Hey folks!
I found a strange behavior of sampleCombine.
It reproduces in case of: input.compose(sampleCombine(derivedFromInput, independentOfInput)) - only when:

  • some derivedFromInput (or just same input) is the first parameter of sampleCombine
  • and other parameters are independent of input.
    In such cases other parameters are independent of input will be one step late.

Code to reproduce:

xs.periodic(100)
  .take(5)
  .compose(all => { // all - independent of values
    // squaresAll - independent of values
    const squaresAll = all.map(v => v * v).debug('squareAll');

    // values - 'derived' from it self
    const values = all.debug('values'); 

    // squaresValues - derived from values
    const squaresValues = values.map(v => v * v).debug('squareValues'); 

    // [ 0, 0, 0 ] - OK
    // [ 2, 4, 2 ] - OK
    // [ 3, 9, 3 ] - OK
    // [ 4, 16, 4 ] - OK
    // return values.compose(sampleCombine(squaresAll, values));

    // [ 0, 0, 0 ] - OK
    // [ 2, 4, 2 ] - OK
    // [ 3, 9, 3 ] - OK
    // [ 4, 16, 4 ] - OK
    // return values.compose(sampleCombine(squaresValues, values));

    // [ 0, 0, 0 ] - OK
    // [ 2, 2, 4 ] - OK
    // [ 3, 3, 9 ] - OK
    // [ 4, 4, 16 ] - OK
    // return values.compose(sampleCombine(values, squaresValues));

    // [ 0, 0, 0 ] - missed,
    // [ 2, 2, 1 ] - incorrect (squaresAll: previous value)
    // [ 3, 3, 4 ] - incorrect (squaresAll: previous value)
    // [ 4, 4, 9 ] - incorrect (squaresAll: previous value)
    // return values.compose(sampleCombine(values, squaresAll));

    // [ 0, 0, 0 ] - OK
    // [ 1, 1, 1 ] - OK
    // [ 2, 2, 4 ] - OK
    // [ 3, 3, 9 ] - OK
    // [ 4, 4, 16 ] - OK
    // return values.compose(sampleCombine(all, squaresAll));

    // [ 0, 0, 0 ] - OK
    // [ 1, 1, 1 ] - OK
    // [ 2, 2, 4 ] - OK
    // [ 3, 3, 9 ] - OK
    // [ 4, 4, 16 ] - OK
    // return values.compose(sampleCombine(all, squaresValues));

    // [ 0, 0, 0 ] - OK
    // [ 1, 1, 1 ] - OK
    // [ 2, 4, 2 ] - OK
    // [ 3, 9, 3 ] - OK
    // [ 4, 16, 4 ] - OK
    // return values.compose(sampleCombine(squaresAll, all));

    // [ 0, 0, 0 ] - missed,
    // [ 1, 1, 0 ] - incorrect (all: previous value)
    // [ 2, 4, 1 ] - incorrect (all: previous value)
    // [ 3, 9, 2 ] - incorrect (all: previous value)
    // [ 4, 16, 3 ] - incorrect (all: previous value)
    // return values.compose(sampleCombine(squaresValues, all));

    // [ 0, 0, 0 ] - OK
    // [ 1, 1, 1 ] - OK
    // [ 2, 4, 4 ] - OK
    // [ 3, 9, 9 ] - OK
    // [ 4, 16, 16 ] - OK
    // return values.compose(sampleCombine(squaresAll, squaresValues));

    // [ 0, 0, 0 ] - missed,
    // [ 1, 1, 0 ] - incorrect (squaresAll: previous value)
    // [ 2, 4, 1 ] - incorrect (squaresAll: previous value)
    // [ 3, 9, 4 ] - incorrect (squaresAll: previous value)
    // [ 4, 16, 9 ] - incorrect (squaresAll: previous value)
    return values.compose(sampleCombine(squaresValues, squaresAll));
  })
  .addListener(printer());

export const printer = <T = any>(): Listener<T> => ({
  next: console.log,
  error: console.error,
  complete: () => console.log('completed'),
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions