The first tenet of functional programming: code is data. Store it in variables, pass it as parameters, return it from methods. This is challenging in the JVM, where all code lives inside methods on objects. Therefore, Scala passes around code in a function object. Code lives in the apply() method. When is a function object created? … Read moreWhen is code data, and when is it code?
flatMap is a method on collections that, at a surface level, transforms each element into another collection of any type, then takes all the elements out of those collections and puts them into one. It is like a map followed by a flatten. Why do two things in one step? Conceptually, List.map is always 1:1 … Read moreWhat is flatMap?
Programming is about translating what a human wants into instructions a computer can understand. Or is it? Thinking down this path, there are two ends of a programming language spectrum. A language can be close to the computer’s perspective: imperative languages that declare data, move and store data, carry out instructions in a fixed order. At … Read moreA third way
Michael O’Church has a lot to say about the functional programming community. His post, Functional Programming is a Ghetto (in the “isolated, exclusive neighborhood” sense of ghetto) contains some great descriptions of what goes through a programmer’s head after he or she learns to think in a functional style. The following is a paraphrase of … Read moreWhat makes a functional programmer?
The book Structure of Scientific Revolutions (1962, Thomas Kuhn) brought the word paradigm into common use. Ideas from fifty years ago about research science apply today to computer science and the software industry. This is one parallel Kuhn makes, extended to illuminate how we think about programming. The human mind does not perceive objects incrementally, … Read moreMental revolution
Why learn functional programming? For better design skills! The other day we designed a process to match cleared deposits with dispensations. This is how I would have white-boarded it a few years ago: Since then I’ve played around with functional programming. It encourages one to think about processing in terms of data streams: data comes … Read moreWhy Functional Matters: Your white board will never be the same
Today I had a sticky problem to solve, the kind business app programmers drool over because it’s nontrivial and contained. Halfway through the TDD process, my logic got all jumbled and I couldn’t see the solution. Stepping back, I took ten minutes to write pseudocode in an F# style, then translated that into Java. Bam! … Read moreF# made Java easier today
SICP insight of the day: iterative programming doesn’t require a for loop. Tail recursion is iterative programming. It looks very close to recursion, but the distinction is: a program is iterative if everything it needs is stored in a few state variables. In an imperative style the state is stored in mutable variables. In a … Read moreLessons from SICP: what is iterative programming?
The previous post discussed advantages of using a continuation style to send our code to the data instead of making our code wait for the data to come back to it. This pseudocode example has three I/O operations, each encasing the operations which should follow in a callback function parameter. let filename = // calculate … Read moreContinuation Style Without the Fugly
Object-oriented code was conceived as message-passing between objects. Service-oriented architecture emphasizes delegation to another system. The entire web is a whole bunch of requests flying around. There is one clear way to be efficient about this: stop waiting for results. When we’re writing imperative code, we want to write the operations in the order they … Read moreBring the data to the code, or the code to the data?