मैंने हाल ही में जोएल स्पोल्स्की का लेख पढ़ा
"क्या आपकी प्रोग्रामिंग भाषा ऐसा कर सकती है?" और मुझे यह इतना पसंद आया कि मैंने इसका अनुवाद करने का फैसला किया। लेकिन सिर्फ इतना ही नहीं बल्कि अपने आप को थोड़ा जोड़ें। अर्थात्, जावास्क्रिप्ट उदाहरणों के बजाय (मूल में जोएल द्वारा प्रयुक्त), मैंने सी # में उदाहरण लिखने का फैसला किया, जो आज मेरे करीब है। दरअसल, मैं कट के तहत समुदाय समुदाय को परिणाम प्रस्तुत करता हूं।
कल्पना कीजिए, आपके कोड को देखकर, आप दो बड़े ब्लॉकों पर ठोकर खाते हैं जो बहुत समान दिखते हैं। वास्तव में, वे समान हैं, बस एक ब्लॉक स्पेगेटी को संदर्भित करता है, और दूसरा चॉकलेट मूस को।
// - Main
Console.WriteLine(“I’d like some Spaghetti!”);
Console.WriteLine(“I’d like some Chocolate Moose!”);
C#, , - - , .
, :
public void SwedishChief(string food)
{
Console.WriteLine(string.Format(“I’d like some {0}!”, food));
}
SwedishChief(“Spaghetti”);
SwedishChief(“Chocolate Moose”);
, , , , - . , — , , = !
, , , BoomBoom, — PutInPot. , .
Console.WriteLine(“get the lobster”);
PutInPot(“water”);
PutInPot(“lobster”);
Console.WriteLine(“get the chicken”);
BoomBoom(“chicken”);
BoomBoom(“coconut”);
, . , , .
// :
// ,
// alert.
public static T Alert<T>(T message)
{
Console.WriteLine(message.ToString());
return message;
}
public void Cook(string ingredientOne, string ingredientTwo, Action<string> function)
{
Console.WriteLine(string.Format("get the {0}",ingredientOne));
function(ingredientOne);
function(ingredientTwo);
}
Cook(“lobster”, “water”, PutInPot);
Cook(“chicken”, “coconut”, BoomBoom);
! .
?
… , PutInPot BoomBoom. (inline), - .
Cook("lobster",
"water",
x => Alert("pot " + x));
Cook("chicken",
"coconut",
x => Alert("boom " + x));
, . , , , Cook()…
“ ”, , , , - .
...
var list = new List<int> { 1, 2, 3 };
for (var i = 0; i < list.Count; i++)
{
list[i] = list[i] * 2;
}
foreach (var el in list)
{
Alert(el.ToString());
}
...
- , , .
public static void Map<T>(Func<T, T> action, IList<T> list)
{
for (var i = 0; i < list.Count; i++)
list[i] = action(list[i]);
}
, :
...
Map((x) => x * 2, list);
Map(Alert, list);
...
, , - .
…
Alert(Sum(list));
Alert(Join(new[] { "a", "b", "c" }));
...
public static int Sum(IEnumerable<int> list)
{
var sum = 0;
foreach (var el in list)
sum += el;
return sum;
}
public static string Join(IEnumerable<string> list)
{
var result = string.Empty;
foreach (var el in list)
result += el;
return result;
}
Sum Join , - , .
public static T Reduce<T>(Func<T, T, T> func, IEnumerable<T> list, T init)
{
var accumulator = init;
foreach (var el in list)
accumulator = func(accumulator, el);
return accumulator;
}
public static int Sum(IEnumerable<int> list)
{
return Reduce((x, y) => x + y, list, 0);
}
public static string Join(IEnumerable<string> list)
{
return Reduce((a,b) => a + b, list, string.Empty);
}
. , ( , -). , - .
Java , , . , , . , . , !
, , , - ?
, map. - , , . -, - , ? . , , , map !
, , , - . , , , . map , .
, map .
, , map reduce , , , -, , map reduce , , , , .
. , , , .
“ -, , Java”.
MapReduce, Google . Map Reduce Lisp . MapReduce, , 6.001 , , , . , Google MapReduce, Microsoft, , Microsoft , , Google — Skynet^H^H^H^H^H^H — . , Microsoft , .
. , , , , , , . Google MapReduce, , - .
, , , . FORTRAN , , . , , , - . Java , . , Java —
.
P.S. FORTRAN 27 . -, . , , GW-BASIC.