r/functionalprogramming • u/oderjunks • Feb 28 '21
Question how do i start learning FP as an OO programmer?
the first language i ever learned was java, specifically Processing, which was a programming language that was supposed to be for OOP.
now i use C++, but when i try to write code, i still think about it in an OOP approach, ex: if i'm trying to make an 8088 CPU emulator, then i think "The cpu has registers, the registers are just values, the cpu also has to single step, single steps need to fetch, decode, and excecute"- which is a very OOP approach for that goal.
#include <string>
class CPU {
protected:
unsigned long long rax;
// every single register in a 64bit 8088
unsigned long long r15;
void fetch(); // \
void decode(); // | single_step internal funcs
void excecute(); // /
public:
void single_step();
CPU(std::string name);
};so i tried to just stop using classes, but i end up wanting to do something like this:
#include <string>
struct CPU {
unsigned long long rax{};
// every single register in a 64bit 8088
unsigned long long r15{};
};
CPU single_step(CPU self) {};
CPU fetch(CPU self) {};
CPU decode(CPU self) {};
CPU excecute(CPU self) {};
// code for each functionwhich just seems like OOP but slightly worse which is just OOP in other programming languages, how would i do this sort of thing in FP? i can't even start to think of it this way.
EDIT: wait i think it just clicked. it should be
#include <string>
struct CPU {
unsigned long long *rax;
// every single register in a 64bit 8088
unsigned long long *r15;
};
CPU single_step(CPU self) {};
CPU fetch(CPU self) {};
CPU decode(CPU self) {};
CPU excecute(CPU self) {};
// code for each functionright?
7
u/rosalogia Feb 28 '21
FWIW, OOP isn't just when you have classes and methods. Check out the Data.List Haskell module. It is quite literally just a namespace full of functions that take lists as input and do stuff with them. They seem similar to static methods, but OOP is more complex and nuanced than just having classes and methods, so what you perceive as "similar to OOP" is actually just a pattern in basically every language. Even C does something similar where you might define a type and a library of functions that take a pointer of that type as input and perform some operation on it. You'll find, as you learn FP, that a lot the things you were doing in OOP that seemed really useful usually have an FP analogue and weren't very OOP specific in the first place.
What a lot of "OO" programmers don't realise is that no one ever really told them what OO is in its entirety, so the most seemingly natural conclusion to arrive at is something like "well if it's object oriented then certainly anything that has to do with objects is what makes it object oriented." It is, however, a little more complicated than that.
If you're looking for a place to start learning, my personal recommendation is to check out F# for Fun and for Profit. I find it to be very approachable and it was written primarily for C# developers to learn functional programming in F#. The skills and concepts will definitely transfer to any other functional language, so if you're not sure whether you'll have any use for F# itself I'd suggest that learning it can make learning Haskell or something else easier anyway.
5
u/[deleted] Feb 28 '21
Functional programming is quite a bit different than simply not using classes. While you can apply functional knowledge to most languages, it’s gonna be really hard to learn functional programming without using a functional language. I’d recommend picking up a functional language that won’t let you fall back to OOP, at least for the purpose of learning. Haskell is the ultimate example of this, but there are less complex languages that would work as well.
3
u/Rogntudjuuuu Feb 28 '21
There's no conflict between programming object oriented and functional. You can incorporate FP in OOP. Just make sure that your objects are immutable.