Skip to content

Instantly share code, notes, and snippets.

@peekg
Created June 7, 2018 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peekg/80327633e2b21aa3129e8421f780f3c2 to your computer and use it in GitHub Desktop.
Save peekg/80327633e2b21aa3129e8421f780f3c2 to your computer and use it in GitHub Desktop.
immutable.js iterate map
//@flow
type Props = {
name: string
}
type MyRecord = RecordOf<Props>
type MyMap = Map<MyRecord,List<number>>
const create: MyRecordFactory<Props> = Record({name:""});
const myMap = Map().set(create({name:"hi"}), List([1]) )
//example 1
const arr = Seq(myMap.entries()).reduce( (list, map) => {
list.push( map[0].name )
return list;
}, [] );
//example 2
const iterMap = ( iter, cb ) => {
const list = [];
const func = (iter, cb) => {
const n = iter.next();
return n.done ? list : list.push( cb( n.value ) ) && func(iter,cb);
}
return func( iter, cb );
}
const arr2 = iterMap( myMap.keys(), it => it.name )
arr.concat(arr2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment