F# Extention Methods on Lists, IEnumberable, etc
Posted
by
flevine100
on Stack Overflow
See other posts from Stack Overflow
or by flevine100
Published on 2011-01-02T17:38:31Z
Indexed on
2011/01/02
17:53 UTC
Read the original article
Hit count: 277
I have searched StackOverflow (and other sources) for this answer, but can't seem to find anything.
In C#, if I had a widget definition, say:
class widget
{
public string PrettyName() { ... do stuff here }
}
and I wanted to allow for easy printing of a list of Widgets, I might do this:
namespace ExtensionMethods
{
public static PrintAll( this IEnumerable<Widget> widgets, TextWriter writer )
{
foreach(var w in widgets) { writer.WriteLine( w.PrettyName() ) }
}
}
How would I accomplish something similar with a record type and a collection (List or Seq preferrably in F#). I'd love to have a list of Widgest and be able to call a function right on the collection that did something like this. Assume (since it's F#) that the function would not be changing the state of the collection that it's attached to, but returning some new value.
© Stack Overflow or respective owner