I'm new to functional programming and I'm using Ko...
# announcements
I'm new to functional programming and I'm using Kotlin with arrow functional library. I would like to convert below function to pure. Each 
 call returns a valid string and it's gets appended to the mutable string variable 
. As far as I know, FP functions should not use any mutable values. So how would I replace those string appending lines?
Copy code
private fun stringifyValue(): String {
            var returnString = String()
            returnString = returnString.plus("=")
            returnString = returnString.plus(func1())
            returnString = returnString.plus("+/")
            returnString = returnString.plus(func2())
            returnString = returnString.plus("@")
            returnString = returnString.plus(func3())
            returnString = returnString.plus("#")
            returnString = returnString.plus(func4())
            returnString = returnString.plus("%")
            returnString = returnString.plus(func5())
            returnString = returnString.plus("^")
            return returnString
You can think of it in a way whereby each function call, given the same parameters, should always return the same value and does not change the object state (no side effects). Now, go to your example. Let's say the call to func1, func2, func3... funcN might give different output in every call, then you can call those functions outside the
function and send it as the parameter to
. Example:
fun stringifyValue ( vals: List<String> ): String {
//do what you need
fun main() {
val f1 = func1 ()
val f2 = func2 ()
val f3 = func3 ()
val f4 = func4 ()
val f5 = func5 ()
val str = stringifyValue ( listOf(f1, f2, f3, f4, f5) )
From the practicality point of view though, it is sometimes necessary to have functions that do have side effects for business logic, so there is certain limit that you should understand as well. Not everything must be pure functions. It depends on the type of functions you want to create I would say.
“Doesn’t have side effects” doesn’t imply “doesn’t use mutable data”. This code is super inefficient (creates tons of intermediate strings), it would be a lot better to use string interpolation, or something like
if you need more flexibility. It would still be a pure function.
🤔 1
@deactivateduser actually in my real code I have to call nearly 20 functions ☹️ and need to append their result. I minimized it for the question. Creating one local var inside a function is illegal in FP.? Because things are more simple if I have a var String and append function results with it.
Well, I would not say it's illegal to create a mutable local variable. I'm more on a pragmatic approach to be honest. I would presume that your stringify is actually your business logic (i don't have clear clue what you are trying to achieve). In this case, I don't see anything wrong with creating the stringify function with side effects. In this case though, you might want to go with StringBuilder() instead of String(). It is more efficient to use StringBuilder() to concatenate all the strings you require and get the final string.
👍 1