Skip to content

Instantly share code, notes, and snippets.

@jonfuller
Created June 25, 2010 13:20
Show Gist options
  • Save jonfuller/452828 to your computer and use it in GitHub Desktop.
Save jonfuller/452828 to your computer and use it in GitHub Desktop.
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{}
}
@jonfuller
Copy link
Author

Fixed! Sweet. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment