DevDays Europe 2025

Bogdan Kolbik: Why Care About Functional Programming

45:36 · 20 May 2025 – 23 May 2025 · YouTube

About this talk

In this talk, Bogdan introduces the significance of functional programming for software developers who may be unfamiliar or reluctant to adopt it. He begins by explaining pure functions and higher-order functions using Scala, describing how they change programming paradigms and improve code clarity. Bogdan emphasizes the benefits of immutability and referential transparency, which simplify debugging and enhance predictability in code execution. He also discusses various functional programming constructs, such as monads, and their applications in error handling and asynchronous programming. The speaker encourages developers to integrate functional programming concepts incrementally into their work, underscoring that these approaches can help make code more manageable and comprehensible.

Full transcript

[Music] welcome everybody to the dev days conference you are in the hall for languages and Frameworks and the first session that we have today is called why care about functional programming now see I use C and net in my programming life and um many years back I was in a company as in a Meetup and that's the first time I heard about functional programming and these guys

were using FSH to do take care of their continuous integration and continuous development and I'm like this sounds highly Ma ma matical very nerdy and how do they even do this and uh today Bogdan is going to tell us why should we care about functional programming and I'm very excited because he does very interesting stuff with vehicle routing using Scala and rust and guess what not just

programming he also does skifi stories in his own blog so I'm looking forward to Bown session on why care about functional programming so here we go Bogdan the stage is yours hello folks I want to tell you why should you care about functional programming this talk is intended to be an introduction to functional programming to people who already saw a couple of introductions and they have vague

idea but they're still not impressed why should they use it but yeah if it's your first introduction then don't worry I bring everyone to the same page and first let's see who am I yeah I'm a software engineer at Moya we do ride pulling services for cities including autonomous driving and to give you some scope of my experience my second programming blog week ago turned around 10

years old another disclaimer there will be a lot of quote examples in this St and all in scolar language it's mostly because I like it but also it can do both functional programming and imperative programming that's how I will call nonfunctional programming today and it doesn't enforce the single way of doing it so it works as a gateway drug for Java developers into functional programming but also

you don't have to know Scala to read these examples because I will use very limited syntax so I don't have to explain a lot of of topics stuff and let's all get to the same page this is the typical introduction to programming first look at this this is a function it's wonderful it's what we call a pure function uh it it means it returns a value and

does nothing else it doesn't access or modify the global scope it doesn't throw exceptions make Network calls or nothing else just Returns the Val but what if I tell you it can be even better like this so what changed now before it was a function definition now it's a value definition of a value with type function and it's the same function and it might seem that there

is no real big change but the contextual change is huge because the previous function it was like a button glued to the wall you can p push it or you can go away and that's all you could do with it but the value which is a type of function it's like a button on a remote you can push it you can take it around with you you

can wrap it in a box you can put it in a pocket or you can give it to someone else from programming perspective it means you can pass it around and you you can pass it to another function or you can return it from a function the functions that accept or return functions as arguments they called they are called the higher order functions and let's look at

the most popular one it's a map that is a generic function that re receives a list of values of any type and the function that takes one value of that type and converts it to another type and the whole map function returns a list of that another type and as implementation it just calls and build built-in map function on a list because I was lazy to implement

it but yeah together it works wonderful we can have a list of numbers and we can have the same square function and now we have a list of squares but yeah map is not the only high order function you might see there also a flat map and we turn every value into a list of values and we deceive a list of lists but then we flatten them

into a single list so now we have a list of both numbers and squares and yeah it quite nice it shows you exactly what you need to achieve so if you make the same code in imperative style it will be something like this we need to create a mutable list buffer and we can need to hit the rate over every number in our original list apply our

function and then append everything to our result buffer and this also works but it's much more tedious it's like in the old joke when a guy comes to a doctor and says you know doctor it for me it hurts when I bend my one knee and then I bend then I I straighten up my knee and I bend another leg I put the hands down and I

straighten up another leg and then I stand up and doctor says well that sounds quite a weird sentence sequence of actions just don't do it and guy says but how else can I put my pant on and yeah that's often the difference between declarative and imperative code with imperative you need to list every action you need to do and after this you don't see that big idea

why would you do this but yeah functional programming has more for example you can compose functions and compose function it's a generic of two types that take that takes two functions and creates a new function by taking result of the first function and passing it as an input to another function and it's quite a paradoxical thing because it changes the world a lot and still it's a

function I never used in production because it's much easier to just call one function after another if you look at it yeah you can create a function better is 42 by composing functions is 42 and how do you do fellow kids and we get a new function that is from into string and that's quite interesting and important thing about it because is that compose function knows nothing

about functions you trying to compose except the fact that they have a single argument and single result value and result of one function matches the input of another but still compost might be implemented somewhere inside the language and you can use it to compose your application functions and if you think calling one function of another it's a code pattern you need to remember how to do this

you need to be able to write this pattern every day multiple times and maybe you are even asked in a tech programming interview how can you call one function after another yeah that sounds quite absurd when we are talking about something like calling one function after another but you could see today that another code patters are also can be changed into code for this thing and you

don't need to remember them because there is a function that remembers it for you also recursion is good now just remember it and that's how you can do a functional programming in two steps first you need to write some functions and then you compose them into the program that's all usually that's how typical introduction to functional programming ends and after this you think well so what it

might seem cool it might be even impressive but then you get back to your day-to-day work you need to fix some bugs you need to write new features and does it doesn't look like you feel that you need to compose some pure functions for doing this so why would you go into functional programming and it's mostly because programming is hard and we should use every opportunity to

make it easy and for example let's look at this at this code we have a class person with two types with two properties name and birth date case classes just a special type of classes that are defined for story data and it has a proper method get H that Returns the that has some implementation it's not interesting right now and then we have a code like where

we create a person with Nam Joo that was born on 1 January 2000 so it's easy to get their age and they try to age and print it and then well what do you think would be the output of this quote you might think georgo is 24 years old but when you call we thought it would be judge but for some reason it's still let's see why

it happened for this we need to get in look into the get H function and it looks like this it changes the property of the person when we try to get its H and again this TW line example that sounds very absurd no one would do this so let's make it much more realistic like this yeah now it's quite realistic code then okay it's a problem we

can uh resolve it by making our properties immutable we do it like this in Scala it just means remove the VAR prefix word from before the property definition so now it should be fine right let's see yeah it's not right now we need to look into the H function again to see happens and this see the comment with the famous last words something you often see during

the and function definition never tells you whether it throws exceptions or not there are some conventions saying you need to write it in the command but if it's something like five functions deep uh you cannot rely on commments of every function that is above it that it will pass the exception notification properly so yeah let's try to get rid of this exception well with another comment that

I promise it doesn't exception should be fine now right it should return what we need and yeah it Returns what we need but also it drops the database and yeah if you look into implementation yeah that's what it does exactly uh we can fix it again by just enforcing get H to do what we need and that's usually the problem with imperative programming you don't know what

to expect from a function unless you know how it's implemented and it means that you need to keep its implementation in mind for you are calling it which is quite easy when you have a oneliner function but let's imagine you are updating the main function of your application right now it means that you need keep the whole application in your hand and if someone is able to

do this then the whole team depends on this all developers who who know all whole the whole application and know what to expect from the simple call and yeah but now it's fine it should be fine right so we can even make a test that validates out code and C it and validates that it produces the string Georgia is 24 years old and it will work fine

for about seven months and then it will suddenly fail and it it's weird that your test might fail start failing just depending on when you run it and yeah it's mostly because this function depends not on the on the birth date of Joo but also on the current date and to make it proper we should also import current date as an argument to this function so it's

reliable now we can you know what to expect from this function and it's good testable but well it's also pure now achieved the pure property of the function just by making it more reliable and also we achieved one property that matters a lot to functional programmers it's referential transparency it means that you can replace the function call with return value of that call and no one will

notice and we care about it so much because with referential transparency it doesn't matter when the function is called where it's called or if it's called at all and that kind of explains why functional programming became more popular with asynchronous parallel or distributed computing because pure functions are much easier to make them asynchronous but also they're predictable you don't need to know how function is implemented to

know what to expect from it you just need to know this oneline function declaration with input types and output types so functional programming is knowing what function and thinking only about the code in front of you just like what and yeah we show that it show that avoiding side effects makes programming easier but on the other hand we want side effects the only useful things our programs

do are side effects and getting rid of them sounds like counterproductive so let's see how we can do side effects without making side effects and for this we have a simple code with side effects that GS it takes a name as a string and it creates a string hello name and prints it to output and let's see how we can do it well first thing we can

just split logic and side effects now our GD just creates a greting string and we print it later and it doesn't actually solve the problem it makes code more testable it makes code nicer but our problem is still there because someone needs to know that this string needs to be printed and we just push the problem one layer higher so you can do something else for example

we can tell make the function that says print this command so knowledge that this string needs to be printed is still in the function and we can implement it later for this we can create a value that says with string and it should be fine and what if I tell you it's a real approach probably used by functional programming because if you sprinkle some monets on top

for composability it's a fre Monet approach I never saw it in production but books say it was popular some time ago it can do easier things for example we can do better dependency injection uh where we make a printer type something that accept a string accepts some string and prints it and we can expect this thing as another argument to our gr function yeah it looks like

dependency injection but the biggest reest here is the type of our grid function it doesn't Define return type it just returns whatever or printer it returns and it means if you think is our grid function pure now you cannot say it completely depends on the printer function so our grid function can work both with uh pure functions and pure functions and doesn't enforce any type and yeah

it's also popular sprinkles some mon on top for posibility and it turns into Tas Final Approach and it's the top notch that best fanciest thing you can do in functional programming right now but you can also make it easier and let's make it easier another way is we return a Lambda function so grid returns a function that if you call it brings our green and this looks

like a Duty trick because we still have this printer but on other hand our grid just returns a function it doesn't do anything else so G function is pure and that's weird but yeah it's real method used by functional programmers compossibility and you get the IM Monet approach and that's the main Workhorse in functional programming that does all the heavy lifting of side effects in our code

and I want to tell you bit more about I but first I use the word mons multiple times now and you might be scared so let's talk about let's talk them and for this let's compose another program that prints a question what is your name reads a line from input and then prints the greeting and for this we use our iot type which is now not just

a function but a case class that ws this function and we have a couple of functions that actually return the S one of them is R name that Returns the string and another one is print that returns a unit which is an interesting type uh if you think for example the type bite has 256 possible values the type ban has two possible values through false the type

unit has only one unit and it might seems useless but it gives it nice properties for example you can create a value of type unit at any moment because it's only one type you know what you want know what to create and also you can drop this value without losing in any information because it's useless type and to use it as a placeholder type for things we

need to pass some value but we don't care about it for example our print function doesn't return anything but we need but ion needs to return something as a result of a side effect effect so we can unit and we can try to compose this two functions into a program that we need for example we print read name and then try to write the greeting and yeah

you can see the problems right now first the first two function calls will be just dropped because no one executes them only the last print will be executed but also we need to take the result of read name and pass it as a part of greeting into our third command and you don't know how to do this we can return a list of iOS but still it

doesn't fix our problem with passing name from one thing to another another so we need to somehow compose this three iOS in like one big program IO that does everything we need and then returns nothing so we need to we need a way to compose an i of some type and a function that takes this result value of the first a and creates it into another a

and then we need to get as a result I that makes both side effects and Returns the result value of the second one and let's do it let's call it a flat map and it creates a new iio that makes the first that executes the first side effect then processes the result value and executes the second side effect now we can compose our program like this so

we take result of the First Command flop M it into second command and flop m b it into third command and we have access to the name value in the middle so we can actually turn it into a grid uh into a griding and with this we almost invented a Monet so we can get to the definition mon is a generic type you can change using flat

map just don't tell it to FP because it's not a right definition it's just a useful one the right definition says that there needs to be a bit more properties to monit and they need to follow some more laws but these laws they just make sure that you can FL map stuff without it exploding so it can reliably be done and this explains why it's so hard

to explain what is a Monet it's so way to change things that are implemented but by other types and what it does directly depends on what these other types do so good way to understand what is a Monet is to actually use it some commonly used monets and see what they do for example one nice Monet is a arrays something like list so every array type means

there might be manyu values of a and when you turn one way each value of your original list into another array and then you flatten it you get a bigger list of all the results and yeah that's how you can change lists let's look at other mods for example option also known as maybe of something and the thing it do does it says I can have a

value of a or I can have no value it's a functional replacement for nullable values and you can use it to chain operations that might return no value for example if you have three operations and each one might return no value instead of every time checking whether it returns something and failing eagerly or not you can combine them in a mon and mon do this eager check

eager failing and check in for you so in result you have one option of result type next thing is result also known as either either is a more generic definition uh result is more use case specific and it says I can have either result value of a or an error of e and it's a replacement for exceptions you might fail and then it will return the first

failure and not run any other uh operations that should go after it or also there's a mon I that says there's a operation that does side effects and returns a and yeah you can use it to change side effects as you can see there are quite different things we can change using mons and this these are only the most popular ones and there might be many more

for example there might be mons for Json encoding decoding or and stuff like this so one more thing about functional programming is it's using the same pattern for many US cases and it's nice and the functional programming languages also introduce some synex sugar uh it's like do notations in h scale or for comprehension in Scala so you don't have to write do flat map all the time

so you you have something like a left Arrow left pointing arrow and it's the same code it does the same thing so writing mon is also easy if your language supports it but again so what I just told you quite confusing things like functional programmers hate imperative programming but they want to reimplement something that looks like imperative programming using mons like with all this mind juggling and

that's quite weird like we're going circles it's not the first time when someone in the tech industry said that something is bad so we reinvent an alternative and Al alternative is the same thing but reinvented by them and branded differently but no I'm telling you we're not going circles it's more like the heroes journey in the monomi when we go into the special world uh see the

abys which probably is mon but then we return to our original point is special knowledge and superpowers and superpower of iOS is operations of values now and that means patterns are C you can combine operations into biggest operations just as you combine values into structures and it turns design or programming patterns into function calls and it helps with stuff like error handling as sync and retrival operations

so let's look how I code might look and here I will be using the syntax of Z library for Scola and it's not the only library for Scola maybe not not even the best iio library for Scala and yeah in different languages they look different so you don't need to remember a specific syntax just see what is possible for example we can write a synchronous code which

looks like this and it looks exactly like synchronous I operation because the difference between synchronous and asynchronous it can be stored within the mon implementation we as a programmers don't need to care about this and for more synchronous code for example we can wait for two operations then return a combined result and here we do it just by zipping two iOS and you get IO of two

values you can race between two or more operations using the race function or race Sol function uh yeah it's nice uh I also write some Rust and we use Tokyo library for this and there you can also do race operation but you need it for this you need to call a macro that does code generation for you I also know that in go it's a select statement

part of the language that does it for you and Scola is just a function call means we did not need require the support from the language designs designers to do it and let's look further timeouts yeah just do operation and call a timeout again go you achieve this by doing the rise between operation and timer which might sound poetic but I think calling the time out method

declares better what your intention is we can also work with resources for example when we open a file we should close it after operation disregarding whether it succeeded or failed and we can do this by colon acquire release with with three functions one that opens another one that closes and third one does operation with the file and then it will take care of closing the resource for

you you just declared everything you need and if you need to synchronously make a stack of resources that depend on one another and just later have an operation that depends on all of those resources as you can expect there's a mon for this let's go further error handling we can make an ation and catch all errors and do something for them and return a fullback value and

yeah that sounds like try accept but the is thing is it's a function implemented by language designers doesn't require any support from the language and it means that there are around 10 or even more error handling functions tailored for different use cases something you will never have in the language l wants to be bloed then we can for example follow back to other operation like do operation

a if it fails do operation B if it fails okay we have a failed operation we can do some rri just by R and we can have exponential delays for example let's try to do operation a and if it fails R try up to 10 times with exponentially grow delay something we often do with network calls I tried to implement it using imperative programming so we need

to catch an error for this we probably need try catch and then what do we do when we get an exception we need to call operation a again somehow should we do it recursively but then we need to also pass some state for example number of the r i and I don't want to blot our function definition with this internal or we can do some Loops but

again it means add in a loop where it shouldn't be so what should we should I do well my best idea was like this and it's also horrible yeah and it works just because the operation a is a single line function call if it was something like five lines of logic it would be unbearable so I did what I could do and as GPT for this It

produced like 50 lines of a horrible code that tries it does the recursion then it also forgets immediately the operation result it increases time out before waiting for it but okay after heavy editing this should be this should work and that's how you do it in functional way you just say that you need retry and you define a schedule for this yes you need to know how

to define a schedule and this is Library specific and probably it has the same ugly logic with loops Rec recursive calls inside but it's implemented at tested by someone else for me I see a declarative code that exactly tells what I need to achieve so another benefit of functional programming is turns Cod turns into function cods and you don't need to remember how to implement them because

functions you call remember all for this and also it means in functional programming you can use all the bells and Vel of fire libraries instead of implementing them now category Theory we're 35 minutes into the talk and I did not talk anything tell anything about category Theory yet and I won't don't look into it when I wanted to get into functional programming I thought okay I need

to learn about functional programming that means I need to learn about category Theory it was a mistake I was confused by two things at the same time and category Theory didn't provide any benefit for my usual day-to-day work so it's very useful especially when you are defining a language or you're making some kind of library but if you're just getting into it you can just learn functional

programming everything useful from category theory was already adopted there and it's much easier to understand when it's written by programmers for programmers and get into category Theory only when you feel that you are ready for example I'm not ready yet and we're getting close to the end of the talk and let's look at some conclusions first functional programing is not for sure enough really don't do it

try no one gets impressed they just talk joke about category from the function and go but on the other hand functional programming is not hard I hope there was nothing inherently complicated in whatever I showed you today it's just well it's might be scary to start because it uses a different vocabulary but it's all simple things and mostly dirty tricks that make your life easier and then

next thing is functional programming makes called easy to work with because you there are less things you need to keep in your head you need to remember on the function definitions of the functions you call and not implementations of these functions and implementations of every function that call and as we also could see applying common sense makes code closer to functional programming if you want to make

your code more testable or more predictable you make it more functional like also and as a result referential transparency makes things predictable and with functional programming that sign patterns become functions implemented and tested by someone else so you don't need know how to implement it it's wonderful and good use you don't have to rewrite everything in ccal right now you can start re using ideas from functional

programming in your code step by step and in each step it will be useful for you even if you don't go the full length and first thing is you can prefer mutable values everyone knows that Global State making it mut is a bad idea but still people often pass mutable references to functions and it's still considered okay and it shouldn't be it makes code much harder and

unpredictable don't do it then prefer declarative style it's I see declarative style as a scale so whatever I gives your intent more then it's better than the thing that just tell you code what to do and that's it then strive for type safety I didn't talk about it much but yeah if the only thing you need to know about the function you're calling is uh types of

inputs and outputs you might want to express as much uh implicit logic into this types as possible and type safety helps you with this and it's also thing take care about in functional programming then you might look into Mon like option and result to use type safe stuff instead of uh nullable and exceptions you can try to avoid and isolate side effects and it will help making

your code easier and then the hard thing try turning operations into values it it's hard but it's us very useful the last step is explain mon to everyone it's just part of life cycle you cannot Avid it yeah thank you and in the end I want to point you to some resources for learning more about functional programming first of them is the Scala exercises website and it's

interactive exercises and it's intended to make you learn about scalar libraries but because they Implement some content Concepts from functional programming it also works nice as an interactive guide into functional programming can really suggest it the next thing is the red book functional programming in Scala and it is good it can be also overwhelming so please don't start with it just get to it once you already

started you have some idea what you're doing and now you want to deepen and systemize your knowledge but yeah star with it overwhelming and the third thing is uh book functional modeling domain modeling functional it's more practical it's also not specifically about functional programming it's a book about domain driven design in FP but they implement the application using functional programming and author says that domain driven design

works very good together with functional programming and I can agree so it's a good practical thing to start and that's it thank you and let's questions to Stage hey thank you so much ban that was a real fun session and uh I think you had a nice balance of you know the technical side and the useful part of uh functional programming in your session and I learned

a lot and you know what I also liked when you kind of closed up and you talked about some of the domain driven modeling which was done using fop so I think that's something I can still you know possibly get trying a bit because it's much Clos from my background from C and so on I am having a look at the hall one chat um I think

there is a small there's one or two minutes delay between our presentation at streamyard and at Pine um so let's give it a minute or so to see if any questions come uh none as of now so ban let me ask you a question so I uh I think for the Scala and the rust people there is a very clear um move into functional programming what about

you know I come from a an older generation of programming who've been using cop and Java what about the Java world how do the Java programmers move into functional programming what would be the easiest shift for them well for Java I can say use color it's interoperable with Java code so you can move gradually and it compiles to jvm so it's the best way but also it's

like if you're not coming to functional programming functional programming is coming to you because uh the language designers look at functional programming and still the most useful and easy to implement Concepts into their languages so you have Lambda function functions in Java and they came there then not just because they decided to want it they stole it from functional programming and that's good so you can see

fun programming is defin even the languages that opposed to it nope very much I mean Lambda functions is also there in C so I mean I like the way you you put it you know if you don't come to functional you I'm going to quote you B I'm going to quote you on perfect so ban you're like session done are you excited to stay in the languages

and framework Hall are you moving to other Halls do you already have a few sessions bookmarked well the next one should be keynote it sounds is the keynote on next well the main hole I me this okay perfect thank you very much bdan thank you for kicking off the day on the languages and framework Hall yeah have a nice conference see you oh I need to say

thank

From event

DevDays Europe 2025

20 May 2025 – 23 May 2025

All event videos
Back to Watch