diff options
author | Yonaba <roland.yonaba@gmail.com> | 2017-04-14 17:45:55 +0300 |
---|---|---|
committer | Yonaba <roland.yonaba@gmail.com> | 2017-04-14 17:45:55 +0300 |
commit | 49aec875a0417c351e5dec2b7bd551e0660e290e (patch) | |
tree | 49da86c520c6027ffee3400979d3668baf528c53 /moses.lua | |
parent | 168c64d7f421cf2e570c6c9efd4d17d0a3e29964 (diff) |
Added _.overEvery and _.overSome
Diffstat (limited to 'moses.lua')
-rw-r--r-- | moses.lua | 36 |
1 files changed, 35 insertions, 1 deletions
@@ -1520,10 +1520,12 @@ function _.flip(f) end end ---- Creates a function that runs transforms on arguments it receives. +--- Creates a function that runs transforms on all arguments it receives. -- @name over -- @param ... a set of functions which will receive all arguments to the returned function -- @return a function +-- @see overEvery +-- @see overSome -- @see overArgs function _.over(...) local transforms = {...} @@ -1536,6 +1538,36 @@ function _.over(...) end end +--- Creates a validation function. The returned function checks if *all* of the given predicates return +-- truthy when invoked with the arguments it receives. +-- @name overEvery +-- @param ... a list of predicate functions +-- @return a new function +-- @see over +-- @see overSome +-- @see overArgs +function _.overEvery(...) + local f = _.over(...) + return function(...) + return _.reduce(f(...),function(state,v) return state and v end) + end +end + +--- Creates a validation function. The return function checks if *any* of a given predicates return +-- truthy when invoked with the arguments it receives. +-- @name overSome +-- @param ... a list of predicate functions +-- @return a new function +-- @see over +-- @see overEvery +-- @see overArgs +function _.overSome(...) + local f = _.over(...) + return function(...) + return _.reduce(f(...),function(state,v) return state or v end) + end +end + --- Creates a function that invokes `f` with its arguments transformed. 1rst arguments will be passed to -- the 1rst transform, 2nd arg to the 2nd transform, etc. Remaining arguments will not be transformed. -- @name overArgs @@ -1543,6 +1575,8 @@ end -- @param ... a list of transforms funcs prototyped as `f (v)` -- @return the result of running `f` with its transformed arguments -- @see over +-- @see overEvery +-- @see overSome function _.overArgs(f,...) local _argf = {...} return function(...) |