Sign in to follow this  
nimrodson

What is functional programming and how can it help us in game developing?

Recommended Posts

Hi,

 

I would like to know some opinions about this programming paradigm: It is useful? How much? Could have an important role or participation in game design and/or coding? It is complementary, opposite, or totally uncomparable to OOP?

 

Thanks. 

Share this post


Link to post
Share on other sites

In computer sciencefunctional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

 

http://en.wikipedia.org/wiki/Functional_programming

 

Using compute shaders is functional programming. Functional programming and associated languages usually map better to hardware (think FPGAs) than software. Come to think of it, any shader is a functional program evaluated simultaneously on blocks of data.

Edited by Steve_Segreto

Share this post


Link to post
Share on other sites

Using compute shaders is functional programming. Functional programming and associated languages usually map better to hardware (think FPGAs) than software. Come to think of it, any shader is a functional program evaluated simultaneously on blocks of data.

 

OK, thanks for the tip. But, what happens in a higher abstraction level? Which elements of functional programming could be beneficious in game design?

 

Newly, thanks. 

Share this post


Link to post
Share on other sites

For one thing, functional programming typically handles multithreaded situations much better because of the lack of mutable state.  As a result, things that you typically would do sequentially, you can spread out among multiple threads, and you can thread by task.  Also, the idea of functions being first class citizens can be a useful paradigm when trying to move away from an global event manager model (which I have grown to hate).  Just a couple of ideas, there are more.

Share this post


Link to post
Share on other sites
It has potentially huge applications to self-correcting AI routines (and really the field of AI in general). Depending on your functional language, output from a function can be fed as input right back into the same function. For example: Lisp can rewrite and evaluate its own code, because there's no differentiation between "code" and "data".

Share this post


Link to post
Share on other sites
If you come from an imperative background, it's kind of hard to envision what functional techniques are good for unless you try using them.

I use C#, so the only functional stuff I really use are first class functions and lambdas. For me, the benefit of using these is reduction in the amount of state that I need to manually implement.

For example, let's say I want to filter a collection:

Imperative style:
public List<int> FindPositiveIntegers(List<int> inputs)
{
  List<int> values = new List<int>();

  foreach (int input in inputs)
    if (input > 0)
      values.Add(input);

  return values;
}
Functional style:
List<int> values = inputs.Where(x => x > 0).ToList();
In my opinion, the functional case's advantages include:

- The code is shorter, which usually means it's easier to maintain.

- The variable mutation is entirely hidden from the programmer by the LINQ methods. Reducing the number of variables that a programmer has to deal with also means that there are fewer *effective* states that the program can be in (this is a good thing - fewer states means it's easier to prove that it works correctly in those states).

- There is less manual flow control being performed by the programmer. Flow control is another kind of state that a programmer has to consider.

- I can more easily write a function which gets negative numbers instead; In both cases I can copy/paste the whole thing and just change the condition, but in the functional case it's easier because there is less code to maintain.

- The "Where" and "ToList" methods seen here are reusable. They've been proven to work correctly, so the only burden on the programmer is understanding what they do and writing the lambda expression "x => x > 0" inside the Where call. Allowing for reuse of code where *parts of the algorithm itself* are interchangeable is a consistent theme when using functional style.


TL;DR:

OOP encapsulates data, Functional style encapsulates code. Edited by Nypyren

Share this post


Link to post
Share on other sites

For folks interested in checking out FP, it may be useful to know that a new round of a functional programming basics course has just started at Coursera. The language used is Scala, but you aren't expected to have previous knowledge, and the focus is on general FP concepts as opposed to diving deep into the languge. (Though it's a good opportunity to get started with Scala as well; the lecturer is Martin Odersky, the author of the language.)

https://www.coursera.org/course/progfun

 

FP-oriented thinking is useful even for the hobbyist because it leads to more expressive code, cutting down development time, errors and maintenance. And it has become critical for high performance. Single cores stopped getting significantly faster a long time ago, and concurrency and parallelism are now the major bottleneck for most interesting things. FP helps you survive in an environment with those two phenomena.

 

I'm "thinking functionally" a lot of the time even when coding C++.

Share this post


Link to post
Share on other sites

The most useful functional programming in games I've seen recently is Javascript + HTML5.

JavaScript is both OOP and Functional.

Using it functionally is the best way to get things done, (especially IO).

There are many other cool things about functional programming, but this is the only way I can justify it to a hobbyist programmer.

 

Aside from this, you should learn & use functional programming at least once just because it's very interesting. It will lead you to new design patterns. Especially where asynchronous tasks are involved.

Edited by SillyCow

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this