5 ms·
Great points, but I have a question about one of them in particular: > - Functional is OK. Imperative is OK. Both in the same program are a mess. What do you
by TeMPOraL 9y ago
Great points, but I have a question about one of them in particular:
> - Functional is OK. Imperative is OK. Both in the same program are a mess.
What do you mean by that?
My experience is that functional alone is impossible, since the only useful thing a program can do is through state changes; imperative is a-OK, and functional+imperative in the same program is the best way to do things (i.e. well-defined stateful areas surrounded by lots of functional code).
- s4vi0r 9y agoOnce you get over the initial learning curve of the functional/pure approach to state/IO, its far superior to imperative imo. You don't need to reason about global state - because everything is explicit, including passing around your state, you never have to worry about "what if someone else or some other code somewhere is touching this" again.
- bryanph_ 9y agoImperative programming is fine imo, as long as it is done in a relatively small scope.
- chrisseaton 9y ago> My experience is that functional alone is impossible, since the only useful thing a program can do is through state changes Some programs are simply pure functions. If your program entry point accepts a string and returns a string, then you can write useful things, such as compilers, image processing, grep, etc, entirely as pure functions.
- fnord123 9y agoUnless you want logging with timestamps.
- chrisseaton 9y agoMy point was that it is not impossible to write useful programs as pure functions. You can't counter that with an example of a program that you can't write as a pure function.
- TeMPOraL 9y agoOk, to clarify - I didn't mean program as in a simple function. I meant a program as in application, something that accepts some real-world input and produces some real-world consequences.
- chrisseaton 9y agoRight - a compiler. That's a real-world application isn't it? A compiler can be a pure function - accepting source text as input, and producing machine code as output. Yes more complicated languages do more complicated things, but for several languages you could write a state-of-the-art compiler as a pure function.
- TeMPOraL 9y agoHm. Fair enough.
- TheOtherHobbes 9y agoOnly if you ignore the dynamic state of the language, of associated libraries, of processor architectures, and so on - i.e. the context the compiler is working in and the mutable state of the language and development environment as a whole. The idea of pure functions is a false friend in CS because it tries to solve the problem of state by wrapping it up and wishing it away. It's true that state causes a lot of problems, but so many useful systems rely on mutable state - at practical application levels - that it might be interesting to design robust systems that manage state, context, and relationship instead of trying to create contrived examples of state-free systems. There seems to be a cognitive bias against this in CS. Most developers appear to love puzzle systems made of hard little components with solid edges, and thinking in terms of context and relationships seems to be disturbingly alien. So there aren't many programming paradigms that explicitly work with contextual inference instead of trying to rigidly delimit interfaces. But there are real prizes to be won from associative context-smart computing, and IMO the domain is wide open for innovation - because it may be possible to give up the pursuit of complete safety and predictability (which doesn't exist anyway) in return for new kinds of powerful, productive, and smart features.
- runeks 9y agoLogging with timestamps is not in opposition to purity. All purity does is force you to change the type of a function that wants to log something, to reflect that it’s returning a value that, when evaluated at run-time, will have the side-effect of printing to the console. It’s still a pure function since it returns the same description given the same argument(s), the only difference is that when this description is evaluated by the run-time system, something will be printed to the console (in accordance with the description). In other words, you’re not restricting the effects your program can have, only how you can choose to describe them (as first-class values whose evaluation has a side-effect that is not observable by your code). It’s all explained perfectly in this talk: https://www.infoq.com/presentations/io-functional-side-effects#.WfRR_6ZtSbY.hackernews https://www.infoq.com/presentations/io-functional-side-effec...
- pdimitar 9y agoErlang / Elixir are 100% immutable inside the code (no var can ever be mutable). However, they have a mutable in-process cache (living in the BEAM VM) that many people have written libraries around for stuff like mutable arrays, matrices, graphs, and many others. It goes like this: do 99.5% functional programming and you have the imperative / stateful tools for when they are absolutely necessary. This works. Extremely well.