Thinking Objectively and Functionally

As I try to expand my knowledge of functional programming, it’s hard for me to imagine solutions to problems that I solved in OOP in terms of functions, especially where widgets are involved. Sites like Project Euler and 4Clojure are great for exploring the basic functions and methods of manipulating primitive data, but I would really like this resource, which discusses how to idiomatically translate OOP constructs into FPs, with particular attention to determining when and how use the state. (To use a concrete example, what is the best way to implement a piano keyboard in Clojure?)

+4
source share
4 answers

I know two books that can help:

"Functional Programming for an Object Oriented Programmer" by Brian Marik.

"Clojure Programming" by Chas Emerick, Brian Carper and Chirstophe Grand. Chapter 12 talks about how to "translate" object-oriented design patterns into Clojure.

+3
source

what I really liked is a resource that discusses how to idiomatically translate OOP constructs into FP

not to do. This is a classic XY issue.

I hate analogies, but the equivalent in technology may be to say that you have mastered metal structures and want to study plastics by re-creating the same shapes in plastic. In fact, you never want to translate what you know into what you study. What you really want to do is learn how to solve familiar problems using new methods. Returning to technology, good plastic structures do not have the same shape as good metal structures. In terms of programming, solutions built using one paradigm never translate well to another. You have to solve problems from scratch.

+7
source

I do not know a single book that will teach us how to translate OOP constructs into functional ones. Just give yourself time and you will understand functional idioms.

Do not try to match the OOP code and the FP code. The best way to learn a language (spoken language) is to immerse yourself in it and think about it. The same goes for programming languages.

Three years ago, I started learning Clojure. At that time, I don’t even know what Lisp is and what functional programming languages ​​are. I said: “Ah? Really, what is it? Can I do something useful with Lisp? I read a lot, studied a lot and even better, I got a job at Clojure!

Now programming in functional languages ​​seems natural to me, it makes sense. Programming in data structures and functions is all I need. Simplicity!

One thing to keep in mind is that functional programming languages ​​are not difficult by default, and OOP languages ​​are not easy by default.

+3
source

I have not read this book, but it sounds very good for what you are looking for (and I am very pleased with the other Pragmatic Bookshelf books that I read)

Functional Programming Patterns In Scala And Clojure

From the commercial:

Using Scala's statically typed, output type and dynamically typed, state-of-the-art Lisp Clojure, you get a broad understanding of functional programming. For each template, you will first see a traditional object-oriented solution, and then go into a functional replacement in both Scala and Clojure.

Re: your question with the piano, you can find the messages core.async and David Nolen in the user interface design with core.async (specifically http://swannodette.imtqy.com/2013/07/31/extracting-processes/ ) are interesting .

In a blog post, he suggests that the user interface (as well as the extension, piano) consist of three elements: processing the event flow, coordinating the flow of events, and presenting the interface. And he shows that this is a much more powerful abstraction than a typical OOP model view controller. Everything is pretty new though (I don't think core.async is even officially released yet). But if you are looking for an idiomatic way to model a piano, I think it might be so ...

+1
source

Source: https://habr.com/ru/post/1496609/


All Articles