tag:blogger.com,1999:blog-4714619521975985815.post8454316802362815997..comments2023-04-13T12:08:48.611+02:00Comments on All Things PHP: Currying vs. Partial ApplicationDennis Haarbrinkhttp://www.blogger.com/profile/11521182524522195056noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4714619521975985815.post-57444170338111452762016-05-26T08:50:29.047+02:002016-05-26T08:50:29.047+02:00This comment has been removed by the author.Anthony Burgoshttps://www.blogger.com/profile/00075823642497484481noreply@blogger.comtag:blogger.com,1999:blog-4714619521975985815.post-22546354591437861012014-05-27T17:28:50.441+02:002014-05-27T17:28:50.441+02:00I often use a similar partial application function...I often use a similar partial application function, although mine doesn't specify any arguments like your "$func" and "$arg1", since that causes PHP to complain if they're not provided.<br /><br />In fact I find it quite useful to give other functions the ability to do partial application. For example, many of PHP's functions aren't designed with partial application in mind, so they take their arguments in an unfortunate order (see http://www.haskell.org/haskellwiki/Parameter_order ). If we want to change their order, we might use a flip function:<br /><br />function flip($f) {<br /> return function() use ($f) {<br /> $args = func_get_args();<br /> return call_user_func_array($f, array_merge([$args[1], $args[0]], array_slice($args, 2)));<br /> };<br />}<br /><br />This looks quite similar to the "partial" function, since both take a function and return a function and both use call_user_func_array, array_merge and func_get_args. In fact, I use "flip" and "partial" together so much that I've started using this definition of flip which also partially-applies!<br /><br />function flip() {<br /> $args1 = func_get_args();<br /> return function() use ($args1) {<br /> $args2 = array_merge($args1, func_get_args());<br /> list(, $x, $y) = $args2;<br /> $args2[1] = $y; $args2[2] = $x;<br /> return call_user_func_array('call_user_func', $args2);<br /> };<br />}<br /><br />I've actually automated this somewhat with a currying function that sends any extra arguments to inner functions: http://lambda-the-ultimate.org/node/4953Warbohttps://www.blogger.com/profile/11167936627543971536noreply@blogger.comtag:blogger.com,1999:blog-4714619521975985815.post-70630353933418631392013-09-10T19:22:44.781+02:002013-09-10T19:22:44.781+02:00Thanks, your discussion here was exactly what I ne...Thanks, your discussion here was exactly what I needed to solve a problem I had posed for myself: I am taking an informal course in PHP at work that is rather elementary. Rather than solving the problems in class using a simple procedural style, I am attempting to figure out a functional alternative. <br /><br />In particular, your partial function resolved the problem of how to pass a function as an argument, when the passed function needed to be 'primed' with a particular argument (in my case a simple of array of strings).<br /><br />What is surprising and powerful about your partial function: it can also receive a multiple functions as arguments. In that way it allows for composing functions together and deferring execution of the functions, until a result is needed.Anonymoushttps://www.blogger.com/profile/01898992446180228337noreply@blogger.com