Skip to content

Instantly share code, notes, and snippets.

@boogie666
Created May 24, 2018 09:30
Show Gist options
  • Save boogie666/bbea73d422788be0fa72dd94e436c31b to your computer and use it in GitHub Desktop.
Save boogie666/bbea73d422788be0fa72dd94e436c31b to your computer and use it in GitHub Desktop.
Finding pairs in a stream of data using windowing
require("./array.js");
const { reduce, reduced } = require("./reduce.js");
const { map, dropWhile, dedupe, window, comp } = require("./transducers.js");
const { into } = require("./into.js");
function pair_items(items){
items = into([], dropWhile(x => x.hasOwnProperty("pair_id")), items);
var first = items[0];
items.shift();
return reduce(function(i, next_item){
if(i.id === next_item.pair_id){
return reduced([i, next_item]);
}
return i;
}, first, items);
}
const process = comp(window(3), map(pair_items), dedupe());
console.log(into([], process, [{id : 0}, {id:1, pair_id: 0}, {id:2}, {id:3}, {id:4}, {id:5, pair_id: 4}]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment