473,472 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Function Composition in C# 3.0

I've been leaning pretty heavily on the new C# 3.0 features while
simultaneously playing around with Haskell and other FP-oriented
languages. One problem that comes up often is the lack of a function
composition syntax.

For instance, there's this bit of code in one of my projects:

/* Convert all doc files in the directory
to plain text, strip out extraneous formatting,
and run them through the summary parser */
var results = filenames.ConvertAll
(file =parseSummary(cleanTxt(docToTxt(file))))

It's not awful. It's just not ideal. In haskell I would just use the
dot notation:

map (parseSummary . cleanTxt . docToTxt) filenames

It's more readable and there's less parenthesis overload.

An alternative is to have a larger list of ConvertAll statements.
Assuming there's no compiler magic happening, this seems like a much
slower approach, although it is more readable than my first example.

var results = filenames
.ConvertAll(docToTxt)
.ConvertAll(cleanTxt)
.ConvertAll(parseSummary)

I'm sure other people trying to adopt a more functional style in C#
3.0 have come up against this. Has a popular pattern emerged for
dealing with this?

Cheers,
kd

Jun 18 '07 #1
1 1824
kd,

This is the whole point of Extension Methods. You can define a static
class with static methods using the "this" keyword for the first parameter
to define methods that you can call on the type of that parameter.

So, depending on the type of the file (let's say it is MyFile) you could
do:

namespace MyNamespace
{
public static class MyFileExtensions
{
public static string docToTxt(this MyFile file)
{
// Do stuff.
}
}
}

Now, when you have a reference to this class (or it is in the same
project) and you have the namespace the class belongs in a using statement
at the top of a code file, you can do this:

// Needed for the extension method if not in that namespace.
using MyNamespace;

// Somewhere in code:
MyFile myFile = ...;

// Call the method.
myFile.docToTxt();

Using this technique (and you can define more than one extension method
on a type), you can end up doing this:

var result = filename.docToTxt().cleanTxt().parseSummary();

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ke********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
I've been leaning pretty heavily on the new C# 3.0 features while
simultaneously playing around with Haskell and other FP-oriented
languages. One problem that comes up often is the lack of a function
composition syntax.

For instance, there's this bit of code in one of my projects:

/* Convert all doc files in the directory
to plain text, strip out extraneous formatting,
and run them through the summary parser */
var results = filenames.ConvertAll
(file =parseSummary(cleanTxt(docToTxt(file))))

It's not awful. It's just not ideal. In haskell I would just use the
dot notation:

map (parseSummary . cleanTxt . docToTxt) filenames

It's more readable and there's less parenthesis overload.

An alternative is to have a larger list of ConvertAll statements.
Assuming there's no compiler magic happening, this seems like a much
slower approach, although it is more readable than my first example.

var results = filenames
.ConvertAll(docToTxt)
.ConvertAll(cleanTxt)
.ConvertAll(parseSummary)

I'm sure other people trying to adopt a more functional style in C#
3.0 have come up against this. Has a popular pattern emerged for
dealing with this?

Cheers,
kd

Jun 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alan G Isaac | last post by:
Given a list of functions, it seems there must be a Pythonic approach to composition. Something like def compose(fns): return lambda x: reduce(lambda f,g: f(g),fns)(x) This will not work...
6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
4
by: fog | last post by:
Given: class A; and B "has-a" A. The composition relationship between A and B can be implemented in three ways: =================== # 1. class B {
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
3
by: jyck91 | last post by:
I want to get some ideas to organise a composition analyzer?? eg. frequencies of letters Thx for your suggestion
5
by: jyck91 | last post by:
Can anyone give me some ideas to organise a composition analyzer in C? It analyses an english composition. What functions should it include? eg. frequency of letter/ words thanks for help
4
by: Kay Schluehr | last post by:
As you know, there is no operator for function composition in Python. When you have two functions F and G and want to express the composition F o G you have to create a new closure lambda...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.