4 ms·
If there was one thing I'd change with python to make it more 'functional' it would be having lambdas that weren't expression-only. I know it's not a functional
by throwaway_pdp09 6y ago
If there was one thing I'd change with python to make it more 'functional' it would be having lambdas that weren't expression-only. I know it's not a functional feature because it's for stateful programming, but the lack of it disallows some nice stuff.
- saghm 6y agoI agree with you that having more than a single expression in lambdas would be super nice. That being said, I don't think it's necessarily stateful to allow more than a single expression! If you don't reassign any variables, then it's equivalent to a single expression. For instance, something like this (using made up syntax for extended lambdas) isn't stateful: lambda i: square = i * i if i < 0: return -1 * square else: return square A decent heuristic for whether an algorithm is stateful might be to check if you can map it pretty easily to something like Haskell. In this case, it's not hard to do at all: \i -> let square = i * i in if x < 0 then -1 * x else x Of course, it might be more natural for some programmers to write the Python code in a stateful way like this: lambda i: square = i * i if i < 0: square *= -1 return square
- throwaway_pdp09 6y agovery quick reply, and I've done this in other langs that allow procedures-done-as-lambdas, create your own control flow mechanisms with 'reasonable' syntax, something like with_file("c:/blah/data.csv", lambda fl: # do stuff with file variable fl # other statements # ... ) The file is opened, the block of code of the lambda does its thing, the file is closed at the end, exception-safe. Python can do this another way (with 'with' objects or something, and it's quite clean and neat, but that's quite recent IIRC), this simpler IMO. Have used this, it's called the loan pattern, in scala, vb.net and C#, and prob others. The arbitrary restriction on lambdas blocks composability, which disallows useful tricks like this.
- frenchyatwork 6y agoI'm sure there are cases where multiple lines would be nice, but I don't think this is one. This seems easier to read to me: lambda i: -i * i if i < 0 else i * i Also, the functional way of doing multiple lines in a lot of situations would typically be to compose smaller lambdas.
- saghm 6y agoYeah, this is a trivial example. I picked it specifically to show that multi-line lambdas don't have to be stateful, not as an example of a lambda that couldn't be written in one line.
- entha_saava 6y agoAnd there is math.copysign
- jennasys 6y agolambda use in Python is discouraged in general. If you need something beyond an expression, PEP-8 encourages you to use a def in that case instead.
- NoahTheDuke 6y agoThe issue with that is the lack of lexical scope. Lambdas have lexical scoping, def functions do not. To “fake” lexical scope, you have to use the nonlocal or global statements, which are gross and self-defeating.
- throwaway_pdp09 6y agoNot sure I understand, defs have lexical scoping too, if you just read variables (or am I getting rusty?). x = 3 def prt(): print(x) prt() prints 3, as expected. It's when you assign that you need P3's global etc. annotations, and you can't assign in lambdas.