Last active
August 29, 2015 14:18
-
-
Save retran/706c3f35ef5d97b7f711 to your computer and use it in GitHub Desktop.
object via closures
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
using System; | |
class Program | |
{ | |
static Func<int, dynamic> Atom(dynamic value) | |
{ | |
return index => value; | |
} | |
static Func<int, dynamic> Cons(Func<int, dynamic> a, Func<int, dynamic> b) | |
{ | |
return index => index == 0 ? a : b; | |
} | |
static Func<int, dynamic> Car(Func<int, dynamic> cons) | |
{ | |
return cons(0); | |
} | |
static Func<int, dynamic> Cdr(Func<int, dynamic> cons) | |
{ | |
return cons(1); | |
} | |
static Func<int, dynamic> Constructor(int age, string name) | |
{ | |
return Cons(Cons(Atom("age"), Atom(age)), Cons(Cons(Atom("name"), Atom(name)), null)); | |
} | |
static int GetAge(Func<int, dynamic> cons) | |
{ | |
return Cdr(Car(cons))(0); | |
} | |
static string GetName(Func<int, dynamic> cons) | |
{ | |
return Cdr(Car(Cdr(cons)))(0); | |
} | |
static void Main(string[] args) | |
{ | |
var record = Constructor(28, "Andrew"); | |
Console.WriteLine(GetAge(record)); | |
Console.WriteLine(GetName(record)); | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment