Implementing a BfsNodeVisitor in the graph package

The documentation of BfsNodeVisitor in the graph package is a little too thin for me to wrap my head around how to use it.

I’d like to separate the labels of a graph into two lists, even depths in one list and odd depths in another. Since this requires the distance property of the visitor, the simple ignorePath visitor doesn’t cut it.

The separation could start with two empty lists tupled: ( [ ], [ ] ), then simply use something like:

let
    ( left, right ) =
        acc
in
if modBy 2 distance then
    ( label :: left, right )
else
    ( left, label :: right )

Can someone a little more capable with Elm take a look at the BfsNodeVisitor type signature, and give me some pointers on how to approach such a task here?

In the interest of completeness, I’d like to add a solution to this myself just in case others have a similar problem.

The crux for me was that I just needed to confine the type signature to the output I needed rather than the general acc.

levelSplit : List (Graph.NodeContext n e) -> Int -> ( List n, List n ) -> ( List n, List n )
levelSplit paths distance acc =
    case paths of
        [] ->
            ( [], [] )

        ctx :: _ ->
            let
                ( left, right ) =
                    acc
            in
            if modBy 2 distance == 0 then
                ( ctx.node.label :: left, right )

            else
                ( left, ctx.node.label :: right )

which can now be used to separate values on even/odd levels into separate lists:

Graph.bfs levelSplit ([], []) graph
--> ([5,3,2,7,2],[6,5,8])

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.