You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using functional approach already in the code which analyze and transform tree-like hierarchical collections (it's transformation of source HTML DOM into Word document paragraphs or internal markup-like representation to display it later on screen).
Naturally, pattern matching is the solution for this class of problems.
There are two basic functional algorithms I use:
"Subtree template" pattern matching to detect particular patterns on subtree and then do some action on it if "shape requirements" are satisfied.
Recursive "tree scan" with transformation of input read-only object tree into output read-only object tree. Scanned tree is "pattern matched" (often with "subtree template" check) and appropriate action is chosen on subtree to output result transformed subtree to insert into result tree (and then passed children of root transformed subtree node again recursively pattern matched) or ignore this subtree completely if needed so.
When I have a look on Succinc<T> code it was interesting how my own implementation of this functional algorithms is very similar to Succinc<T> approach to pattern matching. So it would be good to extend Succinc<T> pattern matching for recursive object tree scan, matching and transformation.
Basic idea is the "tree scan context" which contain original subtree nodes collection from same tree level, result collection of nodes (to insert transformed tree result) and function to get children from subtree node from context.
Particularly compact representation of tree matching and transformation, I think.
Tree scan context looks like this:
/// <summary>/// Tree scan context./// </summary>/// <typeparam name="T">Type of source tree node.</typeparam>/// <typeparam name="TApp">Type of result.</typeparam>publicclassTreeScanContext<T,TApp>:ITreeWalkContextBreak{/// <summary>/// Constructor./// </summary>publicTreeScanContext(){Functions=newList<TreeApplyFunc<T,TApp>>();}/// <summary>/// Source nodes collection./// </summary>internalIEnumerable<T> Source {get;set;}/// <summary>/// Aggregated result for this source nodes collection./// </summary>internalTAppResult{get;set;}/// <summary>/// Apply function./// </summary>internalList<TreeApplyFunc<T,TApp>> Functions {get;privateset;}/// <summary>/// Default apply function./// </summary>internalFunc<T,TApp,TApp> DefaultApply {get;set;}/// <summary>/// Default function to get children./// </summary>internalFunc<T,IEnumerable<T>> DefaultGetChildren {get;set;}/// <summary>/// True if tree scan should globally stop now./// </summary>publicboolIsBreak{get;set;}}/// <summary>/// Tree apply function description./// </summary>/// <typeparam name="T">Type of source tree node.</typeparam>/// <typeparam name="TApp">Type of result.</typeparam>publicclassTreeApplyFunc<T,TApp>{/// <summary>/// Pattern match function./// </summary>internalFunc<T,bool> If {get;set;}/// <summary>/// Apply func. Aggregate result with passed information from original subtree node./// </summary>internalFunc<T,TApp,TApp> Apply {get;set;}/// <summary>/// Get children func. If it returns null or empty enumerable, subtree scan is stopped./// </summary>internalFunc<T,IEnumerable<T>> GetChildren {get;set;}/// <summary>/// True if "If" pattern matching function should be execute after/// all other matches and only if no other pattern apply./// </summary>internalboolIsElse{get;set;}}
Code above is provided as the idea. I don't feel it's perfect. I'm just reinvented it to solve particular and quite common problem of tree scan and transformation using functional approach and recursive pattern matching. It would be good if Succinc<T> do support similar feature, but more systematically and consistently implemented.
The text was updated successfully, but these errors were encountered:
@DavidArno I may fork Succinc<T> and then move some of my "recursive pattern matching" logic to library via pull request. You can modify it to make it more pretty later. It's ok?
That code already is successfully used in production in some of my projects. Just to mention, it's used in Html-to-Microsoft Word component in custom-made human resources management software used at my office.
That sounds great. I can't promise which bits I'll include, but real code will help me understand how your proposal might fit in with what I'm already doing, whether yours is a better, or just a different suggestion etc. So "pull request" away 👍
I am using functional approach already in the code which analyze and transform tree-like hierarchical collections (it's transformation of source HTML DOM into Word document paragraphs or internal markup-like representation to display it later on screen).
Naturally, pattern matching is the solution for this class of problems.
There are two basic functional algorithms I use:
When I have a look on
Succinc<T>
code it was interesting how my own implementation of this functional algorithms is very similar toSuccinc<T>
approach to pattern matching. So it would be good to extendSuccinc<T>
pattern matching for recursive object tree scan, matching and transformation.Basic idea is the "tree scan context" which contain original subtree nodes collection from same tree level, result collection of nodes (to insert transformed tree result) and function to get children from subtree node from context.
Example code looks like
Particularly compact representation of tree matching and transformation, I think.
Tree scan context looks like this:
Code above is provided as the idea. I don't feel it's perfect. I'm just reinvented it to solve particular and quite common problem of tree scan and transformation using functional approach and recursive pattern matching. It would be good if
Succinc<T>
do support similar feature, but more systematically and consistently implemented.The text was updated successfully, but these errors were encountered: