I am curious, if purly functional, serverside kotl...
# functional
c
I am curious, if purly functional, serverside kotlin has any effects, except from confusing any java developer and making its developer irreplaceable. Any effects on cpu/ram/ other performance issues?
g
I would not expect any real significant performance difference. But about the confusing part: It is shown that functional style is in general easier to read / understand than OOP style. What do I mean with this? Functional Style • immutable data structures • programming without side effects • using list api like
map
,
filter
and so on OOP Style • Classes/Objects with distributed, mutable state • using
for
loops for list processing this said, one can easily program with Java in a functional style and one can use kotlin to write OOP. So it is really not an issue of language or framework but of coding style.
K 5
t
Immutability can have an effect on memory usage (there is a reason you use a StringBuilder instead of concat strings inside a forloop with a lot of elements). That being said, because things are immutable they are safe to have multiple pointers pointing to them, that can sometimes be beneficial (thats also why strings are immutable). So the answer is as always.. it depends. In general for most software it is not noticable, so my suggestion is usually, go immutable first and once you notice performance issues use mutable state (but try to keep it withing function scope, so immutable in, immutable out, but inside the function its ok to use mutable state). About confusing developers. I had this discussion a lot. What is going on here is the simple vs easy discussion (for more about this, watch the talk Simple made Easy). Like @Goetz Markgraf says is correct, these concepts are objectively simpler. BUT if you are not familiar with the ways of thinking it can indeed be hard. The thing is that something hard can be made easy with learning and guidance (which in this case is usually very beneficial to know about these concepts (thats why all my conference talks are about these things) even if you choose not to apply them), while something complex will never become something simple. So in the end I would say do try to learn what functional style and try to apply it. It will make people better programmers even if they choose not to use it (its not the best solution in ALL cases).
1
g
Don’t get me wrong, one can write functional programs that no-one understands if he or she throws together too many specialties (higher order functions, combinator etc.) That’s why I stated, what I mean with “functional style”.
u
a good approach is to use a mutable data structure for building stuff and cpu intensive inner loops where you are sure there could be no external references to them but then export them as immutable (or read-only) structured.
❤️ 2
👍 1
also using some kind of typed effects to "mark" impure actions is really beneficial in avoiding bugs on large code bases
👍 1
g
@Uberto Barbini can you explain what you mean with “typed effects”? It sound interesting.
u
some kind of typed algebraic data that allow to safely work with impure action. The most famous is the Haskell IO monad, but there are others. In my book I propose something similar to read from db in a transaction. Arrows has its own. Other languages like Roc or Unison use something different for the same goal.
the main point (imho) is that you cannot simply use a singleton to access db or net in a "vanilla" function. The type system should make that explicit.
🙏 1
c
Learning from giants
r
Arrow arrow just uses the fact that a function marked with
suspend
needs a coroutine scope to execute. In this sense, it's very similar to the
IO
monad in the Cats Effect library. Take a look at this article for more details: https://arrow-kt.io/learn/design/suspend-io/
arrow intensifies 3