diff options
51 files changed, 6598 insertions, 1010 deletions
diff --git a/c++.html.markdown b/c++.html.markdown index ff2a98fd..4acc1b9d 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -4,6 +4,8 @@ filename: learncpp.cpp contributors: - ["Steven Basart", "http://github.com/xksteven"] - ["Matt Kline", "https://github.com/mrkline"] + - ["Geoff Liu", "http://geoffliu.me"] + - ["Connor Waters", "http://github.com/connorwaters"] lang: en --- @@ -52,11 +54,11 @@ int main(int argc, char** argv) // However, C++ varies in some of the following ways: -// In C++, character literals are one byte. -sizeof('c') == 1 +// In C++, character literals are chars +sizeof('c') == sizeof(char) == 1 -// In C, character literals are the same size as ints. -sizeof('c') == sizeof(10) +// In C, character literals are ints +sizeof('c') == sizeof(int) // C++ has strict prototyping @@ -158,11 +160,12 @@ void foo() int main() { - // Assume everything is from the namespace "Second" - // unless otherwise specified. + // Includes all symbols from namespace Second into the current scope. Note + // that simply foo() no longer works, since it is now ambiguous whether + // we're calling the foo in namespace Second or the top level. using namespace Second; - foo(); // prints "This is Second::foo" + Second::foo(); // prints "This is Second::foo" First::Nested::foo(); // prints "This is First::Nested::foo" ::foo(); // prints "This is global foo" } @@ -248,6 +251,59 @@ const string& barRef = bar; // Create a const reference to bar. // Like C, const values (and pointers and references) cannot be modified. barRef += ". Hi!"; // Error, const references cannot be modified. +// Sidetrack: Before we talk more about references, we must introduce a concept +// called a temporary object. Suppose we have the following code: +string tempObjectFun() { ... } +string retVal = tempObjectFun(); + +// What happens in the second line is actually: +// - a string object is returned from tempObjectFun +// - a new string is constructed with the returned object as arugment to the +// constructor +// - the returned object is destroyed +// The returned object is called a temporary object. Temporary objects are +// created whenever a function returns an object, and they are destroyed at the +// end of the evaluation of the enclosing expression (Well, this is what the +// standard says, but compilers are allowed to change this behavior. Look up +// "return value optimization" if you're into this kind of details). So in this +// code: +foo(bar(tempObjectFun())) + +// assuming foo and bar exist, the object returned from tempObjectFun is +// passed to bar, and it is destroyed before foo is called. + +// Now back to references. The exception to the "at the end of the enclosing +// expression" rule is if a temporary object is bound to a const reference, in +// which case its life gets extended to the current scope: + +void constReferenceTempObjectFun() { + // constRef gets the temporary object, and it is valid until the end of this + // function. + const string& constRef = tempObjectFun(); + ... +} + +// Another kind of reference introduced in C++11 is specifically for temporary +// objects. You cannot have a variable of its type, but it takes precedence in +// overload resolution: + +void someFun(string& s) { ... } // Regular reference +void someFun(string&& s) { ... } // Reference to temporary object + +string foo; +someFun(foo); // Calls the version with regular reference +someFun(tempObjectFun()); // Calls the version with temporary reference + +// For example, you will see these two versions of constructors for +// std::basic_string: +basic_string(const basic_string& other); +basic_string(basic_string&& other); + +// Idea being if we are constructing a new string from a temporary object (which +// is going to be destroyed soon anyway), we can have a more efficient +// constructor that "salvages" parts of that temporary string. You will see this +// concept referred to as "move semantics". + ////////////////////////////////////////// // Classes and object-oriented programming ////////////////////////////////////////// @@ -294,7 +350,10 @@ public: // These are called when an object is deleted or falls out of scope. // This enables powerful paradigms such as RAII // (see below) - // Destructors must be virtual to allow classes to be derived from this one. + // The destructor should be virtual if a class is to be derived from; + // if it is not virtual, then the derived class' destructor will + // not be called if the object is destroyed through a base-class reference + // or pointer. virtual ~Dog(); }; // A semicolon must follow the class definition. @@ -437,9 +496,10 @@ int main () { ///////////////////// // Templates in C++ are mostly used for generic programming, though they are -// much more powerful than generics constructs in other languages. It also -// supports explicit and partial specialization, functional-style type classes, -// and also it's Turing-complete. +// much more powerful than generic constructs in other languages. They also +// support explicit and partial specialization and functional-style type +// classes; in fact, they are a Turing-complete functional language embedded +// in C++! // We start with the kind of generic programming you might be familiar with. To // define a class or function that takes a type parameter: @@ -451,7 +511,7 @@ public: }; // During compilation, the compiler actually generates copies of each template -// with parameters substituted, and so the full definition of the class must be +// with parameters substituted, so the full definition of the class must be // present at each invocation. This is why you will see template classes defined // entirely in header files. @@ -465,13 +525,13 @@ intBox.insert(123); Box<Box<int> > boxOfBox; boxOfBox.insert(intBox); -// Up until C++11, you must place a space between the two '>'s, otherwise '>>' -// will be parsed as the right shift operator. +// Until C++11, you had to place a space between the two '>'s, otherwise '>>' +// would be parsed as the right shift operator. // You will sometimes see // template<typename T> -// instead. The 'class' keyword and 'typename' keyword are _mostly_ -// interchangeable in this case. For full explanation, see +// instead. The 'class' keyword and 'typename' keywords are _mostly_ +// interchangeable in this case. For the full explanation, see // http://en.wikipedia.org/wiki/Typename // (yes, that keyword has its own Wikipedia page). @@ -527,12 +587,15 @@ try { // Do not allocate exceptions on the heap using _new_. throw std::runtime_error("A problem occurred"); } + // Catch exceptions by const reference if they are objects catch (const std::exception& ex) { - std::cout << ex.what(); + std::cout << ex.what(); +} + // Catches any exception not caught by previous _catch_ blocks -} catch (...) +catch (...) { std::cout << "Unknown exception caught"; throw; // Re-throws the exception @@ -542,8 +605,8 @@ catch (const std::exception& ex) // RAII /////// -// RAII stands for Resource Allocation Is Initialization. -// It is often considered the most powerful paradigm in C++, +// RAII stands for "Resource Acquisition Is Initialization". +// It is often considered the most powerful paradigm in C++ // and is the simple concept that a constructor for an object // acquires that object's resources and the destructor releases them. @@ -564,9 +627,9 @@ void doSomethingWithAFile(const char* filename) // Unfortunately, things are quickly complicated by error handling. // Suppose fopen can fail, and that doSomethingWithTheFile and // doSomethingElseWithIt return error codes if they fail. -// (Exceptions are the preferred way of handling failure, -// but some programmers, especially those with a C background, -// disagree on the utility of exceptions). +// (Exceptions are the preferred way of handling failure, +// but some programmers, especially those with a C background, +// disagree on the utility of exceptions). // We now have to check each call for failure and close the file handle // if a problem occurred. bool doSomethingWithAFile(const char* filename) @@ -680,26 +743,29 @@ class Foo { virtual void bar(); }; class FooSub : public Foo { - virtual void bar(); // overrides Foo::bar! + virtual void bar(); // Overrides Foo::bar! }; // 0 == false == NULL (most of the time)! bool* pt = new bool; -*pt = 0; // Sets the value points by 'pt' to false. +*pt = 0; // Sets the value points by 'pt' to false. pt = 0; // Sets 'pt' to the null pointer. Both lines compile without warnings. // nullptr is supposed to fix some of that issue: int* pt2 = new int; -*pt2 = nullptr; // Doesn't compile +*pt2 = nullptr; // Doesn't compile pt2 = nullptr; // Sets pt2 to null. -// But somehow 'bool' type is an exception (this is to make `if (ptr)` compile). +// There is an exception made for bools. +// This is to allow you to test for null pointers with if(!ptr), +// but as a consequence you can assign nullptr to a bool directly! *pt = nullptr; // This still compiles, even though '*pt' is a bool! // '=' != '=' != '='! -// Calls Foo::Foo(const Foo&) or some variant copy constructor. +// Calls Foo::Foo(const Foo&) or some variant (see move semantics) copy +// constructor. Foo f2; Foo f1 = f2; @@ -713,6 +779,22 @@ Foo f1 = fooSub; Foo f1; f1 = f2; + +// How to truly clear a container: +class Foo { ... }; +vector<Foo> v; +for (int i = 0; i < 10; ++i) + v.push_back(Foo()); + +// Following line sets size of v to 0, but destructors don't get called +// and resources aren't released! +v.empty(); +v.push_back(Foo()); // New value is copied into the first Foo we inserted + +// Truly destroys all values in v. See section about temporary objects for +// explanation of why this works. +v.swap(vector<Foo>()); + ``` Further Reading: diff --git a/c.html.markdown b/c.html.markdown index d3f20eda..8e631de4 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -18,9 +18,9 @@ memory management and C will take you as far as you need to go. ```c // Single-line comments start with // - only available in C99 and later. - /* +/* Multi-line comments look like this. They work in C89 as well. - */ +*/ /* Multi-line comments don't nest /* Be careful */ // comment ends on this line... @@ -55,7 +55,7 @@ int add_two_ints(int x1, int x2); // function prototype // Your program's entry point is a function called // main with an integer return type. -int main() { +int main(void) { // print output using printf, for "print formatted" // %d is an integer, \n is a newline printf("%d\n", 0); // => Prints 0 @@ -157,12 +157,12 @@ int main() { int cha = 'a'; // fine char chb = 'a'; // fine too (implicit conversion from int to char) - //Multi-dimensional arrays: + // Multi-dimensional arrays: int multi_array[2][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 0} }; - //access elements: + // access elements: int array_int = multi_array[0][2]; // => 3 /////////////////////////////////////// @@ -183,8 +183,8 @@ int main() { i1 / i2; // => 0 (0.5, but truncated towards 0) // You need to cast at least one integer to float to get a floating-point result - (float)i1 / i2 // => 0.5f - i1 / (double)i2 // => 0.5 // Same with double + (float)i1 / i2; // => 0.5f + i1 / (double)i2; // => 0.5 // Same with double f1 / f2; // => 0.5, plus or minus epsilon // Floating-point numbers and calculations are not exact @@ -219,13 +219,13 @@ int main() { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 - //Conditional expression ( ? : ) + // Conditional expression ( ? : ) int e = 5; int f = 10; int z; z = (e > f) ? e : f; // => 10 "if e > f return e, else return f." - //Increment and decrement operators: + // Increment and decrement operators: char *s = "iLoveC"; int j = 0; s[j++]; // => "i". Returns the j-th item of s THEN increments value of j. @@ -371,7 +371,7 @@ int main() { x_array[xx] = 20 - xx; } // Initialize x_array to 20, 19, 18,... 2, 1 - // Declare a pointer of type int and initialize it to point to x_array + // Declare a pointer of type int and initialize it to point to x_array int* x_ptr = x_array; // x_ptr now points to the first element in the array (the integer 20). // This works because arrays often decay into pointers to their first element. @@ -404,8 +404,8 @@ int main() { *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) - // Dereferencing memory that you haven't allocated gives - // "unpredictable results" - the program is said to invoke "undefined behavior" + // Dereferencing memory that you haven't allocated gives + // "unpredictable results" - the program is said to invoke "undefined behavior" printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash. // When you're done with a malloc'd block of memory, you need to free it, @@ -471,13 +471,13 @@ str_reverse(c); printf("%s\n", c); // => ".tset a si sihT" */ -//if referring to external variables outside function, must use extern keyword. +// if referring to external variables outside function, must use extern keyword. int i = 0; void testFunc() { extern int i; //i here is now using external variable i } -//make external variables private to source file with static: +// make external variables private to source file with static: static int j = 0; //other files using testFunc2() cannot access variable j void testFunc2() { extern int j; @@ -634,7 +634,7 @@ Best to find yourself a copy of [K&R, aka "The C Programming Language"](https:// It is *the* book about C, written by Dennis Ritchie, the creator of C, and Brian Kernighan. Be careful, though - it's ancient and it contains some inaccuracies (well, ideas that are not considered good anymore) or now-changed practices. -Another good resource is [Learn C the hard way](http://c.learncodethehardway.org/book/). +Another good resource is [Learn C The Hard Way](http://c.learncodethehardway.org/book/). If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com). diff --git a/chapel.html.markdown b/chapel.html.markdown index c8489371..05e5b867 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -1074,14 +1074,14 @@ Installing the Compiler Chapel can be built and installed on your average 'nix machine (and cygwin). [Download the latest release version](https://github.com/chapel-lang/chapel/releases/) -and its as easy as +and it's as easy as 1. `tar -xvf chapel-1.11.0.tar.gz` 2. `cd chapel-1.11.0` 3. `make` 4. `source util/setchplenv.bash # or .sh or .csh or .fish` -You will need to `source util/setchplenv.EXT` from within the Chapel directory (`$CHPL_HOME`) every time your terminal starts so its suggested that you drop that command in a script that will get executed on startup (like .bashrc). +You will need to `source util/setchplenv.EXT` from within the Chapel directory (`$CHPL_HOME`) every time your terminal starts so it's suggested that you drop that command in a script that will get executed on startup (like .bashrc). Chapel is easily installed with Brew for OS X @@ -1100,4 +1100,4 @@ Notable arguments: * `--fast`: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. * `--set <Symbol Name>=<Value>`: set config param `<Symbol Name>` to `<Value>` at compile-time. * `--main-module <Module Name>`: use the main() procedure found in the module `<Module Name>` as the executable's main. - * `--module-dir <Directory>`: includes `<Directory>` in the module search path.
\ No newline at end of file + * `--module-dir <Directory>`: includes `<Directory>` in the module search path. diff --git a/cs-cz/python3.html.markdown b/cs-cz/python3.html.markdown new file mode 100644 index 00000000..11c8a654 --- /dev/null +++ b/cs-cz/python3.html.markdown @@ -0,0 +1,636 @@ +--- +language: python3 +contributors: + - ["Louie Dinh", "http://pythonpracticeprojects.com"] + - ["Steven Basart", "http://github.com/xksteven"] + - ["Andre Polykanine", "https://github.com/Oire"] + - ["Tomáš Bedřich", "http://tbedrich.cz"] +translators: + - ["Tomáš Bedřich", "http://tbedrich.cz"] +filename: learnpython3.py +lang: cs-cz +--- + +Python byl vytvořen Guidem Van Rossum v raných 90. letech. Nyní je jedním z nejpopulárnějších jazyků. +Zamiloval jsem si Python pro jeho syntaktickou čistotu - je to vlastně spustitelný pseudokód. + +Vaše zpětná vazba je vítána! Můžete mě zastihnout na [@louiedinh](http://twitter.com/louiedinh) nebo louiedinh [at] [email od googlu] anglicky, +autora českého překladu pak na [@tbedrich](http://twitter.com/tbedrich) nebo ja [at] tbedrich.cz + +Poznámka: Tento článek je zaměřen na Python 3. Zde se můžete [naučit starší Python 2.7](http://learnxinyminutes.com/docs/python/). + +```python + +# Jednořádkový komentář začíná křížkem + +""" Víceřádkové komentáře používají tři uvozovky nebo apostrofy + a jsou často využívány jako dokumentační komentáře k metodám +""" + +#################################################### +## 1. Primitivní datové typy a operátory +#################################################### + +# Čísla +3 # => 3 + +# Aritmetické operace se chovají běžným způsobem +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 + +# Až na dělení, které vrací desetinné číslo +35 / 5 # => 7.0 + +# Při celočíselném dělení je desetinná část oříznuta (pro kladná i záporná čísla) +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 # celočíselně dělit lze i desetinným číslem +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 + +# Pokud použiteje desetinné číslo, výsledek je jím také +3 * 2.0 # => 6.0 + +# Modulo +7 % 3 # => 1 + +# Mocnění (x na y-tou) +2**4 # => 16 + +# Pro vynucení priority použijte závorky +(1 + 3) * 2 # => 8 + +# Logické hodnoty +True +False + +# Negace se provádí pomocí not +not True # => False +not False # => True + +# Logické operátory +# U operátorů záleží na velikosti písmen +True and False # => False +False or True # => True + +# Používání logických operátorů s čísly +0 and 2 # => 0 +-5 or 0 # => -5 +0 == False # => True +2 == True # => False +1 == True # => True + +# Rovnost je == +1 == 1 # => True +2 == 1 # => False + +# Nerovnost je != +1 != 1 # => False +2 != 1 # => True + +# Další porovnání +1 < 10 # => True +1 > 10 # => False +2 <= 2 # => True +2 >= 2 # => True + +# Porovnání se dají řetězit! +1 < 2 < 3 # => True +2 < 3 < 2 # => False + + +# Řetězce používají " nebo ' a mohou obsahovat UTF8 znaky +"Toto je řetězec." +'Toto je také řetězec.' + +# Řetězce se také dají sčítat, ale nepoužívejte to +"Hello " + "world!" # => "Hello world!" +# Dají se spojovat i bez '+' +"Hello " "world!" # => "Hello world!" + +# Řetězec lze považovat za seznam znaků +"Toto je řetězec"[0] # => 'T' + +# .format lze použít ke skládání řetězců +"{} mohou být {}".format("řetězce", "skládány") + +# Formátovací argumenty můžete opakovat +"{0} {1} stříkaček stříkalo přes {0} {1} střech".format("tři sta třicet tři", "stříbrných") +# => "tři sta třicet tři stříbrných stříkaček stříkalo přes tři sta třicet tři stříbrných střech" + +# Pokud nechcete počítat, můžete použít pojmenované argumenty +"{jmeno} si dal {jidlo}".format(jmeno="Franta", jidlo="guláš") # => "Franta si dal guláš" + +# Pokud zároveň potřebujete podporovat Python 2.5 a nižší, můžete použít starší způsob formátování +"%s se dají %s jako v %s" % ("řetězce", "skládat", "jazyce C") + + +# None je objekt (jinde NULL, nil, ...) +None # => None + +# Pokud porovnáváte něco s None, nepoužívejte operátor rovnosti "==", +# použijte raději operátor "is", který testuje identitu. +"něco" is None # => False +None is None # => True + +# None, 0, a prázdný řetězec/seznam/slovník se vyhodnotí jako False +# Vše ostatní se vyhodnotí jako True +bool(0) # => False +bool("") # => False +bool([]) # => False +bool({}) # => False + + +#################################################### +## 2. Proměnné a kolekce +#################################################### + +# Python má funkci print +print("Jsem 3. Python 3.") + +# Proměnné není třeba deklarovat před přiřazením +# Konvence je používat male_pismo_s_podtrzitky +nazev_promenne = 5 +nazev_promenne # => 5 +# Názvy proměnných mohou obsahovat i UTF8 znaky +název_proměnné = 5 + +# Přístup k předtím nepoužité proměnné vyvolá výjimku +# Odchytávání vyjímek - viz další kapitola +neznama_promenna # Vyhodí NameError + +# Seznam se používá pro ukládání sekvencí +sez = [] +# Lze ho rovnou naplnit +jiny_seznam = [4, 5, 6] + +# Na konec seznamu se přidává pomocí append +sez.append(1) # sez je nyní [1] +sez.append(2) # sez je nyní [1, 2] +sez.append(4) # sez je nyní [1, 2, 4] +sez.append(3) # sez je nyní [1, 2, 4, 3] +# Z konce se odebírá se pomocí pop +sez.pop() # => 3 a sez je nyní [1, 2, 4] +# Vložme trojku zpátky +sez.append(3) # sez je nyní znovu [1, 2, 4, 3] + +# Přístup k prvkům funguje jako v poli +sez[0] # => 1 +# Mínus počítá odzadu (-1 je poslední prvek) +sez[-1] # => 3 + +# Přístup mimo seznam vyhodí IndexError +sez[4] # Vyhodí IndexError + +# Pomocí řezů lze ze seznamu vybírat různé intervaly +# (pro matematiky: jedná se o uzavřený/otevřený interval) +sez[1:3] # => [2, 4] +# Odříznutí začátku +sez[2:] # => [4, 3] +# Odříznutí konce +sez[:3] # => [1, 2, 4] +# Vybrání každého druhého prvku +sez[::2] # =>[1, 4] +# Vrácení seznamu v opačném pořadí +sez[::-1] # => [3, 4, 2, 1] +# Lze použít jakoukoliv kombinaci parametrů pro vytvoření složitějšího řezu +# sez[zacatek:konec:krok] + +# Odebírat prvky ze seznamu lze pomocí del +del sez[2] # sez je nyní [1, 2, 3] + +# Seznamy můžete sčítat +# Hodnoty sez a jiny_seznam přitom nejsou změněny +sez + jiny_seznam # => [1, 2, 3, 4, 5, 6] + +# Spojit seznamy lze pomocí extend +sez.extend(jiny_seznam) # sez je nyní [1, 2, 3, 4, 5, 6] + +# Kontrola, jestli prvek v seznamu existuje, se provádí pomocí in +1 in sez # => True + +# Délku seznamu lze zjistit pomocí len +len(sez) # => 6 + + +# N-tice je jako seznam, ale je neměnná +ntice = (1, 2, 3) +ntice[0] # => 1 +ntice[0] = 3 # Vyhodí TypeError + +# S n-ticemi lze dělat většinu operací, jako se seznamy +len(ntice) # => 3 +ntice + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) +ntice[:2] # => (1, 2) +2 in ntice # => True + +# N-tice (nebo seznamy) lze rozbalit do proměnných jedním přiřazením +a, b, c = (1, 2, 3) # a je nyní 1, b je nyní 2 a c je nyní 3 +# N-tice jsou vytvářeny automaticky, když vynecháte závorky +d, e, f = 4, 5, 6 +# Prohození proměnných je tak velmi snadné +e, d = d, e # d je nyní 5, e je nyní 4 + + +# Slovníky ukládají klíče a hodnoty +prazdny_slovnik = {} +# Lze je také rovnou naplnit +slovnik = {"jedna": 1, "dva": 2, "tři": 3} + +# Přistupovat k hodnotám lze pomocí [] +slovnik["jedna"] # => 1 + +# Všechny klíče dostaneme pomocí keys() jako iterovatelný objekt. Nyní ještě +# potřebujeme obalit volání v list(), abychom dostali seznam. To rozebereme +# později. Pozor, že jakékoliv pořadí klíčů není garantováno - může být různé. +list(slovnik.keys()) # => ["dva", "jedna", "tři"] + +# Všechny hodnoty opět jako iterovatelný objekt získáme pomocí values(). Opět +# tedy potřebujeme použít list(), abychom dostali seznam. Stejně jako +# v předchozím případě, pořadí není garantováno a může být různé +list(slovnik.values()) # => [3, 2, 1] + +# Operátorem in se lze dotázat na přítomnost klíče +"jedna" in slovnik # => True +1 in slovnik # => False + +# Přístup k neexistujícímu klíči vyhodí KeyError +slovnik["čtyři"] # Vyhodí KeyError + +# Metoda get() funguje podobně jako [], ale vrátí None místo vyhození KeyError +slovnik.get("jedna") # => 1 +slovnik.get("čtyři") # => None +# Metodě get() lze předat i výchozí hodnotu místo None +slovnik.get("jedna", 4) # => 1 +slovnik.get("čtyři", 4) # => 4 + +# metoda setdefault() vloží prvek do slovníku pouze pokud tam takový klíč není +slovnik.setdefault("pět", 5) # slovnik["pět"] je nastaven na 5 +slovnik.setdefault("pět", 6) # slovnik["pět"] je pořád 5 + +# Přidání nové hodnoty do slovníku +slovnik["čtyři"] = 4 +# Hromadně aktualizovat nebo přidat data lze pomocí update(), parametrem je opět slovník +slovnik.update({"čtyři": 4}) # slovnik je nyní {"jedna": 1, "dva": 2, "tři": 3, "čtyři": 4, "pět": 5} + +# Odebírat ze slovníku dle klíče lze pomocí del +del slovnik["jedna"] # odebere klíč "jedna" ze slovnik + + +# Množiny ukládají ... překvapivě množiny +prazdna_mnozina = set() +# Také je lze rovnou naplnit. A ano, budou se vám plést se slovníky. Bohužel. +mnozina = {1, 1, 2, 2, 3, 4} # mnozina je nyní {1, 2, 3, 4} + +# Přidání položky do množiny +mnozina.add(5) # mnozina je nyní {1, 2, 3, 4, 5} + +# Průnik lze udělat pomocí operátoru & +jina_mnozina = {3, 4, 5, 6} +mnozina & jina_mnozina # => {3, 4, 5} + +# Sjednocení pomocí operátoru | +mnozina | jina_mnozina # => {1, 2, 3, 4, 5, 6} + +# Rozdíl pomocí operátoru - +{1, 2, 3, 4} - {2, 3, 5} # => {1, 4} + +# Operátorem in se lze dotázat na přítomnost prvku v množině +2 in mnozina # => True +9 in mnozina # => False + + +#################################################### +## 3. Řízení toku programu, cykly +#################################################### + +# Vytvořme si proměnnou +promenna = 5 + +# Takto vypadá podmínka. Na odsazení v Pythonu záleží! +# Vypíše "proměnná je menší než 10". +if promenna > 10: + print("proměnná je velká jak Rusko") +elif promenna < 10: # Část elif je nepovinná + print("proměnná je menší než 10") +else: # Část else je také nepovinná + print("proměnná je právě 10") + + +""" +Smyčka for umí iterovat (nejen) přes seznamy +vypíše: + pes je savec + kočka je savec + myš je savec +""" +for zvire in ["pes", "kočka", "myš"]: + # Můžete použít formát pro složení řetězce + print("{} je savec".format(zvire)) + +""" +range(cislo) vrací iterovatelný objekt čísel od 0 do cislo +vypíše: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print(i) + +""" +range(spodni_limit, horni_limit) vrací iterovatelný objekt čísel mezi limity +vypíše: + 4 + 5 + 6 + 7 +""" +for i in range(4, 8): + print(i) + +""" +Smyčka while se opakuje, dokud je podmínka splněna. +vypíše: + 0 + 1 + 2 + 3 +""" +x = 0 +while x < 4: + print(x) + x += 1 # Zkrácený zápis x = x + 1. Pozor, žádné x++ neexisuje. + + +# Výjimky lze ošetřit pomocí bloku try/except(/else/finally) +try: + # Pro vyhození výjimky použijte raise + raise IndexError("Přistoupil jste k neexistujícímu prvku v seznamu.") +except IndexError as e: + print("Nastala chyba: {}".format(e)) + # Vypíše: Nastala chyba: Přistoupil jste k neexistujícímu prvku v seznamu. +except (TypeError, NameError): # Více výjimek lze zachytit najednou + pass # Pass znamená nedělej nic - nepříliš vhodný způsob ošetření chyb +else: # Volitelný blok else musí být až za bloky except + print("OK!") # Vypíše OK! v případě, že nenastala žádná výjimka +finally: # Blok finally se spustí nakonec za všech okolností + print("Uvolníme zdroje, uzavřeme soubory...") + +# Místo try/finally lze použít with pro automatické uvolnění zdrojů +with open("soubor.txt") as soubor: + for radka in soubor: + print(radka) + +# Python běžně používá iterovatelné objekty, což je prakticky cokoliv, +# co lze považovat za sekvenci. Například to, co vrací metoda range(), +# nebo otevřený soubor, jsou iterovatelné objekty. + +slovnik = {"jedna": 1, "dva": 2, "tři": 3} +iterovatelny_objekt = slovnik.keys() +print(iterovatelny_objekt) # => dict_keys(["jedna", "dva", "tři"]). Toto je iterovatelný objekt. + +# Můžeme použít cyklus for na jeho projití +for klic in iterovatelny_objekt: + print(klic) # vypíše postupně: jedna, dva, tři + +# Ale nelze přistupovat k prvkům pod jejich indexem +iterovatelny_objekt[1] # Vyhodí TypeError + +# Všechny položky iterovatelného objektu lze získat jako seznam pomocí list() +list(slovnik.keys()) # => ["jedna", "dva", "tři"] + +# Z iterovatelného objektu lze vytvořit iterátor +iterator = iter(iterovatelny_objekt) + +# Iterátor je objekt, který si pamatuje stav v rámci svého iterovatelného objektu +# Další hodnotu dostaneme voláním next() +next(iterator) # => "jedna" + +# Iterátor si udržuje svůj stav v mezi jednotlivými voláními next() +next(iterator) # => "dva" +next(iterator) # => "tři" + +# Jakmile interátor vrátí všechna svá data, vyhodí výjimku StopIteration +next(iterator) # Vyhodí StopIteration + + +#################################################### +## 4. Funkce +#################################################### + +# Pro vytvoření nové funkce použijte def +def secist(x, y): + print("x je {} a y je {}".format(x, y)) + return x + y # Hodnoty se vrací pomocí return + +# Volání funkce s parametry +secist(5, 6) # => Vypíše "x je 5 a y je 6" a vrátí 11 + +# Jiný způsob, jak volat funkci, je použít pojmenované argumenty +secist(y=6, x=5) # Pojmenované argumenty můžete předat v libovolném pořadí + +# Lze definovat funkce s proměnným počtem (pozičních) argumentů +def vrat_argumenty(*argumenty): + return argumenty + +vrat_argumenty(1, 2, 3) # => (1, 2, 3) + +# Lze definovat také funkce s proměnným počtem pojmenovaných argumentů +def vrat_pojmenovane_argumenty(**pojmenovane_argumenty): + return pojmenovane_argumenty + +vrat_pojmenovane_argumenty(kdo="se bojí", nesmi="do lesa") +# => {"kdo": "se bojí", "nesmi": "do lesa"} + + +# Pokud chcete, lze použít obojí najednou +# Konvence je používat pro tyto účely názvy *args a **kwargs +def vypis_vse(*args, **kwargs): + print(args, kwargs) # print() vypíše všechny své parametry oddělené mezerou + +vypis_vse(1, 2, a=3, b=4) # Vypíše: (1, 2) {"a": 3, "b": 4} + +# * nebo ** lze použít k rozbalení N-tic nebo slovníků! +ntice = (1, 2, 3, 4) +slovnik = {"a": 3, "b": 4} +vypis_vse(ntice) # Vyhodnotí se jako vypis_vse((1, 2, 3, 4)) – jeden parametr, N-tice +vypis_vse(*ntice) # Vyhodnotí se jako vypis_vse(1, 2, 3, 4) +vypis_vse(**slovnik) # Vyhodnotí se jako vypis_vse(a=3, b=4) +vypis_vse(*ntice, **slovnik) # Vyhodnotí se jako vypis_vse(1, 2, 3, 4, a=3, b=4) + + +# Viditelnost proměnných - vytvořme si globální proměnnou x +x = 5 + +def nastavX(cislo): + # Lokální proměnná x překryje globální x + x = cislo # => 43 + print(x) # => 43 + +def nastavGlobalniX(cislo): + global x + print(x) # => 5 + x = cislo # Nastaví globální proměnnou x na 6 + print(x) # => 6 + +nastavX(43) +nastavGlobalniX(6) + + +# Funkce jsou first-class objekty +def vyrobit_scitacku(pricitane_cislo): + def scitacka(x): + return x + pricitane_cislo + return scitacka + +pricist_10 = vyrobit_scitacku(10) +pricist_10(3) # => 13 + +# Klíčové slovo lambda vytvoří anonymní funkci +(lambda parametr: parametr > 2)(3) # => True + +# Lze použít funkce map() a filter() z funkcionálního programování +map(pricist_10, [1, 2, 3]) +# => <map object at 0x0123467> - iterovatelný objekt s obsahem: [11, 12, 13] +filter(lambda x: x > 5, [3, 4, 5, 6, 7]) +# => <filter object at 0x0123467> - iterovatelný objekt s obsahem: [6, 7] + +# S generátorovou notací lze dosáhnout podobných výsledků, ale vrací seznam +[pricist_10(i) for i in [1, 2, 3]] # => [11, 12, 13] +[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7] +# Generátorová notace funguje i pro slovníky +{x: x**2 for x in range(1, 5)} # => {1: 1, 2: 4, 3: 9, 4: 16} +# A také pro množiny +{pismeno for pismeno in "abeceda"} # => {"d", "a", "c", "e", "b"} + + +#################################################### +## 5. Třídy +#################################################### + +# Třída Clovek je potomkem (dědí od) třídy object +class Clovek(object): + + # Atribut třídy - je sdílený všemi instancemi + druh = "H. sapiens" + + # Toto je kostruktor. Je volán, když vytváříme instanci třídy. Dvě + # podtržítka na začátku a na konci značí, že se jedná o atribut nebo + # objekt využívaný Pythonem ke speciálním účelům, ale můžete sami + # definovat jeho chování. Metody jako __init__, __str__, __repr__ + # a další se nazývají "magické metody". Nikdy nepoužívejte toto + # speciální pojmenování pro běžné metody. + def __init__(self, jmeno): + # Přiřazení parametru do atributu instance jmeno + self.jmeno = jmeno + + # Metoda instance - všechny metody instance mají "self" jako první parametr + def rekni(self, hlaska): + return "{jmeno}: {hlaska}".format(jmeno=self.jmeno, hlaska=hlaska) + + # Metoda třídy - sdílená všemi instancemi + # Dostává jako první parametr třídu, na které je volána + @classmethod + def vrat_druh(cls): + return cls.druh + + # Statická metoda je volána bez reference na třídu nebo instanci + @staticmethod + def odkaslej_si(): + return "*ehm*" + + +# Vytvoření instance +d = Clovek(jmeno="David") +a = Clovek("Adéla") +print(d.rekni("ahoj")) # Vypíše: "David: ahoj" +print(a.rekni("nazdar")) # Vypíše: "Adéla: nazdar" + +# Volání třídní metody +d.vrat_druh() # => "H. sapiens" + +# Změna atributu třídy +Clovek.druh = "H. neanderthalensis" +d.vrat_druh() # => "H. neanderthalensis" +a.vrat_druh() # => "H. neanderthalensis" + +# Volání statické metody +Clovek.odkaslej_si() # => "*ehm*" + + +#################################################### +## 6. Moduly +#################################################### + +# Lze importovat moduly +import math +print(math.sqrt(16)) # => 4 + +# Lze také importovat pouze vybrané funkce z modulu +from math import ceil, floor +print(ceil(3.7)) # => 4.0 +print(floor(3.7)) # => 3.0 + +# Můžete také importovat všechny funkce z modulu, ale radši to nedělejte +from math import * + +# Můžete si přejmenovat modul při jeho importu +import math as m +math.sqrt(16) == m.sqrt(16) # => True + +# Modul v Pythonu není nic jiného, než obyčejný soubor .py +# Můžete si napsat vlastní a prostě ho importovat podle jména +from muj_modul import moje_funkce # Nyní vyhodí ImportError - muj_modul neexistuje + +# Funkcí dir() lze zjistit, co modul obsahuje +import math +dir(math) + + +#################################################### +## 7. Pokročilé +#################################################### + +# Generátory jsou funkce, které místo return obsahují yield +def nasobicka_2(sekvence): + for i in sekvence: + yield 2 * i + +# Generátor generuje hodnoty postupně, jak jsou potřeba. Místo toho, aby vrátil +# celou sekvenci s prvky vynásobenými dvěma, provádí jeden výpočet v každé iteraci. +# To znamená, že čísla větší než 15 se v metodě nasobicka_2 vůbec nezpracují. + +# Funkce range() je také generátor - vytváření seznamu 900000000 prvků by zabralo +# hodně času i paměti, proto se místo toho čísla generují postupně. + +for i in nasobicka_2(range(900000000)): + print(i) # Vypíše čísla 0, 2, 4, 6, 8, ... 30 + if i >= 30: + break + + +# Dekorátory jsou funkce, které se používají pro obalení jiné funkce, čímž mohou +# přidávat nebo měnit její stávající chování. Funkci dostávají jako parametr +# a typicky místo ní vrací jinou, která uvnitř volá tu původní. + +def nekolikrat(puvodni_funkce): + def opakovaci_funkce(*args, **kwargs): + for i in range(3): + puvodni_funkce(*args, **kwargs) + + return opakovaci_funkce + + +@nekolikrat +def pozdrav(jmeno): + print("Měj se {}!".format(jmeno)) + +pozdrav("Pepo") # Vypíše 3x: Měj se Pepo! +``` + +## Co dál? + +Spoustu odkazů na české i anglické materiály najdete na [webu české Python komunity] +(http://python.cz/). Můžete také přijít na Pyvo, kde to společně probereme. diff --git a/csharp.html.markdown b/csharp.html.markdown index 479b7f01..222ba0d2 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -236,7 +236,8 @@ on a new line! ""Wow!"", the masses cried"; // Ternary operators // A simple if/else can be written as follows // <condition> ? <true> : <false> - string isTrue = (true) ? "True" : "False"; + int toCompare = 17; + string isTrue = toCompare == 17 ? "True" : "False"; // While loop int fooWhile = 0; diff --git a/css.html.markdown b/css.html.markdown index 9e8664b3..7224d80a 100644 --- a/css.html.markdown +++ b/css.html.markdown @@ -7,19 +7,19 @@ contributors: filename: learncss.css --- -In early days of web there was no visual elements, just pure text. But with the -further development of browser fully visual web pages also became common. +In the early days of the web there were no visual elements, just pure text. But with the +further development of browsers, fully visual web pages also became common. CSS is the standard language that exists to keep the separation between the content (HTML) and the look-and-feel of web pages. In short, what CSS does is to provide a syntax that enables you to target different elements on an HTML page and assign different visual properties to them. -Like any other language, CSS has many versions. Here we focus on CSS2.0 -which is not the most recent but the most widely supported and compatible version. +Like any other languages, CSS has many versions. Here we focus on CSS2.0, +which is not the most recent version, but is the most widely supported and compatible version. -**NOTE:** Because the outcome of CSS is some visual effects, in order to -learn it, you need try all different things in a +**NOTE:** Because the outcome of CSS consists of visual effects, in order to +learn it, you need try everything in a CSS playground like [dabblet](http://dabblet.com/). The main focus of this article is on the syntax and some general tips. diff --git a/d.html.markdown b/d.html.markdown new file mode 100644 index 00000000..daba8020 --- /dev/null +++ b/d.html.markdown @@ -0,0 +1,247 @@ +--- +language: D +filename: learnd.d +contributors: + - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"] +lang: en +--- + +```c +// You know what's coming... +module hello; + +import std.stdio; + +// args is optional +void main(string[] args) { + writeln("Hello, World!"); +} +``` + +If you're like me and spend way too much time on the internet, odds are you've heard +about [D](http://dlang.org/). The D programming language is a modern, general-purpose, +multi-paradigm language with support for everything from low-level features to +expressive high-level abstractions. + +D is actively developed by Walter Bright and Andrei Alexandrescu, two super smart, really cool +dudes. With all that out of the way, let's look at some examples! + +```c +import std.stdio; + +void main() { + + // Conditionals and loops work as expected. + for(int i = 0; i < 10000; i++) { + writeln(i); + } + + auto n = 1; // use auto for type inferred variables + + // Numeric literals can use _ as a digit seperator for clarity + while(n < 10_000) { + n += n; + } + + do { + n -= (n / 2); + } while(n > 0); + + // For and while are nice, but in D-land we prefer foreach + // The .. creates a continuous range, excluding the end + foreach(i; 1..1_000_000) { + if(n % 2 == 0) + writeln(i); + } + + foreach_reverse(i; 1..int.max) { + if(n % 2 == 1) { + writeln(i); + } else { + writeln("No!"); + } + } +} +``` + +We can define new types with `struct`, `class`, `union`, and `enum`. Structs and unions +are passed to functions by value (i.e. copied) and classes are passed by reference. Futhermore, +we can use templates to parameterize all of these on both types and values! + +```c +// Here, T is a type parameter. Think <T> from C++/C#/Java +struct LinkedList(T) { + T data = null; + LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think <T> +} + +class BinTree(T) { + T data = null; + + // If there is only one template parameter, we can omit parens + BinTree!T left; + BinTree!T right; +} + +enum Day { + Sunday, + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, +} + +// Use alias to create abbreviations for types + +alias IntList = LinkedList!int; +alias NumTree = BinTree!double; + +// We can create function templates as well! + +T max(T)(T a, T b) { + if(a < b) + return b; + + return a; +} + +// Use the ref keyword to ensure pass by referece. +// That is, even if a and b are value types, they +// will always be passed by reference to swap +void swap(T)(ref T a, ref T b) { + auto temp = a; + + a = b; + b = temp; +} + +// With templates, we can also parameterize on values, not just types +class Matrix(uint m, uint n, T = int) { + T[m] rows; + T[n] columns; +} + +auto mat = new Matrix!(3, 3); // We've defaulted type T to int + +``` + +Speaking of classes, let's talk about properties for a second. A property +is roughly a function that may act like an lvalue, so we can +have the syntax of POD structures (`structure.x = 7`) with the semantics of +getter and setter methods (`object.setX(7)`)! + +```c +// Consider a class parameterized on a types T, U + +class MyClass(T, U) { + T _data; + U _other; + +} + +// And "getter" and "setter" methods like so +class MyClass(T, U) { + T _data; + U _other; + + // Constructors are always named `this` + this(T t, U u) { + data = t; + other = u; + } + + // getters + @property T data() { + return _data; + } + + @property U other() { + return _other; + } + + // setters + @property void data(T t) { + _data = t; + } + + @property void other(U u) { + _other = u; + } +} +// And we use them in this manner + +void main() { + auto mc = MyClass!(int, string); + + mc.data = 7; + mc.other = "seven"; + + writeln(mc.data); + writeln(mc.other); +} +``` + +With properties, we can add any amount of logic to +our getter and setter methods, and keep the clean syntax of +accessing members directly! + +Other object-oriented goodies at our disposal +include `interface`s, `abstract class`es, +and `override`ing methods. D does inheritance just like Java: +Extend one class, implement as many interfaces as you please. + +We've seen D's OOP facilities, but let's switch gears. D offers +functional programming with first-class functions, `pure` +functions, and immutable data. In addition, all of your favorite +functional algorithms (map, filter, reduce and friends) can be +found in the wonderful `std.algorithm` module! + +```c +import std.algorithm : map, filter, reduce; +import std.range : iota; // builds an end-exclusive range + +void main() { + // We want to print the sum of a list of squares of even ints + // from 1 to 100. Easy! + + // Just pass lambda expressions as template parameters! + // You can pass any old function you like, but lambdas are convenient here. + auto num = iota(1, 101).filter!(x => x % 2 == 0) + .map!(y => y ^^ 2) + .reduce!((a, b) => a + b); + + writeln(num); +} +``` + +Notice how we got to build a nice Haskellian pipeline to compute num? +That's thanks to a D innovation know as Uniform Function Call Syntax. +With UFCS, we can choose whether to write a function call as a method +or free function call! Walter wrote a nice article on this +[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394) +In short, you can call functions whose first parameter +is of some type A on any expression of type A as a method. + +I like parallelism. Anyone else like parallelism? Sure you do. Let's do some! + +```c +import std.stdio; +import std.parallelism : parallel; +import std.math : sqrt; + +void main() { + // We want take the square root every number in our array, + // and take advantage of as many cores as we have available. + auto arr = new double[1_000_000]; + + // Use an index, and an array element by referece, + // and just call parallel on the array! + foreach(i, ref elem; parallel(arr)) { + ref = sqrt(i + 1.0); + } +} + + +``` diff --git a/de-de/git-de.html.markdown b/de-de/git-de.html.markdown index c7b6ad86..43939129 100644 --- a/de-de/git-de.html.markdown +++ b/de-de/git-de.html.markdown @@ -48,7 +48,7 @@ Ein Repository besteht in Git aus dem .git-Verzeichnis und dem Arbeitsverzeichni ### .git-Verzeichnis (Teil des Repositorys) -Das .git-Verzeichnis enth? alle Einstellung, Logs, Branches, den HEAD und mehr. +Das .git-Verzeichnis enthält alle Einstellung, Logs, Branches, den HEAD und mehr. [Ausführliche Übersicht](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html) ### Arbeitsverzeichnis (Teil des Repositorys) diff --git a/de-de/go-de.html.markdown b/de-de/go-de.html.markdown index 83d59c8b..765372e0 100644 --- a/de-de/go-de.html.markdown +++ b/de-de/go-de.html.markdown @@ -312,7 +312,7 @@ Dokumentation lesen. Auch zu empfehlen ist die Spezifikation von Go, die nach heutigen Standards sehr kurz und auch gut verständlich formuliert ist. Auf der Leseliste von Go-Neulingen ist außerdem der Quelltext der [Go standard Bibliothek](http://golang.org/src/pkg/). -Gut documentiert, demonstriert sie leicht zu verstehendes und im idiomatischen Stil +Gut dokumentiert, demonstriert sie leicht zu verstehendes und im idiomatischen Stil verfasstes Go. Erreichbar ist der Quelltext auch durch das Klicken der Funktionsnamen in der [offiziellen Dokumentation von Go](http://golang.org/pkg/). diff --git a/de-de/json-de.html.markdown b/de-de/json-de.html.markdown new file mode 100644 index 00000000..3cf4578c --- /dev/null +++ b/de-de/json-de.html.markdown @@ -0,0 +1,65 @@ +--- +language: json +filename: learnjson-de.json +contributors: + - ["Anna Harren", "https://github.com/iirelu"] + - ["Marco Scannadinari", "https://github.com/marcoms"] +translators: + - ["Timm Albers", "https://github.com/nunull"] +lang: de-de +--- + +Da JSON ein äußerst einfaches Format für den Austausch von Daten ist, wird dieses +Dokument das vermutlich einfachste "Learn X in Y Minutes" werden. + +In seiner grundlegenden Form hat JSON keine eigentlichen Kommentare. Dennoch +akzeptieren die meisten Parser Kommentare in C-Syntax (`//`, `/* */`). Dennoch +soll für dieses Dokument nur 100% gültiges JSON verwendet werden, weshalbt keine +Kommentare verwendet werden. Glücklicherweise ist das nachfolgende Dokument +selbsterklärend. + +```json +{ + "schlüssel": "wert", + + "alle schlüssel": "müssen durch doppelte Anführungszeichen begrenzt werden", + "zahlen": 0, + "zeichenketten": "Alle Unicode-Zeichen (inklusive \"escaping\") sind erlaubt.", + "boolesche werte": true, + "nullwert": null, + + "große zahlen": 1.2e+100, + + "objekte": { + "kommentar": "Die meisten Datenstrukturen in JSON kommen aus Objekten.", + + "array": [0, 1, 2, "Arrays können Werte jeglichen Datentyps aufnehmen.", 4], + + "weiteres objekt": { + "kommentar": "Objekte können verschachtelt werden." + } + }, + + "quatsch": [ + { + "quellen von kalium": ["Bananen"] + }, + [ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, "Neo"], + [0, 0, 0, 1] + ] + ], + + "alternative formatierung": { + "kommentar": "..." + , "die position": "des Kommas ist nicht relevant - so lange es vor dem Wert steht." + , "weiterer kommentar": "wie schön" + , "übrigens": "Auch die Einrückung ist nicht relevant." + , "jede": "beliebige Anzahl von Leerzeichen / Tabs ist erlaubt.", "wirklich?":true + }, + + "das war kurz": "Und, du bist fertig. Du weißt nun (fast) alles über JSON." +} +``` diff --git a/de-de/yaml-de.html.markdown b/de-de/yaml-de.html.markdown index 88318014..19ea9e87 100644 --- a/de-de/yaml-de.html.markdown +++ b/de-de/yaml-de.html.markdown @@ -1,10 +1,11 @@ --- language: yaml -filename: learnyaml.yaml contributors: - ["Adam Brenecki", "https://github.com/adambrenecki"] translators: - - ["Ruben M.", https://github.com/switchhax] + - ["Ruben M.", "https://github.com/switchhax"] +filename: learnyaml-de.yaml +lang: de-de --- YAML ist eine Sprache zur Datenserialisierung, die sofort von Menschenhand geschrieben und gelesen werden kann. diff --git a/erlang.html.markdown b/erlang.html.markdown index 8b67a76a..48cee6ec 100644 --- a/erlang.html.markdown +++ b/erlang.html.markdown @@ -25,6 +25,7 @@ filename: learnerlang.erl %% 1. Variables and pattern matching. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% In Erlang new variables are bound with an `=` statement. Num = 42. % All variable names must start with an uppercase letter. % Erlang has single-assignment variables; if you try to assign a different @@ -32,9 +33,11 @@ Num = 42. % All variable names must start with an uppercase letter. Num = 43. % ** exception error: no match of right hand side value 43 % In most languages, `=` denotes an assignment statement. In Erlang, however, -% `=` denotes a pattern-matching operation. `Lhs = Rhs` really means this: -% evaluate the right side (`Rhs`), and then match the result against the -% pattern on the left side (`Lhs`). +% `=` denotes a pattern-matching operation. When an empty variable is used on the +% left hand side of the `=` operator to is bound (assigned), but when a bound +% varaible is used on the left hand side the following behaviour is observed. +% `Lhs = Rhs` really means this: evaluate the right side (`Rhs`), and then +% match the result against the pattern on the left side (`Lhs`). Num = 7 * 6. % Floating-point number. @@ -299,6 +302,39 @@ CalculateArea ! {circle, 2}. % 12.56000000000000049738 % The shell is also a process; you can use `self` to get the current pid. self(). % <0.41.0> +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% 5. Testing with EUnit +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% Unit tests can be written using EUnits's test generators and assert macros +-module(fib). +-export([fib/1]). +-include_lib("eunit/include/eunit.hrl"). + +fib(0) -> 1; +fib(1) -> 1; +fib(N) when N > 1 -> fib(N-1) + fib(N-2). + +fib_test_() -> + [?_assert(fib(0) =:= 1), + ?_assert(fib(1) =:= 1), + ?_assert(fib(2) =:= 2), + ?_assert(fib(3) =:= 3), + ?_assert(fib(4) =:= 5), + ?_assert(fib(5) =:= 8), + ?_assertException(error, function_clause, fib(-1)), + ?_assert(fib(31) =:= 2178309) + ]. + +% EUnit will automatically export to a test() function to allow running the tests +% in the erlang shell +fib:test() + +% The popular erlang build tool Rebar is also compatible with EUnit +% ``` +% rebar eunit +% ``` + ``` ## References diff --git a/es-es/c++-es.html.markdown b/es-es/c++-es.html.markdown new file mode 100644 index 00000000..bcc775e5 --- /dev/null +++ b/es-es/c++-es.html.markdown @@ -0,0 +1,829 @@ +--- +language: c++ +filename: learncpp.cpp +contributors: + - ["Steven Basart", "http://github.com/xksteven"] + - ["Matt Kline", "https://github.com/mrkline"] + - ["Geoff Liu", "http://geoffliu.me"] + - ["Connor Waters", "http://github.com/connorwaters"] +translators: + - ["Gerson Lázaro", "https://gersonlazaro.com"] +lang: es-es +--- + +C++ es un lenguaje de programación de sistemas que, +[de acuerdo a su inventor Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote), +fue diseñado para + +- ser un "mejor C" +- soportar abstracción de datos +- soportar programación orientada a objetos +- soportar programación genérica + +Aunque su sintaxis puede ser más difícil o compleja que los nuevos lenguajes, +es ampliamente utilizado, ya que compila instrucciones nativas que pueden ser +directamente ejecutadas por el procesador y ofrece un estricto control sobre +el hardware (como C), mientras ofrece características de alto nivel como +genericidad, excepciones, y clases. Esta combinación de velocidad y +funcionalidad hace de C ++ uno de los lenguajes de programación más utilizados. + +```c++ +//////////////////// +// Comparación con C +//////////////////// + +// C ++ es _casi_ un superconjunto de C y comparte su sintaxis básica para las +// declaraciones de variables, tipos primitivos y funciones. + +// Al igual que en C, el punto de entrada de tu programa es una función llamada +// main con un retorno de tipo entero. +// Este valor sirve como código de salida del programa. +// Mira http://en.wikipedia.org/wiki/Exit_status para mayor información. +int main(int argc, char** argv) +{ + // Los argumentos de la línea de comandos se pasan por argc y argv de la + // misma manera que en C. + // argc indica el número de argumentos, + // y argv es un arreglo de strings de estilo C (char*) + // representando los argumentos. + // El primer argumento es el nombre con el que el programa es llamado. + // argc y argv pueden omitirse si no te preocupan los argumentos, + // dejando la definición de la función como int main () + + // Un estado de salida 0 indica éxito. + return 0; +} + +// Sin embargo, C ++ varía en algunas de las siguientes maneras: + +// En C++, los caracteres literales son caracteres +sizeof('c') == sizeof(char) == 1 + +// En C, los caracteres literales son enteros +sizeof('c') == sizeof(int) + + +// C++ tiene prototipado estricto +void func(); // función que no acepta argumentos + +// En C +void func(); // función que puede aceptar cualquier número de argumentos + +// Use nullptr en lugar de NULL en C++ +int* ip = nullptr; + +// Las cabeceras (headers) estándar de C están disponibles en C ++, +// pero tienen el prefijo "c" y no tienen sufijo .h. +#include <cstdio> + +int main() +{ + printf("Hola mundo!\n"); + return 0; +} + +////////////////////////// +// Sobrecarga de funciones +////////////////////////// + +// C++ soporta sobrecarga de funciones +// siempre que cada función tenga diferentes parámetros. + +void print(char const* myString) +{ + printf("String %s\n", myString); +} + +void print(int myInt) +{ + printf("Mi entero es %d", myInt); +} + +int main() +{ + print("Hello"); // Resolves to void print(const char*) + print(15); // Resolves to void print(int) +} + +//////////////////////////////////// +// Argumentos de función por defecto +//////////////////////////////////// + +// Puedes proporcionar argumentos por defecto para una función si no son +// proporcionados por quien la llama. + +void doSomethingWithInts(int a = 1, int b = 4) +{ + // Hacer algo con los enteros aqui +} + +int main() +{ + doSomethingWithInts(); // a = 1, b = 4 + doSomethingWithInts(20); // a = 20, b = 4 + doSomethingWithInts(20, 5); // a = 20, b = 5 +} + +// Los argumentos predeterminados deben estar al final de la lista de argumentos. + +void invalidDeclaration(int a = 1, int b) // Error! +{ +} + +///////////////////// +// Espacios de nombre +///////////////////// + +// Espacios de nombres proporcionan ámbitos separados para variable, función y +// otras declaraciones. +// Los espacios de nombres se pueden anidar. + +namespace First { + namespace Nested { + void foo() + { + printf("Esto es First::Nested::foo\n"); + } + } // fin del nombre de espacio Nested +} // fin del nombre de espacio First + +namespace Second { + void foo() + { + printf("Esto es Second::foo\n") + } +} + +void foo() +{ + printf("Este es global: foo\n"); +} + +int main() +{ + + // Incluye todos los símbolos del espacio de nombre Second en el ámbito + // actual. Tenga en cuenta que simplemente foo() no funciona, ya que ahora + // es ambigua si estamos llamando a foo en espacio de nombres Second o en + // el nivel superior. + using namespace Second; + + Second::foo(); // imprime "Esto es Second::foo" + First::Nested::foo(); // imprime "Esto es First::Nested::foo" + ::foo(); // imprime "Este es global: foo" +} + +///////////////// +// Entrada/Salida +///////////////// + +// La entrada y salida de C++ utiliza flujos (streams) +// cin, cout, y cerr representan a stdin, stdout, y stderr. +// << es el operador de inserción >> es el operador de extracción. + + +#include <iostream> // Incluir para el flujo de entrada/salida + +using namespace std; // Los streams estan en std namespace (libreria estandar) + +int main() +{ + int myInt; + + // Imprime a la stdout (o terminal/pantalla) + cout << "Ingresa tu número favorito:\n"; + // Toma una entrada + cin >> myInt; + + // cout puede también ser formateado + cout << "Tu número favorito es " << myInt << "\n"; + // imprime "Tu número favorito es <myInt>" + + cerr << "Usado para mensajes de error"; +} +//////////////////// +// Cadenas (Strings) +//////////////////// + +// Las cadenas en C++ son objetos y tienen muchas funciones +#include <string> + +using namespace std; // Strings también estan en namespace std + +string myString = "Hola"; +string myOtherString = " Mundo"; + +// + es usado para concatenar. +cout << myString + myOtherString; // "Hola Mundo" + +cout << myString + " Tu"; // "Hola Tu" + +// Las cadenas en C++ son mutables y tienen valor semántico. +myString.append(" Perro"); +cout << myString; // "Hola Perro" + + +////////////// +// Referencias +////////////// + +// Además de punteros como los de C, +// C++ tiene _references_. +// Estos tipos de puntero no pueden ser reasignados una vez establecidos +// Y no pueden ser nulos. +// También tienen la misma sintaxis que la propia variable: +// No es necesaria * para eliminar la referencia y +// & (dirección) no se utiliza para la asignación. + +using namespace std; + +string foo = "Yo soy foo"; +string bar = "Yo soy bar"; + +string& fooRef = foo; // Crea una referencia a foo. +fooRef += ". Hola!"; // Modifica foo través de la referencia +cout << fooRef; // Imprime "Yo soy foo. Hola!" + +// No trate de reasignar "fooRef". Esto es lo mismo que "foo = bar", y +// foo == "Yo soy bar" +// después de esta linea. +fooRef = bar; + +const string& barRef = bar; // Crea una referencia constante a bar. +// Como en C, los valores constantes (y punteros y referencias) no pueden ser +// modificados. +barRef += ". Hola!"; // Error, referencia constante no puede ser modificada. + +// Sidetrack: Antes de hablar más sobre referencias, hay que introducir un +// concepto llamado objeto temporal. Supongamos que tenemos el siguiente código: +string tempObjectFun() { ... } +string retVal = tempObjectFun(); + +// Lo que pasa en la segunda línea es en realidad: +// - Un objeto de cadena es retornado desde tempObjectFun +// - Una nueva cadena se construye con el objeto devuelto como argumento al +// constructor +// - El objeto devuelto es destruido +// El objeto devuelto se llama objeto temporal. Objetos temporales son +// creados cada vez que una función devuelve un objeto, y es destruido en el +// fin de la evaluación de la expresión que encierra (Bueno, esto es lo que la +// norma dice, pero los compiladores están autorizados a cambiar este +// comportamiento. Busca "return value optimization" para ver mas detalles). +// Así que en este código: +foo(bar(tempObjectFun())) + +// Suponiendo que foo y bar existen, el objeto retornado de tempObjectFun es +// pasado al bar, y se destruye antes de llamar foo. + +// Ahora, de vuelta a las referencias. La excepción a la regla "en el extremo +// de la expresión encerrada" es si un objeto temporal se une a una +// referencia constante, en cuyo caso su vida se extiende al ámbito actual: + +void constReferenceTempObjectFun() { + // ConstRef obtiene el objeto temporal, y es válido hasta el final de esta + // función. + const string& constRef = tempObjectFun(); + ... +} + +// Otro tipo de referencia introducida en C ++ 11 es específicamente para +// objetos temporales. No se puede tener una variable de este tipo, pero tiene +// prioridad en resolución de sobrecarga: + +void someFun(string& s) { ... } // Referencia regular +void someFun(string&& s) { ... } // Referencia a objeto temporal + +string foo; +someFun(foo); // Llama la función con referencia regular +someFun(tempObjectFun()); // Llama la versión con referencia temporal + +// Por ejemplo, puedes ver estas dos versiones de constructores para +// std::basic_string: +basic_string(const basic_string& other); +basic_string(basic_string&& other); + +// La idea es que si estamos construyendo una nueva cadena de un objeto temporal +// (que va a ser destruido pronto de todos modos), podemos tener un constructor +// mas eficiente que "rescata" partes de esa cadena temporal. Usted verá este +// Concepto denominado "movimiento semántico". + +//////////////////////////////////////////// +// Clases y programación orientada a objetos +//////////////////////////////////////////// + +// Primer ejemplo de clases +#include <iostream> + +// Declara una clase. +// Las clases son usualmente declaradas en archivos de cabeceras (.h o .hpp) +class Dog { + // Variables y funciones de la clase son privados por defecto. + std::string name; + int weight; + +// Todos los miembros siguientes de este son públicos +// Hasta que se encuentre "private" o "protected". +// All members following this are public +// until "private:" or "protected:" is found. +public: + + // Constructor por defecto + Dog(); + + // Declaraciones de funciones de la clase (implementaciones a seguir) + // Nota que usamos std::string aquí en lugar de colocar + // using namespace std; + // arriba. + // Nunca ponga una declaración "using namespace" en un encabezado. + void setName(const std::string& dogsName); + + void setWeight(int dogsWeight); + // Funciones que no modifican el estado del objeto + // Deben marcarse como const. + // Esto le permite llamarlas si se envia una referencia constante al objeto. + // También tenga en cuenta que las funciones deben ser declaradas + // explícitamente como _virtual_ para que sea reemplazada en las clases + // derivadas. + // Las funciones no son virtuales por defecto por razones de rendimiento. + virtual void print() const; + + // Las funciones también se pueden definir en el interior + // del cuerpo de la clase. + // Funciones definidas como tales están entre líneas automáticamente. + void bark() const { std::cout << name << " barks!\n"; } + + // Junto a los constructores, C++ proporciona destructores. + // Estos son llamados cuando un objeto se elimina o está fuera del ámbito. + // Esto permite paradigmas potentes como RAII + // (mira abajo) + // El destructor debe ser virtual si una clase es dervada desde el; + // Si no es virtual, entonces la clase derivada destructor + // No será llamada si el objeto se destruye a través de una referencia de + // la clase base o puntero. + virtual ~Dog(); + + + +}; // Un punto y coma debe seguir la definición de clase. + +// Las funciones de una clase son normalmente implementados en archivos .cpp. +Dog::Dog() +{ + std::cout << "Un perro ha sido construido\n"; +} + +// Objetos (tales como cadenas) deben ser pasados por referencia +// Si los estas modificando o referencia constante en caso contrario. +void Dog::setName(const std::string& dogsName) +{ + name = dogsName; +} + +void Dog::setWeight(int dogsWeight) +{ + weight = dogsWeight; +} + +// Nota que "virtual" sólo se necesita en la declaración, no en la definición. +void Dog::print() const +{ + std::cout << "El perro es " << name << " y pesa " << weight << "kg\n"; +} + +Dog::~Dog() +{ + cout << "Adiós " << name << "\n"; +} + +int main() { + Dog myDog; // imprime "Un perro ha sido construido" + myDog.setName("Barkley"); + myDog.setWeight(10); + myDog.print(); // imprime "El perro es Barkley y pesa 10 kg" + return 0; +} // imprime "Adiós Barkley" + +// Herencia: + +// Esta clase hereda todo lo público y protegido de la clase Dog +class OwnedDog : public Dog { + + void setOwner(const std::string& dogsOwner); + + // Reemplaza el comportamiento de la función de impresión + // de todos los OwnedDogs. Mira + // http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping + // Para una introducción más general si no está familiarizado con el + // polimorfismo de subtipo. + // La palabra clave override es opcional, pero asegura que estás + // reemplazando el método de una clase base. + void print() const override; + +private: + std::string owner; +}; + +// Mientras tanto, en el archivo .cpp correspondiente: + +void OwnedDog::setOwner(const std::string& dogsOwner) +{ + owner = dogsOwner; +} + +void OwnedDog::print() const +{ + Dog::print(); // Llama a la función de impresión en la clase base Dog + std::cout << "El perro es de " << owner << "\n"; + // Imprime "El perro es <name> y pesa <weight>" + // "El perro es de <owner>" +} + +//////////////////////////////////////////// +// Inicialización y sobrecarga de operadores +//////////////////////////////////////////// + +// En C ++ se puede sobrecargar el comportamiento +// de los operadores como +, -, *, /, etc. +// Esto se hace mediante la definición de una función que es llamada +// cada vez que se utiliza el operador. + +#include <iostream> +using namespace std; + +class Point { +public: + // Las variables de la clase pueden dar valores por defecto de esta manera. + double x = 0; + double y = 0; + + // Define un constructor por defecto que no hace nada + // pero inicializa el punto al valor por defecto (0, 0) + Point() { }; + + // The following syntax is known as an initialization list + // and is the proper way to initialize class member values + Point (double a, double b) : + x(a), + y(b) + { /* No hace nada excepto inicializar los valores */ } + + // Sobrecarga el operador + + Point operator+(const Point& rhs) const; + + // Sobrecarga el operador += + Point& operator+=(const Point& rhs); + + // También tendría sentido añadir los operadores - y -=, + // Pero vamos a omitirlos por razones de brevedad. +}; + +Point Point::operator+(const Point& rhs) const +{ + // Crea un nuevo punto que es la suma de este y rhs. + return Point(x + rhs.x, y + rhs.y); +} + +Point& Point::operator+=(const Point& rhs) +{ + x += rhs.x; + y += rhs.y; + return *this; +} + +int main () { + Point up (0,1); + Point right (1,0); + // Llama al operador + de Point + // Point llama la función + con right como parámetro + Point result = up + right; + // Prints "Result is upright (1,1)" + cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; + return 0; +} + +///////////////////////// +// Plantillas (Templates) +///////////////////////// + +// Las plantillas en C++ se utilizan sobre todo en la programación genérica, +// a pesar de que son mucho más poderoso que los constructores genéricos +// en otros lenguajes. Ellos también soportan especialización explícita y +// parcial y clases de tipo estilo funcional; de hecho, son un lenguaje +// funcional Turing-completo incrustado en C ++! + +// Empezamos con el tipo de programación genérica que podría estar +// familiarizado. +// Para definir una clase o función que toma un parámetro de tipo: +template<class T> +class Box { +public: + // En este caso, T puede ser usado como cualquier otro tipo. + void insert(const T&) { ... } +}; + +// Durante la compilación, el compilador realmente genera copias de cada +// plantilla con parámetros sustituidos, por lo que la definición completa +// de la clase debe estar presente en cada invocación. +// Es por esto que usted verá clases de plantilla definidas +// Enteramente en archivos de cabecera. + +//Para crear una instancia de una clase de plantilla en la pila: +Box<int> intBox; + +y puedes utilizarlo como era de esperar: +intBox.insert(123); + +// Puedes, por supuesto, anidar plantillas: +Box<Box<int> > boxOfBox; +boxOfBox.insert(intBox); + +// Hasta C++11, había que colocar un espacio entre los dos '>'s, +// de lo contrario '>>' serían analizados como el operador de desplazamiento +// a la derecha. + + +// A veces verás +// template<typename T> +// en su lugar. La palabra clave "class" y las palabras clave "typename" son +// mayormente intercambiables en este caso. Para la explicación completa, mira +// http://en.wikipedia.org/wiki/Typename +// (sí, esa palabra clave tiene su propia página de Wikipedia). + +// Del mismo modo, una plantilla de función: +template<class T> +void barkThreeTimes(const T& input) +{ + input.bark(); + input.bark(); + input.bark(); +} + +// Observe que no se especifica nada acerca de los tipos de parámetros aquí. +// El compilador generará y comprobará cada invocación de la plantilla, +// por lo que la función anterior funciona con cualquier tipo "T" +// que tenga un método 'bark' constante! + + +Dog fluffy; +fluffy.setName("Fluffy") +barkThreeTimes(fluffy); // Imprime "Fluffy barks" 3 veces. + +Los parámetros de la plantilla no tienen que ser las clases: +template<int Y> +void printMessage() { + cout << "Aprende C++ en " << Y << " minutos!" << endl; +} + +// Y usted puede especializar explícitamente plantillas +// para código más eficiente. +// Por supuesto, la mayor parte del mundo real que utiliza una especialización +// no son tan triviales como esta. +// Tenga en cuenta que usted todavía tiene que declarar la función (o clase) +// como plantilla incluso si ha especificado de forma explícita todos +// los parámetros. + +template<> +void printMessage<10>() { + cout << "Aprende C++ rapido en solo 10 minutos!" << endl; +} + +printMessage<20>(); // Prints "Aprende C++ en 20 minutos!" +printMessage<10>(); // Prints "Aprende C++ rapido en solo 10 minutos!" + + +///////////////////// +// Manejador de excepciones +///////////////////// + +// La biblioteca estándar proporciona algunos tipos de excepción +// (mira http://en.cppreference.com/w/cpp/error/exception) +// pero cualquier tipo puede ser lanzado como una excepción +#include <exception> +#include <stdexcept> + +//Todas las excepciones lanzadas dentro del bloque _try_ pueden ser +// capturados por los siguientes manejadores _catch_. +try { + // No asignar excepciones en el heap usando _new_. + throw std::runtime_error("Ocurrió un problema"); +} + +// Captura excepciones por referencia const si son objetos +catch (const std::exception& ex) +{ + std::cout << ex.what(); +} +******************************************************************************** +// Captura cualquier excepción no capturada por bloques _catch_ anteriores +catch (...) +{ + std::cout << "Excepción desconocida capturada"; + throw; // Re-lanza la excepción +} + +/////// +// RAII +/////// + +// RAII significa "Resource Acquisition Is Initialization" +// (Adquisición de recursos es inicialización). +// A menudo se considera el paradigma más poderoso en C++ +// Y el concepto es simple: un constructor de un objeto +// Adquiere recursos de ese objeto y el destructor les libera. + +// Para entender cómo esto es útil, +// Considere una función que utiliza un identificador de archivo C: +void doSomethingWithAFile(const char* filename) +{ + // Para empezar, asuma que nada puede fallar. + + FILE* fh = fopen(filename, "r"); // Abre el archivo en modo lectura + + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + + fclose(fh); // Cierra el manejador de archivos +} + +// Por desgracia, las cosas se complican rápidamente por el control de errores. +// Supongamos que fopen puede fallar, y que doSomethingWithTheFile y +// DoSomethingElseWithIt retornan códigos de error si fallan. +// (Excepciones son la mejor forma de manejar los fallos, +// pero algunos programadores, especialmente los que tienen un fondo C, +// estan en desacuerdo sobre la utilidad de las excepciones). +// Ahora tenemos que comprobar cada llamado por fallos y cerrar el manejador +// del archivo si se ha producido un problema. +bool doSomethingWithAFile(const char* filename) +{ + FILE* fh = fopen(filename, "r"); // Abre el archivo en modo lectura + if (fh == nullptr) // El puntero retornado es nulo o falla. + return false; // Reporta el fallo a quien hizo el llamado. + + // Asume que cada función retorna falso si falla + if (!doSomethingWithTheFile(fh)) { + fclose(fh); // Cierre el manejador de archivo para que no se filtre. + return false; // Propaga el error. + } + if (!doSomethingElseWithIt(fh)) { + fclose(fh); // Cierre el manejador de archivo para que no se filtre. + return false; // Propaga el error. + } + + fclose(fh); // Cierre el archivo. + return true; // Indica que todo funcionó correctamente. +} + +// Programadores C suelen limpiar esto un poco usando goto: +bool doSomethingWithAFile(const char* filename) +{ + FILE* fh = fopen(filename, "r"); + if (fh == nullptr) + return false; + + if (!doSomethingWithTheFile(fh)) + goto failure; + + if (!doSomethingElseWithIt(fh)) + goto failure; + + fclose(fh); // Cierre el archivo. + return true; // Indica que todo funcionó correctamente. + +failure: + fclose(fh); + return false; // Propagate el error +} + +// Si las funciones indican errores mediante excepciones, +// Las cosas son un poco más claras, pero pueden optimizarse mas. +void doSomethingWithAFile(const char* filename) +{ + FILE* fh = fopen(filename, "r"); // Abrir el archivo en modo lectura + if (fh == nullptr) + throw std::runtime_error("No puede abrirse el archivo."); + + try { + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + } + catch (...) { + fclose(fh); // Asegúrese de cerrar el archivo si se produce un error. + throw; // Luego vuelve a lanzar la excepción. + } + + fclose(fh); // Cierra el archivo +} + +// Compare esto con el uso de la clase de flujo de archivos de C++ (fstream) +// fstream utiliza su destructor para cerrar el archivo. +// Los destructores son llamados automáticamente +// cuando un objeto queda fuera del ámbito. +void doSomethingWithAFile(const std::string& filename) +{ + // ifstream es la abreviatura de el input file stream + std::ifstream fh(filename); // Abre el archivo + + // Hacer algo con el archivo + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + +} // El archivo se cierra automáticamente aquí por el destructor + + +// Esto tiene ventajas _enormes_: +// 1. No importa lo que pase, +// El recurso (en este caso el manejador de archivo) será limpiado. +// Una vez que escribes el destructor correctamente, +// Es _imposible_ olvidar cerrar el identificador y permitir +// fugas del recurso. +// 2. Tenga en cuenta que el código es mucho más limpio. +// El destructor se encarga de cerrar el archivo detrás de cámaras +// Sin que tenga que preocuparse por ello. +// 3. El código es seguro. +// Una excepción puede ser lanzado en cualquier lugar de la función +// y la limpieza ocurrirá. + +// Todo el código idiomático C++ utiliza RAII ampliamente para todos los +// recursos. +// Otros ejemplos incluyen +// - Memoria usando unique_ptr y shared_ptr +// - Contenedores (Containers) - la biblioteca estándar linked list, +// vector (es decir, array con auto-cambio de tamaño), hash maps, etc. +// Destruimos todos sus contenidos de forma automática +// cuando quedan fuera del ámbito. +// - Mutex utilizando lock_guard y unique_lock + + +///////////////////// +// Cosas divertidas +///////////////////// + +// Aspectos de C ++ que pueden sorprender a los recién llegados +// (e incluso algunos veteranos). +// Esta sección es, por desgracia, salvajemente incompleta; +// C++ es uno de los lenguajes con los que mas facil te disparas en el pie. + +// Tu puedes sobreescribir métodos privados! +class Foo { + virtual void bar(); +}; +class FooSub : public Foo { + virtual void bar(); // Sobreescribe Foo::bar! +}; + + +// 0 == false == NULL (La mayoria de las veces)! +bool* pt = new bool; +*pt = 0; // Establece los puntos de valor de 'pt' en falso. +pt = 0; // Establece 'pt' al apuntador nulo. Ambas lineas compilan sin error. + +// nullptr se supone que arregla un poco de ese tema: +int* pt2 = new int; +*pt2 = nullptr; // No compila +pt2 = nullptr; // Establece pt2 como null. + +// Hay una excepción para los valores bool. +// Esto es para permitir poner a prueba punteros nulos con if (!ptr), +// pero como consecuencia se puede asignar nullptr a un bool directamente! +*pt = nullptr; // Esto todavía compila, a pesar de que '*pt' es un bool! + +// '=' != '=' != '='! +// Llama Foo::Foo(const Foo&) o alguna variante (mira movimientos semanticos) +// copia del constructor. +Foo f2; +Foo f1 = f2; + +// Llama Foo::Foo(const Foo&) o variante, pero solo copia el 'Foo' parte de +// 'fooSub'. Cualquier miembro extra de 'fooSub' se descarta. Este +// comportamiento horrible se llama "Corte de objetos." +FooSub fooSub; +Foo f1 = fooSub; + +// Llama a Foo::operator=(Foo&) o variantes. +Foo f1; +f1 = f2; + + +// Cómo borrar realmente un contenedor: +class Foo { ... }; +vector<Foo> v; +for (int i = 0; i < 10; ++i) + v.push_back(Foo()); +// La siguiente línea establece el tamaño de v en 0, +// pero los destructores no son llamados y los recursos no se liberan! + +v.empty(); +v.push_back(Foo()); // Nuevo valor se copia en el primer Foo que insertamos + +// En verdad destruye todos los valores en v. +// Consulta la sección acerca de los objetos temporales para la +// explicación de por qué esto funciona. +v.swap(vector<Foo>()); + +``` +Otras lecturas: + +Una referencia del lenguaje hasta a la fecha se puede encontrar en +<http://cppreference.com/w/cpp> + +Recursos adicionales se pueden encontrar en <http://cplusplus.com> diff --git a/es-es/git-es.html.markdown b/es-es/git-es.html.markdown index 73853a9d..51812447 100644 --- a/es-es/git-es.html.markdown +++ b/es-es/git-es.html.markdown @@ -11,15 +11,15 @@ lang: es-es --- Git es un sistema de control de versiones distribuido diseñado para manejar -cualquier tipo de proyecto ya sea largos o pequeños, con velocidad y eficiencia. +cualquier tipo de proyecto, ya sea largo o pequeño, con velocidad y eficiencia. Git realiza esto haciendo "snapshots" del proyecto, con ello permite versionar y administrar nuestro código fuente. ## Versionamiento, conceptos. -### Que es el control de versiones? -El control de versiones es un sistema que guarda todos los cambios realizados a +### Qué es el control de versiones? +El control de versiones es un sistema que guarda todos los cambios realizados en uno o varios archivos, a lo largo del tiempo. ### Versionamiento centralizado vs Versionamiento Distribuido. @@ -31,15 +31,15 @@ uno o varios archivos, a lo largo del tiempo. + El versionamiento distribuido no tiene una estructura definida, incluso se puede mantener el estilo de los repositorios SVN con git. -[Informacion adicional](http://git-scm.com/book/es/Empezando-Acerca-del-control-de-versiones) +[Información adicional](http://git-scm.com/book/es/Empezando-Acerca-del-control-de-versiones) -### Por que usar Git? +### Por qué usar Git? * Se puede trabajar sin conexion. * Colaborar con otros es sencillo!. -* Derivar, Crear ramas del proyecto (aka: Branching) es facil!. +* Derivar, Crear ramas del proyecto (aka: Branching) es fácil!. * Combinar (aka: Merging) -* Git es rapido. +* Git es rápido. * Git es flexible. ## Arquitectura de Git. @@ -48,10 +48,10 @@ uno o varios archivos, a lo largo del tiempo. Un repositorio es un conjunto de archivos, directorios, registros, cambios (aka: comits), y encabezados (aka: heads). Imagina que un repositorio es una clase, -y que sus atributos otorgan acceso al historial del elemento, ademas de otras +y que sus atributos otorgan acceso al historial del elemento, además de otras cosas. -Un repositorio esta compuesto por la carpeta .git y un "arbol de trabajo". +Un repositorio esta compuesto por la carpeta .git y un "árbol de trabajo". ### Directorio .git (componentes del repositorio) @@ -62,38 +62,38 @@ y mas. ### Directorio de trabajo (componentes del repositorio) -Es basicamente los directorios y archivos dentro del repositorio. La mayorioa de +Es basicamente los directorios y archivos dentro del repositorio. La mayoría de las veces se le llama "directorio de trabajo". -### Inidice (componentes del directorio .git) +### Índice (componentes del directorio .git) -El inidice es la area de inicio en git. Es basicamente la capa que separa el +El índice es el área de inicio en git. Es basicamente la capa que separa el directorio de trabajo, del repositorio en git. Esto otorga a los desarrolladores -mas poder sobre lo que envia y recibe en el repositorio. +mas poder sobre lo que envía y recibe en el repositorio. ### Commit (aka: cambios) Un commit es una captura de un conjunto de cambios, o modificaciones hechas en el directorio de trabajo. Por ejemplo, si se añaden 5 archivos, se remueven 2, -estos cambios se almacenaran en un commit (aka: captura). Este commit puede ser o +estos cambios se almacenarán en un commit (aka: captura). Este commit puede ser o no ser enviado (aka: "pusheado") hacia un repositorio. ### Branch (rama) -Un "branch", es escencialmente un apuntador hacia el ultimo commit (cambio -registrado) que se ha realizado. A medida que se realizan mas commits, este -apuntador se actualizara automaticamente hacia el ultimo commit. +Un "branch", es escencialmente un apuntador hacia el último commit (cambio +registrado) que se ha realizado. A medida que se realizan más commits, este +apuntador se actualizará automaticamente hacia el ultimo commit. ### "HEAD" y "head" (component of .git dir) "HEAD" es un apuntador hacia la rama (branch) que se esta utilizando. Un repositorio solo puede tener un HEAD activo. En cambio "head", es un apuntador a -cualquier commit realizado, un repositorio puede tener cualquier numero de +cualquier commit realizado, un repositorio puede tener cualquier número de "heads". ### conceptos - recursos. -* [Git para informaticos](http://eagain.net/articles/git-for-computer-scientists/) +* [Git para informáticos](http://eagain.net/articles/git-for-computer-scientists/) * [Git para diseñadores](http://hoth.entp.com/output/git_for_designers.html) @@ -102,8 +102,8 @@ cualquier commit realizado, un repositorio puede tener cualquier numero de ### init -Crear un repositorio de git vacio. Las configuraciones, informacion almacenada y -demas son almacenadas en el directorio ".git". +Crear un repositorio de git vacio. Las configuraciones, información almacenada y +demás son almacenadas en el directorio ".git". ```bash $ git init @@ -127,7 +127,7 @@ $ git config --global user.name "nombre" ### help -Otorga un accceso rapido a una guia extremadamente detallada de cada comando en +Otorga un accceso rápido a una guía extremadamente detallada de cada comando en git. O puede ser usada simplemente como un recordatorio de estos. ```bash @@ -146,7 +146,7 @@ $ git help init ### status -Muestra las diferencias entre el archivo indice y el commit al cual apunta el +Muestra las diferencias entre el archivo índice y el commit al cual apunta el HEAD actualmente. @@ -163,7 +163,7 @@ $ git help status Para añadir archivos al arbol (directorio, repositorio) de trabajo. Si no se utiliza `git add`, los nuevos archivos no se añadiran al arbol de trabajo, por -lo que no se incluiran en los commits (cambios). +lo que no se incluirán en los commits (cambios). ```bash # Añade un archivo en el directorio de trabajo actual. @@ -202,7 +202,7 @@ $ git branch master --edit-description ### checkout Actualiza todos los archivos en el directorio de trabajo para que sean igual que -las versiones almacenadas en el indice, o en un arbol de trabajo especificado. +las versiones almacenadas en el índice, o en un árbol de trabajo especificado. ```bash # Despachar un repositorio. - Por defecto la master branch. (la rama principal llamada 'master') @@ -215,8 +215,8 @@ $ git checkout -b jdei ### clone -Clona, o copia, una repo existente en un nuevo directorio. Tambien añada el -seguimiento hacia las ramas existentes del repo que ha sido clonada, lo que +Clona, o copia, un repositorio existente en un nuevo directorio. También añade el +seguimiento hacia las ramas existentes del repositorio que ha sido clonado, lo que permite subir (push) los archivos hacia una rama remota. ```bash @@ -226,8 +226,8 @@ $ git clone https://github.com/jquery/jquery.git ### commit -Almacena los cambios que almacenados en el indice en un nuevo "commit". Este -commit contiene los cambios hechos mas un resumen hecho por el desarrollador. +Almacena el contenido actual del índice en un nuevo "commit". Este +commit contiene los cambios hechos más un resumen proporcionado por el desarrollador. ```bash # commit with a message @@ -237,8 +237,8 @@ $ git commit -m "jedi anakin wil be - jedis.list" ### diff -Muestra las diferencias entre un archivo en el directorio de trabajo, el indice -y commits. +Muestra las diferencias entre un archivo en el directorio de trabajo, el índice +y los commits. ```bash # Muestra la diferencia entre un directorio de trabajo y el indice. @@ -253,7 +253,7 @@ $ git diff HEAD ### grep -Permite realizar una busqueda rapida en un repositorio. +Permite realizar una busqueda rápida en un repositorio. Configuracion opcionales: diff --git a/es-es/go-es.html.markdown b/es-es/go-es.html.markdown index 86de33ec..c41d693d 100644 --- a/es-es/go-es.html.markdown +++ b/es-es/go-es.html.markdown @@ -1,326 +1,450 @@ --- +name: Go +category: language language: Go lang: es-es filename: learngo-es.go contributors: - ["Sonia Keys", "https://github.com/soniakeys"] + - ["Christopher Bess", "https://github.com/cbess"] + - ["Jesse Johnson", "https://github.com/holocronweaver"] + - ["Quint Guvernator", "https://github.com/qguv"] + - ["Jose Donizetti", "https://github.com/josedonizetti"] + - ["Alexej Friesen", "https://github.com/heyalexej"] translators: - ["Adrian Espinosa", "http://www.adrianespinosa.com"] - ["Jesse Johnson", "https://github.com/holocronweaver"] + - ["Nacho Pacheco -- Feb/2015", "https://github.com/gitnacho"] --- -Go fue creado por la necesidad de hacer el trabajo rápidamente. No es -la última tendencia en informática, pero es la forma nueva y más -rápida de resolver problemas reales. +Go fue creado por la necesidad de hacer el trabajo rápidamente. No es la +última tendencia en informática, pero es la forma nueva y más rápida de +resolver problemas reales. -Tiene conceptos familiares de lenguajes imperativos con tipado -estático. Es rápido compilando y rápido al ejecutar, añade una -concurrencia fácil de entender para las CPUs de varios núcleos de hoy -en día, y tiene características que ayudan con la programación a gran -escala. +Tiene conceptos familiares de lenguajes imperativos con tipado estático. +Es rápido compilando y rápido al ejecutar, añade una concurrencia fácil de +entender para las CPUs de varios núcleos de hoy día, y tiene +características que ayudan con la programación a gran escala. -Go viene con una librería estándar muy buena y una comunidad entusiasta. +Go viene con una biblioteca estándar muy buena y una entusiasta comunidad. ```go // Comentario de una sola línea -/* Comentario - multi línea */ +/* Comentario + multilínea */ -// La cláusula package aparece al comienzo de cada archivo fuente. -// Main es un nombre especial que declara un ejecutable en vez de una librería. +// La cláusula `package` aparece al comienzo de cada fichero fuente. +// `main` es un nombre especial que declara un ejecutable en vez de una +// biblioteca. package main -// La declaración Import declara los paquetes de librerías -// referenciados en este archivo. +// La instrucción `import` declara los paquetes de bibliotecas referidos +// en este fichero. import ( - "fmt" // Un paquete en la librería estándar de Go. - "net/http" // Sí, un servidor web! - "strconv" // Conversiones de cadenas. - m "math" // Librería matemáticas con alias local m. + "fmt" // Un paquete en la biblioteca estándar de Go. + "io/ioutil" // Implementa algunas útiles funciones de E/S. + m "math" // Biblioteca de matemáticas con alias local m. + "net/http" // Sí, ¡un servidor web! + "strconv" // Conversiones de cadenas. ) -// Definición de una función. Main es especial. Es el punto de -// entrada para el ejecutable. Te guste o no, Go utiliza llaves. +// Definición de una función. `main` es especial. Es el punto de entrada +// para el ejecutable. Te guste o no, Go utiliza llaves. func main() { - // Println imprime una línea a stdout. - // Cualificalo con el nombre del paquete, fmt. - fmt.Println("Hello world!") + // Println imprime una línea a stdout. + // Cualificalo con el nombre del paquete, fmt. + fmt.Println("¡Hola mundo!") - // Llama a otra función de este paquete. - beyondHello() + // Llama a otra función de este paquete. + másAlláDelHola() } // Las funciones llevan parámetros entre paréntesis. // Si no hay parámetros, los paréntesis siguen siendo obligatorios. -func beyondHello() { - var x int // Declaración de una variable. - // Las variables se deben declarar antes de utilizarlas. - x = 3 // Asignación de variables. - // Declaración "corta" con := para inferir el tipo, declarar y asignar. - y := 4 - sum, prod := learnMultiple(x, y) // Función devuelve dos valores. - fmt.Println("sum:", sum, "prod:", prod) // Simple salida. - learnTypes() // < y minutes, learn more! +func másAlláDelHola() { + var x int // Declaración de una variable. + // Las variables se deben declarar antes de utilizarlas. + x = 3 // Asignación de variable. + // Declaración "corta" con := para inferir el tipo, declarar y asignar. + y := 4 + suma, producto := aprendeMúltiple(x, y) // La función devuelve dos + // valores. + fmt.Println("suma:", suma, "producto:", producto) // Simple salida. + aprendeTipos() // < y minutos, ¡aprende más! } -// Las funciones pueden tener parámetros y (múltiples!) valores de retorno. -func learnMultiple(x, y int) (sum, prod int) { - return x + y, x * y // Devolver dos valores. +// Las funciones pueden tener parámetros y (¡múltiples!) valores de +// retorno. +func aprendeMúltiple(x, y int) (suma, producto int) { + return x + y, x * y // Devuelve dos valores. } // Algunos tipos incorporados y literales. -func learnTypes() { - // La declaración corta suele darte lo que quieres. - s := "Learn Go!" // tipo cadena - - s2 := ` Un tipo cadena "puro" puede incluir +func aprendeTipos() { + // La declaración corta suele darte lo que quieres. + s := "¡Aprende Go!" // tipo cadena. + s2 := `Un tipo cadena "puro" puede incluir saltos de línea.` // mismo tipo cadena - // Literal no ASCII. Los fuentes de Go son UTF-8. - g := 'Σ' // Tipo rune, un alias de int32, alberga un punto unicode. - f := 3.14195 // float64, el estándar IEEE-754 de coma flotante 64-bit. - c := 3 + 4i // complex128, representado internamente por dos float64. - // Sintaxis Var con inicializadores. - var u uint = 7 // Sin signo, pero la implementación depende del - // tamaño como en int. - var pi float32 = 22. / 7 - - // Sintáxis de conversión con una declaración corta. - n := byte('\n') // byte es un alias de uint8. - - // Los Arrays tienen un tamaño fijo a la hora de compilar. - var a4 [4]int // Un array de 4 ints, inicializados a 0. - a3 := [...]int{3, 1, 5} // Un array de 3 ints, inicializados como se indica. - - // Los Slices tienen tamaño dinámico. Los arrays y slices tienen sus ventajas - // y desventajas pero los casos de uso para los slices son más comunes. - s3 := []int{4, 5, 9} // Comparar con a3. No hay puntos suspensivos. - s4 := make([]int, 4) // Asigna slices de 4 ints, inicializados a 0. - var d2 [][]float64 // Solo declaración, sin asignación. - bs := []byte("a slice") // Sintaxis de conversión de tipo. - - p, q := learnMemory() // Declara p, q para ser un tipo puntero a int. - fmt.Println(*p, *q) // * sigue un puntero. Esto imprime dos ints. - - // Los Maps son arrays asociativos dinámicos, como los hash o - // diccionarios de otros lenguajes. - m := map[string]int{"three": 3, "four": 4} - m["one"] = 1 - - // Las variables no utilizadas en Go producen error. - // El guión bajo permite "utilizar" una variable, pero descartar su valor. - _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs - // Esto cuenta como utilización de variables. - fmt.Println(s, c, a4, s3, d2, m) - - learnFlowControl() // Vuelta al flujo. + // Literal no ASCII. Los ficheros fuente de Go son UTF-8. + g := 'Σ' // Tipo rune, un alias de int32, alberga un carácter unicode. + f := 3.14195 // float64, el estándar IEEE-754 de coma flotante 64-bit. + c := 3 + 4i // complex128, representado internamente por dos float64. + // Sintaxis Var con iniciadores. + var u uint = 7 // Sin signo, pero la implementación depende del tamaño + // como en int. + var pi float32 = 22. / 7 + + // Sintáxis de conversión con una declaración corta. + n := byte('\n') // byte es un alias para uint8. + + // Los Arreglos tienen un tamaño fijo a la hora de compilar. + var a4 [4]int // Un arreglo de 4 ints, iniciados a 0. + a3 := [...]int{3, 1, 5} // Un arreglo iniciado con un tamaño fijo de tres + // elementos, con valores 3, 1 y 5. + // Los Sectores tienen tamaño dinámico. Los arreglos y sectores tienen + // sus ventajas y desventajas pero los casos de uso para los sectores + // son más comunes. + s3 := []int{4, 5, 9} // Comparar con a3. No hay puntos suspensivos. + s4 := make([]int, 4) // Asigna sectores de 4 ints, iniciados a 0. + var d2 [][]float64 // Solo declaración, sin asignación. + bs := []byte("a sector") // Sintaxis de conversión de tipo. + // Debido a que son dinámicos, los sectores pueden crecer bajo demanda. + // Para añadir elementos a un sector, se utiliza la función incorporada + // append(). + // El primer argumento es el sector al que se está anexando. Comúnmente, + // la variable del arreglo se actualiza en su lugar, como en el + // siguiente ejemplo. + sec := []int{1, 2 , 3} // El resultado es un sector de longitud 3. + sec = append(sec, 4, 5, 6) // Añade 3 elementos. El sector ahora tiene una + // longitud de 6. + fmt.Println(sec) // El sector actualizado ahora es [1 2 3 4 5 6] + // Para anexar otro sector, en lugar de la lista de elementos atómicos + // podemos pasar una referencia a un sector o un sector literal como + // este, con elipsis al final, lo que significa tomar un sector y + // desempacar sus elementos, añadiéndolos al sector sec. + sec = append(sec, []int{7, 8, 9} ...) // El segundo argumento es un + // sector literal. + fmt.Println(sec) // El sector actualizado ahora es [1 2 3 4 5 6 7 8 9] + p, q := aprendeMemoria() // Declara p, q para ser un tipo puntero a + // int. + fmt.Println(*p, *q) // * sigue un puntero. Esto imprime dos ints. + + // Los Mapas son arreglos asociativos dinámicos, como los hash o + // diccionarios de otros lenguajes. + m := map[string]int{"tres": 3, "cuatro": 4} + m["uno"] = 1 + + // Las variables no utilizadas en Go producen error. + // El guión bajo permite "utilizar" una variable, pero descartar su + // valor. + _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs + // Esto cuenta como utilización de variables. + fmt.Println(s, c, a4, s3, d2, m) + + aprendeControlDeFlujo() // Vuelta al flujo. +} + +// Es posible, a diferencia de muchos otros lenguajes tener valores de +// retorno con nombre en las funciones. +// Asignar un nombre al tipo que se devuelve en la línea de declaración de +// la función nos permite volver fácilmente desde múltiples puntos en una +// función, así como sólo utilizar la palabra clave `return`, sin nada +// más. +func aprendeRetornosNombrados(x, y int) (z int) { + z = x * y + return // aquí z es implícito, porque lo nombramos antes. } -// Go posee recolector de basura. Tiene puntero pero no aritmética de -// punteros. Puedes cometer un errores con un puntero nil, pero no +// Go posee recolector de basura. Tiene punteros pero no aritmética de +// punteros. Puedes cometer errores con un puntero nil, pero no // incrementando un puntero. -func learnMemory() (p, q *int) { - // q y p tienen un tipo puntero a int. - p = new(int) // Función incorporada que asigna memoria. - // La asignación de int se inicializa a 0, p ya no es nil. - s := make([]int, 20) // Asigna 20 ints a un solo bloque de memoria. - s[3] = 7 // Asignar uno de ellos. - r := -2 // Declarar otra variable local. - return &s[3], &r // & toma la dirección de un objeto. +func aprendeMemoria() (p, q *int) { + // Los valores de retorno nombrados q y p tienen un tipo puntero + // a int. + p = new(int) // Función incorporada que reserva memoria. + // La asignación de int se inicia a 0, p ya no es nil. + s := make([]int, 20) // Reserva 20 ints en un solo bloque de memoria. + s[3] = 7 // Asigna uno de ellos. + r := -2 // Declara otra variable local. + return &s[3], &r // & toma la dirección de un objeto. } -func expensiveComputation() float64 { - return m.Exp(10) +func cálculoCaro() float64 { + return m.Exp(10) } -func learnFlowControl() { - // La declaración If requiere llaves, pero no paréntesis. - if true { - fmt.Println("told ya") - } - // El formato está estandarizado por el comando "go fmt." - if false { - // Pout. - } else { - // Gloat. - } - // Utiliza switch preferiblemente para if encadenados. - x := 42.0 - switch x { - case 0: - case 1: - case 42: - // Los cases no se mezclan, no requieren de "break". - case 43: - // No llega. +func aprendeControlDeFlujo() { + // La declaración If requiere llaves, pero no paréntesis. + if true { + fmt.Println("ya relatado") + } + // El formato está estandarizado por la orden "go fmt." + if false { + // Abadejo. + } else { + // Relamido. + } + // Utiliza switch preferentemente para if encadenados. + x := 42.0 + switch x { + case 0: + case 1: + case 42: + // Los cases no se mezclan, no requieren de "break". + case 43: + // No llega. + } + // Como if, for no utiliza paréntesis tampoco. + // Variables declaradas en for e if son locales a su ámbito. + for x := 0; x < 3; x++ { // ++ es una instrucción. + fmt.Println("iteración", x) + } + // aquí x == 42. + + // For es la única instrucción de bucle en Go, pero tiene formas + // alternativas. + for { // Bucle infinito. + break // ¡Solo bromeaba! + continue // No llega. + } + + // Puedes usar `range` para iterar en un arreglo, un sector, una + // cadena, un mapa o un canal. + // `range` devuelve o bien, un canal o de uno a dos valores (arreglo, + // sector, cadena y mapa). + for clave, valor := range map[string]int{"uno": 1, "dos": 2, "tres": 3} { + // por cada par en el mapa, imprime la clave y el valor + fmt.Printf("clave=%s, valor=%d\n", clave, valor) + } + + // Como en for, := en una instrucción if significa declarar y asignar + // primero, luego comprobar y > x. + if y := cálculoCaro(); y > x { + x = y } - // Como if, for no utiliza paréntesis tampoco. - // Variables declaradas en for y if son locales de su ámbito local. - for x := 0; x < 3; x++ { // ++ es una sentencia. - fmt.Println("iteration", x) - } - // x == 42 aqui. + // Las funciones literales son "cierres". + granX := func() bool { + return x > 100 // Referencia a x declarada encima de la instrucción + // switch. + } + fmt.Println("granX:", granX()) // cierto (la última vez asignamos + // 1e6 a x). + x /= 1.3e3 // Esto hace a x == 1300 + fmt.Println("granX:", granX()) // Ahora es falso. + + // Es más las funciones literales se pueden definir y llamar en línea, + // actuando como un argumento para la función, siempre y cuando: + // a) la función literal sea llamada inmediatamente (), + // b) el tipo del resultado sea del tipo esperado del argumento + fmt.Println("Suma dos números + doble: ", + func(a, b int) int { + return (a + b) * 2 + }(10, 2)) // Llamada con argumentos 10 y 2 + // => Suma dos números + doble: 24 + + // Cuando lo necesites, te encantará. + goto encanto +encanto: + + aprendeFunciónFábrica() // func devolviendo func es divertido(3)(3) + aprendeADiferir() // Un rápido desvío a una importante palabra clave. + aprendeInterfaces() // ¡Buen material dentro de poco! +} - // For es la única sentencia de bucle en Go, pero tiene formas alternativas. - for { // Bucle infinito. - break // Solo bromeaba! - continue // No llega. - } - // Como en for, := en una sentencia if significa declarar y asignar primero, - // luego comprobar y > x. - if y := expensiveComputation(); y > x { - x = y - } - // Los literales de funciones son "closures". - xBig := func() bool { - return x > 100 // Referencia a x declarada encima de la sentencia switch. - } - fmt.Println("xBig:", xBig()) // verdadero (la última vez asignamos 1e6 a x). - x /= m.Exp(9) // Esto lo hace x == e. - fmt.Println("xBig:", xBig()) // Ahora es falso. +func aprendeFunciónFábrica() { + // Las dos siguientes son equivalentes, la segunda es más práctica + fmt.Println(instrucciónFábrica("día")("Un bello", "de verano")) + + d := instrucciónFábrica("atardecer") + fmt.Println(d("Un hermoso", "de verano")) + fmt.Println(d("Un maravilloso", "de verano")) +} - // Cuando lo necesites, te encantará. - goto love -love: +// Los decoradores son comunes en otros lenguajes. Lo mismo se puede hacer +// en Go con funciónes literales que aceptan argumentos. +func instrucciónFábrica(micadena string) func(antes, después string) string { + return func(antes, después string) string { + return fmt.Sprintf("¡%s %s %s!", antes, micadena, después) // nueva cadena + } +} - learnInterfaces() // Buen material dentro de poco! +func aprendeADiferir() (ok bool) { + // las instrucciones diferidas se ejecutan justo antes de que la + // función regrese. + defer fmt.Println("las instrucciones diferidas se ejecutan en orden inverso (PEPS).") + defer fmt.Println("\nEsta línea se imprime primero debido a que") + // Defer se usa comunmente para cerrar un fichero, por lo que la + // función que cierra el fichero se mantiene cerca de la función que lo + // abrió. + return true } // Define Stringer como un tipo interfaz con un método, String. type Stringer interface { - String() string + String() string } -// Define pair como un struct con dos campos int, x e y. -type pair struct { - x, y int +// Define par como una estructura con dos campos int, x e y. +type par struct { + x, y int } -// Define un método del tipo pair. Pair ahora implementa Stringer. -func (p pair) String() string { // p se llama "recibidor" - // Sprintf es otra función pública del paquete fmt. - // La sintaxis con punto referencia campos de p. - return fmt.Sprintf("(%d, %d)", p.x, p.y) +// Define un método en el tipo par. Par ahora implementa a Stringer. +func (p par) String() string { // p se conoce como el "receptor" + // Sprintf es otra función pública del paquete fmt. + // La sintaxis con punto se refiere a los campos de p. + return fmt.Sprintf("(%d, %d)", p.x, p.y) } -func learnInterfaces() { - // La sintaxis de llaves es un "literal struct". Evalúa a un struct - // inicializado. La sintaxis := declara e inicializa p a este struct. - p := pair{3, 4} - fmt.Println(p.String()) // Llamar al método String de p, de tipo pair. - var i Stringer // Declarar i como interfaz tipo Stringer. - i = p // Válido porque pair implementa Stringer. - // Llamar al metodo String de i, de tipo Stringer. Misma salida que arriba. - fmt.Println(i.String()) - - // Las funciones en el paquete fmt llaman al método String para - // preguntar a un objeto por una versión imprimible de si mismo. - fmt.Println(p) // Salida igual que arriba. Println llama al método String. - fmt.Println(i) // Salida igual que arriba. - - learnVariadicParams("great", "learning", "here!") +func aprendeInterfaces() { + // La sintaxis de llaves es una "estructura literal". Evalúa a una + // estructura iniciada. La sintaxis := declara e inicia p a esta + // estructura. + p := par{3, 4} + fmt.Println(p.String()) // Llama al método String de p, de tipo par. + var i Stringer // Declara i como interfaz de tipo Stringer. + i = p // Válido porque par implementa Stringer. + // Llama al metodo String de i, de tipo Stringer. Misma salida que + // arriba. + fmt.Println(i.String()) + + // Las funciones en el paquete fmt llaman al método String para + // consultar un objeto por una representación imprimible de si + // mismo. + fmt.Println(p) // Salida igual que arriba. Println llama al método + // String. + fmt.Println(i) // Salida igual que arriba. + aprendeNúmeroVariableDeParámetros("¡gran", "aprendizaje", "aquí!") } // Las funciones pueden tener número variable de argumentos. -func learnVariadicParams(myStrings ...interface{}) { - // Iterar cada valor de la variadic. - for _, param := range myStrings { - fmt.Println("param:", param) - } - - // Pasar valor variadic como parámetro variadic. - fmt.Println("params:", fmt.Sprintln(myStrings...)) - - learnErrorHandling() +func aprendeNúmeroVariableDeParámetros(misCadenas ...interface{}) { + // Itera en cada valor de los argumentos variables. + // El espacio en blanco aquí omite el índice del argumento arreglo. + for _, parámetro := range misCadenas { + fmt.Println("parámetro:", parámetro) + } + + // Pasa el valor de múltiples variables como parámetro variadic. + fmt.Println("parámetros:", fmt.Sprintln(misCadenas...)) + aprendeManejoDeError() } -func learnErrorHandling() { - // ", ok" forma utilizada para saber si algo funcionó o no. - m := map[int]string{3: "three", 4: "four"} - if x, ok := m[1]; !ok { // ok será falso porque 1 no está en el map. - fmt.Println("no one there") - } else { - fmt.Print(x) // x sería el valor, si estuviera en el map. - } - // Un valor de error comunica más información sobre el problema aparte de "ok". - if _, err := strconv.Atoi("non-int"); err != nil { // _ descarta el valor - // Imprime "strconv.ParseInt: parsing "non-int": invalid syntax". - fmt.Println(err) - } - // Revisarmeos las interfaces más tarde. Mientras tanto, - learnConcurrency() +func aprendeManejoDeError() { + // ", ok" forma utilizada para saber si algo funcionó o no. + m := map[int]string{3: "tres", 4: "cuatro"} + if x, ok := m[1]; !ok { // ok será falso porque 1 no está en el mapa. + fmt.Println("nada allí") + } else { + fmt.Print(x) // x sería el valor, si estuviera en el mapa. + } + // Un valor de error comunica más información sobre el problema aparte + // de "ok". + if _, err := strconv.Atoi("no-int"); err != nil { // _ descarta el + // valor + // Imprime "strconv.ParseInt: parsing "no-int": invalid syntax". + fmt.Println(err) + } + // Revisaremos las interfaces más adelante. Mientras tanto... + aprendeConcurrencia() } -// c es un canal, un objeto de comunicación de concurrencia segura. +// c es un canal, un objeto de comunicación concurrente seguro. func inc(i int, c chan int) { - c <- i + 1 // <- es el operador "enviar" cuando un canal aparece a la izquierda. + c <- i + 1 // <- es el operador "enviar" cuando aparece un canal a la + // izquierda. } // Utilizaremos inc para incrementar algunos números concurrentemente. -func learnConcurrency() { - // Misma función make utilizada antes para crear un slice. Make asigna e - // inicializa slices, maps, y channels. - c := make(chan int) - // Iniciar tres goroutines concurrentes. Los números serán incrementados - // concurrentemente, quizás en paralelo si la máquina es capaz y - // está correctamente configurada. Las tres envían al mismo channel. - go inc(0, c) // go es una sentencia que inicia una nueva goroutine. - go inc(10, c) - go inc(-805, c) - // Leer los tres resultados del channel e imprimirlos. - // No se puede saber en que orden llegarán los resultados! - fmt.Println(<-c, <-c, <-c) // Channel a la derecha, <- es el operador "recibir". - - cs := make(chan string) // Otro channel, este gestiona cadenas. - ccs := make(chan chan string) // Un channel de cadenas de channels. - go func() { c <- 84 }() // Iniciar una nueva goroutine solo para - // enviar un valor. - go func() { cs <- "wordy" }() // Otra vez, para cs en esta ocasión. - // Select tiene una sintáxis parecida a la sentencia switch pero - // cada caso involucra una operacion de channels. Selecciona un caso - // de forma aleatoria de los casos que están listos para comunicarse. - select { - case i := <-c: // El valor recibido puede ser asignado a una variable, - fmt.Printf("it's a %T", i) - case <-cs: // o el valor puede ser descartado. - fmt.Println("it's a string") - case <-ccs: // Channel vacío, no está listo para la comunicación. - fmt.Println("didn't happen.") - } - - // En este punto un valor fue devuelvto de c o cs. Uno de las dos - // goroutines que se iniciaron se ha completado, la otrá permancerá - // bloqueada. - - learnWebProgramming() // Go lo hace. Tu también quieres hacerlo. +func aprendeConcurrencia() { + // Misma función make utilizada antes para crear un sector. Make asigna + // e inicia sectores, mapas y canales. + c := make(chan int) + // Inicia tres rutinasgo concurrentes. Los números serán incrementados + // concurrentemente, quizás en paralelo si la máquina es capaz y está + // correctamente configurada. Las tres envían al mismo canal. + go inc(0, c) // go es una instrucción que inicia una nueva rutinago. + go inc(10, c) + go inc(-805, c) + // Lee los tres resultados del canal y los imprime. + // ¡No se puede saber en que orden llegarán los resultados! + fmt.Println(<-c, <-c, <-c) // Canal a la derecha, <- es el operador + // "recibe". + + cs := make(chan string) // Otro canal, este gestiona cadenas. + ccs := make(chan chan string) // Un canal de canales cadena. + go func() { c <- 84 }() // Inicia una nueva rutinago solo para + // enviar un valor. + go func() { cs <- "verboso" }() // Otra vez, para cs en esta ocasión. + // Select tiene una sintáxis parecida a la instrucción switch pero cada + // caso involucra una operacion con un canal. Selecciona un caso de + // forma aleatoria de los casos que están listos para comunicarse. + select { + case i := <-c: // El valor recibido se puede asignar a una variable, + fmt.Printf("es un %T", i) + case <-cs: // o el valor se puede descartar. + fmt.Println("es una cadena") + case <-ccs: // Canal vacío, no está listo para la comunicación. + fmt.Println("no sucedió.") + } + + // En este punto un valor fue devuelto de c o cs. Una de las dos + // rutinasgo que se iniciaron se ha completado, la otrá permancerá + // bloqueada. + + aprendeProgramaciónWeb() // Go lo hace. Tú también quieres hacerlo. } // Una simple función del paquete http inicia un servidor web. -func learnWebProgramming() { - // El primer parámetro de la direccinón TCP a la que escuchar. - // El segundo parámetro es una interfaz, concretamente http.Handler. - err := http.ListenAndServe(":8080", pair{}) - fmt.Println(err) // no ignorar errores +func aprendeProgramaciónWeb() { +// El primer parámetro es la direccinón TCP a la que escuchar. + // El segundo parámetro es una interfaz, concretamente http.Handler. + go func() { + err := http.ListenAndServe(":8080", par{}) + fmt.Println(err) // no ignora errores + }() + consultaAlServidor() } -// Haz pair un http.Handler implementando su único método, ServeHTTP. -func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { - // Servir datos con un método de http.ResponseWriter. - w.Write([]byte("You learned Go in Y minutes!")) +// Hace un http.Handler de par implementando su único método, ServeHTTP. +func (p par) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // Sirve datos con un método de http.ResponseWriter. + w.Write([]byte("¡Aprendiste Go en Y minutos!")) +} + +func consultaAlServidor() { + resp, err := http.Get("http://localhost:8080") + fmt.Println(err) + defer resp.Body.Close() + cuerpo, err := ioutil.ReadAll(resp.Body) + fmt.Printf("\nEl servidor web dijo: `%s`\n", string(cuerpo)) } ``` -## Para leer más +## Más información + +La raíz de todas las cosas sobre Go es el +[sitio web oficial de Go](http://golang.org/). +Allí puedes seguir el tutorial, jugar interactivamente y leer mucho más. -La raíz de todas las cosas de Go es la [web oficial de Go](http://golang.org/). -Ahí puedes seguir el tutorial, jugar interactivamente y leer mucho. +La definición del lenguaje es altamente recomendada. Es fácil de leer y +sorprendentemente corta (como la definición del lenguaje Go en estos +días). -La propia definición del lenguaje también está altamente -recomendada. Es fácil de leer e increíblemente corta (como otras -definiciones de lenguajes hoy en día) +Puedes jugar con el código en el +[parque de diversiones Go](https://play.golang.org/p/ncRC2Zevag). ¡Trata +de cambiarlo y ejecutarlo desde tu navegador! Ten en cuenta que puedes +utilizar [https://play.golang.org]( https://play.golang.org) como un +[REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) para probar +cosas y el código en el navegador, sin ni siquiera instalar Go. -En la lista de lectura de estudiantes de Go está el código fuente de -la librería estándar. Muy bien documentada, demuestra lo mejor de Go -leíble, comprendible, estilo Go y formas Go. Pincha en el nombre de -una función en la documentación y te aparecerá el código fuente! +En la lista de lecturas para estudiantes de Go está el +[código fuente de la biblioteca estándar](http://golang.org/src/pkg/). +Ampliamente documentado, que demuestra lo mejor del legible y comprensible +Go, con su característico estilo y modismos. ¡O puedes hacer clic en un +nombre de función en [la documentación](http://golang.org/pkg/) y +aparecerá el código fuente! +Otro gran recurso para aprender Go está en +[Go con ejemplos](http://goconejemplos.com/). diff --git a/es-es/javascript-es.html.markdown b/es-es/javascript-es.html.markdown index a1348508..fd01e7b9 100644 --- a/es-es/javascript-es.html.markdown +++ b/es-es/javascript-es.html.markdown @@ -16,8 +16,7 @@ con Java para aplicaciones más complejas. Debido a su integracion estrecha con web y soporte por defecto de los navegadores modernos se ha vuelto mucho más común para front-end que Java. -JavaScript no sólo se limita a los navegadores web: -* Node.js: Un proyecto que provee con un ambiente para el motor V8 de Google Chrome. +JavaScript no sólo se limita a los navegadores web, aunque: Node.js, Un proyecto que proporciona un entorno de ejecución independiente para el motor V8 de Google Chrome, se está volviendo más y más popular. ¡La retroalimentación es bienvenida! Puedes encontrarme en: [@adambrenecki](https://twitter.com/adambrenecki), o @@ -49,6 +48,7 @@ hazAlgo() // Toda la aritmética básica funciona como uno esperaría. 1 + 1; // = 2 +0.1 + 0.2; // = 0.30000000000000004 8 - 1; // = 7 10 * 2; // = 20 35 / 5; // = 7 @@ -102,9 +102,11 @@ false; // Los tipos no importan con el operador ==... "5" == 5; // = true +null == undefined; // = true // ...a menos que uses === "5" === 5; // = false +null === undefined; // false // Los Strings funcionan como arreglos de caracteres // Puedes accesar a cada caracter con la función charAt() @@ -126,7 +128,7 @@ undefined; // usado para indicar que un valor no está presente actualmente // Aunque 0 === "0" sí es false. /////////////////////////////////// -// 2. Variables, Arreglos y Objetos +// 2. Variables, Arrays y Objetos // Las variables se declaran con la palabra var. JavaScript cuenta con tipado dinámico, // así que no se necesitan aplicar tipos. La asignación se logra con el operador =. @@ -220,7 +222,6 @@ for (var i = 0; i < 5; i++){ } // && es un "y" lógico, || es un "o" lógico -var casa = {tamano:"grande",casa:"color"}; if (casa.tamano == "grande" && casa.color == "azul"){ casa.contiene = "oso"; } diff --git a/es-es/julia-es.html.markdown b/es-es/julia-es.html.markdown index 203ee3bb..e4181609 100644 --- a/es-es/julia-es.html.markdown +++ b/es-es/julia-es.html.markdown @@ -4,757 +4,937 @@ contributors: - ["Leah Hanson", "http://leahhanson.us"] translators: - ["Guillermo Garza", "http://github.com/ggarza"] + - ["Ismael Venegas Castelló", "https://github.com/Ismael-VC"] filename: learnjulia-es.jl lang: es-es --- -Julia es un nuevo lenguaje funcional homoiconic enfocado en computación técnica. -Aunque que tiene todo el poder de macros homoiconic, funciones de primera -clase, y control de bajo nivel, Julia es tan fácil de aprender y utilizar como -Python. +![JuliaLang](http://s13.postimg.org/z89djuwyf/julia_small.png) -Esto se basa en la versión de desarrollo actual de Julia, del 18 de octubre de -2013. +[Julia](http://julialanges.github.io) es un [lenguaje de programación](http://es.wikipedia.org/wiki/Lenguaje_de_programaci%C3%B3n) [multiplataforma](http://es.wikipedia.org/wiki/Multiplataforma) y [multiparadigma](http://es.wikipedia.org/wiki/Lenguaje_de_programaci%C3%B3n_multiparadigma) de [tipado dinámico](http://es.wikipedia.org/wiki/Tipado_din%C3%A1mico), [alto nivel](http://es.wikipedia.org/wiki/Lenguaje_de_alto_nivel) y [alto desempeño](http://es.wikipedia.org/wiki/Computaci%C3%B3n_de_alto_rendimiento) para la computación [genérica](http://es.wikipedia.org/wiki/Lenguaje_de_programaci%C3%B3n_de_prop%C3%B3sito_general), [técnica y científica](http://es.wikipedia.org/wiki/Computaci%C3%B3n_cient%C3%ADfica), con una sintaxis que es familiar para los usuarios de otros entornos de computación técnica y científica. Provee de un [sofisticado compilador JIT](http://es.wikipedia.org/wiki/Compilaci%C3%B3n_en_tiempo_de_ejecuci%C3%B3n), [ejecución distribuida y paralela](http://docs.julialang.org/en/release-0.3/manual/parallel-computing), [precisión numérica](http://julia.readthedocs.org/en/latest/manual/integers-and-floating-point-numbers) y de una [extensa librería con funciones matemáticas](http://docs.julialang.org/en/release-0.3/stdlib). La librería estándar, escrita casi completamente en Julia, también integra las mejores y más maduras librerías de C y Fortran para el [álgebra lineal](http://docs.julialang.org/en/release-0.3/stdlib/linalg), [generación de números aleatorios](http://docs.julialang.org/en/release-0.3/stdlib/numbers/?highlight=random#random-numbers), [procesamiento de señales](http://docs.julialang.org/en/release-0.3/stdlib/math/?highlight=signal#signal-processing), y [procesamiento de cadenas](http://docs.julialang.org/en/release-0.3/stdlib/strings). Adicionalmente, la comunidad de [desarrolladores de Julia](https://github.com/JuliaLang/julia/graphs/contributors) contribuye un número de [paquetes externos](http://pkg.julialang.org) a través del gestor de paquetes integrado de Julia a un paso acelerado. [IJulia](https://github.com/JuliaLang/IJulia.jl), una colaboración entre las comunidades de [IPython](http://ipython.org) y Julia, provee de una poderosa interfaz gráfica basada en el [navegador para Julia](https://juliabox.org). -```ruby +En Julia los programas están organizados entorno al [despacho múltiple](http://docs.julialang.org/en/release-0.3/manual/methods/#man-methods); definiendo funciones y sobrecargándolas para diferentes combinaciones de tipos de argumentos, los cuales también pueden ser definidos por el usuario. + +### ¡Prueba Julia ahora mismo! + +* [TryJupyter](https://try.jupyter.org) +* [JuliaBox](https://juliabox.org) +* [SageMathCloud](https://cloud.sagemath.com) + +### Resumen de Características: -# Comentarios de una línea comienzan con una almohadilla (o signo gato) +* [Despacho múltiple](http://en.wikipedia.org/wiki/Multiple_dispatch): permite definir el comportamiento de las funciones a través de múltiples combinaciones de tipos de argumentos (**métodos**). +* Sistema de **tipado dinámico**: tipos para la documentación, la optimización y el despacho. +* [Buen desempeño](http://julialang.org/benchmarks), comparado al de lenguajes **estáticamente compilados** como C. +* [Gestor de paquetes](http://docs.julialang.org/en/release-0.3/stdlib/pkg) integrado. +* [Macros tipo Lisp](http://docs.julialang.org/en/release-0.3/manual/metaprogramming/#macros) y otras comodidades para la [meta programación](http://docs.julialang.org/en/release-0.3/manual/metaprogramming). +* Llamar funciones de otros lenguajes, mediante paquetes como: **Python** ([PyCall](https://github.com/stevengj/PyCall.jl)), [Mathematica](http://github.com/one-more-minute/Mathematica.jl), **Java** ([JavaCall](http://github.com/aviks/JavaCall.jl)), **R** ([Rif](http://github.com/lgautier/Rif.jl) y [RCall](http://github.com/JuliaStats/RCall.jl)) y **Matlab** ([MATLAB](http://github.com/JuliaLang/MATLAB.jl)). +* [Llamar funciones de C y Fortran](http://docs.julialang.org/en/release-0.3/manual/calling-c-and-fortran-code) **directamente**: sin necesidad de usar envoltorios u APIs especiales. +* Poderosas características de **línea de comandos** para [gestionar otros procesos](http://docs.julialang.org/en/release-0.3/manual/running-external-programs). +* Diseñado para la [computación paralela y distribuida](http://docs.julialang.org/en/release-0.3/manual/parallel-computing) **desde el principio**. +* [Corrutinas](http://en.wikipedia.org/wiki/Coroutine): hilos ligeros "**verdes**". +* Los [tipos definidos por el usuario](http://docs.julialang.org/en/release-0.3/manual/types) son tan **rápidos y compactos** como los tipos estándar integrados. +* [Generación automática de código](http://docs.julialang.org/en/release-0.3/stdlib/base/?highlight=%40code#internals) **eficiente y especializado** para diferentes tipos de argumentos. +* [Conversiones y promociones](http://docs.julialang.org/en/release-0.3/manual/conversion-and-promotion) para tipos numéricos y de otros tipos, **elegantes y extensibles**. +* Soporte eficiente para [Unicode](http://es.wikipedia.org/wiki/Unicode), incluyendo [UTF-8](http://es.wikipedia.org/wiki/UTF-8) pero sin limitarse solo a este. +* [Licencia MIT](https://github.com/JuliaLang/julia/blob/master/LICENSE.md): libre y de código abierto. + +Esto se basa en la versión `0.3.11`. + +```ruby +# Los comentarios de una línea comienzan con una almohadilla (o signo de gato). -#= Commentarios multilinea pueden escribirse - usando '#=' antes de el texto y '=#' - después del texto. También se pueden anidar. +#= + Los comentarios multilínea pueden escribirse + usando '#=' antes de el texto y '=#' + después del texto. También se pueden anidar. =# -#################################################### -## 1. Tipos de datos primitivos y operadores. -#################################################### -# Todo en Julia es una expresión. +############################################## +# 1. Tipos de datos primitivos y operadores. # +############################################## + +# Todo en Julia es una expresión (Expr). # Hay varios tipos básicos de números. -3 # => 3 (Int64) -3.2 # => 3.2 (Float64) -2 + 1im # => 2 + 1im (Complex{Int64}) -2//3 # => 2//3 (Rational{Int64}) +3 # => 3 # Int64 +3.2 # => 3.2 # Float64 +2 + 1im # => 2 + 1im # Complex{Int64} +2 // 3 # => 2//3 # Rational{Int64} # Todos los operadores infijos normales están disponibles. -1 + 1 # => 2 -8 - 1 # => 7 -10 * 2 # => 20 -35 / 5 # => 7.0 -5/2 # => 2.5 # dividir un Int por un Int siempre resulta en un Float -div (5, 2) # => 2 # para un resultado truncado, usa div -5 \ 35 # => 7.0 -2 ^ 2 # => 4 # exponente, no es xor -12 % 10 # => 2 - -# Refuerza la precedencia con paréntesis -(1 + 3) * 2 # => 8 - -# Operadores a nivel de bit -~2 # => -3 # bitwise not -3 & 5 # => 1 # bitwise and -2 | 4 # => 6 # bitwise or -2 $ 4 # => 6 # bitwise xor -2 >>> 1 # => 1 # logical shift right -2 >> 1 # => 1 # arithmetic shift right -2 << 1 # => 4 # logical/arithmetic shift left - -# Se puede utilizar la función bits para ver la representación binaria de un -# número. +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7.0 # dividir un Int por un Int siempre resulta + # en un Float +5 / 2 # => 2.5 +div(5, 2) # => 2 # para un resultado truncado, usa la función div +5 \ 35 # => 7.0 +2 ^ 2 # => 4 # exponente, no es XOR +12 % 10 # => 2 + +# Refuerza la precedencia con paréntesis. +(1 + 3) * 2 # => 8 + +# Operadores a nivel de bit. +~2 # => -3 # bitwise NOT +3 & 5 # => 1 # bitwise AND +2 | 4 # => 6 # bitwise OR +2 $ 4 # => 6 # bitwise XOR +2 >>> 1 # => 1 # desplazamiento lógico hacia la derecha +2 >> 1 # => 1 # desplazamiento aritmético hacia la derecha +2 << 1 # => 4 # desplazamiento lógico/aritmético hacia la izquierda + +# Se puede utilizar la función bits para ver la representación +# binaria de un número. bits(12345) # => "0000000000000000000000000000000000000000000000000011000000111001" + bits(12345.0) # => "0100000011001000000111001000000000000000000000000000000000000000" -# Valores 'boolean' (booleanos) son primitivos -true -false - -# Operadores Boolean (booleanos) -!true # => false -!false # => true -1 == 1 # => true -2 == 1 # => false -1 != 1 # => false -2 != 1 # => true -1 < 10 # => true -1 > 10 # => false -2 <= 2 # => true -2 >= 2 # => true +# Los valores booleanos (Bool) son primitivos. +true # => true +false # => false + +# Operadores booleanos. +!true # => false +!false # => true +1 == 1 # => true +2 == 1 # => false +1 != 1 # => false +2 != 1 # => true +1 < 10 # => true +1 > 10 # => false +2 <= 2 # => true +2 >= 2 # => true + # ¡Las comparaciones pueden ser concatenadas! -1 < 2 < 3 # => true -2 < 3 < 2 # => false +1 < 2 < 3 # => true +2 < 3 < 2 # => false -# Strings se crean con " -"Esto es un string." +# Los literales de cadenas (String) se crean con la comilla doble: " +"Esto es una cadena." -# Literales de caracteres se escriben con ' +# Los literales de caracteres (Char) se crean con la comilla simple: ' 'a' -# Una string puede ser indexado como una array de caracteres -"Esto es un string."[1] # => 'E' # Índices en Julia empiezen del 1 -# Sin embargo, esto no va a funcionar bien para strings UTF8, +# Una cadena puede ser indexada como una arreglo de caracteres. +"Esto es un string."[1] # => 'E' # Los índices en Julia comienzan en: 1 + +# Sin embargo, esto no va a funcionar bien para las cadenas UTF8 (UTF8String), # Lo que se recomienda es la iteración (map, for, etc). -# $ puede ser utilizado para la interpolación de strings: -"2 + 2 = $(2 + 2)" # => "2 + 2 = 4" -# Se puede poner cualquier expresión de Julia dentro los paréntesis. +# $ puede ser utilizado para la interpolación de cadenas, se puede poner +# cualquier expresión de Julia dentro los paréntesis. +"2 + 2 = $(2 + 2)" # => "2 + 2 = 4" -# Otro forma de formatear strings es el macro printf -@printf "%d es menor de %f" 4.5 5.3 # 5 es menor de 5.300000 +# Otra forma para formatear cadenas es usando el macro printf. +@printf "%d es menor de %f\n" 4.5 5.3 # 5 es menor de 5.300000 -# Imprimir es muy fácil -println("Soy Julia. ¡Encantado de conocerte!") +# ¡Imprimir es muy fácil! +println("¡Hola Julia!") # ¡Hola Julia! -#################################################### -## 2. Variables y Colecciones -#################################################### + +############################## +# 2. Variables y Colecciones # +############################## # No hay necesidad de declarar las variables antes de asignarlas. -una_variable = 5 # => 5 -una_variable # => 5 +una_variable = 5 # => 5 +una_variable # => 5 -# Acceder a variables no asignadas previamente es una excepción. +# Acceder a una variable no asignada previamente es una excepción. try - otra_variable # => ERROR: some_other_var not defined + otra_variable # ERROR: otra_variable not defined catch e - println(e) + println(e) # UndefVarError(:otra_variable) end -# Los nombres de variables comienzan con una letra. -# Después de eso, puedes utilizar letras, dígitos, guiones y signos de +# Los nombres de variables comienzan con una letra o guion bajo: _. +# Después de eso, puedes utilizar letras, dígitos, guiones bajos y signos de # exclamación. -OtraVariable123! = 6 # => 6 +otraVariable_123! = 6 # => 6 + +# También puedes utilizar caracteres Unicode. +☃ = 8 # => 8 -# También puede utilizar caracteres unicode -☃ = 8 # => 8 # Estos son especialmente útiles para la notación matemática -2 * π # => 6.283185307179586 - -# Una nota sobre las convenciones de nomenclatura de Julia: -# -# * Los nombres de las variables aparecen en minúsculas, con separación de -# palabra indicado por underscore ('\ _'). -# -# * Los nombres de los tipos comienzan con una letra mayúscula y separación de -# palabras se muestra con CamelCase en vez de underscore. -# -# * Los nombres de las funciones y los macros están en minúsculas, sin -# underscore. -# -# * Funciones que modifican sus inputs tienen nombres que terminan en!. Estos -# funciones a veces se llaman mutating functions o in-place functions. - -# Los Arrays almacenan una secuencia de valores indexados entre 1 hasta n -a = Int64[] # => 0-element Int64 Array - -# Literales de arrays 1-dimensionales se pueden escribir con valores separados -# por comas. -b = [4, 5, 6] # => 3-element Int64 Array: [4, 5, 6] -b[1] # => 4 -b[end] # => 6 - -# Los arrays 2-dimensionales usan valores separados por espacios y filas -# separados por punto y coma. -matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4] - -# Añadir cosas a la final de una lista con push! y append! -push!(a,1) # => [1] -push!(a,2) # => [1,2] -push!(a,4) # => [1,2,4] -push!(a,3) # => [1,2,4,3] -append!(a,b) # => [1,2,4,3,4,5,6] - -# Eliminar de la final con pop -pop!(b) # => 6 y b ahora es [4,5] - -# Vamos a ponerlo de nuevo -push!(b, 6) # b es ahora [4,5,6] de nuevo. - -a[1] # => 1 # recuerdan que los índices de Julia empiezan desde 1, no desde 0! - -# end es una abreviatura para el último índice. Se puede utilizar en cualquier -# expresión de indexación -a[end] # => 6 - -# tambien hay shift y unshift -shift!(a) # => 1 y a es ahora [2,4,3,4,5,6] -unshift!(a,7) # => [7,2,4,3,4,5,6] - -# Nombres de funciónes que terminan en exclamaciones indican que modifican -# su argumento. -arr = [5,4,6] # => 3-element Int64 Array: [5,4,6] -sort(arr) # => [4,5,6]; arr es todavía [5,4,6] -sort!(arr) # => [4,5,6]; arr es ahora [4,5,6] - -# Buscando fuera de límites es un BoundsError +# (multiplicación implicita). +2π # => 6.283185307179586 + +#= + Una nota sobre las convenciones de nomenclatura de Julia: + + * Los nombres de las variables aparecen en minúsculas, con separación de + palabra indicado por un guion bajo: + + otra_variable + + * Los nombres de los tipos comienzan con una letra mayúscula y separación de + palabras se muestra con CamelCase en vez de guión bajo: + + OtroTipo + + * Los nombres de las funciones y los macros están en minúsculas, sin + underscore: + + otromacro + + * Funciones que modifican sus entradas tienen nombres que terminan en: !. + Estas funciones a veces se les llaman funciones transformadoras o + funciones in situ: + + otra_funcion! +=# + +# Los arreglos (Array) almacenan una secuencia de valores indexados de entre 1 hasta n. +a = Int64[] # => 0-element Array{Int64,1} + +# Los literales de arregos unidimensionales se pueden escribir con valores +# separados por comas. +b = [4, 5, 6] +#= + => 3-element Array{Int64,1}: + 4 + 5 + 6 +=# +b[1] # => 4 +b[end] # => 6 + +# Los arreglos bidimensionales usan valores separados por espacios y filas +# separadas por punto y coma. +matrix = [1 2; 3 4] +#= + => 2x2 Array{Int64,2}: + 1 2 + 3 4 +=# + +# Añadir cosas al final de un arreglo con push! y append!. +push!(a, 1) # => [1] +push!(a, 2) # => [1,2] +push!(a, 4) # => [1,2,4] +push!(a, 3) # => [1,2,4,3] +append!(a, b) # => [1,2,4,3,4,5,6] + +# Eliminar del final con pop!. +pop!(b) # => 6 y b ahora es: [4,5] + +# Vamos a ponerlo de nuevo. +push!(b, 6) # b es ahora [4,5,6] de nuevo + +a[1] # => 1 # recuerda, los índices de Julia empiezan desde 1, no desde 0! + +# end es una abreviatura para el último índice. Se puede utilizar en cualquier +# expresión de indexación. +a[end] # => 6 + +# También hay shift! y unshift!. +shift!(a) # => 1 y a es ahora: [2,4,3,4,5,6] +unshift!(a, 7) # => [7,2,4,3,4,5,6] + +# Los nombres de funciones que terminan en exclamaciones indican que modifican +# su o sus argumentos de entrada. +arr = [5, 4, 6] # => 3-element Array{Int64,1}: [5,4,6] +sort(arr) # => [4,5,6] y arr es todavía: [5,4,6] +sort!(arr) # => [4,5,6] y arr es ahora: [4,5,6] + +# Buscando fuera de límites es un BoundsError. try - a[0] # => ERROR: BoundsError() in getindex at array.jl:270 - a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270 + a[0] # ERROR: BoundsError() in getindex at array.jl:270 + a[end+1] # ERROR: BoundsError() in getindex at array.jl:270 catch e - println(e) + println(e) # BoundsError() end -# Errors dan la línea y el archivo de su procedencia, aunque sea en el standard -# library. Si construyes Julia de source, puedes buscar en el source para -# encontrar estos archivos. +# Las excepciones y los errores dan la línea y el archivo de su procedencia, +# aunque provenga de la librería estándar. Si compilas Julia del código fuente, +# puedes buscar en el código para encontrar estos archivos. -# Se puede inicializar arrays de un range -a = [1:5] # => 5-element Int64 Array: [1,2,3,4,5] +# Se puede inicializar un arreglo con un rango (Range). +a = [1:5] # => 5-element Array{Int64,1}: [1,2,3,4,5] -# Puedes mirar en ranges con sintaxis slice. -a[1:3] # => [1, 2, 3] -a[2:end] # => [2, 3, 4, 5] +# Puedes mirar en los rangos con la sintaxis de rebanada. +a[1:3] # => [1,2,3] +a[2:end] # => [2,3,4,5] -# Eliminar elementos de una array por índice con splice! -arr = [3,4,5] -splice!(arr,2) # => 4 ; arr es ahora [3,5] +# Eliminar elementos de un arreglo por índice con splice! +arr = [3, 4, 5] +splice!(arr, 2) # => 4 y arr es ahora: [3,5] -# Concatenar listas con append! -b = [1,2,3] -append!(a,b) # ahroa a es [1, 2, 3, 4, 5, 1, 2, 3] +# Concatenar arreglos con append! +b = [1, 2, 3] +append!(a, b) # a ahora es: [1,2,3,4,5,1,2,3] -# Comprueba la existencia en una lista con in -in(1, a) # => true +# Comprueba la existencia de un elemento en un arreglo con in. +in(1, a) # => true -# Examina la longitud con length -length(a) # => 8 +# Examina la longitud con length. +length(a) # => 8 + +# Las tuplas (Tuple) son inmutables. +tup = (1, 2, 3) # => (1,2,3) # una tupla tipo (Int64,Int64,Int64) +tup[1] # => 1 -# Tuples son immutable. -tup = (1, 2, 3) # => (1,2,3) # un (Int64,Int64,Int64) tuple. -tup[1] # => 1 try: - tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64) + tup[1] = 3 # ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64) catch e - println(e) + println(e) # MethodError(setindex!,(:tup,3,1)) end -# Muchas funciones de lista también trabajan en las tuples -length(tup) # => 3 -tup[1:2] # => (1,2) -in(2, tup) # => true +# Muchas funciones de arreglos también trabajan en con las tuplas. +length(tup) # => 3 +tup[1:2] # => (1,2) +in(2, tup) # => true + +# Se pueden desempacar las tuplas en variables individuales. +a, b, c = (1, 2, 3) # => (1,2,3) # ahora a es 1, b es 2 y c es 3 + +# Los tuplas se crean, incluso si se omiten los paréntesis. +d, e, f = 4, 5, 6 # => (4,5,6) -# Se puede desempacar tuples en variables -a, b, c = (1, 2, 3) # => (1,2,3) # a is now 1, b is now 2 and c is now 3 +# Una tupla de un elemento es distinta del valor que contiene. +(1,) == 1 # => false +(1) == 1 # => true -# Los tuples se crean, incluso si se omite el paréntesis -d, e, f = 4, 5, 6 # => (4,5,6) +# Mira que fácil es cambiar dos valores! +e, d = d, e # => (5,4) # ahora d es 5 y e es 4 -# Un tuple 1-elemento es distinto del valor que contiene -(1,) == 1 # => false -(1) == 1 # => true +# Los diccionarios (Dict) son arreglos asociativos. +dicc_vacio = Dict() # => Dict{Any,Any} with 0 entries -# Mira que fácil es cambiar dos valores -e, d = d, e # => (5,4) # d is now 5 and e is now 4 +# Se puede crear un diccionario usando una literal. +dicc_lleno = ["uno" => 1, "dos" => 2, "tres" => 3] +#= + => Dict{ASCIIString,Int64} with 3 entries: + "tres" => 3 + "dos" => 2 + "uno" => 1 +=# +# Busca valores con: []. +dicc_lleno["uno"] # => 1 -# Dictionaries almanecan mapeos -dict_vacio = Dict() # => Dict{Any,Any}() +# Obtén todas las claves con. +keys(dicc_lleno) +#= + => KeyIterator for a Dict{ASCIIString,Int64} with 3 entries. Keys: + "tres" + "dos" + "uno" +=# -# Se puede crear un dictionary usando un literal -dict_lleno = ["one"=> 1, "two"=> 2, "three"=> 3] -# => Dict{ASCIIString,Int64} +# Nota: los elementos del diccionario no están ordenados y no se guarda el orden +# en que se insertan. -# Busca valores con [] -dict_lleno["one"] # => 1 +# Obtén todos los valores. +values(dicc_lleno) +#= + => ValueIterator for a Dict{ASCIIString,Int64} with 3 entries. Values: + 3 + 2 + 1 +=# -# Obtén todas las claves -keys(dict_lleno) -# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) -# Nota - claves del dictionary no están ordenados ni en el orden en que se -# insertan. +# Nota: igual que el anterior en cuanto a ordenamiento de los elementos. -# Obtén todos los valores -values(dict_lleno) -# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) -# Nota - Igual que el anterior en cuanto a ordenamiento de claves. +# Comprueba si una clave existe en un diccionario con in y haskey. +in(("uno", 1), dicc_lleno) # => true +in(("tres", 3), dicc_lleno) # => false -# Compruebe si hay existencia de claves en un dictionary con in y haskey -in(("uno", 1), dict_lleno) # => true -in(("tres", 3), dict_lleno) # => false -haskey(dict_lleno, "one") # => true -haskey(dict_lleno, 1) # => false +haskey(dicc_lleno, "uno") # => true +haskey(dicc_lleno, 1) # => false -# Tratando de buscar una clave que no existe producirá un error +# Tratar de obtener un valor con una clave que no existe producirá un error. try - dict_lleno["dos"] # => ERROR: key not found: dos in getindex at dict.jl:489 + # ERROR: key not found: cuatro in getindex at dict.jl:489 + dicc_lleno["cuatro"] catch e - println(e) + println(e) # KeyError("cuatro") end -# Utilice el método get para evitar ese error proporcionando un valor -# predeterminado -# get(dictionary,key,default_value) -get(dict_lleno,"one",4) # => 1 -get(dict_lleno,"four",4) # => 4 +# Utiliza el método get para evitar este error proporcionando un valor +# predeterminado: get(diccionario, clave, valor_predeterminado). +get(dicc_lleno, "uno", 4) # => 1 +get(dicc_lleno, "cuatro", 4) # => 4 -# Usa Sets para representar colecciones (conjuntos) de valores únicos, no -# ordenadas -conjunto_vacio = Set() # => Set{Any}() -# Iniciar una set de valores -conjunto_lleno = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4) +# Usa conjuntos (Set) para representar colecciones de valores únicos, no +# ordenados. +conjunto_vacio = Set() # => Set{Any}({}) -# Añadir más valores a un conjunto -push!(conjunto_lleno,5) # => Set{Int64}(5,4,2,3,1) -push!(conjunto_lleno,5) # => Set{Int64}(5,4,2,3,1) +# Iniciar una conjunto de valores. +conjunto_lleno = Set(1, 2, 2, 3, 4) # => Set{Int64}({4,2,3,1}) -# Compruebe si los valores están en el conjunto -in(2, conjunto_lleno) # => true -in(10, conjunto_lleno) # => false +# Añadir más valores a un conjunto. +push!(conjunto_lleno, 5) # => Set{Int64}({4,2,3,5,1}) +push!(conjunto_lleno, 5) # => Set{Int64}({4,2,3,5,1}) -# Hay funciones de intersección de conjuntos, la unión, y la diferencia. -conjunto_otro= Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3) -intersect(conjunto_lleno, conjunto_otro) # => Set{Int64}(3,4,5) -union(conjunto_lleno, conjunto_otro) # => Set{Int64}(1,2,3,4,5,6) -setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4) +# Comprobar si los valores están en el conjunto. +in(2, conjunto_lleno) # => true +in(10, conjunto_lleno) # => false +# Hay funciones de intersección, unión y diferencia de conjuntos. +otro_conjunto = Set(3, 4, 5, 6) # => Set{Int64}({6,4,5,3}) +intersect(conjunto_lleno, otro_conjunto) # => Set{Int64}({3,4,5}) +union(conjunto_lleno, otro_conjunto) # => Set{Int64}({1,2,3,4,5,6}) +setdiff(Set(1, 2, 3, 4), Set(2, 3, 5)) # => Set{Int64}({1,4}) -#################################################### -## 3. Control de Flujo -#################################################### -# Hagamos una variable +####################### +# 3. Control de Flujo # +####################### + +# Hagamos una variable. una_variable = 5 -# Aquí está una declaración de un 'if'. La indentación no es significativa en -# Julia +# Aquí está la declaración de un if. La indentación no es significativa en +# Julia. if una_variable > 10 - println("una_variable es completamente mas grande que 10.") -elseif una_variable < 10 # Este condición 'elseif' es opcional. - println("una_variable es mas chica que 10.") -else # Esto también es opcional. - println("una_variable es de hecho 10.") + println("una_variable es completamente mayor que 10.") +elseif una_variable < 10 # esta condición elseif es opcional + println("una_variable es menor que 10.") +else # esto también es opcional + println("De echo una_variable es 10.") end -# => imprime "una_variable es mas chica que 10." +# imprime: una_variable es menor que 10. -# For itera sobre tipos iterables -# Tipos iterables incluyen Range, Array, Set, Dict, y String. -for animal=["perro", "gato", "raton"] - println("$animal es un mamifero") - # Se puede usar $ para interpolar variables o expresiónes en strings +# El bucle for itera sobre tipos iterables, ie. Range, Array, Set, +# Dict y String. +for animal in ["perro", "gato", "ratón"] + # Se puede usar $ para interpolar variables o expresiones en ls cadenas. + println("$animal es un mamífero.") end -# imprime: -# perro es un mamifero -# gato es un mamifero -# raton es un mamifero +#= + imprime: + perro es un mamífero. + gato es un mamífero. + ratón es un mamífero. +=# -for a in ["perro"=>"mamifero","gato"=>"mamifero","raton"=>"mamifero"] - println("$(a[1]) es un $(a[2])") +for a in ["perro" => "mamífero", "gato" => "mamífero", "ratón" => "mamífero"] + println("$(a[1]) es un $(a[2]).") end -# imprime: -# perro es un mamifero -# gato es un mamifero -# raton es un mamifero +#= + imprime: + perro es un mamífero. + gato es un mamífero. + ratón es un mamífero. +=# -for (k,v) in ["perro"=>"mamifero", "gato"=>"mamifero", "raton"=>"mamifero"] - println("$k es un $v") +for (k,v) in ["perro"=>"mamífero", "gato"=>"mamífero", "ratón"=>"mamífero"] + println("$k es un $v.") end -# imprime: -# perro es un mamifero -# gato es un mamifero -# raton es un mamifero +#= + imprime: + perro es un mamífero. + gato es un mamífero. + ratón es un mamífero. +=# -# While itera hasta que una condición no se cumple. +# El bucle while itera hasta que una condición se deje de cumplir. x = 0 while x < 4 println(x) - x += 1 # versión corta de x = x + 1 + x += 1 # versión corta de: x = x + 1 end -# imprime: -# 0 -# 1 -# 2 -# 3 +#= +imprime: + 0 + 1 + 2 + 3 +=# -# Maneja excepciones con un bloque try/catch -try - error("ayuda") +# Maneja excepciones con un bloque try/catch. +try # intentar + error("Ooops!") catch e - println("capturando $e") + println("capturando: $e") # capturando: ErrorException("Ooops!") end -# => capturando ErrorException("ayuda") -#################################################### -## 4. Funciones -#################################################### +################ +# 4. Funciones # +################ -# Usa 'function' para crear nuevas funciones +# Usa function para crear nuevas funciones. -#function nombre(arglist) -# cuerpo... -#end +#= + function nombre(arglist) + cuerpo... + end +=# function suma(x, y) println("x es $x e y es $y") - # Las funciones devuelven el valor de su última declaración + # las funciones devuelven el valor de su última expresión x + y end +# => suma (generic function with 1 method) + +suma(5, 6) # => 11 # después de imprimir: x es 5 e y es 6 -suma(5, 6) # => 11 # después de imprimir "x es 5 e y es de 6" +# También puedes usar esta otra sintaxis para definir funciones! +resta(x, y) = x - y # => resta (generic function with 1 method) # Puedes definir funciones que toman un número variable de -# argumentos posicionales +# argumentos posicionales (el ... se llama un splat). function varargs(args...) + # Usa la palabra clave return para regresar desde cualquier + # lugar de la función. return args - # Usa la palabra clave return para devolver en cualquier lugar de la función end # => varargs (generic function with 1 method) -varargs(1,2,3) # => (1,2,3) +varargs(1, 2, 3) # => (1,2,3) +varargs([1, 2, 3]) # => ([1,2,3],) -# El ... se llama un splat. -# Acabamos de utilizar lo en una definición de función. -# También se puede utilizar en una llamada de función, -# donde va splat un Array o el contenido de un Tuple en la lista de argumentos. -Set([1,2,3]) # => Set{Array{Int64,1}}([1,2,3]) # Produce un Set de Arrays -Set([1,2,3]...) # => Set{Int64}(1,2,3) # esto es equivalente a Set(1,2,3) +# Acabamos de utilizar el splat (...) en la definición de una función. También +# se puede utilizar al llamar a una función, donde se esparce un arreglo, tupla +# o en general una secuencia iterable en la tupla de argumentos. +varargs([1, 2, 3]...) # => (1,2,3) # igual que: varargs(1, 2, 3) -x = (1,2,3) # => (1,2,3) -Set(x) # => Set{(Int64,Int64,Int64)}((1,2,3)) # un Set de Tuples -Set(x...) # => Set{Int64}(2,3,1) +x = (1, 2, 3) # => (1,2,3) +varargs(x) # => ((1,2,3),) +varargs(x...) # => (1,2,3) +varargs("abc"...) # => ('a','b','c') -# Puede definir funciones con argumentos posicionales opcionales -function defaults(a,b,x=5,y=6) +# Puedes definir funciones con argumentos posicionales opcionales. +function defaults(a, b, x=5, y=6) return "$a $b y $x $y" end +# => defaults (generic function with 3 methods) + +defaults('h', 'g') # => "h g y 5 6" +defaults('h', 'g', 'j') # => "h g y j 6" +defaults('h', 'g', 'j', 'k') # => "h g y j k" -defaults('h','g') # => "h g y 5 6" -defaults('h','g','j') # => "h g y j 6" -defaults('h','g','j','k') # => "h g y j k" try - defaults('h') # => ERROR: no method defaults(Char,) - defaults() # => ERROR: no methods defaults() + defaults('h') # ERROR: `defaults` has no method matching defaults(::Char) + defaults() # ERROR: `defaults` has no method matching defaults() catch e - println(e) + println(e) # MethodError(defaults,('h',)) end -# Puedes definir funciones que toman argumentos de palabra clave -function args_clave(;k1=4,nombre2="hola") # note the ; - return ["k1"=>k1,"nombre2"=>nombre2] +# Puedes definir funciones que tomen argumentos de palabras clave. +function args_clave(;k1=4, nombre2="hola") # nota el punto y coma: ; + return ["k1" => k1, "nombre2" => nombre2] end +# => args_clave (generic function with 1 method) -args_clave(nombre2="ness") # => ["nombre2"=>"ness","k1"=>4] -args_clave(k1="mine") # => ["k1"=>"mine","nombre2"=>"hola"] -args_clave() # => ["nombre2"=>"hola","k1"=>4] +args_clave(nombre2="ness") # => ["nombre2"=>"ness","k1"=>4] +args_clave(k1="mine") # => ["k1"=>"mine","nombre2"=>"hola"] +args_clave() # => ["nombre2"=>"hola","k1"=>4] -# Puedes combinar todo tipo de argumentos en la misma función -function todos_los_args(arg_normal, arg_posicional_opcional=2; arg_clave="foo") - println("argumento normal: $arg_normal") - println("argumento optional: $arg_posicional_opcional") - println("argumento de clave: $arg_clave") +# Puedes combinar todo tipo de argumentos en la misma función. +function todos_los_args(arg_posicional, arg_opcional=2; arg_clave="foo") + println("argumento posicional: $arg_posicional") + println(" argumento opcional: $arg_opcional") + println(" argumento clave: $arg_clave") end +# => todos_los_args (generic function with 2 methods) +# No se necesita punto y coma ; al llamar la función usando un argumento clave, +# esto solo es necesario en la definición de la función. todos_los_args(1, 3, arg_clave=4) -# imprime: -# argumento normal: 1 -# argumento optional: 3 -# argumento de clave: 4 +#= + imprime: + argumento posicional: 1 + argumento opcional: 3 + argumento clave: 4 +=# -# Julia tiene funciones de primera clase +# Julia tiene funciones de primera clase. function crear_suma(x) - suma = function (y) + suma = function (y) # función anónima return x + y end return suma end +# => crear_suma (generic function with 1 method) -# Esta es el sintaxis "stabby lambda" para crear funciones anónimas -(x -> x > 2)(3) # => true +# Esta es otra sintaxis (estilo cálculo lambda), para crear funciones anónimas. +(x -> x > 2)(3) # => true # Esta función es idéntica a la crear_suma implementación anterior. -function crear_suma(x) - y -> x + y -end +crear_suma(x) = y -> x + y -# También puedes nombrar la función interna, si quieres +# También puedes nombrar la función interna, si quieres. function crear_suma(x) function suma(y) x + y end suma end +# => crear_suma (generic function with 1 method) -suma_10 = crear_suma(10) -suma_10(3) # => 13 +suma_10 = crear_suma(10) # => suma (generic function with 1 method) +suma_10(3) # => 13 +# Hay funciones integradas de orden superior. +map(suma_10, [1, 2, 3]) # => [11,12,13] +filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6,7] -# Hay funciones integradas de orden superior -map(suma_10, [1,2,3]) # => [11, 12, 13] -filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] +# Se puede pasar un bloque a las funciones cuyo primer argumento posicional +# es otra función, como en map y filter. +map([1, 2, 3]) do arr + suma_10(arr) +end +#= + => 3-element Array{Int64,1}: + 11 + 12 + 13 +=# -# Podemos usar listas por comprensión para mapeos -[suma_10(i) for i=[1, 2, 3]] # => [11, 12, 13] -[suma_10(i) for i in [1, 2, 3]] # => [11, 12, 13] +filter([3, 4, 5, 6, 7]) do arr + (x -> x > 5)(arr) +end +#= + => 2-element Array{Int64,1}: + 6 + 7 +=# -#################################################### -## 5. Tipos -#################################################### +# Podemos usar comprensiones de listas multidimensionales. +[suma_10(i) for i = [1, 2, 3]] # => [11, 12, 13] # 1D +[suma_10(i) for i in [1, 2, 3]] # => [11, 12, 13] + +[i*j for i = [1:3], j in [1:3]] # 2D +#= + => 3x3 Array{Int64,2}: + 1 2 3 + 2 4 6 + 3 6 9 +=# + +[i*j/k for i = [1:3], j = [1:3], k in [1:3]] # 3D +#= + => 3x3x3 Array{Float64,3}: + [:, :, 1] = + 1.0 2.0 3.0 + 2.0 4.0 6.0 + 3.0 6.0 9.0 + + [:, :, 2] = + 0.5 1.0 1.5 + 1.0 2.0 3.0 + 1.5 3.0 4.5 + + [:, :, 3] = + 0.333333 0.666667 1.0 + 0.666667 1.33333 2.0 + 1.0 2.0 3.0 +=# + + +############ +# 5. Tipos # +############ -# Julia tiene sistema de tipos. # Cada valor tiene un tipo y las variables no tienen propios tipos. -# Se puede utilizar la función `typeof` para obtener el tipo de un valor. -typeof(5) # => Int64 +# Se puede utilizar la función typeof para obtener el tipo de un valor. +typeof(5) # => Int64 # en un sistema de 64 bits, de lo contrario: Int32 -# Los tipos son valores de primera clase -typeof(Int64) # => DataType -typeof(DataType) # => DataType -# DataType es el tipo que representa los tipos, incluyéndose a sí mismo. +# Los tipos son valores de primera clase, DataType es el tipo que representa a +# los tipos, incluyéndose a sí mismo. +typeof(Int64) # => DataType +typeof(DataType) # => DataType -# Los tipos se usan para la documentación, optimizaciones, y envio. -# No están comprobados estáticamente. +# Los tipos se usan para la documentación, para optimizaciones +# y el despacho múltiple. No están comprobados estáticamente. -# Los usuarios pueden definir tipos -# Son como registros o estructuras en otros idiomas. -# Nuevos tipos se definen utilizado la palabra clave `type`. +# Los usuarios pueden definir sus propios tipos. +# Son como registros o estructuras en otros idiomas. +# Un nuevo tipos se define utilizado la palabra clave type. # type Nombre -# field::OptionalType +# atributo::UnTipo # las anotaciones de tipos son opcionales # ... # end type Tigre - longituddecola::Float64 - colordelpelaje # no incluyendo una anotación de tipo es el mismo que `::Any` + longitud_cola::Float64 + color_pelaje # sin una anotación de tipo, es lo mismo que `::Any` end -# Los argumentos del constructor por default son las propiedades -# del tipo, en el orden en que están listados en la definición -tigger = Tigre(3.5,"anaranjado") # => Tiger(3.5,"anaranjado") +# Los argumentos del constructor por defecto son los atributos +# del tipo, en el orden en que están listados en la definición. +tigre = Tigre(3.5, "anaranjado") # => Tigre(3.5,"anaranjado") -# El tipo funciona como la función constructora de valores de ese tipo -sherekhan = typeof(tigger)(5.6,"fuego") # => Tiger(5.6,"fuego") +# El tipo funciona como método constructor para los valores de ese tipo. +sherekhan = typeof(tigre)(5.6, "fuego") # => Tigre(5.6,"fuego") -# Este estilo de tipos son llamados tipos concrete -# Se pueden crear instancias, pero no pueden tener subtipos. -# La otra clase de tipos es tipos abstractos (abstract types). +# Este estilo de tipos son llamados tipos concretos. +# Se pueden crear instancias de estos, pero no pueden tener subtipos. +# La otra clase de tipos son los tipos abstractos. # abstract Nombre -abstract Gato # sólo un nombre y un punto en la jerarquía de tipos - -# De los tipos Abstract no se pueden crear instancias, pero pueden tener -# subtipos. Por ejemplo, Number es un tipo abstracto. -subtypes(Number) # => 6-element Array{Any,1}: - # Complex{Float16} - # Complex{Float32} - # Complex{Float64} - # Complex{T<:Real} - # Real -subtypes(Gato) # => 0-element Array{Any,1} - -# Cada tipo tiene un supertipo, utilice la función `súper` para conseguirlo. -typeof(5) # => Int64 -super(Int64) # => Signed -super(Signed) # => Real -super(Real) # => Number -super(Number) # => Any -super(super(Signed)) # => Number -super(Any) # => Any -# Todo de estos tipos, a excepción de Int64, son abstractos. - -# <: es el operador de subtipos -type Leon <: Gato # Leon es un subtipo de Gato - color_de_crin - rugido::String -end +abstract Gato # sólo un nombre y un punto en la jerarquía de tipos + +# No se pueden crear instancias de los tipos abstractos, pero pueden tener +# subtipos. Por ejemplo, Number es un tipo abstracto. +subtypes(Number) +#= + => 2-element Array{Any,1}: + Complex{T<:Real} + Real +=# + +subtypes(Gato) # => 0-element Array{Any,1} -# Se puede definir más constructores para su tipo. -# Sólo defina una función del mismo nombre que el tipo -# y llame a un constructor existente para obtener un valor del tipo correcto -Leon(rugido::String) = Leon("verde",rugido) -# Este es un constructor externo porque es fuera de la definición del tipo - -type Pantera <: Gato # Pantera tambien es un a subtipo de Cat - color_de_ojos - Pantera() = new("verde") - # Panteras sólo tendrán este constructor, y ningún constructor - # predeterminado. +# Cada tipo tiene un supertipo, utiliza la función súper para conseguirlo. +typeof(5) # => Int64 +super(Int64) # => Signed +super(Signed) # => Integer +super(Integer) # => Real +super(Real) # => Number +super(Number) # => Any +super(super(Signed)) # => Real +super(Any) # => Any + +# Todos estos tipos, a excepción de Int64, son abstractos. + +# <: es el operador de subtipos. +type Leon <: Gato # Leon es un subtipo de Gato + color_crin + rugido::String end -# Utilizar constructores internos, como Panther hace, te da control sobre cómo -# se pueden crear valores del tipo. Cuando sea posible, debes utilizar -# constructores exteriores en lugar de los internos. -#################################################### -## 6. Envio múltiple -#################################################### +# Se pueden definir más constructores para un tipo. +# Sólo define una función del mismo nombre que el tipo y llama al constructor +# existente para obtener un valor del tipo correcto. -# En Julia, todas las funciones nombradas son funciones genéricas. -# Esto significa que se construyen a partir de muchos métodos pequeños -# Cada constructor de Leon es un método de la función genérica Leon. +# Este es un constructor externo porque está fuera de la definición del tipo. +Leon(rugido::String) = Leon("verde", rugido) -# Por ejemplo, vamos a hacer un maullar función: +type Pantera <: Gato # Pantera también es un a subtipo de Gato + color_ojos -# Definiciones para Leon, Pantera, y Tigre -function maullar(animal::Leon) - animal.rugido # acceso utilizando notación de puntos + # Pantera sólo tendrá este constructor, y ningún constructor predeterminado. + Pantera() = new("verde") end -function maullar(animal::Pantera) - "grrr" -end +# Utilizar constructores internos, como se hace en Pantera, te da control sobre +# cómo se pueden crear valores de este tipo. Cuando sea posible, debes utilizar +# constructores externos en lugar de internos. -function maullar(animal::Tigre) - "rawwwr" -end -# Prueba de la función maullar -maullar(tigger) # => "rawwr" -maullar(Leon("cafe","ROAAR")) # => "ROAAR" -maullar(Pantera()) # => "grrr" +######################## +# 6. Despacho Múltiple # +######################## -# Revisar la jerarquía de tipos locales -issubtype(Tigre,Gato) # => false -issubtype(Leon,Gato) # => true -issubtype(Pantera,Gato) # => true +# En Julia, todas las funciones nombradas son funciones genéricas. +# Esto significa que se construyen a partir de muchos métodos más pequeños. +# Cada constructor de Leon es un método de la función genérica Leon. -# Definición de una función que toma Gatos -function mascota(gato::Gato) - println("El gato dice $(maullar(gato))") -end +# Por ejemplo, vamos a hacer métodos para Leon, Pantera, y Tigre de una +# función genérica maullar: + +# acceso utilizando notación de puntos +maullar(animal::Leon) = animal.rugido +# => maullar (generic function with 1 method) +maullar(animal::Pantera) = "grrr" +# => maullar (generic function with 2 methods) +maullar(animal::Tigre) = "rawwwr" +# => maullar (generic function with 3 methods) + +# Se puede obtener una lista de métodos con la función methods. +methods(maullar) +#= + # 3 methods for generic function "maullar": + maullar(animal::Leon) at none:1 + maullar(animal::Pantera) at none:1 + maullar(animal::Tigre) at none:1 +=# + +# Prueba de la función maullar. +maullar(tigre) # => "rawwwr" +maullar(Leon("cafe", "ROAAR")) # => "ROAAR" +maullar(Pantera()) # => "grrr" + +# Revisar la jerarquía de tipos locales. +issubtype(Tigre, Gato) # => false # igual que: Tigre <: Gato +issubtype(Leon, Gato) # => true # igual que: Leon <: Gato +issubtype(Pantera, Gato) # => true + +# Definición de una función que acepta argumentos de tipo Gato. +mascota(gato::Gato) = println("El gato dice $(maullar(gato))") + +mascota(Leon("42")) # El gato dice 42 -mascota(Leon("42")) # => imprime "El gato dice 42" try - mascota(tigger) # => ERROR: no method mascota(Tigre)) + mascota(tigre) # ERROR: `mascota` has no method matching mascota(::Tigre) catch e - println(e) + println(e) # MethodError(mascota,(Tigre(3.5,"anaranjado"),)) end -# En los lenguajes orientados a objetos, expedición única es común. Esto -# significa que el método se recogió basándose en el tipo del primer argumento. -# En Julia, todos los tipos de argumentos contribuyen a seleccionar el mejor -# método. +# En los lenguajes orientados a objetos, el despacho simple es común. Esto +# significa que la implementación del método a llamar se selecciona en base +# al tipo del primer argumento. + +# En Julia, los tipos de todos los argumentos contribuyen a seleccionar método +# más específico. # Vamos a definir una función con más argumentos, para que podamos ver la # diferencia -function pelear(t::Tigre,c::Gato) - println("¡El tigre $(t.colordelpelaje) gana!") -end +pelear(t::Tigre, c::Gato) = println("¡El tigre $(t.color_pelaje) gana!") # => pelear (generic function with 1 method) -pelear(tigger,Pantera()) # => imprime ¡El tigre anaranjado gana! -pelear(tigger,Leon("ROAR")) # => ¡El tigre anaranjado gana! +pelear(tigre, Pantera()) # ¡El tigre anaranjado gana! +pelear(tigre, Leon("ROAR")) # ¡El tigre anaranjado gana! -# Vamos a cambiar el comportamiento cuando el Gato es específicamente un Leon -pelear(t::Tigre,l::Leon) = println("El león con melena $(l.color_de_crin) gana") +# Vamos a cambiar el comportamiento cuando el Gato sea específicamente un Leon. +pelear(t::Tigre, l::Leon) = println("El león con melena $(l.color_crin) gana.") # => pelear (generic function with 2 methods) -pelear(tigger,Pantera()) # => imprime ¡El tigre anaranjado gana! -pelear(tigger,Leon("ROAR")) # => imprime El león con melena verde gana +pelear(tigre, Pantera()) # ¡El tigre anaranjado gana! +pelear(tigre, Leon("ROAR")) # El león con melena verde gana. -# No necesitamos un tigre para poder luchar -pelear(l::Leon,c::Gato) = println("El gato victorioso dice $(maullar(c))") -# => fight (generic function with 3 methods) +# No necesitamos un tigre para poder luchar. +pelear(l::Leon, c::Gato) = println("El gato victorioso dice $(maullar(c)).") +# => pelear (generic function with 3 methods) -pelear(Leon("balooga!"),Pantera()) # => imprime El gato victorioso dice grrr +methods(pelear) +#= + # 3 methods for generic function "pelear": + pelear(t::Tigre,l::Leon) at none:2 + pelear(t::Tigre,c::Gato) at none:1 + pelear(l::Leon,c::Gato) at none:2 +=# + +pelear(Leon("balooga!"), Pantera()) # El gato victorioso dice grrr. try - pelear(Pantera(),Leon("RAWR")) # => ERROR: no method pelear(Pantera, Leon)) -catch + # ERROR: `pelear` has no method matching pelear(::Pantera, ::Leon) + pelear(Pantera(),Leon("RAWR")) +catch # no hacer nada con la excepción atrapada end -# Un metodo con el gato primero +# Un metodo con el tipo Gato primero. pelear(c::Gato,l::Leon) = println("El gato le gana al León") -# Warning: New definition -# pelear(Gato,Leon) at none:1 -# is ambiguous with: -# pelear(Leon,Gato) at none:1. -# To fix, define -# pelear(Leon,Leon) -# before the new definition. -# pelear (generic function with 4 methods) - -# Esta advertencia se debe a que no está claro que metodo de pelear será llamado -# en: -pelear(Leon("RAR"),Leon("cafe","rar")) # => imprime El gato victorioso dice rar -# El resultado puede ser diferente en otras versiones de Julia +#= + Warning: New definition + pelear(Gato,Leon) at none:1 + is ambiguous with: + pelear(Leon,Gato) at none:1. + To fix, define + pelear(Leon,Leon) + before the new definition. + pelear (generic function with 4 methods) +=# +# Esta advertencia se debe a que no está claro que método de pelear +# será llamado en: +pelear(Leon("RAR"),Leon("cafe","rar")) # El gato victorioso dice rar. + +# El resultado puede ser diferente en otras versiones de Julia pelear(l::Leon,l2::Leon) = println("Los leones llegan a un empate") -pelear(Leon("GR"),Leon("cafe","rar")) # => imprime Los leones llegan a un empate - - -# Un vistazo al nivel bajo -# Se puede echar un vistazo a la LLVM y el código ensamblador generado. - -area_cuadrada(l) = l * l # area_cuadrada (generic function with 1 method) - -area_cuadrada(5) # => 25 - -# ¿Qué sucede cuando damos area_cuadrada diferentes argumentos? -code_native(area_cuadrada, (Int32,)) - # .section __TEXT,__text,regular,pure_instructions - # Filename: none - # Source line: 1 # Prologue - # push RBP - # mov RBP, RSP - # Source line: 1 - # movsxd RAX, EDI # Fetch l from memory? - # imul RAX, RAX # Square l and store the result in RAX - # pop RBP # Restore old base pointer - # ret # Result will still be in RAX - -code_native(area_cuadrada, (Float32,)) - # .section __TEXT,__text,regular,pure_instructions - # Filename: none - # Source line: 1 - # push RBP - # mov RBP, RSP - # Source line: 1 - # vmulss XMM0, XMM0, XMM0 # Scalar single precision multiply (AVX) - # pop RBP - # ret - -code_native(area_cuadrada, (Float64,)) - # .section __TEXT,__text,regular,pure_instructions - # Filename: none - # Source line: 1 - # push RBP - # mov RBP, RSP - # Source line: 1 - # vmulsd XMM0, XMM0, XMM0 # Scalar double precision multiply (AVX) - # pop RBP - # ret - # - -# Ten en cuenta que Julia usará instrucciones de "floating point" si alguno de -# los argumentos son "floats" -# Vamos a calcular el área de un círculo -area_circulo(r) = pi * r * r # circle_area (generic function with 1 method) -area_circulo(5) # 78.53981633974483 + +pelear(Leon("GR"),Leon("cafe","rar")) # Los leones llegan a un empate + + +################################ +# 7. Un vistazo de bajo nivel. # +################################ + +# Se puede echar un vistazo al código IR de LLVM y al código +# ensamblador generado. +area_cuadrado(l) = l * l # => area_cuadrado (generic function with 1 method) + +area_cuadrado(5) # => 25 + +# ¿Qué sucede cuando damos area_cuadrada diferentes tipos de argumentos? +code_native(area_cuadrado, (Int32,)) +#= + .section __TEXT,__text,regular,pure_instructions + Filename: none + Source line: 1 # prólogo + push RBP + mov RBP, RSP + Source line: 1 + imul RDI, RDI # elevar l al cuadrado + mov RAX, RDI # almacenar el resultado en RAX + pop RBP # restaurar el puntero base anterior + ret # el resultado estará en RAX +=# + +code_native(area_cuadrado, (Float32,)) +#= + .section __TEXT,__text,regular,pure_instructions + Filename: none + Source line: 1 + push RBP + mov RBP, RSP + Source line: 1 + mulss XMM0, XMM0 # multiplicación escalar de presición simple (AVX) + pop RBP + ret +=# + +code_native(area_cuadrado, (Float64,)) +#= + .section __TEXT,__text,regular,pure_instructions + Filename: none + Source line: 1 + push RBP + mov RBP, RSP + Source line: 1 + mulsd XMM0, XMM0 # multiplicación escalar de presición doble (AVX) + pop RBP + ret +=# + +# Ten en cuenta que Julia usará instrucciones de punto flotante si el tipo de +# alguno de los argumentos es flotante. + +# Vamos a calcular el área de un círculo. +area_circulo(r) = π * r * r # area_circulo (generic function with 1 method) +area_circulo(5) # 78.53981633974483 code_native(area_circulo, (Int32,)) - # .section __TEXT,__text,regular,pure_instructions - # Filename: none - # Source line: 1 - # push RBP - # mov RBP, RSP - # Source line: 1 - # vcvtsi2sd XMM0, XMM0, EDI # Load integer (r) from memory - # movabs RAX, 4593140240 # Load pi - # vmulsd XMM1, XMM0, QWORD PTR [RAX] # pi * r - # vmulsd XMM0, XMM0, XMM1 # (pi * r) * r - # pop RBP - # ret - # +#= + .section __TEXT,__text,regular,pure_instructions + Filename: none + Source line: 1 + push RBP + mov RBP, RSP + Source line: 1 + cvtsi2sd XMM1, EDI # cargar entero r de la memoria + movabs RAX, 4477117456 # cargar constante matemática π + movsd XMM0, QWORD PTR [RAX] + mulsd XMM0, XMM1 # π * r + mulsd XMM0, XMM1 # (π * r) * r + pop RBP + ret +=# code_native(area_circulo, (Float64,)) - # .section __TEXT,__text,regular,pure_instructions - # Filename: none - # Source line: 1 - # push RBP - # mov RBP, RSP - # movabs RAX, 4593140496 - # Source line: 1 - # vmulsd XMM1, XMM0, QWORD PTR [RAX] - # vmulsd XMM0, XMM1, XMM0 - # pop RBP - # ret - # +#= + .section __TEXT,__text,regular,pure_instructions + Filename: none + Source line: 1 + push RBP + mov RBP, RSP + movabs RAX, 4477120336 + movsd XMM1, QWORD PTR [RAX] + Source line: 1 + mulsd XMM1, XMM0 + mulsd XMM1, XMM0 + movaps XMM0, XMM1 + pop RBP + ret +=# ``` -## ¿Listo para más? +![Julia-tan](http://s27.postimg.org/x37ndhz0j/julia_tan_small.png) -Puedes obtener muchos más detalles en [The Julia Manual](http://docs.julialang.org/en/latest/manual/) +## ¿Listo para más? -El mejor lugar para obtener ayuda con Julia es el (muy amable) [lista de correos](https://groups.google.com/forum/#!forum/julia-users). +Para más detalles, lee el [manual de Julia](http://docs.julialang.org/en/release-0.3). +El mejor lugar para obtener ayuda con Julia, es en su amigable [lista de correos](https://groups.google.com/forum/#!forum/julia-users). diff --git a/es-es/python-es.html.markdown b/es-es/python-es.html.markdown index f7a0ec02..4930eebc 100644 --- a/es-es/python-es.html.markdown +++ b/es-es/python-es.html.markdown @@ -4,6 +4,7 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] translators: - ["Camilo Garrido", "http://www.twitter.com/hirohope"] + - ["Fabio Souto", "http://fabiosouto.me"] lang: es-es filename: learnpython-es.py --- @@ -30,27 +31,47 @@ Nota: Este artículo aplica a Python 2.7 específicamente, pero debería ser apl # Tienes números 3 #=> 3 -# Matemática es lo que esperarías -1 + 1 #=> 2 -8 - 1 #=> 7 -10 * 2 #=> 20 -35 / 5 #=> 7 +# Evidentemente puedes realizar operaciones matemáticas +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 # La división es un poco complicada. Es división entera y toma la parte entera # de los resultados automáticamente. -5 / 2 #=> 2 +5 / 2 #=> 2 # Para arreglar la división necesitamos aprender sobre 'floats' # (números de coma flotante). 2.0 # Esto es un 'float' -11.0 / 4.0 #=> 2.75 ahhh...mucho mejor +11.0 / 4.0 #=> 2.75 ahhh...mucho mejor + +# Resultado de la división de enteros truncada para positivos y negativos +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 # funciona con números en coma flotante +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 + +# El operador módulo devuelve el resto de una división entre enteros +7 % 3 # => 1 + +# Exponenciación (x elevado a y) +2**4 # => 16 # Refuerza la precedencia con paréntesis -(1 + 3) * 2 #=> 8 +(1 + 3) * 2 #=> 8 + +# Operadores booleanos +# Nota: "and" y "or" son sensibles a mayúsculas +True and False #=> False +False or True #=> True -# Valores 'boolean' (booleanos) son primitivos -True -False +# Podemos usar operadores booleanos con números enteros +0 and 2 #=> 0 +-5 or 0 #=> -5 +0 == False #=> True +2 == True #=> False +1 == True #=> True # Niega con 'not' not True #=> False @@ -90,7 +111,7 @@ not False #=> True # Una forma más reciente de formatear strings es el método 'format'. # Este método es la forma preferida "{0} pueden ser {1}".format("strings", "formateados") -# Puedes usar palabras claves si no quieres contar. +# Puedes usar palabras clave si no quieres contar. "{nombre} quiere comer {comida}".format(nombre="Bob", comida="lasaña") # None es un objeto @@ -107,8 +128,8 @@ None is None #=> True # None, 0, y strings/listas vacíos(as) todas se evalúan como False. # Todos los otros valores son True -0 == False #=> True -"" == False #=> True +bool(0) #=> False +bool("") #=> False #################################################### @@ -130,16 +151,16 @@ otra_variable # Levanta un error de nombre # 'if' puede ser usado como una expresión "yahoo!" if 3 > 2 else 2 #=> "yahoo!" -# Listas almacenan secuencias +# Las listas almacenan secuencias lista = [] # Puedes empezar con una lista prellenada otra_lista = [4, 5, 6] # Añadir cosas al final de una lista con 'append' -lista.append(1) #lista ahora es [1] -lista.append(2) #lista ahora es [1, 2] -lista.append(4) #lista ahora es [1, 2, 4] -lista.append(3) #lista ahora es [1, 2, 4, 3] +lista.append(1) # lista ahora es [1] +lista.append(2) # lista ahora es [1, 2] +lista.append(4) # lista ahora es [1, 2, 4] +lista.append(3) # lista ahora es [1, 2, 4, 3] # Remueve del final de la lista con 'pop' lista.pop() #=> 3 y lista ahora es [1, 2, 4] # Pongámoslo de vuelta @@ -173,11 +194,11 @@ lista.extend(otra_lista) # lista ahora es [1, 2, 3, 4, 5, 6] # Chequea la existencia en una lista con 1 in lista #=> True -# Examina el largo de una lista con 'len' +# Examina el tamaño de una lista con 'len' len(lista) #=> 6 -# Tuplas son como listas pero son inmutables. +# Las tuplas son como las listas, pero son inmutables. tupla = (1, 2, 3) tupla[0] #=> 1 tupla[0] = 3 # Levanta un error TypeError @@ -266,7 +287,7 @@ conjunto_lleno | otro_conjunto #=> {1, 2, 3, 4, 5, 6} # Hagamos sólo una variable una_variable = 5 -# Aquí está una declaración de un 'if'. ¡La indentación es significativa en Python! +# Aquí está una declaración de un 'if'. ¡La indentación es importante en Python! # imprime "una_variable es menor que 10" if una_variable > 10: print "una_variable es completamente mas grande que 10." @@ -400,12 +421,12 @@ class Humano(object): # Un atributo de clase es compartido por todas las instancias de esta clase especie = "H. sapiens" - # Constructor basico + # Constructor básico, se llama al instanciar la clase. def __init__(self, nombre): # Asigna el argumento al atributo nombre de la instancia self.nombre = nombre - # Un metodo de instancia. Todos los metodos toman self como primer argumento + # Un método de instancia. Todos los metodos toman self como primer argumento def decir(self, msg): return "%s: %s" % (self.nombre, msg) @@ -470,6 +491,56 @@ import math dir(math) +#################################################### +## 7. Avanzado +#################################################### + +# Los generadores permiten evaluación perezosa +def duplicar_numeros(iterable): + for i in iterable: + yield i + i + +# Un generador crea valores sobre la marcha +# En vez de generar y devolver todos los valores de una vez, crea un valor +# en cada iteración. En este ejemplo los valores mayores que 15 no serán +# procesados en duplicar_numeros. +# Nota: xrange es un generador que hace lo mismo que range. +# Crear una lista de 1 a 900000000 lleva mucho tiempo y ocupa mucho espacio. +# xrange crea un generador, mientras que range crea toda la lista. +# Añadimos un guion bajo a los nombres de variable que coinciden con palabras +# reservadas de python. +xrange_ = xrange(1, 900000000) + +# duplica todos los números hasta que encuentra un resultado >= 30 +for i in duplicar_numeros(xrange_): + print i + if i >= 30: + break + +# Decoradores +# en este ejemplo pedir rodea a hablar +# Si por_favor es True se cambiará el mensaje. +from functools import wraps + + +def pedir(target_function): + @wraps(target_function) + def wrapper(*args, **kwargs): + msg, por_favor = target_function(*args, **kwargs) + if por_favor: + return "{} {}".format(msg, "¡Por favor! Soy pobre :(") + return msg + + return wrapper + + +@pedir +def hablar(por_favor=False): + msg = "¿Me puedes comprar una cerveza?" + return msg, por_favor + +print hablar() # ¿Me puedes comprar una cerveza? +print hablar(por_favor=True) # ¿Me puedes comprar una cerveza? ¡Por favor! Soy pobre :( ``` ## ¿Listo para más? @@ -481,6 +552,7 @@ dir(math) * [The Official Docs](http://docs.python.org/2.6/) * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) * [Python Module of the Week](http://pymotw.com/2/) +* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182) ### Encuadernados diff --git a/fr-fr/livescript-fr.html.markdown b/fr-fr/livescript-fr.html.markdown new file mode 100644 index 00000000..9c3b8003 --- /dev/null +++ b/fr-fr/livescript-fr.html.markdown @@ -0,0 +1,360 @@ +--- +language: LiveScript +filename: learnLivescript-fr.ls +contributors: + - ["Christina Whyte", "http://github.com/kurisuwhyte/"] +translators: + - ["Morgan Bohn", "https://github.com/morganbohn"] +lang: fr-fr +--- + +LiveScript est un langage qui compile en JavaScript. Il a un rapport direct +avec JavaScript, et vous permet d'écrire du JavaScript plus simplement, plus +efficacement et sans répétitivité. LiveScript ajoute non seulement des +fonctionnalités pour écrire du code fonctionnel, mais possède aussi nombre +d'améliorations pour la programmation orientée objet et la programmation +impérative. + +LiveScript est un descendant direct de [Coco][], indirect de [CoffeeScript][], +avec lequel il a beaucoup plus de compatibilité. + +[Coco]: http://satyr.github.io/coco/ +[CoffeeScript]: http://coffeescript.org/ + +Vous pouvez contacter l'auteur du guide original en anglais ici : +[@kurisuwhyte](https://twitter.com/kurisuwhyte) + + +```coffeescript +# Comme son cousin CoffeeScript, LiveScript utilise le symbole dièse pour les +# commentaires sur une ligne. + +/* + Les commentaires sur plusieurs lignes utilisent la syntaxe du C. Utilisez-les + si vous voulez préserver les commentaires dans la sortie JavaScript. + */ +``` +```coffeescript +# LiveScript utilise l'indentation pour délimiter les blocs de code plutôt que +# les accolades, et les espaces pour appliquer les fonctions (bien que les +# parenthèses soient utilisables). + + +######################################################################## +## 1. Valeurs basiques +######################################################################## + +# Les valeurs non définies sont représentées par le mot clé `void` à la place de +# `undefined` +void # comme `undefined` mais plus sûr (ne peut pas être redéfini) + +# Une valeur non valide est représentée par Null. +null + + +# Les booléens s'utilisent de la façon suivante: +true +false + +# Et il existe divers alias les représentant également: +on; off +yes; no + + +# Puis viennent les nombres entiers et décimaux. +10 +0.4 # Notez que le `0` est requis + +# Dans un souci de lisibilité, vous pouvez utiliser les tirets bas et les +# suffixes sur les nombres. Il seront ignorés à la compilation. +12_344km + + +# Les chaînes sont des séquences immutables de caractères, comme en JS: +"Christina" # Les apostrophes fonctionnent également! +"""Multi-line + strings + are + okay + too.""" + +# De temps à autre, vous voulez encoder un mot clé; la notation en backslash +# rend cela facile: +\keyword # => 'keyword' + + +# Les tableaux sont des collections ordonnées de valeurs. +fruits = + * \apple + * \orange + * \pear + +# Il peuvent être écrits de manière plus consises à l'aide des crochets: +fruits = [ \apple, \orange, \pear ] + +# Vous pouvez également utiliser la syntaxe suivante, à l'aide d'espaces, pour +# créer votre liste de valeurs: +fruits = <[ apple orange pear ]> + +# Vous pouvez récupérer une entrée à l'aide de son index: +fruits[0] # => "apple" + +# Les objets sont une collection non ordonnées de paires clé/valeur, et +# d'autres choses (que nous verrons plus tard). +person = + name: "Christina" + likes: + * "kittens" + * "and other cute stuff" + +# A nouveau, vous pouvez utiliser une expression plus consise à l'aide des +# accolades: +person = {name: "Christina", likes: ["kittens", "and other cute stuff"]} + +# Vous pouvez récupérer une entrée via sa clé: +person.name # => "Christina" +person["name"] # => "Christina" + + +# Les expressions régulières utilisent la même syntaxe que JavaScript: +trailing-space = /\s$/ # les mots-composés deviennent motscomposés + +# A l'exception que vous pouvez pouvez utiliser des expressions sur plusieurs +# lignes! +# (les commentaires et les espaces seront ignorés) +funRE = // + function\s+(.+) # nom + \s* \((.*)\) \s* # arguments + { (.*) } # corps + // + + +######################################################################## +## 2. Les opérations basiques +######################################################################## + +# Les opérateurs arithmétiques sont les mêmes que pour JavaScript: +1 + 2 # => 3 +2 - 1 # => 1 +2 * 3 # => 6 +4 / 2 # => 2 +3 % 2 # => 1 + + +# Les comparaisons sont presque identiques, à l'exception que `==` équivaut au +# `===` de JS, là où le `==` de JS est `~=` en LiveScript, et `===` active la +# comparaison d'objets et de tableaux, ainsi que les comparaisons strictes +# (sans conversion de type) +2 == 2 # => true +2 == "2" # => false +2 ~= "2" # => true +2 === "2" # => false + +[1,2,3] == [1,2,3] # => false +[1,2,3] === [1,2,3] # => true + ++0 == -0 # => true ++0 === -0 # => false + +# Les opérateurs suivants sont également disponibles: <, <=, > et >= + +# Les valeurs logiques peuvent être combinéees grâce aux opérateurs logiques +# `or`, `and` et `not` +true and false # => false +false or true # => true +not false # => true + + +# Les collections ont également des opérateurs additionnels +[1, 2] ++ [3, 4] # => [1, 2, 3, 4] +'a' in <[ a b c ]> # => true +'name' of { name: 'Chris' } # => true + + +######################################################################## +## 3. Fonctions +######################################################################## + +# Puisque LiveScript est fonctionnel, vous vous attendez à une bonne prise en +# charge des fonctions. En LiveScript, il est encore plus évident que les +# fonctions sont de premier ordre: +add = (left, right) -> left + right +add 1, 2 # => 3 + +# Les fonctions qui ne prennent pas d'arguments peuvent être appelées avec un +# point d'exclamation! +two = -> 2 +two! + +# LiveScript utilise l'environnement de la fonction, comme JavaScript. +# A l'inverse de JavaScript, le `=` fonctionne comme un opérateur de +# déclaration, et il déclarera toujours la variable située à gauche (sauf si +# la variable a été déclarée dans l'environnement parent). + +# L'opérateur `:=` est disponible pour réutiliser un nom provenant de +# l'environnement parent. + + +# Vous pouvez extraire les arguments d'une fonction pour récupérer +# rapidement les valeurs qui vous intéressent dans une structure de données +# complexe: +tail = ([head, ...rest]) -> rest +tail [1, 2, 3] # => [2, 3] + +# Vous pouvez également transformer les arguments en utilisant les opérateurs +# binaires et unaires. Définir des arguments par défaut est aussi possible. +foo = (a = 1, b = 2) -> a + b +foo! # => 3 + +# You pouvez utiliser cela pour cloner un argument en particulier pour éviter +# les effets secondaires. Par exemple: +copy = (^^target, source) -> + for k,v of source => target[k] = v + target +a = { a: 1 } +copy a, { b: 2 } # => { a: 1, b: 2 } +a # => { a: 1 } + + +# Une fonction peut être curryfiée en utilisant une longue flèche à la place +# d'une courte: +add = (left, right) --> left + right +add1 = add 1 +add1 2 # => 3 + +# Les fonctions ont un argument `it` implicite si vous n'en déclarez pas: +identity = -> it +identity 1 # => 1 + +# Les opérateurs ne sont pas des fonctions en LiveScript, mais vous pouvez +# facilement les transformer en fonction: +divide-by-two = (/ 2) +[2, 4, 8, 16].map(divide-by-two).reduce (+) + +# Comme dans tout bon langage fonctionnel, vous pouvez créer des fonctions +# composées d'autres fonctions: +double-minus-one = (- 1) . (* 2) + +# En plus de la formule mathématique `f . g`, vous avez les opérateurs `>>` +# et `<<`, qui décrivent l'ordre d'application des fonctions composées. +double-minus-one = (* 2) >> (- 1) +double-minus-one = (- 1) << (* 2) + + +# Pour appliquer une valeur à une fonction, vous pouvez utiliser les opérateurs +# `|>` et `<|`: +map = (f, xs) --> xs.map f +[1 2 3] |> map (* 2) # => [2 4 6] + +# La version sans pipe correspond à: +((map (* 2)) [1, 2, 3]) + +# You pouvez aussi choisir où vous voulez que la valeur soit placée, en +# marquant la position avec un tiret bas (_): +reduce = (f, xs, initial) --> xs.reduce f, initial +[1 2 3] |> reduce (+), _, 0 # => 6 + + +# Le tiret bas est également utilisé pour l'application partielle, +# que vous pouvez utiliser pour toute fonction: +div = (left, right) -> left / right +div-by-two = div _, 2 +div-by-two 4 # => 2 + + +# Pour conclure, LiveScript vous permet d'utiliser les fonctions de rappel. +# (mais vous devriez essayer des approches plus fonctionnelles, comme +# Promises). +# Un fonction de rappel est une fonction qui est passée en argument à une autre +# fonction: +readFile = (name, f) -> f name +a <- readFile 'foo' +b <- readFile 'bar' +console.log a + b + +# Equivalent à: +readFile 'foo', (a) -> readFile 'bar', (b) -> console.log a + b + + +######################################################################## +## 4. Conditionnalités +######################################################################## + +# Vous pouvez faire de la conditionnalité à l'aide de l'expression `if...else`: +x = if n > 0 then \positive else \negative + +# A la place de `then`, vous pouvez utiliser `=>` +x = if n > 0 => \positive + else \negative + +# Pour les conditions complexes, il vaut mieux utiliser l'expresssion `switch`: +y = {} +x = switch + | (typeof y) is \number => \number + | (typeof y) is \string => \string + | 'length' of y => \array + | otherwise => \object # `otherwise` et `_` correspondent. + +# Le corps des fonctions, les déclarations et les assignements disposent d'un +# `switch` implicite, donc vous n'avez pas besoin de le réécrire: +take = (n, [x, ...xs]) --> + | n == 0 => [] + | _ => [x] ++ take (n - 1), xs + + +######################################################################## +## 5. Compréhensions +######################################################################## + +# Comme en python, vous allez pouvoir utiliser les listes en compréhension, +# ce qui permet de générer rapidement et de manière élégante une liste de +# valeurs: +oneToTwenty = [1 to 20] +evens = [x for x in oneToTwenty when x % 2 == 0] + +# `when` et `unless` peuvent être utilisés comme des filtres. + +# Cette technique fonctionne sur les objets de la même manière. Vous allez +# pouvoir générer l'ensemble de paires clé/valeur via la syntaxe suivante: +copy = { [k, v] for k, v of source } + + +######################################################################## +## 4. Programmation orientée objet +######################################################################## + +# Bien que LiveScript soit un langage fonctionnel, il dispose d'intéressants +# outils pour la programmation objet. La syntaxe de déclaration d'une classe +# est héritée de CoffeeScript: +class Animal + (@name, kind) -> + @kind = kind + action: (what) -> "*#{@name} (a #{@kind}) #{what}*" + +class Cat extends Animal + (@name) -> super @name, 'cat' + purr: -> @action 'purrs' + +kitten = new Cat 'Mei' +kitten.purr! # => "*Mei (a cat) purrs*" + +# En plus de l'héritage classique, vous pouvez utiliser autant de mixins +# que vous voulez pour votre classe. Les mixins sont juste des objets: +Huggable = + hug: -> @action 'is hugged' + +class SnugglyCat extends Cat implements Huggable + +kitten = new SnugglyCat 'Purr' +kitten.hug! # => "*Mei (a cat) is hugged*" +``` + +## Lectures complémentaires + +Il y a beaucoup plus de choses à dire sur LiveScript, mais ce guide devrait +suffire pour démarrer l'écriture de petites fonctionnalités. +Le [site officiel](http://livescript.net/) dispose de beaucoup d'information, +ainsi que d'un compilateur en ligne vous permettant de tester le langage! + +Jetez également un coup d'oeil à [prelude.ls](http://gkz.github.io/prelude-ls/), +et consultez le channel `#livescript` sur le réseau Freenode. diff --git a/fr-fr/markdown.html.markdown b/fr-fr/markdown.html.markdown index 29c0d65d..e5e7c73a 100644 --- a/fr-fr/markdown.html.markdown +++ b/fr-fr/markdown.html.markdown @@ -177,7 +177,7 @@ des syntaxes spécifiques --> \`\`\`ruby <!-- mais enlevez les backslashes quand vous faites ça, -gardez juste ```ruby ( ou nom de la synatxe correspondant à votre code )--> +gardez juste ```ruby ( ou nom de la syntaxe correspondant à votre code )--> def foobar puts "Hello world!" end diff --git a/fr-fr/python-fr.html.markdown b/fr-fr/python-fr.html.markdown index 58a036ba..5a03ecfc 100644 --- a/fr-fr/python-fr.html.markdown +++ b/fr-fr/python-fr.html.markdown @@ -15,7 +15,7 @@ Je suis tombé amoureux de Python de par la clarté de sa syntaxe. C'est pratiqu Vos retours sont grandement appréciés. Vous pouvez me contacter sur Twitter [@louiedinh](http://twitter.com/louiedinh) ou par e-mail: louiedinh [at] [google's email service] NB: Cet artice s'applique spécifiquement à Python 2.7, mais devrait s'appliquer pour toute version Python 2.x -Vous pourrez bientôt trouver un article pour Python 3! +Vous pourrez bientôt trouver un article pour Python 3 en Français. Pour le moment vous pouvez jettez un coup d'oeil à l'article [Python 3 en Anglais](http://learnxinyminutes.com/docs/python3/). ```python # Une ligne simple de commentaire commence par un dièse diff --git a/git.html.markdown b/git.html.markdown index 4bbc58e7..bf8fce0c 100644 --- a/git.html.markdown +++ b/git.html.markdown @@ -462,6 +462,8 @@ $ git rm /pather/to/the/file/HelloWorld.c * [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1) +* [Udemy Git Tutorial: A Comprehensive Guide](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/) + * [git-scm - Video Tutorials](http://git-scm.com/videos) * [git-scm - Documentation](http://git-scm.com/docs) diff --git a/id-id/json-id.html.markdown b/id-id/json-id.html.markdown new file mode 100644 index 00000000..52e61449 --- /dev/null +++ b/id-id/json-id.html.markdown @@ -0,0 +1,60 @@ +--- +language: json +filename: learnjson.json +contributors: + - ["Anna Harren", "https://github.com/iirelu"] + - ["Marco Scannadinari", "https://github.com/marcoms"] +translators + - ["Rizky Luthfianto", "https://github.com/rilut"] +--- + +JSON adalah format pertukaran data yang sangat simpel, kemungkinan besar, +ini adalah "Learn X in Y Minutes" yang paling singkat. + +Murninya, JSON tidak mempunyai fitur komentar, tapi kebanyakan parser akan +menerima komentar bergaya bahasa C (`//`, `/* */`). Namun, pada halaman ini, +hanya dicontohkan JSON yang 100% valid. + +```json +{ + "kunci": "nilai", + + "kunci": "harus selalu diapit tanda kutip", + "angka": 0, + "strings": "Halø, dunia. Semua karaktor unicode diperbolehkan, terumasuk \"escaping\".", + "punya tipe data boolean?": true, + "nilai kosong": null, + + "angka besar": 1.2e+100, + + "obyek": { + "komentar": "Most of your structure will come from objects.", + + "array": [0, 1, 2, 3, "Array bisa berisi apapun.", 5], + + "obyek lainnya": { + "komentar": "Obyek-obyek JSON dapat dibuat bersarang, sangat berguna." + } + }, + + "iseng-iseng": [ + { + "sumber potassium": ["pisang"] + }, + [ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, "neo"], + [0, 0, 0, 1] + ] + ], + + "gaya alternatif": { + "komentar": "lihat ini!" + , "posisi tanda koma": "tak masalah. selama sebelum nilai berikutnya, valid-valid saja" + , "komentar lainnya": "betapa asyiknya" + }, + + "singkat": "Dan Anda selesai! Sekarang Anda tahu apa saja yang disediakan oleh JSON." +} +``` diff --git a/id-id/xml-id.html.markdown b/id-id/xml-id.html.markdown new file mode 100644 index 00000000..8e8cdf4e --- /dev/null +++ b/id-id/xml-id.html.markdown @@ -0,0 +1,129 @@ +--- +language: xml +filename: learnxml.xml +contributors: + - ["João Farias", "https://github.com/JoaoGFarias"] +translators: + - ["Rizky Luthfianto", "https://github.com/rilut"] +--- + +XML adalah bahasa markup yang dirancang untuk menyimpan dan mengirim data. + +Tidak seperti HTML, XML tidak menentukan bagaimana menampilkan atau format data, hanya membawanya. + +* Sintaks XML + +```xml +<!-- Komentar di XML seperti ini --> + +<?xml version="1.0" encoding="UTF-8"?> +<tokobuku> + <buku category="MEMASAK"> + <judul lang="en">Everyday Italian</judul> + <pengarang>Giada De Laurentiis</pengarang> + <tahun>2005</tahun> + <harga>30.00</harga> + </buku> + <buku category="ANAK"> + <judul lang="en">Harry Potter</judul> + <pengarang>J K. Rowling</pengarang> + <tahun>2005</tahun> + <harga>29.99</harga> + </buku> + <buku category="WEB"> + <judul lang="en">Learning XML</judul> + <pengarang>Erik T. Ray</pengarang> + <tahun>2003</tahun> + <harga>39.95</harga> + </buku> +</tokobuku> + +<!-- Di atas adalah contoh file XML biasa. + Dimulai dengan deklarasi, menginformasikan beberapa metadata (opsional). + + XML menggunakan struktur pohon. Di atas, simpul akar adalah 'tokobuku', + yang memiliki tiga node anak, para 'buku'. Node-node tersebut dapat memiliki + node-node anak, dan seterusnya ... + + Node dibuat menggunakan tag buka/tutup, dan node-node anak hanya + berada di antara tag buka dan tutup .--> + + +<!-- XML membawa dua jenis data: + 1 - Atribut -> Itu metadata tentang sebuah node. + Biasanya, parser XML menggunakan informasi ini untuk menyimpan data dengan + benar. Hal ini ditandai dengan muncul dengan format nama = "nilai" dalam pembukaan tag. + 2 - Elemen -> Itu data yang murni. + Itulah yang parser akan mengambil dari file XML. + Elemen muncul antara tag membuka dan menutup.--> + + +<!-- Di bawah ini, unsur dengan dua atribut--> +<file type="gif" id="4293">komputer.gif</file> + + +``` + +* Dokumen yang well-formated & Validasi + +Sebuah dokumen XML disebut well-formated jika sintaksisnya benar. +Namun, juga mungkin untuk mendefinisikan lebih banyak batasan dalam dokumen, +menggunakan definisi dokumen, seperti DTD dan XML Schema. + +Sebuah dokumen XML yang mengikuti definisi dokumen disebut valid, +jika sesuai dokumen itu. + +Dengan alat ini, Anda dapat memeriksa data XML di luar logika aplikasi. + +```xml + +<!-- Di bawah, Anda dapat melihat versi sederhana dari dokumen tokobuku, + dengan penambahan definisi DTD .--> + +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE catatan SYSTEM "tokobuku.dtd"> +<tokobuku> + <buku category="MEMASAK"> + <judul >Everyday Italian</judul> + <harga>30.00</harga> + </buku> +</tokobuku> + +<!-- This DTD could be something like:--> + +<!DOCTYPE catatan +[ +<!ELEMENT tokobuku (buku+)> +<!ELEMENT buku (judul,harga)> +<!ATTLIST buku category CDATA "Sastra"> +<!ELEMENT judul (#PCDATA)> +<!ELEMENT harga (#PCDATA)> +]> + + +<!-- DTD dimulai dengan deklarasi. + Berikut, node akar dinyatakan, membutuhkan 1 atau lebih anak node 'buku'. + Setiap 'buku' harus berisi tepat satu 'judul' dan 'harga' dan atribut + disebut 'kategori', dengan "Sastra" sebagai nilai default. + Node yang 'judul' dan 'harga' mengandung karakter data diurai .--> + +<!-- DTD dapat dideklarasikan di dalam file XML itu sendiri .--> + +<?xml version="1.0" encoding="UTF-8"?> + +<!DOCTYPE catatan +[ +<!ELEMENT tokobuku (buku+)> +<!ELEMENT buku (judul,harga)> +<!ATTLIST buku category CDATA "Sastra"> +<!ELEMENT judul (#PCDATA)> +<!ELEMENT harga (#PCDATA)> +]> + +<tokobuku> + <buku category="MEMASAK"> + <judul >Everyday Italian</judul> + <harga>30.00</harga> + </buku> +</tokobuku> +``` diff --git a/it-it/coffeescript-it.html.markdown b/it-it/coffeescript-it.html.markdown new file mode 100644 index 00000000..16eb9bd4 --- /dev/null +++ b/it-it/coffeescript-it.html.markdown @@ -0,0 +1,107 @@ +--- +language: coffeescript +contributors: + - ["Luca 'Kino' Maroni", "http://github.com/kino90"] + - ["Tenor Biel", "http://github.com/L8D"] + - ["Xavier Yao", "http://github.com/xavieryao"] +filename: coffeescript-it.coffee +lang: it-it +--- + +CoffeeScript è un piccolo linguaggio che compila direttamente nell'equivalente +JavaScript, non c'è nessuna interpretazione a runtime. Come possibile +successore di Javascript, CoffeeScript fa il suo meglio per restituire +un codice leggibile, ben stampato e performante in ogni ambiente JavaScript. + +Guarda anche [il sito di CoffeeScript](http://coffeescript.org/), che ha una +guida completa a CoffeeScript. + +```coffeescript +# CoffeeScript è un linguaggio hipster. +# Segue le mode di alcuni linguaggi moderni. +# Quindi i commenti sono come quelli di Ruby e Python, usano il cancelletto. + +### +I blocchi di commenti sono definiti con tre cancelletti, che vengono tradotti +direttamente in `/*` e `*/` nel codice JavaScript risultante. + +Prima di continuare devi conoscere la maggior parte +delle semantiche JavaScript. +### + +# Assegnamento: +numero = 42 #=> var numero = 42; +contrario = true #=> var contrario = true; + +# Condizioni: +numero = -42 if contrario #=> if(contrario) { numero = -42; } + +# Funzioni: +quadrato = (x) -> x * x #=> var quadrato = function(x) { return x * x; } + +riempi = (contenitore, liquido = "caffè") -> + "Sto riempiendo #{contenitore} con #{liquido}..." +#=>var riempi; +# +#riempi = function(contenitore, liquido) { +# if (liquido == null) { +# liquido = "caffè"; +# } +# return "Sto riempiendo " + contenitore + " con " + liquido + "..."; +#}; + +# Intervalli: +lista = [1..5] #=> var lista = [1, 2, 3, 4, 5]; + +# Oggetti: +matematica = + radice: Math.sqrt + quadrato: quadrato + cubo: (x) -> x * quadrato x +#=> var matematica = { +# "radice": Math.sqrt, +# "quadrato": quadrato, +# "cubo": function(x) { return x * quadrato(x); } +#} + +# Splats: +gara = (vincitore, partecipanti...) -> + print vincitore, partecipanti +#=>gara = function() { +# var partecipanti, vincitore; +# vincitore = arguments[0], partecipanti = 2 <= arguments.length ? __slice.call(arguments, 1) : []; +# return print(vincitore, partecipanti); +#}; + +# Esistenza: +alert "Lo sapevo!" if elvis? +#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Lo sapevo!"); } + +# Comprensione degli Array: +cubi = (matematica.cubo num for num in lista) +#=>cubi = (function() { +# var _i, _len, _results; +# _results = []; +# for (_i = 0, _len = lista.length; _i < _len; _i++) { +# num = lista[_i]; +# _results.push(matematica.cubo(num)); +# } +# return _results; +# })(); + +cibi = ['broccoli', 'spinaci', 'cioccolato'] +mangia cibo for cibo in cibi when cibo isnt 'cioccolato' +#=>cibi = ['broccoli', 'spinaci', 'cioccolato']; +# +#for (_k = 0, _len2 = cibi.length; _k < _len2; _k++) { +# cibo = cibi[_k]; +# if (cibo !== 'cioccolato') { +# mangia(cibo); +# } +#} +``` + +## Altre risorse + +- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/) +- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read) diff --git a/it-it/elixir-it.html.markdown b/it-it/elixir-it.html.markdown new file mode 100644 index 00000000..f5d0c172 --- /dev/null +++ b/it-it/elixir-it.html.markdown @@ -0,0 +1,418 @@ +--- +language: elixir +contributors: + - ["Luca 'Kino' Maroni", "https://github.com/kino90"] + - ["Joao Marques", "http://github.com/mrshankly"] + - ["Dzianis Dashkevich", "https://github.com/dskecse"] +filename: learnelixir-it.ex +lang: it-it +--- + +Elixir è un linguaggio funzionale moderno, costruito sulla VM Erlang. +È totalmente compatibile con Erlang, ma con una sintassi più standard +e molte altre funzionalità. + +```elixir + +# I commenti su una riga iniziano con un cancelletto. + +# Non esistono commenti multilinea, +# ma puoi concatenare più commenti. + +# Per usare la shell di elixir usa il comando `iex`. +# Compila i tuoi moduli con il comando `elixirc`. + +# Entrambi i comandi dovrebbero già essere nel tuo PATH se hai installato +# elixir correttamente. + +## --------------------------- +## -- Tipi di base +## --------------------------- + +# Numeri +3 # intero (Integer) +0x1F # intero +3.0 # decimale (Float) + +# Atomi, che sono literals, una costante con un nome. Iniziano con `:`. +:ciao # atomo (Atom) + +# Tuple che sono salvate in celle di memoria contigue. +{1,2,3} # tupla (Tuple) + +# Possiamo accedere ad un elemento di una tupla con la funzione `elem`: +elem({1, 2, 3}, 0) #=> 1 + +# Liste, che sono implementate come liste concatenate (o linked list). +[1,2,3] # lista (List) + +# Possiamo accedere alla testa (head) e alla coda (tail) delle liste così: +[testa | coda] = [1,2,3] +testa #=> 1 +coda #=> [2,3] + +# In Elixir, proprio come in Erlang, il simbolo `=` denota pattern matching e +# non un assegnamento. +# +# Questo significa che la parte sinistra (pattern) viene confrontata alla +# parte destra. +# +# Questo spiega il funzionamento dell'esempio dell'accesso alla lista di prima. + +# Un pattern match darà errore quando le parti non combaciano, ad esempio se +# le tuple hanno dimensione differente. +# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2} + +# Ci sono anche i binari +<<1,2,3>> # binari (Binary) + +# Stringhe e liste di caratteri +"ciao" # stringa (String) +'ciao' # lista di caratteri (List) + +# Stringhe multilinea +""" +Sono una stringa +multi-linea. +""" +#=> "Sono una stringa\nmulti-linea.\n" + +# Le stringhe sono tutte codificate in UTF-8: +"cìaò" +#=> "cìaò" + +# le stringhe in realtà sono dei binari, e le liste di caratteri sono liste. +<<?a, ?b, ?c>> #=> "abc" +[?a, ?b, ?c] #=> 'abc' + +# `?a` in elixir restituisce il valore ASCII della lettera `a` +?a #=> 97 + +# Per concatenare liste si usa `++`, per binari si usa `<>` +[1,2,3] ++ [4,5] #=> [1,2,3,4,5] +'ciao ' ++ 'mondo' #=> 'ciao mondo' + +<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>> +"ciao " <> "mondo" #=> "ciao mondo" + +# Gli intervalli sono rappresentati come `inizio..fine` (estremi inclusi) +1..10 #=> 1..10 (Range) +minore..maggiore = 1..10 # Puoi fare pattern matching anche sugli intervalli +[minore, maggiore] #=> [1, 10] + +## --------------------------- +## -- Operatori +## --------------------------- + +# Un po' di matematica +1 + 1 #=> 2 +10 - 5 #=> 5 +5 * 2 #=> 10 +10 / 2 #=> 5.0 + +# In elixir l'operatore `/` restituisce sempre un decimale. + +# Per fare una divisione intera si usa `div` +div(10, 2) #=> 5 + +# Per ottenere il resto di una divisione si usa `rem` +rem(10, 3) #=> 1 + +# Ci sono anche gli operatori booleani: `or`, `and` e `not`. +# Questi operatori si aspettano un booleano come primo argomento. +true and true #=> true +false or true #=> true +# 1 and true #=> ** (ArgumentError) argument error + +# Elixir fornisce anche `||`, `&&` e `!` che accettano argomenti +# di qualsiasi tipo. +# Tutti i valori tranne `false` e `nil` saranno valutati come true. +1 || true #=> 1 +false && 1 #=> false +nil && 20 #=> nil +!true #=> false + +# Per i confronti abbiamo: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` e `>` +1 == 1 #=> true +1 != 1 #=> false +1 < 2 #=> true + +# `===` e `!==` sono più rigidi quando si confrontano interi e decimali: +1 == 1.0 #=> true +1 === 1.0 #=> false + +# Possiamo anche confrontare tipi di dato diversi: +1 < :ciao #=> true + +# L'ordine generale è definito sotto: +# numeri < atomi < riferimenti < funzioni < porte < pid < tuple < liste +# < stringhe di bit + +# Per citare Joe Armstrong su questo: "L'ordine non è importante, +# ma è importante che sia definito un ordine." + +## --------------------------- +## -- Controllo di flusso +## --------------------------- + +# espressione `se` (`if`) +if false do + "Questo non si vedrà mai" +else + "Questo sì" +end + +# c'è anche un `se non` (`unless`) +unless true do + "Questo non si vedrà mai" +else + "Questo sì" +end + +# Ti ricordi il pattern matching? +# Moltre strutture di controllo di flusso in elixir si basano su di esso. + +# `case` ci permette di confrontare un valore a diversi pattern: +case {:uno, :due} do + {:quattro, :cinque} -> + "Questo non farà match" + {:uno, x} -> + "Questo farà match e binderà `x` a `:due`" + _ -> + "Questo farà match con qualsiasi valore" +end + +# Solitamente si usa `_` se non si ha bisogno di utilizzare un valore. +# Ad esempio, se ci serve solo la testa di una lista: +[testa | _] = [1,2,3] +testa #=> 1 + +# Per aumentare la leggibilità possiamo usarlo in questo modo: +[testa | _coda] = [:a, :b, :c] +testa #=> :a + +# `cond` ci permette di verificare più condizioni allo stesso momento. +# Usa `cond` invece di innestare più espressioni `if`. +cond do + 1 + 1 == 3 -> + "Questa stringa non si vedrà mai" + 2 * 5 == 12 -> + "Nemmeno questa" + 1 + 2 == 3 -> + "Questa sì!" +end + +# È pratica comune mettere l'ultima condizione a `true`, che farà sempre match +cond do + 1 + 1 == 3 -> + "Questa stringa non si vedrà mai" + 2 * 5 == 12 -> + "Nemmeno questa" + true -> + "Questa sì! (essenzialmente funziona come un else)" +end + +# `try/catch` si usa per gestire i valori lanciati (throw), +# Supporta anche una clausola `after` che è invocata in ogni caso. +try do + throw(:ciao) +catch + message -> "Ho ricevuto #{message}." +after + IO.puts("Io sono la clausola 'after'.") +end +#=> Io sono la clausola 'after' +# "Ho ricevuto :ciao" + +## --------------------------- +## -- Moduli e Funzioni +## --------------------------- + +# Funzioni anonime (notare il punto) +quadrato = fn(x) -> x * x end +quadrato.(5) #=> 25 + +# Accettano anche guardie e condizioni multiple. +# le guardie ti permettono di perfezionare il tuo pattern matching, +# sono indicate dalla parola chiave `when`: +f = fn + x, y when x > 0 -> x + y + x, y -> x * y +end + +f.(1, 3) #=> 4 +f.(-1, 3) #=> -3 + +# Elixir fornisce anche molte funzioni, disponibili nello scope corrente. +is_number(10) #=> true +is_list("ciao") #=> false +elem({1,2,3}, 0) #=> 1 + +# Puoi raggruppare delle funzioni all'interno di un modulo. +# All'interno di un modulo usa `def` per definire le tue funzioni. +defmodule Matematica do + def somma(a, b) do + a + b + end + + def quadrato(x) do + x * x + end +end + +Matematica.somma(1, 2) #=> 3 +Matematica.quadrato(3) #=> 9 + +# Per compilare il modulo 'Matematica' salvalo come `matematica.ex` e usa +# `elixirc`. +# nel tuo terminale: elixirc matematica.ex + +# All'interno di un modulo possiamo definire le funzioni con `def` e funzioni +# private con `defp`. +# Una funzione definita con `def` è disponibile per essere invocata anche da +# altri moduli, una funziona privata può essere invocata solo localmente. +defmodule MatematicaPrivata do + def somma(a, b) do + esegui_somma(a, b) + end + + defp esegui_somma(a, b) do + a + b + end +end + +MatematicaPrivata.somma(1, 2) #=> 3 +# MatematicaPrivata.esegui_somma(1, 2) #=> ** (UndefinedFunctionError) + +# Anche le dichiarazioni di funzione supportano guardie e condizioni multiple: +defmodule Geometria do + def area({:rettangolo, w, h}) do + w * h + end + + def area({:cerchio, r}) when is_number(r) do + 3.14 * r * r + end +end + +Geometria.area({:rettangolo, 2, 3}) #=> 6 +Geometria.area({:cerchio, 3}) #=> 28.25999999999999801048 +# Geometria.area({:cerchio, "non_un_numero"}) +#=> ** (FunctionClauseError) no function clause matching in Geometria.area/1 + +# A causa dell'immutabilità dei dati, la ricorsione è molto frequente in elixir +defmodule Ricorsione do + def somma_lista([testa | coda], accumulatore) do + somma_lista(coda, accumulatore + testa) + end + + def somma_lista([], accumulatore) do + accumulatore + end +end + +Ricorsione.somma_lista([1,2,3], 0) #=> 6 + +# I moduli di Elixir supportano attributi. Ci sono degli attributi incorporati +# e puoi anche aggiungerne di personalizzati. +defmodule Modulo do + @moduledoc """ + Questo è un attributo incorporato in un modulo di esempio. + """ + + @miei_dati 100 # Questo è un attributo personalizzato . + IO.inspect(@miei_dati) #=> 100 +end + +## --------------------------- +## -- Strutture ed Eccezioni +## --------------------------- + + +# Le Strutture (Structs) sono estensioni alle mappe che portano +# valori di default, garanzia alla compilazione e polimorfismo in Elixir. +defmodule Persona do + defstruct nome: nil, eta: 0, altezza: 0 +end + +luca = %Persona{ nome: "Luca", eta: 24, altezza: 185 } +#=> %Persona{eta: 24, altezza: 185, nome: "Luca"} + +# Legge al valore di 'nome' +luca.nome #=> "Luca" + +# Modifica il valore di eta +luca_invecchiato = %{ luca | eta: 25 } +#=> %Persona{eta: 25, altezza: 185, nome: "Luca"} + +# Il blocco `try` con la parola chiave `rescue` è usato per gestire le eccezioni +try do + raise "un errore" +rescue + RuntimeError -> "Salvato un errore di Runtime" + _error -> "Questo salverà da qualsiasi errore" +end + +# Tutte le eccezioni hanno un messaggio +try do + raise "un errore" +rescue + x in [RuntimeError] -> + x.message +end + +## --------------------------- +## -- Concorrenza +## --------------------------- + +# Elixir si basa sul modello degli attori per la concorrenza. +# Tutto ciò di cui abbiamo bisogno per scrivere programmi concorrenti in elixir +# sono tre primitive: creare processi, inviare messaggi e ricevere messaggi. + +# Per creare un nuovo processo si usa la funzione `spawn`, che riceve una +# funzione come argomento. +f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245> +spawn(f) #=> #PID<0.40.0> + +# `spawn` restituisce un pid (identificatore di processo). Puoi usare questo +# pid per inviare messaggi al processo. +# Per passare messaggi si usa l'operatore `send`. +# Perché tutto questo sia utile dobbiamo essere capaci di ricevere messaggi, +# oltre ad inviarli. Questo è realizzabile con `receive`: +defmodule Geometria do + def calcolo_area do + receive do + {:rettangolo, w, h} -> + IO.puts("Area = #{w * h}") + calcolo_area() + {:cerchio, r} -> + IO.puts("Area = #{3.14 * r * r}") + calcolo_area() + end + end +end + +# Compila il modulo e crea un processo che esegue `calcolo_area` nella shell +pid = spawn(fn -> Geometria.calcolo_area() end) #=> #PID<0.40.0> + +# Invia un messaggio a `pid` che farà match su un pattern nel blocco in receive +send pid, {:rettangolo, 2, 3} +#=> Area = 6 +# {:rettangolo,2,3} + +send pid, {:cerchio, 2} +#=> Area = 12.56000000000000049738 +# {:cerchio,2} + +# Anche la shell è un processo. Puoi usare `self` per ottenere il pid corrente +self() #=> #PID<0.27.0> +``` + +## Referenze + +* [Getting started guide](http://elixir-lang.org/getting_started/1.html) dalla [pagina web ufficiale di elixir](http://elixir-lang.org) +* [Documentazione Elixir](http://elixir-lang.org/docs/master/) +* ["Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) di Dave Thomas +* [Elixir Cheat Sheet](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf) +* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) di Fred Hebert +* ["Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) di Joe Armstrong diff --git a/javascript.html.markdown b/javascript.html.markdown index 588ea86d..ba2e8ce4 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -475,9 +475,6 @@ myNumber === myNumberObj; // = false if (0){ // This code won't execute, because 0 is falsy. } -if (Number(0)){ - // This code *will* execute, because Number(0) is truthy. -} // However, the wrapper objects and the regular builtins share a prototype, so // you can actually add functionality to a string, for instance. diff --git a/make.html.markdown b/make.html.markdown new file mode 100644 index 00000000..75543dcd --- /dev/null +++ b/make.html.markdown @@ -0,0 +1,243 @@ +---
+language: make
+contributors:
+ - ["Robert Steed", "https://github.com/robochat"]
+filename: Makefile
+---
+
+A Makefile defines a graph of rules for creating a target (or targets).
+Its purpose is to do the minimum amount of work needed to update a
+target to the most recent version of the source. Famously written over a
+weekend by Stuart Feldman in 1976, it is still widely used (particularly
+on Unix) despite many competitors and criticisms.
+
+There are many varieties of make in existance, this article assumes that
+we are using GNU make which is the standard on Linux.
+
+```make
+
+# Comments can be written like this.
+
+# Files should be named Makefile and then be can run as `make <target>`.
+# Otherwise we use `make -f "filename" <target>`.
+
+# Warning - only use TABS to indent in Makefiles, never spaces!
+
+#-----------------------------------------------------------------------
+# Basics
+#-----------------------------------------------------------------------
+
+# A rule - this rule will only run if file0.txt doesn't exist.
+file0.txt:
+ echo "foo" > file0.txt
+ # Even comments in these 'recipe' sections get passed to the shell.
+ # Try `make file0.txt` or simply `make` - first rule is the default.
+
+
+# This rule will only run if file0.txt is newer than file1.txt.
+file1.txt: file0.txt
+ cat file0.txt > file1.txt
+ # use the same quoting rules as in the shell.
+ @cat file0.txt >> file1.txt
+ # @ stops the command from being echoed to stdout.
+ -@echo 'hello'
+ # - means that make will keep going in the case of an error.
+ # Try `make file1.txt` on the commandline.
+
+# A rule can have multiple targets and multiple prerequisites
+file2.txt file3.txt: file0.txt file1.txt
+ touch file2.txt
+ touch file3.txt
+
+# Make will complain about multiple recipes for the same rule. Empty
+# recipes don't count though and can be used to add new dependencies.
+
+#-----------------------------------------------------------------------
+# Phony Targets
+#-----------------------------------------------------------------------
+
+# A phony target. Any target that isn't a file.
+# It will never be up to date so make will always try to run it.
+all: maker process
+
+# We can declare things out of order.
+maker:
+ touch ex0.txt ex1.txt
+
+# Can avoid phony rules breaking when a real file has the same name by
+.PHONY: all maker process
+# This is a special target. There are several others.
+
+# A rule with a dependency on a phony target will always run
+ex0.txt ex1.txt: maker
+
+# Common phony targets are: all make clean install ...
+
+#-----------------------------------------------------------------------
+# Automatic Variables & Wildcards
+#-----------------------------------------------------------------------
+
+process: file*.txt #using a wildcard to match filenames
+ @echo $^ # $^ is a variable containing the list of prerequisites
+ @echo $@ # prints the target name
+ #(for multiple target rules, $@ is whichever caused the rule to run)
+ @echo $< # the first prerequisite listed
+ @echo $? # only the dependencies that are out of date
+ @echo $+ # all dependencies including duplicates (unlike normal)
+ #@echo $| # all of the 'order only' prerequisites
+
+# Even if we split up the rule dependency definitions, $^ will find them
+process: ex1.txt file0.txt
+# ex1.txt will be found but file0.txt will be deduplicated.
+
+#-----------------------------------------------------------------------
+# Patterns
+#-----------------------------------------------------------------------
+
+# Can teach make how to convert certain files into other files.
+
+%.png: %.svg
+ inkscape --export-png $^
+
+# Pattern rules will only do anything if make decides to create the \
+target.
+
+# Directory paths are normally ignored when matching pattern rules. But
+# make will try to use the most appropriate rule available.
+small/%.png: %.svg
+ inkscape --export-png --export-dpi 30 $^
+
+# make will use the last version for a pattern rule that it finds.
+%.png: %.svg
+ @echo this rule is chosen
+
+# However make will use the first pattern rule that can make the target
+%.png: %.ps
+ @echo this rule is not chosen if *.svg and *.ps are both present
+
+# make already has some pattern rules built-in. For instance, it knows
+# how to turn *.c files into *.o files.
+
+# Older makefiles might use suffix rules instead of pattern rules
+.png.ps:
+ @echo this rule is similar to a pattern rule.
+
+# Tell make about the suffix rule
+.SUFFIXES: .png
+
+#-----------------------------------------------------------------------
+# Variables
+#-----------------------------------------------------------------------
+# aka. macros
+
+# Variables are basically all string types
+
+name = Ted
+name2="Sarah"
+
+echo:
+ @echo $(name)
+ @echo ${name2}
+ @echo $name # This won't work, treated as $(n)ame.
+ @echo $(name3) # Unknown variables are treated as empty strings.
+
+# There are 4 places to set variables.
+# In order of priority from highest to lowest:
+# 1: commandline arguments
+# 2: Makefile
+# 3: shell enviroment variables - make imports these automatically.
+# 4: make has some predefined variables
+
+name4 ?= Jean
+# Only set the variable if enviroment variable is not already defined.
+
+override name5 = David
+# Stops commandline arguments from changing this variable.
+
+name4 +=grey
+# Append values to variable (includes a space).
+
+# Pattern-specific variable values (GNU extension).
+echo: name2 = Sara # True within the matching rule
+ # and also within its remade recursive dependencies
+ # (except it can break when your graph gets too complicated!)
+
+# Some variables defined automatically by make.
+echo_inbuilt:
+ echo $(CC)
+ echo ${CXX)}
+ echo $(FC)
+ echo ${CFLAGS)}
+ echo $(CPPFLAGS)
+ echo ${CXXFLAGS}
+ echo $(LDFLAGS)
+ echo ${LDLIBS}
+
+#-----------------------------------------------------------------------
+# Variables 2
+#-----------------------------------------------------------------------
+
+# The first type of variables are evaluated each time they are used.
+# This can be expensive, so a second type of variable exists which is
+# only evaluated once. (This is a GNU make extension)
+
+var := hello
+var2 ::= $(var) hello
+#:= and ::= are equivalent.
+
+# These variables are evaluated procedurely (in the order that they
+# appear), thus breaking with the rest of the language !
+
+# This doesn't work
+var3 ::= $(var4) and good luck
+var4 ::= good night
+
+#-----------------------------------------------------------------------
+# Functions
+#-----------------------------------------------------------------------
+
+# make has lots of functions available.
+
+sourcefiles = $(wildcard *.c */*.c)
+objectfiles = $(patsubst %.c,%.o,$(sourcefiles))
+
+# Format is $(func arg0,arg1,arg2...)
+
+# Some examples
+ls: * src/*
+ @echo $(filter %.txt, $^)
+ @echo $(notdir $^)
+ @echo $(join $(dir $^),$(notdir $^))
+
+#-----------------------------------------------------------------------
+# Directives
+#-----------------------------------------------------------------------
+
+# Include other makefiles, useful for platform specific code
+include foo.mk
+
+sport = tennis
+# Conditional compilation
+report:
+ifeq ($(sport),tennis)
+ @echo 'game, set, match'
+else
+ @echo "They think it's all over; it is now"
+endif
+
+# There are also ifneq, ifdef, ifndef
+
+foo = true
+
+ifdef $(foo)
+bar = 'hello'
+endif
+```
+
+
+### More Resources
+
++ [gnu make documentation](https://www.gnu.org/software/make/manual/)
++ [software carpentry tutorial](http://swcarpentry.github.io/make-novice/)
++ learn C the hard way [ex2](http://c.learncodethehardway.org/book/ex2.html) [ex28](http://c.learncodethehardway.org/book/ex28.html)
+
diff --git a/markdown.html.markdown b/markdown.html.markdown index 7541f904..6d19710f 100644 --- a/markdown.html.markdown +++ b/markdown.html.markdown @@ -232,6 +232,12 @@ can be anything so long as they are unique. --> I want to type *this text surrounded by asterisks* but I don't want it to be in italics, so I do this: \*this text surrounded by asterisks\*. +<!-- Keyboard keys --> +<!-- In Github Flavored Markdown, you can use a <kbd> tag to represent keyboard keys --> + +Your computer crashed? Try sending a +<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd> + <!-- Tables --> <!-- Tables are only available in Github Flavored Markdown and are slightly cumbersome, but if you really want it: --> diff --git a/no-nb/bash-no.html.markdown b/no-nb/bash-no.html.markdown new file mode 100644 index 00000000..ab0c064f --- /dev/null +++ b/no-nb/bash-no.html.markdown @@ -0,0 +1,270 @@ +--- +category: tool +tool: bash +contributors: + - ["Max Yankov", "https://github.com/golergka"] + - ["Darren Lin", "https://github.com/CogBear"] + - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] + - ["Denis Arh", "https://github.com/darh"] + - ["akirahirose", "https://twitter.com/akirahirose"] + - ["Anton Strömkvist", "http://lutic.org/"] + - ["Rahil Momin", "https://github.com/iamrahil"] + - ["Gregrory Kielian", "https://github.com/gskielian"] +filename: LearnBash-no.sh +translators: + - ["Andreas Lindahl Flåten", "https://github.com/anlif"] +lang: no-nb +--- +Bash er navnet på unix skallet, som også var distribuert som skallet for GNU +operativsystemet og som standard skall på de fleste Linux distribusjoner og +Mac OS X. + +[Les mer her.](http://www.gnu.org/software/bash/manual/bashref.html) + +```bash +#!/bin/bash +# Den første linjen i et bash skript starter med '#!' (shebang) +# etterfulgt av stien til bash http://en.wikipedia.org/wiki/Shebang_(Unix) +# Kommentarer starter med #. + +# Enkelt hello world eksempel: +echo Hello world! + +# Hver kommando starter på en ny linje, eller etter et semikolon: +echo 'Dette er den første linjen'; echo 'Dette er en andre linjen' + +# Deklarering av en variabel ser slik ut: +VARIABLE="En tekststreng" + +# Men ikke slik: +VARIABLE = "En tekststreng" +# Bash vil tolke dette som at VARIABLE er en kommando den skal kjøre +# og gi en feilmelding dersom kommandoen ikke finnes + +# Bruk av den nydeklarerte variabelen: +echo $VARIABLE +echo "$VARIABLE" +echo '$VARIABLE' +# Når du bruker variabelen, for eksempel setter verdien eller eksporterer den, +# skriver du navnet dens uten $. Hvis du vil bruke variabelens verdi, +# skriver du $ før variabelnavnet. + +# Strenginnhold i en variabel kan erstattes på følgende måte: +echo ${VARIABLE/tull/ball} +# Dette vil erstatte første forekomst av 'tull' med 'ball' + +# Substreng av en variabel: +echo ${VARIABLE:0:7} +# Dette vil returnere de første 7 tegnene i en strengvariabel + +# Å angi en standardverdi dersom en variabel er udeklarert gjøres slik: +echo ${FOO:-"StandardVerdiDersomFOOErTom"} +# Dette fungerer for null (FOO=), tom streng (FOO="") og tallet null (FOO=0) + +# Det finnes en rekke hendige innebygde variable, eksempel: +echo "Siste programs returnerte verdi: $?" +echo "Skript's PID: $$" +echo "Antall argumenter: $#" +echo "Alle argumenter til skriptet: $@" +echo "Argumenter til skriptet i egne variable: $1 $2..." + +# Lesing av input: +echo "Hva heter du?" +read NAME # variabelen NAME blir automatisk deklarert av 'read' kommandoen +echo Hei, $NAME! + +# if setninger ser slik ut: +# se 'man test' for mer informasjon om betingelser +if [ $NAME -ne $USER ] +then + echo "Your name isn't your username" +else + echo "Your name is your username" +fi + +# Det finnes også betinget eksekvering +echo "Kjøres alltid" || echo "Kjøres kun dersom første kommando feilet" +echo "Kjøres alltid" && echo "Kjøres kun dersom første kommando IKKE feilet" + +# For å bruke && (logisk OG) og || (logisk ELLER) sammen med if setninger, +# trenger man par av firkantklammer [] på hver side av et logisk uttrykk: +if [ $NAME == "Steve" ] && [ $AGE -eq 15 ] +then + echo "Dette kjører dersom $NAME er Steve OG $AGE er lik 15." +fi + +if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ] +then + echo "Dette kjører dersom $NAME er Daniya ELLER Zach." +fi + +# Matematiske uttrykk skrives slik: +echo $(( 10 + 5 )) + +# Ulikt de fleste programmeringsspråk, så er bash et skall - det medfører at en +# kommando i et skript kjører i en bestemt mappe i filsystemet. Du kan skrive +# ut innholdet i nåværende mappe med ls kommandoen: +ls + +# Kommandoen har parametre som kontrollerer hvordan kommandoen utføres: +ls -l # Skriv hver fil og mappe på sin egen linje + +# Resultatet av forrige kommando kan bli sendt til neste kommando som input. +# grep kommandoen filtrerer input ved hjelp av et regulært uttrykk. +# Ved å bruke grep kan vi skrive ut kun .txt filer på følgende måte: +ls -l | grep "\.txt" # lær mer om grep ved å skrive 'man grep' + +# Input og output fra filer kan dirigeres (stdin, stdout og stderr). +# 'cat' kommandoen uten argumenter skriver fra stdin til stdout. +# I det følgende eksempelet overskrives filen hello.py med linjene mellom EOF. +cat > hello.py << EOF +#!/usr/bin/env python +from __future__ import print_function +import sys +print("#stdout", file=sys.stdout) +print("#stderr", file=sys.stderr) +for line in sys.stdin: + print(line, file=sys.stdout) +EOF + +# Kjør hello.py (et python skript) +# med ulike stdin, stdout, and stderr omdirigeringer: +python hello.py < "input.in" +python hello.py > "output.out" +python hello.py 2> "error.err" +python hello.py > "output-and-error.log" 2>&1 +python hello.py > /dev/null 2>&1 +# '>' operatoren overskriver filen dersom den finnes. +# Hvis du heller vil legge til på slutten av en eksisterende fil, bruk '>>' +python hello.py >> "output.out" 2>> "error.err" + +# Overskriv output.txt, legg til error.err, og tell antall linjer med 'wc': +info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err +wc -l output.out error.err + +# Run a command and print its file descriptor (e.g. /dev/fd/123) +# Kjør en kommando og print tilhørende 'file descriptor' +# se 'man fd' +echo <(echo "#helloworld") + +# Ulike måter å overskrive output.out med '#helloworld': +cat > output.out <(echo "#helloworld") +echo "#helloworld" > output.out +echo "#helloworld" | cat > output.out +echo "#helloworld" | tee output.out >/dev/null + +# Slett noen filer med økt verbositet '-v', legg til '-i' for interaktiv modus +rm -v output.out error.err output-and-error.log + +# Kommandoer kan kjøres i deklarasjonen av andre kommandoer ved å bruke $( ): +# Følgende kommando skriver antall filer og mapper i nåværende mappe +echo "There are $(ls | wc -l) items here." + +# Det samme kan gjøres med backticks `` men de kan ikke være nøstede, +# det anbefales å bruke $( ) slik som i forrige eksempel. +echo "There are `ls | wc -l` items here." + +# Bash har en 'case' setning som fungerer omtrent som en 'switch' i Java/C: +case "$VARIABLE" in + # Skriv ønskede match med tilhørende kommandoer + 0) echo "There is a zero.";; + 1) echo "There is a one.";; + *) echo "It is not null.";; +esac + +# for løkker kan iterere over en mengde argumenter: +for VARIABLE in {1..3} +do + echo "$VARIABLE" +done + +# Eller vi kan skrive en for løkke omtrent slik det kan gjøres i Java/C: +for ((a=1; a <= 3; a++)) +do + echo $a +done + +# Man kan også iterere over resultatet av en annen kommando. +for OUTPUT in $(ls) +do + cat "$OUTPUT" +done + +# while løkke, se if setninger: +while [ true ] +do + echo "loop body here..." + break +done + +# Man kan også definere funksjoner. +# Definisjon: +function foo () +{ + echo "Argumenter fungerer akkurat som skript argumenter: $@" + echo "Og: $1 $2..." + echo "Dette er en funksjon" + return 0 +} + +# eller bare: +bar () +{ + echo "En annen måte å deklarere en funksjon." + return 0 +} + +# Å kalle en funksjon: +foo "Mitt navn er" $NAME + +# There are a lot of useful commands you should learn: +# prints last 10 lines of file.txt +# Det er mange nyttige kommandoer du bør lære deg: +# "tail" skriver ut slutten av en fil, i dette tilfellet de siste 10 linjene +tail -n 10 file.txt +# skriv ut de første 10 linjene av file.txt +head -n 10 file.txt +# sorter linjene i file.txt ("man sort") +sort file.txt +# skriv ut eller fjern repeterte linjer, med -d skrives de ut +uniq -d file.txt +# skriver kun den første kolonnen før ',' tegnet +cut -d ',' -f 1 file.txt +# erstatter hvert tilfelle av 'bjarne' med 'alfa' i file.txt, +# sed støtter regulære uttrykk ("man sed"). +sed -i 's/bjarne/alfa/g' file.txt +# skriv til stdout alle linjer i file.txt som matches av et regulært uttrykk +# eksempelet skriver ut alle linjer som begynner med "foo" og slutter med "bar" +grep "^foo.*bar$" file.txt +# skriv "-c" hvis du heller vil vite antall linjer som matcher +grep -c "^foo.*bar$" file.txt +# hvis du vil matche en bestemt streng, og ikke et regulært uttrykk +# bruker du enten "fgrep" eller ekvivalenten "grep -f" +fgrep "^foo.*bar$" file.txt + + +# Les Bash sin egen dokumentasjon om innebygde konstruksjoner: +help +help help +help for +help return +help source +help . + +# Les Bash sin "manpage": +apropos bash +man 1 bash +man bash + +# Les "info" dokumentasjon: +apropos info | grep '^info.*(' +man info +info info +info 5 info + +# Les bash sin info dokumentasjon: +info bash +info bash 'Bash Features' +info bash 6 +info --apropos bash +``` diff --git a/perl6.html.markdown b/perl6.html.markdown index de7d2f25..8d425f7d 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -213,7 +213,7 @@ say $x; #=> 52 # - `if` # Before talking about `if`, we need to know which values are "Truthy" # (represent True), and which are "Falsey" (or "Falsy") -- represent False. -# Only these values are Falsey: (), 0, "", Nil, A type (like `Str` or `Int`), +# Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or `Int`), # and of course False itself. # Every other value is Truthy. if True { diff --git a/php.html.markdown b/php.html.markdown index 2d4565e0..3fcce264 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -487,7 +487,7 @@ class MyClass * Declaring class properties or methods as static makes them accessible without * needing an instantiation of the class. A property declared as static can not * be accessed with an instantiated class object (though a static method can). -*/ + */ public static function myStaticMethod() { @@ -495,7 +495,9 @@ class MyClass } } +// Class constants can always be accessed statically echo MyClass::MY_CONST; // Outputs 'value'; + echo MyClass::$staticVar; // Outputs 'static'; MyClass::myStaticMethod(); // Outputs 'I am static'; diff --git a/pl-pl/brainfuck-pl.html.markdown b/pl-pl/brainfuck-pl.html.markdown new file mode 100644 index 00000000..69d814c4 --- /dev/null +++ b/pl-pl/brainfuck-pl.html.markdown @@ -0,0 +1,93 @@ +--- +language: brainfuck +contributors: + - ["Prajit Ramachandran", "http://prajitr.github.io/"] + - ["Mathias Bynens", "http://mathiasbynens.be/"] +translators: + - ["Jakub Młokosiewicz", "https://github.com/hckr"] +lang: pl-pl +--- + +Brainfuck (pisane małymi literami, za wyjątkiem początku zdania) jest bardzo +minimalistycznym, kompletnym w sensie Turinga, językiem programowania. +Zawiera zaledwie 8 poleceń. + +Możesz przetesotwać brainfucka w swojej przeglądarce, korzystając z narzędzia +[brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/). + +``` +Wszystkie znaki oprócz "><+-.,[]" (wyłączając znaki zapytania) są ignorowane. + +Pamięć w brainfucku jest reprezentowana przez tablicę 30.000 komórek +zainicjalizowanych zerami, ze wskaźnikiem pokazującym na aktualną komórkę. + +Oto osiem poleceń brainfucka: ++ : inkrementuje (zwiększa o jeden) wartość aktualnie wskazywanej komórki +- : dekrementuje (zmniejsza o jeden) wartość aktualnie wskazywanej komórki +> : przesuwa wskaźnik na następną komórkę (w prawo) +< : przesuwa wskaźnik na poprzednią komórkę (w lewo) +. : wyświetla wartość bieżącej komórki (w formie znaku ASCII, np. 65 = 'A') +, : wczytuje (jeden) znak z wejścia do bieżącej komórki + (konkretnie jego numer z tabeli ASCII) +[ : jeśli wartość w bieżącej komórce jest rózna zero, przechodzi do + odpowiadającego ]; w przeciwnym wypdaku przechodzi do następnej instrukcji +] : Jeśli wartość w bieżącej komórce jest rózna od zera, przechodzi do + następnej instrukcji; w przeciwnym wypdaku przechodzi do odpowiadającego [ + +[ i ] oznaczają pętlę while. Oczywiście każda pętla rozpoczęta [ +musi być zakończona ]. + +Zobaczmy kilka prostych programów w brainfucku. + + +++++++ [ > ++++++++++ < - ] > +++++ . + +Ten program wypisuje literę 'A'. Najpierw zwiększa wartość komórki #1 do 6. +Komórka #1 będzie wykorzystana w pętli. Następnie program wchodzi w pętlę ([) +i przechodzi do komórki #2. Pętla wykonuje się sześć razy (komórka #1 jest +dekrementowana sześć razy, nim osiągnie wartość zero, kiedy to program +przechodzi do odpowiadającego ] i wykonuje kolejne instrukcje). + +W tym momencie wskaźnik pokazuje na komórkę #1, mającą wartość 0, podczas gdy +komórka #2 ma wartość 60. Przesuwamy wskaźnik na komórkę #2, inkrementujemy ją +pięć razy, uzyskując wartość 65. Następnie wyświetlamy wartość komórki #2. +65 to 'A' w tabeli ASCII, więc właśnie ten znak jest wypisany na konsolę. + + +, [ > + < - ] > . + +Ten program wczytuje znak z wejścia i umieszcza jego kod ASCII w komórce #1. +Następnie zaczyna się pętla, w której znajdują się następujące instrukcje: +przesunięcie wskaźnika na komórkę #2, inkrementacja wartości komóri #2, +powrót do komórki #1 i dekrementacja wartości komórki #1. Instrukcje pętli +wykonują się aż wartość komórki #1 osiągnie zero, a komórka #2 osiągnie +poprednią wartość komórki #1. Ponieważ na końcu pętli wskaźnik pokazuje na +komórkę #1, po pętli następuje instrukcja przejścia do komórki #2 i wysłanie +jej wartości (w formie znaku ASCII) na wyjście. + +Zauważ, że odstępy służą wyłącznie poprawie czytelności. +Równie dobrze można powyższy program zapisać tak: + +,[>+<-]>. + + +Spróbuj odgadnąć, co robi poniższy program: + +,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> + +Ten program pobiera z wejścia dwie liczby i je mnoży. + +Po wczytaniu dwóch wejść (do komórek #1 i #2) następuje pętla zewnętrzna, +warunkowana wartością komórki #1. Następnie program przechodzi do komórki #2 +i rozpoczyna pętlę wewnętrzną z warunkiem zakończenia w komórce #2, +inkrementującą komórkę #3. Tu jednak pojawia się problem: w chwili zakończenia +wewnętrznej pętli komórka #2 ma wartość zero. W takim razie wewętrzna pętla +nie wywoła się następny raz. Aby rozwiązać ten problem, inkrementujemy także +wartość komórki #4, a następnie kopiujemy jej wartość do komórki #2. +Ostatecznie wynik działania znajduje się w komórce #3. +``` + +I to właśnie jest brainfuck. Nie taki trudny, co? W ramach rozrywki możesz +napisać własne programy w brainfucku. Możesz też napisać interpreter brainfucka +w innym języku. Implementacja interpretera to dość proste zadanie. Jeśli +jesteś masochistą, spróbuj napisać interpreter brainfucka w... brainfucku. diff --git a/pt-br/c++-pt.html.markdown b/pt-br/c++-pt.html.markdown index 61625ebe..61e267f5 100644 --- a/pt-br/c++-pt.html.markdown +++ b/pt-br/c++-pt.html.markdown @@ -304,7 +304,7 @@ void Dog::Dog() } // Objetos (como strings) devem ser passados por referência -// se você está modificando-os ou referência const se você não é. +// se você pretende modificá-los, ou com const caso contrário. void Dog::setName(const std::string& dogsName) { name = dogsName; diff --git a/pt-br/javascript-pt.html.markdown b/pt-br/javascript-pt.html.markdown new file mode 100644 index 00000000..e39c6c8e --- /dev/null +++ b/pt-br/javascript-pt.html.markdown @@ -0,0 +1,548 @@ +--- +language: javascript +contributors: + - ["Adam Brenecki", "http://adam.brenecki.id.au"] + - ["Ariel Krakowski", "http://www.learneroo.com"] +filename: javascript.js +--- + +JavaScript foi criada por Brendan Eich, funcionário da Netscape, em 1995. Ela +foi originalmente criada para ser uma linguagem de script para websites, +complementando o uso de Java para aplicações web mais complexas, mas a sua +integração com páginas web e seu suporte nativo nos browsers fez com que +ela se tornasse mais comum que Java no frontend web. + +Javascript não é somente limitado a browsers web, no entanto: existe o Node.js, +que é um projeto que fornece um interpretador baseado no motor V8 do Google +Chrome e está se tornando cada vez mais famoso. + + +Feedback são muito apreciados! Você me encontrar em +[@adambrenecki](https://twitter.com/adambrenecki), ou +[adam@brenecki.id.au](mailto:adam@brenecki.id.au). + +```js +// Comentários são como em C. Comentários de uma linha começam com duas barras, +/* e comentários de múltplas linhas começam com barra-asterisco + e fecham com asterisco-barra */ + +// comandos podem ser terminados com ; +facaAlgo(); + +// ... mas eles não precisam ser, assim como o ponto-e-vírgula é automaticamente +// inserido quando há uma nova linha, exceto alguns casos. +facaAlgo() + +// Porque esses casos podem causar resultados inesperados, vamos continuar +// a usar ponto-e-vírgula neste guia. + +/////////////////////////////////// +// 1. Números, Strings e Operadores + +// Javascript tem um tipo de número (que é o 64-bit IEEE 754 double). +// Doublas tem uma mantissa 52-bit, que é suficiente para guardar inteiros +// acima de 9✕10¹⁵ precisamente. +3; // = 3 +1.5; // = 1.5 + +// A aritmética básica funciona seria de esperar. +1 + 1; // = 2 +0.1 + 0.2; // = 0.30000000000000004 +8 - 1; // = 7 +10 * 2; // = 20 +35 / 5; // = 7 + +// Inclusive divisão desigual. +5 / 2; // = 2.5 + +// Operadores Bitwise também funcionam; quando você faz uma operação bitwise +// seu float é convertido para um int de até 32 bits. +1 << 2; // = 4 + +// A precedência é aplicada com parênteses. +(1 + 3) * 2; // = 8 + +// There are three special not-a-real-number values: +// Existem três especiais valores não-é-número-real: +Infinity; // resultado de 1/0 +-Infinity; // resultado de -1/0 +NaN; // resultado de 0/0 + +// Existe também o tipo booleano. +true; +false; + +// Strings são criados com ' ou ". +'abc'; +"Olá, mundo"; + +// Negation uses the ! symbol +// Negação usa o símbolo ! +!true; // = false +!false; // = true + +// Igualdade é === +1 === 1; // = true +2 === 1; // = false + +// Desigualdade é !== +1 !== 1; // = false +2 !== 1; // = true + +// Mais comparações +1 < 10; // = true +1 > 10; // = false +2 <= 2; // = true +2 >= 2; // = true + +// Strings são concatenadas com + +"Olá " + "mundo!"; // = "Olá mundo!" + +// e comparadas com < e > +"a" < "b"; // = true + +// A coerção de tipos é feita para comparações com dois iguais... +"5" == 5; // = true +null == undefined; // = true + +// ...a menos que use === +"5" === 5; // = false +null === undefined; // = false + +// ...que irá resultar num comportamento estranho... +13 + !0; // 14 +"13" + !0; // '13true' + +// Você pode acessar caracteres de uma String usando o `charAt` +"Isto é uma String".charAt(0); // = 'I' + +// ...ou usar `substring` para pegar pedaços maiores. +"Olá mundo".substring(0, 3); // = "Olá" + +// `length` é uma propriedade, portanto não use (). +"Olá".length; // = 3 + +// Existe também o `null` e o `undefined`. +null; // usado para indicar um valor não considerado +undefined; // usado para indicar um valor que não é a atualmente definido + // (entretando `undefined` é usado como um próprio valor + +// false, null, undefined, NaN, 0 and "" são valores falsy; +// qualquer outro valor é truthy +// Note que 0 é falsy e "0" é truthy, até mesmo 0 == "0". + +/////////////////////////////////// +// 2. Variáveis, Arrays e Objetos + +// Variáveis são declarados com a palavra-chave `var`. O Javascript é +// dinâmicamente tipado, portanto você não precisa especificar o tipo. +// Atribuições usam um simples caracter de `=`. +var someVar = 5; + +// se você deixar de colocar a palavra-chave var, você não receber um erro... +someOtherVar = 10; + +// ...mas sua variável será criada no escopo global, não no escopo em que você +// definiu ela. + +// Variáveis declaradas sem receberem um valor são definidas como `undefined`. +var someThirdVar; // = undefined + +// Existe um shorthad para operações matemáticas em variáveis: +someVar += 5; // equivalente a someVar = someVar + 5; someVar é 10 agora +someVar *= 10; // agora someVar é 100 + +// e um para adição e subtração de 1 +someVar++; // now someVar is 101 +someVar--; // back to 100 + +// Arrays são listas ordenadas de valores, de qualquer tipo. +var myArray = ["Olá", 45, true]; + +// Seus membros podem ser acessados usando a sintaxe de colchetes. +// O indíce de um Array começa pelo 0. +myArray[1]; // = 45 + +// Arrays são mutáveis e de tamanho variável. +myArray.push("World"); +myArray.length; // = 4 + +// Adicionar/modificar em um índice específico +myArray[3] = "Hello"; + +// Objetos de Javascript são equivalentes aos dicionários ou maps de outras +// linguagens: uma coleção não ordenada de pares chave-valor. +var myObj = {chave1: "Olá", chave2: "Mundo"}; + +// Chaves são strings, mas as aspas não são necessárias se elas são +// identificadores válidos no Javascript. Valores podem ser de qualquer tipo. +var myObj = {myKey: "myValue", "my other key": 4}; + +// Atributos de objetos também podem ser acessados com a sintaxe de colchetes. +myObj["my other key"]; // = 4 + +// ... ou usando a sintaxe de ponto, passando a chave que é um identificador +// válido. +myObj.myKey; // = "myValue" + +// Objetos são mutáveis, valores podem ser modificados e novas chaves +// adicionadas. +myObj.myThirdKey = true; + +// Se você tentar acessar um valor que não foi determinado ainda, você irá +// receber `undefined`. +myObj.myFourthKey; // = undefined + +/////////////////////////////////// +// 3. Lógica e Estruturas de Controle + +// A sintaxe para essa seção é quase idêntica a maioria das linguagens. + +// The `if` structure works as you'd expect. +// A estrutura `if` funciona como deveria ser. +var count = 1 +if (count == 3){ + // executa se count é 3 +} else if (count == 4){ + // executa se count é 4 +} else { + // executa se count não é 3 nem 4 +} + +// Como se faz um `while`. +while (true){ + // Um loop infinito! +} + +// Os loops do-while são como os loops de while, exceto quando eles sempre +// executam pelo menos uma vez. +do { + input = getInput(); +} while (!isValid(input)) + +// The `for` loop is the same as C and Java: +// initialisation; continue condition; iteration. + +// O loop `for` é o mesmo de C e Java: +// inicialização, condição para continuar; iteração +for (var i = 0; i < 5; i++){ + // vai rodar cinco vezes +} + +// && é o `e` lógico , || é o `ou` lógico +if (house.size == "big" && house.colour == "blue"){ + house.contains = "bear"; +} +if (cor == "red" || cor == "blue"){ + // cor é vermelha OU azul +} + +// && e || "pequeno circuito", é útil para determinar valores padrões. +var name = otherName || "padrão"; + +// O `switch` checa pela igualdade com `===`. +// Use `break` após cada `case` +grade = 'B'; +switch (grade) { + case 'A': + console.log("Great job"); + break; + case 'B': + console.log("OK job"); + break; + case 'C': + console.log("You can do better"); + break; + default: + console.log("Oy vey"); + break; +} + + +/////////////////////////////////// +// 4. Funções, Escopos e Closures + +// Funções Javascript são declaradas com a palavra-chave `function`. +function myFunction(thing){ + return thing.toUpperCase(); +} +myFunction("foo"); // = "FOO" + +// Repare que o valor a ser retornado deve começar na mesma linha que +// a palavra-chave `return`, senão você sempre irá retornar `undefined` +// visto que o ponto-e-vírgula é inserido automáticamente nas quebras de +// linha. Preste atenção quando usar o estilo Allman. +function myFunction() +{ + return // <- ponto-e-vírgula adicionado automaticamente aqui + { + thisIsAn: 'object literal' + } +} +myFunction(); // = undefined + +// Funções Javascript são objetos de primeira classe, portanto elas podem +// ser atribuídas a nomes de variáveis e serem passadas para outras funções +// como argumentos - por exemplo, quando criamos um manipulador de eventos: +function myFunction(){ + // este código será chamado em 5 segundos +} +setTimeout(myFunction, 5000); +// Nota: `setTimeout` não é parte da linguagem Javascript, mas é provido pelos +// browsers e o Node.js. + +// Objetos de funções não precisam nem serem declarados com nome - você pode +// escrever a definição de uma função anônima diretamente nos argumentos de +// outra função. +setTimeout(function(){ + // este código será chamado em 5 segundos +}, 5000); + +// O Javascript tem escopo de função; as funções tem seu próprio escopo, +// mas outros blocos não. +if (true){ + var i = 5; +} +i; // = 5 - não `undefined` como você esperaria numa linguagem de blogo-escopo + +// Isso levou a padrão comum chamado de IIFE (Imediately Invoked Function +// Expression) ou (Expressão de Função Invocada Imediatamente), que previne +// que variáveis temporárias vazem para o escopo global. +(function(){ + var temporary = 5; + // Nós podemos acessar o escopo global definindo o "objeto global", que + // no browser vai ser sempre `window`. O objeto global pode ter um nome + // diferente para ambiente não-browser como o Node.js. + window.permanent = 10; +})(); +temporary; // levanta um erro de referência inexiste +permanent; // = 10 + +// Uma das principais características do Javascript é a closure. Que é +// uma função definida dentro de outra função, a função interna pode acessar +// todas as variáveis da função externa, mesmo depois da função de fora +// finalizar sua execução. +function sayHelloInFiveSeconds(name){ + var prompt = "Hello, " + name + "!"; + + // Funções internas são colocadas no escopo local por padrão, assim como + // se fossem declaradas com `var`. + function inner(){ + alert(prompt); + } + setTimeout(inner, 5000); + // `setTimeout` é assíncrono, portanto a função `sayHelloInFiveSeconds` + // vai sair imediatamente, e o `setTimeout` irá chamar a interna depois. + // Entretanto. como a interna é fechada dentro de "sayHelloInFiveSeconds", + // a interna permanece podendo acessar a variável `prompt` quando depois + // de chamada. +} +sayHelloInFiveSeconds("Adam"); // Vai abrir um popup com "Hello, Adam!" em 5s + +/////////////////////////////////// +// 5. Mais sobre Objetos; Construtores e Prototypes + +// Objetos podem conter funções. +var myObj = { + myFunc: function(){ + return "Olá mundo!"; + } +}; +myObj.myFunc(); // = "Olá mundo!" + +// Quando uma função ligada a um objeto é chamada, ela pode acessar o objeto +// da qual foi ligada usando a palavra-chave `this`. +myObj = { + myString: "Olá mundo!", + myFunc: function(){ + return this.myString; + } +}; +myObj.myFunc(); // = "Olá mundo!" + +// O `this` só funciona para dentro do escopo do objeto, portanto, se chamarmos +// um método do objeto fora de seu escopo, este não irá funcionar. +var myFunc = myObj.myFunc; +myFunc(); // = undefined + +// Inversamente, uma função pode ser atribuída a um objeto e ganhar a acesso +// através do `this`, até mesmo se ela não for chamada quando foi definida. +var myOtherFunc = function(){ + return this.myString.toUpperCase(); +} +myObj.myOtherFunc = myOtherFunc; +myObj.myOtherFunc(); // = "OLÁ MUNDO!" + +// Nós podemos também especificar um contexto onde a função irá executar, +// usando o `call` ou `apply`. + +var anotherFunc = function(s){ + return this.myString + s; +} +anotherFunc.call(myObj, " E Olá Lua!"); // = "Olá mundo! E Olá Lua!" + +// A função `apply` é praticamente a mesma coisa, mas ela pega um array +// como lista de argumentos. + +anotherFunc.apply(myObj, [" E Olá Sol!"]); // = "Olá mundo! E Olá Sol!" + +// Isto é util quando trabalhamos com uma função que aceita uma sequência de +// argumentos e você quer passar um array. + +Math.min(42, 6, 27); // = 6 +Math.min([42, 6, 27]); // = NaN (uh-oh!) +Math.min.apply(Math, [42, 6, 27]); // = 6 + +// Mas, o `call` e `apply` são somente temporários. Quando você quiser que +// permaneça sempre no escopo, use `bind`. + +var boundFunc = anotherFunc.bind(myObj); +boundFunc(" E Olá Saturno!"); // = "Olá mundo! E Olá Saturno!" + +// `bind` também pode ser usado para parcialmente aplicar (curry) uma função. + +var product = function(a, b){ return a * b; } +var doubler = product.bind(this, 2); +doubler(8); // = 16 + +// Quando você invoca uma função com a palavra-chave `new`, um novo objeto +// é criado, e fica disponível para a função pela palavra-chave `this`. +// Funções são desenhadas para serem invocadas como se invocam os construtores. + +var MyConstructor = function(){ + this.myNumber = 5; +} +myNewObj = new MyConstructor(); // = {myNumber: 5} +myNewObj.myNumber; // = 5 + +// Todo objeto JavaScript possui um `prototype`. Quando você tenta acessar +// uma propriedade de um objeto que não existe no objeto atual, o interpretador +// vai olhar imediatamente para o seu prototype. + +// Algumas implementações em JS deixam você acessar o objeto prototype com a +// propriedade mágica `__proto__`. Enquanto isso é util para explicar +// prototypes, não é parte de um padrão; nós vamos falar de algumas formas de +// usar prototypes depois. + +var myObj = { + myString: "Olá Mundo!" +}; +var myPrototype = { + meaningOfLife: 42, + myFunc: function(){ + return this.myString.toLowerCase() + } +}; + +myObj.__proto__ = myPrototype; +myObj.meaningOfLife; // = 42 + +// This works for functions, too. +// Isto funciona para funções, também. +myObj.myFunc(); // = "olá mundo!" + +// É claro, se sua propriedade não está em seu prototype, +// o prototype do prototype será procurado e por aí vai. +myPrototype.__proto__ = { + myBoolean: true +}; +myObj.myBoolean; // = true + +// Não há cópia envolvida aqui; cada objeto guarda uma referência do +// prototype. Isso significa que podemos alterar o prototype e nossas mudanças +// serão refletidas em qualquer lugar. +myPrototype.meaningOfLife = 43; +myObj.meaningOfLife; // = 43 + + +// Nós mencionamos que o `__proto__` não é uma forma padrão, e não há uma +// forma padrão de mudar o prototype de um objeto já existente. Entretanto, +// existem duas formas de se criar um objeto com um dado prototype. + +// A primeira forma é `Object.create`, que é uma adição recente do JS, +// e ainda não está disponível em todas as implementações. +var myObj = Object.create(myPrototype); +myObj.meaningOfLife; // = 43 + +// A segunda forma, que funciona em qualquer lugar, é feita com construtores. +// Construtores tem uma propriedade chamada prototype. Este *não* é o prototype +// do construtor em si; ao invés disso, ele é o prototype dos novos objetos +// criados pelo construtor. +MyConstructor.prototype = { + myNumber: 5, + getMyNumber: function(){ + return this.myNumber; + } +}; +var myNewObj2 = new MyConstructor(); +myNewObj2.getMyNumber(); // = 5 +myNewObj2.myNumber = 6 +myNewObj2.getMyNumber(); // = 6 + +// Tipos originais da linguagem como strings e números também possuem +// construtores equivalentes. +var myNumber = 12; +var myNumberObj = new Number(12); +myNumber == myNumberObj; // = true + +// Exceto, que eles não são totalmente equivalentes. +typeof myNumber; // = 'number' +typeof myNumberObj; // = 'object' +myNumber === myNumberObj; // = false +if (0){ + // O código não vai executar, porque 0 é um valor falso. +} + +// Entretanto, esses objetos encapsulados e as funções originais compartilham +// um mesmo prototype, portanto você pode adicionar funcionalidades a uma string, +// por exemplo. +String.prototype.firstCharacter = function(){ + return this.charAt(0); +} +"abc".firstCharacter(); // = "a" + +// Esse fato é usado para criar os chamados `polyfills`, que implementam +// uma nova característica do Javascript em uma versão mais velha, para que +// assim funcionem em ambientes mais velhos como browsers ultrapassados. + +// Havíamos mencionado que `Object.create` não estava ainda disponível em +// todos as implementações, mas nós podemos usá-lo com esse polyfill: +if (Object.create === undefined){ // don't overwrite it if it exists + Object.create = function(proto){ + // faz um construtor temporário com o prototype certo + var Constructor = function(){}; + Constructor.prototype = proto; + // então utiliza o new para criar um objeto prototype apropriado + return new Constructor(); + } +} +``` + +## Leitura Adicional + +O [Mozilla Developer +Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript) dispõe de uma +excelente documentação sobre Javascript e seu uso nos browsers. E mais, +é uma wiki, portanto conforme você vai aprendendo, mais você pode ir ajudando +os outros compartilhando do seu conhecimento. + +[Uma re-introdução do JavaScript pela MDN] +(https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) +cobre muito dos conceitos abordados aqui em mais detalhes. Este guia fala +somente sobre a linguagem JavaScript em si; se você quiser aprender mais +sobre e como usar o JavaScript em páginas na web, comece aprendendo sobre +[Document Object +Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core) + +[Aprenda Javascript por Exemplos e com Desafios](http://www.learneroo.com/modules/64/nodes/350) é uma +variação desse guia com desafios. + +[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) é um guia +profundo de todas as partes do JavaScript. + +[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) é o guia clássico +/ livro de referência. + +Parte desse artigo foi adaptado do tutorial de Python do Louie Dinh que está +nesse site e do [Tutorial de JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) +da Mozilla Developer Network. diff --git a/pt-br/perl-pt.html.markdown b/pt-br/perl-pt.html.markdown new file mode 100644 index 00000000..cc07a2ec --- /dev/null +++ b/pt-br/perl-pt.html.markdown @@ -0,0 +1,166 @@ +--- +name: perl +category: language +language: perl +filename: learnperl-pt.pl +contributors: + - ["Korjavin Ivan", "http://github.com/korjavin"] +translators: + - ["Miguel Araújo", "https://github.com/miguelarauj1o"] +lang: pt-br +--- + +Perl 5 é, uma linguagem de programação altamente capaz, rica em recursos, com mais de 25 anos de desenvolvimento. + +Perl 5 roda em mais de 100 plataformas, de portáteis a mainframes e é adequada tanto para prototipagem rápida, quanto em projetos de desenvolvimento em grande escala. + +```perl +# Comentários de uma linha começam com um sinal de número. + +#### Tipos de variáveis em Perl + +# Variáveis iniciam com um sigilo, que é um símbolo que mostra o tipo. +# Um nome de variável válido começa com uma letra ou sublinhado, +# seguido por qualquer número de letras, números ou sublinhados. + +### Perl has three main variable types: $scalar, @array, e %hash. + +## Scalars +# Um scalar representa um valor único: +my $animal = "camelo"; +my $resposta = 42; + +# Valores scalar podem ser strings, inteiros ou números ponto-flutuantes e +# Perl vai automaticamente converter entre eles quando for preciso. + +## Arrays +# Um array representa uma lista de valores: +my @animais = ("camelo", "vaca", "boi"); +my @números = (23, 42, 69); +my @misturado = ("camelo", 42, 1.23); + +## Hashes +# Um hash representa um conjunto de pares chave/valor: + +my %fruta_cor = ("maçã", "vermelho", "banana", "amarelo"); + +# Você pode usar o espaço em branco e o operador "=>" para colocá-los de +# maneira mais agradável: + +my %fruta_cor = ( + maçã => "vermelho", + banana => "amarelo", +); + +# Scalars, arrays and hashes são documentados mais profundamentes em perldata. +# (perldoc perldata). + +# Mais tipos de dados complexos podem ser construídas utilizando referências, +# o que permite que você crie listas e hashes dentro de listas e hashes. + +#### Condicionais e construtores de iteração + +# Perl possui a maioria das construções condicionais e de iteração habituais. + +if ($var) { + ... +} elsif ($var eq 'bar') { + ... +} else { + ... +} + +unless (condição) { + ... +} +# Isto é fornecido como uma versão mais legível de "if (!condition)" + +# A forma Perlish pós-condição +print "Yow!" if $zippy; +print "Nós não temos nenhuma banana" unless $bananas; + +# while +while (condição) { + ... +} + +# for +for (my $i = 0; $i < $max; $i++) { + print "valor é $i"; +} + +for (my $i = 0; $i < @elements; $i++) { + print "Elemento atual é " . $elements[$i]; +} + +for my $element (@elements) { + print $element; +} + +# implícito + +for (@elements) { + print; +} + +#### Expressões regulares + +# O suporte a expressões regulares do Perl é ao mesmo tempo amplo e profundo, +# e é objeto de longa documentação em perlrequick, perlretut, e em outros +# lugares. No entanto, em suma: + +# Casamento simples +if (/foo/) { ... } # verdade se $_ contém "foo" +if ($a =~ /foo/) { ... } # verdade se $a contém "foo" + +# Substituição simples + +$a =~ s/foo/bar/; # substitui foo com bar em $a +$a =~ s/foo/bar/g; # substitui TODAS AS INSTÂNCIAS de foo com bar em $a + +#### Arquivos e I/O + +# Você pode abrir um arquivo para entrada ou saída usando a função "open()". + +open(my $in, "<", "input.txt") ou desistir "Não pode abrir input.txt: $!"; +open(my $out, ">", "output.txt") ou desistir "Não pode abrir output.txt: $!"; +open(my $log, ">>", "my.log") ou desistir "Não pode abrir my.log: $!"; + +# Você pode ler de um arquivo aberto usando o operador "<>". No contexto +# scalar, ele lê uma única linha do arquivo, e em contexto de lista lê o +# arquivo inteiro, atribuindo cada linha a um elemento da lista: + +my $linha = <$in>; +my @linhas = <$in>; + +#### Escrevendo subrotinas + +# Escrever subrotinas é fácil: + +sub logger { + my $mensagem = shift; + + open my $arquivo, ">>", "my.log" or die "Não poderia abrir my.log: $!"; + + print $arquivo $ensagem; +} + +# Agora nós podemos usar a subrotina como qualquer outra função construída: + +logger("Nós temos uma subrotina de log!"); +``` + +#### Usando módulos Perl + +Módulos Perl provê uma lista de recursos para lhe ajudar a evitar redesenhar +a roda, e tudo isso pode ser baixado do CPAN (http://www.cpan.org/). Um número +de módulos populares podem ser incluídos com a própria distribuição do Perl. + +perlfaq contém questões e respostas relacionadas a muitas tarefas comuns, e frequentemente provê sugestões para um bom números de módulos CPAN. + +#### Leitura Adicional + + - [perl-tutorial](http://perl-tutorial.org/) + - [Learn at www.perl.com](http://www.perl.org/learn.html) + - [perldoc](http://perldoc.perl.org/) + - and perl built-in : `perldoc perlintro` diff --git a/python.html.markdown b/python.html.markdown index 88e0deb1..5572e38e 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -198,7 +198,7 @@ li[::-1] # => [3, 4, 2, 1] # Remove arbitrary elements from a list with "del" del li[2] # li is now [1, 2, 3] -r + # You can add lists li + other_li # => [1, 2, 3, 4, 5, 6] # Note: values for li and for other_li are not modified. @@ -379,7 +379,13 @@ except (TypeError, NameError): pass # Multiple exceptions can be handled together, if required. else: # Optional clause to the try/except block. Must follow all except blocks print "All good!" # Runs only if the code in try raises no exceptions +finally: # Execute under all circumstances + print "We can clean up resources here" +# Instead of try/finally to cleanup resources you can use a with statement +with open("myfile.txt") as f: + for line in f: + print line #################################################### ## 4. Functions @@ -406,7 +412,7 @@ varargs(1, 2, 3) # => (1, 2, 3) # You can define functions that take a variable number of -# keyword args, as well, which will be interpreted as a map if you do not use ** +# keyword args, as well, which will be interpreted as a dict if you do not use ** def keyword_args(**kwargs): return kwargs diff --git a/python3.html.markdown b/python3.html.markdown index 9d965fb1..b3acb122 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -374,6 +374,13 @@ except (TypeError, NameError): pass # Multiple exceptions can be handled together, if required. else: # Optional clause to the try/except block. Must follow all except blocks print("All good!") # Runs only if the code in try raises no exceptions +finally: # Execute under all circumstances + print("We can clean up resources here") + +# Instead of try/finally to cleanup resources you can use a with statement +with open("myfile.txt") as f: + for line in f: + print(line) # Python offers a fundamental abstraction called the Iterable. # An iterable is an object that can be treated as a sequence. diff --git a/ro-ro/clojure-ro.html.markdown b/ro-ro/clojure-ro.html.markdown new file mode 100644 index 00000000..32ba9620 --- /dev/null +++ b/ro-ro/clojure-ro.html.markdown @@ -0,0 +1,386 @@ +--- +language: clojure +contributors: + - ["Adam Bard", "http://adambard.com/"] +translators: + - ["Bogdan Paun", "http://twitter.com/bgdnpn"] +filename: learnclojure-ro.clj +lang: ro-ro +--- + +Clojure este un limbaj din familia Lisp dezvoltat pentru Masina Virtuala Java +(Java Virtual Machine - JVM). Pune un accent mult mai puternic pe +[programarea funcionala](https://en.wikipedia.org/wiki/Functional_programming) +pura decat Common Lisp, dar include utilitare [STM](https://en.wikipedia.org/wiki/Software_transactional_memory) +pentru a gestiona starea, atunci cand aceasta apare. + +Combinatia aceasta ii permite sa gestioneze procese concurente foarte usor, +de multe ori in mod automat. + +(Aveti nevoie deo versiune Clojure 1.2 sau mai noua) + + +```clojure +; Comentariile incep cu punct si virgula. + +; Clojure se scrie in "forme", care nu sunt decat +; liste de lucruri in interiorul unor paranteze, separate prin spatii. +; +; Reader-ul Clojure presupune ca primul lucru este o +; functie sau un macro de apelat, iar restul sunt argumente. + +; Prima apelare intr-un fisier ar trebui sa fie ns, pentru a configura namespace-ul +(ns learnclojure) + +; Mai multe exemple de baza: + +; str va crea un string folosint toate argumentele sale +(str "Hello" " " "World") ; => "Hello World" + +; Matematica este simpla +(+ 1 1) ; => 2 +(- 2 1) ; => 1 +(* 1 2) ; => 2 +(/ 2 1) ; => 2 + +; Egalitatea este = +(= 1 1) ; => true +(= 2 1) ; => false + +; Folosim si not pentru logica +(not true) ; => false + +; Formele imbricate functioneaza asa +(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2 + +; Tipuri +;;;;;;;;;;;;; + +; Clojure foloseste sistemul de obiecte Java pentru boolean, string si numere. +; Folositi `class` pentru a le inspecta. +(class 1) ; Numere intregi sunt jaba.lang.Long, in mod normal +(class 1.); Numelere reale sunt java.lang.Double +(class ""); Sirurile de caractere sunt mere intre apostrofuri duble, si sunt java.lang.String +(class false) ; Booleanele sunt java.lang.Boolean +(class nil); Valoarea "null" este numita nil + +; Daca doriti sa creati o lista de date literale, folositi ' pentru a preveni +; evaluarea ei +'(+ 1 2) ; => (+ 1 2) +; (prescurtare pentru (quote (+ 1 2))) + +; Puteti evalua o lista cu apostrof +(eval '(+ 1 2)) ; => 3 + +; Colectii & Secvente +;;;;;;;;;;;;;;;;;;; + +; Listele sunt structuri de date lista-inlantuita, spre deosebire de Vectori +; Vectorii si Listele sunt si ele clase Java! +(class [1 2 3]); => clojure.lang.PersistentVector +(class '(1 2 3)); => clojure.lang.PersistentList + +; O liste ar putea fi scrisa direct ca (1 2 3), dar trebuie sa folosim apostrof +; pentru a preveni reader-ul din a crede ca e o functie. +; De asemenea, (list 1 2 3) este acelasi lucru cu '(1 2 3) + +; "Colectiile" sunt grupuri de date +; Atat listele cat si vectorii sunt colectii: +(coll? '(1 2 3)) ; => true +(coll? [1 2 3]) ; => true + +; "Sequences" (seqs) are abstract descriptions of lists of data. +; Only lists are seqs. +(seq? '(1 2 3)) ; => true +(seq? [1 2 3]) ; => false + +; O secventa necesita un punct de intrare doar cand este accesata. +; Deci, secventele, care pot fi "lazy" -- pot defini serii infinite: +(range 4) ; => (0 1 2 3) +(range) ; => (0 1 2 3 4 ...) (o serie infinita) +(take 4 (range)) ; (0 1 2 3) + +; Folositi cons pentru a adauga un element la inceputul unei liste sau unui vector +(cons 4 [1 2 3]) ; => (4 1 2 3) +(cons 4 '(1 2 3)) ; => (4 1 2 3) + +; Conj va adauga un element unei colectii in modul cel mai eficient. +; Pentru liste, aceastea sunt inserate la inceput. Pentru vectori, sunt inserate la final. +(conj [1 2 3] 4) ; => [1 2 3 4] +(conj '(1 2 3) 4) ; => (4 1 2 3) + +; Folositi concat pentru a uni liste sau vectori +(concat [1 2] '(3 4)) ; => (1 2 3 4) + +; Folositi filter, map pentru a interactiona cu colectiile +(map inc [1 2 3]) ; => (2 3 4) +(filter even? [1 2 3]) ; => (2) + +; Folositi reduce pentru a le reduce +(reduce + [1 2 3 4]) +; = (+ (+ (+ 1 2) 3) 4) +; => 10 + +; Reduce poate lua un argument valoare-initiala +(reduce conj [] '(3 2 1)) +; = (conj (conj (conj [] 3) 2) 1) +; => [3 2 1] + +; Functii +;;;;;;;;;;;;;;;;;;;;; + +; Folositi fn pentru a crea functii noi. O functie returneaza intotdeauna +; ultima sa instructiune. +(fn [] "Hello World") ; => fn + +; (Necesita paranteze suplimentare pentru a fi apelata) +((fn [] "Hello World")) ; => "Hello World" + +; Puteti crea o variabila folosind def +(def x 1) +x ; => 1 + +; Atribuiti o functie unei variabile +(def hello-world (fn [] "Hello World")) +(hello-world) ; => "Hello World" + +; Puteti scurta acest proces folosind defn +(defn hello-world [] "Hello World") + +; Elementul [] este lista de argumente a functiei. +(defn hello [name] + (str "Hello " name)) +(hello "Steve") ; => "Hello Steve" + +; Puteti, de asemenea, folosi aceasta prescurtare pentru a crea functii: +(def hello2 #(str "Hello " %1)) +(hello2 "Fanny") ; => "Hello Fanny" + +; Puteti avea si functii cu mai multe variabile +(defn hello3 + ([] "Hello World") + ([name] (str "Hello " name))) +(hello3 "Jake") ; => "Hello Jake" +(hello3) ; => "Hello World" + +; Functiile pot primi mai mult argumente dintr-o secventa +(defn count-args [& args] + (str "Ati specificat " (count args) " argumente: " args)) +(count-args 1 2 3) ; => "Ati specificat 3 argumente: (1 2 3)" + +; Puteti interschimba argumente normale si argumente-secventa +(defn hello-count [name & args] + (str "Salut " name ", ati specificat " (count args) " argumente extra")) +(hello-count "Finn" 1 2 3) +; => "Salut Finn, ai specificat 3 argumente extra" + + +; Maps (Dictionare) +;;;;;;;;;; + +; Hash maps si Array maps impart o interfata. Hash maps au cautari mai rapide +; dar nu retin ordinea cheilor. +(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap +(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap + +; Arraymaps de vin automat hashmaps prin majoritatea operatiilor +; daca sunt suficient de mari, asa ca nu trebuie sa va preocupe acest aspect. + +; Dictionarele pot folosi orice tip hashable ca si cheie, dar cuvintele cheie +; (keywords) sunt, de obicei, cele mai indicate. Cuvintele cheie sunt ca niste +; siruri de caractere cu un plus de eficienta +(class :a) ; => clojure.lang.Keyword + +(def stringmap {"a" 1, "b" 2, "c" 3}) +stringmap ; => {"a" 1, "b" 2, "c" 3} + +(def keymap {:a 1, :b 2, :c 3}) +keymap ; => {:a 1, :c 3, :b 2} + +; Apropo, virgulele sunt intotdeauna considerate echivalente cu spatiile. + +; Apelati un dictionar (map) ca pe o functie pentru a primi o valoare anume +(stringmap "a") ; => 1 +(keymap :a) ; => 1 + +; Cuvintele cheie pot fi folosite si ele pentru a "cere" dictionarului valorile lor! +(:b keymap) ; => 2 + +; Nu incercati asta cu siruri de caractere. +;("a" stringmap) +; => Exception: java.lang.String cannot be cast to clojure.lang.IFn + +; Recuperarea unei chei inexistente returneaza nil +(stringmap "d") ; => nil + +; Folositi assoc pentru a adauga nou chei unui ductionar +(def newkeymap (assoc keymap :d 4)) +newkeymap ; => {:a 1, :b 2, :c 3, :d 4} + +; Dar retineti ca tipurile sunt imuabile in clojure +keymap ; => {:a 1, :b 2, :c 3} + +; Folositi dissoc pentru a elimina chei +(dissoc keymap :a :b) ; => {:c 3} + +; Seturi (multimi) +;;;;;; + +(class #{1 2 3}) ; => clojure.lang.PersistentHashSet +(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3} + +; Adaugati un membru cu conj +(conj #{1 2 3} 4) ; => #{1 2 3 4} + +; Eliminati unul cu disj +(disj #{1 2 3} 1) ; => #{2 3} + +; Testati existenta unuia folosing setul ca o functie: +(#{1 2 3} 1) ; => 1 +(#{1 2 3} 4) ; => nil + +; Exista mai multe functii in namespace-ul clojure.sets. + +; Forme utile +;;;;;;;;;;;;;;;;; + +; In Clojure constructiile logice sunt macro-uri, si arata ca +; oricare alta forma +(if false "a" "b") ; => "b" +(if false "a") ; => nil + +; Folositi let pentru a crea atribuiri temporare +(let [a 1 b 2] + (> a b)) ; => false + +; Grupati instructiuni impreuna folosind do +(do + (print "Hello") + "World") ; => "World" (prints "Hello") + +; Functiile contin un do implicit +(defn print-and-say-hello [name] + (print "Saying hello to " name) + (str "Hello " name)) +(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff") + +; Asemanator pentru let +(let [name "Urkel"] + (print "Saying hello to " name) + (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel") + +; Module +;;;;;;;;;;;;;;; + +; Folositi "use" pentru a recupera toate functiile dintr-un modul +(use 'clojure.set) + +; Acum putem folosi operatiuni pe seturi +(intersection #{1 2 3} #{2 3 4}) ; => #{2 3} +(difference #{1 2 3} #{2 3 4}) ; => #{1} + +; Puteri de asemenea alege un subset al functiilor de importat +(use '[clojure.set :only [intersection]]) + +; Folositi require pentru a importa un modul +(require 'clojure.string) + +; Folositi / pentru a apela functii dintr-un modul +; In acest caz, modulul este clojure.string, iar functia este blank? +(clojure.string/blank? "") ; => true + +; Puteti atribui un nume mai scurt unui modul in momentul importului +(require '[clojure.string :as str]) +(str/replace "Acesta este un test." #"[a-o]" str/upper-case) ; => "ACEstA EstE un tEst." +; (#"" denota o expresie regulata) + +; Puteti folsi require (sau use, contraindicat) dintr-un namespace folosind :require. +; Nu trebuie sa folositi apostrof pentru module daca procedati astfel. +(ns test + (:require + [clojure.string :as str] + [clojure.set :as set])) + +; Java +;;;;;;;;;;;;;;;;; + +; Java are o biblioteca standard imensa si folositoare, deci +; ar fi util sa stiti cum sa o folositi. + +; Folositi import pentru a incarca un modul Java +(import java.util.Date) + +; Puteti importa si dintr-un namesopace. +(ns test + (:import java.util.Date + java.util.Calendar)) + +; Folositi numele clasei cu "." la final pentru a crea o noua instanta +(Date.) ; <a date object> + +; Folositi . pentru a apela metode. Pe scurt, folositi ".method" +(. (Date.) getTime) ; <a timestamp> +(.getTime (Date.)) ; exact acelasi lucru. + +; Folositi / pentru a apela metode statice +(System/currentTimeMillis) ; <a timestamp> (System este prezent intotdeauna) + +; Folositi doto pentru a gestiona clase (mutable) mai usor +(import java.util.Calendar) +(doto (Calendar/getInstance) + (.set 2000 1 1 0 0 0) + .getTime) ; => A Date. set to 2000-01-01 00:00:00 + +; STM +;;;;;;;;;;;;;;;;; + +; Software Transactional Memory este un mecanism folost de Clojure pentru +; a gestiona stari persistente. Sunt putine instante in care este folosit. + +; Un atom este cel mai simplu exemplu. Dati-i o valoare initiala +(def my-atom (atom {})) + +; Modificati-l cu swap!. +; swap! primeste o functie si o apeleaza cu valoarea actuala a atomului +; ca prim argument si orice argumente suplimentare ca al doilea +(swap! my-atom assoc :a 1) ; Atomul ia valoarea rezultata din (assoc {} :a 1) +(swap! my-atom assoc :b 2) ; Atomul ia valoarea rezultata din (assoc {:a 1} :b 2) + +; Folositi '@' pentru a dereferentia atomul si a-i recupera valoarea +my-atom ;=> Atom<#...> (Returmeaza obiectul Atom) +@my-atom ; => {:a 1 :b 2} + +; Aici avem un contor simplu care foloseste un atom +(def counter (atom 0)) +(defn inc-counter [] + (swap! counter inc)) + +(inc-counter) +(inc-counter) +(inc-counter) +(inc-counter) +(inc-counter) + +@counter ; => 5 + +; Alte utilizari ale STM sunt referintele (refs) si agentii (agents). +; Refs: http://clojure.org/refs +; Agents: http://clojure.org/agents +``` + +### Lectura suplimentara + +Lista nu este in niciun caz exhaustiva, dar speram ca este suficienta pentru +a va oferi un inceput bun in Clojure. + +Clojure.org contine multe articole: +[http://clojure.org/](http://clojure.org/) + +Clojuredocs.org contine documentatie cu exemple pentru majoritatea functiilor de baza: +[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core) + +4Clojure este o metoda excelenta pentru a exersa Clojure/FP (Programarea Functionala): +[http://www.4clojure.com/](http://www.4clojure.com/) + +Clojure-doc.org are un numar de article pentru incepatori: +[http://clojure-doc.org/](http://clojure-doc.org/) diff --git a/ru-ru/brainfuck-ru.html.markdown b/ru-ru/brainfuck-ru.html.markdown index 500ac010..fcee185f 100644 --- a/ru-ru/brainfuck-ru.html.markdown +++ b/ru-ru/brainfuck-ru.html.markdown @@ -11,6 +11,8 @@ lang: ru-ru Brainfuck (пишется маленькими буквами, кроме начала предложения) - это очень маленький Тьюринг-полный язык программирования лишь с 8 командами. +Вы можете испытать brainfuck в вашем браузере с помощью [brainfuck-визуализатора](http://fatiherikli.github.io/brainfuck-visualizer/). + ``` Любой символ, кроме "><+-.,[]", игнорируется, за исключением кавычек. diff --git a/ru-ru/objective-c-ru.html.markdown b/ru-ru/objective-c-ru.html.markdown index 3246de82..ddff2e5c 100644 --- a/ru-ru/objective-c-ru.html.markdown +++ b/ru-ru/objective-c-ru.html.markdown @@ -1,106 +1,171 @@ --- language: Objective-C -filename: LearnObjectiveC.m +filename: LearnObjectiveC-ru.m contributors: - ["Eugene Yagrushkin", "www.about.me/yagrushkin"] - ["Yannick Loriot", "https://github.com/YannickL"] + - ["Levi Bostian", "https://github.com/levibostian"] translators: - ["Evlogy Sutormin", "http://evlogii.com"] + - ["Dmitry Bessonov", "https://github.com/TheDmitry"] lang: ru-ru --- -Objective-C — компилируемый объектно-ориентированный язык программирования, используемый корпорацией Apple, -построенный на основе языка Си и парадигм Smalltalk. -В частности, объектная модель построена в стиле Smalltalk — то есть объектам посылаются сообщения. +Objective-C — основной язык программирования, используемый корпорацией Apple +для операционных систем OS X и iOS и их соответствующих фреймворках Cocoa и +Cocoa Touch. +Он является объектно-ориентированным языком программирования общего назначения, +который добавляет обмен сообщениями в Smalltalk-стиле к языку программирования C. ```objective_c -// Однострочный комментарий +// Однострочные комментарии начинаются с // /* -Многострочный -комментарий +Так выглядят многострочные комментарии */ -// Импорт файлов фреймворка Foundation с помощью #import +// Импорт заголовочных файлов фреймворка Foundation с помощью #import +// Используйте <>, чтобы импортировать глобальные файлы (обычно фреймворки) +// Используйте "", чтобы импортировать локальные файлы (из проекта) #import <Foundation/Foundation.h> #import "MyClass.h" -// Точка входа в программу это функция main, -// которая возвращает целый тип integer +// Если вы включили модули для iOS >= 7.0 или OS X >= 10.9 проектов в +// Xcode 5, вы можете импортировать фреймворки подобным образом: +@import Foundation; + +// Точка входа в программу - это функция main, +// которая возвращает целый тип int main (int argc, const char * argv[]) { - // Создание autorelease pool для управления памятью + // Создание autorelease pool для управления памятью в программе NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - + // В место этого воспользуйтесь @autoreleasepool, если вы используете + // автоматический подсчет ссылок (ARC) + @autoreleasepool { + // Используйте NSLog для печати в консоль - NSLog(@"Hello World!"); // Напечатает строку "Hello World!" + NSLog(@"Привет Мир!"); // Напечатает строку "Привет Мир!" /////////////////////////////////////// // Типы и переменные /////////////////////////////////////// - // Простое объявление + // Объявление простых типов int myPrimitive1 = 1; long myPrimitive2 = 234554664565; + // Объявление объектов // Помещайте * в начало названия объекта для строго типизированного объявления MyClass *myObject1 = nil; // Строгая типизация id myObject2 = nil; // Слабая типизация - - NSLog(@"%@ and %@", myObject1, [myObject2 description]); // напечатает "(null) and (null)" // %@ – это объект - // 'description' это общий для всех объектов метод вывода данных + // 'description' - это общий для всех объектов метод вывода данных + NSLog(@"%@ and %@", myObject1, [myObject2 description]); // напечатает "(null) and (null)" // Строка - NSString *worldString = @"World"; - NSLog(@"Hello %@!", worldString); // напечатает "Hello World!" + NSString *worldString = @"Мир"; + NSLog(@"Привет %@!", worldString); // напечатает "Привет Мир!" + // NSMutableString - это изменяемая версия NSString-объекта + NSMutableString *mutableString = [NSMutableString stringWithString:@"Привет"]; + [mutableString appendString:@" Мир!"]; + NSLog(@"%@", mutableString); // напечатает => "Привет Мир!" // Символьные литералы NSNumber *theLetterZNumber = @'Z'; - char theLetterZ = [theLetterZNumber charValue]; + char theLetterZ = [theLetterZNumber charValue]; // или 'Z' NSLog(@"%c", theLetterZ); - // Целочисленный литералы + // Целочисленные литералы NSNumber *fortyTwoNumber = @42; - int fortyTwo = [fortyTwoNumber intValue]; + int fortyTwo = [fortyTwoNumber intValue]; // или '42' NSLog(@"%i", fortyTwo); // Беззнаковый целочисленный литерал NSNumber *fortyTwoUnsignedNumber = @42U; - unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; + unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; // или 42 NSLog(@"%u", fortyTwoUnsigned); NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42]; - short fortyTwoShort = [fortyTwoShortNumber shortValue]; + short fortyTwoShort = [fortyTwoShortNumber shortValue]; // или 42 NSLog(@"%hi", fortyTwoShort); + NSNumber *fortyOneShortNumber = [NSNumber numberWithShort:41]; + unsigned short fortyOneUnsigned = [fortyOneShortNumber unsignedShortValue]; // или 41 + NSLog(@"%u", fortyOneUnsigned); + NSNumber *fortyTwoLongNumber = @42L; - long fortyTwoLong = [fortyTwoLongNumber longValue]; + long fortyTwoLong = [fortyTwoLongNumber longValue]; // или 42 NSLog(@"%li", fortyTwoLong); + + NSNumber *fiftyThreeLongNumber = @53L; + unsigned long fiftyThreeUnsigned = [fiftyThreeLongNumber unsignedLongValue]; // или 53 + NSLog(@"%lu", fiftyThreeUnsigned); // Вещественный литерал NSNumber *piFloatNumber = @3.141592654F; - float piFloat = [piFloatNumber floatValue]; - NSLog(@"%f", piFloat); + float piFloat = [piFloatNumber floatValue]; // или 3.141592654f + NSLog(@"%f", piFloat); // напечатает 3.141592654 + NSLog(@"%5.2f", piFloat); // напечатает " 3.14" NSNumber *piDoubleNumber = @3.1415926535; - double piDouble = [piDoubleNumber doubleValue]; + double piDouble = [piDoubleNumber doubleValue]; // или 3.1415926535 NSLog(@"%f", piDouble); - + NSLog(@"%4.2f", piDouble); // напечатает "3.14" + + // NSDecimalNumber - это класс с фиксированной точкой, который является + // более точным, чем float или double + NSDecimalNumber *oneDecNum = [NSDecimalNumber decimalNumberWithString:@"10.99"]; + NSDecimalNumber *twoDecNum = [NSDecimalNumber decimalNumberWithString:@"5.002"]; + // NSDecimalNumber не способен использовать стандартные +, -, *, / операторы, + // поэтому он предоставляет свои собственные: + [oneDecNum decimalNumberByAdding:twoDecNum]; + [oneDecNum decimalNumberBySubtracting:twoDecNum]; + [oneDecNum decimalNumberByMultiplyingBy:twoDecNum]; + [oneDecNum decimalNumberByDividingBy:twoDecNum]; + NSLog(@"%@", oneDecNum); // напечатает "10.99", т.к. NSDecimalNumber - изменяемый + // BOOL (булевый) литерал NSNumber *yesNumber = @YES; NSNumber *noNumber = @NO; - + // или + BOOL yesBool = YES; + BOOL noBool = NO; + NSLog(@"%i", yesBool); // напечатает 1 + // Массив + // Может содержать различные типы данных, но должен быть объектом Objective-C NSArray *anArray = @[@1, @2, @3, @4]; NSNumber *thirdNumber = anArray[2]; - NSLog(@"Third number = %@", thirdNumber); // Print "Third number = 3" + NSLog(@"Третье число = %@", thirdNumber); // Напечатает "Третье число = 3" + // NSMutableArray - это изменяемая версия NSArray, допускающая вам изменять + // элементы в массиве и расширять или сокращать массив. + // Удобный, но не эффективный как NSArray. + NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2]; + [mutableArray addObject:@"Привет"]; + [mutableArray addObject:@"Мир"]; + [mutableArray removeObjectAtIndex:0]; + NSLog(@"%@", [mutableArray objectAtIndex:0]); // напечатает "Мир" // Словарь - NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; - NSObject *valueObject = aDictionary[@"A Key"]; - NSLog(@"Object = %@", valueObject); // Напечатает "Object = (null)" - + NSDictionary *aDictionary = @{ @"ключ1" : @"значение1", @"ключ2" : @"значение2" }; + NSObject *valueObject = aDictionary[@"Ключ"]; + NSLog(@"Объект = %@", valueObject); // Напечатает "Объект = (null)" + // NSMutableDictionary тоже доступен, как изменяемый словарь + NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2]; + [mutableDictionary setObject:@"значение1" forKey:@"ключ1"]; + [mutableDictionary setObject:@"значение2" forKey:@"ключ2"]; + [mutableDictionary removeObjectForKey:@"ключ1"]; + + // Множество + NSSet *set = [NSSet setWithObjects:@"Привет", @"Привет", @"Мир", nil]; + NSLog(@"%@", set); // напечатает {(Hello, World)} (порядок может отличаться) + // NSMutableSet тоже доступен, как изменяемое множество + NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:2]; + [mutableSet addObject:@"Привет"]; + [mutableSet addObject:@"Привет"]; + NSLog(@"%@", mutableSet); // напечатает => {(Привет)} + /////////////////////////////////////// // Операторы /////////////////////////////////////// @@ -124,13 +189,13 @@ int main (int argc, const char * argv[]) // Условный оператор if (NO) { - NSLog(@"I am never run"); + NSLog(@"Я никогда не выполнюсь"); } else if (0) { - NSLog(@"I am also never run"); + NSLog(@"Я тоже никогда не выполнюсь"); } else { - NSLog(@"I print"); + NSLog(@"Я напечатаюсь"); } // Ветвление с множественным выбором @@ -138,15 +203,15 @@ int main (int argc, const char * argv[]) { case 0: { - NSLog(@"I am never run"); + NSLog(@"Я никогда не выполнюсь"); } break; case 1: { - NSLog(@"I am also never run"); + NSLog(@"Я тоже никогда не выполнюсь"); } break; default: { - NSLog(@"I print"); + NSLog(@"Я напечатаюсь"); } break; } @@ -170,7 +235,7 @@ int main (int argc, const char * argv[]) // "2," // "3," - // // Цикл просмотра + // Цикл просмотра NSArray *values = @[@0, @1, @2, @3]; for (NSNumber *value in values) { @@ -180,20 +245,32 @@ int main (int argc, const char * argv[]) // "2," // "3," + // Цикл for для объектов. Может использоваться с любым объектом Objective-C + for (id item in values) { + NSLog(@"%@,", item); + } // напечатает => "0," + // "1," + // "2," + // "3," + // Обработка исключений @try { // Ваше исключение здесь @throw [NSException exceptionWithName:@"FileNotFoundException" - reason:@"File Not Found on System" userInfo:nil]; + reason:@"Файл не найден в системе" userInfo:nil]; } @catch (NSException * e) { - NSLog(@"Exception: %@", e); + NSLog(@"Исключение: %@", e); } @finally { - NSLog(@"Finally"); - } // => напечатает "Exception: File Not Found on System" - // "Finally" + NSLog(@"В конце отводится время для очистки."); + } // => напечатает "Исключение: Файл не найден в системе" + // "В конце отводится время для очистки." + + // NSError - это полезные объекты для аргументов функции, чтобы заполнить их + // пользовательскими ошибками. + NSError *error = [NSError errorWithDomain:@"Неправильный эл. адрес." code:4 userInfo:nil]; /////////////////////////////////////// // Объекты @@ -203,13 +280,16 @@ int main (int argc, const char * argv[]) // Объект не является полнофункциональным пока обе части не выполнятся. MyClass *myObject = [[MyClass alloc] init]; - // В Objective-C можель ООП базируется на передаче сообщений. + // В Objective-C модель ООП базируется на передаче сообщений. // В Objective-C Вы не просто вызваете метод; вы посылаете сообщение. - [myObject instanceMethodWithParameter:@"Steve Jobs"]; + [myObject instanceMethodWithParameter:@"Стив Джобс"]; // Очищайте память, перед завершением работы программы. [pool drain]; + // Конец @autoreleasepool + } + // Конец программы. return 0; } @@ -222,63 +302,144 @@ int main (int argc, const char * argv[]) // Синтаксис объявления: // @interface ИмяКласса : ИмяКлассаРодителя <ИмплементируемыеПротоколы> // { -// Объявление переменных; +// тип имя; <= Объявление переменных; // } +// @property тип имя; <= объявление свойств // -/+ (тип) Объявление метода(ов). // @end - - -@interface MyClass : NSObject <MyProtocol> +@interface MyClass : NSObject <MyProtocol> // NSObject - это базовый класс в Objective-C. { - int count; - id data; + // Объявления экземпляров переменных (может существовать в файлах интерфейса или реализвации) + int count; // По умолчанию защищенный доступ. + @private id data; // Приватный доступ (Намного удобнее объявлять в файле реализации) NSString *name; } -// При объявлении свойств сразу генерируются геттер и сеттер -@property int count; -@property (copy) NSString *name; // Скопировать объект в ходе присвоения. -@property (readonly) id data; // Генерация только геттера +// Удобное обозначение для переменных с открытым (public) доступом +// автоматически генерируется сеттер-метод +// По умолчанию название сеттер-метода начинается с 'set' с последующим именем +// переменной из @property +@property int propInt; // Имя сеттер-метода = 'setPropInt' +@property (copy) id copyId; // (copy) => Скопировать объект в ходе присвоения. +// (readonly) => Не позволяет установить значение вне @interface +@property (readonly) NSString *roString; // Используйте @synthesize + // в @implementation, чтобы создать аксессор +// Вы можете настроить геттер и сеттер имена вместо используемого 'set'-имени по умолчанию: +@property (getter=lengthGet, setter=lengthSet:) int length; // Методы -+/- (return type)methodSignature:(Parameter Type *)parameterName; ++/- (возвращаемый тип)сигнатураМетода:(Параметр типа *)имяПараметра; // + для методов класса + (NSString *)classMethod; ++ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight; -// - для метода объекта +// - для методов объекта - (NSString *)instanceMethodWithParameter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; -@end +// Методы-конструктор с аргументом: +- (id)initWithDistance:(int)defaultDistance; +// В Objective-C имена методов очень описательные. Всегда имена методов соответствуют своим аргументам + +@end // Устанавливает конец интерфейса (interface) + + +// Чтобы обратиться к открытым (public) переменным из файла реализации, @property генерирует сеттер-метод +// автоматически. Название метода - это 'set' с последующим именем переменной из @property: +MyClass *myClass = [[MyClass alloc] init]; // создает экземпляр объекта класса MyClass +[myClass setCount:10]; +NSLog(@"%d", [myClass count]); // напечатает => 10 +// Или используйте свой геттер и сеттер методы, которые определены в @interface: +[myClass lengthSet:32]; +NSLog(@"%i", [myClass lengthGet]); // напечатает => 32 +// Для удобства вы можете использовать точечную нотацию, +// чтобы установить и получить доступ к переменным объекта: +myClass.count = 45; +NSLog(@"%i", myClass.count); // напечатает => 45 + +// Вызов методов класса: +NSString *classMethodString = [MyClass classMethod]; +MyClass *classFromName = [MyClass myClassFromName:@"Привет"]; + +// Вызов методов экземпляра: +MyClass *myClass = [[MyClass alloc] init]; // Создает экземпляр объекта MyClass +NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Привет"]; + +// Селекторы +// Это способ динамически представить методы. Используйте для вызова методов класса, передайте методы +// через функции, чтобы сказать другим классам, что они должны вызвать их и сохранить методы +// как переменные +// SEL - это тип данных. @selector() вернет селектор из предоставленного имени метода +// methodAParameterAsString:andAParameterAsNumber: - это название метода в MyClass +SEL selectorVar = @selector(methodAParameterAsString:andAParameterAsNumber:); +if ([myClass respondsToSelector:selectorVar]) { // Проверяет содержит ли класс метод + // Необходимо установить все аргументы метода в один объект, что отправить его в performSelector-функцию + NSArray *arguments = [NSArray arrayWithObjects:@"Привет", @4, nil]; + [myClass performSelector:selectorVar withObject:arguments]; // Вызывает метод +} else { + // NSStringFromSelector() вернет NSString название метода полученного селектором + NSLog(@"MyClass не содержит метод: %@", NSStringFromSelector(selectedVar)); +} // Имплементируйте методы в файле МойКласс.m: +@implementation MyClass { + long distance; // Переменная экземпляра с закрытым (private) доступом + NSNumber height; +} -@implementation MyClass +// To access a public variable from the interface file, use '_' followed by variable name: +_count = 5; // References "int count" from MyClass interface +// Access variables defined in implementation file: +distance = 18; // References "long distance" from MyClass implementation +// To use @property variable in implementation, use @synthesize to create accessor variable: +@synthesize roString = _roString; // _roString available now in @implementation + +// Called before calling any class methods or instantiating any objects ++ (void)initialize +{ + if (self == [MyClass class]) { + distance = 0; + } +} // Вызывается при высвобождении памяти под объектом - (void)dealloc { + [height release]; // Если не используется ARC, убедитесь в освобождении переменных объекта класса + [super dealloc]; // and call parent class dealloc } -// Конструкторы – это способ осздания объектов класса. -// Это обычный конструктор вызываемый при создании объекта клсааа. +// Конструкторы – это способ создания объектов класса. +// Это конструктор по умолчанию, который вызывается, когда объект инициализируется. - (id)init { - if ((self = [super init])) + if ((self = [super init])) // 'super' используется для того, чтобы обратиться к методам родительского класса { - self.count = 1; + self.count = 1; // 'self' используется для вызова самого себя } return self; } +// Можно создать конструкторы, которые содержат аргументы: +- (id)initWithDistance:(int)defaultDistance +{ + distance = defaultDistance; + return self; +} + (NSString *)classMethod { return [[self alloc] init]; } ++ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight +{ + height = defaultHeight; + return [[self alloc] init]; +} + - (NSString *)instanceMethodWithParameter:(NSString *)string { - return @"New string"; + return @"Новая строка"; } - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number @@ -286,23 +447,364 @@ int main (int argc, const char * argv[]) return @42; } +// Objective-C не содержит объявление приватных методов, но вы можете имитировать их. +// Чтобы сымитировать приватный метод, создайте метод в @implementation, но не в @interface. +- (NSNumber *)secretPrivateMethod { + return @72; +} +[self secretPrivateMethod]; // Вызывает приватный метод + // Методы объявленные в МyProtocol (см. далее) - (void)myProtocolMethod { - // имплементация + // операторы } +@end // Устанавливает конец реализации (implementation) + +/////////////////////////////////////// +// Категории +/////////////////////////////////////// +// Категория - это группа методов предназначенные для того, чтобы расширить класс. Они позволяют вам добавить новые методы +// к существующему классу для организационных целей. Это не стоит путать с подклассами. +// Подклассы предназначены для ИЗМЕНЕНИЯ функциональности объекта пока как категории ДОБАВЛЯЮТ +// функциональность в объект. +// Категории позволяют вам: +// -- Добавлять методы в существующий класс для организационных целей. +// -- Допускает вам расширять объекты Objective-C классов (напр.: NSString) добавить ваши собственные методы. +// -- Добавляет возможность создать защищенные и закрытые методы классов. +// ПРИМЕЧАНИЕ: Не переопределяйте методы базового класса в категории даже если у вас есть возможность это сделать +// to. Переопределение методов может привести к ошибкам компиляции позднее между различными категориями и это +// нарушает цель категорий, чтобы добавлять только функциональность. Вместо этого подклассы переопределяют методы. + +// Здесь простой базовый класс Car. +@interface Car : NSObject + +@property NSString *make; +@property NSString *color; + +- (void)turnOn; +- (void)accelerate; + @end -/* - * Протокол объявляет методы которые должны быть имплементированы - * Протокол не является классом. Он просто определяет интерфейс, - * который должен быть имплементирован. - */ +// И реализация базового класса Car: +#import "Car.h" + +@implementation Car + +@synthesize make = _make; +@synthesize color = _color; + +- (void)turnOn { + NSLog(@"Машина заведена."); +} +- (void)accelerate { + NSLog(@"Ускорение."); +} -@protocol MyProtocol - - (void)myProtocolMethod; @end + +// Теперь, если мы хотели создать грузовой объект, мы должны вместо создания подкласса класса Car, как это будет +// изменять функциональность Car чтобы вести себя подобно грузовику. Но давайте посмотрим, если мы хотим только добавить +// функциональность в существующий Car. Хороший пример должен быть чистить автомобиль. Итак мы создадим +// категорию для добавления его очистительных методов: +// @interface ИмяФайла: Car+Clean.h (ИмяБазовогоКласса+ИмяКатегории.h) +#import "Car.h" // Убедитесь в том, что базовый класс импортирован для расширения. + +@interface Car (Clean) // Имя категории внутри (), следующие после имени базового класса. + +- (void)washWindows; // Названия новых методов, которые мы добавляем в наш объект Car. +- (void)wax; + +@end + +// @implementation имя файла: Car+Clean.m (ИмяБазовогоКласса+ИмяКатегории.m) +#import "Car+Clean.h" // Импортируйте Очистку файл @interface категории. + +@implementation Car (Clean) + +- (void)washWindows { + NSLog(@"Окна промыли."); +} +- (void)wax { + NSLog(@"Воском натерли."); +} + +@end + +// Любой экземпляр объекта Car имеет возможность воспользоваться категорией. Все, что нужно сделать, это импортировать ее: +#import "Car+Clean.h" // Импортировать как множество различных категорий, как вы хотите использовать. +#import "Car.h" // Кроме того, необходимо импортировать базовый класс для использования его оригинальные функциональные возможности. + +int main (int argc, const char * argv[]) { + @autoreleasepool { + Car *mustang = [[Car alloc] init]; + mustang.color = @"Красный"; + mustang.make = @"Форд"; + + [mustang turnOn]; // Используйте методы из базового класса Car. + [mustang washWindows]; // Используйте методы категории Clean из класса Car. + } + return 0; +} + +// Objective-C не поддерживает объявление защищенных методов, но вы можете имитировать их. +// Создайте категорию, содержащую все защищенные методы, затем импортируйте ее только в +// @implementation-файле класса, относящегося к классу Car: +@interface Car (Protected) // Наименование категории с помощью 'Protected' +// дает знать, что методы защищенные. + +- (void)lockCar; // Здесь перечисляются методы, которые должны быть созданы +// только с помощью объектов класса Car. + +@end +// Чтобы воспользоваться защищенными методами, импортируйте категорию, затем реализуйте методы: +#import "Car+Protected.h" // Запомните, делайте импорт только в файле с @implementation. + +@implementation Car + +- (void)lockCar { + NSLog(@"Машина закрыта."); // Экземпляры класса Car не могут использовать +// метод lockCar, потому что он объявлен не в @interface. +} + +@end + +/////////////////////////////////////// +// Расширения +/////////////////////////////////////// +// Расширения позволяют вам переопределять атрибуты свойств и методов +// с открытым доступом в @interface. +// @interface имя файла: Shape.h +@interface Shape : NSObject // Расширение базового класса Shape переопределяет + // свои поля ниже. + +@property (readonly) NSNumber *numOfSides; + +- (int)getNumOfSides; + +@end +// Вы можете переопределить numOfSides-переменную или getNumOfSides-метод +// Внесение изменений с помощью расширения делается следующим образом: +// @implementation имя файла: Shape.m +#import "Shape.h" +// Расширения "живут" в том же файле, где и @implementation класса. +@interface Shape () // После имени базового класса скобки () объявляют расширение. + +@property (copy) NSNumber *numOfSides; // Делает numOfSides-свойство + // копирующим (copy) вместо свойства только для чтения (readonly). +-(NSNumber)getNumOfSides; // Изменяет метод getNumOfSides так, + // чтобы он возвращал объект NSNumber вместо типа int. +-(void)privateMethod; // Вы также можете создать новый закрытый метод + // внутри расширения. + +@end +// Главный @implementation: +@implementation Shape + +@synthesize numOfSides = _numOfSides; + +-(NSNumber)getNumOfSides { // Все операторы внутри расширения + // должны быть в @implementation. + return _numOfSides; +} +-(void)privateMethod { + NSLog(@"Закрытый метод созданный с помощью расширения."); + NSLog(@"Экземпляр Shape не может вызвать этот метод."); +} + +@end + +/////////////////////////////////////// +// Протоколы +/////////////////////////////////////// +// Протокол объявляет методы, которые могут быть реализованы с помощью +// любого класса. Протоколы сами по себе не являются классами. Они просто +// определяют интерфейс, который должен быть реализован другими объектами. +// @protocol имя файла: "CarUtilities.h" +@protocol CarUtilities <NSObject> // <NSObject> => Имя другого протокола, +// который включен в этот протокол. + @property BOOL engineOn; // Адаптирующий класс должен определить +// все @synthesize для @property и + - (void)turnOnEngine; // определить все методы. +@end +// Ниже пример класса, реализующий протокол. +#import "CarUtilities.h" // Импорт файла с @protocol. + +@interface Car : NSObject <CarUtilities> // Внутри <> имя протокола +// Здесь вам не нужно указывать @property или имена методов для CarUtilities. +// Они нужны только для @implementation. +- (void)turnOnEngineWithUtilities:(id <CarUtilities>)car; // Вы также можете +// указать тип протоколов. +@end +// В @implementation нужно реализовать все @property и методы для протокола. +@implementation Car : NSObject <CarUtilities> + +@synthesize engineOn = _engineOn; // Создайте @synthesize-оператор +// для "@property engineOn". + +- (void)turnOnEngine { // Реализуйте turnOnEngine как вам угодно. Протоколы +// не определят, + _engineOn = YES; // как вам реализовать метод, он только требует, +// чтобы вы реализовали его. +} +// Вы можете использовать протокол как данные, если вы знаете, что он реализует +// методы и переменные. +- (void)turnOnEngineWithCarUtilities:(id <CarUtilities>)objectOfSomeKind { + [objectOfSomeKind engineOn]; // У вас есть доступ к переменным объекта + [objectOfSomeKind turnOnEngine]; // и методам. + [objectOfSomeKind engineOn]; // Может или не может быть значение YES. Класс +// реализует как нужно. +} + +@end +// Экземпляры класса Car сейчас имеют доступ к протоколу. +Car *carInstance = [[Car alloc] init]; +[carInstance setEngineOn:NO]; +[carInstance turnOnEngine]; +if ([carInstance engineOn]) { + NSLog(@"Двигатель запущен."); // напечатает => "Двигатель запущен." +} +// Убедитись в том, что объект типа 'id' реализует протокол перед вызовом методов протокола: +if ([myClass conformsToProtocol:@protocol(CarUtilities)]) { + NSLog(@"Не работает, т.к. класс MyClass не реализует протокол CarUtilities."); +} else if ([carInstance conformsToProtocol:@protocol(CarUtilities)]) { + NSLog(@"Работает как класс Car, который реализует протокол CarUtilities."); +} +// Категории тоже могут реализовать протоколы: +// @interface Car (CarCategory) <CarUtilities> +// Вы можете реализовать много протоколов: +// @interface Car : NSObject <CarUtilities, CarCleaning> +// ЗАМЕЧАНИЕ: Если два или более протоколов полагаются друг на друга, +// убедитесь, что они ранее объявлены: +#import "Brother.h" + +@protocol Brother; // Оператор раннего объявления. Без него компилятор +// выдаст ошибку. + +@protocol Sister <NSObject> + +- (void)beNiceToBrother:(id <Brother>)brother; + +@end + +// Рассмотрите проблему, где протокол Sister полагается на протокол Brother, +// а Brother полагается на Sister. +#import "Sister.h" + +@protocol Sister; // Эти строки предотвращают рекурсию, решая этим проблему. + +@protocol Brother <NSObject> + +- (void)beNiceToSister:(id <Sister>)sister; + +@end + + +/////////////////////////////////////// +// Блоки +/////////////////////////////////////// +// Блоки - это операторы кода, наподобие функции, которую возможно использовать +// как данные. +// Ниже простой блок с целочисленным аргументом, и возвращает аргумент плюс 4. +int (^addUp)(int n); // Объявите переменную, чтобы сохранить блок. +void (^noParameterBlockVar)(void); // Пример объявления блока-переменной +// без аргументов. +// Блоки имею доступ к переменным в той же области видимости. Но переменные +// будут только для чтения, и значения переданных в блок станут значением +// переменной, когда блок создастся. +int outsideVar = 17; // Если мы редактируем outsideVar после объявления addUp, +// outsideVar остается равным 17. +__block long mutableVar = 3; // __block делают переменные перезаписываемыми +// в блоках, в отличие от outsideVar. +addUp = ^(int n) { // Удалите (int n) в блоке, чтобы не принимать +// какие-либо параметры. + NSLog(@"Вы можете иметь столько строк в блоке, сколько вы хотели."); + NSSet *blockSet; // Также вы можете объявить локальные переменные. + mutableVar = 32; // Присвоить новое значение к __block-переменной. + return n + outsideVar; // Необязательный оператор возврата. +} +int addUp = add(10 + 16); // Вызывает блок кода с аргументами. +// Блоки часто используются как аргументы функции, чтобы позже их вызвать, или +// как функции обратного вызова (callbacks). +@implementation BlockExample : NSObject + +- (void)runBlock:(void (^)(NSString))block { + NSLog(@"В аргументе блок ничего не возвращает и принимает NSString-объект."); + block(@"Аргумент передан блоку на исполнение."); // Вызов блока. +} + +@end + + +/////////////////////////////////////// +// Управление памятью +/////////////////////////////////////// +/* +Для каждого объекта, используемого в приложении, должна быть выделена память +для таких объектов. Когда приложение прекращает использование объекта, память +должна быть освобождена, чтобы гарантировать эффективность приложения. +Objective-C не использует сборщик мусора, а вместо этого применяет подсчет ссылок. +Пока существует по крайней мере одна ссылка на объект (также называется +"владение" объектом), то объект будет доступен к использованию (еще известно +как "право владения"). + +Когда экземпляр владеет объектом, его ссылка увеличивается на один. Когда +объекта освобождается, счетчик ссылки уменьшается на один. Когда счетчик ссылки +равен нулю, объект удаляется из памяти. + +Над всеми объектами взаимодействуют, следуя паттерну: +(1) создание объекта, (2) использование объекта, (3) затем освобождение объекта из памяти. +*/ + +MyClass *classVar = [MyClass alloc]; // 'alloc' устанавливает счетчик ссылки +// объекта classVar на 1 и возвращает указатель на объект. +[classVar release]; // Уменьшает счетчик ссылки объекта classVar +// 'retain' заявляет право собственности на существующий экземпляр объекта +// и увеличивает счетчик ссылки. Затем вернет указатель на объект. +MyClass *newVar = [classVar retain]; // Если classVar освободится, объект +// останется в памяти, потому что newVar - владелец +[classVar autorelease]; // Удалит право на владение объектом +// в конце @autoreleasepool блока. Вернет указатель на объект. + +// @property может использовать 'retain' и 'assign' тоже для маленького +// удобного определения +@property (retain) MyClass *instance; // Освободит старое значение и сохранит +// одно новое (строгая ссылка) +@property (assign) NSSet *set; // Укажет на новое значение +// без сохранения/освобождения старого значения (слабая ссылка) + +// Автоматический подсчет ссылок (ARC) +// Управление памятью может быть трудным, поэтому в Xcode 4.2 и iOS 4 введен +// автоматический подсчет ссылок (ARC). +// ARC - это особенность компилятора, который помещает "retain", "release" +// и "autorelease" автоматически за вас тогда, когда используется ARC, +// вам не нужно больше обращаться к "retain", "relase" или "autorelease" +MyClass *arcMyClass = [[MyClass alloc] init]; +// ... код, использующий объект arcMyClass +// Без ARC, вам нужно было бы вызвать: [arcMyClass release] после того, как вы +// завершите работу с объектом arcMyClass. Но с ARC, +// теперь этого не нужно делать. Он будет помещать release-вызов за вас + +// Что касается 'assign' и 'retain' @property атрибутов, в ARC вы должны +// использовать 'weak' и 'strong' +@property (weak) MyClass *weakVar; // 'weak' не принимает право на владение +// объектом. Если исходный счетчик ссылки экземпляра обнуляется, +// weakVar-свойство автоматически примет значение nil, +// во избежание падения приложения +@property (strong) MyClass *strongVar; // 'strong' принимает право на владение +// объектом. Гарантирует, что объект останится в памяти для использования + +// Для обычных переменных (не объявленных с помощью @property), используйте +// следующий способ: +__strong NSString *strongString; // По умолчанию. Переменная сохраняется в памяти, +// пока она не покинет область видимости +__weak NSSet *weakSet; // Слабая ссылка на существующий объект. Когда существующий +// объект освобождается, weakSet принимает nil +__unsafe_unretained NSArray *unsafeArray; // Похож на __weak, но unsafeArray +// не принимает nil, когда существующий объект освобождается + ``` ## На почитать diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown index 8b292edd..d8a02d36 100644 --- a/ruby-ecosystem.html.markdown +++ b/ruby-ecosystem.html.markdown @@ -54,7 +54,7 @@ the community has moved to at least 1.9.2 or 1.9.3. ## Ruby Implementations The Ruby ecosystem enjoys many different implementations of Ruby, each with -unique strengths and states of compatability. To be clear, the different +unique strengths and states of compatibility. To be clear, the different implementations are written in different languages, but *they are all Ruby*. Each implementation has special hooks and extra features, but they all run normal Ruby files well. For instance, JRuby is written in Java, but you do diff --git a/ruby.html.markdown b/ruby.html.markdown index 66a0774d..7bd28d86 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -173,6 +173,8 @@ array[1..3] #=> [2, 3, 4] # Add to an array like this array << 6 #=> [1, 2, 3, 4, 5, 6] +# Or like this +array.push(6) #=> [1, 2, 3, 4, 5, 6] # Check if an item exists in an array array.include?(1) #=> true diff --git a/rust.html.markdown b/rust.html.markdown index dd03acdd..4fbd6144 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -281,7 +281,7 @@ fn main() { println!("{}", var); // Unlike `box`, `var` can still be used println!("{}", *ref_var); // var = 5; // this would not compile because `var` is borrowed - // *ref_var = 6; // this would too, because `ref_var` is an immutable reference + // *ref_var = 6; // this would not too, because `ref_var` is an immutable reference // Mutable reference // While a value is mutably borrowed, it cannot be accessed at all. diff --git a/tcl.html.markdown b/tcl.html.markdown index 198f675e..af2911c9 100755..100644 --- a/tcl.html.markdown +++ b/tcl.html.markdown @@ -121,7 +121,8 @@ puts lots\nof\n\n\n\n\n\nnewlines # A word enclosed in braces is not subject to any special interpretation or -# substitutions, except that a backslash before a brace is not counted when look#ing for the closing brace +# substitutions, except that a backslash before a brace is not counted when +# looking for the closing brace set somevar { This is a literal $ sign, and this \} escaped brace remains uninterpreted @@ -148,6 +149,9 @@ set greeting "Hello, [set {first name}]" # To promote the words within a word to individual words of the current # command, use the expansion operator, "{*}". +``` + +```tcl set {*}{name Neo} # is equivalent to @@ -163,7 +167,7 @@ set greeting "Hello, $person(name)" # A namespace holds commands and variables namespace eval people { namespace eval person1 { - set name Neo + variable name Neo } } @@ -189,7 +193,10 @@ set greeting "Hello $people::person1::name" namespace delete :: -# Because of name resolution behaviour, it's safer to use the "variable" command to declare or to assign a value to a namespace. +# Because of name resolution behaviour, it's safer to use the "variable" command to +# declare or to assign a value to a namespace. If a variable called "name" already +# exists in the global namespace, using "set" here will assign a value to the global variable +# instead of creating a new variable in the local namespace. namespace eval people { namespace eval person1 { variable name Neo @@ -256,7 +263,7 @@ proc greet greeting\ name return\ \"Hello,\ \$name! proc fold {cmd args} { set res 0 foreach arg $args { - set res [cmd $res $arg] + set res [$cmd $res $arg] } } fold ::tcl::mathop::* 5 3 3 ;# -> 45 diff --git a/visualbasic.html.markdown b/visualbasic.html.markdown index 00d61843..f9906e96 100644 --- a/visualbasic.html.markdown +++ b/visualbasic.html.markdown @@ -236,7 +236,7 @@ Module Module1 'Nine Private Sub IfElseStatement() Console.Title = "If / Else Statement | Learn X in Y Minutes" - 'Sometimes its important to consider more than two alternatives. + 'Sometimes it is important to consider more than two alternatives. 'Sometimes there are a good few others. 'When this is the case, more than one if statement would be required. 'An if statement is great for vending machines. Where the user enters a code. diff --git a/xml.html.markdown b/xml.html.markdown index fce1a3a4..059ea132 100644 --- a/xml.html.markdown +++ b/xml.html.markdown @@ -49,10 +49,11 @@ Unlike HTML, XML does not specify how to display or to format data, just carry i <!-- XML carries two kind of data: 1 - Attributes -> That's metadata about a node. Usually, the XML parser uses this information to store the data properly. - It is characterized by appearing in parenthesis within the opening tag + It is characterized by appearing with the format name="value" within the opening + tag. 2 - Elements -> That's pure data. That's what the parser will retrieve from the XML file. - Elements appear between the open and close tags, without parenthesis. --> + Elements appear between the open and close tags. --> <!-- Below, an element with two attributes --> diff --git a/zh-cn/bash-cn.html.markdown b/zh-cn/bash-cn.html.markdown index 558d9110..d85e5b8f 100644 --- a/zh-cn/bash-cn.html.markdown +++ b/zh-cn/bash-cn.html.markdown @@ -258,7 +258,7 @@ help return help source help . -# 用 mam 指令阅读相关的 Bash 手册 +# 用 man 指令阅读相关的 Bash 手册 apropos bash man 1 bash man bash diff --git a/zh-cn/go-cn.html.markdown b/zh-cn/go-cn.html.markdown index 3a461efe..49224085 100644 --- a/zh-cn/go-cn.html.markdown +++ b/zh-cn/go-cn.html.markdown @@ -239,7 +239,7 @@ func learnConcurrency() { go inc(0, c) // go is a statement that starts a new goroutine. go inc(10, c) go inc(-805, c) - // 从channel中独处结果并打印。 + // 从channel中读取结果并打印。 // 打印出什么东西是不可预知的。 fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是读操作。 diff --git a/zh-cn/javascript-cn.html.markdown b/zh-cn/javascript-cn.html.markdown index b450ab84..dfeb2012 100644 --- a/zh-cn/javascript-cn.html.markdown +++ b/zh-cn/javascript-cn.html.markdown @@ -402,7 +402,7 @@ myObj.meaningOfLife; // = 42 // 函数也可以工作。 myObj.myFunc() // = "hello world!" -// 当然,如果你要访问的成员在原型当中也没有定义的话,解释器就会去找原型的原型,以此类堆。 +// 当然,如果你要访问的成员在原型当中也没有定义的话,解释器就会去找原型的原型,以此类推。 myPrototype.__proto__ = { myBoolean: true }; diff --git a/zh-cn/markdown-cn.html.markdown b/zh-cn/markdown-cn.html.markdown index b1143dac..b633714d 100644 --- a/zh-cn/markdown-cn.html.markdown +++ b/zh-cn/markdown-cn.html.markdown @@ -69,7 +69,7 @@ __此文本也是__ <!-- 如果你插入一个 HTML中的<br />标签,你可以在段末加入两个以上的空格, 然后另起一段。--> -此段落结尾有两个空格(选中以显示)。 +此段落结尾有两个空格(选中以显示)。 上文有一个 <br /> ! |