Created
June 25, 2010 13:20
-
-
Save jonfuller/452828 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Scratch | |
{ | |
public class Tester | |
{ | |
public class Taco<T> | |
{ | |
public T Filling { get; set; } | |
public Taco(T value) | |
{ | |
Filling = value; | |
} | |
} | |
public class Burrito<T> | |
{ | |
public T Filling { get; set; } | |
public Burrito(T value) | |
{ | |
Filling = value; | |
} | |
} | |
public class Tray<T> | |
{ | |
public T Food { get; set; } | |
public Tray(T value) | |
{ | |
Food = value; | |
} | |
} | |
#region Static Creators | |
public static class Tray | |
{ | |
public static Tray<T> Create<T>(T value) | |
{ | |
return new Tray<T>(value); | |
} | |
} | |
public static class Taco | |
{ | |
public static Taco<T> Create<T>(T value) | |
{ | |
return new Taco<T>(value); | |
} | |
} | |
public static class Burrito | |
{ | |
public static Burrito<T> Create<T>(T value) | |
{ | |
return new Burrito<T>(value); | |
} | |
} | |
#endregion | |
// a function for converting from a taco to a burrito | |
public static Burrito<T> ConvertTaco<T>(Taco<T> taco) | |
{ | |
return Burrito.Create(taco.Filling); | |
} | |
// here is what I want... a higher order function for mapping Trays | |
public static Tray<TOutput> ConvertTray<TInput, TOutput>(Tray<TInput> original, Func<TInput, TOutput> mapper) | |
{ | |
return Tray.Create(mapper(original.Food)); | |
} | |
public void Scratch() | |
{ | |
var withTaco = Tray.Create(Taco.Create(new ChickenQueso())); | |
// lame, one-off way to convert this tray | |
var withBurrito1 = Tray.Create(ConvertTaco(withTaco.Food)); | |
// less-lame, way to convert the tray | |
var withBurrito2 = ConvertTray(withTaco, ConvertTaco); | |
// yes! | |
var withBurrito3 = ConvertTray(withTaco, taco => Burrito.Create(taco.Filling)); | |
} | |
} | |
public class SteakFajita{} | |
public class ChickenQueso{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public static Tray ConvertTray<T1, T2>(Tray original, Func<T1, T2> mapper)
{
return Tray.Create(mapper(original.Food));
}
var withBurrito3 = ConvertTray(withTaco, (taco) => Burrito.Create(taco.Filling));