From 3425535d58efd3c1f4a69af6b67109aab7d9af45 Mon Sep 17 00:00:00 2001 From: Aaron Levin Date: Mon, 11 Feb 2013 18:29:19 -0500 Subject: [PATCH] Update reader/config.scala MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without thinking about the for comprehension, look at how using the map method seems to push the need to instantiate configuration all the way up to main. The problem is that then you have to deal with "ConfigReader"'s everywhere.  --- reader/config.scala | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/reader/config.scala b/reader/config.scala index 0f884bf..01fc8ac 100644 --- a/reader/config.scala +++ b/reader/config.scala @@ -69,6 +69,49 @@ object TestApp { val lifted = ConfigReader.lift3ConfigReader(dummyLen) // now what? + + // Just makin' my method, don't care about no config! + // UrlServiceClient => String => String + def getBody(urlClient: UrlServiceClient)(url: String): String = { + urlClient.getBody(url) + } + def getHeader(urlClient: UrlServiceClient)(url: String): String = { + urlClient.getHeader(url) + } + def getAllTheData(url: String): String = { + val urlClient = new UrlServiceClient(port=5432) + getBody(urlClient, url) + getHeader(urlClient, url) + + } + def sayHello: String = { + "hello: " + getAllTheData("http://realycool.geocities.com/toky/shrine/23424") + + } + + // OH NOZ!!! UrlClient now depends on configuration parameters!!!!! + def getAllTheData2(url: String): ConfigReader[String] = { + portReader.map { urlClient => getBody(urlClient, url) + getHeader(urlClient, url) } + } + // getAllTheData has now defered the passing of configuration. + // Say hello now becomes: + def sayHello2: String = { + val config = GlobalConfigGrossness.getConfig + "hello: " + getAllTheData2("http://reallycool.geocities.com/tokyo/shrine/23424")(config) + } + + // If we needed to defer computation, we could just keep using map, pushing it all the way up to main + def sayHelloThenGoodBye: ConfigReader[String] = { + sayHello2.map { str => str + ", goodbye" } + } + + def main(args: Array[String]) { + // Look! Caring about config was pushed all the way up to main + val config = GlobalConfigGrossness.getConfig + sayHelloThenGoodBye(config) + } + + + } }