juxt

function juxt( ... ) --> func

Description

Returns a function that is the juxtaposition of a list of functions. func takes a variable number of arguments, and returns an array table containing the result of applying each function to the arguments (left-to-right).

Parameters

A variable number of functions

Return Values

func

Function that calls all functions and returns an array table with the results.

Code

--ZFUNC-juxt-v1
local function juxt( ... ) --> func
   local set = { ... }

   return function ( ... )
      local func = {}
      for _, f in ipairs( set ) do
         table.insert( func, f( ... ) )
      end
      return func
   end
end

return juxt

Examples

local t = require( "taptest" )
local juxt = require( "juxt" )
local same = require( "same" )

local j1 = juxt( function( v ) return v - 1 end,
                 function( v ) return v end,
                 function( v ) return v + 1 end )

t( same( j1( 3 ), { 2, 3 ,4 } ), true )

t()

Inspired by

See also

  • compose

  • link:partial.html[partial