Hacker News new | past | comments | ask | show | jobs | submit login

"In python, it would be proper to say that ALL functions are lambdas except that some/most of those lambdas are assigned to variables via syntactic sugar"

If a function is "assigned to a variable" then it's no longer a lambda, because it's bound to an identifier. But I suppose the lines are a tad blurred.




While I agree that the function "has a name", the function is still anonymous in the sense that pointers don't have types, only values have types. As a side effect of converting church's model to Turing's model, all "unnamed" lambda functions are given one-time symbols as are all anonymous numbers, arrays, classes, objects, etc.

  x = lambda x: x+x #a lambda, but has a named pointer
  x = 5
lambda is still in memory (until garbage collection), but it no longer has a pointer to it.

  def x(y):
    return y+y
  
  x = 5 #the "named" function above is now without a pointer
A little off-topic (but answers your previous question and perhaps looks a little deeper into lambdas), but let's take a quick look at javascript. JS developers use lambda functions both because lexical closures don't pollute the global namespace, they offer modules and alternatives to prototypal objects, and because the language tends to make heavy use of callbacks (which demonstrate the bad side of lambda functions). JS also only makes subtle differences between lambdas and named functions.

  //lambda expression
  var x = function (){};

  //named function
  function x() {}

  //named lambda expression
  var x = function x() {};

  //self-executing lambda (returns 5)
  (function (x) {return x;}(5));
note that this would be a self-executing lambda in python

  (lambda x: x)(5)
basic module pattern using self-executing lambda

  my_module = (function (self) {
    "use strict"
    self = self || {};

    //private
    //pretty much inaccessible to the outside scope
    var privateVar = 5;
    //lambda expression again
    var privateFunc = function () {};

    //public
    self.publicVar = 5;
    self.publicFunc = function () {};
    self.getPrivateVar = function () {
      return privateVar;
    };

    return self;
  }(my_module));

  my_module.publicVar = 8;
  my_module.publicFunc();
  my_module.getPrivateVar();
This is where too many lambdas becomes a problem. Judicious use of naming (even though they're only used once) makes things more legible.

  func1(function () {
    func2(function () {
      func3(function () {
        func4(function () {
      })
    })
  })




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: