5 ms·
What are you doing that can't be done with python's lambda functions?
by DocSavage 18y ago
What are you doing that can't be done with python's lambda functions?
- aristus 18y agoAnything that is not a simple expression. assignment, if..else, etc.
- kashif 18y agoThen use a function with some introspection and pass it some parameters.
- cturner 18y agoYou can but you shouldn't need to. Sometimes I want to do something like: dict['item'] = def _(var1, var2): fn_var = 1 do_some_magic(fn_var, var1, var2) There's no good reason why I should instead have to do: def fn(var1, var2): blah dict['item'] = fn .. or write a decorator. I'm frequently tempted to write a python-like scheme syntax to sit in my python programs so I can just do this stuff and to hell with the excuses used to prevent my first code block from above from working.
- LogicHoleFlaw 18y agoIt's been a while since I wrote any Python but... here's a shot in Lua. Lua: function dict.item (var1, var2) local fn_var = 1 do_some_magic(fn_var, var1, var2) end -- note that: function dict.item (arg) return arg end -- is syntax sugar for: dict.item = function (arg) return arg end -- for an implicit "self" parameter use a : in your declaration and invocations: function dict:item (arg) return self(arg) end -- is sugar for dict.item = function (self, arg) return self(arg) end You can in fact call functions with the implicit (via :) self parameter with any desired "self" by using the "." rather than ":" invocation syntax and providing an explicit self. ":" is purely sugar which is optional in all cases. function table:swap() local swapped = {} for k, v in pairs(self) do swapped[v] = k end return swapped end -- assume "Table()" returns a table which routes "method_missing" calls -- (which is actually the __index metamethod in Lua) -- to `table` t = Table { a = "foo", b = "bar" } a = t:swap() b = table.swap(t) -- a and b are both equal to: -- { foo = "a", "bar" = b } One neat thing about Lua is that the user can define basically arbitrary semantics for inheritance, mixins, and all sorts of meta stuff very simply. And you can define them on a per-object basis. It's very powerful. The drawback is that you have to implement these things yourself (unless you use a 3rd-party lib which provides some common patterns like you'd see in Python or Ruby).
- cturner 18y agoThanks for that. I've looked at io a bit, which is similar to lua, and been very impressed by stuff like this. Also that you can effectively reflect against function definitions in order to create modified versions at a later time.
- raganwald 18y agoWhat are you doing with Python that can't be done with FORTRAN?
- DocSavage 18y agoRunning on Google's App Engine :) Seriously, though, I'm not arguing python's anonymous functions are just as good as ruby's. I'm curious about his particular situation where "3-4 lines of Ruby" is all it takes.
- deleted 18y ago[deleted]
- eru 18y agoYou could run FORTRAN on a virtual machine on Python.