1-- Minimal functional programming utilities. 2-- Written by Gary V. Vaughan, 2015 3-- 4-- Copyright(C) 2015-2022 Gary V. Vaughan 5-- 6-- Permission is hereby granted, free of charge, to any person obtaining 7-- a copy of this software and associated documentation files(the 8-- "Software"), to deal in the Software without restriction, including 9-- without limitation the rights to use, copy, modify, merge, publish, 10-- distribute, sublicense, and/or sell copies of the Software, and to 11-- permit persons to whom the Software is furnished to do so, subject to 12-- the following conditions: 13-- 14-- The above copyright notice and this permission notice shall be 15-- included in all copies or substantial portions of the Software. 16-- 17-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25--- @module lyaml.functional 26 27 28--- `lyaml.null` value. 29-- @table NULL 30local NULL = setmetatable({}, {_type='LYAML null'}) 31 32 33--- `lyaml.null` predicate. 34-- @param x operand 35-- @treturn bool `true` if *x* is `lyaml.null`. 36local function isnull(x) 37 return(getmetatable(x) or {})._type == 'LYAML null' 38end 39 40 41--- Callable predicate. 42-- @param x operand 43-- @treturn bool `true` if *x* is a function has a __call metamethod 44-- @usage r = iscallable(x) and x(...) 45local function iscallable(x) 46 if type(x) ~= 'function' then 47 x =(getmetatable(x) or {}).__call 48 end 49 if type(x) == 'function' then 50 return x 51 end 52end 53 54 55--- Compose a function to try each callable with supplied args. 56-- @tparam table fns list of functions to try 57-- @treturn function a new function to call *...* functions, stopping 58-- and returning the first non-nil result, if any 59local function anyof(fns) 60 return function(...) 61 for _, fn in ipairs(fns) do 62 if iscallable(fn) then 63 local r = fn(...) 64 if r ~= nil then 65 return r 66 end 67 end 68 end 69 end 70end 71 72 73--- Return arguments unchanged. 74-- @param ... arguments 75-- @return *...* 76local function id(...) 77 return ... 78end 79 80--- @export 81return { 82 NULL = NULL, 83 anyof = anyof, 84 id = id, 85 iscallable = iscallable, 86 isnull = isnull, 87} 88