Sometimes I want to recursively traverse a structure and collect elements along the way, an example is this function that collects all the concret subtypes of a type
function allsubtypes(T::Type) isconcretetype(T) && return [T] out = Type map(K->allsubtypes(K,out), subtypes(T)) out end allsubtypes(T,out) = isconcretetype(T) ? push!(out,T) : map(K->allsubtypes(K,out), subtypes(T))
As you can see the two methods are very similar, the only difference is that the first one initialize the output. This looks like something that could dealt with with
reduce or something similar, but I haven’t manage to find a solution. Is there a nicer pattern to do this ?