Search code examples
lambdaargumentsopahigher-order-functionsthunk

Opa: higher order function application


In Opa, it is possible to partially apply functions. However, I would like to supply a function with all its arguments, and return a function taking no arguments. For example:

function make-thunk(f, a) { function() { f(a) } }

The code above works fine, but it has one problem: It is only suited for functions taking exactly one argument. I am curious whether it is possible in Opa to make the above more generic, so the function can be used for functions of arbitrary arity.

I was unable to find information about apply- (scheme) or call- (prolog) like functions on the Opa API, forum, IRC, Github or SO, so here I am hoping I missed something, somewhere. ;-) (There were some traces of an @lazy keyword, but no explanation or documentation so I didn't keep a reference, sorry.)

To further clarify / illustrate: If Opa were scheme, I'd write the following code:

(define (make-thunk f . args)
  (lambda () (apply f args)))

Solution

  • If you want to pass an arbitrary number of arguments to make-thunk then you have to use a tuple:

    function f((a,b,c)) {
       ...
    }
    
    args = ("a","b","c")
    make-thunk(f, args)
    

    Btw, where did you find mentions to @lazy?