Programming in a functional way series

Intro

It’s been a great experience learning Clojure, lisp and functional style programming. This is just a short list of some principles I’ve learned about Functional Programming (FP) design.

What you bring from your previous paradigm

It’s only natural to bring your previous experiences along with you when learning something new.
Usually it’s a positive, however coming into the FP with with my OOP background has felt at times  a hinderance.

Ideas such as

  • SOLID principle
  • DRY
  • Design patterns e.g. Strategy, MVC, Factory.
  • Classes and objects
  • Methods

But not all of these were a hinderance. For example Functions are simply methods. Well that’s not a 100% accurate.

A method is associated with an object and a function is independent of an object. Then again it depends if you’re using C++ where it depends whether you’re in a class.

I feel DRY principle can still be applied in FP, in fact DRY can probably be apply in so many parts of anyone’s life outside programming.

Functions as parameters

  • preserve the distinction
  • remove the duplication

OOP uses interfaces to force you to program to a contract.

Functional version of a strategy pattern is kind of built in. You simply pass in the function that you would like to call

  • Function composition – they’re like Lego piece

  • We don’t care how functions work, just the input and the output

  • No Side effects – Given a certain input, no matter how many times that repeated function is called, the function should return the same result.
  • Immutable values – Variables cannot be changed. Period. So really they’re no longer really variables, as they don’t vary. They’re really constants.
  • Higher order functions – Functions are treated as first class citizen, so they can passed in as an argument.
  • Currying – Transforming a function that has 2 ore more arguments into a function that takes only one function.
  • Recursion – Traditional looping
  • Lazy Evaluation – Not processing values until the moment that you need it. An example is when you’re generating a list, the result would be evaluated until called by another function.

Meanwhile in Ruby land

Side effects

def square(int x)
x * x
end

A small example of a function with no side effects. If x is 5, then 25 will return, every single time. It’s result only depends on it’s input arguments and nothing else. In addition it doesn’t modify anything outside of it’s scope.

def processEmailMessage
latest_message = Server.latest_message
return process(latest_message)
end

The method signature indicates there’s no input, which is a bit deceiving. It does have an input. A hidden input, the latest message. Calling this method more than once will cause side effects. It has an effect on object(s) or variable(s) outside it’s origin intended purpose.
Another aspect of this function is that if we knew the inputs of this method like below then if we were to send the same latest_message object as an input parameter, then we should get the same processed message as an output.

def processEmailMessage(latest_message)
return process(latest_message)
end

Functions within functions

In ruby you can do this…

def my_function
def returned_function
end
end

my_function

=> :returned_function

a function has been declared within a function! When we call “my_function” we get a symbol back but not the function. Strange behaviour I find. I would expect to get a function back, so maybe ruby doesn’t treat functions as first class citizens.

But what about high order functions?

High order functions

Simply put, when you pass functions into a function’s parameter. Let’s look at some examples.

def name
"Ced"
end

def upper_case(name)
name.upcase
end

upper_case(name)

We passed the name function into the upper_case function and Ruby is happy to accept that. Can we do the same thing in Java?

First class object

Here are examples of functions as first class objects in Ruby and Java

public String name () {
return "Ced";
}

public String upperCase(name()) {
return name().toUpperCase();
}

Nope, Java doesn’t accept functions as parameters, won’t even compile. But is that the end of it, well not really.

Lets look at adding two numbers together but in a more functional way than just “a + b”….. with functions!!

In Java we can write the following…

interface IAddTwoNumbers {
Integer call(Integer a, Integer b);
}

IAddTwoNumbers add_two_numbers = new IAddTwoNumbers() {
public Integer call(final Integer a, final Integer b) {
return a + b;
}
};

add_two_numbers.call(2, 3); //output is 5

Here we’ve done something neat, we’ve put a function into a variable which can then later call. So whilst it’s possible in Java it’s possible, it’s not as straightforward as in ruby.

add_two_numbers = lambda { |a,b| a + b }
add_two_numbers.call(2, 3) //output is 5
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s