• Post Reply Bookmark Topic Watch Topic
  • New Topic

Functional Programming in Java: imperative to functional programming transition

 
Thillai Sakthi
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have been imperative programmer for years. How do I transition to "think and program" like a functional programmer ? Some of the symbols and acronyms are new to me in the FP world. Is there a kick start guide in your book or you suggest any ideas ?

Thank you,
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to learn ML, as a beginner in FP, it should help you more than trying to do functional programming in Java ;-)

http://www.thefreecountry.com/compilers/standard-ml-language.shtml

http://sml-family.org/

http://www.smlnj.org/


 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 96
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Yes, there will be something called "road map to functional programming". In short, here are a number of progressive steps. This is only a proposition and does not mean in anyway that you should be fully compliant to one step before going to the next. Make you own experiments and adapt as suitable:

1) Insure that all methods are either functions or intentional effects. Try not to mix them. Write your application in two parts, one being a library containing only functions, the other being a client of this library, only responsible for effects, and not for any computations. Functions may be extensively tested, and it is even often possible to demonstrate that they are correct. Effect are much more problematic. This of course, does not require a functional language nor any knowledge about FP other than what is a function. This step may be achieve by any programmer or programmer team without changing anything to the current coding rules. The only problem is about what to do with exceptions. This step roughly correspond to chapter 1 to 4 of the book.

2) Start using contexts for you computations. The most useful context is one allowing to isolate computations in order to handle the exception problem. Java 8 does not have the necessary tools for this. Optional allows handling the absence of data (NullPointeException, IndexOutOfBoundException and the like) but it only allows doing it by ignoring the errors. You should either write your own tool, or use one from an existing library. In my book, you will learn to develop and use the Result class for this kind of use. Note that it is possible to implement such a class in terms of Optional, although I would not recommend going this route.

Note that this step implies that you should understand some intermediate concepts from functional programming such as how you can compose such contexts. Accessing an "in context" value from outside the context is of course forbidden, so you should never call Optional.get, for example. (This method should not even exist.)

With such a tool, you can completely remove exceptions from your code and make all functions in you library pure.

This step corresponds to chapter 5 to 8, 10 and 11 of the book.

3) Make all your variables final (making your components immutable) and stop using mutable components from existing libraries, replacing them with immutable ones. This may involve creating your components (such as List, Stream, Tree, Heap, Map) or using some from existing functional libraries. In my book, I show how to implement such components, which is important to understand how they work and how they are supposed to be used. This does not mean you must develop all the components yourself. You may prefer to use those from an existing library.

This does not correspond to a specific part of the book because it is applied from chapter 1.

4) Learn and apply some advanced functional techniques. This is where one may argue about Java being suitable for functional programming. Java lacks a number of functional tools and/or syntactic sugar that other more functional friendly languages offer, but it is not a good reason to ignore advanced concepts. It is probably as important to understand what you miss by using Java than to master applicable functional techniques.

BTW, this is a technical road map. To switch from imperative to functional programming using Java, you also need a particular attitude. You must not fear doing what others do not want you to do. Do not listen to people saying "This is not the way Java was intended to be used" or "You can't do functional programming in Java because Java is not a functional language" (There is no such thing as a functional language. Only languages that are more or less functional friendly.) And you must forget about a number of so called "best practices" because they do not fit functional programming (return in a catch block? specific exception you shouldn't be catching? the list is quite long.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!