Page MenuHomePhabricator

Make it nice to use function closures and possibly anonymous functions in composition editing mode
Open, Needs TriagePublicFeature

Description

Feature summary (what you would like to be able to do and where):
In Wikifunctions, implement function closures for function composition. Specifically, I would like to be able to define an anonymous helper function inside of a function composition, which can reference variables from the scope it was declared in (e.g. arguments of the parent function). However, I believe this need would also be alleviated by allowing for closures through OCaml-style partial function application, where add x gives you a function closure, which can then be called to give you a result (add x) y, which is equivalent to add x y.

Use case(s) (list the steps that you performed to discover that problem, and describe the actual underlying problem which you want to solve. Do not describe only a solution):
Many possible use cases revolving around map & fold/reduce, rather than relying directly on recursion. I was thinking of another implementation for Z13322/is sorted using fold left, but I am unsure of how that implementation could be done without (1) function closures, made smoother with anonymous functions, or (2) the ability to call external ZFunctions from within a programming language implementation (which is another thing I would like to see, but that's for a different ticket).

Benefits (why should this be implemented?):
I believe this would enable a huge number of new functions/implementations and start bringing Wikifunctions up to speed with an actual functional programming language, like OCaml.

Event Timeline

See also T386422 - didn't notice that when I made this. However, these tickets have not-entirely-overlapping scopes (the other is asking for currying/partial function application, this is asking in part for anonymous/inline functions).

Jdforrester-WMF renamed this task from Function closures and possibly anonymous functions to Make it nice to use function closures and possibly anonymous functions in composition editing mode.May 21 2025, 3:39 PM

We're pretty sure this should work in the back-end, but building the composition in the UX may well be hard/impossible. Did you manage to write such a composition and run into issues (in which case it's a definite bug), or is this more that it seemed/was impossible?

Implementing this particular function (Z13322/is sorted) using fold left seems to be impossible, at current. Implementing "is sorted" for a particular comparison function should be possible using fold left, but implementing it for an arbitrary comparison function does not seem to be possible, from my understanding (it could be implemented by creating another version of fold left that passes additional arguments to the applied function... but that would get unwieldly). Quick mockup of how I might write it in OCaml:

let is_sorted (comp_fun : 'a -> 'a -> bool) (lst : 'a list) =
    let helper (acc : bool * 'a) (elem : 'a) =
        match acc with
        | (sorted, last) -> (sorted && (comp_fun last elem), elem)
    in
    match List.fold_left helper (true, List.hd lst) lst with
    | (res, _) -> res

(This specific implementation wouldn't work for empty lists, but that's besides the point.)
If this was to be translated into a function composition, it would not be possible, because there would not be a helper function closure that has access to the comp_fun variable. Some other form of fold/reduce would have to be made that calls the given function with an additional argument.

Hi, @WrenFalcon , I'd like to know a bit more about this. Are you saying that closures and anonymous functions do not work when you try to run them, or that you cannot create them in the UI?

If the former, that's definitely a bug. I would expect such things to work. If you can provide me reproduction instructions for a function call that did not work as expected, I'll be happy to investigate.

If the latter, this is a UI feature request. In that case, I'll make sure this is filed appropriately so the feature request can be considered.

Hi, @WrenFalcon , I'd like to know a bit more about this. Are you saying that closures and anonymous functions do not work when you try to run them, or that you cannot create them in the UI?

If the former, that's definitely a bug. I would expect such things to work. If you can provide me reproduction instructions for a function call that did not work as expected, I'll be happy to investigate.

If the latter, this is a UI feature request. In that case, I'll make sure this is filed appropriately so the feature request can be considered.

Ah! Apologies, I hadn't seen your comment.

So, this is possible using argument references. The helper function would have to be provided as an argument to is_sorted. I don't know OCaml, so I can't give you proper pseudocode, but basically, helper would be defined as an argument to is_sorted, and it could then be provided as an anonymous Z8/Function when calling is_sorted.