Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce Functional Programming on Scala

Tags:

scala

I'm starting to learn Functional Programming and would like to do so with Scala, not Haskell or Lisp.

But some people claim that learning Scala as the first functional language slows down your learning of Functional Programming, because Scala allows you to program both ways, and one tends to program the procedural way when confronted with a hard problem.

How can i make sure that 'm programming in a purely functional way? Maybe, due to not being able to properly distinguish both styles, I'll inadvertently program procedurally).

I know, for example, that I should only use vals and not vars.

like image 502
Márcio Paiva Avatar asked May 07 '13 11:05

Márcio Paiva


4 Answers

The other answers have made some good points, but for an attempt to quickly get down some guidelines, here's how I'd start:

Firstly, some things to completely avoid:

  • Don't use the var keyword.
  • Don't use the while keyword.
  • Don't use anything in the scala.collection.mutable package.
  • Don't use the asInstanceOf method.
  • Don't use null. If you ever come across null (in someone else's code), immediately wrap it in a more appropriate datatype (usually Option will do nicely).

Then, a couple of things to generally avoid:

  • Be wary of calling anything with a return type of Unit. A function with a return type of Unit is either doing nothing, or acting only by side-effects. In some cases you won't be able to avoid this (IO being the obvious one), but where you see it elsewhere it's probably a sign of impurity.
  • Be wary of calling into Java libraries - they are typically not designed with functional programming in mind, and will often require you to abandon the functional approach.

Once you've avoided these things, what can you do to move your code to being more functional?

  • When you're performing direct recursion, look for opportunities to generalise it through the use of higher order combinators. fold is likely your biggest candidate here - most operations on lists can be implemented in terms of a suitable fold.
  • When you see destructuring operations on a data structure (typically through pattern matching), consider whether instead you can lift the computation into the structure and avoid destructuring it. An obvious example is the following code snippet:

    foo match {
      case Some(x) => Some(x + 2)
      case None => None
    }
    

    can be replaced with:

    foo map ( _ + 2 )
    
like image 175
Impredicative Avatar answered Oct 06 '22 10:10

Impredicative


I dare say that your goal is already misleading:

I'm starting to learn Function Programming, and I really wanna learn Scala, not Haskell or Lisp.

If you are really interested in learning concepts of function programming, then why not use a language such as Haskell that (more or less) does not allow you to use procedural or object-oriented concepts? In the end, the language is "just" a tool that helps you learning concepts of FP, you could just as well read loads of papers about FP. At least theoretically, I think it is usually easier to learn concepts of computer science with concrete tools at hand.

On the other hand, if you are interested in learning the language Scala, then why not use all features that it offers, regardless of whether they stem from the FP or the OO world?

In order to conclude with a somewhat practical advise: You could search for FP tutorials that use Scala or for blog entries etc. that describe how to realise certain FP-concepts in Scala and try to follow them. This way, it is less likely that you make use of non-FP concepts.

like image 36
Malte Schwerhoff Avatar answered Oct 06 '22 10:10

Malte Schwerhoff


You don't buy a Ferarri to deliver furniture. Scala's fundamental strength is the fact that in your words, it goes both ways:). Whether or not you are programming in a functional style is decided by the techniques you use.

The best thing you can do is thoroughly review fundamental concepts of functional programming and seek the appropriate Scala implementation of the respective concepts. But if you want to program purely functional style, then go for Haskell, Lisp, Erlang, OCaml, or whatever other purely functional dialect.

Functional programming

Introduction

Functional thinking

Scala

If you want to learn Scala, then make sure to include both OO and FP in your learning curve. Lambda expressions + OO concepts + syntactic sugar made possible by IMHO the most advanced compiler on the face of the planet lead to something quite amazing. Take advantage of it!

like image 33
flavian Avatar answered Oct 06 '22 09:10

flavian


I think learning is non linear process, it helps to see lots of ways of doing the same thing, also be opportunistic and use any learning resources that are available for you. For example Martin Odersky the creator of Scala offers a free course called "Functional Programming Principles in Scala" https://class.coursera.org/progfun-002/class/index there are some very high quality video lectures, and some really good assignments where the automated grader will tell you that your code is not functional enough and you loose style points because you are using var instead of val

I think the thing you want to focus on is learning the Functional Programming Paradigm and for me learning a paradigm is about learning what types of problems are easy to solve in one paradigm and are hard to solve in another paradigm. Focus on the paradigm and I think you will find that learning both about Haskell and Scala will teach you the functional paradigm faster, because you will be able to ask the question what are the common features between Scala and Haskell, what are the differences .... etc

like image 36
ams Avatar answered Oct 06 '22 10:10

ams