Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Function Handles

    Is there something similar to MATLAB's function handles in MATA? It seems like it would be similar to function pointers, except that you can specify default parameters to the function.

    For instance, if I have a function, func_a(a, b, c), and I want to pass it in as an argument to another function but with b and c pre-specified so it is passed in as a function with only one argument.

    In MATLAB, we could do something like this: simp_func_a = @(a)func_a(a, b0, c0), where b0 and c0 are defined in the calling function. Is there any way to do the same in MATA?

  • #2
    Not that I'm aware of, at least not exactly. Maybe others can chime in with better information.

    inline is a reserved word, but I wouldn't hold my breath waiting for its implementation.

    In the meantime, you can try a kludge; maybe something like the following.

    .ÿ
    .ÿversionÿ17.0

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿlocalÿline_sizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ80

    .ÿ
    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ------
    :ÿmataÿsetÿmatastrictÿon

    :ÿ
    :ÿstructÿPseudoCurriedÿ{
    >ÿÿÿÿÿÿÿÿÿpointer(realÿscalarÿfunction)ÿscalarÿf
    >ÿÿÿÿÿÿÿÿÿrealÿscalarÿb,ÿc
    >ÿ}

    :ÿ
    :ÿvoidÿfunctionÿpassee(structÿPseudoCurriedÿscalarÿp,ÿ|ÿrealÿscalarÿa)ÿ{
    >ÿ
    >ÿÿÿÿÿÿÿÿÿaÿ=ÿmissing(a)ÿ?ÿ1ÿ:ÿa
    >ÿ
    >ÿÿÿÿÿÿÿÿÿrealÿscalarÿresult
    >ÿÿÿÿÿÿÿÿÿresultÿ=ÿ(*p.f)(a,ÿp.b,ÿp.c)
    >ÿÿÿÿÿÿÿÿÿprintf("%3.0f\n",ÿresult)
    >ÿ}

    :ÿ
    :ÿrealÿscalarÿfunctionÿpassed(realÿscalarÿa,ÿrealÿscalarÿb,ÿrealÿscalarÿc)ÿ
    >ÿÿÿÿÿÿÿÿÿreturn(aÿ+ÿbÿ+ÿc)

    :ÿ
    :ÿvoidÿfunctionÿpasser()ÿ{
    >ÿ
    >ÿÿÿÿÿÿÿÿÿstructÿPseudoCurriedÿscalarÿp
    >ÿÿÿÿÿÿÿÿÿp.fÿ=ÿ&passed()
    >ÿÿÿÿÿÿÿÿÿp.bÿ=ÿ2
    >ÿÿÿÿÿÿÿÿÿp.cÿ=ÿ3
    >ÿ
    >ÿÿÿÿÿÿÿÿÿpassee(p)
    >ÿ}

    :ÿ
    :ÿpasser()
    ÿÿ6

    :ÿ
    :ÿend
    --------------------------------------------------------------------------------

    .ÿ
    .ÿsetÿlinesizeÿ`line_size'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    In these circumstances,though, an object (class programming) has always seemed to me as cleaner (more to-the-point) and more versatile than a struct.

    Comment

    Working...
    X