Created
October 15, 2020 12:50
-
-
Save afghl/707448616df319ccaee4a72d0a24e148 to your computer and use it in GitHub Desktop.
This file contains 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
const fs = require('fs') | |
class KeyValue { | |
constructor(key, value) { | |
this.key = key | |
this.value = value | |
} | |
} | |
const mapF = (fileName, contents) => | |
contents.split(/[^A-Za-z]/).filter(a => a.length > 0).map((word) => new KeyValue(word, 1)) | |
const reduceF = (key, values) => {return {[key]: values.reduce((sum, v) => sum + v)}} | |
const sort = (a, b) => { | |
if (a.key < b.key) { | |
return -1; | |
} else if (a.key > b.key) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
const start = () => { | |
// './contents/pg*.txt' | |
["./contents/pg-being_ernest.txt", "./contents/pg-dorian_gray.txt", "./contents/pg-frankenstein.txt"] | |
// map | |
let intermediate = [] | |
fileNames.forEach((name, _) => { | |
contents = fs.readFileSync(name, 'utf8'); | |
const results = mapF(name, contents) | |
intermediate.push.apply(intermediate, results) | |
}); | |
// shuffle | |
intermediate.sort(sort) | |
// reduce | |
let i = 0 | |
let results = [] | |
while (i < intermediate.length) { | |
let j = i | |
let values = [] | |
while (j < intermediate.length && intermediate[i].key == intermediate[j].key) { | |
values.push(intermediate[j].value) | |
j = j + 1 | |
} | |
const reduceResult = reduceF(intermediate[i].key, values) | |
results.push(reduceResult) | |
i = j | |
} | |
console.log(results); | |
} | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment