Skip to content

Instantly share code, notes, and snippets.

@bfriesen
Created December 3, 2012 07:13
Show Gist options
  • Save bfriesen/4193301 to your computer and use it in GitHub Desktop.
Save bfriesen/4193301 to your computer and use it in GitHub Desktop.
My solution to David Poeschl's (‏@dpoeschl) twitter code golf challenge
// Make this C# code compile with minimal additional code (no commenting, etc.): Foo(0)(1)("two")("three")(4)(5)("six")("seven")(8)(9)("ten!");
// https://twitter.com/dpoeschl/status/275464052216573952
// How much more code to enforce the argument type pattern (i.e. int, int, string string, int, int, string, string...)?
// https://twitter.com/dpoeschl/status/275477464183103488
// https://twitter.com/dpoeschl/status/275478110290444288
// https://twitter.com/brianfriesen/status/275479513490669568
delegate WhatTheFunc<T2,T3,T4,T1> WhatTheFunc<T1,T2,T3,T4>(T1 _);
void CreateFoo()
{
WhatTheFunc<int, int, string, string> Foo = null; // The first function needs to start out null because we have a cycle of functions.
WhatTheFunc<string, int, int, string> c = _ => Foo; // If it wasn't initially declared and assigned, we wouldn't be able to compile,
WhatTheFunc<string, string, int, int> b = _ => c; // since the second functon refers to the first (and the third to the second, etc).
WhatTheFunc<int, string, string, int> a = _ => b; // It is only after the rest of the cycle has been declared and assigned, that we can
Foo = _ => a; // assign the first function its real value (which is to return the fourth function).
// Compiles and runs
Foo(0)(1)("two")("three")(4)(5)("six")("seven")(8)(9)("ten!");
// None of these compile (they have the wrong pattern of argument types)
// Foo("0")(1)("two")("three")(4)(5)("six")("seven")(8)(9)("ten!");
// Foo(0)("1")("two")("three")(4)(5)("six")("seven")(8)(9)("ten!");
// Foo(0)(1)(2)("three")(4)(5)("six")("seven")(8)(9)("ten!");
// Foo(0)(1)("two")(3)(4)(5)("six")("seven")(8)(9)("ten!");
// Foo(0)(1)("two")("three")("4")(5)("six")("seven")(8)(9)("ten!");
// Foo(0)(1)("two")("three")(4)("5")("six")("seven")(8)(9)("ten!");
// Foo(0)(1)("two")("three")(4)(5)(6)("seven")(8)(9)("ten!");
// Foo(0)(1)("two")("three")(4)(5)("six")(7)(8)(9)("ten!");
// Foo(0)(1)("two")("three")(4)(5)("six")("seven")("8")(9)("ten!");
// Foo(0)(1)("two")("three")(4)(5)("six")("seven")(8)("9")("ten!");
// Foo(0)(1)("two")("three")(4)(5)("six")("seven")(8)(9)(10);
}
// Golf Version - 249 characters, including 62 characters from original problem:
delegate R<U,V,W,T> R<T,U,V,W>(T _);void F(){R<int,int,string,string> Foo=null;R<string,int,int,string> c=_=>Foo;R<string,string,int,int> b=_=>c;R<int,string,string,int> a=_=>b;Foo=_=>a;Foo(0)(1)("two")("three")(4)(5)("six")("seven")(8)(9)("ten!");}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment