lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Mon, Feb 10, 2020 at 2:53 PM Gavin Holt <holtgavin@gmail.com> wrote:
Hi,

I am having fun chaining string functions to process text in Textadept. This is not for the purists as I have been adding to the standard string library e.g.

    function string.pre(text,prefix)
        -- Prefix each line assuming "\n" for end of line
        return prefix..text:gsub("\n","\n"..prefix)
    end

Adding a few global functions to select and insert text (and define undo_action boundaries), I can then type in the command entry line "macros" e.g.

    selection():pre("--  "):gsub("Python ","Lua "):insert()
   
Repeatable and undo-able macros and not quite as unreadable as TECO commands :) 

Q1. Is there a limit to the number of chained string methods Lua 5.3 can process?

There's no real limit, no. It's just a normal _expression_, and there's not an arbitrary limit for how deeply expressions can nest.
 
Q2. Is there any way to use chained methods for tables - in standard Lua 5.3?  
      This syntax appeals to me: 

    t = {"one","two","three"}
    print(t:insert("four"):sort():concat("\n"))

Unfortunately, that doesn't work nearly so well in the generic case, because tables are Lua's fundamental compound data type. However, if you want to make a specific "list" structure that offers those things through the metatable (you could have a syntax like `t = list{"one", "two", "three"}`) that would be easy with no real limitations.

/s/ Adam