I see a lot of discussion about Kotlin and functio...
# getting-started
f
I see a lot of discussion about Kotlin and functional programming. I have a very basic beginner question: Just doing stuff in file-level functions doesn't mean I am doing "functional programming", right?
😆 1
a
no, not on its own. If you want a lot of background on this, https://en.wikipedia.org/wiki/Functional_programming
f
Thanks. Yea, I am already reading articles on it, my question was just for clarification
as these tend to get very complicated very quickly
a
Why not? You can locally use a functional programming style by using higher order functions (basically use
map
/
filter
/
let
etc instead of for loops... additionally you could keep your functions 'pure', meaning you don't really do side-effects in those functions. Finally using the immutable datastructures also help with pure functions style... and if all that functional code is inside a bigger class structure, doesn't make that smaller piece not functional imo.
f
But those are additional preconditions
I'm asking if doing stuff in top-level functions automatically means I'm doing "functional programming"
apparently not
a
Depends, maybe not automatically... C only has 'top level functions' but isn't very functional... but I believe it's more about the style and using functional features... if you're using top level functions and mutate global variables and use a very procedural style, it's not functional if you're using top level functions only, use pure functions, immutable data structures and a functional style, why not?
f
that makes sense, thanks
if I don't follow these rules and just execute some stuff in top-level functions, is it called "procedural programming"?
r
@Florian You will find (and may already be discovering) in practice there isn't agreement on what does and doesn't count as functional. Unfortunately, many don't realize this and believe their definition to be the only right and true definition.
If I may add my $0.02, you should learn about what the different paradigms have to offer, and don't bother with what makes one different from the other. There's always quite a bit of overlap, so saying something is only the domain of a single paradigm is only useful if you're trying to trigger a Holy War.
a
Most of the time functional programming style refers to style where functors/applicatives/monads/whatever are involved. For example, the following code
list.map { ... }
is usually called functional, because
map
is a functor: https://en.wikipedia.org/wiki/Map_(higher-order_function)#Generalization . Imperative style would be:
for (int i = 0; i < list.size(); ++i) list[i] = map(list[i]);
. It's the basics most of imperative programmers know, so it's enough for the start. Some people do not really know functional programming and they refer to anything with chained calls (e.g. builder pattern) as functional, because it's easily recognizable, though it's not true in some cases.
f
Thank you for the explanation!