2

Functional languages often have Functor types/interfaces.

In .NET a Functor interface would be an Interface implementable by generic types (T<A>) that has a function called fmap that takes a function from the container type (A) to a different type (B) and returns an object with container type B.

For example a type List<A> would have a method List.fmap. The signature would be something like:

public List<B> fmap<B>(Func<A,B>);

Is it possible to create such an interface? Is it possible to do something similar? Are there any packages that provide an Interface like this and provide extension methods for obvious classes (List<T>, Dictionary<A,B>, Set<A>, etc.) ?

Update:

I know that IEnumerable objects almost do this. The key difference is that IEnumerable.Select returns an IEnumerable not the type of the implementer. For example, List.Select(A => B) returns IEnumerable not List. This shows why you can not implement IMappable in the obvious way for .NET, but is there a hack or extra type information you could use to effectively implement it?

2 Answers 2

3

The difficulty of giving a type to fmap is that it is parameterized by a higher-kinded type variable, the container type. So beyond being a simple polymorphic function, like say, map on lists:

map :: (a -> b) -> List a -> List b

fmap generalizes this to work on any container, as well as any element type:

fmap :: Functor (f :: * -> *) => (a -> b) -> f a -> f b

The problem in C# is that f is a higher-kinded type variable (it is a type function). Since C# doesn't support higher kinds, you'll be out of luck writing this directly, think.

2

Okay, based on the comment, I suspect the answer's no - you'd need higher-order generic types. You'd want something like:

// Not real syntax!
interface IFunctor<TFunctor<?>, TValue>
    where TFunctor<X> : IFunctor<TFunctor<X>, TValue>
{
    TFunctor<TProjection> Project<TProjection>(Func<TValue, TProjection> func);
}

The problem is trying to express the idea that the implementation of the interface must also be generic in a particular way, and allow that generic relationship to be used in the rest of the interface. That's not part of .NET generics, I'm afraid.

Joe Duffy has written about wishing for higher-order types before now - unfortunately I can't tell whether or not that article is relevant, as I can't get to it at the moment. (His blog server is somewhat temporamently, but the content is great :)


It's not entirely clear, but it's possible you're just talking about LINQ to Objects, basically. For example:

var kids = people.Where(x => x.Age < 18) // Filtering
                 .Select(x => x.Name)    // Projection
                 .ToList();

You could write a more general purpose interface, e.g.

public interface IFunctor<T>
{
    IFunctor<TOther> Foo<TOther>(Func<T, TOther> selector);
}

... but there's no such interface actually implemented by List<T> etc. LINQ to Objects works via extension methods instead.

6
  • Sorry, I should have clarified. I know that IEnumerable objects almost do this. The key difference is that IEnumerable<T>.Select returns an IEnumerable<B> not the type of the implementer<B>. For example, List<A>.Select(A => B) returns IEnumerable<B> not List<B>. This clarifies for me why you can not implement IMappable in the obvious way for .NET, but I wonder if there is some kind of a hack or extra type information you could use to effectively implement. Question updated to reflect this, thanks :) Jul 7, 2011 at 16:46
  • @John: Are you interested in the academic sense? It's not like you'll be able to make List<T> implement an interface that it doesn't already implement... but if you're interested in how it could work, that might be a different matter.
    – Jon Skeet
    Jul 7, 2011 at 16:52
  • @John: I've edited my answer - see if that's more useful to you.
    – Jon Skeet
    Jul 7, 2011 at 16:57
  • I believe you can implement interfaces for existing classes using extension methods: msdn.microsoft.com/en-us/library/bb383977.aspx Jul 7, 2011 at 18:52
  • 1
    @John: You can't actually implement interfaces. You can make it appear that you're adding methods, but you can't make a type actually implement an interface - if you do if (foo is IBar) it will still return false. Extension methods are just C# syntactic sugar for calling static methods.
    – Jon Skeet
    Jul 7, 2011 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.