Search code examples
kotlinfunctional-programmingarrow-kt

How to append data in Functional programming style using kotlin with arrow library


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 func*() call returns a valid string and it's gets appended to mutable string variable returnString. As far as I know, FP functions should not use any mutable values. So how would I replace those string appending lines?

    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
}

Solution

  • This question doesn't seem to be related to Arrow. Do you have more code related to this example? And maybe more context?

    As for the code at hand, it may be easier to use Kotlin's template strings:

    private fun stringifyValue(): String = 
      "=${fun1()}+/${func2()}@${func3()}#${func4()}%${func5()}^"