Skip to content

Instantly share code, notes, and snippets.

@giuliohome
Last active August 9, 2018 14:15
Show Gist options
  • Select an option

  • Save giuliohome/007d25d53d939966f4271acca6d73862 to your computer and use it in GitHub Desktop.

Select an option

Save giuliohome/007d25d53d939966f4271acca6d73862 to your computer and use it in GitHub Desktop.
let rec bfs2 (fanout: Map<'node, 'node seq> -> 'node -> 'node seq) (tree: Map<'node, 'node seq>) (node: 'node) : 'node seq =
let single = seq [node]
match fanout tree node with
| e when e = Seq.empty -> single
| s -> Seq.fold (fun acc item ->
bfs2 fanout tree item
|> Seq.append acc) single s
@giuliohome

Copy link
Copy Markdown
Author

the above simpler code is valid if the map is guaranteed to be a tree without cycles,
otherwise, for a more robust but still functional implementation,look at https://gist.github.com/giuliohome/c9d4e4104e618d3cc94d2d09436c0952

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment