Last active
October 25, 2016 14:12
-
-
Save MikeBild/dfefdf0c09c3b4fbb4baa256d9f8de2e to your computer and use it in GitHub Desktop.
Node.js - CPU bound operations with Rx
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 RxNode = require('rx-node'); | |
const Rx = require('rx'); | |
const spawn = require('child_process').spawn; | |
RxNode.fromStream(spawn('find', ['/','-type','f','-exec', 'cat', '{}', '\+']).stdout) | |
.map(x => x.toString()) | |
// using python | |
.map(x => RxNode.fromStream(spawn('./word-count.py', [x]).stdout)) | |
// or using javascript | |
//.map(x => RxNode.fromStream(spawn('./word-count.js', [x]).stdout)) | |
.mergeAll() | |
.map(x => x.toString()) | |
.do(x => console.log(x)) | |
.subscribe(); |
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
#!/usr/bin/env node | |
const wordcount = {}; | |
process.argv[2] | |
.replace(/[.,?!;()"'-]/g, ' ') | |
.replace(/\s+/g, ' ') | |
.toLowerCase() | |
.split(' ') | |
.forEach(function (word) { | |
if (!(wordcount.hasOwnProperty(word))) wordcount[word] = 0; | |
wordcount[word]++; | |
}); | |
console.log(wordcount) |
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
#!/usr/bin/env python | |
import sys | |
wordcount={} | |
for word in sys.argv[1].split(): | |
if word not in wordcount: | |
wordcount[word] = 1 | |
else: | |
wordcount[word] += 1 | |
print (wordcount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment