3 ms·
Maybe it's just my being a greenhorn talking, but I was under the impression that for loops and explicit recursion were "ugly" in functional-style code and you
by Swizec 15y ago
Maybe it's just my being a greenhorn talking, but I was under the impression that for loops and explicit recursion were "ugly" in functional-style code and you should instead use maps and reduces.
While my iterative solution is functional in the sense that it doesn't have any external side-effects. Internally it's still for loops and "unclean" in the sense you couldn't dissect it into a bunch of reusable functions whereas the functional solution could potentially be broken down and those lambda functions could be named and then would be re-usable.
Your python solution is very python-idiomatic though and from that standpoint therefore alright. I would probably have written something similar in python.
- ColinWright 15y agoHaskell is about as functional as you can get, and the comment in http://news.ycombinator.com/item?id=3084429 http://news.ycombinator.com/item?id=3084429 and its reply contain effectively the same code.
- Dn_Ab 15y agoI don't know. reirob's and fab13n's code are more general as they do not require that some type of ordering is placeable on its set of inputs. Your code requires that the elements are comparable. So your effectively is predicated on whether you think that limitation is not a big deal (technically it greatly restricts the type of elements, practically maybe not?)
- ColinWright 15y agoAll true, but only serving to obscure my question about why this is not considered functional by the OP. My first thought was to go with running down the list of indices, as fab13n does, but I thought I'd go for the less generic but cleaner code to try to get to the real point. I guess I failed.