Hi,
There is a question regarding usage of functional programming techiques in C# code. Example
Let we have interface
interface IGraph { /*contains vertices and edges*/}
Suppose we need to layout graph's vertices (assign Point to each vertex).
interface ILayoutInfo {
Point GetVertexPoint(vertex);
}
Simple layout route can have such signature:
ILayoutInfo SimpleLayout(IGraph graph);
Which can be used in such way
void Demo() {
IGraph graph = CreateGraphInAnyWay();
ILayoutInfo layout = SimpleLayout(graph);
PrintCoordinates(graph,layout);
}
In design below PrintCoordinates need both references to graph and layout.
Consider functional style design where layouting routing augments graph information with information about graph vertices coordenates.
ILayoutedGraph SimpleLayoutNew(IGraph graph);
Where ILayoutedGraph implements BOTH IGraph and ILayoutInfo
void DemoNew() {
IGraph graph = CreateGraphInAnyWay();
ILayoutedGraph layoutedGraph = SimpleLayoutNew(graph);
PrintCoordinatesNew(layoutedGraph);
}
1)In this design PrintCoordinatesNew gets only ONE parameter. 2)Weird interface ILayoutedGraph was born which doesn't contain any methods and just wraps other interfaces. If some library has other types like INetwork, ITree we end up creating wrap interfaces ILayoutedNetwork, ILayoutedTree (which is bad).
So such techique is used only in functinal languages just because they can't work in other way (there are no state so function must combine input with calculated info to be used by outer routines) or it is viable in imperative world too?
Thanks a lot,
PS: a more verbose pretty printed example can be found here http://tivadj.blogspot.com/2009/02/designing-c-api-in-functional-style.html