Functional Programming in C# 2.0 - Bidirectional Folding

Last time we took a look at folding. It may not have escaped the readers notice that some operations are dependent on the order the parameters are processed. For instance in the string concatenation example the order the parameters are processed affects the order they appear in the output string. But in this example to reverse the order of result does not actually require access to the list in both directions, it is only necessary to reverse the order that the parameters are concatenated in:

 

// intList = {1, 2, 3, 4, 5, 6}

string result2 =

Fold(

delegate(int value1, string value2) { return value2 + ", " + value1; },

"",

intList);

// result2 = “, 1, 2, 3, 4, 5, 6”

 

string result3 =

Fold(

delegate(int value1, string value2) { return value1 + ", " + value2; },

"",

intList);

// result3 = “6, 5, 4, 3, 2, 1,”

 

However some operations such as division are trickier and process the list in different directions will give different results; we can just get away with process the parameters differently. So here we need to define a fold left and fold right functions:

 

public static TAcc FoldLeft<TList, TAcc>(

AStarBToB<TList, TAcc> funct,

TAcc acc,

IList<TList> list)

{

         for (int index = 0; index < list.Count; index ++ )

         {

                   acc = funct(list[index], acc);

         }

         return acc;

}

 

public static TAcc FoldRight<TList, TAcc>(

AStarBToB<TList, TAcc> funct,

TAcc acc,

IList<TList> list)

{

         for (int index = list.Count - 1; index >= 0 ; index--)

         {

                   acc = funct(list[index], acc);

         }

         return acc;

}

 

So here we can that processing a list of integers in different direction, will give different results:

// intList = {1, 2, 3, 4, 5, 6}

double result4 =

FoldLeft(

delegate(int value1, double value2) { return value1 / value2; },

1d,

intList);

// result4 = 3.2

 

// intList = {1, 2, 3, 4, 5, 6}

double result5 =

FoldRight(

delegate(int value1, double value2) { return value1 / value2; },

1d,

intList);

// result5 = 0.3125

 

Download the source for all these samples here.

Bookmark
dotnetkicks+, digg+, reddit+, del.icio.us+, dzone+, facebook+

Print | posted @ Saturday, August 13, 2005 7:48 AM

Comments on this entry:

No comments posted yet.

Your comment:

(Note: all comments are moderated so it may take sometime to appear)

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 7 and type the answer here:
 

 Subscribe in a reader

Links

CVMy CV
stackoverflowMy Stack Overflow CV
Twitter Follow me on Twitter
FaceBook View my Facebook
LinkedIn View my LinkedIn Profile
Viadeo Viadeo Profile (Fran�ais)

Conferences/Workshops

Robert Pickering:Robert Pickering's Beginning F# Workshop,  Robert Pickering's Beginning F# Workshop
2 DAY COURSE. Featuring Robert Pickering
London, Monday, May 10th
Progressive .NET Tutorials, Progressive .NET Tutorials
CONFERENCE (3 DAYS)
London, Wednesday, May 12th BOOK NOW!

Badges


Progressive .NET Tutorials 2009

Disclaimer

The views expressed on this weblog are mine and do not necessarily reflect the views of my employer.

All postings are provided "AS IS" with no warranties, and confer no rights.

www.flickr.com
This is a Flickr badge showing public photos and videos from Robert Pickering. Make your own badge here.