Skip to content

Instantly share code, notes, and snippets.

@MikeBild
Last active October 25, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikeBild/dfefdf0c09c3b4fbb4baa256d9f8de2e to your computer and use it in GitHub Desktop.
Save MikeBild/dfefdf0c09c3b4fbb4baa256d9f8de2e to your computer and use it in GitHub Desktop.
Node.js - CPU bound operations with Rx
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();
#!/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)
#!/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