There is a `f(g) = function(args...) broadcast(g, args...) end` in `Base`?

I know I can define a function like:

wrap_in_broadcast(f) = function (args...)
    broadcast(f, args...)
end

I defined this function because many times I need to pass a function to another function, or use over a broadcasted version of simple function (like sum ∘ ismissing. but this dot is not valid sintax). As simple as it is to define this function, seems to me that I am missing something, as it seems useful enough to exist in the standard library but I did not found it. What have I missed?

Do you have other examples of when you’d use wrap_in_broadcast? For your given example, count(ismissing, v) works well, or sum(skipmissing(v)) if you’re trying to sum the non-missing values.

1 Like

Do you want Transducers.jl ?

julia> using Transducers
julia> skipmissing_sum(iter) = reduce(+, Filter(!ismissing), iter)
julia> skipmissing_sum([1, 2, missing, 3, 5]
11
2 Likes

@stillyslalom Hard to remember examples for memory, as it is a very generic situation. However, it was frequent enough to me to perceive the pattern. It started with me creating a plural version of some function to be able to pass them, after I started to use an anonymous function for the same purpose, and then finally I defined a wrap_in_broadcast (after renamed to broadwrap). So I would say it is frequent.

@L_Adam I had already heard of Transducers.jl but never really looked into it. Seems something worth learning. It has a Broadcasting that seems what I want, but inside their framework. Remember me the flow of Haskell code what is a plus to me.