Get shortest/longest string in array

I would certainly use findfirst rather than filter here (since the latter creates a new array unnecessarily). But you could also easily write a function to do this in a single pass:

function extrema_elements(f, itr)
    i = iterate(itr)
    i === nothing && throw(ArgumentError("iterator must be nonempty"))
    xmin = xmax = i[1]
    vmin = vmax = f(xmin)
    while true
        i = iterate(itr, i[2])
        i === nothing && return (xmin, xmax)
        x = i[1]; v = f(x)
        if v < vmin
            xmin, vmin = x, v
        elseif v > vmax
            xmax, vmax = x, v
        end
    end
end

giving:

julia> extrema_elements(length, arr)
("!", "beautiful")
3 Likes