4 ms·
But why won't it scale up? This is essentially my question. And, it is not like there are not explorations of cooking in non imperative forms. When your meta
by taeric 13y ago
But why won't it scale up? This is essentially my question. And, it is not like there are not explorations of cooking in non imperative forms.
When your metaphor is "with these ingredients and devices, perform these transformations on them" an imperative form works really well. This is just as true for any algorithm you will want to write. Consider, write down any algorithm for how to do something. I challenge that key to your algorithm will be the idea of transforming something. Often in a "destructive" way.
An example, with the concept of a browser that has state, the following algorithm will get you logged in to your bank.
browser = new browser
browser.location = "www.yourbank.com"
browser.focusFieldLabeled("username")
browser.type("your name")
browser.focusFieldLabeled("password")
browser.type("your password")
browser.focusButtonLabeled("login").or("submit")
browser.click()
Implementing something that does this would be trivial, all things told. Now, do so without the metaphor of a "stateful" browser. Just trying to come up with "pure functional" implementations of each of these is not something that sounds pleasant to me. (I look forward to being surprised. :) )
- agumonkey 13y agoAbout the scaling, my take is implicit dependencies expressed by order of statements. It's fragile, not composable, not abstractable. Your browser example is completely imperative, it's a sequence of mutations. You just don't think that way in FP, you describe the new. (def url "http...") ;; url -> (html,session) (get url) ;; (url, form, session) -> (html, session) (let ((form {username ... password ...})) (post url form)) You can thread these calls to pass the http state for you.
- taeric 13y agoRight, my argument is that the imperative form is much more easily reasoned about in this example. It is not as easily decomposed, but the question comes down to how much it needs to be. To wit, I don't know how I would actually fill out the implementations of what you described such that it really worked. Without actually just manipulating the state of some browser thing.
- agumonkey 13y agoHave you seen Rich Hickey talks ? he mentions the notion of ease in one, and the notion of mutation in many others. An html browser right now is a large stateful tree, imperative programming so it is easy to think about and write code this way, while FP orientation would need to rethink things, less easy, but probably simpler. In clojure data structure are immutable, you "only" compute new, I think that's how they designed a web app in http://pedestal.io/ http://pedestal.io/ (clojure[script] based system). If you really care about accepting small grained input you'd probably write some read eval/validate print loop on the client side before then sending a main request for a http json patch.
- taeric 13y agoRight, but my assertion here is that this really only works because Hickey has basically put forth "mathematics" as the core metaphor for the things he is building. That is, he effectively wants an identity equation for every moment of time in the application. This idea that "state" is somehow hard to reason about is one that I am growing to reject. Sure, if it is not part of the metaphor that one has built, it can be tough. But this is also true for what many of us mean when we say "mathematics." What is the equation for how you get home every day? How do you calculate what represents you yesterday? Look at some of the classic algorithms. Is quicksort really that tough to reason about because it is in place (and thus relies on mutation)? Bubble sort? (Any sort...) Is cleaning your room or office truly difficult to reason about because it involves modifying the room? Fixing dinner, since it involves modifying the ingredients? This is what I loved about that paper linked up thread. When you build your abstractions around the ways in which things interact by mutations, then the code gets much clearer. This is not because we wall off the mutations, but because they are given names and reasoned about. It is not that we constantly have new instances of a list or collection with the old one hanging around, as that can be just as problematic as otherwise. Consider, sure this is an obvious error to a casual programmer in Java: String x = ... x.toLowerCase(); doSomething(x); But, why is this a mistake? Wouldn't it be MUCH more intuitive if you wanted to keep both values the code would look like: String x = ... String original = x; x.toUpperCase(); doSomething(x); Just from a communication perspective. That there really isn't a "focused" object in languages often strikes me as something that could be addressed. (And, I believe this is the key of stack based languages, where they actually would read like I indicated.)