CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var range = Enumerable.Range(1, 10);
Console.WriteLine(EnumerableFunctor.Map(range, x => x).SequenceEqual(range)); // выведет True
int? nullableTen = 10;
int? nullableNull = null;
Console.WriteLine(NullableFunctor.Map(nullableTen, x => x) == nullableTen); // выведет True
Console.WriteLine(NullableFunctor.Map(nullableNull, x => x) == nullableNull); // выведет True
}
}
public static class EnumerableFunctor
{
public static IEnumerable<B> Map<A, B>(IEnumerable<A> source, Func<A, B> map) =>
source.Select(map);
}
public static class NullableFunctor
{
public static B? Map<A, B>(A? source, Func<A, B> map) where A : struct where B : struct =>
source is A notNullSource ? map(notNullSource) : default(B?);
}