Skip to content

Instantly share code, notes, and snippets.

@mattflo
Last active August 29, 2015 13:56
Show Gist options
  • Save mattflo/9211285 to your computer and use it in GitHub Desktop.
Save mattflo/9211285 to your computer and use it in GitHub Desktop.
DataMunger.iced
# The data munging kata - http://codekata.com/kata/kata04-data-munging/
fs = require("fs")
readline = require("readline")
footballParseJob =
file: "./football.dat"
max_column: 6
min_column: 8
id: 1
weatherParseJob =
file: "./weather.dat"
max_column: 1
min_column: 2
id: 0
find_min_delta = (job, cb) ->
rd = readline.createInterface(
input: fs.createReadStream(job.file)
output: process.stdout
terminal: false
)
rd.on "line", (line) ->
matches = line.match(/\s+(\S+)/g)
return unless matches
return unless parseInt(matches[0])
max = parseInt(matches[job.max_column])
min = parseInt(matches[job.min_column])
delta = Math.abs(max - min)
unless job.smallest_delta and job.smallest_delta < delta
job.smallest_delta = delta
job.smallest_id = matches[job.id]
rd.on "close", ->
console.log "The smallest row in #{job.file} is #{job.smallest_id} with a delta of #{job.smallest_delta}"
await
find_min_delta footballParseJob, defer result
find_min_delta weatherParseJob, defer result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment