54

Just a little niggle about LINQ syntax. I'm flattening an IEnumerable<IEnumerable<T>> with SelectMany(x => x).

My problem is with the lambda expression x => x. It looks a bit ugly. Is there some static 'identity function' object that I can use instead of x => x? Something like SelectMany(IdentityFunction)?

1
  • 9
    I'll never understand why they don't include SKI combinators into c#. Sep 23, 2009 at 15:30

7 Answers 7

38

Unless I misunderstand the question, the following seems to work fine for me in C# 4:

public static class Defines
{
   public static T Identity<T>(T pValue)
   {
      return pValue;
   }

   ...

You can then do the following in your example:

var result =
   enumerableOfEnumerables
   .SelectMany(Defines.Identity);

As well as use Defines.Identity anywhere you would use a lambda that looks like x => x.

1
  • 6
    I know this was a late submission, but I really feel like it answers the question better.
    – Anthony
    Nov 5, 2012 at 20:59
31

Note: this answer was correct for C# 3, but at some point (C# 4? C# 5?) type inference improved so that the IdentityFunction method shown below can be used easily.


No, there isn't. It would have to be generic, to start with:

public static Func<T, T> IdentityFunction<T>()
{
    return x => x;
}

But then type inference wouldn't work, so you'd have to do:

SelectMany(Helpers.IdentityFunction<Foo>())

which is a lot uglier than x => x.

Another possibility is that you wrap this in an extension method:

public static IEnumerable<T> Flatten<T>
    (this IEnumerable<IEnumerable<T>> source)
{
    return source.SelectMany(x => x);
}

Unfortunately with generic variance the way it is, that may well fall foul of various cases in C# 3... it wouldn't be applicable to List<List<string>> for example. You could make it more generic:

public static IEnumerable<TElement> Flatten<TElement, TWrapper>
    (this IEnumerable<TWrapper> source) where TWrapper : IEnumerable<TElement>
{
    return source.SelectMany(x => x);
}

But again, you've then got type inference problems, I suspect...

EDIT: To respond to the comments... yes, C# 4 makes this easier. Or rather, it makes the first Flatten method more useful than it is in C# 3. Here's an example which works in C# 4, but doesn't work in C# 3 because the compiler can't convert from List<List<string>> to IEnumerable<IEnumerable<string>>:

using System;
using System.Collections.Generic;
using System.Linq;

public static class Extensions
{
    public static IEnumerable<T> Flatten<T>
        (this IEnumerable<IEnumerable<T>> source)
    {
        return source.SelectMany(x => x);
    }
}

class Test
{
    static void Main()
    {
        List<List<string>> strings = new List<List<string>>
        {
            new List<string> { "x", "y", "z" },
            new List<string> { "0", "1", "2" }
        };

        foreach (string x in strings.Flatten())
        {
            Console.WriteLine(x);
        }
    }
}
9
  • 2
    Thanks! That's given me some interesting leads too. I think I'll stay with x => x for the time being...
    – Joe
    Sep 23, 2009 at 15:29
  • 3
    why did you write a function that returns a function? shouldn't all you need be a single function with a Func<T, T> signature that does nothing but return its input? Jun 5, 2012 at 8:00
  • 3
    @JonSkeet I mean for the identity function. You wrote public static Func<T, T> IdentityFunction<T>() which isn't an identity function, it's a function that returns an identity function. Was that necessary in C# 3? I have added my version as an answer. Jun 5, 2012 at 8:06
  • 1
    @JonSkeet I realise this is VERY old - but is it possible that in VS2012 and newer this problem is solved - and we can directly use a static Id function (instead of your suggested generator function)? A quick performace comparion sugessted this...
    – FrankB
    Aug 2, 2016 at 7:19
  • 1
    @FrankB: Hmm. Not sure when that was improved, but yes, it seems okay with C# 6, anyway. Can't edit right now, but will do when I get a chance.
    – Jon Skeet
    Aug 2, 2016 at 7:37
14

With C# 6.0 and if you reference FSharp.Core you can do:

using static Microsoft.FSharp.Core.Operators

And then you're free to do:

SelectMany(Identity)
1
  • 11
    FSharp.Core is a big dependency to ask. probably not worth it unless you're actually writing F#
    – bug
    Sep 13, 2018 at 15:18
8

With C# 6.0 things are getting better. We can define the identity function in the way suggested by @Sahuagin:

static class Functions
{
    public static T It<T>(T item) => item;
}

And then use it in SelectMany the using static constructor:

using Functions;

...

var result = enumerableOfEnumerables.SelectMany(It);

I think it looks very laconic in the such way. I also find the identity function useful when building dictionaries:

class P
{
    P(int id, string name) // Sad. We are not getting primary constructors in C# 6.0
    {
        ID = id;
        Name = id;
    }

    int ID { get; }
    int Name { get; }

    static void Main(string[] args)
    {
        var items = new[] { new P(1, "Jack"), new P(2, "Jill"), new P(3, "Peter") };
        var dict = items.ToDictionary(x => x.ID, It);
    }
}
1
  • 2
    Great answer! In your code sample you forgot to write static in using static Functions
    – andy
    Dec 31, 2019 at 12:20
3

This may work in the way you want. I realize Jon posted a version of this solution, but he has a second type parameter which is only necessary if the resulting sequence type is different from the source sequence type.

public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source)
    where T : IEnumerable<T>
{
    return source.SelectMany(item => item);
}
2

You can get close to what you need. Instead of a regular static function, consider an extension method for your IEnumerable<T>, as if the identity function is of the collection, not the type (a collection can generate the identity function of its items):

public static Func<T, T> IdentityFunction<T>(this IEnumerable<T> enumerable)
{
     return x => x;
}

with this, you don't have to specify the type again, and write:

IEnumerable<IEnumerable<T>> deepList = ... ;
var flat = deepList.SelectMany(deepList.IdentityFunction());

This does feel a bit abusive though, and I'd probably go with x=>x. Also, you cannot use it fluently (in chaining), so it will not always be useful.

2
  • You need to return Func<T, IEnumerable<T>> to make it work with SelectMany. Sep 23, 2009 at 23:23
  • 1
    True, it works for Select but fails for SelectMany - it cant figure out the types. shame.
    – Kobi
    Sep 24, 2009 at 6:25
1

I'd go with a simple class with a single static property and add as many as required down the line

    internal class IdentityFunction<TSource>
    {
        public static Func<TSource, TSource> Instance
        {
            get { return x => x; }
        }
    }

    SelectMany(IdentityFunction<Foo>.Instance)

Your Answer

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

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