Created
August 30, 2014 15:41
-
-
Save TrevorBasinger/a65e374d3e7fab0b7f10 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require('pointfree-fantasy').expose(global); | |
var Future = require('data.future'); | |
var Either = require('data.either'); | |
var State = require('fantasy-states'); | |
var Tuple2 = require('fantasy-tuples').Tuple2; | |
var Tuple3 = require('fantasy-tuples').Tuple3; | |
var IO = require('fantasy-io'); | |
var Maybe = require('pointfree-fantasy/instances/maybe'); | |
var _ = require('ramda'); | |
var http = require('http'); | |
var rl = require('readline'); | |
///////////////////////////////////////////////////////////////// | |
//// Application | |
///////////////////////////////////////////////////////////////// | |
//--> Load config | |
// --> Start App With Config | |
// --> Return config | |
// --> Print Config | |
///////////////////////////////////////////////////////////////// | |
//// Pure | |
///////////////////////////////////////////////////////////////// | |
var decode = function ( x ) { return Maybe (x.toString('utf-8')); } | |
var getInput = IO.of (function (msg) { | |
var prompt = rl.createInterface(process.stdin, process.stdout); | |
prompts.question(msg, function(res) { | |
return msg; | |
}); | |
}); | |
// println = Object -> IO Object | |
var println = console.log; | |
// makeRequest :: String -> String -> Future | |
var makeRequest = function (host, path) { | |
var options = { host: host, path: path, port: 80 }; | |
return new Future (function (err, succ) { | |
var data = ''; | |
var req = http.get(options, function(res) { | |
res.on('data', function(d) { data += d.toString('utf-8'); }); | |
res.on('end', function() { succ(data); }); | |
}); | |
req.on('error', err); | |
}); | |
}; | |
// replace = RegExp -> String -> String -> String | |
var replace = curry (function (match, repl, str) { | |
return String.prototype.replace.call(str, match, repl); | |
}); | |
// getUrlFromConfig = Maybe ConfigObject -> Maybe String | |
var getUrlFromConfig = map(_.prop('url')); | |
// replaceUrlWithQuery = String -> String -> String | |
var replaceUrlWithQuery = replace(/XXXX/g); | |
// loadConfig :: String -> IO Maybe String | |
var loadConfig = compose(IO.of, Maybe, require); | |
// runWithConfig = String -> IO Maybe String | |
var runWithConfig = compose(map(getUrlFromConfig), loadConfig); | |
var main = runWithConfig('./blah') | |
///////////////////////////////////////////////////////////////// | |
//// Impure | |
///////////////////////////////////////////////////////////////// | |
//ap(println, main) // Returns Maybe Url | |
var v = makeRequest('www.reddit.com','/'); | |
v.fork(println, println); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment