How to spec an algorithm in a paradigm-independent way
By "paradigm", I mean object-oriented, procedural, or functional.
For example, here is fairly minimal python code to traverse inflow from a resource to all the flows that went into that resource, using a depth-first search:
def resource_inflows(resource):
visited = set()
flows = [resource,]
inflows_dfs(resource, flows, visited)
return flows
def inflows_dfs(element, flows, visited):
visited.add(element)
preds = element.preds()
for pred in preds:
if pred not in visited:
flows.append(pred)
inflows_dfs(pred, flows, visited)
These are procedural functions, but they depend on each element being able to respond to element.preds().
Which I think assumes that each element is an object, and preds() is a method of that object.
How would this be written in a functional-programming style?
And can some pseudo-code be written that would work for either style?
[edited] Still takes me awhile to figure out if this is really depth-first. But
for pred in preds:
takes each predecessor and then calls for all of its inflows before starting with the next predecessor. And each of those inflows will also explode one predecessor at a time.