Higher order functions

Published .. 27-08-2024
Type ....... article
Tags ....... v

V supports higher order functions.

Example:

fn chain(funcs ...fn (string) string) fn (string) string {
    return fn [funcs] (i string) string {
        mut r := i
        for f in funcs {
            r = f(r)
        }
        return r
    }
}

The chain function returns a new function that, when called, runs all the funcs on the input string and returns the result.

Here are an example, term.bright_yellow and term.bg_black are both functions that take a string as argument and returns a string.

println(term.colorize(chain(term.bright_yellow, term.bg_black), 'qStart!'))