montagejs / collections

This package contains JavaScript implementations of common data structures with idiomatic interfaces.
http://www.collectionsjs.com
Other
2.09k stars 185 forks source link

SortedMap.reduceRight behaviour seems incorrect #198

Closed Nipatsku closed 6 years ago

Nipatsku commented 6 years ago

I have a SortedMap with number keys (0, 1, 2) and corresponding string values. I'd expect 'reduceRight' to callback in the following key-order: (2, 1, 0) but instead I'm getting (2, 0, 1). Seems like a problem that people would run into quite frequently.

This is the code I'm testing with ` import { SortedMap } from 'collections/sorted-map'

const map: SortedMap<number, string> = new SortedMap() map.set( 0, "element_0" ) map.set( 1, "element_1" ) map.set( 2, "element_2" )

console.log( "\nforEach" ) map.forEach( ( val, key ) => { console.log( key + ": " + val ) } )

console.log( "\nreduce" ) map.reduce( ( cur, val, key ) => { console.log( key + ": " + val ) return false } )

console.log( "\nreduceRight" ) map.reduceRight( ( cur, val, key ) => { console.log( key + ": " + val ) return false } ) `

Nipatsku commented 6 years ago

My console log looks like this:

forEach 0: element_0 1: element_1 2: element_2

reduce 0: element_0 1: element_1 2: element_2

reduceRight 2: element_2 0: element_0 1: element_1

hthetiot commented 6 years ago

This is fixed by #206 and released today as part of v5.1.3

If you still have the issue please re-open @Nipatsku