GithubHelp home page GithubHelp logo

Clone a function? about penlight HOT 8 CLOSED

kalekje avatar kalekje commented on September 22, 2024
Clone a function?

from penlight.

Comments (8)

alerque avatar alerque commented on September 22, 2024 2

If there is a case to be made for this in a general purpose utility library and a way to do it robustly across Lua implementations then please feel free to still comment.

But, I think I'm in agreement with Thijs here, this is such a niche need and so brittle across platforms that I think we'd do more people a disservice by adding such a tool than we would help.

from penlight.

Tieske avatar Tieske commented on September 22, 2024

What s the use case voor something like this?

from penlight.

kalekje avatar kalekje commented on September 22, 2024

If there's an existing function that you want to patch and use the same name. It might be considered bad practice though. Say a library provides a function that does something to a string, call it, xlib.changestring(s). I might want to do something like this so that it works on a table as well:

local _changestring = pl.clone_function(xlib.changestring)
function xlib.changestring(s_or_t)
if type(s) == 'string' then
  return _changestring(s_or_t)
elseif type(s) == 'table' then
  return pl.tablex.map(_changestring, s_or_t)
end
end

from penlight.

alerque avatar alerque commented on September 22, 2024

@kalekje What would the clone_function() actually be accomplishing here? The example code you gave will work just fine if you leave it out. Is this just about making that workflow safe for multi-thread usage? If so can we have an MWE that actually shows the problematic use case and proposed solution?

from penlight.

kalekje avatar kalekje commented on September 22, 2024

There would be a stack overflow problem otherwise, try the following:

function f(x) return x end
function f(x) return 2*f(x) end
print(f(1))

from penlight.

kalekje avatar kalekje commented on September 22, 2024

Okay I see it now.. I thought this would not work.

function f(x) return x end
f_temp = f
function f(x) return 2*f_temp(x) end
print(f(1))

from penlight.

Tieske avatar Tieske commented on September 22, 2024

This is severe hackery, see also the limitations over different Lua versions in Leafo's article.

My 2cts; if you need to do it, do it in your application. But this seems like bad practice in general and I see no place for that in a general utility lib. Using Penlight should be easy and safe imo.

from penlight.

Tieske avatar Tieske commented on September 22, 2024

@kalekje There are some things at play here;

  1. in Lua all functions are anonymous. Except that defining functions by name has syntactic sugar to define the name before the function.

    local function f(x) return 2*f(x) end

    is equivalent to

    local f
    f = function(x) return 2*f(x) end

    Note that local f is now available inside the function, which is different from:

    local f = function(x) return 2*f(x) end

    In which case it is NOT defined, because Lua will first evaluate the right hand side, and only then assign the values

  2. using tail calls does not consume stack space, since the tail call will replace the last entry with the new one, instead of adding to the stack.

from penlight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.