1
\$\begingroup\$

How can I make this more functional?

#collects the first x + 1 items of a collection as long as predicate is true
#returns the a tuple containing a list of x+1 items and 
# the rest of the collection
def collect(source, target \\ []) do
    [h|t] = source
    if predicate(h) do
        target = target ++ [h]
        collect(t, target)
    else
        if target != [] do
            target = target ++ [h]
            {target, t}
        else
            {[], source}
        end
    end
end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.