Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: queryInlines -> foldInlines #7

Open
xarvh opened this issue May 20, 2019 · 0 comments
Open

Improvement: queryInlines -> foldInlines #7

xarvh opened this issue May 20, 2019 · 0 comments

Comments

@xarvh
Copy link

xarvh commented May 20, 2019

Right now the module has a queryInlines : (Inline i -> List a) -> Block b i -> List a.

An improvement on this, that would make the function more flexible and help with performance, could be to follow the fold pattern, ie:

foldInlines : (Inline i -> a -> a) -> a -> Block b i -> a

With this, queryInlines can be expressed in terms of this more generic function:

queryInlines f block = foldInlines (\inline acc -> f inline :: acc) [] block

This would allow the function to iterate on accumulators different than lists, avoiding intermediate allocations, and inserting more or less than one item per inline.
The example could be rewritten as:

    links : List String
    links =
        myMarkdownString
            |> Block.parse Nothing
            |> Block.foldInlines appendLink []

    getLinks : Inline i -> List String -> List String
    getLinks inline accum =
        case inline of
            Link url _ _ ->
                url :: accum
            _ ->
                accum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant