lua-users home
lua-l archive

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


It was thus said that the Great Miroslav Janíček once stated:
> 
> > On 4 Oct 2016, at 0:04, Sean Conner <sean@conman.org> wrote:
> > 
> > It was thus said that the Great Mike Jones once stated:
> >> Hi,
> >> 
> >> I am working on a project where I have basically an event style
> >> framework, and I would like some way to "chain together" functions. 
> > 
> >  I dug through my archives and I found the following bit of code (cleaned
> > up---I was starting to learn Lua):
> > 
> > 	function fappend(f1,f2)
> > 	  return function(...)
> > 	    return f2(f1(...))
> > 	  end
> > 	end
> 
> It might be a good idea to handle cases in which f1 or f2 are undefined:

  I disagree [1].  It makes *no* sense to call the function with any of the
parameters nil (it comes across as pointless) and at worse, it could hide
bugs in the code elsewhere (why is f1 nil?  How could that happen?). [2]

>       function fappend(f1, f2)
>         if f1 ~= nil and f2 ~= nil then return function(...) return f2(f1(...)) end
>         elseif f1 ~= nil then return f1
>         else return f2
>         end
>       end
> 
> (And maybe also calling the function “compose” would be more indicative of its intent.)

  That I have no problem with.

  -spc

[1]	And I feel I might be in the minority here.

[2]	I'm firm in the "crash fast and crash loud" crowd here.