Click here to Skip to main content
Click here to Skip to main content

Passing collections between functions - The functional way

By , 21 Jun 2012
 

Introduction

Passing collections between functions is a basic programming requirement. Usually I use any built-in .NET data structures such as a dictionary or list of any custom defined struct or class in order to do this. In many situations this procedure is something like create a list object and add values into it with or without a loop. Then pass that object into the function and finally extract values from that function using a loop. Here the collection act something like a bucket to keep the values together. In my experience, if the collection object creates only for this purpose, all these steps are unnecessary and causes lot of bugs (Sometimes the collection may tampered by some other code). So it was always my concern to avoid these issues. I found an alternative solution using functional programming.

The scenario

Suppose if I want to pass a set of key and value pair values into a function, usually I will declare the function like this.

public void PassValues(IDictionary<int,string> dict)
{
    foreach (KeyValuePair<int,string> item in dict)
    {
        Console.WriteLine(string.Format("Key : {0}, Value : {1}", item.Key, item.Value));
    }
} 

The following code is use to call this function.

IDictionary<int, string> dict = new Dictionary<int, string>();

dict.Add(1, "One");
dict.Add(2, "Two");
dict.Add(3, "Three");

PassValues(dict);   

The functional way

Using the functional programming we can avoid this dictionary and do the same thing in more flexible way. Most importantly, it will create an immutable collection. First step is to create two generic delegates.

public delegate void AddParam<TKey, TValue>(TKey key, TValue value);
public delegate void AddCollection<TKey, TValue>(AddParam<TKey, TValue> paramCollection);
The first delegate accepts two generic values and the second delegate accepts the first delegate as a parameter. Now we can rewrite the PassValues function as below.
public void PassValues(AddCollection<int,string> paramCollection)
{
    paramCollection((key, value) => { Console.WriteLine(string.Format("Key : {0}, Value : {1}", key, value)); });
} 

Since the PassValues function can receive a function as parameter, it becomes a high order function. The final step is to call this function.

PassValues((addParam) =>
    {
        addParam(1, "One");
        addParam(2, "Two");
        addParam(3, "Three");
    });

You can reuse this collection by passing a different piece of code. For example:

IList<string> lst = new List<string>();

paramCollection((key, value) => { if (key > 2) { lst.Add(value); } }); 

If you want to pass a different data structure, you need to declare new delegates.

Points of Interest

You should be careful while choosing this method. This is not suitable for the all the situations. For example if you want to persist this collection in an ASP.NET session or on the disk, better not to use this method.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kannan Ar
Software Developer (Senior)
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 21 Jun 2012
Article Copyright 2012 by Kannan Ar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid