<commit_before>var Bacon   = require('baconjs').Bacon;
var express = require('express');

var __    = require('./functional');
var db    = require('./database');

var connections, response,                               // streams
    fromRecordToResp, fromQueryToRecord, fromReqToQuery, // combinators
    app = express();

function streaminate(app, path) {
  var bus = new Bacon.Bus()

  app.get(path, function(req, res) {
    bus.push({
      req: function() { return req; },
      res: function() { return res; }
    });
  });

  return bus;
}

// return 'No match found' if null, val otherwise
fromRecordToResp = __.iif(__.cnst('No match found'), __.id, __.neq(null));

// given an http conn, returns its title param
fromReqToQuery = __.compose(__.prop('title'), __.prop('params'), __.send('req'));

// given a book title, queries database
fromQueryToRecord = __.partial(Bacon.fromNodeCallback, db.getBookByTitle)

// a stream of inbound HTTP connections
connections = streaminate(app, '/books/:title');

// a mapping of inbound HTTP connections to books in our
// mongo database
responses = connections
  .map(fromReqToQuery)
  .flatMap(fromQueryToRecord)
  .map(fromRecordToResp)

// zip both streams together and respond
Bacon.zipAsArray(connections, responses).onValues(function(conn, body) {
  conn.res().send(body);
});

app.listen(3001);

console.log("Listening on port 3001");<commit_msg>flatMap can take a bunch of functions - who knew?<commit_after>var Bacon   = require('baconjs').Bacon;
var express = require('express');

var __    = require('./functional');
var db    = require('./database');

var connections, response,                               // streams
    fromRecordToResp, fromQueryToRecord, fromReqToQuery, // combinators
    app = express();

function streaminate(app, path) {
  var bus = new Bacon.Bus()

  app.get(path, function(req, res) {
    bus.push({
      req: function() { return req; },
      res: function() { return res; }
    });
  });

  return bus;
}

// return 'No match found' if null, val otherwise
fromRecordToResp = __.iif(__.cnst('No match found'), __.id, __.neq(null));

// given an http conn, returns its title param
fromReqToQuery = __.compose(__.prop('title'), __.prop('params'), __.send('req'));

// a stream of inbound HTTP connections
connections = streaminate(app, '/books/:title');

// a mapping of inbound HTTP connections to books in our
// mongo database
responses = connections
  .map(fromReqToQuery)
  .flatMap(Bacon.fromNodeCallback, db.getBookByTitle)
  .map(fromRecordToResp)

// zip both streams together and respond
Bacon.zipAsArray(connections, responses).onValues(function(conn, body) {
  conn.res().send(body);
});

app.listen(3001);

console.log("Listening on port 3001");
{'fraction_non_alphanumeric': 0.14233709501274117, 'fraction_numerical': 0.005824535857298871, 'mean_word_length': 3.729776247848537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7020526', 'n_tokens_mistral': 863, 'n_tokens_neox': 834, 'n_words': 235}