This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Functional Programming in Java: Translating between imperative and functional  RSS feed

 
Ranch Hand
Posts: 109
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of my teachers in programming rebuked my attempt to explain an Erlang concept with what I thought was an appropriate analogy to a Java ThreadLocal. He told me that whenever you are learning a new language you usually start writing your old language in the new language. I want to learn functional programming and I want to know how to translate between the two. Is it a better decision to build something purely functional and get used to functional thinking before going back and attempting to translate between imperative and functional or is it easy enough to translate imperative to functional?

The hardest thing I am doing here is training my mind and my reflexes to think and act functionally.
 
Rancher
Posts: 334
18
Clojure Linux Mac OS X Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As someone who has been programming in Clojure increasingly full-time since 2010, I've seen a lot of people come to functional programming (in Clojure) from imperative / object-oriented backgrounds. The two things that most of those folks struggle with are the loss of variables (immutability) and the loss of loops (switching to map/filter/reduce etc). Some folks are able to make that mental switch easily but a lot of folks really do struggle with it.

To some extent, your teacher is right that it's very easy to fall into the trap of writing code in whatever new language you're learning using the style and idioms from the (current) language that you're most familiar with. I'm going to date myself here and talk about a time when I worked for a QA company that had source code analysis tools: we originally wrote a FORTRAN analyzer and then we wrote a C analyzer (I later wrote a C++ analyzer). We would go on-site with clients and perform analysis on their source code and then present our findings. One class of clients back then was companies that had historically written FORTRAN but had recently switched to C. We could pretty much always spot these clients, purely by source code analysis (software metrics, in particular) -- we coined the term "C-TRAN" to describe their new C code. They were generally pretty good-natured about our teasing and usually knew they were still writing C in the style of FORTRAN (and it usually led to us writing a C Coding Standard for them and helping them transition to better style via our source code analyzer!).

When you're moving between similar languages -- or similar styles of languages -- it's easy to fall into this trap because the new language won't "punish" you for not being idiomatic. When you move between, say, Java and Clojure, you will find those imperative and object-oriented idioms become very ugly and, sometimes, downright hard to write in the new language. That's one downside of trying to learn functional programming while staying in your home language: Java won't "punish" you at all for continuing to use imperative and object-oriented idioms even when you're trying to shift to a functional style.

I think it's wonderful that Java now supports this sort of stuff (I'd love to see full immutable collection classes as part of the core library) and I think you can write some very elegant (and performant!) code with the new features. I do think, overall, it's "better" to learn a new paradigm in a language that is designed for that paradigm if you have that option (and I highly recommend looking at Clojure since it runs on the JVM and has full interoperability with Java and all its libraries).

The Pragmatic Programmer book recommends trying to learn a new language every year -- which is a hard goal for most developers -- and there's no doubt that lessons you learn from each new language will make you a better programmer in your "home" language. A good way to get a sample of what other languages can offer is to buy both of the "Seven Languages" books and work through all of the exercises in them.

To answer your core question: I think it is better to build something purely functional, to force yourself to get used to functional thinking.
 
Kent O. Johnson
Ranch Hand
Posts: 109
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sean, I will take your recommendation. I am looking forward to how my mind will be expanded as I read the seven languages and databases books. I have looked into Clojure and Lisp before on a surface level. I need to dive in deep to really understand their value. I hope the seven languages books will give me sufficient tastes of each language.
 
Sean Corfield
Rancher
Posts: 334
18
Clojure Linux Mac OS X Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a Clojure forum here at the Ranch and I'm always happy to answer questions at length about it!
 
Marshal
Posts: 6015
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kent O. Johnson wrote:I hope the seven languages books will give me sufficient tastes of each language.


In case somebody wonder, I think OP have in mind book "Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming by Bruce Tate".

@OP
What book Sean meant, was "The Pragmatic Programmer By Andrew Hunt"

Some confusion my arose for OP I think, hence tried to clarify, I may wrong.
 
Kent O. Johnson
Ranch Hand
Posts: 109
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras, I found two Seven languages books, the 2010 Seven Languages in Seven Weeks by Bruce Tate and the 2014 Seven More Languages in Seven Weeks by Bruce Tate, Fred Daoud, Ian Dees, and Jack Moffit.

I have the Pragmatic Programmer and highly recommend it to any fellow programmer aspiring to become a greater programmer.
 
Sean Corfield
Rancher
Posts: 334
18
Clojure Linux Mac OS X Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kent O. Johnson wrote:Liutauras, I found two Seven languages books, the 2010 Seven Languages in Seven Weeks by Bruce Tate and the 2014 Seven More Languages in Seven Weeks by Bruce Tate, Fred Daoud, Ian Dees, and Jack Moffit.

I have the Pragmatic Programmer and highly recommend it to any fellow programmer aspiring to become a greater programmer.


Yup, those are the three books I was referring to!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!