old relationships between functional and object-oriented program‐ ming. There's only so much you . read because it obscures the programmer's intent. We don't want to . of assets and render the BalanceSheet to a PDF report. If the imple‐. portal7.info - Download as PDF File .pdf), hard to read because it obscures the programmer's intent. but first let's take a. Functional Programming for the Object Oriented Programmer. 24 August This book, written by Brian Marick, is important. Indeed, it may.
|Language:||English, Spanish, French|
|ePub File Size:||25.35 MB|
|PDF File Size:||10.22 MB|
|Distribution:||Free* [*Sign up for free]|
Functional Programming for the Object-Oriented Programmer. Brian Marick For more about what the book covers, see the sample PDF. Once upon a time, object-oriented programming was a radical . In the PDF version, links within a book appear as colored text, and links to. Functional Programming for the Object-Oriented Programmer by Brian Marick ( portal7.info) portal7.info~rwh/plbook/portal7.info
Shelves: software-development , quit-reading This book tries to explain functional programming with a lot of code examples using Clojure. Although the first chapter introduces the reader to Clojure which was very enlighting , the fact that this was my first adventure into the language got constantly in my way of understanding the bigger picture. After reading this book halfway through, I was totally lost. Perhaps picking up another book on Clojure before reading this book is a much better approach in order to get the most out of it. Jun 07, Nathan rated it really liked it A great intro to functional programming.
But just look at how many ways you could write factorial in a functional programming language. This makes functional programming much harder to learn. I don't really care if you downvote. I come here discuss programming.
This list is a joke, based on a similar list for imperative programmers writing a "hello world" program. There are certainly a number of ways to write write something in either paradigm. The number of ways you can write something is a horrible metric to evaluate languages on. Any sufficiently clever programmer can come up with a number of bad solutions.
The useful imperative techniques are more obvious to you because you have more experience with that framework. Experienced functional programmers would just as quickly narrow their list of useful solutions. In order to be productive in a functional style, you don't need to know much of the language's standard library.
You simply write small functions that can be applied in a number of areas. You'll probably end up re-inventing the wheel though, as most functional languages come with most of the essential functional constructs included in the standard library.
If anything, functional programming is more beginner friendly, because you don't end up dealing with mutable state and you can easily work on the level of abstraction that you're most comfortable with you're just composing ever more advanced functions from simpler ones.
Most of these are not not what a "reasonable programmer" would do. An unreasonable programmer would be able to screw up on any paradigm.
You'd probably have even more ways to screw up like this with an OO language, creating classes for recursive functions, trampolines, a pattern matching class, implement an language interpreter inside your factorial implementation, etc. You assertion on how many ways one could screw up on OO is irrelevant and besides, you'd really need a good understanding of OO to even try implementing those.
I didn't mean to use to imply any of those methods were bad. I have revised my post so people can't miss the point.
Now, addressing your concerns. All of the examples except for few particularly egregious ones seems reasonable to me. I'm sure there's more that's as elegant if not more elegant ways to do the same thing. After the first 2, the rest are based on common and very useful abstractions. You could translate those to an OO language quite easily too, though with a lot more code.
For example, what foldl does, from my understanding, is to recursively apply a function to both a starting value and the first element of a list, and then the same thing again, where the result from the last call becomes the starting value and the rest of the list becomes the list we're working on, up until the list is empty.
Not sure if I've explained this well but I've not been into this long. If you see the second function, that's what it's doing, except it's working just with numbers and not a list. Since foldl works on lists, you need an enumerator, which in this case is just 1 through to n. The sixth example is great here. Product could easily be an abstraction on something quite similar to three, which itself could very well be an abstraction quite similar to two using lists, functions to deal with lists and a given function.
Please correct me if I'm wrong at any point here. I'm looking to improve these skills quite a lot so it'd be very, very welcome. You got the core, I will correct your edges. The [ And no foldl is not an abstraction of 2 because foldl is tail recursive while 2 is not.
But you are right in that any tail recursive function on a list can be rewritten using foldl. What is particular to haskell vs a strict language like say F is that foldl can still pop a stack due to haskell's laziness.
You want foldl' as it will force the initial argument. Fold is also a fundamental operation on any algebraic data structure such as trees , lists, and natural numbers. You can write filter, map, filtermap etc in terms of it for example.
There is so much to say about fold - they are like the cupboard which leads to narnia.. Oh wow, thanks!
I should have also noted I have no experience with the language in the examples so please ignore any ignorance there probably should have read up on it. All well noted and I'll look into this more. I'm vaguely aware of fold's power and can definitely see how filter, map and the likes would be implemented in terms of it but I would be really interested in the infodump!
I'm just wondering, ignoring laziness and typing or any language-specifics , if foldl could be written similar to 2? Obviously it wouldn't be very good. I've written an example in Racket of what I have in mind: IvoDankolov on June 24, Actually, what you have written is not foldl, it's foldr.
Why is it going right to left? Let's expand your example: What would actually be foldl is this: The tail recursion is automatically optimized for you and you can assume that for any functional language - it's a very crucial optimization for this style of code, after all. That's what the core of it will be in an actual implementation, though with added error handling, type checking and so on. Ivo's answered so I'll just say the main trick to turning a function tail recursive is to have the function lug its state around.
So always look to see if you can rewrite your function so it passes the current value explicitly to the next iteration. Non tail recursion is like winding a spring and then having it unwind, a jack in a box. Tail recursion is like a snowball rolling down a hill. You should always try to make a recursive function tail recursive if you can.
As for fold it's a bit involved and has a lot of scary sounding jargon on the way to understanding it. You can write a fold for a tree and then code depth first search in a couple lines. Folds follow some basic properties that allow for program derivation.
I rarely use those but it does inform my programming. It's kind of like how knowing Lisp informs your python. There is also an opposite or dual to fold called unfold. You can write many algorithms efficiently just using fold and unfold and a couple techniques to prune inefficiencies.
Fold is the same kind of object to your initial tree as say a matrix is to a vector, or a derivative is to a polynomial. Also ignoring tail recursion for the purpose of the above piece of code, haha. Objects and Classes It seems, at first, that the fundamental concept in object-oriented programming is objects.
A class defines an object; an object instantiates a class. There are a lot of customers, but they all do the same thing. So there would be a Customer class, in which the programmer defined what a customer is, and how it works. Then, each individual customer is represented in the system by a customer object. Since class definitions specify all the implementation details, most of the work of developing an object-oriented program involves working with classes, not objects.
These are things that the object is capable of doing, and are also defined within the class. One of the challenges of developing good object-oriented programs is determining how to split up functions among several potential classes. For example, should a checkout method belong to the Customer or to the Shopping Cart? Understanding that ecommerce needs Products and a project management system needs Projects is pretty easy. But large object-oriented programs have countless classes defining all sorts of abstract bundles of functionality, like object relational mappers, object factories, and controllers just to name three easy-to-understand examples.
Navigating these concerns takes practice, of course. An object-oriented developer can also be aided by an understanding of design patterns, and by a development process that includes modeling. Here are a few resources to help you better understand class, properties, and methods: Classes, Objects, Properties, and Method Concepts is a great minute video overview of these concepts; Choosing Between Properties and Methods is a. NET tutorial, covering some design considerations which are applicable to any language; Understanding Classes, Methods, and Properties in C provides a good explanation, with some helpful diagrams.
Message Passing and Dynamic Dispatch Classes, and a number of other concepts covered here, have become inexorably linked to object-oriented programming. However, according to the computer scientist who coined the term , object-oriented programming is about message passing. Message passing means that objects communicate with each other by sending messages.
This sounds trivial, but it affects how function calls are made. The message contains a method name and any associated parameters. The called object then has the ability to handle that method call however is needed. This ability for each object to determine its own response to method calls as needed is called dynamic dispatch. It allows objects to evolve over run time, and respond to messages according to their present state.
Message passing can be a little confusing, especially since most languages in use today only implement one particular kind the method call. Here are a few resources to help you better understand it: Some one asked What is message passing in OO? Encapsulation Closely associated with message passing and dynamic dispatch is the concept of encapsulation. Encapsulation means that code outside of an object cannot directly access its internal data.
This is usually done with a method call.
However, many experts in OO development encourage programmers to adhere to encapsulation principles by not calling properties directly , even in languages that allow it. There are many benefits to encapsulation. Primarily, it allows any details or side effects to be abstracted away from the point at which the property is called. For example, each time a particular value is read, you might want to check when it was last calculated, and re-calculate it if certain conditions are met.
Being able to add, remove, or change that logic in one place at any time, without affecting any of the other places in the code which make the call, is a benefit of encapsulation. Inheritance and Composition Inheritance and composition are two ways that various classes and their instantiated objects are related to each other. In this example, there might be a Person class which defines all the properties and methods of Persons in general name, birthday.
Then a User class would extend the Person class, to add things like a username attribute or login and logout methods. Like the inheritance hierarchy, the compositional hierarchy can easily be several layers deep.