3 ms·
I'm a python-native programmer who just recently picked up SICP. I've been trying to use some more functional tools; I told a friend at work one day that I was
by zipfle 13y ago
I'm a python-native programmer who just recently picked up SICP. I've been trying to use some more functional tools; I told a friend at work one day that I was going to try to do everything with just map and filter.
But I also just had an experience I haven't had much since I was extremely new: the experience of going back to something I wrote last week and not being able to decipher it. I'm going to keep it up, partly because I like it and also because I have some hope (unjustified?) that map will give me better performance than looping. I wish there was a way to enable tail recursion though--seriously, I promise I'll ask for clear stack traces when I need them.
- bulatb 13y agoNot pretty, but it does the trick. Kinda. Or at least it won't destroy the stack if you recurse too deep. from collections import namedtuple TailCall = namedtuple('TailCall', ['function', 'args']) def tail_recurse(fn, args): next_call = fn(*args) while isinstance(next_call, TailCall): next_call = next_call.function(*next_call.args) return next_call def count_to_4(start): """Dummy tail-recursive function""" if start == 4: return 4 else: return TailCall(count_to_4, [start + 1]) print tail_recurse(count_to_4, [-6])
- deleted 13y ago[deleted]
- dorolow 13y agohttp://wiki.python.org/moin/PythonSpeed/PerformanceTips http://wiki.python.org/moin/PythonSpeed/PerformanceTips List comprehensions beat map in CPython. As a fellow pythonista, I can guarantee you that using the builtin higher-order functions excessively will cause you nothing but pain. The feeling of being lost when you revisit code will be common. I had a coworker who undertook a plan similar to yours, except he gave himself less rigid constraints. He is a very smart guy but he fell into the tar pit of code masturbation and couldn't finish the project he took on. He simply couldn't manage the complexity of his application in his quest to be As Functional As Possible. The project was never completed and after wasting many man hours, the company was forced to purchase $25,000+ worth of software to compensate. It didn't reflect well on him. Python has functional programming support, but it's not intended to be the dominant paradigm. Python 3 even removed reduce as a builtin. The high order functions are still useful, but definitely not all the time. The principles of functional programming, such as limiting side effects and statefulness, are always useful. But Python has awful lambdas and no lexical scoping (fixed in 3). Python's functional support will always be hobbled as Guido doesn't support it. Since you seem to have a healthy interest in functional programming, I'd suggest looking at Clojure. If you want to challenge yourself, it's a good choice as you'd have to learn lisp (macros, code is data, s-exprs, etc) and the JVM ecosystem, while being almost totally constrained to writing functional code. Haskell is even more strict and is a pure functional language, forbidding state entirely and relying on monads for things like I/O. I almost like it as much as Clojure. Erlang and Ocaml are also worth checking out.
- teddyh 13y ago@dorolow, your comment is dead because it used the auto-banned word "mast?rb?tion" (uncensored). FYI.
- lelf 13y agoWhat? We've got auto-banned words?
- teddyh 13y agoTry it and see.
- dorolow 13y agoBizarre. I guess I'll have to be more careful. Thanks for the heads up
- spidergears 13y agofuck