Created
May 24, 2018 09:30
-
-
Save boogie666/bbea73d422788be0fa72dd94e436c31b to your computer and use it in GitHub Desktop.
Finding pairs in a stream of data using windowing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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