What general-purpose commands do you usually end up adding to your projects?

Related to this response from Stefan,

What commands do you usually end up adding to your projects to get around common nuisances?

1 Like

I use this for unit tests:

"Lenient comparison operator for `struct`, both mutable and immutable (type with \\eqsim)."
@generated function ≂(x, y)
    if !isempty(fieldnames(x)) && x == y
        mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
    else
        :(x == y)
    end
end
11 Likes

Not sure if there is a built-in command for listing files in a directory but I use this one almost everywhere!

searchdir(path, key) = filter(x -> contains(x, key), readdir(path))

3 Likes

if anyone wants to make this check recursive (for the case when inner fields can be struct as well) – just replace :(x.$n == y.$n) with :(x.$n ≂ y.$n).

1 Like