swift curried functions

Ran across this just now and found the comment by Pavol more interesting than curried functions. Yes Pavol! Totally. This is going to be a recurring problem for a lot of imperative programmers beginning to enter the world of functional programming through Swift. ‘Functional first’ is something I have to continually remind myself of. The original (imperative) gist went something like this:

curried1

Pavol advocates a more functional approach, without the “minutiae” of loops and temp vars:

curried2

Can we continue that train of functional thought? What if we move the separator up the chain, eliminating the function call:

curried3

That was an interim step to see if it worked. Now lets wrap it up and bring back that append function, allowing us to pass any separator:

curried6

Lastly, to make it even more compact, we can remove some syntax noise in the call to reduce:

curried7