diff options
93 files changed, 12444 insertions, 1480 deletions
diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md index 022dedab..96278da9 100644 --- a/ISSUE_TEMPLATE.md +++ b/ISSUE_TEMPLATE.md @@ -1,3 +1,12 @@ +## Is this a major issue that you cannot fix? + +**Being a community driven documents of languages and tools,"YOUR" contributions +are also important. +If the issue you're reporting is trivial to report to maintainers why not contribute +to fix it. In that way, you will have contributed to an awesome open-source project. +The changes can be typo fix, fixing of data in examples or grammar fix. If you found it, +why not do it and take full credit for it?** + Make sure the issue title is prepended with '[language/lang-code]' if the language is already on the site. If it's a request for a new language, use: '[Request] [language/lang-code]' diff --git a/asymptotic-notation.html.markdown b/asymptotic-notation.html.markdown index 6a6df968..a1dfe9e1 100644 --- a/asymptotic-notation.html.markdown +++ b/asymptotic-notation.html.markdown @@ -155,7 +155,7 @@ Small-o, commonly written as **o**, is an Asymptotic Notation to denote the upper bound (that is not asymptotically tight) on the growth rate of runtime of an algorithm. -`f(n)` is o(g(n)), if for some real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is < `c g(n)` +`f(n)` is o(g(n)), if for all real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is < `c g(n)` for every input size n (n > n<sub>0</sub>). The definitions of O-notation and o-notation are similar. The main difference @@ -168,7 +168,7 @@ Small-omega, commonly written as **ω**, is an Asymptotic Notation to denote the lower bound (that is not asymptotically tight) on the growth rate of runtime of an algorithm. -`f(n)` is ω(g(n)), if for some real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is > `c g(n)` +`f(n)` is ω(g(n)), if for all real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is > `c g(n)` for every input size n (n > n<sub>0</sub>). The definitions of Ω-notation and ω-notation are similar. The main difference diff --git a/awk.html.markdown b/awk.html.markdown index e3ea6318..3d2c4ccb 100644 --- a/awk.html.markdown +++ b/awk.html.markdown @@ -6,14 +6,15 @@ contributors: --- -AWK is a standard tool on every POSIX-compliant UNIX system. It's like a -stripped-down Perl, perfect for text-processing tasks and other scripting -needs. It has a C-like syntax, but without semicolons, manual memory -management, or static typing. It excels at text processing. You can call to it -from a shell script, or you can use it as a stand-alone scripting language. - -Why use AWK instead of Perl? Mostly because AWK is part of UNIX. You can always -count on it, whereas Perl's future is in question. AWK is also easier to read +AWK is a standard tool on every POSIX-compliant UNIX system. It's like +flex/lex, from the command-line, perfect for text-processing tasks and +other scripting needs. It has a C-like syntax, but without mandatory +semicolons (although, you should use them anyway, because they are required +when you're writing one-liners, something AWK excells at), manual memory +management, or static typing. It excels at text processing. You can call to +it from a shell script, or you can use it as a stand-alone scripting language. + +Why use AWK instead of Perl? Readability. AWK is easier to read than Perl. For simple text-processing scripts, particularly ones that read files line by line and split on delimiters, AWK is probably the right tool for the job. @@ -23,8 +24,23 @@ the job. # Comments are like this -# AWK programs consist of a collection of patterns and actions. The most -# important pattern is called BEGIN. Actions go into brace blocks. + +# AWK programs consist of a collection of patterns and actions. +pattern1 { action; } # just like lex +pattern2 { action; } + +# There is an implied loop and AWK automatically reads and parses each +# record of each file supplied. Each record is split by the FS delimiter, +# which defaults to white-space (multiple spaces,tabs count as one) +# You cann assign FS either on the command line (-F C) or in your BEGIN +# pattern + +# One of the special patterns is BEGIN. The BEGIN pattern is true +# BEFORE any of the files are read. The END pattern is true after +# an End-of-file from the last file (or standard-in if no files specified) +# There is also an output field separator (OFS) that you can assign, which +# defaults to a single space + BEGIN { # BEGIN will run at the beginning of the program. It's where you put all @@ -32,114 +48,116 @@ BEGIN { # have no text files, then think of BEGIN as the main entry point. # Variables are global. Just set them or use them, no need to declare.. - count = 0 + count = 0; # Operators just like in C and friends - a = count + 1 - b = count - 1 - c = count * 1 - d = count / 1 # integer division - e = count % 1 # modulus - f = count ^ 1 # exponentiation - - a += 1 - b -= 1 - c *= 1 - d /= 1 - e %= 1 - f ^= 1 + a = count + 1; + b = count - 1; + c = count * 1; + d = count / 1; # integer division + e = count % 1; # modulus + f = count ^ 1; # exponentiation + + a += 1; + b -= 1; + c *= 1; + d /= 1; + e %= 1; + f ^= 1; # Incrementing and decrementing by one - a++ - b-- + a++; + b--; # As a prefix operator, it returns the incremented value - ++a - --b + ++a; + --b; # Notice, also, no punctuation such as semicolons to terminate statements # Control statements if (count == 0) - print "Starting with count of 0" + print "Starting with count of 0"; else - print "Huh?" + print "Huh?"; # Or you could use the ternary operator - print (count == 0) ? "Starting with count of 0" : "Huh?" + print (count == 0) ? "Starting with count of 0" : "Huh?"; # Blocks consisting of multiple lines use braces while (a < 10) { print "String concatenation is done" " with a series" " of" - " space-separated strings" - print a + " space-separated strings"; + print a; - a++ + a++; } for (i = 0; i < 10; i++) - print "Good ol' for loop" + print "Good ol' for loop"; # As for comparisons, they're the standards: - a < b # Less than - a <= b # Less than or equal - a != b # Not equal - a == b # Equal - a > b # Greater than - a >= b # Greater than or equal + # a < b # Less than + # a <= b # Less than or equal + # a != b # Not equal + # a == b # Equal + # a > b # Greater than + # a >= b # Greater than or equal # Logical operators as well - a && b # AND - a || b # OR + # a && b # AND + # a || b # OR # In addition, there's the super useful regular expression match if ("foo" ~ "^fo+$") - print "Fooey!" + print "Fooey!"; if ("boo" !~ "^fo+$") - print "Boo!" + print "Boo!"; # Arrays - arr[0] = "foo" - arr[1] = "bar" - # Unfortunately, there is no other way to initialize an array. Ya just - # gotta chug through every value line by line like that. - - # You also have associative arrays - assoc["foo"] = "bar" - assoc["bar"] = "baz" + arr[0] = "foo"; + arr[1] = "bar"; + + # You can also initialize an array with the built-in function split() + + n = split("foo:bar:baz", arr, ":"); + + # You also have associative arrays (actually, they're all associative arrays) + assoc["foo"] = "bar"; + assoc["bar"] = "baz"; # And multi-dimensional arrays, with some limitations I won't mention here - multidim[0,0] = "foo" - multidim[0,1] = "bar" - multidim[1,0] = "baz" - multidim[1,1] = "boo" + multidim[0,0] = "foo"; + multidim[0,1] = "bar"; + multidim[1,0] = "baz"; + multidim[1,1] = "boo"; # You can test for array membership if ("foo" in assoc) - print "Fooey!" + print "Fooey!"; # You can also use the 'in' operator to traverse the keys of an array for (key in assoc) - print assoc[key] + print assoc[key]; # The command line is in a special array called ARGV for (argnum in ARGV) - print ARGV[argnum] + print ARGV[argnum]; # You can remove elements of an array # This is particularly useful to prevent AWK from assuming the arguments # are files for it to process - delete ARGV[1] + delete ARGV[1]; # The number of command line arguments is in a variable called ARGC - print ARGC + print ARGC; # AWK has several built-in functions. They fall into three categories. I'll # demonstrate each of them in their own functions, defined later. - return_value = arithmetic_functions(a, b, c) - string_functions() - io_functions() + return_value = arithmetic_functions(a, b, c); + string_functions(); + io_functions(); } # Here's how you define a function @@ -159,26 +177,26 @@ function arithmetic_functions(a, b, c, d) { # Now, to demonstrate the arithmetic functions # Most AWK implementations have some standard trig functions - localvar = sin(a) - localvar = cos(a) - localvar = atan2(a, b) # arc tangent of b / a + localvar = sin(a); + localvar = cos(a); + localvar = atan2(b, a); # arc tangent of b / a # And logarithmic stuff - localvar = exp(a) - localvar = log(a) + localvar = exp(a); + localvar = log(a); # Square root - localvar = sqrt(a) + localvar = sqrt(a); # Truncate floating point to integer - localvar = int(5.34) # localvar => 5 + localvar = int(5.34); # localvar => 5 # Random numbers - srand() # Supply a seed as an argument. By default, it uses the time of day - localvar = rand() # Random number between 0 and 1. + srand(); # Supply a seed as an argument. By default, it uses the time of day + localvar = rand(); # Random number between 0 and 1. # Here's how to return a value - return localvar + return localvar; } function string_functions( localvar, arr) { @@ -188,61 +206,66 @@ function string_functions( localvar, arr) { # Search and replace, first instance (sub) or all instances (gsub) # Both return number of matches replaced - localvar = "fooooobar" - sub("fo+", "Meet me at the ", localvar) # localvar => "Meet me at the bar" - gsub("e+", ".", localvar) # localvar => "m..t m. at th. bar" + localvar = "fooooobar"; + sub("fo+", "Meet me at the ", localvar); # localvar => "Meet me at the bar" + gsub("e+", ".", localvar); # localvar => "m..t m. at th. bar" # Search for a string that matches a regular expression # index() does the same thing, but doesn't allow a regular expression - match(localvar, "t") # => 4, since the 't' is the fourth character + match(localvar, "t"); # => 4, since the 't' is the fourth character # Split on a delimiter - split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"] + n = split("foo-bar-baz", arr, "-"); # a[1] = "foo"; a[2] = "bar"; a[3] = "baz"; n = 3 # Other useful stuff - sprintf("%s %d %d %d", "Testing", 1, 2, 3) # => "Testing 1 2 3" - substr("foobar", 2, 3) # => "oob" - substr("foobar", 4) # => "bar" - length("foo") # => 3 - tolower("FOO") # => "foo" - toupper("foo") # => "FOO" + sprintf("%s %d %d %d", "Testing", 1, 2, 3); # => "Testing 1 2 3" + substr("foobar", 2, 3); # => "oob" + substr("foobar", 4); # => "bar" + length("foo"); # => 3 + tolower("FOO"); # => "foo" + toupper("foo"); # => "FOO" } function io_functions( localvar) { # You've already seen print - print "Hello world" + print "Hello world"; # There's also printf - printf("%s %d %d %d\n", "Testing", 1, 2, 3) + printf("%s %d %d %d\n", "Testing", 1, 2, 3); # AWK doesn't have file handles, per se. It will automatically open a file # handle for you when you use something that needs one. The string you used # for this can be treated as a file handle, for purposes of I/O. This makes - # it feel sort of like shell scripting: + # it feel sort of like shell scripting, but to get the same output, the string + # must match exactly, so use a vaiable: + + outfile = "/tmp/foobar.txt"; - print "foobar" >"/tmp/foobar.txt" + print "foobar" > outfile; - # Now the string "/tmp/foobar.txt" is a file handle. You can close it: - close("/tmp/foobar.txt") + # Now the string outfile is a file handle. You can close it: + close(outfile); # Here's how you run something in the shell - system("echo foobar") # => prints foobar + system("echo foobar"); # => prints foobar # Reads a line from standard input and stores in localvar - getline localvar + getline localvar; - # Reads a line from a pipe - "echo foobar" | getline localvar # localvar => "foobar" - close("echo foobar") + # Reads a line from a pipe (again, use a string so you close it properly) + cmd = "echo foobar"; + cmd | getline localvar; # localvar => "foobar" + close(cmd); # Reads a line from a file and stores in localvar - getline localvar <"/tmp/foobar.txt" - close("/tmp/foobar.txt") + infile = "/tmp/foobar.txt"; + getline localvar < infile; + close(infile); } # As I said at the beginning, AWK programs consist of a collection of patterns -# and actions. You've already seen the all-important BEGIN pattern. Other +# and actions. You've already seen the BEGIN pattern. Other # patterns are used only if you're processing lines from files or standard # input. # @@ -257,7 +280,7 @@ function io_functions( localvar) { # expression, /^fo+bar$/, and will be skipped for any line that fails to # match it. Let's just print the line: - print + print; # Whoa, no argument! That's because print has a default argument: $0. # $0 is the name of the current line being processed. It is created @@ -268,16 +291,16 @@ function io_functions( localvar) { # does. And, like the shell, each field can be access with a dollar sign # This will print the second and fourth fields in the line - print $2, $4 + print $2, $4; # AWK automatically defines many other variables to help you inspect and # process each line. The most important one is NF # Prints the number of fields on this line - print NF + print NF; # Print the last field on this line - print $NF + print $NF; } # Every pattern is actually a true/false test. The regular expression in the @@ -286,7 +309,7 @@ function io_functions( localvar) { # currently processing. Thus, the complete version of it is this: $0 ~ /^fo+bar$/ { - print "Equivalent to the last pattern" + print "Equivalent to the last pattern"; } a > 0 { @@ -315,10 +338,10 @@ a > 0 { BEGIN { # First, ask the user for the name - print "What name would you like the average age for?" + print "What name would you like the average age for?"; # Get a line from standard input, not from files on the command line - getline name <"/dev/stdin" + getline name < "/dev/stdin"; } # Now, match every line whose first field is the given name @@ -335,8 +358,8 @@ $1 == name { # ...etc. There are plenty more, documented in the man page. # Keep track of a running total and how many lines matched - sum += $3 - nlines++ + sum += $3; + nlines++; } # Another special pattern is called END. It will run after processing all the @@ -348,7 +371,7 @@ $1 == name { END { if (nlines) - print "The average age for " name " is " sum / nlines + print "The average age for " name " is " sum / nlines; } ``` @@ -357,3 +380,4 @@ Further Reading: * [Awk tutorial](http://www.grymoire.com/Unix/Awk.html) * [Awk man page](https://linux.die.net/man/1/awk) * [The GNU Awk User's Guide](https://www.gnu.org/software/gawk/manual/gawk.html) GNU Awk is found on most Linux systems. +* [AWK one-liner collection](http://tuxgraphics.org/~guido/scripts/awk-one-liner.html) diff --git a/bash.html.markdown b/bash.html.markdown index 3f3e49eb..cb805da7 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -25,7 +25,7 @@ Nearly all examples below can be a part of a shell script or executed directly i [Read more here.](http://www.gnu.org/software/bash/manual/bashref.html) ```bash -#!/bin/bash +#!/usr/bin/env bash # First line of the script is shebang which tells the system how to execute # the script: http://en.wikipedia.org/wiki/Shebang_(Unix) # As you already figured, comments start with #. Shebang is also a comment. @@ -74,7 +74,7 @@ echo ${Variable/Some/A} # => A string # Substring from a variable Length=7 -echo ${Variable:0:Length} # => Some st +echo ${Variable:0:$Length} # => Some st # This will return only the first 7 characters of the value # Default value for variable diff --git a/c.html.markdown b/c.html.markdown index 0c6df413..bf93dcf5 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -9,6 +9,7 @@ contributors: - ["Zachary Ferguson", "https://github.io/zfergus2"] - ["himanshu", "https://github.com/himanshu81494"] - ["Joshua Li", "https://github.com/JoshuaRLi"] + - ["Dragos B. Chirila", "https://github.com/dchirila"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -18,7 +19,7 @@ it more than makes up for it with raw speed. Just be aware of its manual memory management and C will take you as far as you need to go. > **About compiler flags** -> +> > By default, gcc and clang are pretty quiet about compilation warnings and > errors, which can be very useful information. Explicitly using stricter > compiler flags is recommended. Here are some recommended defaults: @@ -89,6 +90,8 @@ int main (int argc, char** argv) // All variables MUST be declared at the top of the current block scope // we declare them dynamically along the code for the sake of the tutorial + // (however, C99-compliant compilers allow declarations near the point where + // the value is used) // ints are usually 4 bytes int x_int = 0; @@ -101,7 +104,7 @@ int main (int argc, char** argv) char y_char = 'y'; // Char literals are quoted with '' // longs are often 4 to 8 bytes; long longs are guaranteed to be at least - // 64 bits + // 8 bytes long x_long = 0; long long x_long_long = 0; @@ -141,6 +144,17 @@ int main (int argc, char** argv) // You can initialize an array to 0 thusly: char my_array[20] = {0}; + // where the "{0}" part is called an "array initializer". + // NOTE that you get away without explicitly declaring the size of the array, + // IF you initialize the array on the same line. So, the following declaration + // is equivalent: + char my_array[] = {0}; + // BUT, then you have to evaluate the size of the array at run-time, like this: + size_t my_array_size = sizeof(my_array) / sizeof(my_array[0]); + // WARNING If you adopt this approach, you should evaluate the size *before* + // you begin passing the array to function (see later discussion), because + // arrays get "downgraded" to raw pointers when they are passed to functions + // (so the statement above will produce the wrong result inside the function). // Indexing an array is like other languages -- or, // rather, other languages are like C @@ -374,8 +388,8 @@ int main (int argc, char** argv) // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from <limits.h> // Integral types can be cast to floating-point types, and vice-versa. - printf("%f\n", (float)100); // %f formats a float - printf("%lf\n", (double)100); // %lf formats a double + printf("%f\n", (double) 100); // %f always formats a double... + printf("%f\n", (float) 100); // ...even with a float. printf("%d\n", (char)100.0); /////////////////////////////////////// @@ -433,7 +447,7 @@ int main (int argc, char** argv) // or when it's the argument of the `sizeof` or `alignof` operator: int arraythethird[10]; int *ptr = arraythethird; // equivalent with int *ptr = &arr[0]; - printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr); + printf("%zu, %zu\n", sizeof(arraythethird), sizeof(ptr)); // probably prints "40, 4" or "40, 8" // Pointers are incremented and decremented based on their type @@ -449,7 +463,7 @@ int main (int argc, char** argv) for (xx = 0; xx < 20; xx++) { *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) - + // Be careful passing user-provided values to malloc! If you want // to be safe, you can use calloc instead (which, unlike malloc, also zeros out the memory) int* my_other_ptr = calloc(20, sizeof(int)); @@ -522,9 +536,11 @@ Example: in-place string reversal void str_reverse(char *str_in) { char tmp; - int ii = 0; + size_t ii = 0; size_t len = strlen(str_in); // `strlen()` is part of the c standard library - for (ii = 0; ii < len / 2; ii++) { + // NOTE: length returned by `strlen` DOESN'T include the + // terminating NULL byte ('\0') + for (ii = 0; ii < len / 2; ii++) { // in C99 you can directly declare type of `ii` here tmp = str_in[ii]; str_in[ii] = str_in[len - ii - 1]; // ii-th char from end str_in[len - ii - 1] = tmp; @@ -703,7 +719,8 @@ typedef void (*my_fnp_type)(char *); "%3.2f"; // minimum 3 digits left and 2 digits right decimal float "%7.4s"; // (can do with strings too) "%c"; // char -"%p"; // pointer +"%p"; // pointer. NOTE: need to (void *)-cast the pointer, before passing + // it as an argument to `printf`. "%x"; // hexadecimal "%o"; // octal "%%"; // prints % diff --git a/chapel.html.markdown b/chapel.html.markdown index 9190f462..354cd832 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -100,7 +100,7 @@ writeln(varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg); // be made to alias a variable other than the variable it is initialized with. // Here, refToActual refers to actual. var actual = 10; -ref refToActual = actual; +ref refToActual = actual; writeln(actual, " == ", refToActual); // prints the same value actual = -123; // modify actual (which refToActual refers to) writeln(actual, " == ", refToActual); // prints the same value @@ -444,7 +444,7 @@ arrayFromLoop = [value in arrayFromLoop] value + 1; // Procedures -// Chapel procedures have similar syntax functions in other languages. +// Chapel procedures have similar syntax functions in other languages. proc fibonacci(n : int) : int { if n <= 1 then return n; return fibonacci(n-1) + fibonacci(n-2); @@ -893,7 +893,6 @@ foo(); // We can declare a main procedure, but all the code above main still gets // executed. proc main() { - writeln("PARALLELISM START"); // A begin statement will spin the body of that statement off // into one new task. @@ -1141,11 +1140,13 @@ to see if more topics have been added or more tutorials created. Your input, questions, and discoveries are important to the developers! ----------------------------------------------------------------------- -The Chapel language is still in-development (version 1.16.0), so there are +The Chapel language is still in active development, so there are occasional hiccups with performance and language features. The more information you give the Chapel development team about issues you encounter or features you -would like to see, the better the language becomes. Feel free to email the team -and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). +would like to see, the better the language becomes. +There are several ways to interact with the developers: ++ [Gitter chat](https://gitter.im/chapel-lang/chapel) ++ [sourceforge email lists](https://sourceforge.net/p/chapel/mailman) If you're really interested in the development of the compiler or contributing to the project, [check out the master GitHub repository](https://github.com/chapel-lang/chapel). @@ -1154,12 +1155,14 @@ It is under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0) Installing the Compiler ----------------------- +[The Official Chapel documentation details how to download and compile the Chapel compiler.](https://chapel-lang.org/docs/usingchapel/QUICKSTART.html) + 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 it's as easy as - 1. `tar -xvf chapel-1.16.0.tar.gz` - 2. `cd chapel-1.16.0` + 1. `tar -xvf chapel-<VERSION>.tar.gz` + 2. `cd chapel-<VERSION>` 3. `source util/setchplenv.bash # or .sh or .csh or .fish` 4. `make` 5. `make check # optional` diff --git a/citron.html.markdown b/citron.html.markdown new file mode 100644 index 00000000..bd3c398c --- /dev/null +++ b/citron.html.markdown @@ -0,0 +1,212 @@ +--- +language: citron +filename: learncitron.ctr +contributors: + - ["AnotherTest", ""] +lang: en-us +--- +```ruby +# Comments start with a '#' +# All comments encompass a single line + +########################################### +## 1. Primitive Data types and Operators +########################################### + +# You have numbers +3. # 3 + +# Numbers are all doubles in interpreted mode + +# Mathematical operator precedence is not respected. +# binary 'operators' are evaluated in ltr order +1 + 1. # 2 +8 - 4. # 4 +10 + 2 * 3. # 36 + +# Division is always floating division +35 / 2 # 17.5. + +# Integer division is non-trivial, you may use floor +(35 / 2) floor # 17. + +# Booleans are primitives +True. +False. + +# Boolean messages +True not. # False +False not. # True +1 = 1. # True +1 !=: 1. # False +1 < 10. # True + +# Here, `not` is a unary message to the object `Boolean` +# Messages are comparable to instance method calls +# And they have three different forms: +# 1. Unary messages: Length > 1, and they take no arguments: + False not. +# 2. Binary Messages: Length = 1, and they take a single argument: + False & True. +# 3. Keyword messages: must have at least one ':', they take as many arguments +# as they have `:` s + False either: 1 or: 2. # 2 + +# Strings +'This is a string'. +'There are no character types exposed to the user'. +# "You cannot use double quotes for strings" <- Error + +# Strins can be summed +'Hello, ' + 'World!'. # 'Hello, World!' + +# Strings allow access to their characters +'This is a beautiful string' at: 0. # 'T' + +########################################### +## intermission: Basic Assignment +########################################### + +# You may assign values to the current scope: +var name is value. # assignes `value` into `name` + +# You may also assign values into the current object's namespace +my name is value. # assigns `value` into the current object's `name` property + +# Please note that these names are checked at compile (read parse if in interpreted mode) time +# but you may treat them as dynamic assignments anyway + +########################################### +## 2. Lists(Arrays?) and Tuples +########################################### + +# Arrays are allowed to have multiple types +Array new < 1 ; 2 ; 'string' ; Nil. # Array new < 1 ; 2 ; 'string' ; Nil + +# Tuples act like arrays, but are immutable. +# Any shenanigans degrade them to arrays, however +[1, 2, 'string']. # [1, 2, 'string'] + +# They can interoperate with arrays +[1, 'string'] + (Array new < 'wat'). # Array new < 1 ; 'string' ; 'wat' + +# Indexing into them +[1, 2, 3] at: 1. # 2 + +# Some array operations +var arr is Array new < 1 ; 2 ; 3. + +arr head. # 1 +arr tail. # Array new < 2 ; 3. +arr init. # Array new < 1 ; 2. +arr last. # 3 +arr push: 4. # Array new < 1 ; 2 ; 3 ; 4. +arr pop. # 4 +arr pop: 1. # 2, `arr` is rebound to Array new < 1 ; 3. + +# List comprehensions +[x * 2 + y,, arr, arr + [4, 5],, x > 1]. # Array ← 7 ; 9 ; 10 ; 11 +# fresh variable names are bound as they are encountered, +# so `x` is bound to the values in `arr` +# and `y` is bound to the values in `arr + [4, 5]` +# +# The general format is: [expr,, bindings*,, predicates*] + + +#################################### +## 3. Functions +#################################### + +# A simple function that takes two variables +var add is {:a:b ^a + b.}. + +# this function will resolve all its names except the formal arguments +# in the context it is called in. + +# Using the function +add applyTo: 3 and: 5. # 8 +add applyAll: [3, 5]. # 8 + +# Also a (customizable -- more on this later) pseudo-operator allows for a shorthand +# of function calls +# By default it is REF[args] + +add[3, 5]. # 8 + +# To customize this behaviour, you may simply use a compiler pragma: +#:callShorthand () + +# And then you may use the specified operator. +# Note that the allowed 'operator' can only be made of any of these: []{}() +# And you may mix-and-match (why would anyone do that?) + +add(3, 5). # 8 + +# You may also use functions as operators in the following way: + +3 `add` 5. # 8 +# This call binds as such: add[(3), 5] +# because the default fixity is left, and the default precedance is 1 + +# You may change the precedence/fixity of this operator with a pragma +#:declare infixr 1 add + +3 `add` 5. # 8 +# now this binds as such: add[3, (5)]. + +# There is another form of functions too +# So far, the functions were resolved in a dynamic fashion +# But a lexically scoped block is also possible +var sillyAdd is {\:x:y add[x,y].}. + +# In these blocks, you are not allowed to declare new variables +# Except with the use of Object::'letEqual:in:` +# And the last expression is implicitly returned. + +# You may also use a shorthand for lambda expressions +var mul is \:x:y x * y. + +# These capture the named bindings that are not present in their +# formal parameters, and retain them. (by ref) + +########################################### +## 5. Control Flow +########################################### + +# inline conditional-expressions +var citron is 1 = 1 either: 'awesome' or: 'awful'. # citron is 'awesome' + +# multiple lines is fine too +var citron is 1 = 1 + either: 'awesome' + or: 'awful'. + +# looping +10 times: {:x + Pen writeln: x. +}. # 10. -- side effect: 10 lines in stdout, with numbers 0 through 9 in them + +# Citron properly supports tail-call recursion in lexically scoped blocks +# So use those to your heart's desire + +# mapping most data structures is as simple as `fmap:` +[1, 2, 3, 4] fmap: \:x x + 1. # [2, 3, 4, 5] + +# You can use `foldl:accumulator:` to fold a list/tuple +[1, 2, 3, 4] foldl: (\:acc:x acc * 2 + x) accumulator: 4. # 90 + +# That expression is the same as +(2 * (2 * (2 * (2 * 4 + 1) + 2) + 3) + 4) + +################################### +## 6. IO +################################### + +# IO is quite simple +# With `Pen` being used for console output +# and Program::'input' and Program::'waitForInput' being used for console input + +Pen writeln: 'Hello, ocean!' # prints 'Hello, ocean!\n' to the terminal + +Pen writeln: Program waitForInput. # reads a line and prints it back +``` diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index e2cf62fb..28b5173b 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -16,7 +16,7 @@ popular and recent book is [Land of Lisp](http://landoflisp.com/). A new book ab -```common-lisp +```lisp ;;;----------------------------------------------------------------------------- ;;; 0. Syntax @@ -502,7 +502,7 @@ nil ; false; also, the empty list: () (fact 5) ; => 120 -(loop :for x :across "abc" :collect x) +(loop :for x :across "abcd" :collect x) ; => (#\a #\b #\c #\d) (dolist (i '(1 2 3 4)) diff --git a/cs-cz/markdown.html.markdown b/cs-cz/markdown.html.markdown index 568e4343..35becf94 100644 --- a/cs-cz/markdown.html.markdown +++ b/cs-cz/markdown.html.markdown @@ -13,7 +13,7 @@ Markdown byl vytvořen Johnem Gruberem v roce 2004. Je zamýšlen jako lehce či a psatelná syntaxe, která je jednoduše převeditelná do HTML (a dnes i do mnoha dalších formátů) -```markdown +```md <!-- Markdown je nadstavba nad HTML, takže jakýkoliv kód HTML je validní Markdown, to znamená, že můžeme používat HTML elementy, třeba jako komentář, a nebudou ovlivněny parserem Markdownu. Avšak, pokud vytvoříte HTML element v diff --git a/css.html.markdown b/css.html.markdown index 3b378d44..64dc097c 100644 --- a/css.html.markdown +++ b/css.html.markdown @@ -135,6 +135,10 @@ selector::after {} .parent * { } /* all descendants */ .parent > * { } /* all children */ +/* Group any number of selectors to define styles that affect all selectors + in the group */ +selector1, selector2 { } + /* #################### ## PROPERTIES #################### */ diff --git a/cypher.html.markdown b/cypher.html.markdown index b7be544a..acd44733 100644 --- a/cypher.html.markdown +++ b/cypher.html.markdown @@ -16,19 +16,19 @@ Nodes **Represents a record in a graph.** -```()``` +`()` It's an empty *node*, to indicate that there is a *node*, but it's not relevant for the query. -```(n)``` +`(n)` It's a *node* referred by the variable **n**, reusable in the query. It begins with lowercase and uses camelCase. -```(p:Person)``` +`(p:Person)` You can add a *label* to your node, here **Person**. It's like a type / a class / a category. It begins with uppercase and uses camelCase. -```(p:Person:Manager)``` +`(p:Person:Manager)` A node can have many *labels*. -```(p:Person {name : 'Théo Gauchoux', age : 22})``` +`(p:Person {name : 'Théo Gauchoux', age : 22})` A node can have some *properties*, here **name** and **age**. It begins with lowercase and uses camelCase. The types allowed in properties : @@ -40,7 +40,7 @@ The types allowed in properties : *Warning : there isn't datetime property in Cypher ! You can use String with a specific pattern or a Numeric from a specific date.* -```p.name``` +`p.name` You can access to a property with the dot style. @@ -49,16 +49,16 @@ Relationships (or Edges) **Connects two nodes** -```[:KNOWS]``` +`[:KNOWS]` It's a *relationship* with the *label* **KNOWS**. It's a *label* as the node's label. It begins with uppercase and use UPPER_SNAKE_CASE. -```[k:KNOWS]``` +`[k:KNOWS]` The same *relationship*, referred by the variable **k**, reusable in the query, but it's not necessary. -```[k:KNOWS {since:2017}]``` +`[k:KNOWS {since:2017}]` The same *relationship*, with *properties* (like *node*), here **since**. -```[k:KNOWS*..4]``` +`[k:KNOWS*..4]` It's a structural information to use in a *path* (seen later). Here, **\*..4** says "Match the pattern, with the relationship **k** which be repeated between 1 and 4 times. @@ -67,16 +67,16 @@ Paths **The way to mix nodes and relationships.** -```(a:Person)-[:KNOWS]-(b:Person)``` +`(a:Person)-[:KNOWS]-(b:Person)` A path describing that **a** and **b** know each other. -```(a:Person)-[:MANAGES]->(b:Person)``` +`(a:Person)-[:MANAGES]->(b:Person)` A path can be directed. This path describes that **a** is the manager of **b**. -```(a:Person)-[:KNOWS]-(b:Person)-[:KNOWS]-(c:Person)``` +`(a:Person)-[:KNOWS]-(b:Person)-[:KNOWS]-(c:Person)` You can chain multiple relationships. This path describes the friend of a friend. -```(a:Person)-[:MANAGES]->(b:Person)-[:MANAGES]->(c:Person)``` +`(a:Person)-[:MANAGES]->(b:Person)-[:MANAGES]->(c:Person)` A chain can also be directed. This path describes that **a** is the boss of **b** and the big boss of **c**. Patterns often used (from Neo4j doc) : @@ -230,13 +230,13 @@ DELETE n, r Other useful clauses --- -```PROFILE``` +`PROFILE` Before a query, show the execution plan of it. -```COUNT(e)``` +`COUNT(e)` Count entities (nodes or relationships) matching **e**. -```LIMIT x``` +`LIMIT x` Limit the result to the x first results. diff --git a/de-de/bash-de.html.markdown b/de-de/bash-de.html.markdown index 7928b136..7a0db157 100644 --- a/de-de/bash-de.html.markdown +++ b/de-de/bash-de.html.markdown @@ -180,7 +180,7 @@ esac # 'for' Schleifen iterieren über die angegebene Zahl von Argumenten: # Der Inhalt von $Variable wird dreimal ausgedruckt. -for $Variable in {1..3} +for Variable in {1..3} do echo "$Variable" done diff --git a/de-de/git-de.html.markdown b/de-de/git-de.html.markdown index a0ed120f..d2222d2c 100644 --- a/de-de/git-de.html.markdown +++ b/de-de/git-de.html.markdown @@ -10,7 +10,7 @@ lang: de-de Git ist eine verteilte Versions- und Quellcodeverwaltung. -Es nimmt Schnappschüsse der Projekte, um mit diesen Schnappschüssen verschiedene Versionen unterscheiden und den Quellcode verwalten zu können. +Es nimmt Schnappschüsse der Projekte auf, um mit diesen Schnappschüssen verschiedene Versionen unterscheiden und den Quellcode verwalten zu können. Anmerkung des Übersetzers: Einige englische Begriffe wie *Repository*, *Commit* oder *Head* sind idiomatische Bestandteile im Umgang mit Git. Sie wurden nicht übersetzt. @@ -43,7 +43,7 @@ Eine Versionsverwaltung erfasst die Änderungen einer Datei oder eines Verzeichn ### Repository (Repo) -Ein Satz von Dateien, Verzeichnisen, Historieneinträgen, Commits und Heads. Stell es dir wie eine Quellcode-Datenstruktur vor, unter anderem mit der Eigenschaft, dass alle *Elemente* dir Zugriff auf die Revisionshistorie geben. +Ein Satz von Dateien, Verzeichnissen, Historieneinträgen, Commits und Heads. Stell es dir wie eine Quellcode-Datenstruktur vor, unter anderem mit der Eigenschaft, dass alle *Elemente* dir Zugriff auf die Revisionshistorie geben. Ein Repository besteht in Git aus dem .git-Verzeichnis und dem Arbeitsverzeichnis. @@ -66,7 +66,7 @@ Ein Commit ist ein Schnappschuss von Änderungen in deinem Arbeitsverzeichnis. W ### Branch -Ein Branch, ein Ast oder Zweig, ist im Kern ein Pointer auf den letzten Commit, den du gemacht hast. Während des Commits wird der Pointer automatisch auf Stand gebracht und zeigt dann auf den neuen letzten Commit. +Ein Branch, ein Ast oder Zweig, ist im Kern ein Pointer auf den letzten Commit, den du gemacht hast. Während des Commits wird der Pointer automatisch auf diesen Stand gebracht und zeigt dann auf den neuen letzten Commit. ### HEAD und head (Teil des .git-Verzeichnisses) @@ -215,7 +215,7 @@ $ git commit --amend -m "Correct message" ### diff -Zeigt die Unterschiede zwischen Dateien von Arbeitsverzeichnisse, dem Index und Commits an. +Zeigt die Unterschiede zwischen Dateien vom Arbeitsverzeichnis, dem Index und Commits an. ```bash # Unterschiede zwischen deinem Arbeitsverzeichnis und dem Index anzeigen @@ -330,7 +330,7 @@ $ git push origin master ### rebase (mit Vorsicht einsetzen) -Nimm alle Änderungen, die in einem Branch durch Commits vorgenommen wurden, und übertrage sie auf einen anderen Branch. Achtung: Führe keinen Rebase von Commits durch, die auf ein öffentliches Repo gepusht wurden. +Nimm alle Änderungen, die in einem Branch durch Commits vorgenommen wurden, und übertrage sie auf einen anderen Branch. Achtung: Führe keinen Rebase von Commits durch, die auf ein öffentliches Repo gepusht wurden! ```bash # Rebase "experimentBranch" in den "master"-Branch diff --git a/de-de/python3-de.html.markdown b/de-de/python3-de.html.markdown index 33897225..b313727c 100644 --- a/de-de/python3-de.html.markdown +++ b/de-de/python3-de.html.markdown @@ -152,7 +152,7 @@ print("Ich bin Python. Schön, dich kennenzulernen!") some_var = 5 # kleinschreibung_mit_unterstrichen entspricht der Norm some_var #=> 5 -# Das Ansprechen einer noch nicht deklarierte Variable löst eine Exception aus. +# Das Ansprechen einer noch nicht deklarierten Variable löst eine Exception aus. # Unter "Kontrollstruktur" kann noch mehr über # Ausnahmebehandlung erfahren werden. some_unknown_var # Löst einen NameError aus @@ -225,7 +225,7 @@ a, b, c = (1, 2, 3) # a ist jetzt 1, b ist jetzt 2 und c ist jetzt 3 # Tupel werden standardmäßig erstellt, wenn wir uns die Klammern sparen d, e, f = 4, 5, 6 # Es ist kinderleicht zwei Werte zu tauschen -e, d = d, e # d is now 5 and e is now 4 +e, d = d, e # d ist nun 5 und e ist nun 4 # Dictionarys (Wörterbucher) speichern Schlüssel-Werte-Paare @@ -379,8 +379,8 @@ with open("meineDatei.txt") as f: print(line) # Python bietet ein fundamentales Konzept der Iteration. -# Das Objekt, auf das die Interation, also die Wiederholung einer Methode angewandt wird heißt auf Englisch "iterable". -# Die range Method gibt ein solches Objekt aus. +# Das Objekt, auf das die Iteration, also die Wiederholung einer Methode angewandt wird heißt auf Englisch "iterable". +# Die range Methode gibt ein solches Objekt aus. filled_dict = {"one": 1, "two": 2, "three": 3} our_iterable = filled_dict.keys() @@ -396,8 +396,8 @@ our_iterable[1] # TypeError # Ein iterable ist ein Objekt, das weiß wie es einen Iteratoren erschafft. our_iterator = iter(our_iterable) -# Unser Iterator ist ein Objekt, das sich merkt, welchen Status es geraden hat während wir durch es gehen. -# Das jeweeils nächste Objekt bekommen wir mit "next()" +# Unser Iterator ist ein Objekt, das sich merkt, welchen Status es gerade hat während wir durch es gehen. +# Das jeweils nächste Objekt bekommen wir mit "next()" next(our_iterator) #=> "one" # Es hält den vorherigen Status @@ -442,7 +442,7 @@ def keyword_args(**kwargs): # Rufen wir es mal auf, um zu sehen, was passiert keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} -# Wir können beides gleichzeitig machem, wenn wir wollen +# Wir können beides gleichzeitig machen, wenn wir wollen def all_the_args(*args, **kwargs): print(args) print(kwargs) diff --git a/es-es/awk-es.html.markdown b/es-es/awk-es.html.markdown index 307ba817..0516ea92 100644 --- a/es-es/awk-es.html.markdown +++ b/es-es/awk-es.html.markdown @@ -166,7 +166,7 @@ function arithmetic_functions(a, b, c, localvar) { # trigonométricas estándar localvar = sin(a) localvar = cos(a) - localvar = atan2(a, b) # arcotangente de b / a + localvar = atan2(b, a) # arcotangente de b / a # Y cosas logarítmicas localvar = exp(a) diff --git a/es-es/csharp-es.html.markdown b/es-es/csharp-es.html.markdown index 5d730497..72a0f90c 100644 --- a/es-es/csharp-es.html.markdown +++ b/es-es/csharp-es.html.markdown @@ -5,7 +5,7 @@ contributors: - ["Irfan Charania", "https://github.com/irfancharania"] - ["Max Yankov", "https://github.com/golergka"] translators: - - ["Olfran Jiménez", "https://twitter.com/neslux"] + - ["Olfran Jiménez", "https://twitter.com/neslux"] lang: es-es --- diff --git a/es-es/fsharp-es.html.markdown b/es-es/fsharp-es.html.markdown new file mode 100644 index 00000000..b7f80c44 --- /dev/null +++ b/es-es/fsharp-es.html.markdown @@ -0,0 +1,629 @@ +--- +language: F# +lang: es-es +contributors: + - ['Scott Wlaschin', 'http://fsharpforfunandprofit.com/'] +translators: + - ['Angel Arciniega', 'https://github.com/AngelsProjects'] +filename: learnfsharp-es.fs +--- + +F# es un lenguaje de programación funcional y orientado a objetos. Es gratis y su código fuente está abierto. Se ejecuta en Linux, Mac, Windows y más. + +Tiene un poderoso sistema de tipado que atrapa muchos errores de tiempo de compilación, pero usa inferencias de tipados que le permiten ser leídos como un lenguaje dinámico. + +La sintaxis de F# es diferente de los lenguajes que heredan de C. + +- Las llaves no se usan para delimitar bloques de código. En cambio, se usa sangría (como en Python). +- Los espacios se usan para separar parámetros en lugar de comas. + +Si quiere probar el siguiente código, puede ir a [tryfsharp.org](http://www.tryfsharp.org/Create) y pegarlo en [REPL](https://es.wikipedia.org/wiki/REPL). + +```fsharp +// Los comentarios de una línea se escibren con una doble diagonal +(* Los comentarios multilínea usan parentesis (* . . . *) + +-final del comentario multilínea- *) + +// ================================================ +// Syntaxis básica +// ================================================ + +// ------ "Variables" (pero no realmente) ------ +// La palabra reservada "let" define un valor (inmutable) +let miEntero = 5 +let miFlotante = 3.14 +let miCadena = "hola" // Tenga en cuenta que no es necesario ningún tipado + +// ------ Listas ------ +let dosACinco = [2;3;4;5] // Los corchetes crean una lista con + // punto y coma para delimitadores. +let unoACinco = 1 :: dosACinco // :: Crea una lista con un nuevo elemento +// El resultado es [1;2;3;4;5] +let ceroACinco = [0;1] @ dosACinco // @ Concatena dos listas + +// IMPORTANTE: las comas no se usan para delimitar, +// solo punto y coma ! + +// ------ Funciones ------ +// La palabra reservada "let" también define el nombre de una función. +let cuadrado x = x * x // Tenga en cuenta que no se usa paréntesis. +cuadrado 3 // Ahora, ejecutemos la función. + // De nuevo, sin paréntesis. + +let agregar x y = x + y // ¡No use add (x, y)! Eso significa + // algo completamente diferente. +agregar 2 3 // Ahora, ejecutemos la función. + +// Para definir una función en varias líneas, usemos la sangría. +// Los puntos y coma no son necesarios. +let pares lista = + let esPar x = x%2 = 0 // Establece "esPar" como una función anidada + List.filter esPar lista // List.filter es una función de la biblioteca + // dos parámetros: una función que devuelve un + // booleano y una lista en la que trabajar + +pares unoACinco // Ahora, ejecutemos la función. + +// Puedes usar paréntesis para aclarar. +// En este ejemplo, "map" se ejecuta primero, con dos argumentos, +// entonces "sum" se ejecuta en el resultado. +// Sin los paréntesis, "List.map" se pasará como argumento a List.sum. +let sumaDeCuadradosHasta100 = + List.sum ( List.map cuadrado [1..100] ) + +// Puedes redirigir la salida de una función a otra con "|>" +// Redirigir datos es muy común en F#, como con los pipes de UNIX. + +// Aquí está la misma función sumOfSquares escrita usando pipes +let sumaDeCuadradosHasta100piped = + [1..100] |> List.map cuadrado |> List.sum // "cuadrado" se declara antes + +// Puede definir lambdas (funciones anónimas) gracias a la palabra clave "fun" +let sumaDeCuadradosHasta100ConFuncion = + [1..100] |> List.map (fun x -> x*x) |> List.sum + +// En F#, no hay palabra clave "return". Una función siempre regresa +// el valor de la última expresión utilizada. + +// ------ Coincidencia de patrones ------ +// Match..with .. es una sobrecarga de la condición de case/ switch. +let coincidenciaDePatronSimple = + let x = "a" + match x with + | "a" -> printfn "x es a" + | "b" -> printfn "x es b" + | _ -> printfn "x es algo mas" // guion bajo corresponde con todos los demás + +// F# no permite valores nulos por defecto - debe usar el tipado de Option +// y luego coincide con el patrón. +// Some(..) y None son aproximadamente análogos a los envoltorios Nullable +let valorValido = Some(99) +let valorInvalido = None + +// En este ejemplo, match..with encuentra una coincidencia con "Some" y "None", +// y muestra el valor de "Some" al mismo tiempo. +let coincidenciaDePatronDeOpciones entrada = + match entrada with + | Some i -> printfn "la entrada es un int=%d" i + | None -> printfn "entrada faltante" + +coincidenciaDePatronDeOpciones validValue +coincidenciaDePatronDeOpciones invalidValue + +// ------ Viendo ------ +// Las funciones printf/printfn son similares a las funciones +// Console.Write/WriteLine de C#. +printfn "Imprimiendo un int %i, a float %f, a bool %b" 1 2.0 true +printfn "Un string %s, y algo generico %A" "hola" [1;2;3;4] + +// También hay funciones printf/sprintfn para formatear datos +// en cadena. Es similar al String.Format de C#. + +// ================================================ +// Mas sobre funciones +// ================================================ + +// F# es un verdadero lenguaje funcional - las funciones son +// entidades de primer nivel y se pueden combinar fácilmente +// para crear construcciones poderosas + +// Los módulos se utilizan para agrupar funciones juntas. +// Se requiere sangría para cada módulo anidado. +module EjemploDeFuncion = + + // define una función de suma simple + let agregar x y = x + y + + // uso básico de una función + let a = agregar 1 2 + printfn "1+2 = %i" a + + // aplicación parcial para "hornear en" los parámetros (?) + let agregar42 = agregar 42 + let b = agregar42 1 + printfn "42+1 = %i" b + + // composición para combinar funciones + let agregar1 = agregar 1 + let agregar2 = agregar 2 + let agregar3 = agregar1 >> agregar2 + let c = agregar3 7 + printfn "3+7 = %i" c + + // funciones de primer nivel + [1..10] |> List.map agregar3 |> printfn "la nueva lista es %A" + + // listas de funciones y más + let agregar6 = [agregar1; agregar2; agregar3] |> List.reduce (>>) + let d = agregar6 7 + printfn "1+2+3+7 = %i" d + +// ================================================ +// Lista de colecciones +// ================================================ + +// Il y a trois types de collection ordonnée : +// * Les listes sont les collections immutables les plus basiques +// * Les tableaux sont mutables et plus efficients +// * Les séquences sont lazy et infinies (e.g. un enumerator) +// +// Des autres collections incluent des maps immutables et des sets +// plus toutes les collections de .NET + +module EjemplosDeLista = + + // las listas utilizan corchetes + let lista1 = ["a";"b"] + let lista2 = "c" :: lista1 // :: para una adición al principio + let lista3 = lista1 @ lista2 // @ para la concatenación + + // Lista de comprensión (alias generadores) + let cuadrados = [for i in 1..10 do yield i*i] + + // Generador de números primos + let rec tamiz = function + | (p::xs) -> p :: tamiz [ for x in xs do if x % p > 0 then yield x ] + | [] -> [] + let primos = tamiz [2..50] + printfn "%A" primos + + // coincidencia de patrones para listas + let listaDeCoincidencias unaLista = + match unaLista with + | [] -> printfn "la lista esta vacia" + | [primero] -> printfn "la lista tiene un elemento %A " primero + | [primero; segundo] -> printfn "la lista es %A y %A" primero segundo + | _ -> printfn "la lista tiene mas de dos elementos" + + listaDeCoincidencias [1;2;3;4] + listaDeCoincidencias [1;2] + listaDeCoincidencias [1] + listaDeCoincidencias [] + + // Récursion en utilisant les listes + let rec suma unaLista = + match unaLista with + | [] -> 0 + | x::xs -> x + suma xs + suma [1..10] + + // ----------------------------------------- + // Funciones de la biblioteca estándar + // ----------------------------------------- + + // mapeo + let agregar3 x = x + 3 + [1..10] |> List.map agregar3 + + // filtrado + let par x = x % 2 = 0 + [1..10] |> List.filter par + + // mucho más - consulte la documentación + +module EjemploDeArreglo = + + // los arreglos usan corchetes con barras. + let arreglo1 = [| "a";"b" |] + let primero = arreglo1.[0] // se accede al índice usando un punto + + // la coincidencia de patrones de los arreglos es la misma que la de las listas + let coincidenciaDeArreglos una Lista = + match unaLista with + | [| |] -> printfn "la matriz esta vacia" + | [| primero |] -> printfn "el arreglo tiene un elemento %A " primero + | [| primero; second |] -> printfn "el arreglo es %A y %A" primero segundo + | _ -> printfn "el arreglo tiene mas de dos elementos" + + coincidenciaDeArreglos [| 1;2;3;4 |] + + // La biblioteca estándar funciona como listas + [| 1..10 |] + |> Array.map (fun i -> i+3) + |> Array.filter (fun i -> i%2 = 0) + |> Array.iter (printfn "el valor es %i. ") + +module EjemploDeSecuencia = + + // Las secuencias usan llaves + let secuencia1 = seq { yield "a"; yield "b" } + + // Las secuencias pueden usar yield y + // puede contener subsecuencias + let extranio = seq { + // "yield" agrega un elemento + yield 1; yield 2; + + // "yield!" agrega una subsecuencia completa + yield! [5..10] + yield! seq { + for i in 1..10 do + if i%2 = 0 then yield i }} + // prueba + extranio |> Seq.toList + + // Las secuencias se pueden crear usando "unfold" + // Esta es la secuencia de fibonacci + let fib = Seq.unfold (fun (fst,snd) -> + Some(fst + snd, (snd, fst + snd))) (0,1) + + // prueba + let fib10 = fib |> Seq.take 10 |> Seq.toList + printf "Los primeros 10 fib son %A" fib10 + +// ================================================ +// Tipos de datos +// ================================================ + +module EejemploDeTipoDeDatos = + + // Todos los datos son inmutables por defecto + + // las tuplas son tipos anónimos simples y rápidos + // - Usamos una coma para crear una tupla + let dosTuplas = 1,2 + let tresTuplas = "a",2,true + + // Combinación de patrones para desempaquetar + let x,y = dosTuplas // asignado x=1 y=2 + + // ------------------------------------ + // Los tipos de registro tienen campos con nombre + // ------------------------------------ + + // Usamos "type" con llaves para definir un tipo de registro + type Persona = {Nombre:string; Apellido:string} + + // Usamos "let" con llaves para crear un registro + let persona1 = {Nombre="John"; Apellido="Doe"} + + // Combinación de patrones para desempaquetar + let {Nombre=nombre} = persona1 // asignado nombre="john" + + // ------------------------------------ + // Los tipos de unión (o variantes) tienen un conjunto de elección + // Solo un caso puede ser válido a la vez. + // ------------------------------------ + + // Usamos "type" con barra/pipe para definir una unión estándar + type Temp = + | GradosC of float + | GradosF of float + + // Una de estas opciones se usa para crear una + let temp1 = GradosF 98.6 + let temp2 = GradosC 37.0 + + // Coincidencia de patrón en todos los casos para desempaquetar (?) + let imprimirTemp = function + | GradosC t -> printfn "%f gradC" t + | GradosF t -> printfn "%f gradF" t + + imprimirTemp temp1 + imprimirTemp temp2 + + // ------------------------------------ + // Tipos recursivos + // ------------------------------------ + + // Los tipos se pueden combinar recursivamente de formas complejas + // sin tener que crear subclases + type Empleado = + | Trabajador of Persona + | Gerente of Empleado lista + + let jdoe = {Nombre="John";Apellido="Doe"} + let trabajador = Trabajador jdoe + + // ------------------------------------ + // Modelado con tipados (?) + // ------------------------------------ + + // Los tipos de unión son excelentes para modelar el estado sin usar banderas (?) + type DireccionDeCorreo = + | DireccionDeCorreoValido of string + | DireccionDeCorreoInvalido of string + + let intentarEnviarCorreo correoElectronico = + match correoElectronico with // uso de patrones de coincidencia + | DireccionDeCorreoValido direccion -> () // enviar + | DireccionDeCorreoInvalido direccion -> () // no enviar + + // Combinar juntos, los tipos de unión y tipos de registro + // ofrece una base excelente para el diseño impulsado por el dominio. + // Puedes crear cientos de pequeños tipos que reflejarán fielmente + // el dominio. + + type ArticuloDelCarrito = { CodigoDelProducto: string; Cantidad: int } + type Pago = Pago of float + type DatosActivosDelCarrito = { ArticulosSinPagar: ArticuloDelCarrito lista } + type DatosPagadosDelCarrito = { ArticulosPagados: ArticuloDelCarrito lista; Pago: Pago} + + type CarritoDeCompras = + | CarritoVacio // sin datos + | CarritoActivo of DatosActivosDelCarrito + | CarritoPagado of DatosPagadosDelCarrito + + // ------------------------------------ + // Comportamiento nativo de los tipos + // ------------------------------------ + + // Los tipos nativos tienen el comportamiento más útil "listo para usar", sin ningún código para agregar. + // * Inmutabilidad + // * Bonita depuración de impresión + // * Igualdad y comparación + // * Serialización + + // La impresión bonita se usa con %A + printfn "dosTuplas=%A,\nPersona=%A,\nTemp=%A,\nEmpleado=%A" + dosTuplas persona1 temp1 trabajador + + // La igualdad y la comparación son innatas + // Aquí hay un ejemplo con tarjetas. + type JuegoDeCartas = Trebol | Diamante | Espada | Corazon + type Rango = Dos | Tres | Cuatro | Cinco | Seis | Siete | Ocho + | Nueve | Diez | Jack | Reina | Rey | As + + let mano = [ Trebol,As; Corazon,Tres; Corazon,As; + Espada,Jack; Diamante,Dos; Diamante,As ] + + // orden + List.sort mano |> printfn "la mano ordenada es (de menos a mayor) %A" + List.max mano |> printfn "la carta más alta es%A" + List.min mano |> printfn "la carta más baja es %A" + +// ================================================ +// Patrones activos +// ================================================ + +module EjemplosDePatronesActivos = + + // F# tiene un tipo particular de coincidencia de patrón llamado "patrones activos" + // donde el patrón puede ser analizado o detectado dinámicamente. + + // "clips de banana" es la sintaxis de los patrones activos + + // por ejemplo, definimos un patrón "activo" para que coincida con los tipos de "caracteres" ... + let (|Digito|Latra|EspacioEnBlanco|Otros|) ch = + if System.Char.IsDigit(ch) then Digito + else if System.Char.IsLetter(ch) then Letra + else if System.Char.IsWhiteSpace(ch) then EspacioEnBlanco + else Otros + + // ... y luego lo usamos para hacer que la lógica de análisis sea más clara + let ImprimirCaracter ch = + match ch with + | Digito -> printfn "%c es un Digito" ch + | Letra -> printfn "%c es una Letra" ch + | Whitespace -> printfn "%c es un Espacio en blanco" ch + | _ -> printfn "%c es algo mas" ch + + // ver una lista + ['a';'b';'1';' ';'-';'c'] |> List.iter ImprimirCaracter + + // ----------------------------------------- + // FizzBuzz usando patrones activos + // ----------------------------------------- + + // Puede crear un patrón de coincidencia parcial también + // Solo usamos un guión bajo en la definición y devolvemos Some si coincide. + let (|MultDe3|_|) i = if i % 3 = 0 then Some MultDe3 else None + let (|MultDe5|_|) i = if i % 5 = 0 then Some MultDe5 else None + + // la función principal + let fizzBuzz i = + match i with + | MultDe3 & MultDe5 -> printf "FizzBuzz, " + | MultDe3 -> printf "Fizz, " + | MultDe5 -> printf "Buzz, " + | _ -> printf "%i, " i + + // prueba + [1..20] |> List.iter fizzBuzz + +// ================================================ +// concisión +// ================================================ + +module EjemploDeAlgoritmo = + + // F# tiene una alta relación señal / ruido, lo que permite leer el código + // casi como un algoritmo real + + // ------ Ejemplo: definir una función sumaDeCuadrados ------ + let sumaDeCuadrados n = + [1..n] // 1) Tome todos los números del 1 al n + |> List.map cuadrado // 2) Elevar cada uno de ellos al cuadrado + |> List.sum // 3) Realiza su suma + + // prueba + sumaDeCuadrados 100 |> printfn "Suma de cuadrados = %A" + + // ------ Ejemplo: definir una función de ordenación ------ + let rec ordenar lista = + match lista with + // Si la lista está vacía + | [] -> + [] // devolvemos una lista vacía + // si la lista no está vacía + | primerElemento::otrosElementos -> // tomamos el primer elemento + let elementosMasPequenios = // extraemos los elementos más pequeños + otrosElementos // tomamos el resto + |> List.filter (fun e -> e < primerElemento) + |> ordenar // y los ordenamos + let elementosMasGrandes = // extraemos el mas grande + otrosElementos // de los que permanecen + |> List.filter (fun e -> e >= primerElemento) + |> ordenar // y los ordenamos + // Combinamos las 3 piezas en una nueva lista que devolvemos + List.concat [elementosMasPequenios; [primerElemento]; elementosMasGrandes] + + // prueba + ordenar [1;5;23;18;9;1;3] |> printfn "Ordenado = %A" + +// ================================================ +// Código asíncrono +// ================================================ + +module AsyncExample = + + // F# incluye características para ayudar con el código asíncrono + // sin conocer la "pirámide del destino" + // + // El siguiente ejemplo descarga una secuencia de página web en paralelo. + + open System.Net + open System + open System.IO + open Microsoft.FSharp.Control.CommonExtensions + + // Recuperar el contenido de una URL de forma asincrónica + let extraerUrlAsync url = + async { // La palabra clave "async" y llaves + // crear un objeto "asincrónico" + let solicitud = WebRequest.Create(Uri(url)) + use! respuesta = solicitud.AsyncGetResponse() + // use! es una tarea asincrónica + use flujoDeDatos = resp.GetResponseStream() + // "use" dispara automáticamente la funcion close() + // en los recursos al final de las llaves + use lector = new IO.StreamReader(flujoDeDatos) + let html = lector.ReadToEnd() + printfn "terminó la descarga %s" url + } + + // una lista de sitios para informar + let sitios = ["http://www.bing.com"; + "http://www.google.com"; + "http://www.microsoft.com"; + "http://www.amazon.com"; + "http://www.yahoo.com"] + + // ¡Aqui vamos! + sitios + |> List.map extraerUrlAsync // crear una lista de tareas asíncrona + |> Async.Parallel // decirle a las tareas que se desarrollan en paralelo + |> Async.RunSynchronously // ¡Empieza! + +// ================================================ +// Compatibilidad .NET +// ================================================ + +module EjemploCompatibilidadNet = + + // F# puede hacer casi cualquier cosa que C# pueda hacer, y se ajusta + // perfectamente con bibliotecas .NET o Mono. + + // ------- Trabaja con las funciones de las bibliotecas existentes ------- + + let (i1success,i1) = System.Int32.TryParse("123"); + if i1success then printfn "convertido como %i" i1 else printfn "conversion fallida" + + // ------- Implementar interfaces sobre la marcha! ------- + + // Crea un nuevo objeto que implemente IDisposable + let crearRecurso name = + { new System.IDisposable + with member this.Dispose() = printfn "%s creado" name } + + let utilizarYDisponerDeRecursos = + use r1 = crearRecurso "primer recurso" + printfn "usando primer recurso" + for i in [1..3] do + let nombreDelRecurso = sprintf "\tinner resource %d" i + use temp = crearRecurso nombreDelRecurso + printfn "\thacer algo con %s" nombreDelRecurso + use r2 = crearRecurso "segundo recurso" + printfn "usando segundo recurso" + printfn "hecho." + + // ------- Código orientado a objetos ------- + + // F# es también un verdadero lenguaje OO. + // Admite clases, herencia, métodos virtuales, etc. + + // interfaz de tipo genérico + type IEnumerator<'a> = + abstract member Actual : 'a + abstract MoverSiguiente : unit -> bool + + // Clase base abstracta con métodos virtuales + [<AbstractClass>] + type Figura() = + // propiedades de solo lectura + abstract member Ancho : int with get + abstract member Alto : int with get + // método no virtual + member this.AreaDelimitadora = this.Alto * this.Ancho + // método virtual con implementación de la clase base + abstract member Imprimir : unit -> unit + default this.Imprimir () = printfn "Soy una Figura" + + // clase concreta que hereda de su clase base y sobrecarga + type Rectangulo(x:int, y:int) = + inherit Figura() + override this.Ancho = x + override this.Alto = y + override this.Imprimir () = printfn "Soy un Rectangulo" + + // prueba + let r = Rectangulo(2,3) + printfn "La anchura es %i" r.Ancho + printfn "El area es %i" r.AreaDelimitadora + r.Imprimir() + + // ------- extensión de método ------- + + // Al igual que en C#, F# puede extender las clases existentes con extensiones de método. + type System.String with + member this.EmpiezaConA = this.EmpiezaCon "A" + + // prueba + let s = "Alice" + printfn "'%s' empieza con una 'A' = %A" s s.EmpiezaConA + + // ------- eventos ------- + + type MiBoton() = + let eventoClick = new Event<_>() + + [<CLIEvent>] + member this.AlHacerClick = eventoClick.Publish + + member this.PruebaEvento(arg) = + eventoClick.Trigger(this, arg) + + // prueba + let miBoton = new MiBoton() + miBoton.AlHacerClick.Add(fun (sender, arg) -> + printfn "Haga clic en el evento con arg=%O" arg) + + miBoton.PruebaEvento("Hola Mundo!") +``` + +## Más información + +Para más demostraciones de F#, visite el sitio [Try F#](http://www.tryfsharp.org/Learn), o sigue la serie [why use F#](http://fsharpforfunandprofit.com/why-use-fsharp/). + +Aprenda más sobre F# en [fsharp.org](http://fsharp.org/). diff --git a/es-es/learnsmallbasic-es.html.markdown b/es-es/learnsmallbasic-es.html.markdown index 21208792..ff320afb 100644 --- a/es-es/learnsmallbasic-es.html.markdown +++ b/es-es/learnsmallbasic-es.html.markdown @@ -18,7 +18,7 @@ SmallBASIC fue desarrollado originalmente por Nicholas Christopoulos a finales d Versiones de SmallBASIC se han hecho para una serie dispositivos de mano antiguos, incluyendo Franklin eBookman y el Nokia 770. También se han publicado varias versiones de escritorio basadas en una variedad de kits de herramientas GUI, algunas de las cuales han desaparecido. Las plataformas actualmente soportadas son Linux y Windows basadas en SDL2 y Android basadas en NDK. También está disponible una versión de línea de comandos de escritorio, aunque no suele publicarse en formato binario. Alrededor de 2008 una gran corporación lanzó un entorno de programación BASIC con un nombre de similar. SmallBASIC no está relacionado con este otro proyecto. -```SmallBASIC +``` REM Esto es un comentario ' y esto tambien es un comentario diff --git a/es-es/markdown-es.html.markdown b/es-es/markdown-es.html.markdown index 0505b4cb..e23a94ea 100644 --- a/es-es/markdown-es.html.markdown +++ b/es-es/markdown-es.html.markdown @@ -14,7 +14,7 @@ fácilmente a HTML (y, actualmente, otros formatos también). ¡Denme toda la retroalimentación que quieran! / ¡Sientanse en la libertad de hacer forks o pull requests! -```markdown +```md <!-- Markdown está basado en HTML, así que cualquier archivo HTML es Markdown válido, eso significa que podemos usar elementos HTML en Markdown como, por ejemplo, el comentario y no serán afectados por un parseador Markdown. Aún diff --git a/es-es/objective-c-es.html.markdown b/es-es/objective-c-es.html.markdown index bdbce524..26cd14d9 100644 --- a/es-es/objective-c-es.html.markdown +++ b/es-es/objective-c-es.html.markdown @@ -13,7 +13,7 @@ Objective C es el lenguaje de programación principal utilizado por Apple para l Es un lenguaje de programación para propósito general que le agrega al lenguaje de programación C una mensajería estilo "Smalltalk". -```objective_c +```objectivec // Los comentarios de una sola línea inician con // /* diff --git a/es-es/perl6-es.html.markdown b/es-es/perl6-es.html.markdown new file mode 100644 index 00000000..bf3ae65e --- /dev/null +++ b/es-es/perl6-es.html.markdown @@ -0,0 +1,1935 @@ +--- +name: perl6 +category: language +language: perl6 +filename: perl6-es.p6 +contributors: + - ["vendethiel", "http://github.com/vendethiel"] + - ["Samantha McVey", "https://cry.nu"] +translators: + - ["Luis F. Uceta", "https://github.com/uzluisf"] +lang: es-es +--- + +Perl 6 es un lenguaje de programación altamente capaz y con características +abundantes para hacerlo el lenguage ideal por los próximos 100 años. + +El compilador primario de Perl 6 se llama [Rakudo](http://rakudo.org), el cual +se ejecuta en JVM y en [MoarVM](http://moarvm.com). + +Meta-nota: dos signos de números (##) son usados para indicar párrafos, +mientras que un solo signo de número (#) indica notas. + +`#=>` representa la salida de un comando. + +```perl6 +# Un comentario de una sola línea comienza con un signo de número + +#`( + Comentarios multilíneas usan #` y signos de encerradura tales + como (), [], {}, 「」, etc. +) +``` + +## Variables + +```perl6 +## En Perl 6, se declara una variable lexical usando `my` +my $variable; +## Perl 6 tiene 3 tipos básicos de variables: escalares, arrays, y hashes. +``` + +### Escalares + +```perl6 +# Un escalar representa un solo valor. Variables escalares comienzan +# con un `$` + +my $str = 'Cadena'; +# Las comillas inglesas ("") permiten la intepolación (lo cual veremos +# luego): +my $str2 = "Cadena"; + +## Los nombres de variables pueden contener pero no terminar con comillas +## simples y guiones. Sin embargo, pueden contener +## (y terminar con) guiones bajos (_): +my $nombre'de-variable_ = 5; # Esto funciona! + +my $booleano = True; # `True` y `False` son valores booleanos en Perl 6. +my $inverso = !$booleano; # Puedes invertir un booleano con el operador prefijo `!` +my $bool-forzado = so $str; # Y puedes usar el operador prefijo `so` que + # convierte su operador en un Bool +``` + +### Arrays y Listas + +```perl6 +## Un array representa varios valores. Variables arrays comienzan con `@`. +## Las listas son similares pero son un tipo inmutable. + +my @array = 'a', 'b', 'c'; +# equivalente a: +my @letras = <a b c>; # array de palabras, delimitado por espacios. + # Similar al qw de perl5, o el %w de Ruby. +my @array = 1, 2, 3; + +say @array[2]; # Los índices de un array empiezan por el 0 -- Este es + # el tercer elemento. + +say "Interpola todos los elementos de un array usando [] : @array[]"; +#=> Interpola todos los elementos de un array usando [] : 1 2 3 + +@array[0] = -1; # Asigna un nuevo valor a un índice del array +@array[0, 1] = 5, 6; # Asigna varios valores + +my @llaves = 0, 2; +@array[@llaves] = @letras; # Asignación usando un array que contiene valores + # índices +say @array; #=> a 6 b +``` + +### Hashes, o Pairs (pares) de llaves-valores. + +```perl6 +## Un hash contiene parejas de llaves y valores. +## Puedes construir un objeto Pair usando la sintaxis `LLave => Valor`. +## Tablas de hashes son bien rápidas para búsqueda, y son almacenadas +## sin ningún orden. +## Ten en cuenta que las llaves son "aplanadas" en contexto de hash, y +## cualquier llave duplicada es deduplicada. +my %hash = 1 => 2, + 3 => 4; +my %hash = foo => "bar", # las llaves reciben sus comillas + # automáticamente. + "some other" => "value", # las comas colgantes estań bien. + ; + +## Aunque los hashes son almacenados internamente de forma diferente a los +## arrays, Perl 6 te permite crear un hash usando un array +## con un número par de elementos fácilmente. +my %hash = <llave1 valor1 llave2 valor2>; + +my %hash = llave1 => 'valor1', llave2 => 'valor2'; # ¡el mismo resultado! + +## También puedes usar la sintaxis "pareja con dos puntos": +## (especialmente útil para parámetros nombrados que verás más adelante) +my %hash = :w(1), # equivalente a `w => 1` + # esto es útil para el atajo `True`: + :truey, # equivalente a `:truey(True)`, o `truey => True` + # y para el `False`: + :!falsey, # equivalente a `:falsey(False)`, o `falsey => False` + ; + +say %hash{'llave1'}; # Puedes usar {} para obtener el valor de una llave +say %hash<llave2>; # Si es una cadena de texto, puedes actualmente usar <> + # (`{llave1}` no funciona, debido a que Perl 6 no tiene + # palabras desnudas (barewords en inglés)) +``` + +## Subrutinas + +```perl6 +## Subrutinas, o funciones como otros lenguajes las llaman, son +## creadas con la palabra clave `sub`. +sub di-hola { say "¡Hola, mundo!" } + +## Puedes proveer argumentos (tipados). Si especificado, +## el tipo será chequeado al tiempo de compilación si es posible. +## De lo contrario, al tiempo de ejecución. +sub di-hola-a(Str $nombre) { + say "¡Hola, $nombre!"; +} + +## Una subrutina devuelve el último valor evaluado del bloque. +sub devolver-valor { + 5; +} +say devolver-valor; # imprime 5 +sub devolver-vacio { +} +say devolver-vacio; # imprime Nil + +## Algunas estructuras de control producen un valor. Por ejemplo if: +sub devuelva-si { + if True { + "Truthy"; + } +} +say devuelva-si; # imprime Truthy + +## Otras no, como un bucle for: +sub return-for { + for 1, 2, 3 { } +} +say return-for; # imprime Nil + +## Una subrutina puede tener argumentos opcionales: +sub con-opcional($arg?) { # el signo "?" marca el argumento opcional + say "Podría returnar `(Any)` (valor de Perl parecido al 'null') si no me pasan + un argumento, o returnaré mi argumento"; + $arg; +} +con-opcional; # devuelve Any +con-opcional(); # devuelve Any +con-opcional(1); # devuelve 1 + +## También puedes proveer un argumento por defecto para +## cuando los argumentos no son proveídos: +sub hola-a($nombre = "Mundo") { + say "¡Hola, $nombre!"; +} +hola-a; #=> ¡Hola, Mundo! +hola-a(); #=> ¡Hola, Mundo! +hola-a('Tú'); #=> ¡Hola, Tú! + +## De igual manera, al usar la sintaxis parecida a la de los hashes +## (¡Hurra, sintaxis unificada!), puedes pasar argumentos *nombrados* +## a una subrutina. Ellos son opcionales, y por defecto son del tipo "Any". +sub con-nombre($arg-normal, :$nombrado) { + say $arg-normal + $nombrado; +} +con-nombre(1, nombrado => 6); #=> 7 +## Sin embargo, debes tener algo en cuenta aquí: +## Si pones comillas alrededor de tu llave, Perl 6 no será capaz de verla +## al tiempo de compilación, y entonces tendrás un solo objeto Pair como +## un argumento posicional, lo que significa que el siguiente ejemplo +## falla: +con-nombre(1, 'nombrado' => 6); + +con-nombre(2, :nombrado(5)); #=> 7 + +## Para hacer un argumento nombrado mandatorio, puedes utilizar el +## inverso de `?`, `!`: +sub con-nombre-mandatorio(:$str!) { + say "$str!"; +} +con-nombre-mandatorio(str => "Mi texto"); #=> Mi texto! +con-nombre-mandatorio; # error al tiempo de ejecución: + # "Required named parameter not passed" + # ("Parámetro nombrado requerido no proveído") +con-nombre-mandatorio(3);# error al tiempo de ejecución: + # "Too many positional parameters passed" + # ("Demasiados argumentos posicionales proveídos") + +## Si una subrutina toma un argumento booleano nombrado ... +sub toma-un-bool($nombre, :$bool) { + say "$nombre toma $bool"; +} +## ... puedes usar la misma sintaxis de hash de un "booleano corto": +takes-a-bool('config', :bool); # config toma True +takes-a-bool('config', :!bool); # config toma False + +## También puedes proveer tus argumentos nombrados con valores por defecto: +sub nombrado-definido(:$def = 5) { + say $def; +} +nombrado-definido; #=> 5 +nombrado-definido(def => 15); #=> 15 + +## Dado que puedes omitir los paréntesis para invocar una función sin +## argumentos, necesitas usar "&" en el nombre para almacenar la función +## `di-hola` en una variable. +my &s = &di-hola; +my &otra-s = sub { say "¡Función anónima!" } + +## Una subrutina puede tener un parámetro "slurpy", o "no importa cuantos", +## indicando que la función puede recibir cualquier número de parámetros. +sub muchos($principal, *@resto) { #`*@` (slurpy) consumirá lo restante +## Nota: Puedes tener parámetros *antes que* un parámetro "slurpy" (como +## aquí) pero no *después* de uno. + say @resto.join(' / ') ~ "!"; +} +say muchos('Feliz', 'Cumpleaño', 'Cumpleaño'); #=> Feliz / Cumpleaño! + # Nota que el asterisco (*) no + # consumió el parámetro frontal. + +## Puedes invocar un función con un array usando el +## operador "aplanador de lista de argumento" `|` +## (actualmente no es el único rol de este operador pero es uno de ellos) +sub concat3($a, $b, $c) { + say "$a, $b, $c"; +} +concat3(|@array); #=> a, b, c + # `@array` fue "aplanado" como parte de la lista de argumento +``` + +## Contenedores + +```perl6 +## En Perl 6, valores son actualmente almacenados en "contenedores". +## El operador de asignación le pregunta al contenedor en su izquierda +## almacenar el valor a su derecha. Cuando se pasan alrededor, contenedores +## son marcados como inmutables. Esto significa que, en una función, tu +## tendrás un error si tratas de mutar uno de tus argumentos. +## Si realmente necesitas hacerlo, puedes preguntar por un contenedor +## mutable usando `is rw`: +sub mutar($n is rw) { + $n++; + say "¡\$n es ahora $n!"; +} + +my $m = 42; +mutar $m; # ¡$n es ahora 43! + +## Esto funciona porque estamos pasando el contenedor $m para mutarlo. Si +## intentamos pasar un número en vez de pasar una variable, no funcionará +## dado que no contenedor ha sido pasado y números enteros son inmutables +## por naturaleza: + +mutar 42; # Parámetro '$n' esperaba un contenedor mutable, + # pero recibió un valor Int + +## Si en cambio quieres una copia, debes usar `is copy`. + +## Por si misma, una subrutina devuelve un contenedor, lo que significa +## que puede ser marcada con rw: +my $x = 42; +sub x-almacena() is rw { $x } +x-almacena() = 52; # En este caso, los paréntesis son mandatorios + # (porque de otra forma, Perl 6 piensa que la función + # `x-almacena` es un identificador). +say $x; #=> 52 +``` + +## Estructuras de control +### Condicionales + +```perl6 +## - `if` +## Antes de hablar acerca de `if`, necesitamos saber cuales valores son +## "Truthy" (representa True (verdadero)), y cuales son "Falsey" +## (o "Falsy") -- representa False (falso). Solo estos valores son +## Falsey: 0, (), {}, "", Nil, un tipo (como `Str` o`Int`) y +## por supuesto False. Todos los valores son Truthy. +if True { + say "¡Es verdadero!"; +} + +unless False { + say "¡No es falso!"; +} + +## Como puedes observar, no necesitas paréntesis alrededor de condiciones. +## Sin embargo, necesitas las llaves `{}` alrededor del cuerpo de un bloque: +# if (true) say; # !Esto no funciona! + +## También puedes usar sus versiones sufijos seguidas por la palabra clave: +say "Un poco verdadero" if True; + +## - La condicional ternaria, "?? !!" (como `x ? y : z` en otros lenguajes) +## devuelve $valor-si-verdadera si la condición es verdadera y +## $valor-si-falsa si es falsa. +## my $resultado = $valor condición ?? $valor-si-verdadera !! $valor-si-falsa; + +my $edad = 30; +say $edad > 18 ?? "Eres un adulto" !! "Eres menor de 18"; +``` + +### given/when, ó switch + +```perl6 +## - `given`-`when` se parece al `switch` de otros lenguajes, pero es más +## poderoso gracias a la coincidencia inteligente ("smart matching" en inglés) +## y la "variable tópica" $_ de Perl. +## +## Esta variable ($_) contiene los argumentos por defecto de un bloque, +## la iteración actual de un loop (a menos que sea explícitamente +## nombrado), etc. +## +## `given` simplemente pone su argumento en `$_` (como un bloque lo haría), +## y `when` lo compara usando el operador de "coincidencia inteligente" (`~~`). +## +## Dado que otras construcciones de Perl 6 usan esta variable (por ejemplo, +## el bucle `for`, bloques, etc), esto se significa que el poderoso `when` no +## solo se aplica con un `given`, sino que se puede usar en cualquier +## lugar donde exista una variable `$_`. + +given "foo bar" { + say $_; #=> foo bar + when /foo/ { # No te preocupies acerca de la coincidencia inteligente – + # solo ten presente que `when` la usa. + # Esto es equivalente a `if $_ ~~ /foo/`. + say "¡Yay!"; + } + when $_.chars > 50 { # coincidencia inteligente con cualquier cosa True es True, + # i.e. (`$a ~~ True`) + # por lo tanto puedes también poner condiciones "normales". + # Este `when` es equivalente a este `if`: + # if $_ ~~ ($_.chars > 50) {...} + # que significa: + # if $_.chars > 50 {...} + say "¡Una cadena de texto bien larga!"; + } + default { # lo mismo que `when *` (usando la Whatever Star) + say "Algo más"; + } +} +``` + +### Construcciones de bucle + +```perl6 +## - `loop` es un bucle infinito si no le pasas sus argumentos, +## pero también puede ser un bucle for al estilo de C: +loop { + say "¡Este es un bucle infinito!"; + last; # last interrumpe el bucle, como la palabra clave `break` + # en otros lenguajes. +} + +loop (my $i = 0; $i < 5; $i++) { + next if $i == 3; # `next` salta a la siguiente iteración, al igual + # que `continue` en otros lenguajes. Ten en cuenta que + # también puedes usar la condicionales postfix (sufijas) + # bucles, etc. + say "¡Este es un bucle al estilo de C!"; +} + +## - `for` - Hace iteraciones en un array +for @array -> $variable { + say "¡He conseguido una $variable!"; +} + +## Como vimos con `given`, la variable de una "iteración actual" por defecto +## es `$_`. Esto significa que puedes usar `when` en un bucle `for` como +## normalmente lo harías con `given`. +for @array { + say "he conseguido a $_"; + + .say; # Esto es también permitido. + # Una invocación con punto (dot call) sin "tópico" (recibidor) es + # enviada a `$_` por defecto. + $_.say; # lo mismo de arriba, lo cual es equivalente. +} + +for @array { + # Puedes... + next if $_ == 3; # Saltar a la siguiente iteración (`continue` en + # lenguages parecido a C) + redo if $_ == 4; # Re-hacer la iteración, manteniendo la + # misma variable tópica (`$_`) + last if $_ == 5; # Salir fuera del bucle (como `break` + # en lenguages parecido a C) +} + +## La sintaxis de "bloque puntiagudo" no es específica al bucle for. +## Es solo una manera de expresar un bloque en Perl 6. +if computación-larga() -> $resultado { + say "El resultado es $resultado"; +} +``` + +## Operadores + +```perl6 +## Dados que los lenguajes de la familia Perl son lenguages basados +## mayormente en operadores, los operadores de Perl 6 son actualmente +## subrutinas un poco cómicas en las categorías sintácticas. Por ejemplo, +## infix:<+> (adición) o prefix:<!> (bool not). + +## Las categorías son: +## - "prefix" (prefijo): anterior a (como `!` en `!True`). +## - "postfix" (sufijo): posterior a (como `++` en `$a++`). +## - "infix" (infijo): en medio de (como `*` en `4 * 3`). +## - "circumfix" (circunfijo): alrededor de (como `[`-`]` en `[1, 2]`). +## - "post-circumfix" (pos-circunfijo): alrededor de un término, +## posterior a otro término. +## (como `{`-`}` en `%hash{'key'}`) + +## La lista de asociatividad y precedencia se explica más abajo. + +## ¡Bueno, ya estás listo(a)! + +## * Chequeando igualdad + +## - `==` se usa en comparaciones numéricas. +3 == 4; # Falso +3 != 4; # Verdadero + +## - `eq` se usa en comparaciones de cadenas de texto. +'a' eq 'b'; +'a' ne 'b'; # no igual +'a' !eq 'b'; # lo mismo que lo anterior + +## - `eqv` es equivalencia canónica (or "igualdad profunda") +(1, 2) eqv (1, 3); + +## - Operador de coincidencia inteligente (smart matching): `~~` +## Asocia (aliasing en inglés) el lado izquierda a la variable $_ +## y después evalúa el lado derecho. +## Aquí algunas comparaciones semánticas comunes: + +## Igualdad de cadena de texto o numérica + +'Foo' ~~ 'Foo'; # True si las cadenas de texto son iguales. +12.5 ~~ 12.50; # True si los números son iguales. + +## Regex - Para la comparación de una expresión regular en contra +## del lado izquierdo. Devuelve un objeto (Match), el cual evalúa +## como True si el regex coincide con el patrón. + +my $obj = 'abc' ~~ /a/; +say $obj; # 「a」 +say $obj.WHAT; # (Match) + +## Hashes +'llave' ~~ %hash; # True si la llave existe en el hash + +## Tipo - Chequea si el lado izquierdo "tiene un tipo" (puede chequear +## superclases y roles) + +1 ~~ Int; # True (1 es un número entero) + +## Coincidencia inteligente contra un booleano siempre devuelve ese +## booleano (y lanzará una advertencia). + +1 ~~ True; # True +False ~~ True; # True + +## La sintaxis general es $arg ~~ &función-returnando-bool; +## Para una lista completa de combinaciones, usa esta tabla: +## http://perlcabal.org/syn/S03.html#Smart_matching + +## También, por supuesto, tienes `<`, `<=`, `>`, `>=`. +## Sus equivalentes para cadenas de texto están disponibles: +## `lt`, `le`, `gt`, `ge`. +3 > 4; + +## * Constructores de rango +3 .. 7; # 3 a 7, ambos incluidos +## `^` en cualquier lado excluye a ese lado: +3 ^..^ 7; # 3 a 7, no incluidos (básicamente `4 .. 6`) +## Esto también funciona como un atajo para `0..^N`: +^10; # significa 0..^10 + +## Esto también nos permite demostrar que Perl 6 tiene arrays +## ociosos/infinitos, usando la Whatever Star: +my @array = 1..*; # 1 al Infinito! `1..Inf` es lo mismo. +say @array[^10]; # puedes pasar arrays como subíndices y devolverá + # un array de resultados. Esto imprimirá + # "1 2 3 4 5 6 7 8 9 10" (y no se quedaré sin memoria!) +## Nota: Al leer una lista infinita, Perl 6 "cosificará" los elementos que +## necesita y los mantendrá en la memoria. Ellos no serán calculados más de +## una vez. Tampoco calculará más elementos de los que necesita. + +## Un índice de array también puede ser una clausura ("closure" en inglés). +## Será llamada con la longitud como el argumento +say join(' ', @array[15..*]); #=> 15 16 17 18 19 +## lo que es equivalente a: +say join(' ', @array[-> $n { 15..$n }]); +## Nota: Si tratas de hacer cualquiera de esos con un array infinito, +## provocará un array infinito (tu programa nunca terminará) + +## Puedes usar eso en los lugares que esperaría, como durante la asignación +## a un array +my @números = ^20; + +## Aquí los números son incrementados por "6"; más acerca del +## operador `...` adelante. +my @seq = 3, 9 ... * > 95; # 3 9 15 21 27 [...] 81 87 93 99; +@números[5..*] = 3, 9 ... *; # aunque la secuencia es infinita, + # solo los 15 números necesarios será calculados. +say @números; #=> 0 1 2 3 4 3 9 15 21 [...] 81 87 + # (solamente 20 valores) + +## * And &&, Or || +3 && 4; # 4, el cual es Truthy. Invoca `.Bool` en `4` y obtiene `True`. +0 || False; # False. Invoca `.Bool` en `0` + +## * Versiones circuito corto de lo de arriba +## && Devuelve el primer argumento que evalúa a False, o el último. + +my ( $a, $b, $c ) = 1, 0, 2; +$a && $b && $c; # Devuelve 0, el primer valor que es False + +## || Devuelve el primer argumento que evalúa a True. +$b || $a; # 1 + +## Y porque tu lo querrás, también tienes operadores de asignación +## compuestos: +$a *= 2; # multiplica y asigna. Equivalente a $a = $a * 2; +$b %%= 5; # divisible por y asignación. Equivalente $b = $b %% 5; +@array .= sort; # invoca el método `sort` y asigna el resultado devuelto. +``` + +## ¡Más sobre subrutinas! + +```perl6 +## Como dijimos anteriormente, Perl 6 tiene subrutinas realmente poderosas. +## Veremos unos conceptos claves que la hacen mejores que en cualquier otro +## lenguaje :-). +``` + +### !Desempacado! + +```perl6 +## Es la abilidad de extraer arrays y llaves (También conocido como +## "destructuring"). También funcionará en `my` y en las listas de parámetros. +my ($f, $g) = 1, 2; +say $f; #=> 1 +my ($, $, $h) = 1, 2, 3; # mantiene los anónimos no interesante +say $h; #=> 3 + +my ($cabeza, *@cola) = 1, 2, 3; # Sí, es lo mismo que con subrutinas "slurpy" +my (*@small) = 1; + +sub desempacar_array(@array [$fst, $snd]) { + say "Mi primero es $fst, mi segundo es $snd! De todo en todo, soy un @array[]."; + # (^ recuerda que `[]` interpola el array) +} +desempacar_array(@cola); #=> My first is 2, my second is 3 ! All in all, I'm 2 3 + + +## Si no está usando el array, puedes también mantenerlo anónimo, como un +## escalar: +sub primero-de-array(@ [$fst]) { $fst } +primero-de-array(@small); #=> 1 +primero-de-array(@tail); # Lanza un error "Demasiados argumentos posicionales + # proveídos" + # (lo que significa que el array es muy grande). + +## También puedes usar un slurp ... +sub slurp-en-array(@ [$fst, *@rest]) { # Podrías mantener `*@rest` anónimos + say $fst + @rest.elems; # `.elems` returna la longitud de una lista. + # Aquí, `@rest` es `(3,)`, since `$fst` holds the `2`. +} +slurp-en-array(@tail); #=> 3 + +## Hasta podrías hacer un extracción usando una slurpy (pero no sería útil ;-).) +sub fst(*@ [$fst]) { # o simplemente: `sub fst($fst) { ... }` + say $fst; +} +fst(1); #=> 1 +fst(1, 2); # errores con "Too many positional parameters passed" + +## También puedes desestructurar hashes (y clases, las cuales +## veremos adelante). La sintaxis es básicamente +## `%nombre-del-hash (:llave($variable-para-almacenar))`. +## El hash puede permanecer anónimos si solo necesitas los valores extraídos. +sub llave-de(% (:azul($val1), :red($val2))) { + say "Valores: $val1, $val2."; +} +## Después invócala con un hash: (necesitas mantener las llaves +## de los parejas de llave y valor para ser un hash) +llave-de({azul => 'blue', rojo => "red"}); +#llave-de(%hash); # lo mismo (para un `%hash` equivalente) + +## La última expresión de una subrutina es devuelta inmediatamente +## (aunque puedes usar la palabra clave `return`): +sub siguiente-indice($n) { + $n + 1; +} +my $nuevo-n= siguiente-indice(3); # $nuevo-n es ahora 4 + +## Este es cierto para todo, excepto para las construcciones de bucles +## (debido a razones de rendimiento): Hay una razón de construir una lista +## si la vamos a desechar todos los resultados. +## Si todavías quieres construir una, puedes usar la sentencia prefijo `do`: +## (o el prefijo `gather`, el cual veremos luego) +sub lista-de($n) { + do for ^$n { # nota el uso del operador de rango `^` (`0..^N`) + $_ # iteración de bucle actual + } +} +my @list3 = lista-de(3); #=> (0, 1, 2) +``` + +### lambdas + +```perl6 +## Puedes crear una lambda con `-> {}` ("bloque puntiagudo") o `{}` ("bloque") +my &lambda = -> $argumento { "El argumento pasado a esta lambda es $argumento" } +## `-> {}` y `{}` son casi la misma cosa, excepto que la primerra puede +## tomar argumentos, y la segunda puede ser malinterpretada como un hash +## por el parseador. + +## Podemos, por ejemplo, agregar 3 a cada valor de un array usando map: +my @arraymas3 = map({ $_ + 3 }, @array); # $_ es el argumento implícito + +## Una subrutina (`sub {}`) tiene semánticas diferentes a un +## bloque (`{}` or `-> {}`): Un bloque no tiene "contexto funcional" +## (aunque puede tener argumentos), lo que significa que si quieres devolver +## algo desde un bloque, vas a returnar desde la función parental. Compara: +sub is-in(@array, $elem) { + # esto `devolverá` desde la subrutina `is-in` + # Una vez que la condición evalúa a True, el bucle terminará + map({ return True if $_ == $elem }, @array); +} +sub truthy-array(@array) { + # esto producirá un array de `True` Y `False`: + # (también puedes decir `anon sub` para "subrutina anónima") + map(sub ($i) { if $i { return True } else { return False } }, @array); + # ^ el `return` solo devuelve desde la `sub` +} + +## También puedes usar la "whatever star" para crear una función anónima +## (terminará con el último operador en la expresión actual) +my @arraymas3 = map(*+3, @array); # `*+3` es lo mismo que `{ $_ + 3 }` +my @arraymas3 = map(*+*+3, @array); # lo mismo que `-> $a, $b { $a + $b + 3 }` + # también `sub ($a, $b) { $a + $b + 3 }` +say (*/2)(4); #=> 2 + # Inmediatamente ejecuta la función que Whatever creó. +say ((*+3)/5)(5); #=> 1.6 + # ¡funciona hasta con los paréntesis! + +## Pero si necesitas más que un argumento (`$_`) en un bloque +## (sin depender en `-> {}`), también puedes usar la sintaxis implícita +## de argumento, `$` : +map({ $^a + $^b + 3 }, @array); # equivalente a lo siguiente: +map(sub ($a, $b) { $a + $b + 3 }, @array); # (aquí con `sub`) + +## Nota : Esos son ordernados lexicográficamente. +# `{ $^b / $^a }` es como `-> $a, $b { $b / $a }` +``` + +### Acerca de tipos... + +```perl6 +## Perl 6 es gradualmente tipado. Esto quiere decir que tu especifica el +## tipo de tus variables/argumentos/devoluciones (return), o puedes omitirlos +## y serán "Any" por defecto. +## Obviamente tienes acceso a algunas tipos básicos, como Int y Str. +## Las construcciones para declarar tipos son "class", "role", lo cual +## verás más adelante. + +## Por ahora, examinemos "subset" (subconjunto). +## Un "subset" es un "sub-tipo" con chequeos adicionales. +## Por ejemplo: "un número entero bien grande es un Int que es mayor que 500" +## Puedes especificar el tipo del que creas el subconjunto (por defecto, Any), +## y añadir chequeos adicionales con la palabra clave "where" (donde): +subset EnteroGrande of Int where * > 500; +``` + +### Despacho Múltiple (Multiple Dispatch) + +```perl6 +## Perl 6 puede decidir que variante de una subrutina invocar basado en el +## tipo de los argumento, o precondiciones arbitrarias, como con un tipo o +## un `where`: + +## con tipos +multi sub dilo(Int $n) { # nota la palabra clave `multi` aquí + say "Número: $n"; +} +multi dilo(Str $s) { # un multi es una subrutina por defecto + say "Cadena de texto: $s"; +} +dilo("azul"); # prints "Cadena de texto: azul" +dilo(True); # falla al *tiempo de compilación* con + # "calling 'dilo' will never work with arguments of types ..." + # (invocar 'dilo' nunca funcionará con argumentos de tipos ...") +## con precondición arbitraria (¿recuerdas los subconjuntos?): +multi es-grande(Int $n where * > 50) { "¡Sí!" } # usando una clausura +multi es-grande(Int $ where 10..50) { "Tal vez." } # Usando coincidencia inteligente + # (podrías usar un regexp, etc) +multi es-grande(Int $) { "No" } + +subset Par of Int where * %% 2; + +multi inpar-o-par(Par) { "Par" } # El caso principal usando el tipo. + # No nombramos los argumentos, +multi inpar-o-par($) { "Inpar" } # "else" + +## ¡Podrías despachar basado en la presencia de argumentos posicionales! +multi sin_ti-o-contigo(:$with!) { # Necesitas hacerlo mandatorio + # para despachar en contra del argumento. + say "¡Puedo vivir! Actualmente, no puedo."; +} +multi sin_ti-o-contigo { + say "Definitivamente no puedo vivir."; +} +## Esto es muy útil para muchos propósitos, como subrutinas `MAIN` (de las +## cuales hablaremos luego), y hasta el mismo lenguaje la está usando +## en muchos lugares. +## +## - `is`, por ejemplo, es actualmente un `multi sub` llamado +## `trait_mod:<is>`. +## - `is rw`, es simplemente un despacho a una función con esta signatura: +## sub trait_mod:<is>(Routine $r, :$rw!) {} +## +## (¡lo pusimos en un comentario dado que ejecutando esto sería una terrible +## idea!) +``` + +## Ámbito (Scoping) + +```perl6 +## En Perl 6, a diferencia de otros lenguajes de scripting, (tales como +## (Python, Ruby, PHP), debes declarar tus variables antes de usarlas. El +## declarador `my`, del cual aprendiste anteriormente, usa "ámbito léxical". +## Hay otros declaradores (`our`, `state`, ..., ) los cuales veremos luego. +## Esto se llama "ámbito léxico", donde en los bloques internos, +## puedes acceder variables de los bloques externos. +my $archivo-en-ámbito = 'Foo'; +sub externo { + my $ámbito-externo = 'Bar'; + sub interno { + say "$archivo-en-ámbito $ámbito-externo"; + } + &interno; # devuelve la función +} +outer()(); #=> 'Foo Bar' + +## Como puedes ver, `$archivo-en-ámbito` y `$ámbito-externo` +## fueron capturados. Pero si intentaramos usar `$bar` fuera de `foo`, +## la variable estaría indefinida (y obtendrías un error al tiempo de +## compilación). +``` + +## Twigils + +```perl6 +## Hay muchos `twigils` especiales (sigilos compuestos) en Perl 6. +## Los twigils definen el ámbito de las variables. +## Los twigils * y ? funcionan con variables regulares: +## * Variable dinámica +## ? Variable al tiempo de compilación +## Los twigils ! y . son usados con los objetos de Perl 6: +## ! Atributo (miembro de la clase) +## . Método (no una variable realmente) + +## El twigil `*`: Ámbito dinámico +## Estas variables usan el twigil `*` para marcar variables con ámbito +## dinámico. Variables con ámbito dinámico son buscadas a través del +## invocador, no a través del ámbito externo. + +my $*ambito_din_1 = 1; +my $*ambito_din_2 = 10; + +sub di_ambito { + say "$*ambito_din_1 $*ambito_din_2"; +} + +sub invoca_a_di_ambito { + my $*ambito_din_1 = 25; # Define a $*ambito_din_1 solo en esta subrutina. + $*ambito_din_2 = 100; # Cambiará el valor de la variable en ámbito. + di_ambito(); #=> 25 100 $*ambito_din_1 y 2 serán buscadas en la invocación. + # Se usa el valor de $*ambito_din_1 desde el ámbito léxico de esta + # subrutina aunque los bloques no están anidados (están anidados por + # invocación). +} +di_ambito(); #=> 1 10 +invoca_a_di_ambito(); #=> 25 100 + # Se usa a $*ambito_din_1 como fue definida en invoca_a_di_ambito + # aunque la estamos invocando desde afuera. +di_ambito(); #=> 1 100 Cambiamos el valor de $*ambito_din_2 en invoca_a_di_ambito + # por lo tanto su valor a cambiado. +``` + +## Modelo de Objeto + +```perl6 +## Para invocar a un método en un objeto, agrega un punto seguido por el +## nombre del objeto: +## => $object.method +## Las classes son declaradas usando la palabra clave `class`. Los atributos +## son declarados con la palabra clave `has`, y los métodos con `method`. +## Cada atributo que es privado usa el twigil `!`. Por ejemplo: `$!attr`. +## Atributos públicos inmutables usan el twigil `.` (los puedes hacer +## mutables con `is rw`). +## La manera más fácil de recordar el twigil `$.` is comparándolo +## con como los métodos son llamados. + +## El modelo de objeto de Perl 6 ("SixModel") es muy flexible, y te permite +## agregar métodos dinámicamente, cambiar la semántica, etc ... +## (no hablaremos de todo esto aquí. Por lo tanto, refiérete a: +## https://docs.perl6.org/language/objects.html). + +class Clase-Atrib { + has $.atrib; # `$.atrib` es inmutable. + # Desde dentro de la clase, usa `$!atrib` para modificarlo. + has $.otro-atrib is rw; # Puedes marcar un atributo como público con `rw`. + has Int $!atrib-privado = 10; + + method devolver-valor { + $.atrib + $!atrib-privado; + } + + method asignar-valor($param) { # Métodos pueden tomar parámetros. + $!attrib = $param; # Esto funciona porque `$!` es siempre mutable. + # $.attrib = $param; # Incorrecto: No puedes usar la versión inmutable `$.`. + + $.otro-atrib = 5; # Esto funciona porque `$.otro-atrib` es `rw`. + } + + method !metodo-privado { + say "Este método es privado para la clase !"; + } +}; + +## Crear una nueva instancia de Clase-Atrib con $.atrib asignado con 5: +## Nota: No puedes asignarle un valor a atrib-privado desde aquí (más de +## esto adelante). +my $class-obj = Clase-Atrib.new(atrib => 5); +say $class-obj.devolver-valor; #=> 5 +# $class-obj.atrib = 5; # Esto falla porque `has $.atrib` es inmutable +$class-obj.otro-atrib = 10; # En cambio, esto funciona porque el atributo + # público es mutable (`rw`). +``` + +### Herencia de Objeto + +```perl6 +## Perl 6 también tiene herencia (junto a herencia múltiple) +## Mientras los métodos declarados con `method` son heredados, aquellos +## declarados con `submethod` no lo son. +## Submétodos son útiles para la construcción y destrucción de tareas, +## tales como BUILD, o métodos que deben ser anulados por subtipos. +## Aprenderemos acerca de BUILD más adelante. + +class Padre { + has $.edad; + has $.nombre; + # Este submétodo no será heredado por la clase Niño. + submethod color-favorito { + say "Mi color favorito es Azul"; + } + # Este método será heredado + method hablar { say "Hola, mi nombre es $!nombre" } +} +# Herencia usa la palabra clave `is` +class Niño is Padre { + method hablar { say "Goo goo ga ga" } + # Este método opaca el método `hablar` de Padre. + # Este niño no ha aprendido a hablar todavía. +} +my Padre $Richard .= new(edad => 40, nombre => 'Richard'); +$Richard.color-favorito; #=> "Mi color favorito es Azul" +$Richard.hablar; #=> "Hola, mi nombre es Richard" +## $Richard es capaz de acceder el submétodo; él sabe como decir su nombre. + +my Niño $Madison .= new(edad => 1, nombre => 'Madison'); +$Madison.hablar; # imprime "Goo goo ga ga" dado que el método fue cambiado + # en la clase Niño. +# $Madison.color-favorito # no funciona porque no es heredado + +## Cuando se usa `my T $var` (donde `T` es el nombre de la clase), `$var` +## inicia con `T` en si misma, por lo tanto puedes invocar `new` en `$var`. +## (`.=` es sólo la invocación por punto y el operador de asignación: +## `$a .= b` es lo mismo que `$a = $a.b`) +## Por ejemplo, la instancia $Richard pudo también haber sido declarada así: +## my $Richard = Padre.new(edad => 40, nombre => 'Richard'); + +## También observa que `BUILD` (el método invocado dentro de `new`) +## asignará propiedades de la clase padre, por lo que puedes pasar +## `val => 5`. +``` + +### Roles, o Mixins + +```perl6 +## Roles son suportados también (comúnmente llamados Mixins en otros +## lenguajes) +role PrintableVal { + has $!counter = 0; + method print { + say $.val; + } +} + +## Se "importa" un mixin (un "role") con "does": +class Item does PrintableVal { + has $.val; + + ## Cuando se utiliza `does`, un `rol` se mezcla en al clase literalmente: + ## los métodos y atributos se ponen juntos, lo que significa que una clase + ## puede acceder los métodos y atributos privados de su rol (pero no lo inverso!): + method access { + say $!counter++; + } + + ## Sin embargo, esto: + ## method print {} + ## es SÓLO válido cuando `print` no es una `multi` con el mismo dispacho. + ## (esto significa que una clase padre puede opacar una `multi print() {}` + ## de su clase hijo/a, pero es un error sin un rol lo hace) + + ## NOTA: Puedes usar un rol como una clase (con `is ROLE`). En este caso, + ## métodos serán opacados, dado que el compilador considerará `ROLE` + ## como una clase. +} +``` + +## Excepciones + +```perl6 +## Excepciones están construidas al tope de las clases, en el paquete +## `X` (como `X::IO`). +## En Perl 6, excepciones son lanzadas automáticamente. +open 'foo'; #=> Failed to open file foo: no such file or directory +## También imprimirá la línea donde el error fue lanzado y otra información +## concerniente al error. + +## Puedes lanzar una excepción usando `die`: +die 'Error!'; #=> Error! + +## O más explícitamente: +die X::AdHoc.new(payload => 'Error!'); + +## En Perl 6, `orelse` es similar al operador `or`, excepto que solamente +## coincide con variables indefinidas, en cambio de cualquier cosa +## que evalúa a falso. +## Valores indefinidos incluyen: `Nil`, `Mu` y `Failure`, también como +## `Int`, `Str` y otros tipos que no han sido inicializados a ningún valor +## todavía. +## Puedes chequear si algo está definido o no usando el método defined: +my $no-inicializada; +say $no-inicializada.defined; #=> False +## Al usar `orelse`, se desarmará la excepción y creará un alias de dicho +## fallo en $_ +## Esto evitará que sea automáticamente manejado e imprima una marejada de +## mensajes de errores en la pantalla. +## Podemos usar el método de excepción en $_ para acceder la excepción: +open 'foo' orelse say "Algo pasó {.exception}"; + +## Esto también funciona: +open 'foo' orelse say "Algo pasó $_"; #=> Algo pasó + #=> Failed to open file foo: no such file or directory +## Ambos ejemplos anteriores funcionan pero en caso de que consigamos un +## objeto desde el lado izquierdo que no es un fallo, probablemente +## obtendremos una advertencia. Más abajo vemos como usar `try` y `CATCH` +## para ser más expecíficos con las excepciones que capturamos. +``` + +### Usando `try` y `CATCH` + +```perl6 +## Al usar `try` y `CATCH`, puedes contener y manejar excepciones sin +## interrumpir el resto del programa. `try` asignará la última excepción +## a la variable especial `$!`. +## Nota: Esto no tiene ninguna relación con las variables $!. + +try open 'foo'; +say "Bueno, lo intenté! $!" if defined $!; #=> Bueno, lo intenté! Failed to open file + #foo: no such file or directory +## Ahora, ¿qué debemos hacer si queremos más control sobre la excepción? +## A diferencia de otros lenguajes, en Perl 6 se pone el bloque `CATCH` +## *dentro* del bloque a intentar (`try`). Similarmente como $_ fue asignada +## cuando 'disarmamos' la excepción con `orelse`, también usamos $_ en el +## bloque CATCH. +## Nota: ($! es solo asignada *después* del bloque `try`) +## Por defecto, un bloque `try` tiene un bloque `CATCH` que captura +## cualquier excepción (`CATCH { default {} }`). + +try { my $a = (0 %% 0); CATCH { say "Algo pasó: $_" } } + #=> Algo pasó: Attempt to divide by zero using infix:<%%> + +## Puedes redefinir lo anterior usando `when` y (`default`) +## para manejar las excepciones que desees: +try { + open 'foo'; + CATCH { # En el bloque `CATCH`, la excepción es asignada a $_ + when X::AdHoc { say "Error: $_" } + #=>Error: Failed to open file /dir/foo: no such file or directory + + ## Cualquier otra excepción será levantada de nuevo, dado que no + ## tenemos un `default`. + ## Básicamente, si un `when` + ## Basically, if a `when` matches (or there's a `default`) marks the + ## exception as + ## "handled" so that it doesn't get re-thrown from the `CATCH`. + ## You still can re-throw the exception (see below) by hand. + } +} + +## En Perl 6, excepciones poseen ciertas sutilezas. Algunas +## subrutinas en Perl 6 devuelven un `Failure`, el cual es un tipo de +## "excepción no levantada". Ellas no son levantadas hasta que tu intentas +## mirar a sus contenidos, a menos que invoques `.Bool`/`.defined` sobre +## ellas - entonces, son manejadas. +## (el método `.handled` es `rw`, por lo que puedes marcarlo como `False` +## por ti mismo) +## Puedes levantar un `Failure` usando `fail`. Nota que si el pragma +## `use fatal` estás siendo utilizado, `fail` levantará una excepión (como +## `die`). +fail "foo"; # No estamos intentando acceder el valor, por lo tanto no problema. +try { + fail "foo"; + CATCH { + default { say "Levantó un error porque intentamos acceder el valor del fallo!" } + } +} + +## También hay otro tipo de excepción: Excepciones de control. +## Esas son excepciones "buenas", las cuales suceden cuando cambias el flujo +## de tu programa, usando operadores como `return`, `next` or `last`. +## Puedes capturarlas con `CONTROL` (no lista un 100% en Rakudo todavía). +``` + +## Paquetes + +```perl6 +## Paquetes son una manera de reusar código. Paquetes son como +## "espacio de nombres" (namespaces en inglés), y cualquier elemento del +## modelo seis (`module`, `role`, `class`, `grammar`, `subset` y `enum`) +## son paquetes por ellos mismos. (Los paquetes son como el mínimo común +## denominador) +## Los paquetes son importantes - especialmente dado que Perl es bien +## reconocido por CPAN, the Comprehensive Perl Archive Nertwork. + +## Puedes usar un módulo (traer sus declaraciones al ámbito) con `use` +use JSON::Tiny; # si intalaste Rakudo* o Panda, tendrás este módulo +say from-json('[1]').perl; #=> [1] + +## A diferencia de Perl 5, no deberías declarar paquetes usando +## la palabra clave `package`. En vez, usa `class Nombre::Paquete::Aquí;` +## para declarar una clase, o si solamente quieres exportar +## variables/subrutinas, puedes usar `module`. + +module Hello::World { # forma de llaves + # Si `Hello` no existe todavía, solamente será una cola ("stub"), + # que puede ser redeclarada más tarde. + # ... declaraciones aquí ... +} +unit module Parse::Text; # forma de ámbito de archivo + +grammar Parse::Text::Grammar { # Una gramática (grammar en inglés) es un paquete, + # en el cual puedes usar `use` +} # Aprenderás más acerca de gramáticas en la sección de regex + +## Como se dijo anteriormente, cualquier parte del modelo seis es también un +## paquete. Dado que `JSON::Tiny` usa su propia clase `JSON::Tiny::Actions`, +## tu puedes usarla de la manera siguiente: +my $acciones = JSON::Tiny::Actions.new; + +## Veremos como exportar variables y subrutinas en la siguiente parte: +``` + +## Declaradores + +```perl6 +## En Perl 6, tu obtienes diferentes comportamientos basado en como declaras +## una variable. +## Ya has visto `my` y `has`, ahora exploraremos el resto. + +## * las declaraciones `our` ocurren al tiempo `INIT` (ve "Phasers" más abajo) +## Es como `my`, pero también crea una variable paquete. +## (Todas las cosas relacionadas con paquetes (`class`, `role`, etc) son +## `our` por defecto) +module Var::Incrementar { + our $nuestra-var = 1; # Nota: No puedes colocar una restricción de tipo + my $mi-var = 22; # como Int (por ejemplo) en una variable `our`. + our sub Inc { + + our sub disponible { # Si tratas de hacer subrutinas internas `our`... + # Mejor que sepas lo que haces (No lo haga!). + say "No hagas eso. En serio. Estás jugando con fuego y te quemarás."; + } + + my sub no-disponible { # `my sub` es por defecto + say "No puedes acceder aquí desde fuera. Soy 'my'!"; + } + say ++$nuestra-var; # Incrementa la variable paquete y muestra su valor + } + +} +say $Var::Incrementar::nuestra-var; #=> 1 Esto funciona +say $Var::Incrementar::mi-var; #=> (Any) Esto no funcionará. + +Var::Incrementar::Inc; #=> 2 +Var::Incrementar::Inc; #=> 3 # Nota como el valor de $nuestra-var fue + # retenido +Var::Incrementar::no-disponible; #=> Could not find symbol '&no-disponible' + +## * `constant` (ocurre al tiempo `BEGIN`) +## Puedes usar la palabra clave `constant` para declarar una +## variable/símbolo al tiempo de compilación: +constant Pi = 3.14; +constant $var = 1; + +## Y por si te estás preguntando, sí, también puede contener listas infinitas. +constant porque-no = 5, 15 ... *; +say porque-no[^5]; #=> 5 15 25 35 45 + +## * `state` (ocurre al tiempo de ejecución, pero una sola vez) +## Variables "states" son solo inicializadas una vez. +## (ellas existen en otros lenguaje como `static` en C) +sub aleatorio-fijo { + state $valor = rand; + say $valor; +} +aleatorio-fijo for ^10; # imprimirá el mismo número 10 veces + +## Nota, sin embargo, que ellas existen separadamente en diferentes contextos. +## Si declaras una función con un `state` dentro de un bucle, recreará la +## variable por cada iteración del bucle. Observa: +for ^5 -> $a { + sub foo { + state $valor = rand; # Esto imprimirá un valor diferente + # por cada valor de `$a` + } + for ^5 -> $b { + say foo; # Esto imprimirá el mismo valor 5 veces, pero sólo 5. + # La siguiente iteración ejecutará `rand` nuevamente. + } +} +``` + +## Phasers + +```perl6 +## Un phaser en Perl 6 es un bloque que ocurre a determinados puntos de tiempo +## en tu programa. Se les llama phaser porque marca un cambio en la fase de +## de tu programa. Por ejemplo, cuando el programa es compilado, un bucle +## for se ejecuta, dejas un bloque, o una excepción se levanta. +## (¡`CATCH` es actualmente un phaser!) +## Algunos de ellos pueden ser utilizados por sus valores devueltos, otros +## no pueden (aquellos que tiene un "[*]" al inicio de su texto de +## explicación). +## ¡Tomemos una mirada! + +## * Phasers al tiempo de compilación +BEGIN { say "[*] Se ejecuta al tiempo de compilación, " ~ + "tan pronto como sea posible, una sola vez" } +CHECK { say "[*] Se ejecuta al tiempo de compilación, " ~ + "tan tarde como sea posible, una sola vez" } + +## * Phasers al tiempo de ejecución +INIT { say "[*] Se ejecuta al tiempo de ejecución, " ~ + "tan pronto como sea posible, una sola vez" } +END { say "Se ejecuta al tiempo de ejecución, " ~ + "tan tarde como sea posible, una sola vez" } + +## * Phasers de bloques +ENTER { say "[*] Se ejecuta cada vez que entra en un bloque, " ~ + "se repite en bloques de bucle" } +LEAVE { say "Se ejecuta cada vez que abandona un bloque, incluyendo " ~ + "cuando una excepción ocurre. Se repite en bloques de bucle"} + +PRE { + say "Impone una precondición a cada entrada de un bloque, " ~ + "antes que ENTER (especialmente útil para bucles)"; + say "Si este bloque no returna un valor truthy, " ~ + "una excepción del tipo X::Phaser::PrePost será levantada."; +} + +## Ejemplos: +for 0..2 { + PRE { $_ > 1 } # Esto fallará con un "Precondition failed" +} + +POST { + say "Impone una postcondAsserts a poscondición a la salida de un bloque, " ~ + "después de LEAVE (especialmente útil para bucles)"; + say "Si este bloque no returna un valor truthy, " ~ + "una excepción del tipo X::Phaser::PrePost será levantada, como con PRE."; +} +for 0..2 { + POST { $_ < 2 } # Esto fallará con un "Postcondition failed" +} + +## * Phasers de bloques/excepciones +sub { + KEEP { say "Se ejecuta cuando sales de un bloque exitosamente + (sin lanzar un excepción)" } + UNDO { say "Se ejecuta cuando sale de bloque sin éxito + (al lanzar una excepción)" } +} + +## * Phasers de bucle +for ^5 { + FIRST { say "[*] La primera vez que un bucle se ejecuta, antes que ENTER" } + NEXT { say "Al tiempo de la continuación del bucle, antes que LEAVE" } + LAST { say "Al tiempo de la terminación del bucle, después de LEAVE" } +} + +## * Phasers de rol/clase +COMPOSE { "Cuando un rol es compuesto en una clase. /!\ NO IMPLEMENTADO TODAVÍA" } + +## Ellos permite pequeños trucos o código brillante...: +say "Este código tomó " ~ (time - CHECK time) ~ "s para compilar"; + +## ... o brillante organización: +sub do-db-stuff { + $db.start-transaction; # comienza una transacción nueva + KEEP $db.commit; # commit (procede con) la transacción si todo estuvo bien + UNDO $db.rollback; # o retrocede si todo falló +} +``` + +## Prefijos de sentencias + +```perl6 +## Los prefijos de sentencias actúan como los phasers: Ellos afectan el +## comportamiento del siguiente código. +## Debido a que son ejecutados en línea con el código ejecutable, ellos +## se escriben en letras minúsculas. (`try` and `start` están teoréticamente +## en esa lista, pero serán explicados en otra parte) +## Nota: Ningunos de estos (excepto `start`) necesitan las llaves `{` y `}`. + +## - `do` (el cual ya viste) - ejecuta un bloque o una sentencia como un +## término. +## Normalmente no puedes usar una sentencia como un valor (o término): +## +## my $valor = if True { 1 } # `if` es una sentencia - error del parseador +## +## Esto funciona: +my $a = do if True { 5 } # con `do`, `if` ahora se comporta como un término. + +## - `once` - se asegura que una porción de código se ejecute una sola vez. +for ^5 { once say 1 }; #=> 1 + # solo imprime ... una sola vez. +## Al igual que `state`, ellos son clonados por ámbito +for ^5 { sub { once say 1 }() } #=> 1 1 1 1 1 + # Imprime una sola vez por ámbito léxico + +## - `gather` - Hilo de co-rutina +## `gather` te permite tomar (`take`) varios valores en un array, +## al igual que `do`. Encima de esto, te permite tomar cualquier expresión. +say gather for ^5 { + take $_ * 3 - 1; + take $_ * 3 + 1; +} #=> -1 1 2 4 5 7 8 10 11 13 +say join ',', gather if False { + take 1; + take 2; + take 3; +} # no imprime nada. + +## - `eager` - Evalúa una sentencia ávidamente (forza contexto ávido) +## No intentes esto en casa: +## +## eager 1..*; # esto probablemente se colgará por un momento +## # (y podría fallar...). +## +## Pero considera lo siguiente: +constant tres-veces = gather for ^3 { say take $_ }; # No imprime nada + +## frente a esto: +constant tres-veces = eager gather for ^3 { say take $_ }; #=> 0 1 2 +``` + +## Iterables + +```perl6 +## En Perl 6, los iterables son objetos que pueden ser iterados similar +## a la construcción `for`. +## `flat`, aplana iterables: +say (1, 10, (20, 10) ); #=> (1 10 (20 10)) Nota como la agrupación se mantiene +say (1, 10, (20, 10) ).flat; #=> (1 10 20 10) Ahora el iterable es plano + +## - `lazy` - Aplaza la evaluación actual hasta que el valor sea requirido +## (forza contexto perezoso) +my @lazy-array = (1..100).lazy; +say @lazy-array.is-lazy; #=> True # Chequea por "pereza" con el método `is-lazy`. +say @lazy-array; #=> [...] No se ha iterado sobre la lista +for @lazy-array { .print }; # Esto funciona y hará tanto trabajo como sea necesario. + +[//]: # ( TODO explica que gather/take y map son todos perezosos) +## - `sink` - Un `eager` que desecha los resultados (forza el contexto sink) +constant nilthingie = sink for ^3 { .say } #=> 0 1 2 +say nilthingie.perl; #=> Nil + +## - `quietly` - Un bloque `quietly` reprime las advertencias: +quietly { warn 'Esto es una advertencia!' }; #=> No salida + +## - `contend` - Intenta efectos secundarios debajo de STM +## ¡No implementado todavía! +``` + +## ¡Más operadores! + +```perl6 +## ¡Todo el mundo ama los operadores! Tengamos más de ellos. + +## La lista de precedencia puede ser encontrada aquí: +## https://docs.perl6.org/language/operators#Operator_Precedence +## Pero primero, necesitamos un poco de explicación acerca +## de la asociatividad: + +## * Operadores binarios: +$a ! $b ! $c; # con asociatividad izquierda `!`, esto es `($a ! $b) ! $c` +$a ! $b ! $c; # con asociatividad derecha `!`, esto es `$a ! ($b ! $c)` +$a ! $b ! $c; # sin asociatividad `!`, esto es ilegal +$a ! $b ! $c; # con una cadena de asociatividad `!`, esto es `($a ! $b) and ($b ! $c)` +$a ! $b ! $c; # con asociatividad de lista `!`, esto es `infix:<>` + +## * Operadores unarios: +!$a! # con asociatividad izquierda `!`, esto es `(!$a)!` +!$a! # con asociatividad derecha `!`, esto es `!($a!)` +!$a! # sin asociatividad `!`, esto es ilegal +``` + +### ¡Crea tus propios operadores! + +```perl6 +## Okay, has leído todo esto y me imagino que debería mostrarte +## algo interesante. +## Te mostraré un pequeño secreto (o algo no tan secreto): +## En Perl 6, todos los operadores son actualmente solo subrutinas. + +## Puedes declarar un operador como declaras una subrutina: +sub prefix:<ganar>($ganador) { # se refiere a las categorías de los operadores + # (exacto, es el "operador de palabras" `<>`) + say "¡$ganador ganó!"; +} +ganar "El Rey"; #=> ¡El Rey Ganó! + # (prefijo se pone delante) + +## todavías puedes invocar la subrutina con su "nombre completo": +say prefix:<!>(True); #=> False + +sub postfix:<!>(Int $n) { + [*] 2..$n; # usando el meta-operador reduce ... Ve más abajo! +} +say 5!; #=> 120 + # Operadores sufijos (postfix) van *directamente* después del témino. + # No espacios en blanco. Puedes usar paréntesis para disambiguar, + # i.e. `(5!)!` + + +sub infix:<veces>(Int $n, Block $r) { # infijo va en el medio + for ^$n { + $r(); # Necesitas los paréntesis explícitos para invocar la función + # almacenada en la variable `$r`. De lo contrario, te estaría + # refiriendo a la variable (no a la función), como con `&r`. + } +} +3 veces -> { say "hola" }; #=> hola + #=> hola + #=> hola + # Se te recomienda que ponga espacios + # alrededor de la invocación de operador infijo. + +## Para los circunfijos y pos-circunfijos +sub circumfix:<[ ]>(Int $n) { + $n ** $n +} +say [5]; #=> 3125 + # un circunfijo va alrededor. De nuevo, no espacios en blanco. + +sub postcircumfix:<{ }>(Str $s, Int $idx) { + ## un pos-circunfijo es + ## "después de un término y alrededor de algo" + $s.substr($idx, 1); +} +say "abc"{1}; #=> b + # depués del término `"abc"`, y alrededor del índice (1) + +## Esto es de gran valor -- porque todo en Perl 6 usa esto. +## Por ejemplo, para eliminar una llave de un hash, tu usas el adverbio +## `:delete` (un simple argumento con nombre debajo): +%h{$llave}:delete; +## es equivalente a: +postcircumfix:<{ }>(%h, $llave, :delete); # (puedes invocar + # operadores de esta forma) +## ¡*Todos* usan los mismos bloques básicos! +## Categorías sintácticas (prefix, infix, ...), argumentos nombrados +## (adverbios), ... - usados para construir el lenguaje - están al alcance +## de tus manos y disponibles para ti. +## (obviamente, no se te recomienda que hagas un operador de *cualquier +## cosa* -- Un gran poder conlleva una gran responsabilidad.) +``` + +### Meta-operadores! + +```perl6 +## ¡Prepárate! Prepárate porque nos estamos metiendo bien hondo +## en el agujero del conejo, y probablemente no querrás regresar a +## otros lenguajes después de leer esto. +## (Me imagino que ya no quieres a este punto). +## Meta-operadores, como su nombre lo sugiere, son operadores *compuestos*. +## Básicamente, ellos son operadores que se aplican a otros operadores. + +## * El meta-operador reduce (reducir) +## Es un meta-operador prefijo que toman una función binaria y +## una o varias listas. Sino se pasa ningún argumento, +## returna un "valor por defecto" para este operador +## (un valor sin significado) o `Any` si no hay ningún valor. +## +## De lo contrario, remueve un elemento de la(s) lista(s) uno a uno, y +## aplica la función binaria al último resultado (o al primer elemento de +## la lista y el elemento que ha sido removido). +## +## Para sumar una lista, podrías usar el meta-operador "reduce" con `+`, +## i.e.: +say [+] 1, 2, 3; #=> 6 +## es equivalente a `(1+2)+3` + +say [*] 1..5; #=> 120 +## es equivalente a `((((1*2)*3)*4)*5)`. + +## Puedes reducir con cualquier operador, no solo con operadores matemáticos. +## Por ejemplo, podrías reducir con `//` para conseguir +## el primer elemento definido de una lista: +say [//] Nil, Any, False, 1, 5; #=> False + # (Falsey, pero definido) + +## Ejemplos con valores por defecto: +say [*] (); #=> 1 +say [+] (); #=> 0 + # valores sin significado, dado que N*1=N y N+0=N. +say [//]; #=> (Any) + # No hay valor por defecto para `//`. +## También puedes invocarlo con una función de tu creación usando +## los dobles corchetes: +sub add($a, $b) { $a + $b } +say [[&add]] 1, 2, 3; #=> 6 + +## * El meta-operador zip +## Este es un meta-operador infijo que también puede ser usado como un +## operador "normal". Toma una función binaria opcional (por defecto, solo +## crear un par), y remueve un valor de cada array e invoca su función +## binaria hasta que no tenga más elementos disponibles. Al final, returna +## un array con todos estos nuevos elementos. +(1, 2) Z (3, 4); # ((1, 3), (2, 4)), dado que por defecto, la función + # crea un array. +1..3 Z+ 4..6; # (5, 7, 9), usando la función personalizada infix:<+> + +## Dado que `Z` tiene asociatividad de lista (ve la lista más arriba), +## puedes usarlo en más de una lista +(True, False) Z|| (False, False) Z|| (False, False); # (True, False) + +## Y pasa que también puedes usarlo con el meta-operador reduce: +[Z||] (True, False), (False, False), (False, False); # (True, False) + + +## Y para terminar la lista de operadores: + +## * El operador secuencia +## El operador secuencia es uno de la más poderosas características de +## Perl 6: Está compuesto, en la izquierda, de la lista que quieres que +## Perl 6 use para deducir (y podría incluir una clausura), y en la derecha, +## un valor o el predicado que dice cuando parar (o Whatever para una +## lista infinita perezosa). +my @list = 1, 2, 3 ... 10; # deducción básica +#my @list = 1, 3, 6 ... 10; # esto muere porque Perl 6 no puede deducir el final +my @list = 1, 2, 3 ...^ 10; # como con rangos, puedes excluir el último elemento + # (la iteración cuando el predicado iguala). +my @list = 1, 3, 9 ... * > 30; # puedes usar un predicado + # (con la Whatever Star, aquí). +my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalente a lo de arriba) + +my @fib = 1, 1, *+* ... *; # lista infinita perezosa de la serie fibonacci, + # computada usando una clausura! +my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalene a lo de arriba) +my @fib = 1, 1, { $^a + $^b } ... *; #(... también equivalene a lo de arriba) +## $a and $b siempre tomarán el valor anterior, queriendo decir que +## ellos comenzarán con $a = 1 y $b = 1 (valores que hemos asignado +## de antemano). Por lo tanto, $a = 1 y $b = 2 (resultado del anterior $a+$b), +## etc. + +say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55 + # (usandi un rango como el índice) +## Nota: Los elementos de un rango, una vez cosificados, no son re-calculados. +## Esta es la razón por la cual `@primes[^100]` tomará más tiempo la primera +## vez que se imprime. Después de esto, será hará en un instante. +``` + +## Expresiones Regulares + +```perl6 +## Estoy seguro que has estado esperando por esta parte. Bien, ahora que +## sabes algo acerca de Perl 6, podemos comenzar. Primeramente, tendrás +## que olvidarte acerca de "PCRE regexps" (perl-compatible regexps) +## (expresiones regulares compatible de perl). +## +## IMPORTANTE: No salte esto porque ya sabes acerca de PCRE. Son totalmente +## distintos. Algunas cosas son las mismas (como `?`, `+`, y `*`) pero +## algunas veces la semántica cambia (`|`). Asegúrate de leer esto +## cuidadosamente porque podrías trospezarte sino lo haces. +## +## Perl 6 tiene muchas características relacionadas con RegExps. Después de +## todo, Rakudo se parsea a si mismo. Primero vamos a estudiar la sintaxis +## por si misma, después hablaremos acerca de gramáticas (parecido a PEG), +## las diferencias entre los declaradores `token`, `regex`, y `rule` y +## mucho más. +## Nota aparte: Todavía tienes acceso a los regexes PCRE usando el +## mofificador `:P5` (Sin embargo, no lo discutiremos en este tutorial). +## +## En esencia, Perl 6 implementa PEG ("Parsing Expression Grammars") +## ("Parseado de Expresiones de Gramáticas") nativamente. El orden jerárquico +## para los parseos ambiguos es determinado por un examen multi-nivel de +## desempate: +## - La coincidencia de token más larga. `foo\s+` le gana a `foo` +## (por 2 o más posiciones) +## - El prefijo literal más largo. `food\w*` le gana a `foo\w*` (por 1) +## - Declaración desde la gramática más derivada a la menos derivada +## (las gramáticas son actualmente clases) +## - La declaración más temprana gana +say so 'a' ~~ /a/; #=> True +say so 'a' ~~ / a /; #=> True # ¡Más legible con los espacios! + +## Nota al lector (del traductor): +## Como pudiste haber notado, he decidido traducir "match" y sus diferentes +## formas verbales como "coincidir" y sus diferentes formas. Cuando digo que +## un regex (o regexp) coincide con cierto texto, me refiero a que el regex +## describe cierto patrón dentro del texto. Por ejemplo, el regex "cencia" +## coincide con el texto "reminiscencia", lo que significa que dentro del +## texto aparece ese patrón de caracteres (una `c`, seguida de una `e`, +## (seguida de una `n`, etc.) + +## En todos nuestros ejemplos, vamos a usar el operador de +## "coincidencia inteligente" contra una expresión regular ("regexp" or +## "regex" de aquí en adelante). Estamos convirtiendo el resultado usando `so`, +## pero en efecto, está devolviendo un objeto Match. Ellos saben como responder +## a la indexación de lista, indexación de hash, y devolver la cadena de +## texto coincidente. +## Los resultados de la coincidencia están disponible como `$/` (en +## ámbito implícito lexical). También puedes usar las variables de captura +## las cuales comienzan con 0: +## `$0`, `$1', `$2`... +## +## Nota que `~~` no hace un chequeo de inicio/final (es decir, +## el regexp puede coincider con solo un carácter de la cadena de texto). +## Explicaremos luego como hacerlo. + +## En Perl 6, puedes tener un carácter alfanumérico como un literal, +## todo lo demás debe escaparse usando una barra invertida o comillas. +say so 'a|b' ~~ / a '|' b /; # `True`. No sería lo mismo si no se escapara `|` +say so 'a|b' ~~ / a \| b /; # `True`. Otra forma de escaparlo + +## El espacio en blanco actualmente no se significa nada en un regexp, +## a menos que uses el adverbio `:s` (`:sigspace`, espacio significante). +say so 'a b c' ~~ / a b c /; #=> `False`. Espacio no significa nada aquí. +say so 'a b c' ~~ /:s a b c /; #=> `True`. Agregamos el modificador `:s` aquí. +## Si usamos solo un espacio entre cadenas de texto en un regexp, Perl 6 +## nos advertirá: +say so 'a b c' ~~ / a b c /; #=> 'False' # Espacio no significa nada aquí. +## Por favor usa comillas o el modificador :s (:sigspace) para suprimir +## esta advertencia, omitir el espacio, o cambiar el espaciamiento. Para +## arreglar esto y hacer los espacios menos ambiguos, usa por lo menos +## dos espacios entre las cadenas de texto o usa el adverbio `:s`. + +## Como vimos anteriormente, podemos incorporar `:s` dentro de los +## delimitadores de barras. También podemos ponerlos fuera de ellos si +## especificamos `m` for `match` (coincidencia): +say so 'a b c' ~~ m:s/a b c/; #=> `True` +## Al usar `m` para especificar 'match', podemos también otros delimitadore: +say so 'abc' ~~ m{a b c}; #=> `True` +say so 'abc' ~~ m[a b c]; #=> `True` + +## Usa el adverbio :i para especificar que no debería haber distinción entre +## minúsculas y mayúsculas: +say so 'ABC' ~~ m:i{a b c}; #=> `True` + +## Sin embargo, es importante para como los modificadores son aplicados +## (lo cual verás más abajo)... + +## Cuantificando - `?`, `+`, `*` y `**`. +## - `?` - 0 o 1 +so 'ac' ~~ / a b c /; # `False` +so 'ac' ~~ / a b? c /; # `True`, la "b" coincidió (apareció) 0 veces. +so 'abc' ~~ / a b? c /; # `True`, la "b" coincidió 1 vez. + +## ... Como debes saber, espacio en blancos son importante porque +## determinan en que parte del regexp es el objetivo del modificador: +so 'def' ~~ / a b c? /; # `False`. Solamente la `c` es opcional +so 'def' ~~ / a b? c /; # `False`. Espacio en blanco no es significante +so 'def' ~~ / 'abc'? /; # `True`. El grupo "abc"completo es opcional. + +## Aquí (y más abajo) el cuantificador aplica solamente a la `b` + +## - `+` - 1 o más +so 'ac' ~~ / a b+ c /; # `False`; `+` quiere por lo menos una coincidencia +so 'abc' ~~ / a b+ c /; # `True`; una es suficiente +so 'abbbbc' ~~ / a b+ c /; # `True`, coincidió con 4 "b"s + +## - `*` - 0 o más +so 'ac' ~~ / a b* c /; # `True`, todos son opcionales. +so 'abc' ~~ / a b* c /; # `True` +so 'abbbbc' ~~ / a b* c /; # `True` +so 'aec' ~~ / a b* c /; # `False`. "b"(s) son opcionales, no reemplazables. + +## - `**` - Cuantificador (sin límites) +## Si entrecierras los ojos lo suficiente, pueder ser que entiendas +## por qué la exponenciación es usada para la cantidad. +so 'abc' ~~ / a b**1 c /; # `True` (exactamente una vez) +so 'abc' ~~ / a b**1..3 c /; # `True` (entre una y tres veces) +so 'abbbc' ~~ / a b**1..3 c /; # `True` +so 'abbbbbbc' ~~ / a b**1..3 c /; # `False` (demasiado) +so 'abbbbbbc' ~~ / a b**3..* c /; # `True` (rangos infinitos no son un problema) + +## - `<[]>` - Clases de carácteres +## Las clases de carácteres son equivalentes a las clases `[]` de PCRE, +## pero usan una sintaxis de Perl 6: +say 'fooa' ~~ / f <[ o a ]>+ /; #=> 'fooa' + +## Puedes usar rangos: +say 'aeiou' ~~ / a <[ e..w ]> /; #=> 'ae' + +## Al igual que regexes normales, si quieres usar un carácter especial, +## escápalo (el último está escapando un espacio) +say 'he-he !' ~~ / 'he-' <[ a..z \! \ ]> + /; #=> 'he-he !' + +## Obtendrás una advertencia si pones nombres duplicados +## (lo cual tiene el efecto de capturar la frase escrita) +'he he' ~~ / <[ h e ' ' ]> /; # Advierte "Repeated characters found in characters + # class" + +## También puedes negarlos... (equivalenta a `[^]` en PCRE) +so 'foo' ~~ / <-[ f o ]> + /; # False + +## ... y componerlos: +so 'foo' ~~ / <[ a..z ] - [ f o ]> + /; # False (cualquier letra excepto f y o) +so 'foo' ~~ / <-[ a..z ] + [ f o ]> + /; # True (no letra excepto f and o) +so 'foo!' ~~ / <-[ a..z ] + [ f o ]> + /; # True (el signo + no reemplaza la + # parte de la izquierda) +``` + +### Grupos y Capturas + +```perl6 +## Grupo: Puedes agrupar partes de tu regexp con `[]`. +## Estos grupos *no son* capturados (como con `(?:)` en PCRE). +so 'abc' ~~ / a [ b ] c /; # `True`. El agrupamiento no hace casi nada +so 'foo012012bar' ~~ / foo [ '01' <[0..9]> ] + bar /; +## La línea anterior returna `True`. +## Coincidimos (o encotramos el patrón) "012" una o más de una vez ( +## (el signo `+` fue aplicado al grupo). +## Pero esto no va demasiado lejos, porque no podemos actualmente obtener +## devuelta el patrón que coincidió. + +## Captura: Podemos actualmente *capturar* los resultados del regexp, +## usando paréntesis. +so 'fooABCABCbar' ~~ / foo ( 'A' <[A..Z]> 'C' ) + bar /; # `True`. (usando `so` + # aquí, `$/` más abajo) + +## Ok. Comenzando con las explicaciones de grupos. Como dijimos, +### nuestra objeto `Match` está disponible en la variable `$/`: +say $/; # Imprimirá algo extraño (explicaremos luego) o + # "Nil" si nada coincidió + +## Como dijimos anteriormente, un objeto Match tiene indexación de array: +say $/[0]; #=> 「ABC」 「ABC」 + # Estos corchetes extranos son los objetos `Match`. + # Aquí, tenemos un array de ellos. +say $0; # Lo mismo que lo anterior. + +## Nuestra captura es `$0` porque es la primera y única captura en el +## regexp. Podrías estarte preguntando porque un array y la respuesta es +## simple: Algunas capturas (indezadas usando `$0`, `$/[0]` o una nombrada) +## será un array si y solo si puedes tener más de un elemento. +## (Así que, con `*`, `+` y `**` (cualquiera los operandos), pero no con `?`). +## Usemos algunos ejemplos para ver como funciona: + +## Nota: Pusimos A B C entre comillas para demostrar que el espacio en blanco +## entre ellos no es significante. Si queremos que el espacio en blanco +## *sea* significante, podemos utilizar el modificador `:sigspace`. +so 'fooABCbar' ~~ / foo ( "A" "B" "C" )? bar /; # `True` +say $/[0]; #=> 「ABC」 +say $0.WHAT; #=> (Match) + # Puede haber más de uno, por lo tanto es solo un solo objeto match. +so 'foobar' ~~ / foo ( "A" "B" "C" )? bar /; #=> True +say $0.WHAT; #=> (Any) + # Esta captura no coincidió, por lo tanto está vacía +so 'foobar' ~~ / foo ( "A" "B" "C" ) ** 0..1 bar /; # `True` +say $0.WHAT; #=> (Array) + # Un cuantificador específico siempre capturará un Array, + # puede ser un rango o un valor específico (hasta 1). + +## Las capturas son indezadas por anidación. Esto quiere decir que un grupo +## dentro de un grup estará anidado dentro de su grupo padre: `$/[0][0]`, +## para este código: +'hello-~-world' ~~ / ( 'hello' ( <[ \- \~ ]> + ) ) 'world' /; +say $/[0].Str; #=> hello~ +say $/[0][0].Str; #=> ~ + +## Esto se origina de un hecho bien simple: `$/` no contiene cadenas de +## texto, números enteros o arrays sino que solo contiene objetos Match. +## Estos objetos contienen los métodos `.list`, `.hash` y `.Str`. (Pero +## también puedes usar `match<llave>` para accesar un hash y `match[indice]` +## para accesar un array. +say $/[0].list.perl; #=> (Match.new(...),).list + # Podemos ver que es una lista de objetos Match. + # Estos contienen un montón de información: dónde la + # coincidencia comenzó o terminó, el "ast" + # (chequea las acciones más abajo), etc. + # Verás capturas nombradas más abajo con las gramáticas. + +## Alternativas - el `or` de regexes +## Advertencia: Es diferente a los regexes de PCRE. +so 'abc' ~~ / a [ b | y ] c /; # `True`. o "b" o "y". +so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviamente suficiente... + +## La diferencia entre este `|` y el otro al que estás acustombrado es LTM. +## LTM significa "Longest Token Matching", traducido libremente como +## "Coincidencia de Token Más Larga". Esto significa que el motor ("engine") +## siempre intentará coindidir tanto como sea posible en la cadena de texto. +## Básicamente, intentará el patrón más largo que concuerde con el regexp. +'foo' ~~ / fo | foo /; # `foo` porque es más largo. +## Para decidir cual parte es la "más larga", primero separa el regex en +## dos partes: +## El "prefijo declarativo" (la parte que puede ser analizada estáticamente) +## y las partes procedimentales. +## Los prefijos declarativos incluyen alternaciones (`|`), conjunciones (`&`), +## invocaciones de sub-reglas (no han sido introducidos todavía), clases de +## caracteres y cuantificadores. +## Las partes procidimentales incluyen todo lo demás: referencias a elementos +## anteriores, aserciones de código, y otras cosas que tradicionalmente no pueden +## ser representadas por regexes normales. +## +## Entonces, todas las alternativas se intentan al mismo tiempo, y la +## más larga gana. +## Ejemplos: +## DECLARATIVO | PROCEDIMENTAL +/ 'foo' \d+ [ <subrule1> || <subrule2> ] /; +## DECLARATIVO (grupos anidados no son un problema) +/ \s* [ \w & b ] [ c | d ] /; +## Sin embargo, las clausuras y la recursión (de regexes nombrados) +## son procedimentales. +## ... Hay más reglas complicadas, como la especifidad (los literales ganan +## son las clases de caracteres) ++ +## Nota: la primera coincidencia `or` todavía existen, pero ahora se +## deletrea `||` +'foo' ~~ / fo || foo /; # `fo` ahora. +``` + +## Extra: la subrutina MAIN + +```perl6 +## La subrutina `MAIN` se invoca cuando tu ejecuta un archivo de Perl 6 +## directamente. Es realmente poderosa porque Perl 6 actualmente parsea +## los argumentos y los pasas a la subrutina. También maneja argumentos +## nombrados (`--foo`) y hasta autogenerará un `--help`. +sub MAIN($nombre) { say "¡Hola, $nombre!" } +## Esto produce: +## $ perl6 cli.pl +## Uso: +## t.pl <nombre> + +## Y dado que una subrutina regular en Perl 6, puedes tener múltiples +## despachos: +## (usando un "Bool" por un argumento nombrado para que podamos hacer +## `--replace` a cambio de `--replace=1`) +subset File of Str where *.IO.d; # convierte a un objeto IO para chequear si + # un archivo existe + +multi MAIN('add', $key, $value, Bool :$replace) { ... } +multi MAIN('remove', $key) { ... } +multi MAIN('import', File, Str :$as) { ... } # omitiendo parámetros nombrados +## Esto produce: +## $ perl6 cli.pl +## Uso: +## t.pl [--replace] add <key> <value> +## t.pl remove <key> +## t.pl [--as=<Str>] import (File) +## Como puedes ver, esto es *realmente* poderoso. +## Fue tan lejos como para mostrar las constantes en líneas. +## (el tipo solo se muestra cuando el argumento `$`/ es nombrado) +``` + +## APÉNDICE A: +### Lista de cosas + +```perl6 +## Consideramos que por ahora ya sabes lo básico de Perl 6. +## Esta sección es solo para listar algunas operaciones comunes +## las cuales no están en la "parte principal" del tutorial. + +## Operadores + +## * Comparación para ordenar +## Ellos returnan un valor de los enum `Order`: `Less`, `Same` y `More` +## (los cuales representan los números -1, 0 o +1). +1 <=> 4; # comparación de orden para caracteres numéricos +'a' leg 'b'; # comparación de orden para cadenas de texto +$obj eqv $obj2; # comparación de orden usando la semántica eqv + +## * Ordenación genérica +3 before 4; # True +'b' after 'a'; # True + +## * Operador (por defecto) de circuito corto +## Al igual que `or` y `||`, pero devuelve el primer valor *defined* +## (definido): +say Any // Nil // 0 // 5; #=> 0 + +## * Circuito corto exclusivo or (XOR) +## Devuelve `True` si uno (y solo uno) de sus argumentos es verdadero: +say True ^^ False; #=> True + +## * Flip Flop +## Los operadores flip flop (`ff` y `fff`, equivalente a `..`/`...` en P5) +## son operadores que toman dos predicados para evalualarlos: +## Ellos son `False` hasta que su lado izquierdo devuelve `True`, entonces +## son `True` hasta que su lado derecho devuelve `True`. +## Como los rangos, tu puedes excluir la iteración cuando se convierte en +## `True`/`False` usando `^` en cualquier lado. +## Comencemos con un ejemplo: +for <well met young hero we shall meet later> { + # por defecto, `ff`/`fff` hace coincidencia inteligente (`~~`) contra `$_`: + if 'met' ^ff 'meet' { # no entrará el bucle if por "met" + # (se explica más abajo). + .say + } + + if rand == 0 ff rand == 1 { # compara variables más que `$_` + say "Esto ... probablemente nunca se ejecutará ..."; + } +} +## Esto imprimirá "young hero we shall meet" (exluyendo "met"): +## el flip-flop comenzará devolviendo `True` cuando primero encuentra "met" +## (pero no returnará `False` por "met" dabido al `^` al frente de `ff`), +## hasta que ve "meet", lo cual es cuando comenzará devolviendo `False`. + +## La diferencia entre `ff` (al estilo de awk) y `fff` (al estilo de sed) +## es que `ff` probará su lado derecho cuando su lado izquierdo cambia +## a `True`, y puede returnar a `False` inmediamente (*excepto* que será +## `True` por la iteración con la cual coincidió). Por lo contrario, +## `fff` esperará por la próxima iteración para intentar su lado +## derecho, una vez que su lado izquierdo ha cambiado: +.say if 'B' ff 'B' for <A B C B A>; #=> B B + # porque el lado derecho se puso a prueba + # directamente (y returnó `True`). + # Las "B"s se imprimen dadó que coincidió + # en ese momento (returnó a `False` + # inmediatamente). +.say if 'B' fff 'B' for <A B C B A>; #=> B C B + # El lado derecho no se puso a prueba + # hasta que `$_` se convirtió en "C" + # (y por lo tanto no coincidió + # inmediamente). + +## Un flip-flop puede cambiar estado cuantas veces se necesite: +for <test start print it stop not printing start print again stop not anymore> { + .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # excluye a "start" y "stop", + #=> "print it print again" +} + +## También podrías usar una Whatever Star, lo cual es equivalente +## a `True` para el lado izquierdo o `False` para el lado derecho: +for (1, 3, 60, 3, 40, 60) { # Nota: los paréntesis son superfluos aquí + # (algunas veces se les llaman "paréntesis superticiosos") + .say if $_ > 50 ff *; # Una vez el flip-flop alcanza un número mayor que 50, + # no returnará jamás a `False` + #=> 60 3 40 60 +} + +## También puedes usar esta propiedad para crear un `If` +## que no pasará la primera vez: +for <a b c> { + .say if * ^ff *; # el flip-flop es `True` y nunca returna a `False`, + # pero el `^` lo hace *que no se ejecute* en la + # primera iteración + #=> b c +} + +## - `===` es la identidad de valor y usa `.WHICH` +## en los objetos para compararlos. +## - `=:=` es la identidad de contenedor y usa `VAR()` +## en los objetos para compararlos. + +``` +Si quieres ir más allá de lo que se muestra aquí, puedes: + + - Leer la [documentación de Perl 6](https://docs.perl6.org/). Esto es un recurso + grandioso acerca de Perl 6. Si estás buscando por algo en particular, usa la + barra de búsquedas. Esto te dará un menú de todas las páginas concernientes + a tu término de búsqueda (¡Es mucho mejor que usar Google para encontrar + documentos acerca de Perl 6!) + - Leer el [Perl 6 Advent Calendar](http://perl6advent.wordpress.com/). Este es + un gran recurso de fragmentos de código de Perl 6 y explicaciones. Si la documentación + no describe algo lo suficientemente bien, puedes encontrar información más detallada + aquí. Esta información puede ser un poquito más antigua pero hay muchos ejemplos y + explicaciones. Las publicaciones fueron suspendidas al final del 2015 cuando + el lenguaje fue declarado estable y Perl 6.c fue lanzado. + - Unirte a `#perl6` en `irc.freenode.net`. Las personas aquí son siempre serviciales. + - Chequear la [fuente de las funciones y clases de Perl 6 + ](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo está principalmente + escrito en Perl 6 (con mucho de NQP, "Not Quite Perl" ("No Perl Todavía"), un + subconjunto de Perl 6 que es más fácil de implementar y optimizar). + - Leer [documentos acerca del diseño del lenguaje](http://design.perl6.org). + Estos explican P6 desde la perspectiva de un implementador, lo cual es bastante + interesante. diff --git a/es-es/visualbasic-es.html.markdown b/es-es/visualbasic-es.html.markdown index c7f581c0..ca00626b 100644 --- a/es-es/visualbasic-es.html.markdown +++ b/es-es/visualbasic-es.html.markdown @@ -10,7 +10,7 @@ filename: learnvisualbasic-es.vb lang: es-es --- -```vb +``` Module Module1 Sub Main() diff --git a/fi-fi/markdown-fi.html.markdown b/fi-fi/markdown-fi.html.markdown index c5ee52b0..defc7100 100644 --- a/fi-fi/markdown-fi.html.markdown +++ b/fi-fi/markdown-fi.html.markdown @@ -10,7 +10,7 @@ lang: fi-fi John Gruber loi Markdownin vuona 2004. Sen tarkoitus on olla helposti luettava ja kirjoitettava syntaksi joka muuntuu helposti HTML:ksi (ja nyt myös moneksi muuksi formaatiksi). -```markdown +```md <!-- Jokainen HTML-tiedosto on pätevää Markdownia. Tämä tarkoittaa että voimme käyttää HTML-elementtejä Markdownissa, kuten kommentteja, ilman että markdown -jäsennin vaikuttaa niihin. Tästä johtuen et voi kuitenkaan käyttää markdownia diff --git a/fr-fr/dynamic-programming-fr.html.markdown b/fr-fr/dynamic-programming-fr.html.markdown index 24e8c95f..b3660ac9 100644 --- a/fr-fr/dynamic-programming-fr.html.markdown +++ b/fr-fr/dynamic-programming-fr.html.markdown @@ -8,7 +8,6 @@ translators: lang: fr-fr --- - # Programmation dynamique ## Introduction @@ -17,9 +16,9 @@ La programmation dynamique est une technique très efficace pour résoudre une c ## Moyens de résoudre ces problèmes -1.) *De haut en bas* : Commençons à résoudre le problème en le séparant en morceaux. Si nous voyons que le problème a déjà été résolu, alors nous retournons la réponse précédemment sauvegardée. Si le problème n'a pas été résolu, alors nous le résolvons et sauvegardons la réponse. C'est généralement facile et intuitif de réfléchir de cette façon. Cela s'appelle la Mémorisation. +1. *De haut en bas* : Commençons à résoudre le problème en le séparant en morceaux. Si nous voyons que le problème a déjà été résolu, alors nous retournons la réponse précédemment sauvegardée. Si le problème n'a pas été résolu, alors nous le résolvons et sauvegardons la réponse. C'est généralement facile et intuitif de réfléchir de cette façon. Cela s'appelle la Mémorisation. -2.) *De bas en haut* : Il faut analyser le problème et trouver les sous-problèmes, et l'ordre dans lequel il faut les résoudre. Ensuite, nous devons résoudre les sous-problèmes et monter jusqu'au problème que nous voulons résoudre. De cette façon, nous sommes assurés que les sous-problèmes sont résolus avant de résoudre le vrai problème. Cela s'appelle la Programmation Dynamique. +2. *De bas en haut* : Il faut analyser le problème et trouver les sous-problèmes, et l'ordre dans lequel il faut les résoudre. Ensuite, nous devons résoudre les sous-problèmes et monter jusqu'au problème que nous voulons résoudre. De cette façon, nous sommes assurés que les sous-problèmes sont résolus avant de résoudre le vrai problème. Cela s'appelle la Programmation Dynamique. ## Exemple de Programmation Dynamique @@ -27,7 +26,7 @@ Le problème de la plus grande sous-chaîne croissante est de trouver la plus gr Premièrement, nous avons à trouver la valeur de la plus grande sous-chaîne (LSi) à chaque index `i`, avec le dernier élément de la sous-chaîne étant ai. Alors, la plus grande sous-chaîne sera le plus gros LSi. Pour commencer, LSi est égal à 1, car ai est le seul élément de la chaîne (le dernier). Ensuite, pour chaque `j` tel que `j<i` et `aj<ai`, nous trouvons le plus grand LSj et ajoutons le à LSi. L'algorithme fonctionne en temps *O(n2)*. Pseudo-code pour trouver la longueur de la plus grande sous-chaîne croissante : -La complexité de cet algorithme peut être réduite en utilisant une meilleure structure de données qu'un tableau. Par exemple, si nous sauvegardions le tableau d'origine, ou une variable comme plus_grande_chaîne_jusqu'à_maintenant et son index, nous pourrions sauver beaucoup de temps. +La complexité de cet algorithme peut être réduite en utilisant une meilleure structure de données qu'un tableau. Par exemple, si nous sauvegardions le tableau d'origine, ou une variable comme `plus_grande_chaîne_jusqu'à_maintenant` et son index, nous pourrions sauver beaucoup de temps. Le même concept peut être appliqué pour trouver le chemin le plus long dans un graphe acyclique orienté. @@ -43,12 +42,9 @@ Le même concept peut être appliqué pour trouver le chemin le plus long dans u ### Problèmes classiques de programmation dynamique -L'algorithme de Floyd Warshall(EN)) - Tutorial and C Program source code:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code - -Problème du sac à dos(EN) - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem - - -Plus longue sous-chaîne commune(EN) - Tutorial and C Program source code : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence +- L'algorithme de Floyd Warshall(EN)) - Tutorial and C Program source code:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code +- Problème du sac à dos(EN) - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem +- Plus longue sous-chaîne commune(EN) - Tutorial and C Program source code : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence ## Online Resources diff --git a/fr-fr/java-fr.html.markdown b/fr-fr/java-fr.html.markdown index d0f91611..d6c68343 100644 --- a/fr-fr/java-fr.html.markdown +++ b/fr-fr/java-fr.html.markdown @@ -11,7 +11,7 @@ contributors: - ["Michael Dähnert", "https://github.com/JaXt0r"] - ["Rob Rose", "https://github.com/RobRoseKnows"] - ["Sean Nam", "https://github.com/seannam"] -filename: JavaFr.java +filename: java-fr.java translators: - ['Mathieu Gemard', 'https://github.com/mgemard'] lang: fr-fr diff --git a/fr-fr/jquery-fr.html.markdown b/fr-fr/jquery-fr.html.markdown index 1842e02b..1a5baf71 100644 --- a/fr-fr/jquery-fr.html.markdown +++ b/fr-fr/jquery-fr.html.markdown @@ -13,6 +13,7 @@ jQuery est une bibliothèque JavaScript dont le but est de permettre de "faire p C'est pourquoi aujourd'hui, jQuery est utilisée par de nombreuses grandes entreprises et par des développeurs du monde entier. Étant donné que jQuery est une bibliothèque JavaScript, vous devriez d'abord [apprendre le JavaScript](https://learnxinyminutes.com/docs/fr-fr/javascript-fr/) + ```js @@ -138,5 +139,5 @@ $('p').each(function() { }); -`` +``` diff --git a/fr-fr/lambda-calculus-fr.html.markdown b/fr-fr/lambda-calculus-fr.html.markdown new file mode 100644 index 00000000..c91f21d6 --- /dev/null +++ b/fr-fr/lambda-calculus-fr.html.markdown @@ -0,0 +1,106 @@ +--- +category: Algorithms & Data Structures +name: Lambda Calculus +contributors: + - ["Max Sun", "http://github.com/maxsun"] +translators: + - ["Yvan Sraka", "https://github.com/yvan-sraka"] +lang: fr-fr +--- + +# Lambda-calcul + +Le Lambda-calcul (λ-calcul), créé à l'origine par [Alonzo Church](https://en.wikipedia.org/wiki/Alonzo_Church), est le plus petit langage de programmation au monde. En dépit de ne pas avoir de nombres, de chaînes, de booléens, ou de tout type de données sans fonction, le lambda calcul peut être utilisé pour représenter n'importe quelle machine de Turing! + +Le Lambda-calcul est composé de 3 éléments : **variables**, **fonctions** et **applications**. + + +| Nom | Syntaxe | Exemple | Explication | +|-------------|------------------------------------|-----------|---------------------------------------------------| +| Variable | `<nom>` | `x` | une variable nommée "x" | +| Fonction | `λ<paramètres>.<corps>` | `λx.x` | une fonction avec le paramètre "x" et le corps "x"| +| Application | `<fonction><variable ou function>` | `(λx.x)a` | appel de la fonction "λx.x" avec l'argument "a" | + +La fonction la plus fondamentale est la fonction identité: `λx.x` qui est équivalente à `f(x) = x`. Le premier "x" est l'argument de la fonction, et le second est le corps de la fonction. + +## Variables libres et liées : + +- Dans la fonction `λx.x`, "x" s'appelle une variable liée car elle est à la fois dans le corps de la fonction et l'un des paramètres. +- Dans `λx.y`, "y" est appelé une variable libre car elle n'a pas été déclarée plus tôt. + +## Évaluation : + +L'évaluation est réalisée par [β-Réduction](https://en.wikipedia.org/wiki/Lambda_calculus#Beta_reduction), qui est essentiellement une substitution lexicale. + +Lors de l'évaluation de l'expression `(λx.x)a`, nous remplaçons toutes les occurrences de "x" dans le corps de la fonction par "a". + +- `(λx.x)a` vaut après évaluation: `a` +- `(λx.y)a` vaut après évaluation: `y` + +Vous pouvez même créer des fonctions d'ordre supérieur: + +- `(λx.(λy.x))a` vaut après évaluation: `λy.a` + +Bien que le lambda-calcul ne prenne traditionnellement en charge que les fonctions à un seul paramètre, nous pouvons créer des fonctions multi-paramètres en utilisant une technique appelée currying. + +- `(λx.λy.λz.xyz)` est équivalent à `f(x, y, z) = x(y(z))` + +Parfois, `λxy.<corps>` est utilisé de manière interchangeable avec: `λx.λy.<corps>` + +---- + +Il est important de reconnaître que le lambda-calcul traditionnel n'a pas de nombres, de caractères ou tout autre type de données sans fonction! + +## Logique booléenne : + +Il n'y a pas de "Vrai" ou de "Faux" dans le calcul lambda. Il n'y a même pas 1 ou 0. + +Au lieu: + +`T` est représenté par: `λx.λy.x` + +`F` est représenté par: `λx.λy.y` + +Premièrement, nous pouvons définir une fonction "if" `λbtf` qui renvoie `t` si `b` est vrai et `f` si `b` est faux + +`IF` est équivalent à: `λb.λt.λf.b t f` + +En utilisant `IF`, nous pouvons définir les opérateurs logiques de base booléens: + +`a AND b` est équivalent à: `λab.IF a b F` + +`a OR b` est équivalent à: `λab.IF a T b` + +`a NOT b` est équivalent à: `λa.IF a F T` + +*Note: `IF a b c` est equivalent à : `IF(a(b(c)))`* + +## Nombres : + +Bien qu'il n'y ait pas de nombres dans le lambda-calcul, nous pouvons encoder des nombres en utilisant les [nombres de Church](https://en.wikipedia.org/wiki/Church_encoding). + +Pour tout nombre n: <code>n = λf.f<sup>n</sup></code> donc: + +`0 = λf.λx.x` + +`1 = λf.λx.f x` + +`2 = λf.λx.f(f x)` + +`3 = λf.λx.f(f(f x))` + +Pour incrémenter un nombre de Church, nous utilisons la fonction successeur `S(n) = n + 1` qui est: + +`S = λn.λf.λx.f((n f) x)` + +En utilisant `S`, nous pouvons définir la fonction `ADD`: + +`ADD = λab.(a S)n` + +**Défi:** essayez de définir votre propre fonction de multiplication! + +## Pour aller plus loin : + +1. [A Tutorial Introduction to the Lambda Calculus](http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf) +2. [Cornell CS 312 Recitation 26: The Lambda Calculus](http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html) +3. [Wikipedia - Lambda Calculus](https://en.wikipedia.org/wiki/Lambda_calculus) diff --git a/fr-fr/pyqt-fr.html.markdown b/fr-fr/pyqt-fr.html.markdown new file mode 100644 index 00000000..6da9a380 --- /dev/null +++ b/fr-fr/pyqt-fr.html.markdown @@ -0,0 +1,85 @@ +--- +category: tool +tool: PyQT +filename: learnpyqt-fr.py +contributors: + - ["Nathan Hughes", "https://github.com/sirsharpest"] +translators: + - ["DevHugo", "http://twitter.com/devhugo"] +lang: fr-fr +--- + +**Qt** est un framework très connu pour le développement de logiciel cross-platform qui peuvent être lancé sur différents systèmes avec de petit ou aucun changement dans le code, tout en ayant la puissance et la vitesse des applications natives. Bien que **Qt** ait été écrit à l'origine en *C++*. + + +Ceci est une adaptation de l'intro C++ à QT par [Aleksey Kholovchuk](https://github.com/vortexxx192 +), certains exemples du code doivent avoir la même fonctionnalité, +cette version ayant juste été faite en utilisant pyqt! + +```python +import sys +from PyQt4 import QtGui + +def window(): + # Création de l'objet application + app = QtGui.QApplication(sys.argv) + # Création d'un widget où notre label sera placé + w = QtGui.QWidget() + # Ajout d'un label au widget + b = QtGui.QLabel(w) + # Assignation de texte au label + b.setText("Hello World!") + # Assignation des tailles et des informations de placement + w.setGeometry(100, 100, 200, 50) + b.move(50, 20) + # Assignation d'un nom à notre fenêtre + w.setWindowTitle("PyQt") + # Affichage de la fenêtre + w.show() + # Exécution de l'application + sys.exit(app.exec_()) + +if __name__ == '__main__': + window() + +``` + +Pour obtenir certaines des fonctionnalités les plus avancées de **pyqt** nous devons commencer par chercher à construire des éléments supplémentaires. +Ici nous voyons comment introduire une boîte de dialogue popup, utile pour demander une confirmation à un utilisateur ou fournir des informations. + +```Python +import sys +from PyQt4.QtGui import * +from PyQt4.QtCore import * + + +def window(): + app = QApplication(sys.argv) + w = QWidget() + # Creation d'un bouton attaché au widget w + b = QPushButton(w) + b.setText("Press me") + b.move(50, 50) + # Dire à b d'appeler cette fonction quand il est cliqué + # remarquez l'absence de "()" sur l'appel de la fonction + b.clicked.connect(showdialog) + w.setWindowTitle("PyQt Dialog") + w.show() + sys.exit(app.exec_()) + +# Cette fonction devrait créer une fenêtre de dialogue avec un bouton +# qui attend d'être cliqué puis quitte le programme +def showdialog(): + d = QDialog() + b1 = QPushButton("ok", d) + b1.move(50, 50) + d.setWindowTitle("Dialog") + # Cette modalité dit au popup de bloquer le parent pendant qu'il est actif + d.setWindowModality(Qt.ApplicationModal) + # En cliquant je voudrais que tout le processus se termine + b1.clicked.connect(sys.exit) + d.exec_() + +if __name__ == '__main__': + window() +``` diff --git a/fsharp.html.markdown b/fsharp.html.markdown index bbf477ba..59461eed 100644 --- a/fsharp.html.markdown +++ b/fsharp.html.markdown @@ -34,7 +34,7 @@ let myFloat = 3.14 let myString = "hello" // note that no types needed // ------ Lists ------ -let twoToFive = [2; 3; 4; 5] // Square brackets create a list with +let twoToFive = [2; 3; 4; 5] // Square brackets create a list with // semicolon delimiters. let oneToFive = 1 :: twoToFive // :: creates list with new 1st element // The result is [1; 2; 3; 4; 5] @@ -53,7 +53,8 @@ add 2 3 // Now run the function. // to define a multiline function, just use indents. No semicolons needed. let evens list = - let isEven x = x % 2 = 0 // Define "isEven" as a sub function + let isEven x = x % 2 = 0 // Define "isEven" as a sub function. Note + // that equality operator is single char "=". List.filter isEven list // List.filter is a library function // with two parameters: a boolean function // and a list to work on @@ -306,7 +307,7 @@ module DataTypeExamples = // ------------------------------------ // Union types (aka variants) have a set of choices - // Only case can be valid at a time. + // Only one case can be valid at a time. // ------------------------------------ // Use "type" with bar/pipe to define a union type diff --git a/go.html.markdown b/go.html.markdown index 47d9c234..df677894 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -15,15 +15,15 @@ contributors: --- Go was created out of the need to get work done. It's not the latest trend -in computer science, but it is the newest fastest way to solve real-world +in programming language theory, but it is a way to solve real-world problems. -It has familiar concepts of imperative languages with static typing. +It draws concepts from imperative languages with static typing. It's fast to compile and fast to execute, it adds easy-to-understand -concurrency to leverage today's multi-core CPUs, and has features to -help with large-scale programming. +concurrency because multi-core CPUs are now common, and it's used successfully +in large codebases (~100 million loc at Google, Inc.). -Go comes with a great standard library and an enthusiastic community. +Go comes with a good standard library and a sizeable community. ```go // Single line comment @@ -48,7 +48,7 @@ import ( // executable program. Love it or hate it, Go uses brace brackets. func main() { // Println outputs a line to stdout. - // Qualify it with the package name, fmt. + // It comes from the package fmt. fmt.Println("Hello world!") // Call another function within this package. diff --git a/haskell.html.markdown b/haskell.html.markdown index 266cf11b..90d47c27 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -124,6 +124,9 @@ last [1..5] -- 5 fst ("haskell", 1) -- "haskell" snd ("haskell", 1) -- 1 +-- pair element accessing does not work on n-tuples (i.e. triple, quadruple, etc) +snd ("snd", "can't touch this", "da na na na") -- error! see function below + ---------------------------------------------------- -- 3. Functions ---------------------------------------------------- @@ -159,8 +162,8 @@ fib 1 = 1 fib 2 = 2 fib x = fib (x - 1) + fib (x - 2) --- Pattern matching on tuples: -foo (x, y) = (x + 1, y + 2) +-- Pattern matching on tuples +sndOfTriple (_, y, _) = y -- use a wild card (_) to bypass naming unused value -- Pattern matching on lists. Here `x` is the first element -- in the list, and `xs` is the rest of the list. We can write @@ -203,11 +206,11 @@ foo = (4*) . (10+) foo 5 -- 60 -- fixing precedence --- Haskell has an operator called `$`. This operator applies a function --- to a given parameter. In contrast to standard function application, which --- has highest possible priority of 10 and is left-associative, the `$` operator +-- Haskell has an operator called `$`. This operator applies a function +-- to a given parameter. In contrast to standard function application, which +-- has highest possible priority of 10 and is left-associative, the `$` operator -- has priority of 0 and is right-associative. Such a low priority means that --- the expression on its right is applied as the parameter to the function on its left. +-- the expression on its right is applied as a parameter to the function on its left. -- before even (fib 7) -- false @@ -223,7 +226,7 @@ even . fib $ 7 -- false -- 5. Type signatures ---------------------------------------------------- --- Haskell has a very strong type system, and every valid expression has a type. +-- Haskell has a very strong type system, and every valid expression has a type. -- Some basic types: 5 :: Integer diff --git a/haxe.html.markdown b/haxe.html.markdown index df2a1e78..afb9d1a3 100644 --- a/haxe.html.markdown +++ b/haxe.html.markdown @@ -770,19 +770,18 @@ class UsingExample { ``` We're still only scratching the surface here of what Haxe can do. For a formal -overview of all Haxe features, checkout the [online -manual](http://haxe.org/manual), the [online API](http://api.haxe.org/), and -"haxelib", the [haxe library repo] (http://lib.haxe.org/). +overview of all Haxe features, see the [manual](https://haxe.org/manual) and +the [API docs](https://api.haxe.org/). For a comprehensive directory of available +third-party Haxe libraries, see [Haxelib](https://lib.haxe.org/). For more advanced topics, consider checking out: -* [Abstract types](http://haxe.org/manual/abstracts) -* [Macros](http://haxe.org/manual/macros), and [Compiler Macros](http://haxe.org/manual/macros_compiler) -* [Tips and Tricks](http://haxe.org/manual/tips_and_tricks) - - -Finally, please join us on [the mailing list](https://groups.google.com/forum/#!forum/haxelang), on IRC [#haxe on -freenode](http://webchat.freenode.net/), or on -[Google+](https://plus.google.com/communities/103302587329918132234). +* [Abstract types](https://haxe.org/manual/types-abstract.html) +* [Macros](https://haxe.org/manual/macro.html) +* [Compiler Features](https://haxe.org/manual/cr-features.html) +Finally, please join us on [the Haxe forum](https://community.haxe.org/), +on IRC [#haxe on +freenode](http://webchat.freenode.net/), or on the +[Haxe Gitter chat](https://gitter.im/HaxeFoundation/haxe). diff --git a/id-id/markdown.html.markdown b/id-id/markdown.html.markdown index 06ad1092..1ff1963b 100644 --- a/id-id/markdown.html.markdown +++ b/id-id/markdown.html.markdown @@ -13,7 +13,7 @@ Markdown dibuat oleh John Gruber pada tahun 2004. Tujuannya untuk menjadi syntax Beri masukan sebanyak-banyaknya! / Jangan sungkan untuk melakukan fork dan pull request! -```markdown +```md <!-- Markdown adalah superset dari HTML, jadi setiap berkas HTML adalah markdown yang valid, ini berarti kita dapat menggunakan elemen HTML dalam markdown, seperti elemen komentar, dan ia tidak akan terpengaruh parser markdown. Namun, jika Anda membuat diff --git a/it-it/markdown.html.markdown b/it-it/markdown.html.markdown index 44801747..b0a123f1 100644 --- a/it-it/markdown.html.markdown +++ b/it-it/markdown.html.markdown @@ -28,7 +28,7 @@ Markdown varia nelle sue implementazioni da un parser all'altro. Questa guida ce ## Elementi HTML Markdown è un superset di HTML, quindi ogni file HTML è a sua volta un file Markdown valido. -```markdown +```md <!-- Questo significa che possiamo usare elementi di HTML in Markdown, come per esempio i commenti, e questi non saranno modificati dal parser di Markdown. State attenti però, se inserite un elemento HTML nel vostro file Markdown, non potrete usare la sua sintassi @@ -39,7 +39,7 @@ all'interno del contenuto dell'elemento. --> Potete creare gli elementi HTML da `<h1>` a `<h6>` facilmente, basta che inseriate un egual numero di caratteri cancelletto (#) prima del testo che volete all'interno dell'elemento -```markdown +```md # Questo è un <h1> ## Questo è un <h2> ### Questo è un <h3> @@ -49,7 +49,7 @@ Potete creare gli elementi HTML da `<h1>` a `<h6>` facilmente, basta che inseria ``` Markdown inoltre fornisce due alternative per indicare gli elementi h1 e h2 -```markdown +```md Questo è un h1 ============== @@ -60,7 +60,7 @@ Questo è un h2 ## Stili di testo semplici Il testo può essere stilizzato in corsivo o grassetto usando markdown -```markdown +```md *Questo testo è in corsivo.* _Come pure questo._ @@ -74,12 +74,12 @@ __Come pure questo.__ In Github Flavored Markdown, che è utilizzato per renderizzare i file markdown su Github, è presente anche lo stile barrato: -```markdown +```md ~~Questo testo è barrato.~~ ``` ## Paragrafi -```markdown +```md I paragrafi sono una o più linee di testo adiacenti separate da una o più righe vuote. Questo è un paragrafo. Sto scrivendo in un paragrafo, non è divertente? @@ -93,7 +93,7 @@ Qui siamo nel paragrafo 3! Se volete inserire l'elemento HTML `<br />`, potete terminare la linea con due o più spazi e poi iniziare un nuovo paragrafo. -```markdown +```md Questa frase finisce con due spazi (evidenziatemi per vederli). C'è un <br /> sopra di me! @@ -101,7 +101,7 @@ C'è un <br /> sopra di me! Le citazioni sono semplici da inserire, basta usare il carattere >. -```markdown +```md > Questa è una citazione. Potete > mandare a capo manualmente le linee e inserire un `>` prima di ognuna, oppure potete usare una sola linea e lasciare che vada a capo automaticamente. > Non c'è alcuna differenza, basta che iniziate ogni riga con `>`. @@ -115,7 +115,7 @@ Le citazioni sono semplici da inserire, basta usare il carattere >. ## Liste Le liste non ordinate possono essere inserite usando gli asterischi, il simbolo più o dei trattini -```markdown +```md * Oggetto * Oggetto * Altro oggetto @@ -135,7 +135,7 @@ oppure Le liste ordinate invece, sono inserite con un numero seguito da un punto. -```markdown +```md 1. Primo oggetto 2. Secondo oggetto 3. Terzo oggetto @@ -143,7 +143,7 @@ Le liste ordinate invece, sono inserite con un numero seguito da un punto. Non dovete nemmeno mettere i numeri nell'ordine giusto, markdown li visualizzerà comunque nell'ordine corretto, anche se potrebbe non essere una buona idea. -```markdown +```md 1. Primo oggetto 1. Secondo oggetto 1. Terzo oggetto @@ -152,7 +152,7 @@ Non dovete nemmeno mettere i numeri nell'ordine giusto, markdown li visualizzer Potete inserire anche sotto liste -```markdown +```md 1. Primo oggetto 2. Secondo oggetto 3. Terzo oggetto @@ -163,7 +163,7 @@ Potete inserire anche sotto liste Sono presenti anche le task list. In questo modo è possibile creare checkbox in HTML. -```markdown +```md I box senza la 'x' sono checkbox HTML ancora da completare. - [ ] Primo task da completare. - [ ] Secondo task che deve essere completato. @@ -174,14 +174,14 @@ Il box subito sotto è una checkbox HTML spuntata. Potete inserire un estratto di codice (che utilizza l'elemento `<code>`) indentando una linea con quattro spazi oppure con un carattere tab. -```markdown +```md Questa è una linea di codice Come questa ``` Potete inoltre inserire un altro tab (o altri quattro spazi) per indentare il vostro codice -```markdown +```md my_array.each do |item| puts item end @@ -189,7 +189,7 @@ Potete inoltre inserire un altro tab (o altri quattro spazi) per indentare il vo Codice inline può essere inserito usando il carattere backtick ` -```markdown +```md Giovanni non sapeva neppure a cosa servisse la funzione `go_to()`! ``` @@ -205,7 +205,7 @@ Se usate questa sintassi, il testo non richiederà di essere indentato, inoltre ## Linea orizzontale Le linee orizzontali (`<hr/>`) sono inserite facilmente usanto tre o più asterischi o trattini, con o senza spazi. -```markdown +```md *** --- - - - @@ -215,24 +215,24 @@ Le linee orizzontali (`<hr/>`) sono inserite facilmente usanto tre o più asteri ## Links Una delle funzionalità migliori di markdown è la facilità con cui si possono inserire i link. Mettete il testo da visualizzare fra parentesi quadre [] seguite dall'url messo fra parentesi tonde () -```markdown +```md [Cliccami!](http://test.com/) ``` Potete inoltre aggiungere al link un titolo mettendolo fra doppi apici dopo il link -```markdown +```md [Cliccami!](http://test.com/ "Link a Test.com") ``` La sintassi funziona anche con i path relativi. -```markdown +```md [Vai a musica](/music/). ``` Markdown supporta inoltre anche la possibilità di aggiungere i link facendo riferimento ad altri punti del testo. -```markdown +```md [Apri questo link][link1] per più informazioni! [Guarda anche questo link][foobar] se ti va. @@ -242,7 +242,7 @@ Markdown supporta inoltre anche la possibilità di aggiungere i link facendo rif l titolo può anche essere inserito in apici singoli o in parentesi, oppure omesso interamente. Il riferimento può essere inserito in un punto qualsiasi del vostro documento e l'identificativo del riferimento può essere lungo a piacere a patto che sia univoco. Esiste anche un "identificativo implicito" che vi permette di usare il testo del link come id. -```markdown +```md [Questo][] è un link. [Questo]: http://thisisalink.com/ @@ -252,13 +252,13 @@ Ma non è comunemente usato. ## Immagini Le immagini sono inserite come i link ma con un punto esclamativo inserito prima delle parentesi quadre! -```markdown +```md ![Qeusto è il testo alternativo per l'immagine](http://imgur.com/myimage.jpg "Il titolo opzionale") ``` E la modalità a riferimento funziona esattamente come ci si aspetta -```markdown +```md ![Questo è il testo alternativo.][myimage] [myimage]: relative/urls/cool/image.jpg "Se vi serve un titolo, lo mettete qui" @@ -266,25 +266,25 @@ E la modalità a riferimento funziona esattamente come ci si aspetta ## Miscellanea ### Auto link -```markdown +```md <http://testwebsite.com/> è equivalente ad [http://testwebsite.com/](http://testwebsite.com/) ``` ### Auto link per le email -```markdown +```md <foo@bar.com> ``` ### Caratteri di escaping -```markdown +```md Voglio inserire *questo testo circondato da asterischi* ma non voglio che venga renderizzato in corsivo, quindi lo inserirò così: \*questo testo è circondato da asterischi\*. ``` ### Combinazioni di tasti In Github Flavored Markdown, potete utilizzare il tag `<kbd>` per raffigurare i tasti della tastiera. -```markdown +```md Il tuo computer è crashato? Prova a premere <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Canc</kbd> ``` @@ -292,7 +292,7 @@ Il tuo computer è crashato? Prova a premere ### Tabelle Le tabelle sono disponibili solo in Github Flavored Markdown e sono leggeremente complesse, ma se proprio volete inserirle fate come segue: -```markdown +```md | Col1 | Col2 | Col3 | | :------------------- | :------: | -----------------: | | Allineato a sinistra | Centrato | Allineato a destra | @@ -300,7 +300,7 @@ Le tabelle sono disponibili solo in Github Flavored Markdown e sono leggeremente ``` oppure, per lo stesso risultato -```markdown +```md Col 1 | Col2 | Col3 :-- | :-: | --: È una cosa orrenda | fatela | finire in fretta diff --git a/it-it/matlab-it.html.markdown b/it-it/matlab-it.html.markdown index 8d6d4385..38be8848 100644 --- a/it-it/matlab-it.html.markdown +++ b/it-it/matlab-it.html.markdown @@ -199,8 +199,7 @@ size(A) % ans = 3 3 A(1, :) =[] % Rimuove la prima riga della matrice A(:, 1) =[] % Rimuove la prima colonna della matrice -transpose(A) % Traspone la matrice, equivale a: -A one +transpose(A) % Traspone la matrice, equivale a: A.' ctranspose(A) % Trasposizione hermitiana della matrice % (ovvero il complesso coniugato di ogni elemento della matrice trasposta) diff --git a/it-it/python3-it.html.markdown b/it-it/python3-it.html.markdown new file mode 100644 index 00000000..04f78cff --- /dev/null +++ b/it-it/python3-it.html.markdown @@ -0,0 +1,1016 @@ +--- +language: python3 +filename: learnpython3-it.py +contributors: + - ["Louie Dinh", "http://pythonpracticeprojects.com"] + - ["Steven Basart", "http://github.com/xksteven"] + - ["Andre Polykanine", "https://github.com/Oire"] + - ["Zachary Ferguson", "http://github.com/zfergus2"] + - ["evuez", "http://github.com/evuez"] + - ["Rommel Martinez", "https://ebzzry.io"] +translators: + - ["Draio", "http://github.com/Draio/"] + - ["Ale46", "http://github.com/Ale46/"] + - ["Tommaso Pifferi", "http://github.com/neslinesli93/"] +lang: it-it +--- + +Python è stato creato da Guido Van Rossum agli inizi degli anni 90. Oggi è uno dei più popolari linguaggi esistenti. Mi sono innamorato di Python per la sua chiarezza sintattica. E' sostanzialmente pseudocodice eseguibile. + +Feedback sono altamente apprezzati! Potete contattarmi su [@louiedinh](http://twitter.com/louiedinh) oppure [at] [google's email service] + +Nota: Questo articolo è riferito a Python 3 in modo specifico. Se volete avete la necessità di utilizzare Python 2.7 potete consultarla [qui](https://learnxinyminutes.com/docs/it-it/python-it/) + +```python + +# I commenti su una sola linea iniziano con un cancelletto + + +""" Più stringhe possono essere scritte + usando tre ", e sono spesso usate + come documentazione +""" + +#################################################### +## 1. Tipi di dati primitivi ed Operatori +#################################################### + +# Ci sono i numeri +3 # => 3 + +# La matematica è quello che vi aspettereste +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7.0 + +# Risultato della divisione intera troncata sia in positivo che in negativo +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 # works on floats too +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 + +# Il risultato di una divisione è sempre un numero decimale (float) +10.0 / 3 # => 3.3333333333333335 + +# Operazione Modulo +7 % 3 # => 1 + +# Elevamento a potenza (x alla y-esima potenza) +2**3 # => 8 + +# Forzare le precedenze con le parentesi +(1 + 3) * 2 # => 8 + +# I valori booleani sono primitive del linguaggio (nota la maiuscola) +True +False + +# nega con not +not True # => False +not False # => True + +# Operatori Booleani +# Nota "and" e "or" sono case-sensitive +True and False # => False +False or True # => True + +# Note sull'uso di operatori Bool con interi +# False è 0 e True è 1 +# Non confonderti tra bool(ints) e le operazioni bitwise and/or (&,|) +0 and 2 # => 0 +-5 or 0 # => -5 +0 == False # => True +2 == True # => False +1 == True # => True +-5 != False != True #=> True + +# Uguaglianza è == +1 == 1 # => True +2 == 1 # => False + +# Disuguaglianza è != +1 != 1 # => False +2 != 1 # => True + +# Altri confronti +1 < 10 # => True +1 > 10 # => False +2 <= 2 # => True +2 >= 2 # => True + +# I confronti possono essere concatenati! +1 < 2 < 3 # => True +2 < 3 < 2 # => False + +# ('is' vs. '==') +# 'is' controlla se due variabili si riferiscono allo stesso oggetto +# '==' controlla se gli oggetti puntati hanno lo stesso valore. +a = [1, 2, 3, 4] # a punta ad una nuova lista [1, 2, 3, 4] +b = a # b punta a ciò a cui punta a +b is a # => True, a e b puntano allo stesso oggeto +b == a # => True, gli oggetti di a e b sono uguali +b = [1, 2, 3, 4] # b punta ad una nuova lista [1, 2, 3, 4] +b is a # => False, a e b non puntano allo stesso oggetto +b == a # => True, gli oggetti di a e b sono uguali + +# Le stringhe sono create con " o ' +"Questa è una stringa." +'Anche questa è una stringa.' + +# Anche le stringhe possono essere sommate! Ma cerca di non farlo. +"Hello " + "world!" # => "Hello world!" +# Le stringhe (ma non le variabili contenenti stringhe) possono essere +# sommate anche senza '+' +"Hello " "world!" # => "Hello world!" + +# Una stringa può essere considerata come una lista di caratteri +"Questa è una stringa"[0] # => 'Q' + +# Puoi conoscere la lunghezza di una stringa +len("Questa è una stringa") # => 20 + +# .format può essere usato per formattare le stringhe, in questo modo: +"{} possono essere {}".format("Le stringhe", "interpolate") # => "Le stringhe possono essere interpolate" + +# Puoi ripetere gli argomenti di formattazione per risparmiare un po' di codice +"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick") +# => "Jack be nimble, Jack be quick, Jack jump over the candle stick" + +# Puoi usare dei nomi se non vuoi contare gli argomenti +"{nome} vuole mangiare {cibo}".format(nome="Bob", cibo="le lasagne") # => "Bob vuole mangiare le lasagne" + +# Se il tuo codice Python 3 necessita di eseguire codice Python 2.x puoi ancora +# utilizzare il vecchio stile di formattazione: +"%s possono essere %s nel %s modo" % ("Le stringhe", "interpolate", "vecchio") # => "Le stringhe possono essere interpolate nel vecchio modo" + +# None è un oggetto +None # => None + +# Non usare il simbolo di uguaglianza "==" per comparare oggetti a None +# Usa "is" invece +"etc" is None # => False +None is None # => True + +# None, 0, e stringhe/liste/dizionari/tuple vuoti vengono considerati +# falsi (False). Tutti gli altri valori sono considerati veri (True). +bool(0) # => False +bool("") # => False +bool([]) # => False +bool({}) # => False +bool(()) # => False + +#################################################### +## 2. Variabili e Collections +#################################################### + +# Python ha una funzione per scrivere (sul tuo schermo) +print("Sono Python. Piacere di conoscerti!") # => Sono Python. Piacere di conoscerti! + +# Di default la funzione print() scrive e va a capo aggiungendo un carattere +# newline alla fine della stringa. È possibile utilizzare l'argomento opzionale +# end per cambiare quest'ultimo carattere aggiunto. +print("Hello, World", end="!") # => Hello, World! + +# Un modo semplice per ricevere dati in input dalla riga di comando +variabile_stringa_input = input("Inserisci del testo: ") # Restituisce i dati letti come stringa +# Nota: Nelle precedenti vesioni di Python, il metodo input() +# era chiamato raw_input() + +# Non c'è bisogno di dichiarare una variabile per assegnarle un valore +# Come convenzione, per i nomi delle variabili, si utilizzano i caratteri +# minuscoli separati, se necessario, da underscore +some_var = 5 +some_var # => 5 + +# Accedendo ad una variabile non precedentemente assegnata genera un'eccezione. +# Dai un'occhiata al Control Flow per imparare di più su come gestire +# le eccezioni. +some_unknown_var # Genera un errore di nome + +# if può essere usato come un'espressione +# È l'equivalente dell'operatore ternario in C +"yahoo!" if 3 > 2 else 2 # => "yahoo!" + +# Le liste immagazzinano sequenze +li = [] +# Puoi partire con una lista pre-riempita +other_li = [4, 5, 6] + +# Aggiungere alla fine di una lista con append +li.append(1) # li ora è [1] +li.append(2) # li ora è [1, 2] +li.append(4) # li ora è [1, 2, 4] +li.append(3) # li ora è [1, 2, 4, 3] +# Rimuovi dalla fine della lista con pop +li.pop() # => 3 e li ora è [1, 2, 4] +# Rimettiamolo a posto +li.append(3) # li ora è [1, 2, 4, 3] di nuovo. + +# Accedi ad una lista come faresti con un array +li[0] # => 1 +# Guarda l'ultimo elemento +li[-1] # => 3 + +# Guardare al di fuori dei limiti genera un IndexError +li[4] # Genera IndexError + +# Puoi guardare gli intervalli con la sintassi slice (a fetta). +# (E' un intervallo chiuso/aperto per voi tipi matematici.) +li[1:3] # => [2, 4] +# Ometti l'inizio +li[2:] # => [4, 3] +# Ometti la fine +li[:3] # => [1, 2, 4] +# Seleziona ogni seconda voce +li[::2] # =>[1, 4] +# Copia al contrario della lista +li[::-1] # => [3, 4, 2, 1] +# Usa combinazioni per fare slices avanzate +# li[inizio:fine:passo] + +# Crea una copia (one layer deep copy) usando la sintassi slices +li2 = li[:] # => li2 = [1, 2, 4, 3] ma (li2 is li) risulterà falso. + +# Rimuovi arbitrariamente elementi da una lista con "del" +del li[2] # li è ora [1, 2, 3] + +# Rimuove la prima occorrenza di un elemento +li.remove(2) # Ora li è [1, 3, 4, 5, 6] +li.remove(2) # Emette un ValueError, poichè 2 non è contenuto nella lista + +# Inserisce un elemento all'indice specificato +li.insert(1, 2) # li è di nuovo [1, 2, 3, 4, 5, 6] + + Ritorna l'indice della prima occorrenza dell'elemento fornito +li.index(2) # => 1 +li.index(7) # Emette un ValueError, poichè 7 non è contenuto nella lista + +# Puoi sommare le liste +# Nota: i valori per li e per other_li non vengono modificati. +li + other_li # => [1, 2, 3, 4, 5, 6] + +# Concatena le liste con "extend()" +li.extend(other_li) # Adesso li è [1, 2, 3, 4, 5, 6] + +# Controlla l'esistenza di un valore in una lista con "in" +1 in li # => True + +# Esamina la lunghezza con "len()" +len(li) # => 6 + + +# Le tuple sono come le liste ma immutabili. +tup = (1, 2, 3) +tup[0] # => 1 +tup[0] = 3 # Genera un TypeError + +# Note that a tuple of length one has to have a comma after the last element but +# tuples of other lengths, even zero, do not. +type((1)) # => <class 'int'> +type((1,)) # => <class 'tuple'> +type(()) # => <class 'tuple'> + +# Puoi fare tutte queste cose da lista anche sulle tuple +len(tup) # => 3 +tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) +tup[:2] # => (1, 2) +2 in tup # => True + +# Puoi scompattare le tuple (o liste) in variabili +a, b, c = (1, 2, 3) # a è ora 1, b è ora 2 e c è ora 3 +d, e, f = 4, 5, 6 # puoi anche omettere le parentesi +# Le tuple sono create di default se non usi le parentesi +g = 4, 5, 6 # => (4, 5, 6) +# Guarda come è facile scambiare due valori +e, d = d, e # d è ora 5 ed e è ora 4 + +# I dizionari memorizzano insiemi di dati indicizzati da nomi arbitrari (chiavi) +empty_dict= {} +# Questo è un dizionario pre-caricato +filled_dict = {"uno": 1, "due": 2, "tre": 3} + +# Nota: le chiavi dei dizionari devono essere di tipo immutabile. Questo per +# assicurare che le chiavi possano essere convertite in calori hash costanti +# per un risposta più veloce. +invalid_dict = {[1,2,3]: "123"} # => Emette un TypeError: unhashable type: 'list' +valid_dict = {(1,2,3):[1,2,3]} # I valori, invece, possono essere di qualunque tipo + +# Accedi ai valori indicando la chiave tra [] +filled_dict["uno"] # => 1 + +# Puoi ottenere tutte le chiavi di un dizionario con "keys()" +# (come oggetto iterabile). Per averle in formato lista è necessario +# utilizzare list(). +# Nota - Nei dizionari l'ordine delle chiavi non è garantito. +# Il tuo risultato potrebbe non essere uguale a questo. +list(filled_dict.keys()) # => ["tre", "due", "uno"] + + +# Puoi ottenere tutti i valori di un dizionario con "values()" +# (come oggetto iterabile). +# Anche in questo caso, er averle in formato lista, è necessario utilizzare list() +# Anche in questo caso, come per le chiavi, l'ordine non è garantito +list(filled_dict.values()) # => [3, 2, 1] + +# Controlla l'esistenza delle chiavi in un dizionario con "in" +"uno" in filled_dict # => True +1 in filled_dict # => False + +# Cercando una chiave non esistente genera un KeyError +filled_dict["quattro"] # KeyError + +# Usa il metodo "get()" per evitare KeyError +filled_dict.get("uno") # => 1 +filled_dict.get("quattro") # => None +# Il metodo get supporta un argomento di default quando il valore è mancante +filled_dict.get("uno", 4) # => 1 +filled_dict.get("quattro", 4) # => 4 + + +# "setdefault()" inserisce un valore per una chiave in un dizionario +# solo se la chiave data non è già presente +filled_dict.setdefault("cinque", 5) # filled_dict["cinque"] viene impostato a 5 +filled_dict.setdefault("cinque", 6) # filled_dict["cinque"] rimane 5 + +# Aggiungere una coppia chiave->valore a un dizionario +filled_dict.update({"quattro":4}) # => {"uno": 1, "due": 2, "tre": 3, "quattro": 4} +filled_dict["quattro"] = 4 # un altro modo pe aggiungere a un dizionario + +# Rimuovi una chiave da un dizionario con del +del filled_dict["uno"] # Rimuove la chiave "uno" dal dizionario + +# Da Python 3.5 puoi anche usare ulteriori opzioni di spacchettamento +{'a': 1, **{'b': 2}} # => {'a': 1, 'b': 2} +{'a': 1, **{'a': 2}} # => {'a': 2} + +# I set sono come le liste ma non possono contenere doppioni +empty_set = set() +# Inizializza un "set()" con un dei valori. Sì, sembra un dizionario. +some_set = {1, 1, 2, 2, 3, 4} # set_nuovo è {1, 2, 3, 4} + +# Come le chiavi di un dizionario, gli elementi di un set devono essere +# di tipo immutabile +invalid_set = {[1], 1} # => Genera un "TypeError: unhashable type: 'list'"" +valid_set = {(1,), 1} + +# Aggiungere uno o più elementi ad un set +some_set.add(5) # some_set ora è {1, 2, 3, 4, 5} + +# Fai intersezioni su un set con & +other_set = {3, 4, 5, 6} +some_set & other_set # => {3, 4, 5} + +# Fai unioni su set con | +some_set | other_set # => {1, 2, 3, 4, 5, 6} + +# Fai differenze su set con - +{1, 2, 3, 4} - {2, 3, 5} # => {1, 4} + +# Effettua la differenza simmetrica con ^ +{1, 2, 3, 4} ^ {2, 3, 5} # => {1, 4, 5} + +# Controlla se il set a sinistra contiene quello a destra +{1, 2} >= {1, 2, 3} # => False + +# Controlla se il set a sinistra è un sottoinsieme di quello a destra +{1, 2} <= {1, 2, 3} # => True + +# Controlla l'esistenza in un set con in +2 in some_set # => True +10 in some_set # => False + + + +#################################################### +## 3. Control Flow e oggetti Iterabili +#################################################### + +# Dichiariamo una variabile +some_var = 5 + +# Questo è un controllo if. L'indentazione è molto importante in python! +# Come convenzione si utilizzano quattro spazi, non la tabulazione. +# Il seguente codice stampa "some_var è minore di 10" +if some_var > 10: + print("some_var è maggiore di 10") +elif some_var < 10: # La clausolo elif è opzionale + print("some_var è minore di 10") +else: # Anche else è opzionale + print("some_var è 10.") + +""" +I cicli for iterano sulle liste, cioè ripetono un codice per ogni elemento +di una lista. +Il seguente codice scriverà: + cane è un mammifero + gatto è un mammifero + topo è un mammifero +""" +for animale in ["cane", "gatto", "topo"]: + # Puoi usare format() per interpolare le stringhe formattate. + print("{} è un mammifero".format(animale)) + +""" +"range(numero)" restituisce una lista di numeri da zero al numero dato +Il seguente codice scriverà: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print(i) + +""" +"range(lower, upper)" restituisce una lista di numeri dal più piccolo (lower) +al più grande (upper). +Il seguente codice scriverà: + 4 + 5 + 6 + 7 +""" +for i in range(4, 8): + print(i) + +""" +"range(lower, upper, step)" rrestituisce una lista di numeri dal più piccolo +(lower) al più grande (upper), incrementando del valore step. +Se step non è indicato, avrà come valore di default 1. +Il seguente codice scriverà: + 4 + 6 +""" +for i in range(4, 8, 2): + print(i) +""" + +I cicli while vengono eseguiti finchè una condizione viene a mancare +Il seguente codice scriverà: + 0 + 1 + 2 + 3 +""" +x = 0 +while x < 4: + print(x) + x += 1 # Forma compatta per x = x + 1 + +# Gestione delle eccezioni con un blocco try/except +try: + # Usa "raise" per generare un errore + raise IndexError("Questo è un IndexError") +except IndexError as e: + pass # Pass è solo una non-operazione. Solitamente vorrai rimediare all'errore. +except (TypeError, NameError): + pass # Eccezioni multiple possono essere gestite tutte insieme, se necessario. +else: # Clausola opzionale al blocco try/except. Deve essere dopo tutti i blocchi except + print("Tutto ok!") # Viene eseguita solo se il codice dentro try non genera eccezioni +finally: # Eseguito sempre + print("Possiamo liberare risorse qui") + +# Se ti serve solo un try/finally, per liberare risorse, puoi usare il metodo with +with open("myfile.txt") as f: + for line in f: + print(line) + +# In Python qualunque oggetto in grado di essere trattato come una +# sequenza è definito un oggetto Iterable (itarabile). +# L'oggetto restituito da una funzione range è un iterabile. + +filled_dict = {"uno": 1, "due": 2, "tre": 3} +our_iterable = filled_dict.keys() +print(our_iterable) # => dict_keys(['uno', 'due', 'tre']). +# Questo è un oggetto che implementa la nostra interfaccia Iterable. + +# È possibile utilizzarlo con i loop: +for i in our_iterable: + print(i) # Scrive uno, due, tre + +# Tuttavia non possiamo recuperarne i valori tramite indice. +our_iterable[1] # Genera un TypeError + +# Un oggetto iterabile è in grado di generare un iteratore +our_iterator = iter(our_iterable) + +# L'iteratore è un oggetto che ricorda il suo stato mentro lo si "attraversa" +# Possiamo accedere al successivo elemento con "next()". +next(our_iterator) # => "uno" + +# Mantiene il suo stato mentro eseguiamo l'iterazione +next(our_iterator) # => "due" +next(our_iterator) # => "tre" + +# Dopo che un iteratore ha restituito tutti i suoi dati, genera +# un'eccezione StopIteration +next(our_iterator) # Raises StopIteration + +# Puoi prendere tutti gli elementi di un iteratore utilizzando list(). +list(filled_dict.keys()) # => Returns ["one", "two", "three"] + + + +#################################################### +## 4. Funzioni +#################################################### + +# Usa "def" per creare nuove funzioni +def aggiungi(x, y): + print("x è {} e y è {}".format(x, y)) // Scrive i valori formattati in una stringa + return x + y # Restituisce la somma dei valori con il metodo return + +# Chiamare funzioni con parametri +aggiungi(5, 6) # => scrive "x è 5 e y è 6" e restituisce 11 + +# Un altro modo per chiamare funzioni è con parole chiave come argomenti +aggiungi(y=6, x=5) # In questo modo non è necessario rispettare l'ordine degli argomenti + +# Puoi definire funzioni che accettano un numero non definito di argomenti +def varargs(*args): + return args + +varargs(1, 2, 3) # => (1, 2, 3) + +# Puoi definire funzioni che accettano un numero variabile di parole chiave +# come argomento, che saranno interpretati come un dizionario usando ** +def keyword_args(**kwargs): + return kwargs + +# Chiamiamola per vedere cosa succede +keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"} + + +# Puoi farle entrambi in una volta, se ti va +def all_the_args(*args, **kwargs): + print(args) + print(kwargs) +""" +all_the_args(1, 2, a=3, b=4) stampa: + (1, 2) + {"a": 3, "b": 4} +""" + +# Quando chiami funzioni, puoi fare l'opposto di args/kwargs! +# Usa * per sviluppare gli argomenti posizionale ed usa ** per +# espandere gli argomenti parola chiave +args = (1, 2, 3, 4) +kwargs = {"a": 3, "b": 4} +all_the_args(*args) # equivalente a foo(1, 2, 3, 4) +all_the_args(**kwargs) # equivalente a foo(a=3, b=4) +all_the_args(*args, **kwargs) # equivalente a foo(1, 2, 3, 4, a=3, b=4) + + +# Restituire valori multipli (with tuple assignments) +def swap(x, y): + return y, x # Restituisce valori multipli come tupla senza parentesi + # (Nota: le parentesi sono state escluse ma possono essere messe) + +x = 1 +y = 2 +x, y = swap(x, y) # => x = 2, y = 1 +# (x, y) = swap(x,y) # Le parentesi sono state escluse ma possono essere incluse. + +# Funzioni - Visibilità delle variabili (variable scope) +x = 5 + +def set_x(num): + # La variabile locale x non è la variabile globale x + x = num # => 43 + print(x) # => 43 + +def set_global_x(num): + global x + print(x) # => 5 + x = num # la variabile globable x è ora 6 + print(x) # => 6 + +set_x(43) +set_global_x(6) + + +# Python ha "first class functions" +def create_adder(x): + def adder(y): + return x + y + return adder + +add_10 = create_adder(10) +add_10(3) # => 13 + +# Ci sono anche funzioni anonime +(lambda x: x > 2)(3) # => True +(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5 + +# È possibile creare "mappe" e "filtri" +list(map(add_10, [1, 2, 3])) # => [11, 12, 13] +list(map(max, [1, 2, 3], [4, 2, 1])) # => [4, 2, 3] + +list(filter(lambda x: x > 5, [3, 4, 5, 6, 7])) # => [6, 7] + +# Possiamo usare le "list comprehensions" per mappe e filtri +# Le "list comprehensions" memorizzano l'output come una lista che può essere +# di per sé una lista annidata +[add_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] + +# Puoi fare anche la comprensione di set e dizionari +{x for x in 'abcddeef' if x not in 'abc'} # => {'d', 'e', 'f'} +{x: x**2 for x in range(5)} # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} + + +#################################################### +## 5. Modules +#################################################### + +# Puoi importare moduli +import math +print(math.sqrt(16)) # => 4.0 + +# Puoi ottenere specifiche funzione da un modulo +from math import ceil, floor +print(ceil(3.7)) # => 4.0 +print(floor(3.7)) # => 3.0 + +# Puoi importare tutte le funzioni da un modulo +# Attenzione: questo non è raccomandato +from math import * + +# Puoi abbreviare i nomi dei moduli +import math as m +math.sqrt(16) == m.sqrt(16) # => True + + +# I moduli di Python sono normali file python. Ne puoi +# scrivere di tuoi ed importarli. Il nome del modulo +# è lo stesso del nome del file. + +# Potete scoprire quali funzioni e attributi +# sono definiti in un modulo +import math +dir(math) + +# Se nella cartella corrente hai uno script chiamato math.py, +# Python caricherà quello invece del modulo math. +# Questo succede perchè la cartella corrente ha priorità +# sulle librerie standard di Python + +# Se hai uno script Python chiamato math.py nella stessa +# cartella del tua script, Python caricherà quello al posto del +# comune modulo math. +# Questo accade perché la cartella locale ha la priorità +# sulle librerie built-in di Python. + + +#################################################### +## 6. Classes +#################################################### + +# Usiamo l'istruzione "class" per creare una classe +class Human: + + # Un attributo della classe. E' condiviso tra tutte le istanze delle classe + species = "H. sapiens" + + # Si noti che i doppi underscore iniziali e finali denotano gli oggetti o + # attributi utilizzati da Python ma che vivono nel namespace controllato + # dall'utente + # Metodi, oggetti o attributi come: __init__, __str__, __repr__, etc. sono + # chiamati metodi speciali (o talvolta chiamati "dunder methods"). + # Non dovresti inventare tali nomi da solo. + + def __init__(self, name): + # Assegna l'argomento all'attributo name dell'istanza + self.name = name + + # Inizializza una proprietà + self._age = 0 + + # Un metodo dell'istanza. Tutti i metodi prendo "self" come primo argomento + def say(self, msg): + print("{name}: {message}".format(name=self.name, message=msg)) + + # Un altro metodo dell'istanza + def sing(self): + return 'yo... yo... microphone check... one two... one two...' + + # Un metodo della classe è condiviso fra tutte le istanze + # Sono chiamati con la classe chiamante come primo argomento + @classmethod + def get_species(cls): + return cls.species + + # Un metodo statico è chiamato senza classe o istanza di riferimento + @staticmethod + def grunt(): + return "*grunt*" + + # Una property è come un metodo getter. + # Trasforma il metodo age() in un attributo in sola lettura, che ha + # lo stesso nome + # In Python non c'è bisogno di scrivere futili getter e setter. + @property + def age(self): + return self._age + + # Questo metodo permette di modificare una property + @age.setter + def age(self, age): + self._age = age + + # Questo metodo permette di cancellare una property + @age.deleter + def age(self): + del self._age + +# Quando l'interprete Python legge un sorgente esegue tutto il suo codice. +# Questo controllo su __name__ assicura che questo blocco di codice venga +# eseguito solo quando questo modulo è il programma principale. + +if __name__ == '__main__': + # Crea un'istanza della classe + i = Human(name="Ian") + i.say("hi") # "Ian: hi" + j = Human("Joel") + j.say("hello") # "Joel: hello" + # i e j sono istanze del tipo Human, o in altre parole sono oggetti Human + + # Chiama un metodo della classe + i.say(i.get_species()) # "Ian: H. sapiens" + # Cambia l'attributo condiviso + Human.species = "H. neanderthalensis" + i.say(i.get_species()) # => "Ian: H. neanderthalensis" + j.say(j.get_species()) # => "Joel: H. neanderthalensis" + + # Chiama un metodo statico + print(Human.grunt()) # => "*grunt*" + + # Non è possibile chiamare il metodo statico con l'istanza dell'oggetto + # poiché i.grunt() metterà automaticamente "self" (l'oggetto i) + # come argomento + print(i.grunt()) # => TypeError: grunt() takes 0 positional arguments but 1 was given + + # Aggiorna la property (age) di questa istanza + i.age = 42 + # Leggi la property + i.say(i.age) # => "Ian: 42" + j.say(j.age) # => "Joel: 0" + # Cancella la property + del i.age + i.age # => questo genererà un AttributeError + + +#################################################### +## 6.1 Ereditarietà (Inheritance) +#################################################### + +# L'ereditarietà consente di definire nuove classi figlio che ereditano metodi e +# variabili dalla loro classe genitore. + +# Usando la classe Human definita sopra come classe base o genitore, possiamo +# definire una classe figlia, Superhero, che erediterà le variabili di classe +# come "species", "name" e "age", così come i metodi, come "sing" e "grunt", +# dalla classe Human, ma potrà anche avere le sue proprietà uniche. + +# Per importare le funzioni da altri file usa il seguente formato +# from "nomefile-senza-estensione" import "funzione-o-classe" + +from human import Human + +# Specificare le classi genitore come parametri della definizione della classe +class Superhero(Human): + + # Se la classe figlio deve ereditare tutte le definizioni del genitore + # senza alcuna modifica, puoi semplicemente usare la parola chiave "pass" + # (e nient'altro) + + #Le classi figlio possono sovrascrivere gli attributi dei loro genitori + species = 'Superhuman' + + # Le classi figlie ereditano automaticamente il costruttore della classe + # genitore, inclusi i suoi argomenti, ma possono anche definire ulteriori + # argomenti o definizioni e sovrascrivere i suoi metodi (compreso il + # costruttore della classe). + # Questo costruttore eredita l'argomento "nome" dalla classe "Human" e + # aggiunge gli argomenti "superpowers" e "movie": + + def __init__(self, name, movie=False, + superpowers=["super strength", "bulletproofing"]): + + # aggiungi ulteriori attributi della classe + self.fictional = True + self.movie = movie + self.superpowers = superpowers + + # La funzione "super" ti consente di accedere ai metodi della classe + # genitore che sono stati sovrascritti dalla classe figlia, + # in questo caso il metodo __init__. + # Il seguente codice esegue il costruttore della classe genitore: + super().__init__(name) + + # Sovrascrivere il metodo "sing" + def sing(self): + return 'Dun, dun, DUN!' + + # Aggiungi un ulteriore metodo dell'istanza + def boast(self): + for power in self.superpowers: + print("I wield the power of {pow}!".format(pow=power)) + + +if __name__ == '__main__': + sup = Superhero(name="Tick") + + # Controllo del tipo di istanza + if isinstance(sup, Human): + print('I am human') + if type(sup) is Superhero: + print('I am a superhero') + + # Ottieni il "Method Resolution search Order" usato sia da getattr () + # che da super (). Questo attributo è dinamico e può essere aggiornato + print(Superhero.__mro__) # => (<class '__main__.Superhero'>, + # => <class 'human.Human'>, <class 'object'>) + + # Esegui il metodo principale ma utilizza il proprio attributo di classe + print(sup.get_species()) # => Superhuman + + # Esegui un metodo che è stato sovrascritto + print(sup.sing()) # => Dun, dun, DUN! + + # Esegui un metodo di Human + sup.say('Spoon') # => Tick: Spoon + + # Esegui un metodo che esiste solo in Superhero + sup.boast() # => I wield the power of super strength! + # => I wield the power of bulletproofing! + + # Attributo di classe ereditato + sup.age = 31 + print(sup.age) # => 31 + + # Attributo che esiste solo in Superhero + print('Am I Oscar eligible? ' + str(sup.movie)) + +#################################################### +## 6.2 Ereditarietà multipla +#################################################### + +# Un'altra definizione di classe +# bat.py +class Bat: + + species = 'Baty' + + def __init__(self, can_fly=True): + self.fly = can_fly + + # Questa classe ha anche un metodo "say" + def say(self, msg): + msg = '... ... ...' + return msg + + # E anche un suo metodo personale + def sonar(self): + return '))) ... (((' + +if __name__ == '__main__': + b = Bat() + print(b.say('hello')) + print(b.fly) + +# Definizione di classe che eredita da Superhero e Bat +# superhero.py +from superhero import Superhero +from bat import Bat + +# Definisci Batman come classe figlia che eredita sia da Superhero che da Bat +class Batman(Superhero, Bat): + + def __init__(self, *args, **kwargs): + # In genere per ereditare gli attributi devi chiamare super: + # super(Batman, self).__init__(*args, **kwargs) + # Ma qui abbiamo a che fare con l'ereditarietà multipla, e super() + # funziona solo con la successiva classe nell'elenco MRO. + # Quindi, invece, chiamiamo esplicitamente __init__ per tutti gli + # antenati. L'uso di *args e **kwargs consente di passare in modo + # pulito gli argomenti, con ciascun genitore che "sbuccia un + # livello della cipolla". + Superhero.__init__(self, 'anonymous', movie=True, + superpowers=['Wealthy'], *args, **kwargs) + Bat.__init__(self, *args, can_fly=False, **kwargs) + # sovrascrivere il valore per l'attributo name + self.name = 'Sad Affleck' + + def sing(self): + return 'nan nan nan nan nan batman!' + + +if __name__ == '__main__': + sup = Batman() + + # Ottieni il "Method Resolution search Order" utilizzato da getattr() e super(). + # Questo attributo è dinamico e può essere aggiornato + print(Batman.__mro__) # => (<class '__main__.Batman'>, + # => <class 'superhero.Superhero'>, + # => <class 'human.Human'>, + # => <class 'bat.Bat'>, <class 'object'>) + + # Esegui il metodo del genitore ma utilizza il proprio attributo di classe + print(sup.get_species()) # => Superhuman + + # Esegui un metodo che è stato sovrascritto + print(sup.sing()) # => nan nan nan nan nan batman! + + # Esegui un metodo da Human, perché l'ordine di ereditarietà è importante + sup.say('I agree') # => Sad Affleck: I agree + + # Esegui un metodo che esiste solo nel 2o antenato + print(sup.sonar()) # => ))) ... ((( + + # Attributo di classe ereditato + sup.age = 100 + print(sup.age) # => 100 + + # Attributo ereditato dal secondo antenato il cui valore predefinito + # è stato ignorato. + print('Can I fly? ' + str(sup.fly)) # => Can I fly? False + + + +#################################################### +## 7. Advanced +#################################################### + +# I generatori ti aiutano a creare codice pigro (lazy code). +# Codice che darà un risultato solo quando sarà "valutato" +def double_numbers(iterable): + for i in iterable: + yield i + i + +# I generatori sono efficienti in termini di memoria perché caricano +# solo i dati necessari per elaborare il valore successivo nell'iterabile. +# Ciò consente loro di eseguire operazioni su intervalli di valori +# altrimenti proibitivi. +# NOTA: `range` sostituisce` xrange` in Python 3. +for i in double_numbers(range(1, 900000000)): # `range` is a generator. + print(i) + if i >= 30: + break + +# Proprio come è possibile creare una "list comprehension", è possibile +# creare anche delle "generator comprehensions". +values = (-x for x in [1,2,3,4,5]) +for x in values: + print(x) # prints -1 -2 -3 -4 -5 to console/terminal + +# Puoi anche trasmettere una "generator comprehensions" direttamente +# ad un elenco. +values = (-x for x in [1,2,3,4,5]) +gen_to_list = list(values) +print(gen_to_list) # => [-1, -2, -3, -4, -5] + + +# Decoratori +# In questo esempio "beg" avvolge/wrappa "say". +# Se say_please è True, cambierà il messaggio restituito. +from functools import wraps + +def beg(target_function): + @wraps(target_function) + def wrapper(*args, **kwargs): + msg, say_please = target_function(*args, **kwargs) + if say_please: + return "{} {}".format(msg, "Per favore! Sono povero :(") + return msg + + return wrapper + + +@beg +def say(say_please=False): + msg = "Puoi comprarmi una birra?" + return msg, say_please + + +print(say()) # Puoi comprarmi una birra? +print(say(say_please=True)) # Puoi comprarmi una birra? Per favore! Sono povero :( +``` + +## Pronto per qualcosa di più? + +### Gratis Online + +* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com) +* [Ideas for Python Projects](http://pythonpracticeprojects.com) +* [The Official Docs](http://docs.python.org/3/) +* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) +* [Python Course](http://www.python-course.eu/index.php) +* [First Steps With Python](https://realpython.com/learn/python-first-steps/) +* [A curated list of awesome Python frameworks, libraries and software](https://github.com/vinta/awesome-python) +* [30 Python Language Features and Tricks You May Not Know About](http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html) +* [Official Style Guide for Python](https://www.python.org/dev/peps/pep-0008/) +* [Python 3 Computer Science Circles](http://cscircles.cemc.uwaterloo.ca/) +* [Dive Into Python 3](http://www.diveintopython3.net/index.html) +* [A Crash Course in Python for Scientists](http://nbviewer.jupyter.org/gist/anonymous/5924718) diff --git a/java.html.markdown b/java.html.markdown index ab2be4a2..ca0b04c2 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -11,6 +11,7 @@ contributors: - ["Michael Dähnert", "https://github.com/JaXt0r"] - ["Rob Rose", "https://github.com/RobRoseKnows"] - ["Sean Nam", "https://github.com/seannam"] + - ["Shawn M. Hanes", "https://github.com/smhanes15"] filename: LearnJava.java --- @@ -858,6 +859,108 @@ public class EnumTest { // The enum body can include methods and other fields. // You can see more at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html +// Getting Started with Lambda Expressions +// +// New to Java version 8 are lambda expressions. Lambdas are more commonly found +// in functional programming languages, which means they are methods which can +// be created without belonging to a class, passed around as if it were itself +// an object, and executed on demand. +// +// Final note, lambdas must implement a functional interface. A functional +// interface is one which has only a single abstract method declared. It can +// have any number of default methods. Lambda expressions can be used as an +// instance of that functional interface. Any interface meeting the requirements +// is treated as a functional interface. You can read more about interfaces +// above. +// +import java.util.Map; +import java.util.HashMap; +import java.util.function.*; +import java.security.SecureRandom; + +public class Lambdas { + public static void main(String[] args) { + // Lambda declaration syntax: + // <zero or more parameters> -> <expression body or statement block> + + // We will use this hashmap in our examples below. + Map<String, String> planets = new HashMap<>(); + planets.put("Mercury", "87.969"); + planets.put("Venus", "224.7"); + planets.put("Earth", "365.2564"); + planets.put("Mars", "687"); + planets.put("Jupiter", "4,332.59"); + planets.put("Saturn", "10,759"); + planets.put("Uranus", "30,688.5"); + planets.put("Neptune", "60,182"); + + // Lambda with zero parameters using the Supplier functional interface + // from java.util.function.Supplier. The actual lambda expression is + // what comes after numPlanets =. + Supplier<String> numPlanets = () -> Integer.toString(planets.size()); + System.out.format("Number of Planets: %s\n\n", numPlanets.get()); + + // Lambda with one parameter and using the Consumer functional interface + // from java.util.function.Consumer. This is because planets is a Map, + // which implements both Collection and Iterable. The forEach used here, + // found in Iterable, applies the lambda expression to each member of + // the Collection. The default implementation of forEach behaves as if: + /* + for (T t : this) + action.accept(t); + */ + + // The actual lambda expression is the parameter passed to forEach. + planets.keySet().forEach((p) -> System.out.format("%s\n", p)); + + // If you are only passing a single argument, then the above can also be + // written as (note absent parentheses around p): + planets.keySet().forEach(p -> System.out.format("%s\n", p)); + + // Tracing the above, we see that planets is a HashMap, keySet() returns + // a Set of its keys, forEach applies each element as the lambda + // expression of: (parameter p) -> System.out.format("%s\n", p). Each + // time, the element is said to be "consumed" and the statement(s) + // referred to in the lambda body is applied. Remember the lambda body + // is what comes after the ->. + + // The above without use of lambdas would look more traditionally like: + for (String planet : planets.keySet()) { + System.out.format("%s\n", planet); + } + + // This example differs from the above in that a different forEach + // implementation is used: the forEach found in the HashMap class + // implementing the Map interface. This forEach accepts a BiConsumer, + // which generically speaking is a fancy way of saying it handles + // the Set of each Key -> Value pairs. This default implementation + // behaves as if: + /* + for (Map.Entry<K, V> entry : map.entrySet()) + action.accept(entry.getKey(), entry.getValue()); + */ + + // The actual lambda expression is the parameter passed to forEach. + String orbits = "%s orbits the Sun in %s Earth days.\n"; + planets.forEach((K, V) -> System.out.format(orbits, K, V)); + + // The above without use of lambdas would look more traditionally like: + for (String planet : planets.keySet()) { + System.out.format(orbits, planet, planets.get(planet)); + } + + // Or, if following more closely the specification provided by the + // default implementation: + for (Map.Entry<String, String> planet : planets.entrySet()) { + System.out.format(orbits, planet.getKey(), planet.getValue()); + } + + // These examples cover only the very basic use of lambdas. It might not + // seem like much or even very useful, but remember that a lambda can be + // created as an object that can later be passed as parameters to other + // methods. + } +} ``` ## Further Reading diff --git a/javascript.html.markdown b/javascript.html.markdown index e7066291..ecaf02c5 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -266,6 +266,15 @@ for (var x in person){ description += person[x] + " "; } // description = 'Paul Ken 18 ' +// The for/of statement allows iteration over iterable objects (including the built-in String, +// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, +// and user-defined iterables). +var myPets = ""; +var pets = ["cat", "dog", "hamster", "hedgehog"]; +for (var pet of pets){ + myPets += pet + " "; +} // myPets = 'cat dog hamster hedgehog ' + // && is logical and, || is logical or if (house.size == "big" && house.colour == "blue"){ house.contains = "bear"; @@ -600,10 +609,6 @@ of the language. [Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with attached terminal -[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great -derivative of Eloquent Javascript with extra explanations and clarifications for -some of the more complicated examples. - [Javascript: The Right Way][10] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. @@ -624,6 +629,5 @@ Mozilla Developer Network. [6]: http://www.amazon.com/gp/product/0596805527/ [7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript [8]: http://eloquentjavascript.net/ -[9]: http://watchandcode.com/courses/eloquent-javascript-the-annotated-version [10]: http://jstherightway.org/ [11]: https://javascript.info/ diff --git a/julia.html.markdown b/julia.html.markdown index 9e28452f..15c09da4 100644 --- a/julia.html.markdown +++ b/julia.html.markdown @@ -2,17 +2,18 @@ language: Julia contributors: - ["Leah Hanson", "http://leahhanson.us"] - - ["Pranit Bauva", "http://github.com/pranitbauva1997"] + - ["Pranit Bauva", "https://github.com/pranitbauva1997"] + - ["Daniel YC Lin", "https://github.com/dlintw"] filename: learnjulia.jl --- Julia is a new homoiconic functional language focused on technical computing. -While having the full power of homoiconic macros, first-class functions, and low-level control, Julia is as easy to learn and use as Python. +While having the full power of homoiconic macros, first-class functions, +and low-level control, Julia is as easy to learn and use as Python. -This is based on Julia 0.4. - -```ruby +This is based on Julia 1.0.0 +```julia # Single line comments start with a hash (pound) symbol. #= Multiline comments can be written by putting '#=' before the text and '=#' @@ -26,38 +27,38 @@ This is based on Julia 0.4. # Everything in Julia is an expression. # There are several basic types of numbers. -3 # => 3 (Int64) -3.2 # => 3.2 (Float64) -2 + 1im # => 2 + 1im (Complex{Int64}) -2//3 # => 2//3 (Rational{Int64}) +typeof(3) # => Int64 +typeof(3.2) # => Float64 +typeof(2 + 1im) # => Complex{Int64} +typeof(2 // 3) # => Rational{Int64} # All of the normal infix operators are available. -1 + 1 # => 2 -8 - 1 # => 7 -10 * 2 # => 20 -35 / 5 # => 7.0 -5 / 2 # => 2.5 # dividing an Int by an Int always results in a Float -div(5, 2) # => 2 # for a truncated result, use div -5 \ 35 # => 7.0 -2 ^ 2 # => 4 # power, not bitwise xor -12 % 10 # => 2 +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7.0 +10 / 2 # => 5.0 # dividing integers always results in a Float64 +div(5, 2) # => 2 # for a truncated result, use div +5 \ 35 # => 7.0 +2^2 # => 4 # power, not bitwise xor +12 % 10 # => 2 # Enforce precedence with parentheses -(1 + 3) * 2 # => 8 +(1 + 3) * 2 # => 8 # Bitwise Operators -~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 - -# You can use the bits function to see the binary representation of a number. -bits(12345) +~2 # => -3 # bitwise not +3 & 5 # => 1 # bitwise and +2 | 4 # => 6 # bitwise or +xor(2, 4) # => 6 # bitwise xor +2 >>> 1 # => 1 # logical shift right +2 >> 1 # => 1 # arithmetic shift right +2 << 1 # => 4 # logical/arithmetic shift left + +# Use the bitstring function to see the binary representation of a number. +bitstring(12345) # => "0000000000000000000000000000000000000000000000000011000000111001" -bits(12345.0) +bitstring(12345.0) # => "0100000011001000000111001000000000000000000000000000000000000000" # Boolean values are primitives @@ -65,70 +66,70 @@ true false # Boolean operators -!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 +!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 # Comparisons can be chained -1 < 2 < 3 # => true -2 < 3 < 2 # => false +1 < 2 < 3 # => true +2 < 3 < 2 # => false # Strings are created with " "This is a string." -# Julia has several types of strings, including ASCIIString and UTF8String. -# More on this in the Types section. - # Character literals are written with ' 'a' -# Some strings can be indexed like an array of characters -"This is a string"[1] # => 'T' # Julia indexes from 1 -# However, this is will not work well for UTF8 strings, -# so iterating over strings is recommended (map, for loops, etc). +# Strings are UTF8 encoded. Only if they contain only ASCII characters can +# they be safely indexed. +ascii("This is a string")[1] +# => 'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase) +# Julia indexes from 1 +# Otherwise, iterating over strings is recommended (map, for loops, etc). # $ can be used for string interpolation: "2 + 2 = $(2 + 2)" # => "2 + 2 = 4" # You can put any Julia expression inside the parentheses. -# Another way to format strings is the printf macro. -@printf "%d is less than %f" 4.5 5.3 # 4.5 is less than 5.300000 +# Another way to format strings is the printf macro from the stdlib Printf. +using Printf +@printf "%d is less than %f\n" 4.5 5.3 # => 5 is less than 5.300000 # Printing is easy -println("I'm Julia. Nice to meet you!") +println("I'm Julia. Nice to meet you!") # => I'm Julia. Nice to meet you! # String can be compared lexicographically "good" > "bye" # => true "good" == "good" # => true -"1 + 2 = 3" == "1 + 2 = $(1+2)" # => true +"1 + 2 = 3" == "1 + 2 = $(1 + 2)" # => true #################################################### ## 2. Variables and Collections #################################################### # You don't declare variables before assigning to them. -some_var = 5 # => 5 -some_var # => 5 +some_var = 5 # => 5 +some_var # => 5 # Accessing a previously unassigned variable is an error try - some_other_var # => ERROR: some_other_var not defined + some_other_var # => ERROR: UndefVarError: some_other_var not defined catch e println(e) end # Variable names start with a letter or underscore. # After that, you can use letters, digits, underscores, and exclamation points. -SomeOtherVar123! = 6 # => 6 +SomeOtherVar123! = 6 # => 6 # You can also use certain unicode characters -☃ = 8 # => 8 +☃ = 8 # => 8 # These are especially handy for mathematical notation 2 * π # => 6.283185307179586 @@ -147,166 +148,201 @@ SomeOtherVar123! = 6 # => 6 # functions are sometimes called mutating functions or in-place functions. # Arrays store a sequence of values indexed by integers 1 through n: -a = Int64[] # => 0-element Int64 Array +a = Int64[] # => 0-element Array{Int64,1} # 1-dimensional array literals can be written with comma-separated values. -b = [4, 5, 6] # => 3-element Int64 Array: [4, 5, 6] -b = [4; 5; 6] # => 3-element Int64 Array: [4, 5, 6] -b[1] # => 4 -b[end] # => 6 +b = [4, 5, 6] # => 3-element Array{Int64,1}: [4, 5, 6] +b = [4; 5; 6] # => 3-element Array{Int64,1}: [4, 5, 6] +b[1] # => 4 +b[end] # => 6 # 2-dimensional arrays use space-separated values and semicolon-separated rows. -matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4] +matrix = [1 2; 3 4] # => 2×2 Array{Int64,2}: [1 2; 3 4] -# Arrays of a particular Type -b = Int8[4, 5, 6] # => 3-element Int8 Array: [4, 5, 6] +# Arrays of a particular type +b = Int8[4, 5, 6] # => 3-element Array{Int8,1}: [4, 5, 6] # Add stuff to the end of a list with push! and 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] +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] # Remove from the end with pop -pop!(b) # => 6 and b is now [4,5] +pop!(b) # => 6 +b # => [4,5] # Let's put it back -push!(b,6) # b is now [4,5,6] again. +push!(b, 6) # => [4,5,6] +b # => [4,5,6] -a[1] # => 1 # remember that Julia indexes from 1, not 0! +a[1] # => 1 # remember that Julia indexes from 1, not 0! # end is a shorthand for the last index. It can be used in any # indexing expression -a[end] # => 6 +a[end] # => 6 -# we also have shift and unshift -shift!(a) # => 1 and a is now [2,4,3,4,5,6] -unshift!(a,7) # => [7,2,4,3,4,5,6] +# we also have popfirst! and pushfirst! +popfirst!(a) # => 1 +a # => [2,4,3,4,5,6] +pushfirst!(a, 7) # => [7,2,4,3,4,5,6] +a # => [7,2,4,3,4,5,6] # Function names that end in exclamations points indicate that they modify # their argument. -arr = [5,4,6] # => 3-element Int64 Array: [5,4,6] -sort(arr) # => [4,5,6]; arr is still [5,4,6] -sort!(arr) # => [4,5,6]; arr is now [4,5,6] +arr = [5,4,6] # => 3-element Array{Int64,1}: [5,4,6] +sort(arr) # => [4,5,6] +arr # => [5,4,6] +sort!(arr) # => [4,5,6] +arr # => [4,5,6] # Looking out of bounds is a 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: attempt to access 7-element Array{Int64,1} at + # index [0] + # => Stacktrace: + # => [1] getindex(::Array{Int64,1}, ::Int64) at .\array.jl:731 + # => [2] top-level scope at none:0 + # => [3] ... + # => in expression starting at ...\LearnJulia.jl:180 + a[end + 1] + # => ERROR: BoundsError: attempt to access 7-element Array{Int64,1} at + # index [8] + # => Stacktrace: + # => [1] getindex(::Array{Int64,1}, ::Int64) at .\array.jl:731 + # => [2] top-level scope at none:0 + # => [3] ... + # => in expression starting at ...\LearnJulia.jl:188 catch e println(e) end # Errors list the line and file they came from, even if it's in the standard -# library. If you built Julia from source, you can look in the folder base -# inside the julia folder to find these files. +# library. You can look in the folder share/julia inside the julia folder to +# find these files. # You can initialize arrays from ranges -a = [1:5;] # => 5-element Int64 Array: [1,2,3,4,5] +a = [1:5;] # => 5-element Array{Int64,1}: [1,2,3,4,5] +a2 = [1:5] # => 1-element Array{UnitRange{Int64},1}: [1:5] # You can look at ranges with slice syntax. -a[1:3] # => [1, 2, 3] -a[2:end] # => [2, 3, 4, 5] +a[1:3] # => [1, 2, 3] +a[2:end] # => [2, 3, 4, 5] # Remove elements from an array by index with splice! arr = [3,4,5] -splice!(arr,2) # => 4 ; arr is now [3,5] +splice!(arr, 2) # => 4 +arr # => [3,5] # Concatenate lists with append! b = [1,2,3] -append!(a,b) # Now a is [1, 2, 3, 4, 5, 1, 2, 3] +append!(a, b) # => [1, 2, 3, 4, 5, 1, 2, 3] +a # => [1, 2, 3, 4, 5, 1, 2, 3] # Check for existence in a list with in -in(1, a) # => true +in(1, a) # => true # Examine the length with length -length(a) # => 8 +length(a) # => 8 # Tuples are immutable. -tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple. +tup = (1, 2, 3) # => (1,2,3) +typeof(tup) # => Tuple{Int64,Int64,Int64} tup[1] # => 1 -try: - tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64) +try + tup[1] = 3 + # => ERROR: MethodError: no method matching + # setindex!(::Tuple{Int64,Int64,Int64}, ::Int64, ::Int64) catch e println(e) end -# Many list functions also work on tuples +# Many array functions also work on tuples length(tup) # => 3 -tup[1:2] # => (1,2) -in(2, tup) # => true +tup[1:2] # => (1,2) +in(2, tup) # => true # You can unpack tuples into variables -a, b, c = (1, 2, 3) # => (1,2,3) # a is now 1, b is now 2 and c is now 3 +a, b, c = (1, 2, 3) # => (1,2,3) +a # => 1 +b # => 2 +c # => 3 # Tuples are created even if you leave out the parentheses -d, e, f = 4, 5, 6 # => (4,5,6) +d, e, f = 4, 5, 6 # => (4,5,6) +d # => 4 +e # => 5 +f # => 6 # A 1-element tuple is distinct from the value it contains (1,) == 1 # => false -(1) == 1 # => true +(1) == 1 # => true # Look how easy it is to swap two values -e, d = d, e # => (5,4) # d is now 5 and e is now 4 - +e, d = d, e # => (5,4) +d # => 5 +e # => 4 # Dictionaries store mappings -empty_dict = Dict() # => Dict{Any,Any}() +empty_dict = Dict() # => Dict{Any,Any} with 0 entries # You can create a dictionary using a literal -filled_dict = Dict("one"=> 1, "two"=> 2, "three"=> 3) -# => Dict{ASCIIString,Int64} +filled_dict = Dict("one" => 1, "two" => 2, "three" => 3) +# => Dict{String,Int64} with 3 entries: +# => "two" => 2, "one" => 1, "three" => 3 # Look up values with [] -filled_dict["one"] # => 1 +filled_dict["one"] # => 1 # Get all keys keys(filled_dict) -# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) +# => Base.KeySet for a Dict{String,Int64} with 3 entries. Keys: +# => "two", "one", "three" # Note - dictionary keys are not sorted or in the order you inserted them. # Get all values values(filled_dict) -# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) +# => Base.ValueIterator for a Dict{String,Int64} with 3 entries. Values: +# => 2, 1, 3 # Note - Same as above regarding key ordering. # Check for existence of keys in a dictionary with in, haskey -in(("one" => 1), filled_dict) # => true -in(("two" => 3), filled_dict) # => false -haskey(filled_dict, "one") # => true -haskey(filled_dict, 1) # => false +in(("one" => 1), filled_dict) # => true +in(("two" => 3), filled_dict) # => false +haskey(filled_dict, "one") # => true +haskey(filled_dict, 1) # => false # Trying to look up a non-existent key will raise an error try - filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489 + filled_dict["four"] # => ERROR: KeyError: key "four" not found catch e println(e) end # Use the get method to avoid that error by providing a default value -# get(dictionary,key,default_value) -get(filled_dict,"one",4) # => 1 -get(filled_dict,"four",4) # => 4 +# get(dictionary, key, default_value) +get(filled_dict, "one", 4) # => 1 +get(filled_dict, "four", 4) # => 4 # Use Sets to represent collections of unordered, unique values -empty_set = Set() # => Set{Any}() +empty_set = Set() # => Set(Any[]) # Initialize a set with values -filled_set = Set([1,2,2,3,4]) # => Set{Int64}(1,2,3,4) +filled_set = Set([1, 2, 2, 3, 4]) # => Set([4, 2, 3, 1]) # Add more values to a set -push!(filled_set,5) # => Set{Int64}(5,4,2,3,1) +push!(filled_set, 5) # => Set([4, 2, 3, 5, 1]) # Check if the values are in the set -in(2, filled_set) # => true -in(10, filled_set) # => false +in(2, filled_set) # => true +in(10, filled_set) # => false # There are functions for set intersection, union, and difference. -other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3) -intersect(filled_set, other_set) # => Set{Int64}(3,4,5) -union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6) -setdiff(Set([1,2,3,4]),Set([2,3,5])) # => Set{Int64}(1,4) - +other_set = Set([3, 4, 5, 6]) # => Set([4, 3, 5, 6]) +intersect(filled_set, other_set) # => Set([4, 3, 5]) +union(filled_set, other_set) # => Set([4, 2, 3, 5, 6, 1]) +setdiff(Set([1,2,3,4]), Set([2,3,5])) # => Set([4, 1]) #################################################### ## 3. Control Flow @@ -325,72 +361,67 @@ else # The else clause is optional too. end # => prints "some var is smaller than 10" - # For loops iterate over iterables. # Iterable types include Range, Array, Set, Dict, and AbstractString. -for animal=["dog", "cat", "mouse"] +for animal = ["dog", "cat", "mouse"] println("$animal is a mammal") # You can use $ to interpolate variables or expression into strings end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => dog is a mammal +# => cat is a mammal +# => mouse is a mammal # You can use 'in' instead of '='. for animal in ["dog", "cat", "mouse"] println("$animal is a mammal") end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => dog is a mammal +# => cat is a mammal +# => mouse is a mammal -for a in Dict("dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal") - println("$(a[1]) is a $(a[2])") +for pair in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal") + from, to = pair + println("$from is a $to") end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => mouse is a mammal +# => cat is a mammal +# => dog is a mammal -for (k,v) in Dict("dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal") +for (k, v) in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal") println("$k is a $v") end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => mouse is a mammal +# => cat is a mammal +# => dog is a mammal # While loops loop while a condition is true -x = 0 -while x < 4 - println(x) - x += 1 # Shorthand for x = x + 1 +let x = 0 + while x < 4 + println(x) + x += 1 # Shorthand for x = x + 1 + end end -# prints: -# 0 -# 1 -# 2 -# 3 +# => 0 +# => 1 +# => 2 +# => 3 # Handle exceptions with a try/catch block try - error("help") + error("help") catch e - println("caught it $e") + println("caught it $e") end # => caught it ErrorException("help") - #################################################### ## 4. Functions #################################################### # The keyword 'function' creates new functions -#function name(arglist) -# body... -#end +# function name(arglist) +# body... +# end function add(x, y) println("x is $x and y is $y") @@ -398,15 +429,17 @@ function add(x, y) x + y end -add(5, 6) # => 11 after printing out "x is 5 and y is 6" +add(5, 6) +# => x is 5 and y is 6 +# => 11 # Compact assignment of functions -f_add(x, y) = x + y # => "f (generic function with 1 method)" -f_add(3, 4) # => 7 +f_add(x, y) = x + y # => f_add (generic function with 1 method) +f_add(3, 4) # => 7 # Function can also return multiple values as tuple -f(x, y) = x + y, x - y -f(3, 4) # => (7, -1) +fn(x, y) = x + y, x - y # => fn (generic function with 1 method) +fn(3, 4) # => (7, -1) # You can define functions that take a variable number of # positional arguments @@ -416,41 +449,43 @@ function varargs(args...) end # => varargs (generic function with 1 method) -varargs(1,2,3) # => (1,2,3) +varargs(1, 2, 3) # => (1,2,3) # The ... is called a splat. # We just used it in a function definition. # It can also be used in a function call, # where it will splat an Array or Tuple's contents into the argument list. -add([5,6]...) # this is equivalent to add(5,6) +add([5,6]...) # this is equivalent to add(5,6) -x = (5,6) # => (5,6) -add(x...) # this is equivalent to add(5,6) +x = (5, 6) # => (5,6) +add(x...) # this is equivalent to add(5,6) # You can define functions with optional positional arguments -function defaults(a,b,x=5,y=6) +function defaults(a, b, x=5, y=6) return "$a $b and $x $y" end +# => defaults (generic function with 3 methods) -defaults('h','g') # => "h g and 5 6" -defaults('h','g','j') # => "h g and j 6" -defaults('h','g','j','k') # => "h g and j k" +defaults('h', 'g') # => "h g and 5 6" +defaults('h', 'g', 'j') # => "h g and j 6" +defaults('h', 'g', 'j', 'k') # => "h g and j k" try - defaults('h') # => ERROR: no method defaults(Char,) - defaults() # => ERROR: no methods defaults() + defaults('h') # => ERROR: MethodError: no method matching defaults(::Char) + defaults() # => ERROR: MethodError: no method matching defaults() catch e println(e) end # You can define functions that take keyword arguments -function keyword_args(;k1=4,name2="hello") # note the ; - return Dict("k1"=>k1,"name2"=>name2) +function keyword_args(;k1=4, name2="hello") # note the ; + return Dict("k1" => k1, "name2" => name2) end +# => keyword_args (generic function with 1 method) -keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4] -keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"] -keyword_args() # => ["name2"=>"hello","k1"=>4] +keyword_args(name2="ness") # => ["name2"=>"ness", "k1"=>4] +keyword_args(k1="mine") # => ["name2"=>"hello", "k1"=>"mine"] +keyword_args() # => ["name2"=>"hello", "k1"=>4] # You can combine all kinds of arguments in the same function function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo") @@ -458,12 +493,12 @@ function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo") println("optional arg: $optional_positional_arg") println("keyword arg: $keyword_arg") end +# => all_the_args (generic function with 2 methods) all_the_args(1, 3, keyword_arg=4) -# prints: -# normal arg: 1 -# optional arg: 3 -# keyword arg: 4 +# => normal arg: 1 +# => optional arg: 3 +# => keyword arg: 4 # Julia has first class functions function create_adder(x) @@ -472,14 +507,16 @@ function create_adder(x) end return adder end +# => create_adder (generic function with 1 method) # This is "stabby lambda syntax" for creating anonymous functions -(x -> x > 2)(3) # => true +(x -> x > 2)(3) # => true # This function is identical to create_adder implementation above. function create_adder(x) y -> x + y end +# => create_adder (generic function with 1 method) # You can also name the internal function, if you want function create_adder(x) @@ -488,18 +525,21 @@ function create_adder(x) end adder end +# => create_adder (generic function with 1 method) -add_10 = create_adder(10) +add_10 = create_adder(10) # => (::getfield(Main, Symbol("#adder#11")){Int64}) + # (generic function with 1 method) add_10(3) # => 13 # There are built-in higher order functions -map(add_10, [1,2,3]) # => [11, 12, 13] -filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] +map(add_10, [1,2,3]) # => [11, 12, 13] +filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] -# We can use list comprehensions for nicer maps -[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13] -[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] +# We can use list comprehensions +[add_10(i) for i = [1, 2, 3]] # => [11, 12, 13] +[add_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] #################################################### ## 5. Types @@ -508,11 +548,11 @@ filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] # Julia has a type system. # Every value has a type; variables do not have types themselves. # You can use the `typeof` function to get the type of a value. -typeof(5) # => Int64 +typeof(5) # => Int64 # Types are first-class values -typeof(Int64) # => DataType -typeof(DataType) # => DataType +typeof(Int64) # => DataType +typeof(DataType) # => DataType # DataType is the type that represents types, including itself. # Types are used for documentation, optimizations, and dispatch. @@ -520,80 +560,76 @@ typeof(DataType) # => DataType # Users can define types # They are like records or structs in other languages. -# New types are defined using the `type` keyword. +# New types are defined using the `struct` keyword. -# type Name +# struct Name # field::OptionalType # ... # end -type Tiger - taillength::Float64 - coatcolor # not including a type annotation is the same as `::Any` +struct Tiger + taillength::Float64 + coatcolor # not including a type annotation is the same as `::Any` end # The default constructor's arguments are the properties # of the type, in the order they are listed in the definition -tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange") +tigger = Tiger(3.5, "orange") # => Tiger(3.5,"orange") # The type doubles as the constructor function for values of that type -sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire") +sherekhan = typeof(tigger)(5.6, "fire") # => Tiger(5.6,"fire") # These struct-style types are called concrete types # They can be instantiated, but cannot have subtypes. # The other kind of types is abstract types. # abstract Name -abstract Cat # just a name and point in the type hierarchy +abstract type Cat end # just a name and point in the type hierarchy # Abstract types cannot be instantiated, but can have subtypes. # For example, Number is an abstract type -subtypes(Number) # => 2-element Array{Any,1}: - # Complex{T<:Real} - # Real -subtypes(Cat) # => 0-element Array{Any,1} +subtypes(Number) # => 2-element Array{Any,1}: + # => Complex + # => Real +subtypes(Cat) # => 0-element Array{Any,1} # AbstractString, as the name implies, is also an abstract type -subtypes(AbstractString) # 8-element Array{Any,1}: - # Base.SubstitutionString{T<:AbstractString} - # DirectIndexString - # RepString - # RevString{T<:AbstractString} - # RopeString - # SubString{T<:AbstractString} - # UTF16String - # UTF8String - -# Every type has a super type; use the `super` function to get it. +subtypes(AbstractString) # => 4-element Array{Any,1}: + # => String + # => SubString + # => SubstitutionString + # => Test.GenericString + +# Every type has a super type; use the `supertype` function to get it. 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 +supertype(Int64) # => Signed +supertype(Signed) # => Integer +supertype(Integer) # => Real +supertype(Real) # => Number +supertype(Number) # => Any +supertype(supertype(Signed)) # => Real +supertype(Any) # => Any # All of these type, except for Int64, are abstract. -typeof("fire") # => ASCIIString -super(ASCIIString) # => DirectIndexString -super(DirectIndexString) # => AbstractString -# Likewise here with ASCIIString +typeof("fire") # => String +supertype(String) # => AbstractString +# Likewise here with String +supertype(SubString) # => AbstractString # <: is the subtyping operator -type Lion <: Cat # Lion is a subtype of Cat - mane_color - roar::AbstractString +struct Lion <: Cat # Lion is a subtype of Cat + mane_color + roar::AbstractString end # You can define more constructors for your type # Just define a function of the same name as the type # and call an existing constructor to get a value of the correct type -Lion(roar::AbstractString) = Lion("green",roar) +Lion(roar::AbstractString) = Lion("green", roar) # This is an outer constructor because it's outside the type definition -type Panther <: Cat # Panther is also a subtype of Cat - eye_color - Panther() = new("green") - # Panthers will only have this constructor, and no default constructor. +struct Panther <: Cat # Panther is also a subtype of Cat + eye_color + Panther() = new("green") + # Panthers will only have this constructor, and no default constructor. end # Using inner constructors, like Panther does, gives you control # over how values of the type can be created. @@ -611,35 +647,36 @@ end # Definitions for Lion, Panther, Tiger function meow(animal::Lion) - animal.roar # access type properties using dot notation + animal.roar # access type properties using dot notation end function meow(animal::Panther) - "grrr" + "grrr" end function meow(animal::Tiger) - "rawwwr" + "rawwwr" end # Testing the meow function -meow(tigger) # => "rawwr" -meow(Lion("brown","ROAAR")) # => "ROAAR" +meow(tigger) # => "rawwwr" +meow(Lion("brown", "ROAAR")) # => "ROAAR" meow(Panther()) # => "grrr" # Review the local type hierarchy -issubtype(Tiger,Cat) # => false -issubtype(Lion,Cat) # => true -issubtype(Panther,Cat) # => true +Tiger <: Cat # => false +Lion <: Cat # => true +Panther <: Cat # => true # Defining a function that takes Cats function pet_cat(cat::Cat) - println("The cat says $(meow(cat))") + println("The cat says $(meow(cat))") end +# => pet_cat (generic function with 1 method) -pet_cat(Lion("42")) # => prints "The cat says 42" +pet_cat(Lion("42")) # => The cat says 42 try - pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,) + pet_cat(tigger) # => ERROR: MethodError: no method matching pet_cat(::Tiger) catch e println(e) end @@ -649,130 +686,179 @@ end # In Julia, all of the argument types contribute to selecting the best method. # Let's define a function with more arguments, so we can see the difference -function fight(t::Tiger,c::Cat) - println("The $(t.coatcolor) tiger wins!") +function fight(t::Tiger, c::Cat) + println("The $(t.coatcolor) tiger wins!") end # => fight (generic function with 1 method) -fight(tigger,Panther()) # => prints The orange tiger wins! -fight(tigger,Lion("ROAR")) # => prints The orange tiger wins! +fight(tigger, Panther()) # => The orange tiger wins! +fight(tigger, Lion("ROAR")) # => The orange tiger wins! # Let's change the behavior when the Cat is specifically a Lion -fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!") +fight(t::Tiger, l::Lion) = println("The $(l.mane_color)-maned lion wins!") # => fight (generic function with 2 methods) -fight(tigger,Panther()) # => prints The orange tiger wins! -fight(tigger,Lion("ROAR")) # => prints The green-maned lion wins! +fight(tigger, Panther()) # => The orange tiger wins! +fight(tigger, Lion("ROAR")) # => The green-maned lion wins! # We don't need a Tiger in order to fight -fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))") +fight(l::Lion, c::Cat) = println("The victorious cat says $(meow(c))") # => fight (generic function with 3 methods) -fight(Lion("balooga!"),Panther()) # => prints The victorious cat says grrr +fight(Lion("balooga!"), Panther()) # => The victorious cat says grrr try - fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion) -catch + fight(Panther(), Lion("RAWR")) + # => ERROR: MethodError: no method matching fight(::Panther, ::Lion) + # => Closest candidates are: + # => fight(::Tiger, ::Lion) at ... + # => fight(::Tiger, ::Cat) at ... + # => fight(::Lion, ::Cat) at ... + # => ... +catch e + println(e) end # Also let the cat go first -fight(c::Cat,l::Lion) = println("The cat beats the Lion") -# => Warning: New definition -# fight(Cat,Lion) at none:1 -# is ambiguous with -# fight(Lion,Cat) at none:2. -# Make sure -# fight(Lion,Lion) -# is defined first. -#fight (generic function with 4 methods) +fight(c::Cat, l::Lion) = println("The cat beats the Lion") +# => fight (generic function with 4 methods) # This warning is because it's unclear which fight will be called in: -fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The victorious cat says rarrr +try + fight(Lion("RAR"), Lion("brown", "rarrr")) + # => ERROR: MethodError: fight(::Lion, ::Lion) is ambiguous. Candidates: + # => fight(c::Cat, l::Lion) in Main at ... + # => fight(l::Lion, c::Cat) in Main at ... + # => Possible fix, define + # => fight(::Lion, ::Lion) + # => ... +catch e + println(e) +end # The result may be different in other versions of Julia -fight(l::Lion,l2::Lion) = println("The lions come to a tie") -fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The lions come to a tie +fight(l::Lion, l2::Lion) = println("The lions come to a tie") +# => fight (generic function with 5 methods) +fight(Lion("RAR"), Lion("brown", "rarrr")) # => The lions come to a tie # Under the hood # You can take a look at the llvm and the assembly code generated. -square_area(l) = l * l # square_area (generic function with 1 method) +square_area(l) = l * l # square_area (generic function with 1 method) -square_area(5) #25 +square_area(5) # => 25 # What happens when we feed square_area an integer? -code_native(square_area, (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(square_area, (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(square_area, (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 - # +code_native(square_area, (Int32,), syntax = :intel) + # .text + # ; Function square_area { + # ; Location: REPL[116]:1 # Prologue + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: int.jl:54 + # imul ecx, ecx # Square l and store the result in ECX + # ;} + # mov eax, ecx + # pop rbp # Restore old base pointer + # ret # Result will still be in EAX + # nop dword ptr [rax + rax] + # ;} + +code_native(square_area, (Float32,), syntax = :intel) + # .text + # ; Function square_area { + # ; Location: REPL[116]:1 + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: float.jl:398 + # vmulss xmm0, xmm0, xmm0 # Scalar single precision multiply (AVX) + # ;} + # pop rbp + # ret + # nop word ptr [rax + rax] + # ;} + +code_native(square_area, (Float64,), syntax = :intel) + # .text + # ; Function square_area { + # ; Location: REPL[116]:1 + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm0, xmm0, xmm0 # Scalar double precision multiply (AVX) + # ;} + # pop rbp + # ret + # nop word ptr [rax + rax] + # ;} + # Note that julia will use floating point instructions if any of the # arguments are floats. # Let's calculate the area of a circle circle_area(r) = pi * r * r # circle_area (generic function with 1 method) -circle_area(5) # 78.53981633974483 - -code_native(circle_area, (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 - # - -code_native(circle_area, (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 - # +circle_area(5) # 78.53981633974483 + +code_native(circle_area, (Int32,), syntax = :intel) + # .text + # ; Function circle_area { + # ; Location: REPL[121]:1 + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: operators.jl:502 + # ; Function *; { + # ; Location: promotion.jl:314 + # ; Function promote; { + # ; Location: promotion.jl:284 + # ; Function _promote; { + # ; Location: promotion.jl:261 + # ; Function convert; { + # ; Location: number.jl:7 + # ; Function Type; { + # ; Location: float.jl:60 + # vcvtsi2sd xmm0, xmm0, ecx # Load integer (r) from memory + # movabs rax, 497710928 # Load pi + # ;}}}}} + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm1, xmm0, qword ptr [rax] # pi * r + # vmulsd xmm0, xmm1, xmm0 # (pi * r) * r + # ;}} + # pop rbp + # ret + # nop dword ptr [rax] + # ;} + +code_native(circle_area, (Float64,), syntax = :intel) + # .text + # ; Function circle_area { + # ; Location: REPL[121]:1 + # push rbp + # mov rbp, rsp + # movabs rax, 497711048 + # ; Function *; { + # ; Location: operators.jl:502 + # ; Function *; { + # ; Location: promotion.jl:314 + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm1, xmm0, qword ptr [rax] + # ;}}} + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm0, xmm1, xmm0 + # ;} + # pop rbp + # ret + # nop dword ptr [rax + rax] + # ;} ``` ## Further Reading -You can get a lot more detail from [The Julia Manual](http://docs.julialang.org/en/latest/#Manual-1) +You can get a lot more detail from the [Julia Documentation](https://docs.julialang.org/) The best place to get help with Julia is the (very friendly) [Discourse forum](https://discourse.julialang.org/). diff --git a/kdb+.html.markdown b/kdb+.html.markdown index 097f177b..027b6571 100644 --- a/kdb+.html.markdown +++ b/kdb+.html.markdown @@ -689,14 +689,14 @@ first each (1 2 3;4 5 6;7 8 9) / each-left (\:) and each-right (/:) modify a two-argument function / to treat one of the arguments and individual variables instead of a list -1 2 3 +\: 1 2 3 -/ => 2 3 4 -/ => 3 4 5 -/ => 4 5 6 -1 2 3 +/: 1 2 3 -/ => 2 3 4 -/ => 3 4 5 -/ => 4 5 6 +1 2 3 +\: 11 22 33 +/ => 12 23 34 +/ => 13 24 35 +/ => 14 25 36 +1 2 3 +/: 11 22 33 +/ => 12 13 14 +/ => 23 24 25 +/ => 34 35 36 / The true alternatives to loops in q are the adverbs scan (\) and over (/) / their behaviour differs based on the number of arguments the function they diff --git a/ko-kr/markdown-kr.html.markdown b/ko-kr/markdown-kr.html.markdown index bfa2a877..397e9f30 100644 --- a/ko-kr/markdown-kr.html.markdown +++ b/ko-kr/markdown-kr.html.markdown @@ -25,7 +25,7 @@ lang: ko-kr ## HTML 요소 HTML은 마크다운의 수퍼셋입니다. 모든 HTML 파일은 유효한 마크다운이라는 것입니다. -```markdown +```md <!--따라서 주석과 같은 HTML 요소들을 마크다운에 사용할 수 있으며, 마크다운 파서에 영향을 받지 않을 것입니다. 하지만 마크다운 파일에서 HTML 요소를 만든다면 그 요소의 안에서는 마크다운 문법을 사용할 수 없습니다.--> @@ -34,7 +34,7 @@ HTML은 마크다운의 수퍼셋입니다. 모든 HTML 파일은 유효한 마 텍스트 앞에 붙이는 우물 정 기호(#)의 갯수에 따라 `<h1>`부터 `<h6>`까지의 HTML 요소를 손쉽게 작성할 수 있습니다. -```markdown +```md # <h1>입니다. ## <h2>입니다. ### <h3>입니다. @@ -43,7 +43,7 @@ HTML은 마크다운의 수퍼셋입니다. 모든 HTML 파일은 유효한 마 ###### <h6>입니다. ``` 또한 h1과 h2를 나타내는 다른 방법이 있습니다. -```markdown +```md h1입니다. ============= @@ -53,7 +53,7 @@ h2입니다. ## 간단한 텍스트 꾸미기 마크다운으로 쉽게 텍스트를 기울이거나 굵게 할 수 있습니다. -```markdown +```md *기울인 텍스트입니다.* _이 텍스트도 같습니다._ @@ -65,14 +65,14 @@ __이 텍스트도 같습니다.__ *__이것도 같습니다.__* ``` 깃헙 전용 마크다운에는 취소선도 있습니다. -```markdown +```md ~~이 텍스트에는 취소선이 그려집니다.~~ ``` ## 문단 문단은 하나 이상의 빈 줄로 구분되는, 한 줄 이상의 인접한 텍스트입니다. -```markdown +```md 문단입니다. 문단에 글을 쓰다니 재밌지 않나요? 이제 두 번째 문단입니다. @@ -83,7 +83,7 @@ __이 텍스트도 같습니다.__ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어쓰기로 문단을 끝내고 새 문단을 시작할 수 있습니다. -```markdown +```md 띄어쓰기 두 개로 끝나는 문단 (마우스로 긁어 보세요). 이 위에는 `<br />` 태그가 있습니다. @@ -91,7 +91,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 인용문은 > 문자로 쉽게 쓸 수 있습니다. -```markdown +```md > 인용문입니다. 수동으로 개행하고서 > 줄마다 `>`를 칠 수도 있고 줄을 길게 쓴 다음에 저절로 개행되게 내버려 둘 수도 있습니다. > `>`로 시작하기만 한다면 차이가 없습니다. @@ -103,7 +103,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 ## 목록 순서가 없는 목록은 별표, 더하기, 하이픈을 이용해 만들 수 있습니다. -```markdown +```md * 이거 * 저거 * 그거 @@ -111,7 +111,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 또는 -```markdown +```md + 이거 + 저거 + 그거 @@ -119,7 +119,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 또는 -```markdown +```md - 이거 - 저거 - 그거 @@ -127,7 +127,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 순서가 있는 목록은 숫자와 마침표입니다. -```markdown +```md 1. 하나 2. 둘 3. 셋 @@ -135,7 +135,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 숫자를 정확히 붙이지 않더라도 제대로 된 순서로 보여주겠지만, 좋은 생각은 아닙니다. -```markdown +```md 1. 하나 1. 둘 1. 셋 @@ -144,7 +144,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 목록 안에 목록이 올 수도 있습니다. -```markdown +```md 1. 하나 2. 둘 3. 셋 @@ -155,7 +155,7 @@ HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어 심지어 할 일 목록도 있습니다. HTML 체크박스가 만들어집니다. -```markdown +```md x가 없는 박스들은 체크되지 않은 HTML 체크박스입니다. - [ ] 첫 번째 할 일 - [ ] 두 번째 할 일 @@ -168,13 +168,13 @@ x가 없는 박스들은 체크되지 않은 HTML 체크박스입니다. 띄어쓰기 네 개 혹은 탭 한 개로 줄을 들여씀으로서 (`<code> 요소를 사용하여`) 코드를 나타낼 수 있습니다. -```markdown +```md puts "Hello, world!" ``` 탭을 더 치거나 띄어쓰기를 네 번 더 함으로써 코드를 들여쓸 수 있습니다. -```markdown +```md my_array.each do |item| puts item end @@ -182,7 +182,7 @@ x가 없는 박스들은 체크되지 않은 HTML 체크박스입니다. 인라인 코드는 백틱 문자를 이용하여 나타냅니다. ` -```markdown +```md 철수는 `go_to()` 함수가 뭘 했는지도 몰랐어! ``` @@ -202,7 +202,7 @@ end 수평선(`<hr/>`)은 셋 이상의 별표나 하이픈을 이용해 쉽게 나타낼 수 있습니다. 띄어쓰기가 포함될 수 있습니다. -```markdown +```md *** --- - - - @@ -213,19 +213,19 @@ end 마크다운의 장점 중 하나는 링크를 만들기 쉽다는 것입니다. 대괄호 안에 나타낼 텍스트를 쓰고 괄호 안에 URL을 쓰면 됩니다. -```markdown +```md [클릭](http://test.com/) ``` 괄호 안에 따옴표를 이용해 링크에 제목을 달 수도 있습니다. -```markdown +```md [클릭](http://test.com/ "test.com으로 가기") ``` 상대 경로도 유효합니다. -```markdown +```md [music으로 가기](/music/). ``` @@ -251,7 +251,7 @@ end ## 이미지 이미지는 링크와 같지만 앞에 느낌표가 붙습니다. -```markdown +```md ![이미지의 alt 속성](http://imgur.com/myimage.jpg "제목") ``` @@ -264,18 +264,18 @@ end ## 기타 ### 자동 링크 -```markdown +```md <http://testwebsite.com/>와 [http://testwebsite.com/](http://testwebsite.com/)는 동일합니다. ``` ### 이메일 자동 링크 -```markdown +```md <foo@bar.com> ``` ### 탈출 문자 -```markdown +```md *별표 사이에 이 텍스트*를 치고 싶지만 기울이고 싶지는 않다면 이렇게 하시면 됩니다. \*별표 사이에 이 텍스트\*. ``` @@ -284,7 +284,7 @@ end 깃헙 전용 마크다운에서는 `<kbd>` 태그를 이용해 키보드 키를 나타낼 수 있습니다. -```markdown +```md 컴퓨터가 멈췄다면 눌러보세요. <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd> ``` @@ -292,14 +292,14 @@ end ### 표 표는 깃헙 전용 마크다운에서만 쓸 수 있고 다소 복잡하지만, 정말 쓰고 싶으시다면 -```markdown +```md | 1열 | 2열 | 3열 | | :--------| :-------: | --------: | | 왼쪽 정렬 | 가운데 정렬 | 오른쪽 정렬 | | 머시기 | 머시기 | 머시기 | ``` 혹은 -```markdown +```md 1열 | 2열 | 3열 :-- | :-: | --: 으악 너무 못생겼어 | 그만 | 둬 diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 0c787d7e..86d1baa0 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -370,6 +370,12 @@ fun useObject() { val someRef: Any = ObjectExample // we use objects name just as is } + +/* The not-null assertion operator (!!) converts any value to a non-null type and +throws an exception if the value is null. +*/ +var b: String? = "abc" +val l = b!!.length ``` ### Further Reading diff --git a/lambda-calculus.html.markdown b/lambda-calculus.html.markdown index 6103c015..72ed78ba 100644 --- a/lambda-calculus.html.markdown +++ b/lambda-calculus.html.markdown @@ -3,6 +3,7 @@ category: Algorithms & Data Structures name: Lambda Calculus contributors: - ["Max Sun", "http://github.com/maxsun"] + - ["Yan Hui Hang", "http://github.com/yanhh0"] --- # Lambda Calculus @@ -114,8 +115,100 @@ Using successor, we can define add: **Challenge:** try defining your own multiplication function! +## Get even smaller: SKI, SK and Iota + +### SKI Combinator Calculus + +Let S, K, I be the following functions: + +`I x = x` + +`K x y = x` + +`S x y z = x z (y z)` + +We can convert an expression in the lambda calculus to an expression +in the SKI combinator calculus: + +1. `λx.x = I` +2. `λx.c = Kc` +3. `λx.(y z) = S (λx.y) (λx.z)` + +Take the church number 2 for example: + +`2 = λf.λx.f(f x)` + +For the inner part `λx.f(f x)`: +``` + λx.f(f x) += S (λx.f) (λx.(f x)) (case 3) += S (K f) (S (λx.f) (λx.x)) (case 2, 3) += S (K f) (S (K f) I) (case 2, 1) +``` + +So: +``` + 2 += λf.λx.f(f x) += λf.(S (K f) (S (K f) I)) += λf.((S (K f)) (S (K f) I)) += S (λf.(S (K f))) (λf.(S (K f) I)) (case 3) +``` + +For the first argument `λf.(S (K f))`: +``` + λf.(S (K f)) += S (λf.S) (λf.(K f)) (case 3) += S (K S) (S (λf.K) (λf.f)) (case 2, 3) += S (K S) (S (K K) I) (case 2, 3) +``` + +For the second argument `λf.(S (K f) I)`: +``` + λf.(S (K f) I) += λf.((S (K f)) I) += S (λf.(S (K f))) (λf.I) (case 3) += S (S (λf.S) (λf.(K f))) (K I) (case 2, 3) += S (S (K S) (S (λf.K) (λf.f))) (K I) (case 1, 3) += S (S (K S) (S (K K) I)) (K I) (case 1, 2) +``` + +Merging them up: +``` + 2 += S (λf.(S (K f))) (λf.(S (K f) I)) += S (S (K S) (S (K K) I)) (S (S (K S) (S (K K) I)) (K I)) +``` + +Expanding this, we would end up with the same expression for the +church number 2 again. + +### SK Combinator Calculus + +The SKI combinator calculus can still be reduced further. We can +remove the I combinator by noting that `I = SKK`. We can substitute +all `I`'s with `SKK`. + +### Iota Combinator + +The SK combinator calculus is still not minimal. Defining: + +``` +ι = λf.((f S) K) +``` + +We have: + +``` +I = ιι +K = ι(ιI) = ι(ι(ιι)) +S = ι(K) = ι(ι(ι(ιι))) +``` + ## For more advanced reading: 1. [A Tutorial Introduction to the Lambda Calculus](http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf) 2. [Cornell CS 312 Recitation 26: The Lambda Calculus](http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html) -3. [Wikipedia - Lambda Calculus](https://en.wikipedia.org/wiki/Lambda_calculus)
\ No newline at end of file +3. [Wikipedia - Lambda Calculus](https://en.wikipedia.org/wiki/Lambda_calculus) +4. [Wikipedia - SKI combinator calculus](https://en.wikipedia.org/wiki/SKI_combinator_calculus) +5. [Wikipedia - Iota and Jot](https://en.wikipedia.org/wiki/Iota_and_Jot) diff --git a/lua.html.markdown b/lua.html.markdown index 1e2d4366..32174a81 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -62,6 +62,11 @@ if not aBoolValue then print('twas false') end -- in C/js: ans = aBoolValue and 'yes' or 'no' --> 'no' +-- BEWARE: this only acts as a ternary if the value returned when the condition +-- evaluates to true is not `false` or Nil +iAmNotFalse = (not aBoolValue) and false or true --> true +iAmAlsoNotFalse = (not aBoolValue) and true or false --> true + karlSum = 0 for i = 1, 100 do -- The range includes both ends. karlSum = karlSum + i diff --git a/markdown.html.markdown b/markdown.html.markdown index ece2567c..cf4286e2 100644 --- a/markdown.html.markdown +++ b/markdown.html.markdown @@ -197,7 +197,7 @@ inside your code end ``` -Inline code can be created using the backtick character ` +Inline code can be created using the backtick character `` ` `` ```md John didn't even know what the `go_to()` function did! diff --git a/matlab.html.markdown b/matlab.html.markdown index 6dc9f697..5790bcc6 100644 --- a/matlab.html.markdown +++ b/matlab.html.markdown @@ -221,11 +221,11 @@ A(1, :) =[] % Delete the first row of the matrix A(:, 1) =[] % Delete the first column of the matrix transpose(A) % Transpose the matrix, which is the same as: -A one -ctranspose(A) % Hermitian transpose the matrix -% (the transpose, followed by taking complex conjugate of each element) -A' % Concise version of complex transpose A.' % Concise version of transpose (without taking complex conjugate) +ctranspose(A) % Hermitian transpose the matrix, which is the same as: +A' % Concise version of complex transpose + % (the transpose, followed by taking complex conjugate of each element) + @@ -374,8 +374,8 @@ disp('Hello World') % Print out a string fprintf % Print to Command Window with more control % Conditional statements (the parentheses are optional, but good style) -if (a > 15) - disp('Greater than 15') +if (a > 23) + disp('Greater than 23') elseif (a == 23) disp('a is 23') else @@ -545,7 +545,7 @@ ans = multiplyLatBy(a,3) % The method can also be called using dot notation. In this case, the object % does not need to be passed to the method. -ans = a.multiplyLatBy(a,1/3) +ans = a.multiplyLatBy(1/3) % Matlab functions can be overloaded to handle objects. % In the method above, we have overloaded how Matlab handles diff --git a/mips.html.markdown b/mips.html.markdown new file mode 100644 index 00000000..4134d3fa --- /dev/null +++ b/mips.html.markdown @@ -0,0 +1,366 @@ +--- +language: "MIPS Assembly" +filename: MIPS.asm +contributors: + - ["Stanley Lim", "https://github.com/Spiderpig86"] +--- + +The MIPS (Microprocessor without Interlocked Pipeline Stages) Assembly language +is designed to work with the MIPS microprocessor paradigm designed by J. L. +Hennessy in 1981. These RISC processors are used in embedded systems such as +gateways and routers. + +[Read More](https://en.wikipedia.org/wiki/MIPS_architecture) + +```asm +# Comments are denoted with a '#' + +# Everything that occurs after a '#' will be ignored by the assembler's lexer. + +# Programs typically contain a .data and .text sections + +.data # Section where data is stored in memory (allocated in RAM), similar to + # variables in higher level languages + + # Declarations follow a ( label: .type value(s) ) form of declaration + hello_world: .asciiz "Hello World\n" # Declare a null terminated string + num1: .word 42 # Integers are referred to as words + # (32 bit value) + + arr1: .word 1, 2, 3, 4, 5 # Array of words + arr2: .byte 'a', 'b' # Array of chars (1 byte each) + buffer: .space 60 # Allocates space in the RAM + # (not cleared to 0) + + # Datatype sizes + _byte: .byte 'a' # 1 byte + _halfword: .half 53 # 2 bytes + _word: .word 3 # 4 bytes + _float: .float 3.14 # 4 bytes + _double: .double 7.0 # 8 bytes + + .align 2 # Memory alignment of data, where + # number indicates byte alignment in + # powers of 2. (.align 2 represents + # word alignment since 2^2 = 4 bytes) + +.text # Section that contains instructions + # and program logic +.globl _main # Declares an instruction label as + # global, making it accessible to + # other files + + _main: # MIPS programs execute instructions + # sequentially, where the code under + # this label will be executed firsts + + # Let's print "hello world" + la $a0, hello_world # Load address of string stored in + # memory + li $v0, 4 # Load the syscall value (indicating + # type of functionality) + syscall # Perform the specified syscall with + # the given argument ($a0) + + # Registers (used to hold data during program execution) + # $t0 - $t9 # Temporary registers used for + # intermediate calculations inside + # subroutines (not saved across + # function calls) + + # $s0 - $s7 # Saved registers where values are + # saved across subroutine calls. + # Typically saved in stack + + # $a0 - $a3 # Argument registers for passing in + # arguments for subroutines + # $v0 - $v1 # Return registers for returning + # values to caller function + + # Types of load/store instructions + la $t0, label # Copy the address of a value in + # memory specified by the label into + # register $t0 + lw $t0, label # Copy a word value from memory + lw $t1, 4($s0) # Copy a word value from an address + # stored in a register with an offset + # of 4 bytes (addr + 4) + lb $t2, label # Copy a byte value to the lower order + # portion of the register $t2 + lb $t2, 0($s0) # Copy a byte value from the source + # address in $s0 with offset 0 + # Same idea with 'lh' for halfwords + + sw $t0, label # Store word value into memory address + # mapped by label + sw $t0, 8($s0) # Store word value into address + # specified in $s0 and offset of 8 bytes + # Same idea using 'sb' and 'sh' for bytes and halfwords. 'sa' does not exist + +### Math ### + _math: + # Remember to load your values into a register + lw $t0, num # From the data section + li $t0, 5 # Or from an immediate (constant) + li $t1, 6 + add $t2, $t0, $t1 # $t2 = $t0 + $t1 + sub $t2, $t0, $t1 # $t2 = $t0 - $t1 + mul $t2, $t0, $t1 # $t2 = $t0 * $t1 + div $t2, $t0, $t1 # $t2 = $t0 / $t1 (Might not be + # supported in some versons of MARS) + div $t0, $t1 # Performs $t0 / $t1. Get the quotient + # using 'mflo' and remainder using 'mfhi' + + # Bitwise Shifting + sll $t0, $t0, 2 # Bitwise shift to the left with + # immediate (constant value) of 2 + sllv $t0, $t1, $t2 # Shift left by a variable amount in + # register + srl $t0, $t0, 5 # Bitwise shift to the right (does + # not sign preserve, sign-extends with 0) + srlv $t0, $t1, $t2 # Shift right by a variable amount in + # a register + sra $t0, $t0, 7 # Bitwise arithmetic shift to the right + # (preserves sign) + srav $t0, $t1, $t2 # Shift right by a variable amount + # in a register + + # Bitwise operators + and $t0, $t1, $t2 # Bitwise AND + andi $t0, $t1, 0xFFF # Bitwise AND with immediate + or $t0, $t1, $t2 # Bitwise OR + ori $t0, $t1, 0xFFF # Bitwise OR with immediate + xor $t0, $t1, $t2 # Bitwise XOR + xori $t0, $t1, 0xFFF # Bitwise XOR with immediate + nor $t0, $t1, $t2 # Bitwise NOR + +## BRANCHING ## + _branching: + # The basic format of these branching instructions typically follow <instr> + # <reg1> <reg2> <label> where label is the label we want to jump to if the + # given conditional evaluates to true + # Sometimes it is easier to write the conditional logic backwards, as seen + # in the simple if statement example below + + beq $t0, $t1, reg_eq # Will branch to reg_eq if + # $t0 == $t1, otherwise + # execute the next line + bne $t0, $t1, reg_neq # Branches when $t0 != $t1 + b branch_target # Unconditional branch, will always execute + beqz $t0, req_eq_zero # Branches when $t0 == 0 + bnez $t0, req_neq_zero # Branches when $t0 != 0 + bgt $t0, $t1, t0_gt_t1 # Branches when $t0 > $t1 + bge $t0, $t1, t0_gte_t1 # Branches when $t0 >= $t1 + bgtz $t0, t0_gt0 # Branches when $t0 > 0 + blt $t0, $t1, t0_gt_t1 # Branches when $t0 < $t1 + ble $t0, $t1, t0_gte_t1 # Branches when $t0 <= $t1 + bltz $t0, t0_lt0 # Branches when $t0 < 0 + slt $s0, $t0, $t1 # Instruction that sends a signal when + # $t0 < $t1 with reuslt in $s0 (1 for true) + + # Simple if statement + # if (i == j) + # f = g + h; + # f = f - i; + + # Let $s0 = f, $s1 = g, $s2 = h, $s3 = i, $s4 = j + bne $s3, $s4, L1 # if (i !=j) + add $s0, $s1, $s2 # f = g + h + + L1: + sub $s0, $s0, $s3 # f = f - i + + # Below is an example of finding the max of 3 numbers + # A direct translation in Java from MIPS logic: + # if (a > b) + # if (a > c) + # max = a; + # else + # max = c; + # else + # max = b; + # else + # max = c; + + # Let $s0 = a, $s1 = b, $s2 = c, $v0 = return register + ble $s0, $s1, a_LTE_b # if (a <= b) branch(a_LTE_b) + ble $s0, $s2, max_C # if (a > b && a <=c) branch(max_C) + move $v0, $s1 # else [a > b && a > c] max = a + j done # Jump to the end of the program + + a_LTE_b: # Label for when a <= b + ble $s1, $s2, max_C # if (a <= b && b <= c) branch(max_C) + move $v0, $s1 # if (a <= b && b > c) max = b + j done # Jump to done + + max_C: + move $v0, $s2 # max = c + + done: # End of program + +## LOOPS ## + _loops: + # The basic structure of loops is having an exit condition and a jump + instruction to continue its execution + li $t0, 0 + while: + bgt $t0, 10, end_while # While $t0 is less than 10, keep iterating + addi $t0, $t0, 1 # Increment the value + j while # Jump back to the beginning of the loop + end_while: + + # 2D Matrix Traversal + # Assume that $a0 stores the address of an integer matrix which is 3 x 3 + li $t0, 0 # Counter for i + li $t1, 0 # Counter for j + matrix_row: + bgt $t0, 3, matrix_row_end + + matrix_col: + bgt $t1, 3, matrix_col_end + + # Do stuff + + addi $t1, $t1, 1 # Increment the col counter + matrix_col_end: + + # Do stuff + + addi $t0, $t0, 1 + matrix_row_end: + +## FUNCTIONS ## + _functions: + # Functions are callable procedures that can accept arguments and return + values all denoted with labels, like above + + main: # Programs begin with main func + jal return_1 # jal will store the current PC in $ra + # and then jump to return_1 + + # What if we want to pass in args? + # First we must pass in our parameters to the argument registers + li $a0, 1 + li $a1, 2 + jal sum # Now we can call the function + + # How about recursion? + # This is a bit more work since we need to make sure we save and restore + # the previous PC in $ra since jal will automatically overwrite on each call + li $a0, 3 + jal fact + + li $v0, 10 + syscall + + # This function returns 1 + return_1: + li $v0, 1 # Load val in return register $v0 + jr $ra # Jump back to old PC to continue exec + + + # Function with 2 args + sum: + add $v0, $a0, $a1 + jr $ra # Return + + # Recursive function to find factorial + fact: + addi $sp, $sp, -8 # Allocate space in stack + sw $s0, ($sp) # Store reg that holds current num + sw $ra, 4($sp) # Store previous PC + + li $v0, 1 # Init return value + beq $a0, 0, fact_done # Finish if param is 0 + + # Otherwise, continue recursion + move $s0, $a0 # Copy $a0 to $s0 + sub $a0, $a0, 1 + jal fact + + mul $v0, $s0, $v0 # Multiplication is done + + fact_done: + lw $s0, ($sp) + lw $ra, ($sp) # Restore the PC + addi $sp, $sp, 8 + + jr $ra + +## MACROS ## + _macros: + # Macros are extremly useful for substituting repeated code blocks with a + # single label for better readability + # These are in no means substitutes for functions + # These must be declared before it is used + + # Macro for printing new lines (since these can be very repetitive) + .macro println() + la $a0, newline # New line string stored here + li $v0, 4 + syscall + .end_macro + + println() # Assembler will copy that block of + # code here before running + + # Parameters can be passed in through macros. + # These are denoted by a '%' sign with any name you choose + .macro print_int(%num) + li $v0, 1 + lw $a0, %num + syscall + .end_macro + + li $t0, 1 + print_int($t0) + + # We can also pass in immediates for macros + .macro immediates(%a, %b) + add $t0, %a, %b + .end_macro + + immediates(3, 5) + + # Along with passing in labels + .macro print(%string) + la $a0, %string + li $v0, 4 + syscall + .end_macro + + print(hello_world) + +## ARRAYS ## +.data + list: .word 3, 0, 1, 2, 6 # This is an array of words + char_arr: .asciiz "hello" # This is a char array + buffer: .space 128 # Allocates a block in memory, does + # not automatically clear + # These blocks of memory are aligned + # next each other + +.text + la $s0, list # Load address of list + li $t0, 0 # Counter + li $t1, 5 # Length of the list + + loop: + bgt $t0, $t1, end_loop + + lw $a0, ($s0) + li $v0, 1 + syscall # Print the number + + addi $s0, $s0, 4 # Size of a word is 4 bytes + addi $t0, $t0, 1 # Increment + j loop + end_loop: + +## INCLUDE ## +# You do this to import external files into your program (behind the scenes, +# it really just takes whatever code that is in that file and places it where +# the include statement is) +.include "somefile.asm" + +``` diff --git a/montilang.html.markdown b/montilang.html.markdown new file mode 100644 index 00000000..cceb7aa1 --- /dev/null +++ b/montilang.html.markdown @@ -0,0 +1,233 @@ +--- +language: "montilang" +filename: montilang.ml +contributors: + - ["Leo Whitehead", "https://github.com/lduck11007"] +--- + +MontiLang is a Stack-Oriented concatenative imperative programming language. Its syntax +is roughly based off of forth with similar style for doing arithmetic in [reverse polish notation.](https://en.wikipedia.org/wiki/Reverse_Polish_notation) + +A good way to start with MontiLang is to read the documentation and examples at [montilang.ml](http://montilang.ml), +then download MontiLang or build from source code with the instructions provided. + +``` +/# Monti Reference sheet #/ +/# +Comments are multiline +Nested comments are not supported +#/ +/# Whitespace is all arbitrary, indentation is optional #/ +/# All programming in Monti is done by manipulating the parameter stack +arithmetic and stack operations in MontiLang are similar to FORTH +https://en.wikipedia.org/wiki/Forth_(programming_language) +#/ + +/# in Monti, everything is either a string or a number. Operations treat all numbers +similarly to floats, but anything without a remainder is treated as type int #/ + +/# numbers and strings are added to the stack from left to right #/ + +/# Arithmetic works by manipulating data on the stack #/ + +5 3 + PRINT . /# 8 #/ + +/# 5 and 3 are pushed onto the stack + '+' replaces top 2 items on stack with sum of top 2 items + 'PRINT' prints out the top item on the stack + '.' pops the top item from the stack. + #/ + +6 7 * PRINT . /# 42 #/ +1360 23 - PRINT . /# 1337 #/ +12 12 / PRINT . /# 1 #/ +13 2 % PRINT . /# 1 #/ + +37 NEG PRINT . /# -37 #/ +-12 ABS PRINT . /# 12 #/ +52 23 MAX PRINT . /# 52 #/ +52 23 MIN PRINT . /# 23 #/ + +/# 'PSTACK' command prints the entire stack, 'CLEAR' clears the entire stack #/ + +3 6 8 PSTACK CLEAR /# [3, 6, 8] #/ + +/# Monti comes with some tools for stack manipulation #/ + +2 DUP PSTACK CLEAR /# [2, 2] - Duplicate the top item on the stack#/ +2 6 SWAP PSTACK CLEAR /# [6, 2] - Swap top 2 items on stack #/ +1 2 3 ROT PSTACK CLEAR /# [2, 3, 1] - Rotate top 3 items on stack #/ +2 3 NIP PSTACK CLEAR /# [3] - delete second item from the top of the stack #/ +4 5 6 TRIM PSTACK CLEAR /# [5, 6] - Deletes first item on stack #/ +/# variables are assigned with the syntax 'VAR [name]'#/ +/# When assigned, the variable will take the value of the top item of the stack #/ + +6 VAR six . /# assigns var 'six' to be equal to 6 #/ +3 6 + VAR a . /# assigns var 'a' to be equal to 9 #/ + +/# the length of the stack can be calculated with the statement 'STKLEN' #/ +1 2 3 4 STKLEN PRINT CLEAR /# 4 #/ + +/# strings are defined with | | #/ + +|Hello World!| VAR world . /# sets variable 'world' equal to string 'Hello world! #/ + +/# variables can be called by typing its name. when called, the value of the variable is pushed +to the top of the stack #/ +world PRINT . + +/# with the OUT statement, the top item on the stack can be printed without a newline #/ + +|world!| |Hello, | OUT SWAP PRINT CLEAR + +/# Data types can be converted between strings and integers with the commands 'TOINT' and 'TOSTR'#/ +|5| TOINT PSTACK . /# [5] #/ +45 TOSTR PSTACK . /# ['45'] #/ + +/# User input is taken with INPUT and pushed to the stack. If the top item of the stack is a string, +the string is used as an input prompt #/ + +|What is your name? | INPUT NIP +|Hello, | OUT SWAP PRINT CLEAR + + +/# FOR loops have the syntax 'FOR [condition] [commands] ENDFOR' At the moment, [condition] can +only have the value of an integer. Either by using an integer, or a variable call to an integer. +[commands] will be interpereted the amount of time specified in [condition] #/ +/# E.G: this prints out 1 to 10 #/ + +1 VAR a . +FOR 10 + a PRINT 1 + VAR a +ENDFOR + +/# the syntax for while loops are similar. A number is evaluated as true if it is larger than +0. a string is true if its length > 0. Infinite loops can be used by using literals. +#/ +10 var loop . +WHILE loop + loop print + 1 - var loop +ENDWHILE +/# +this loop would count down from 10. + +IF statements are pretty much the same, but only are executed once. +#/ +IF loop + loop PRINT . +ENDIF + +/# This would only print 'loop' if it is larger than 0 #/ + +/# If you would want to use the top item on the stack as loop parameters, this can be done with the ':' character #/ + +/# eg, if you wanted to print 'hello' 7 times, instead of using #/ + +FOR 7 + |hello| PRINT . +ENDFOR + +/# this could be used #/ +7 +FOR : + |hello| PRINT . +ENDFOR + +/# Equality and inequality statements use the top 2 items on the stack as parameters, and replace the top two items with the output #/ +/# If it is true, the top 2 items are replaced with '1'. If false, with '0'. #/ + +7 3 > PRINT . /# 1 #/ +2 10 > PRINT . /# 0 #/ +5 9 <= PRINT . /# 1 #/ +5 5 == PRINT . /# 1 #/ +5 7 == PRINT . /# 0 #/ +3 8 != PRINT . /# 1 #/ + +/# User defined commands have the syntax of 'DEF [name] [commands] ENDDEF'. #/ +/# eg, if you wanted to define a function with the name of 'printseven' to print '7' 10 times, this could be used #/ + +DEF printseven + FOR 10 + 7 PRINT . + ENDFOR +ENDDEF + +/# to run the defined statement, simply type it and it will be run by the interpereter #/ + +printseven + +/# Montilang supports AND, OR and NOT statements #/ + +1 0 AND PRINT . /# 0 #/ +1 1 AND PRINT . /# 1 #/ +1 0 OR PRINT . /# 1 #/ +0 0 OR PRINT . /# 0 #/ +1 NOT PRINT . /# 0 #/ +0 NOT PRINT . /# 1 #/ + +/# Preprocessor statements are made inbetween '&' characters #/ +/# currently, preprocessor statements can be used to make c++-style constants #/ + +&DEFINE LOOPSTR 20& +/# must have & on either side with no spaces, 'DEFINE' is case sensative. #/ +/# All statements are scanned and replaced before the program is run, regardless of where the statements are placed #/ + +FOR LOOPSTR 7 PRINT . ENDFOR /# Prints '7' 20 times. At run, 'LOOPSTR' in source code is replaced with '20' #/ + +/# Multiple files can be used with the &INCLUDE <filename>& Command that operates similar to c++, where the file specified is tokenized, + and the &INCLUDE statement is replaced with the file #/ + +/# E.G, you can have a program be run through several files. If you had the file 'name.mt' with the following data: + +[name.mt] +|Hello, | OUT . name PRINT . + +a program that asks for your name and then prints it out can be defined as such: #/ + +|What is your name? | INPUT VAR name . &INCLUDE name.mt& + +/# ARRAYS: #/ + +/# arrays are defined with the statement 'ARR' +When called, everything currently in the stack is put into one +array and all items on the stack are replaced with the new array. #/ + +2 3 4 ARR PSTACK . /# [[2, 3, 4]] #/ + +/# the statement 'LEN' adds the length of the last item on the stack to the stack. +This can be used on arrays, as well as strings. #/ + +3 4 5 ARR LEN PRINT . /# 3 #/ + +/# values can be appended to an array with the statement 'APPEND' #/ + +1 2 3 ARR 5 APPEND . PRINT . /# [1, 2, 3, 5] #/ + +/# an array at the top of the stack can be wiped with the statement 'WIPE' #/ +3 4 5 ARR WIPE PRINT . /# [] #/ + +/# The last item of an array can be removed with the statement 'DROP' #/ + +3 4 5 ARR DROP PRINT . /# [3, 4] +/# arrays, like other datatypes can be stored in variables #/ +5 6 7 ARR VAR list . +list PRINT . /# [5, 6, 7] #/ + +/# Values at specific indexes can be changed with the statement 'INSERT <index>' #/ +4 5 6 ARR +97 INSERT 1 . PRINT /# 4, 97, 6 #/ + +/# Values at specific indexes can be deleted with the statement 'DEL <index>' #/ +1 2 3 ARR +DEL 1 PRINT . /# [1, 3] #/ + +/# items at certain indexes of an array can be gotten with the statement 'GET <index>' #/ + +1 2 3 ARR GET 2 PSTACK /# [[1, 2, 3], 3] #/ +``` + +## Extra information + +- [MontiLang.ml](http://montilang.ml/) +- [Github Page](https://github.com/lduck11007/MontiLang) diff --git a/moonscript.html.markdown b/moonscript.html.markdown new file mode 100644 index 00000000..941578e7 --- /dev/null +++ b/moonscript.html.markdown @@ -0,0 +1,570 @@ +--- +language: moonscript +contributors: + - ["RyanSquared", "https://ryansquared.github.io/"] + - ["Job van der Zwan", "https://github.com/JobLeonard"] +filename: moonscript.moon +--- + +MoonScript is a dynamic scripting language that compiles into Lua. It gives +you the power of one of the fastest scripting languages combined with a +rich set of features. + +See [the MoonScript website](https://moonscript.org/) to see official guides on installation for all platforms. + +```moon +-- Two dashes start a comment. Comments can go until the end of the line. +-- MoonScript transpiled to Lua does not keep comments. + +-- As a note, MoonScript does not use 'do', 'then', or 'end' like Lua would and +-- instead uses an indented syntax, much like Python. + +-------------------------------------------------- +-- 1. Assignment +-------------------------------------------------- + +hello = "world" +a, b, c = 1, 2, 3 +hello = 123 -- Overwrites `hello` from above. + +x = 0 +x += 10 -- x = x + 10 + +s = "hello " +s ..= "world" -- s = s .. "world" + +b = false +b and= true or false -- b = b and (true or false) + +-------------------------------------------------- +-- 2. Literals and Operators +-------------------------------------------------- + +-- Literals work almost exactly as they would in Lua. Strings can be broken in +-- the middle of a line without requiring a \. + +some_string = "exa +mple" -- local some_string = "exa\nmple" + +-- Strings can also have interpolated values, or values that are evaluated and +-- then placed inside of a string. + +some_string = "This is an #{some_string}" -- Becomes 'This is an exa\nmple' + +-------------------------------------------------- +-- 2.1. Function Literals +-------------------------------------------------- + +-- Functions are written using arrows: + +my_function = -> -- compiles to `function() end` +my_function() -- calls an empty function + +-- Functions can be called without using parenthesis. Parentheses may still be +-- used to have priority over other functions. + +func_a = -> print "Hello World!" +func_b = -> + value = 100 + print "The value: #{value}" + +-- If a function needs no parameters, it can be called with either `()` or `!`. + +func_a! +func_b() + +-- Functions can use arguments by preceding the arrow with a list of argument +-- names bound by parentheses. + +sum = (x, y)-> x + y -- The last expression is returned from the function. +print sum(5, 10) + +-- Lua has an idiom of sending the first argument to a function as the object, +-- like a 'self' object. Using a fat arrow (=>) instead of a skinny arrow (->) +-- automatically creates a `self` variable. `@x` is a shorthand for `self.x`. + +func = (num)=> @value + num + +-- Default arguments can also be used with function literals: + +a_function = (name = "something", height=100)-> + print "Hello, I am #{name}.\nMy height is #{height}." + +-- Because default arguments are calculated in the body of the function when +-- transpiled to Lua, you can reference previous arguments. + +some_args = (x = 100, y = x + 1000)-> print(x + y) + +-------------------------------------------------- +-- Considerations +-------------------------------------------------- + +-- The minus sign plays two roles, a unary negation operator and a binary +-- subtraction operator. It is recommended to always use spaces between binary +-- operators to avoid the possible collision. + +a = x - 10 -- a = x - 10 +b = x-10 -- b = x - 10 +c = x -y -- c = x(-y) +d = x- z -- d = x - z + +-- When there is no space between a variable and string literal, the function +-- call takes priority over following expressions: + +x = func"hello" + 100 -- func("hello") + 100 +y = func "hello" + 100 -- func("hello" + 100) + +-- Arguments to a function can span across multiple lines as long as the +-- arguments are indented. The indentation can be nested as well. + +my_func 5, -- called as my_func(5, 8, another_func(6, 7, 9, 1, 2), 5, 4) + 8, another_func 6, 7, -- called as + 9, 1, 2, -- another_func(6, 7, 9, 1, 2) + 5, 4 + +-- If a function is used at the start of a block, the indentation can be +-- different than the level of indentation used in a block: + +if func 1, 2, 3, -- called as func(1, 2, 3, "hello", "world") + "hello", + "world" + print "hello" + +-------------------------------------------------- +-- 3. Tables +-------------------------------------------------- + +-- Tables are defined by curly braces, like Lua: + +some_values = {1, 2, 3, 4} + +-- Tables can use newlines instead of commas. + +some_other_values = { + 5, 6 + 7, 8 +} + +-- Assignment is done with `:` instead of `=`: + +profile = { + name: "Bill" + age: 200 + "favorite food": "rice" +} + +-- Curly braces can be left off for `key: value` tables. + +y = type: "dog", legs: 4, tails: 1 + +profile = + height: "4 feet", + shoe_size: 13, + favorite_foods: -- nested table + foo: "ice cream", + bar: "donuts" + +my_function dance: "Tango", partner: "none" -- :( forever alone + +-- Tables constructed from variables can use the same name as the variables +-- by using `:` as a prefix operator. + +hair = "golden" +height = 200 +person = {:hair, :height} + +-- Like in Lua, keys can be non-string or non-numeric values by using `[]`. + +t = + [1 + 2]: "hello" + "hello world": true -- Can use string literals without `[]`. + +-------------------------------------------------- +-- 3.1. Table Comprehensions +-------------------------------------------------- + +-- List Comprehensions + +-- Creates a copy of a list but with all items doubled. Using a star before a +-- variable name or table can be used to iterate through the table's values. + +items = {1, 2, 3, 4} +doubled = [item * 2 for item in *items] +-- Uses `when` to determine if a value should be included. + +slice = [item for item in *items when i > 1 and i < 3] + +-- `for` clauses inside of list comprehensions can be chained. + +x_coords = {4, 5, 6, 7} +y_coords = {9, 2, 3} + +points = [{x,y} for x in *x_coords for y in *y_coords] + +-- Numeric for loops can also be used in comprehensions: + +evens = [i for i=1, 100 when i % 2 == 0] + +-- Table Comprehensions are very similar but use `{` and `}` and take two +-- values for each iteration. + +thing = color: "red", name: "thing", width: 123 +thing_copy = {k, v for k, v in pairs thing} + +-- Tables can be "flattened" from key-value pairs in an array by using `unpack` +-- to return both values, using the first as the key and the second as the +-- value. + +tuples = {{"hello", "world"}, {"foo", "bar"}} +table = {unpack tuple for tuple in *tuples} + +-- Slicing can be done to iterate over only a certain section of an array. It +-- uses the `*` notation for iterating but appends `[start, end, step]`. + +-- The next example also shows that this syntax can be used in a `for` loop as +-- well as any comprehensions. + +for item in *points[1, 10, 2] + print unpack item + +-- Any undesired values can be left off. The second comma is not required if +-- the step is not included. + +words = {"these", "are", "some", "words"} +for word in *words[,3] + print word + +-------------------------------------------------- +-- 4. Control Structures +-------------------------------------------------- + +have_coins = false +if have_coins + print "Got coins" +else + print "No coins" + +-- Use `then` for single-line `if` +if have_coins then "Got coins" else "No coins" + +-- `unless` is the opposite of `if` +unless os.date("%A") == "Monday" + print "It is not Monday!" + +-- `if` and `unless` can be used as expressions +is_tall = (name)-> if name == "Rob" then true else false +message = "I am #{if is_tall "Rob" then "very tall" else "not so tall"}" +print message -- "I am very tall" + +-- `if`, `elseif`, and `unless` can evaluate assignment as well as expressions. +if x = possibly_nil! -- sets `x` to `possibly_nil()` and evaluates `x` + print x + +-- Conditionals can be used after a statement as well as before. This is +-- called a "line decorator". + +is_monday = os.date("%A") == "Monday" +print("It IS Monday!") if isMonday +print("It is not Monday..") unless isMonday +--print("It IS Monday!" if isMonday) -- Not a statement, does not work + +-------------------------------------------------- +-- 4.1 Loops +-------------------------------------------------- + +for i = 1, 10 + print i + +for i = 10, 1, -1 do print i -- Use `do` for single-line loops. + +i = 0 +while i < 10 + continue if i % 2 == 0 -- Continue statement; skip the rest of the loop. + print i + +-- Loops can be used as a line decorator, just like conditionals +print "item: #{item}" for item in *items + +-- Using loops as an expression generates an array table. The last statement +-- in the block is coerced into an expression and added to the table. +my_numbers = for i = 1, 6 do i -- {1, 2, 3, 4, 5, 6} + +-- use `continue` to filter out values +odds = for i in *my_numbers + continue if i % 2 == 0 -- acts opposite to `when` in comprehensions! + i -- Only added to return table if odd + +-- A `for` loop returns `nil` when it is the last statement of a function +-- Use an explicit `return` to generate a table. +print_squared = (t) -> for x in *t do x*x -- returns `nil` +squared = (t) -> return for x in *t do x*x -- returns new table of squares + +-- The following does the same as `(t) -> [i for i in *t when i % 2 == 0]` +-- But list comprehension generates better code and is more readable! + +filter_odds = (t) -> + return for x in *t + if x % 2 == 0 then x else continue +evens = filter_odds(my_numbers) -- {2, 4, 6} + +-------------------------------------------------- +-- 4.2 Switch Statements +-------------------------------------------------- + +-- Switch statements are a shorthand way of writing multiple `if` statements +-- checking against the same value. The value is only evaluated once. + +name = "Dan" + +switch name + when "Dave" + print "You are Dave." + when "Dan" + print "You are not Dave, but Dan." + else + print "You are neither Dave nor Dan." + +-- Switches can also be used as expressions, as well as compare multiple +-- values. The values can be on the same line as the `when` clause if they +-- are only one expression. + +b = 4 +next_even = switch b + when 1 then 2 + when 2, 3 then 4 + when 4, 5 then 6 + else error "I can't count that high! D:" + +-------------------------------------------------- +-- 5. Object Oriented Programming +-------------------------------------------------- + +-- Classes are created using the `class` keyword followed by an identifier, +-- typically written using CamelCase. Values specific to a class can use @ as +-- the identifier instead of `self.value`. + +class Inventory + new: => @items = {} + add_item: (name)=> -- note the use of fat arrow for classes! + @items[name] = 0 unless @items[name] + @items[name] += 1 + +-- The `new` function inside of a class is special because it is called when +-- an instance of the class is created. + +-- Creating an instance of the class is as simple as calling the class as a +-- function. Calling functions inside of the class uses \ to separate the +-- instance from the function it is calling. + +inv = Inventory! +inv\add_item "t-shirt" +inv\add_item "pants" + +-- Values defined in the class - not the new() function - will be shared across +-- all instances of the class. + +class Person + clothes: {} + give_item: (name)=> + table.insert @clothes name + +a = Person! +b = Person! + +a\give_item "pants" +b\give_item "shirt" + +-- prints out both "pants" and "shirt" + +print item for item in *a.clothes + +-- Class instances have a value `.__class` that are equal to the class object +-- that created the instance. + +assert(b.__class == Person) + +-- Variables declared in class body the using the `=` operator are locals, +-- so these "private" variables are only accessible within the current scope. + +class SomeClass + x = 0 + reveal: -> + x += 1 + print x + +a = SomeClass! +b = SomeClass! +print a.x -- nil +a.reveal! -- 1 +b.reveal! -- 2 + +-------------------------------------------------- +-- 5.1 Inheritance +-------------------------------------------------- + +-- The `extends` keyword can be used to inherit properties and methods from +-- another class. + +class Backpack extends Inventory + size: 10 + add_item: (name)=> + error "backpack is full" if #@items > @size + super name -- calls Inventory.add_item with `name`. + +-- Because a `new` method was not added, the `new` method from `Inventory` will +-- be used instead. If we did want to use a constructor while still using the +-- constructor from `Inventory`, we could use the magical `super` function +-- during `new()`. + +-- When a class extends another, it calls the method `__inherited` on the +-- parent class (if it exists). It is always called with the parent and the +-- child object. + +class ParentClass + @__inherited: (child)=> + print "#{@__name} was inherited by #{child.__name}" + a_method: (a, b) => print a .. ' ' .. b + +-- Will print 'ParentClass was inherited by MyClass' + +class MyClass extends ParentClass + a_method: => + super "hello world", "from MyClass!" + assert super == ParentClass + +-------------------------------------------------- +-- 6. Scope +-------------------------------------------------- + +-- All values are local by default. The `export` keyword can be used to +-- declare the variable as a global value. + +export var_1, var_2 +var_1, var_3 = "hello", "world" -- var_3 is local, var_1 is not. + +export this_is_global_assignment = "Hi!" + +-- Classes can also be prefixed with `export` to make them global classes. +-- Alternatively, all CamelCase variables can be exported automatically using +-- `export ^`, and all values can be exported using `export *`. + +-- `do` lets you manually create a scope, for when you need local variables. + +do + x = 5 +print x -- nil + +-- Here we use `do` as an expression to create a closure. + +counter = do + i = 0 + -> + i += 1 + return i + +print counter! -- 1 +print counter! -- 2 + +-- The `local` keyword can be used to define variables +-- before they are assigned. + +local var_4 +if something + var_4 = 1 +print var_4 -- works because `var_4` was set in this scope, not the `if` scope. + +-- The `local` keyword can also be used to shadow an existing variable. + +x = 10 +if false + local x + x = 12 +print x -- 10 + +-- Use `local *` to forward-declare all variables. +-- Alternatively, use `local ^` to forward-declare all CamelCase values. + +local * + +first = -> + second! + +second = -> + print data + +data = {} + +-------------------------------------------------- +-- 6.1 Import +-------------------------------------------------- + +-- Values from a table can be brought to the current scope using the `import` +-- and `from` keyword. Names in the `import` list can be preceded by `\` if +-- they are a module function. + +import insert from table -- local insert = table.insert +import \add from state: 100, add: (value)=> @state + value +print add 22 + +-- Like tables, commas can be excluded from `import` lists to allow for longer +-- lists of imported items. + +import + asdf, gh, jkl + antidisestablishmentarianism + from {} + +-------------------------------------------------- +-- 6.2 With +-------------------------------------------------- + +-- The `with` statement can be used to quickly call and assign values in an +-- instance of a class or object. + +file = with File "lmsi15m.moon" -- `file` is the value of `set_encoding()`. + \set_encoding "utf8" + +create_person = (name, relatives)-> + with Person! + .name = name + \add_relative relative for relative in *relatives +me = create_person "Ryan", {"sister", "sister", "brother", "dad", "mother"} + +with str = "Hello" -- assignment as expression! :D + print "original: #{str}" + print "upper: #{\upper!}" + +-------------------------------------------------- +-- 6.3 Destructuring +-------------------------------------------------- + +-- Destructuring can take arrays, tables, and nested tables and convert them +-- into local variables. + +obj2 = + numbers: {1, 2, 3, 4} + properties: + color: "green" + height: 13.5 + +{numbers: {first, second}, properties: {:color}} = obj2 + +print first, second, color -- 1 2 green + +-- `first` and `second` return [1] and [2] because they are as an array, but +-- `:color` is like `color: color` so it sets itself to the `color` value. + +-- Destructuring can be used in place of `import`. + +{:max, :min, random: rand} = math -- rename math.random to rand + +-- Destructuring can be done anywhere assignment can be done. + +for {left, right} in *{{"hello", "world"}, {"egg", "head"}} + print left, right +``` + +## Additional Resources + +- [Language Guide](https://moonscript.org/reference/) +- [Online Compiler](https://moonscript.org/compiler/) diff --git a/nl-nl/markdown-nl.html.markdown b/nl-nl/markdown-nl.html.markdown index 35cc67c5..b5b4681c 100644 --- a/nl-nl/markdown-nl.html.markdown +++ b/nl-nl/markdown-nl.html.markdown @@ -12,7 +12,7 @@ Markdown is gecreëerd door John Gruber in 2004. Het is bedoeld om met een gemak schrijven syntax te zijn die gemakkelijk omgevormd kan worden naar HTML (en op heden verschillende andere formaten) -```markdown +```md <!-- Markdown erft over van HTML, dus ieder HTML bestand is een geldig Markdown bestand. Dit betekend ook dat html elementen gebruikt kunnen worden in Markdown zoals het commentaar element. Echter, als je een html element maakt in een Markdown diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 04c4e529..de3884af 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -731,7 +731,10 @@ if ([myClass conformsToProtocol:@protocol(CarUtilities)]) { /////////////////////////////////////// // Blocks are statements of code, just like a function, that are able to be used as data. // Below is a simple block with an integer argument that returns the argument plus 4. -int (^addUp)(int n); // Declare a variable to store the block. +^(int n) { + return n + 4; +} +int (^addUp)(int n); // Declare a variable to store a block. void (^noParameterBlockVar)(void); // Example variable declaration of block with no arguments. // Blocks have access to variables in the same scope. But the variables are readonly and the // value passed to the block is the value of the variable when the block is created. diff --git a/processing.html.markdown b/processing.html.markdown new file mode 100644 index 00000000..fc5dc997 --- /dev/null +++ b/processing.html.markdown @@ -0,0 +1,421 @@ +--- +language: processing +filename: learnprocessing.pde +contributors: + - ["Phone Thant Ko", "http://github.com/phonethantko"] +--- +## Introduction + +Processing is a programming language for creation of digital arts and multimedia content, allowing non-programmers to +learn fundamentals of computer programming in a visual context. +While the language is based on Java language, +its syntax has been largely influenced by both Java and Javascript syntaxes. [See more here](https://processing.org/reference/) +The language is statically typed, and also comes with its official IDE to compile and run the scripts. + +```processing +/* --------- + Comments + --------- +*/ + +// Single-line comment starts with // + +/* + Since Processing is based on Java, + the syntax for its comments are the same as Java (as you may have noticed above)! + Multi-line comments are wrapped as seen here. +*/ + +/* --------------------------------------- + Writing and Running Processing Programs + --------------------------------------- + */ + +// In Processing, your program's entry point is a function named setup() with a void return type. +// Note! The syntax looks strikingly similar to that of C++. +void setup() { + // This prints out the classic output "Hello World!" to the console when run. + println("Hello World!"); // Another language with a semi-column trap, ain't it? +} + +// Normally, we put all the static codes inside the setup() method as the name suggest since it only runs once. +// It can range from setting the background colours, setting the canvas size. +background(color); // setting the background colour +size(width,height,[renderer]); // setting the canvas size with optional parameter defining renderer +// You will see more of them throughout this document. + +// If you want to run the codes indefinitely, it has to be placed in draw() method. +// draw() must exist if you want the code to run continuously and obviously, there can only be one draw() method. +int i = 0; +void draw() { + // This block of code loops forever until stopped + print(i); + i++; // Increment Operator! +} + +// Now that we know how to write the working script and how to run it, +// we will proceed to explore what data types and collections are supported in Processing. + +/* ------------------------ + Datatypes & collections + ------------------------ +*/ + +// According to Processing References, Processing supports 8 primitive datatypes as follows. + +boolean booleanValue = true; // Boolean +byte byteValueOfA = 23; // Byte +char charValueOfA = 'A'; // Char +color colourValueOfWhiteM = color(255, 255, 255); // Colour (Specified using color() method) +color colourValueOfWhiteH = #FFFFFF; // Colour (Specified using hash value) +int intValue = 5; // Integer (Number without decimals) +long longValue = 2147483648L; // "L" is added to the number to mark it as a long +float floatValue = 1.12345; // Float (32-bit floating-point numbers) +double doubleValue = 1.12345D; // Double (64-bit floating-point numbers) + +// NOTE! +// Although datatypes "long" and "double" work in the language, +// processing functions do not use these datatypes, therefore +// they need to be converted into "int" and "float" datatypes respectively, +// using (int) and (float) syntax before passing into a function. + +// There is a whole bunch of default composite datatypes available for use in Processing. +// Primarily, I will brief through the most commonly used ones to save time. + +// String +// While char datatype uses '', String datatype uses "" - double quotes. +String sampleString = "Hello, Processing!"; +// String can be constructed from an array of char datatypes as well. We will discuss array very soon. +char source = {'H', 'E', 'L', 'L', 'O'}; +String stringFromSource = new String(source); // HELLO +// As in Java, strings can be concatenated using the "+" operator. +print("Hello " + "World!"); // Hello World! + +// Array +// Arrays in Processing can hold any datatypes including Objects themselves. +// Since arrays are similar to objects, they must be created with the keyword "new". +int[] intArray = new int[5]; +int[] intArrayWithValues = {1, 2, 3}; // You can also populate with data. + +// ArrayList +// Functions are similar to those of array; arraylists can hold any datatypes. +// The only difference is arraylists resize dynamically, +// as it is a form of resizable-array implementation of the Java "List" interface. +ArrayList<Integer> intArrayList = new ArrayList<Integer>(); + +// Object +// Since it is based on Java, Processing supports object-oriented programming. +// That means you can basically define any datatypes of your own and manipulate them to your needs. +// Of course, a class has to be defined before for the object you want. +// Format --> ClassName InstanceName +SomeRandomClass myObject // then instantiate later +//or +SomeRandomClass myObjectInstantiated = new SomeRandomClass(); + +// Processing comes up with more collections (eg. - Dictionaries and Lists) by default, +// for the simplicity sake, I will leave them out of discussion here. + +/* ------------ + Maths + ------------ +*/ + +// Arithmetic +1 + 1 // 2 +2 - 1 // 0 +2 * 3 // 6 +3 / 2 // 1 +3.0 / 2 // 1.5 +3.0 % 2 // 1.0 + +// Processing also comes with a set of functions that simplify mathematical operations. +float f = sq(3); // f = 9.0 +float p = pow(3, 3); // p = 27.0 +int a = abs(-13) // a = 13 +int r1 = round(3.1); // r1 = 3 +int r2 = round(3.7); // r2 = 4 +float sr = sqrt(25); // sr = 5.0 + +// Vectors +// Processing provides an easy way to implement vectors in its environment using PVector class. +// It can describe a two or three dimensional vector and +// comes with a set of methods which are useful for matrices operations. +// You can find more information on PVector class and its functions here. +// (https://processing.org/reference/PVector.html) + +// Trigonometry +// Processing also supports trigonometric operations by supplying a set of functions. +// sin(), cos(), tan(), asin(), acos(), atan() and also degrees() and radians() for convenient conversion. +// However, those functions take angle in radians as the parameter so it has to be converted beforehand. +float one = sin(PI/2); // one = 1.0 +// As you may have noticed, there exists a set of constants for trigonometric uses; +// PI, HALF_PI, QUARTER_PI and so on... + +/* ------------- + Control Flow + ------------- +*/ + +// Conditional Statements +// If Statements - The same syntax as if statements in Java. +if (author.getAppearance().equals("hot")) { + print("Narcissism at its best!"); +} else { + // You can check for other conditions here. + print("Something is really wrong here!"); +} +// A shortcut for if-else statements can also be used. +int i = 3; +String value = (i > 5) ? "Big" : "Small"; // "Small" + +// Switch-case structure can be used to check multiple conditions more concisely. +int value = 2; +switch(value) { + case 0: + print("Nought!"); // This doesn't get executed. + break; // Jumps to the next statement + case 1: + print("Getting there..."); // This again doesn't get executed. + break; + case 2: + print("Bravo!"); // This line gets executed. + break; + default: + print("Not found!"); // This line gets executed if our value was some other value. + break; +} + +// Iterative statements +// For Statements - Again, the same syntax as in Java +for(int i = 0; i < 5; i ++){ + print(i); // prints from 0 to 4 +} + +// While Statements - Again, nothing new if you are familiar with Java syntax. +int j = 3; +while(j > 0) { + print(j); + j--; // This is important to prevent from the code running indefinitely. +} + +// loop()| noLoop() | redraw() | exit() +// These are more of Processing-specific functions to configure program flow. +loop(); // allows the draw() method to run forever while +noLoop(); // only allows it to run once. +redraw(); // runs the draw() method once more. +exit(); // This stops the program. It is useful for programs with draw() running continuously. +``` +## Drawing with Processing +Since you will have understood the basics of the language by now, we will now look into the best part of Processing; DRAWING. + +```processing + +/* ------ + Shapes + ------ +*/ + +// 2D Shapes + +// Point +point(x, y); // In 2D space +point(x, y, z); // In 3D space +// Draws a point in the coordinate space. + +// Line +line(x1, y1, x2, y2); // In 2D space +line(x1, y1, z1, x2, y2, z2); // In 3D space +// Draws a line connecting two points defined by (x1, y1) and (x2, y2). + +// Triangle +triangle(x1, y1, x2, y2, x3, y3); +// Draws a triangle connecting three points defined by coordinate paramters. + +// Rectangle +rect(a, b, c, d, [r]); // With optional parameter defining the radius of all corners +rect(a, b, c, d, [tl, tr, br, bl]); // With optional set of parameters defining radius of each corner +// Draws a rectangle with {a, b} as a top left coordinate and c and d as width and height respectively. + +// Quad +quad(x, y, x2, y2, x3, y3, x4, y4); +// Draws a quadrilateral with parameters defining coordinates of each corner point. + +// Ellipse +ellipse(x, y, width, height); +// Draws an eclipse at point {x, y} with width and height specified. + +// Arc +arc(x, y, width, height, start, stop, [mode]); +// While the first four parameters are self-explanatory, +// start and end defined the angles the arc starts and ends (in radians). +// Optional parameter [mode] defines the filling; +// PIE gives pie-like outline, CHORD gives the chord-like outline and OPEN is CHORD without strokes + +// Curves +// Processing provides two implementation of curves; using curve() and bezier(). +// Since I plan to keep this simple I won't be discussing any further details. +// However, if you want to implement it in your sketch, here are the references: +// (https://processing.org/reference/curve_.html)(https://processing.org/reference/bezier_.html) + +// 3D Shapes + +// 3D space can be configured by setting "P3D" to the renderer parameter in size() method. +size(width, height, P3D); +// In 3D space, you will have to translate to the particular coordinate to render the 3D shapes. + +// Box +box(size); // Cube with same length defined by size +box(w, h, d); // Box with width, height and depth separately defined + +// Sphere +sphere(radius); // Its size is defined using the radius parameter +// Mechanism behind rendering spheres is implemented by tessellating triangles. +// That said, how much detail being rendered is controlled by function sphereDetail(res) +// More information here: (https://processing.org/reference/sphereDetail_.html) + +// Irregular Shapes +// What if you wanted to draw something that's not made available by Processing's functions? +// You can use beginShape(), endShape(), vertex(x,y) to define shapes by specifying each point. +// More information here: (https://processing.org/reference/beginShape_.html) +// You can also use custom made shapes using PShape class.(https://processing.org/reference/PShape.html) + +/* --------------- + Transformations + --------------- +*/ + +// Transformations are particularly useful to keep track of the coordinate space +// and the vertices of the shapes you have drawn. +// Particularly, matrix stack methods; pushMatrix(), popMatrix() and translate(x,y) +pushMatrix(); // Saves the current coordinate system to the stack +// ... apply all the transformations here ... +popMatrix(); // Restores the saved coordinate system +// Using them, the coordinate system can be preserved and visualized without causing any conflicts. + +// Translate +translate(x, y); // Translates to point{x, y} i.e. - setting origin to that point +translate(x, y, z); // 3D counterpart of the function + +// Rotate +rotate(angle); // Rotate the amount specified by the angle parameter +// It has 3 3D counterparts to perform rotation, each for every dimension, +// namely: rotateX(angle), rotateY(angle), rotateZ(angle) + +// Scale +scale(s); // Scale the coordinate system by either expanding or contracting it. + +/* -------------------- + Styling and Textures + -------------------- +*/ + +// Colours +// As I have discussed earlier, the background colour can be configured using background() function. +// You can define a color object beforehand and then pass it to the function as an argument. +color c = color(255, 255, 255); // WHITE! +// By default, Processing uses RGB colour scheme but it can be configured to HSB using colorMode(). +// Read here: (https://processing.org/reference/colorMode_.html) +background(color); // By now, the background colour should be white. +// You can use fill() function to select the colour for filling the shapes. +// It has to be configured before you start drawing shapes so the colours gets applied. +fill(color(0, 0, 0)); +// If you just want to colour the outlines of the shapes then you can use stroke() function. +stroke(255, 255, 255, 200); // stroke colour set to yellow with transparency set to a lower value. + +// Images +// Processing can render images and use them in several ways. Mostly stored as PImage datatype. +filter(shader); // Processing supports several filter functions for image manipulation. +texture(image); // PImage can be passed into arguments for texture-mapping the shapes. + +``` +If you want to take things further, there are more things Processing is powered for. Rendering models, shaders and whatnot. +There's too much to cover in a short documentation, so I will leave them out here. Shoud you be interested, please check out the references. +```processing +// Before we move on, I will touch a little bit more on how to import libraries +// so you can extend Processing's functionality to another horizon. + +/* ------- + Imports + ------- +*/ + +// The power of Processing can be further visualized when we import libraries and packages into our sketches. +// Import statement can be written as below at the top of the source code. +import processing.something.*; + +``` +## DTC? + +Down To Code? Let's get our hands dirty! + +Let us see an example from openprocessing to visualize how much Processing is capable of within few lines of code. +Copy the code below into your Processing IDE and see the magic. + +```processing + +// Disclaimer: I did not write this program since I currently am occupied with internship and +// this sketch is adapted from openprocessing since it shows something cool with simple codes. +// Retrieved from: (https://www.openprocessing.org/sketch/559769) + +float theta; +float a; +float col; +float num; + +void setup() { + size(600,600); +} + +void draw() { + background(#F2F2F2); + translate(width/2, height/2); + theta = map(sin(millis()/1000.0), -1, 1, 0, PI/6); + + float num=6; + for (int i=0; i<num; i++) { + a =350; + rotate(TWO_PI/num); + branch(a); + } + +} + + + +void branch(float len) { + col=map(len, 0, 90, 150, 255); + fill(col, 0, 74); + stroke (col, 0, 74); + line(0, 0, 0, -len); + ellipse(0, -len, 3, 3); + len *= 0.7; + + + if (len>30) { + pushMatrix(); + translate(0, -30); + rotate(theta); + branch(len); + popMatrix(); + + pushMatrix(); + translate(0, -30); + rotate(-theta); + branch(len); + popMatrix(); + + } +} + +``` + +Processing is easy to learn and is particularly useful to create multimedia contents (even in 3D) without +having to type a lot of codes. It is so simple that you can read through the code and get a rough idea of +the program flow. +However, that does not apply when you introduce external libraries, packages and even your own classes. +(Trust me! Processing projects can get real humongous...) + +## Some useful resources: + + - [Processing Website](http://processing.org) + - [Processing Sketches](http://openprocessing.org) diff --git a/pt-br/awk-pt.html.markdown b/pt-br/awk-pt.html.markdown index 75b73abe..761f5294 100644 --- a/pt-br/awk-pt.html.markdown +++ b/pt-br/awk-pt.html.markdown @@ -171,7 +171,7 @@ function arithmetic_functions(a, b, c, d) { # Muitas implementações AWK possuem algumas funções trigonométricas padrão localvar = sin(a) localvar = cos(a) - localvar = atan2(a, b) # arco-tangente de b / a + localvar = atan2(b, a) # arco-tangente de b / a # E conteúdo logarítmico localvar = exp(a) diff --git a/pt-br/c-pt.html.markdown b/pt-br/c-pt.html.markdown index 0dca7ab0..d594b7b9 100644 --- a/pt-br/c-pt.html.markdown +++ b/pt-br/c-pt.html.markdown @@ -638,16 +638,17 @@ typedef void (*minha_função_type)(char *); ## Leitura adicional É recomendado ter uma cópia de [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language). -Este é *o* livro sobre C, escrito pelos criadores da linguage. Mas cuidado - ele é antigo e contém alguns erros (bem, -ideias que não são consideradas boas hoje) ou práticas mudadas. +Este é *o* livro sobre C, escrito pelos criadores da linguagem. Mas cuidado - ele é antigo e contém alguns erros (bem, +ideias que não são mais consideradas boas) ou práticas ultrapassadas. Outra boa referência é [Learn C the hard way](http://c.learncodethehardway.org/book/). Se você tem uma pergunta, leia [compl.lang.c Frequently Asked Questions](http://c-faq.com). É importante usar espaços e indentação adequadamente e ser consistente com seu estilo de código em geral. -Código legível é melhor que código 'esperto' e rápido. Para adotar um estilo de código bom e são, veja +Código legível é melhor que código 'esperto' e rápido. Para adotar um estilo de código bom e sensato, veja [Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle). Além disso, Google é teu amigo. + [1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member diff --git a/pt-br/common-lisp-pt.html.markdown b/pt-br/common-lisp-pt.html.markdown index c3381824..c22cfd8e 100644 --- a/pt-br/common-lisp-pt.html.markdown +++ b/pt-br/common-lisp-pt.html.markdown @@ -19,7 +19,7 @@ Outro livro recente e popular é o [Land of Lisp](http://landoflisp.com/). -```common-lisp +```lisp ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 0. Sintaxe diff --git a/pt-br/markdown-pt.html.markdown b/pt-br/markdown-pt.html.markdown index f22093f9..c2aa515d 100644 --- a/pt-br/markdown-pt.html.markdown +++ b/pt-br/markdown-pt.html.markdown @@ -14,7 +14,7 @@ escrever sintaxe que converte facilmente em HTML (hoje, suporta outros formatos Dê-me feedback tanto quanto você quiser! / Sinta-se livre para a garfar (fork) e puxar o projeto (pull request) -```markdown +```md <!-- Markdown é um superconjunto do HTML, de modo que qualquer arvquivo HTML é um arquivo Markdown válido, isso significa que nós podemos usar elementos HTML em Markdown, como o elemento de comentário, e eles não serão afetados pelo analisador diff --git a/pt-br/matlab-pt.html.markdown b/pt-br/matlab-pt.html.markdown index eb660d4c..5ed6b7ba 100644 --- a/pt-br/matlab-pt.html.markdown +++ b/pt-br/matlab-pt.html.markdown @@ -206,8 +206,7 @@ size(A) % Resposta = 3 3 A(1, :) =[] % Remove a primeira linha da matriz A(:, 1) =[] % Remove a primeira coluna da matriz -transpose(A) % Transposta a matriz, que é o mesmo de: -A one +transpose(A) % Transposta a matriz, que é o mesmo de: A.' ctranspose(A) % Transposta a matriz % (a transposta, seguida pelo conjugado complexo de cada elemento) diff --git a/pt-br/solidity-pt.html.markdown b/pt-br/solidity-pt.html.markdown index 37d15bf2..d4555fa7 100644 --- a/pt-br/solidity-pt.html.markdown +++ b/pt-br/solidity-pt.html.markdown @@ -1,6 +1,6 @@ --- language: Solidity -filename: learnSolidity.sol +filename: learnSolidity-br.sol contributors: - ["Nemil Dalal", "https://www.nemil.com"] - ["Joseph Chow", ""] diff --git a/pt-br/visualbasic-pt.html.markdown b/pt-br/visualbasic-pt.html.markdown index b94ab609..2a7205cd 100644 --- a/pt-br/visualbasic-pt.html.markdown +++ b/pt-br/visualbasic-pt.html.markdown @@ -8,7 +8,7 @@ lang: pt-br filename: learnvisualbasic-pt.vb --- -```vb +``` Module Module1 module Module1 diff --git a/python3.html.markdown b/python3.html.markdown index 019934cb..b378a8c6 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Zachary Ferguson", "http://github.com/zfergus2"] - ["evuez", "http://github.com/evuez"] - ["Rommel Martinez", "https://ebzzry.io"] + - ["Roberto Fernandez Diaz", "https://github.com/robertofd1995"] filename: learnpython3.py --- @@ -138,6 +139,10 @@ len("This is a string") # => 16 # still use the old style of formatting: "%s can be %s the %s way" % ("Strings", "interpolated", "old") # => "Strings can be interpolated the old way" +# You can also format using f-strings or formatted string literals +name = "Reiko" +f"She said her name is {name}." # => "She said her name is Reiko" + # None is an object None # => None @@ -348,6 +353,8 @@ valid_set = {(1,), 1} # Add one more item to the set filled_set = some_set filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} +# Sets do not have duplicate elements +filled_set.add(5) # it remains as before {1, 2, 3, 4, 5} # Do set intersection with & other_set = {3, 4, 5, 6} diff --git a/pythonstatcomp.html.markdown b/pythonstatcomp.html.markdown index 6dde1cf0..2440d859 100644 --- a/pythonstatcomp.html.markdown +++ b/pythonstatcomp.html.markdown @@ -38,18 +38,16 @@ r.text # raw page source print(r.text) # prettily formatted # save the page source in a file: os.getcwd() # check what's the working directory -f = open("learnxinyminutes.html", "wb") -f.write(r.text.encode("UTF-8")) -f.close() +with open("learnxinyminutes.html", "wb") as f: + f.write(r.text.encode("UTF-8")) # downloading a csv fp = "https://raw.githubusercontent.com/adambard/learnxinyminutes-docs/master/" fn = "pets.csv" r = requests.get(fp + fn) print(r.text) -f = open(fn, "wb") -f.write(r.text.encode("UTF-8")) -f.close() +with open(fn, "wb") as f: + f.write(r.text.encode("UTF-8")) """ for more on the requests module, including APIs, see http://docs.python-requests.org/en/latest/user/quickstart/ @@ -71,8 +69,8 @@ pets # 1 vesuvius 6 23 fish # 2 rex 5 34 dog -""" R users: note that Python, like most normal programming languages, starts - indexing from 0. R is the unusual one for starting from 1. +""" R users: note that Python, like most C-influenced programming languages, starts + indexing from 0. R starts indexing at 1 due to Fortran influence. """ # two different ways to print out a column @@ -205,7 +203,7 @@ hre["DeathY"] = extractYear(hre.Death) hre["EstAge"] = hre.DeathY.astype(int) - hre.BirthY.astype(int) # simple scatterplot, no trend line, color represents dynasty -sns.lmplot("BirthY", "EstAge", data=hre, hue="Dynasty", fit_reg=False); +sns.lmplot("BirthY", "EstAge", data=hre, hue="Dynasty", fit_reg=False) # use scipy to run a linear regression from scipy import stats @@ -222,7 +220,7 @@ rval**2 # 0.020363950027333586 pval # 0.34971812581498452 # use seaborn to make a scatterplot and plot the linear regression trend line -sns.lmplot("BirthY", "EstAge", data=hre); +sns.lmplot("BirthY", "EstAge", data=hre) """ For more information on seaborn, see - http://web.stanford.edu/~mwaskom/software/seaborn/ diff --git a/ro-ro/elixir-ro.html.markdown b/ro-ro/elixir-ro.html.markdown new file mode 100644 index 00000000..10fec3c5 --- /dev/null +++ b/ro-ro/elixir-ro.html.markdown @@ -0,0 +1,459 @@ +--- +language: elixir +contributors: + - ["Joao Marques", "http://github.com/mrshankly"] + - ["Dzianis Dashkevich", "https://github.com/dskecse"] + - ["Ryan Plant", "https://github.com/ryanplant-au"] + - ["Ev Bogdanov", "https://github.com/evbogdanov"] +translators: + - ["Vitalie Lazu", "https://github.com/vitaliel"] +lang: ro-ro +filename: learnelixir-ro.ex +--- + +Elixir este un limbaj funcțional modern construit pe baza mașinii virtuale Erlang. +E total compatibil cu Erlang, dar are o sintaxă mai prietenoasă și propune mai multe +posibilități. + +```elixir + +# Comentariile de o linie încep cu simbolul diez. + +# Pentru comentarii pe mai multe linii nu există sintaxă separată, +# de aceea folosiți mai multe linii cu comentarii. + +# Pentru a folosi shell-ul elixir utilizați comanda `iex`. +# Compilați modulele cu comanda `elixirc`. + +# Ambele comenzi vor lucra în terminal, dacă ați instalat Elixir corect. + +## --------------------------- +## -- Tipuri de bază +## --------------------------- + +# Numere +3 # număr întreg +0x1F # număr întreg +3.0 # număr cu virgulă mobilă + +# Atomii, sunt constante nenumerice. Ei încep cu `:`. +:salut # atom + +# Tuplele sunt păstrate în memorie consecutiv. +{1,2,3} # tuple + +# Putem accesa elementul tuplelui folosind funcția `elem`: +elem({1, 2, 3}, 0) #=> 1 + +# Listele sunt implementate ca liste înlănțuite. +[1,2,3] # listă + +# Fiecare listă ne vidă are cap (primul element al listei) +# și coadă (restul elementelor). +# Putem accesa capul și coada listei cum urmează: +[cap | coadă] = [1,2,3] +cap #=> 1 +coadă #=> [2, 3] + +# În Elixir, ca și în Erlang, simbolul `=` denotă potrivirea șabloanelor și +# nu atribuire. +# +# Aceasta înseamnă că expresia din stînga (șablonul) se potrivește cu +# expresia din dreaptă. +# +# În modul acesta exemplul de mai sus lucrează accesînd capul și coada unei liste. + +# Potrivirea șablonului va da eroare cînd expresiile din stînga și dreapta nu se +# potrivesc, în exemplu acesta tuplele au lungime diferită. +{a, b, c} = {1, 2} #=> ** (MatchError) + +# Există și date binare +<<1,2,3>> + +# Sunt două tipuri de șiruri de caractere +"salut" # șir de caractere Elixir +'salut' # listă de caractere Erlang + +# Șir de caractere pe mai multe linii +""" +Sunt un șir de caractere +pe mai multe linii. +""" +#=> "Sunt un șir de caractere\npe mai multe linii..\n" + +# Șirurile de caractere sunt codificate în UTF-8: +"Bună dimineața" #=> "Bună dimineața" + +# Șirurile de caractere sunt date binare, listele de caractere doar liste. +<<?a, ?b, ?c>> #=> "abc" +[?a, ?b, ?c] #=> 'abc' + +# `?a` în Elixir întoarce codul ASCII pentru litera `a` +?a #=> 97 + +# Pentru a concatena listele folosiți `++`, pentru date binare - `<>` +[1,2,3] ++ [4,5] #=> [1,2,3,4,5] +'Salut ' ++ 'lume' #=> 'Salut lume' + +<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>> +"Salut " <> "lume" #=> "Salut lume" + +# Diapazoanele sunt reprezentate ca `început..sfîrșit` (inclusiv) +1..10 #=> 1..10 +început..sfîrșit = 1..10 # Putem folosi potrivirea șabloanelor cu diapazoane de asemenea +[început, sfîrșit] #=> [1, 10] + +# Dicţionarele stochează chei şi o valoare pentru fiecare cheie +genuri = %{"Ion" => "bărbat", "Maria" => "femeie"} +genuri["Ion"] #=> "bărbat" + +# Dicționare cu chei de tip atom au sintaxă specială +genuri = %{ion: "bărbat", maria: "femeie"} +genuri.ion #=> "bărbat" + +## --------------------------- +## -- Operatori +## --------------------------- + +# Operații matematice +1 + 1 #=> 2 +10 - 5 #=> 5 +5 * 2 #=> 10 +10 / 2 #=> 5.0 + +# În Elixir operatorul `/` întotdeauna întoarce un număr cu virgulă mobilă. + +# Folosiți `div` pentru împărțirea numerelor întregi +div(10, 2) #=> 5 + +# Pentru a obține restul de la împărțire utilizați `rem` +rem(10, 3) #=> 1 + +# Există și operatori booleni: `or`, `and` and `not`. +# Acești operatori așteaptă ca primul argument o expresie booleană. +true and true #=> true +false or true #=> true +1 and true #=> ** (BadBooleanError) + +# Elixir de asemenea oferă `||`, `&&` și `!` care acceptă argumente de orice tip. +# Toate valorile în afară de `false` și `nil` se vor evalua ca `true`. +1 || true #=> 1 +false && 1 #=> false +nil && 20 #=> nil +!true #=> false + +# Operatori de comparație: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` și `>` +1 == 1 #=> true +1 != 1 #=> false +1 < 2 #=> true + +# `===` și `!==` au strictețe mai mare cînd comparăm numere întregi și reale: +1 == 1.0 #=> true +1 === 1.0 #=> false + +# Putem compara de asemenea și date de diferite tipuri: +1 < :salut #=> true + +# La compararea diferitor tipuri folosiți următoare prioritate: +# număr < atom < referință < funcție < port < proces < tuple < listă < șir de caractere + +# Cităm pe Joe Armstrong în acest caz: "Ordinea actuală nu e importantă, +dar că ordinea totală este bine definită este important." + +## --------------------------- +## -- Ordinea execuției +## --------------------------- + +# expresia `if` +if false do + "Aceasta nu veți vedea niciodată" +else + "Aceasta veți vedea" +end + +# expresia opusă `unless` +unless true do + "Aceasta nu veți vedea niciodată" +else + "Aceasta veți vedea" +end + +# Țineți minte potrivirea șabloanelor? Multe structuri în Elixir se bazează pe ea. + +# `case` ne permite să comparăm o valoare cu multe șabloane: +case {:unu, :doi} do + {:patru, :cinci} -> + "Aceasta nu se potrivește" + {:unu, x} -> + "Aceasta se potrivește și atribuie lui `x` `:doi` în acest bloc" + _ -> + "Aceasta se va potrivi cu orice valoare" +end + +# Simbolul `_` se numește variabila anonimă. +# Folosiți-l pentru valori ce nu vă interesează. +# De exemplu, dacă doar capul listei ne intereseaza: +[cap | _] = [1,2,3] +cap #=> 1 + +# Pentru o citire mai bună putem scri: +[cap | _coadă] = [:a, :b, :c] +cap #=> :a + +# `cond` ne permite să verificăm multe condiții de odată. +# Folosiți `cond` în schimbul la multe expresii `if`. +cond do + 1 + 1 == 3 -> + "Aceasta nu veți vedea niciodată" + 2 * 5 == 12 -> + "Pe mine la fel" + 1 + 2 == 3 -> + "Aceasta veți vedea" +end + +# Este obușnuit de setat ultima condiție cu `true`, care se va potrivi întotdeauna. +cond do + 1 + 1 == 3 -> + "Aceasta nu veți vedea niciodată" + 2 * 5 == 12 -> + "Pe mine la fel" + true -> + "Aceasta veți vedea (este else în esență)" +end + +# Blocul `try/catch` se foloște pentru prelucrarea excepțiilor. +# Elixir suportă blocul `after` care se execută în orice caz. +try do + throw(:salut) +catch + mesaj -> "Am primit #{mesaj}." +after + IO.puts("Sunt în blocul after.") +end +#=> Sunt în blocul after. +# "Am primit salut" + +## --------------------------- +## -- Module și Funcții +## --------------------------- + +# Funcții anonime (atenție la punct la apelarea funcției) +square = fn(x) -> x * x end +square.(5) #=> 25 + +# Ele de asemenea aceptă multe clauze și expresii de gardă. +# Expresiile de gardă vă permit să acordați potrivirea șabloanelor, +# ele sunt indicate după cuvîntul cheie `when`: +f = fn + x, y when x > 0 -> x + y + x, y -> x * y +end + +f.(1, 3) #=> 4 +f.(-1, 3) #=> -3 + +# Elixir de asemenea oferă multe funcții incorporate. +# Ele sunt accesibile în scopul curent. +is_number(10) #=> true +is_list("salut") #=> false +elem({1,2,3}, 0) #=> 1 + +# Puteți grupa cîteva funcții într-un modul. În interiorul modulului folosiți `def` +# pentru a defini funcțiile necesare. +defmodule Math do + def sum(a, b) do + a + b + end + + def square(x) do + x * x + end +end + +Math.sum(1, 2) #=> 3 +Math.square(3) #=> 9 + +# Pentru a compila modulul nostru simplu Math îl salvăm ca `math.ex` și utilizăm `elixirc`. +# în terminal: elixirc math.ex + +# În interiorul modulului putem defini funcții cu `def` și funcții private cu `defp`. +defmodule PrivateMath do + # O funcție definită cu `def` este accesibilă pentru apelare din alte module, + def sum(a, b) do + do_sum(a, b) + end + + # O funcție privată poate fi apelată doar local. + defp do_sum(a, b) do + a + b + end +end + +PrivateMath.sum(1, 2) #=> 3 +PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError) + +# Declarația funcției de asemenea suportă expresii de gardă și multe clauze: +defmodule Geometry do + def area({:rectangle, w, h}) do + w * h + end + + def area({:circle, r}) when is_number(r) do + 3.14 * r * r + end +end + +Geometry.area({:rectangle, 2, 3}) #=> 6 +Geometry.area({:circle, 3}) #=> 28.25999999999999801048 +Geometry.area({:circle, "not_a_number"}) #=> ** (FunctionClauseError) + +# Din cauza variabilelor imutabile, un rol important îl ocupă funcțiile recursive +defmodule Recursion do + def sum_list([head | tail], acc) do + sum_list(tail, acc + head) + end + + def sum_list([], acc) do + acc + end +end + +Recursion.sum_list([1,2,3], 0) #=> 6 + +# Modulele în Elixir suportă atribute, există atribute incorporate și +# puteți adăuga altele. +defmodule MyMod do + @moduledoc """ + Este un atribut incorporat + """ + + @my_data 100 # Acesta e atributul nostru + IO.inspect(@my_data) #=> 100 +end + +# Operatorul |> permite transferarea rezultatului unei expresii din stînga +# ca primul argument al unei funcții din dreapta. +Range.new(1,10) +|> Enum.map(fn x -> x * x end) +|> Enum.filter(fn x -> rem(x, 2) == 0 end) +#=> [4, 16, 36, 64, 100] + +## --------------------------- +## -- Structuri și Excepții +## --------------------------- + +# Structurile sunt extensii a dicționarelor ce au valori implicite, +# verificări în timpul compilării și polimorfism +defmodule Person do + defstruct name: nil, age: 0, height: 0 +end + +joe_info = %Person{ name: "Joe", age: 30, height: 180 } +#=> %Person{age: 30, height: 180, name: "Joe"} + +# Acesarea cîmpului din structură +joe_info.name #=> "Joe" + +# Actualizarea valorii cîmpului +older_joe_info = %{ joe_info | age: 31 } +#=> %Person{age: 31, height: 180, name: "Joe"} + +# Blocul `try` cu cuvîntul cheie `rescue` e folosit pentru a prinde excepții +try do + raise "o eroare" +rescue + RuntimeError -> "a fost prinsă o eroare runtime" + _error -> "aici vor fi prinse toate erorile" +end +#=> "a fost prinsă o eroare runtime" + +# Toate excepțiile au un mesaj +try do + raise "o eroare" +rescue + x in [RuntimeError] -> + x.message +end +#=> "o eroare" + +## --------------------------- +## -- Concurența +## --------------------------- + +# Concurența în Elixir se bazează pe modelul actor. Pentru a scrie programe +# concurente avem nevoie de trei lucruri: +# 1. Crearea proceselor +# 2. Trimiterea mesajelor +# 3. Primirea mesajelor + +# Un nou proces se crează folosind funcția `spawn`, care primește o funcție +# ca argument. +f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245> +spawn(f) #=> #PID<0.40.0> + +# `spawn` întoarce identificatorul procesului pid, îl puteți folosi pentru +# a trimite mesaje procesului. Mesajele se transmit folosind operatorul `send`. +# Pentru primirea mesajelor se folosește mecanismul `receive`: + +# Blocul `receive do` este folosit pentru așteptarea mesajelor și prelucrarea lor +# cînd au fost primite. Blocul `receive do` va procesa doar un singur mesaj primit. +# Pentru a procesa mai multe mesaje, funcția cu blocul `receive do` trebuie +# recursiv să se auto apeleze. + +defmodule Geometry do + def area_loop do + receive do + {:rectangle, w, h} -> + IO.puts("Aria = #{w * h}") + area_loop() + {:circle, r} -> + IO.puts("Aria = #{3.14 * r * r}") + area_loop() + end + end +end + +# Compilați modulul și creați un proces +pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0> +# Un alt mod +pid = spawn(Geometry, :area_loop, []) + +# Trimiteți un mesaj către `pid` care se va potrivi cu un șablon din blocul `receive` +send pid, {:rectangle, 2, 3} +#=> Aria = 6 +# {:rectangle,2,3} + +send pid, {:circle, 2} +#=> Aria = 12.56000000000000049738 +# {:circle,2} + +# Interpretatorul este de asemenea un proces, puteți folosi `self` +# pentru a primi identificatorul de proces: +self() #=> #PID<0.27.0> + +## --------------------------- +## -- Agenții +## --------------------------- + +# Un agent este un proces care urmărește careva valori ce se schimbă. + +# Creați un agent cu `Agent.start_link`, transmițînd o funcție. +# Stare inițială a agentului va fi rezultatul funcției. +{ok, my_agent} = Agent.start_link(fn -> ["roșu", "verde"] end) + +# `Agent.get` primește numele agentului și o `fn` care primește starea curentă +# Orice va întoarce `fn` este ceea ce veți primi înapoi: +Agent.get(my_agent, fn colors -> colors end) #=> ["roșu", "verde"] + +# Actualizați starea agentului în acelaș mod: +Agent.update(my_agent, fn colors -> ["albastru" | colors] end) +``` + +## Link-uri utile + +* [Primii pași](http://elixir-lang.org/getting-started/introduction.html) de pe [situl Elixir](http://elixir-lang.org) +* [Documentația oficială Elixir](http://elixir-lang.org/docs/master/) +* [Un mic conspect pe Elixir](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf) +* [Cartea "Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) de Dave Thomas +* [Cartea "Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) de Fred Hebert +* [Cartea "Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) de Joe Armstrong diff --git a/rst.html.markdown b/rst.html.markdown index 65f848ed..fbf9a069 100644 --- a/rst.html.markdown +++ b/rst.html.markdown @@ -47,9 +47,6 @@ Title are underlined with equals signs too Subtitles with dashes --------------------- -And sub-subtitles with tildes -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - You can put text in *italic* or in **bold**, you can "mark" text as code with double backquote ``: ``print()``. Lists are as simple as in Markdown: diff --git a/ru-ru/clojure-ru.html.markdown b/ru-ru/clojure-ru.html.markdown index 356d1cc0..19233d23 100644 --- a/ru-ru/clojure-ru.html.markdown +++ b/ru-ru/clojure-ru.html.markdown @@ -8,9 +8,9 @@ translators: lang: ru-ru --- -Clojure, это представитель семейства Lisp-подобных языков, разработанный +Clojure — это представитель семейства Lisp-подобных языков, разработанный для Java Virtual Machine. Язык идейно гораздо ближе к чистому -[функциональному программированию](https://ru.wikipedia.org/wiki/%D0%A4%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%BE%D0%BD%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B5_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5) чем его прародитель Common Lisp, но в то же время обладает набором инструментов для работы с состоянием, +[функциональному программированию](https://ru.wikipedia.org/wiki/%D0%A4%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%BE%D0%BD%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B5_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5), чем его прародитель Common Lisp, но в то же время обладает набором инструментов для работы с состоянием, таких как [STM](https://ru.wikipedia.org/wiki/Software_transactional_memory). Благодаря такому сочетанию технологий в одном языке, разработка программ, @@ -23,9 +23,9 @@ Clojure, это представитель семейства Lisp-подобн ```clojure ; Комментарии начинаются символом ";". -; Код на языке Clojure записывается в виде "форм", +; Код на языке Clojure записывается в виде «форм», ; которые представляют собой обычные списки элементов, разделенных пробелами, -; заключённые в круглые скобки +; заключённые в круглые скобки. ; ; Clojure Reader (инструмент языка, отвечающий за чтение исходного кода), ; анализируя форму, предполагает, что первым элементом формы (т.е. списка) @@ -76,32 +76,32 @@ Clojure, это представитель семейства Lisp-подобн '(+ 1 2) ; => (+ 1 2) ; ("'", это краткая запись формы (quote (+ 1 2)) -; "Квотированный" список можно вычислить, передав его функции eval +; «Квотированный» список можно вычислить, передав его функции eval (eval '(+ 1 2)) ; => 3 ; Коллекции и Последовательности ;;;;;;;;;;;;;;;;;;; -; Списки (Lists) в clojure структурно представляют собой "связанные списки", +; Списки (Lists) в clojure структурно представляют собой «связанные списки», ; тогда как Векторы (Vectors), устроены как массивы. ; Векторы и Списки тоже являются классами Java! (class [1 2 3]); => clojure.lang.PersistentVector (class '(1 2 3)); => clojure.lang.PersistentList -; Список может быть записан, как (1 2 3), но в этом случае +; Список может быть записан как (1 2 3), но в этом случае ; он будет воспринят reader`ом, как вызов функции. ; Есть два способа этого избежать: ; '(1 2 3) - квотирование, ; (list 1 2 3) - явное конструирование списка с помощью функции list. -; "Коллекции", это некие наборы данных +; «Коллекции» — это некие наборы данных. ; И списки, и векторы являются коллекциями: (coll? '(1 2 3)) ; => true (coll? [1 2 3]) ; => true -; "Последовательности" (seqs), это абстракция над наборами данных, +; «Последовательности» (seqs) — это абстракция над наборами данных, ; элементы которых "упакованы" последовательно. -; Списки - последовательности, а вектора - нет. +; Списки — последовательности, а векторы — нет. (seq? '(1 2 3)) ; => true (seq? [1 2 3]) ; => false @@ -119,7 +119,7 @@ Clojure, это представитель семейства Lisp-подобн ; Функция conj добавляет элемент в коллекцию ; максимально эффективным для неё способом. -; Для списков эффективно добавление в начло, а для векторов - в конец. +; Для списков эффективно добавление в начло, а для векторов — в конец. (conj [1 2 3] 4) ; => [1 2 3 4] (conj '(1 2 3) 4) ; => (4 1 2 3) @@ -130,7 +130,7 @@ Clojure, это представитель семейства Lisp-подобн (map inc [1 2 3]) ; => (2 3 4) (filter even? [1 2 3]) ; => (2) -; reduce поможет "свернуть" коллекцию +; reduce поможет «свернуть» коллекцию (reduce + [1 2 3 4]) ; = (+ (+ (+ 1 2) 3) 4) ; => 10 @@ -144,12 +144,12 @@ Clojure, это представитель семейства Lisp-подобн ;;;;;;;;;;;;;;;;;;;;; ; Функция создается специальной формой fn. -; "Тело" функции может состоять из нескольких форм, +; «Тело» функции может состоять из нескольких форм, ; но результатом вызова функции всегда будет результат вычисления ; последней из них. (fn [] "Hello World") ; => fn -; (Вызов функции требует "оборачивания" fn-формы в форму вызова) +; (Вызов функции требует «оборачивания» fn-формы в форму вызова) ((fn [] "Hello World")) ; => "Hello World" ; Назначить значению имя можно специальной формой def @@ -160,7 +160,7 @@ x ; => 1 (def hello-world (fn [] "Hello World")) (hello-world) ; => "Hello World" -; Поскольку именование функций - очень частая операция, +; Поскольку именование функций — очень частая операция, ; clojure позволяет, сделать это проще: (defn hello-world [] "Hello World") @@ -211,7 +211,7 @@ x ; => 1 ; Отображения могут использовать в качестве ключей любые хэшируемые значения, ; однако предпочтительными являются ключи, -; являющиеся "ключевыми словами" (keywords) +; являющиеся «ключевыми словами» (keywords) (class :a) ; => clojure.lang.Keyword (def stringmap {"a" 1, "b" 2, "c" 3}) @@ -263,7 +263,7 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут ; Исключаются - посредством disj (disj #{1 2 3} 1) ; => #{2 3} -; Вызов множества, как функции, позволяет проверить +; Вызов множества как функции позволяет проверить ; принадлежность элемента этому множеству: (#{1 2 3} 1) ; => 1 (#{1 2 3} 4) ; => nil @@ -274,8 +274,8 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут ; Полезные формы ;;;;;;;;;;;;;;;;; -; Конструкции ветвления в clojure, это обычные макросы -; и подобны их собратьям в других языках: +; Конструкции ветвления в clojure — это обычные макросы, +; они подобны своим собратьям в других языках: (if false "a" "b") ; => "b" (if false "a") ; => nil @@ -285,7 +285,7 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут (let [a 1 b 2] (> a b)) ; => false -; Несколько форм можно объединить в одну форму посредством do +; Несколько форм можно объединить в одну форму посредством do. ; Значением do-формы будет значение последней формы из списка вложенных в неё: (do (print "Hello") @@ -298,7 +298,7 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут (str "Hello " name)) (print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff") -; Ещё один пример - let: +; Ещё один пример — let: (let [name "Urkel"] (print "Saying hello to " name) (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel") @@ -306,7 +306,7 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут ; Модули ;;;;;;;;; -; Форма "use" позволяет добавить в текущее пространство имен +; Форма use позволяет добавить в текущее пространство имен ; все имена (вместе со значениями) из указанного модуля: (use 'clojure.set) @@ -392,7 +392,7 @@ keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут my-atom ;=> Atom<#...> (Возвращает объект типа Atom) @my-atom ; => {:a 1 :b 2} -; Пример реализации счётчика на атоме +; Пример реализации счётчика на атоме: (def counter (atom 0)) (defn inc-counter [] (swap! counter inc)) @@ -414,13 +414,13 @@ my-atom ;=> Atom<#...> (Возвращает объект типа Atom) Это руководство не претендует на полноту, но мы смеем надеяться, способно вызвать интерес к дальнейшему изучению языка. -Clojure.org - сайт содержит большое количество статей по языку: +Сайт Clojure.org содержит большое количество статей по языку: [http://clojure.org/](http://clojure.org/) -Clojuredocs.org - сайт документации языка с примерами использования функций: +Clojuredocs.org — сайт документации языка с примерами использования функций: [http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core) -4Clojure - отличный способ закрепить навыки программирования на clojure, решая задачи вместе с коллегами со всего мира: +4Clojure — отличный способ закрепить навыки программирования на clojure, решая задачи вместе с коллегами со всего мира: [http://www.4clojure.com/](http://www.4clojure.com/) Clojure-doc.org (да, именно) неплохой перечень статей для начинающих: diff --git a/ru-ru/common-lisp-ru.html.markdown b/ru-ru/common-lisp-ru.html.markdown new file mode 100644 index 00000000..d5f9bf0e --- /dev/null +++ b/ru-ru/common-lisp-ru.html.markdown @@ -0,0 +1,704 @@ +--- + +language: "Common Lisp" +filename: commonlisp.lisp +contributors: + - ["Paul Nathan", "https://github.com/pnathan"] + - ["Rommel Martinez", "https://ebzzry.io"] +translators: + - ["Michael Filonenko", "https://github.com/filonenko-mikhail"] +lang: ru-ru +--- + +Common Lisp - мультипарадигменный язык программирования общего назначения, подходящий для широкого +спектра задач. +Его частенько называют программируемым языком программирования. + +Идеальная отправная точка - книга [Common Lisp на практике (перевод)](http://lisper.ru/pcl/). +Ещё одна популярная книга [Land of Lisp](http://landoflisp.com/). +И одна из последних книг [Common Lisp Recipes](http://weitz.de/cl-recipes/) вобрала в себя лучшие +архитектурные решения на основе опыта коммерческой работки автора. + + + +```common-lisp + +;;;----------------------------------------------------------------------------- +;;; 0. Синтаксис +;;;----------------------------------------------------------------------------- + +;;; Основные формы + +;;; Существует два фундамента CL: АТОМ и S-выражение. +;;; Как правило, сгруппированные S-выражения называют `формами`. + +10 ; атом; вычисляется в самого себя +:thing ; другой атом; вычисляется в символ :thing +t ; ещё один атом, обозначает `истину` (true) +(+ 1 2 3 4) ; s-выражение +'(4 :foo t) ; ещё одно s-выражение + +;;; Комментарии + +;;; Однострочные комментарии начинаются точкой с запятой. Четыре знака подряд +;;; используют для комментария всего файла, три для раздела, два для текущего +;;; определения; один для текущей строки. Например: + +;;;; life.lisp + +;;; То-сё - пятое-десятое. Оптимизировано для максимального бадабума и ччччч. +;;; Требуется для функции PoschitatBenzinIsRossiiVBelarus + +(defun meaning (life) + "Возвращает смысл Жизни" + (let ((meh "abc")) + ;; Вызывает бадабум + (loop :for x :across meh + :collect x))) ; сохранить значения в x, и потом вернуть + +;;; А вот целый блок комментария можно использовать как угодно. +;;; Для него используются #| и |# + +#| Целый блок комментария, который размазан + на несколько строк + #| + которые могут быть вложенными! + |# +|# + +;;; Чем пользоваться + +;;; Существует несколько реализаций: и коммерческих, и открытых. +;;; Все они максимально соответствуют стандарту языка. +;;; SBCL, например, добротен. А за дополнительными библиотеками +;;; нужно ходить в Quicklisp + +;;; Обычно разработка ведется в текстовом редакторе с запущенным в цикле +;;; интерпретатором (в CL это Read Eval Print Loop). Этот цикл (REPL) +;;; позволяет интерактивно выполнять части программы вживую сразу наблюдая +;;; результат. + +;;;----------------------------------------------------------------------------- +;;; 1. Базовые типы и операторы +;;;----------------------------------------------------------------------------- + +;;; Символы + +'foo ; => FOO Символы автоматически приводятся к верхнему регистру. + +;;; INTERN создаёт символ из строки. + +(intern "AAAA") ; => AAAA +(intern "aaa") ; => |aaa| + +;;; Числа + +9999999999999999999999 ; целые +#b111 ; двоичные => 7 +#o111 ; восьмеричные => 73 +#x111 ; шестнадцатиричные => 273 +3.14159s0 ; с плавающей точкой +3.14159d0 ; с плавающей точкой с двойной точностью +1/2 ; рациональные) +#C(1 2) ; комплексные + +;;; Вызов функции пишется как s-выражение (f x y z ....), где f это функция, +;;; x, y, z, ... аругменты. + +(+ 1 2) ; => 3 + +;;; Если вы хотите просто представить код как данные, воспользуйтесь формой QUOTE +;;; Она не вычисляет аргументы, а возвращает их как есть. +;;; Она даёт начало метапрограммированию + +(quote (+ 1 2)) ; => (+ 1 2) +(quote a) ; => A + +;;; QUOTE можно сокращенно записать знаком ' + +'(+ 1 2) ; => (+ 1 2) +'a ; => A + +;;; Арифметические операции + +(+ 1 1) ; => 2 +(- 8 1) ; => 7 +(* 10 2) ; => 20 +(expt 2 3) ; => 8 +(mod 5 2) ; => 1 +(/ 35 5) ; => 7 +(/ 1 3) ; => 1/3 +(+ #C(1 2) #C(6 -4)) ; => #C(7 -2) + +;;; Булевые + +t ; истина; любое не-NIL значение `истинно` +nil ; ложь; а ещё пустой список () тоже `ложь` +(not nil) ; => T +(and 0 t) ; => T +(or 0 nil) ; => 0 + +;;; Строковые символы + +#\A ; => #\A +#\λ ; => #\GREEK_SMALL_LETTER_LAMDA +#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA + +;;; Строки это фиксированные массивы символов + +"Hello, world!" +"Тимур \"Каштан\" Бадтрудинов" ; экранировать двойную кавычку обратным слешом + +;;; Строки можно соединять + +(concatenate 'string "ПРивет, " "мир!") ; => "ПРивет, мир!" + +;;; Можно пройтись по строке как по массиву символов + +(elt "Apple" 0) ; => #\A + +;;; Для форматированного вывода используется FORMAT. Он умеет выводить, как просто значения, +;;; так и производить циклы и учитывать условия. Первый агрумент указывает куда отправить +;;; результат. Если NIL, FORMAT вернет результат как строку, если T результат отправиться +;;; консоль вывода а форма вернет NIL. + +(format nil "~A, ~A!" "Привет" "мир") ; => "Привет, мир!" +(format t "~A, ~A!" "Привет" "мир") ; => NIL + + +;;;----------------------------------------------------------------------------- +;;; 2. Переменные +;;;----------------------------------------------------------------------------- + +;;; С помощью DEFVAR и DEFPARAMETER вы можете создать глобальную (динамческой видимости) +;;; переменную. +;;; Имя переменной может состоять из любых символов кроме: ()",'`;#|\ + +;;; Разница между DEFVAR и DEFPARAMETER в том, что повторное выполнение DEFVAR +;;; переменную не поменяет. А вот DEFPARAMETER меняет переменную при каждом вызове. + +;;; Обычно глобальные (динамически видимые) переменные содержат звездочки в имени. + +(defparameter *some-var* 5) +*some-var* ; => 5 + +;;; Можете использовать unicode. +(defparameter *КУКУ* nil) + +;;; Доступ к необъявленной переменной - это непредсказуемое поведение. Не делайте так. + +;;; С помощью LET можете сделать локальное связывание. +;;; В следующем куске кода, `я` связывается с "танцую с тобой" только +;;; внутри формы (let ...). LET всегда возвращает значение последней формы. + +(let ((я "танцую с тобой")) я) ; => "танцую с тобой" + + +;;;-----------------------------------------------------------------------------; +;;; 3. Структуры и коллекции +;;;-----------------------------------------------------------------------------; + + +;;; Структуры + +(defstruct dog name breed age) +(defparameter *rover* + (make-dog :name "rover" + :breed "collie" + :age 5)) +*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5) +(dog-p *rover*) ; => T +(dog-name *rover*) ; => "rover" + +;;; DEFSTRUCT автоматически создала DOG-P, MAKE-DOG, и DOG-NAME + + +;;; Пары (cons-ячейки) + +;;; CONS создаёт пары. CAR и CDR возвращают начало и конец CONS-пары. + +(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB) +(car (cons 'SUBJECT 'VERB)) ; => SUBJECT +(cdr (cons 'SUBJECT 'VERB)) ; => VERB + + +;;; Списки + +;;; Списки это связанные CONS-пары, в конце самой последней из которых стоит NIL +;;; (или '() ). + +(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3) + +;;; Списки с произвольным количеством элементов удобно создавать с помощью LIST + +(list 1 2 3) ; => '(1 2 3) + +;;; Если первый аргумент для CONS это атом и второй аргумент список, CONS +;;; возвращает новую CONS-пару, которая представляет собой список + +(cons 4 '(1 2 3)) ; => '(4 1 2 3) + +;;; Чтобы объединить списки, используйте APPEND + +(append '(1 2) '(3 4)) ; => '(1 2 3 4) + +;;; Или CONCATENATE + +(concatenate 'list '(1 2) '(3 4)) ; => '(1 2 3 4) + +;;; Списки это самый используемый элемент языка. Поэтому с ними можно делать +;;; многие вещи. Вот несколько примеров: + +(mapcar #'1+ '(1 2 3)) ; => '(2 3 4) +(mapcar #'+ '(1 2 3) '(10 20 30)) ; => '(11 22 33) +(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4) +(every #'evenp '(1 2 3 4)) ; => NIL +(some #'oddp '(1 2 3 4)) ; => T +(butlast '(subject verb object)) ; => (SUBJECT VERB) + + +;;; Вектора + +;;; Вектора заданные прямо в коде - это массивы с фиксированной длинной. + +#(1 2 3) ; => #(1 2 3) + +;;; Для соединения векторов используйте CONCATENATE + +(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6) + + +;;; Массивы + +;;; И вектора и строки это подмножества массивов. + +;;; Двухмерные массивы + +(make-array (list 2 2)) ; => #2A((0 0) (0 0)) +(make-array '(2 2)) ; => #2A((0 0) (0 0)) +(make-array (list 2 2 2)) ; => #3A(((0 0) (0 0)) ((0 0) (0 0))) + +;;; Внимание: значение по-умолчанию элемента массива зависит от реализации. +;;; Лучше явно указывайте: + +(make-array '(2) :initial-element 'unset) ; => #(UNSET UNSET) + +;;; Для доступа к элементу в позиции 1, 1, 1: + +(aref (make-array (list 2 2 2)) 1 1 1) ; => 0 + + +;;; Вектора с изменяемой длиной + +;;; Вектора с изменяемой длиной при выводе на консоль выглядят также, +;;; как и вектора, с константной длиной + +(defparameter *adjvec* (make-array '(3) :initial-contents '(1 2 3) + :adjustable t :fill-pointer t)) +*adjvec* ; => #(1 2 3) + +;;; Добавление новых элементов + +(vector-push-extend 4 *adjvec*) ; => 3 +*adjvec* ; => #(1 2 3 4) + + +;;; Множества, это просто списки: + +(set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1) +(intersection '(1 2 3 4) '(4 5 6 7)) ; => 4 +(union '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1 4 5 6 7) +(adjoin 4 '(1 2 3 4)) ; => (1 2 3 4) + +;;; Несмотря на все, для действительно больших объемов данных, вам нужно что-то +;;; лучше, чем просто связанные списки + +;;; Словари представлены хеш таблицами. + +;;; Создание хеш таблицы: + +(defparameter *m* (make-hash-table)) + +;;; Установка пары ключ-значение + +(setf (gethash 'a *m*) 1) + +;;; Возврат значения по ключу + +(gethash 'a *m*) ; => 1, T + +;;; CL выражения умеют возвращать сразу несколько значений. + +(values 1 2) ; => 1, 2 + +;;; которые могут быть распределены по переменным с помощью MULTIPLE-VALUE-BIND + +(multiple-value-bind (x y) + (values 1 2) + (list y x)) + +; => '(2 1) + +;;; GETHASH как раз та функция, которая возвращает несколько значений. Первое +;;; значение - это значение по ключу в хеш таблицу. Если ключ не был найден, +;;; возвращает NIL. + +;;; Второе возвращаемое значение, указывает был ли ключ в хеш таблице. Если ключа +;;; не было, то возвращает NIL. Таким образом можно проверить, это значение +;;; NIL, или ключа просто не было. + +;;; Вот возврат значений, в случае когда ключа в хеш таблице не было: + +(gethash 'd *m*) ;=> NIL, NIL + +;;; Можете задать значение по умолчанию. + +(gethash 'd *m* :not-found) ; => :NOT-FOUND + +;;; Давайте обработаем возврат несколько значений. + +(multiple-value-bind (a b) + (gethash 'd *m*) + (list a b)) +; => (NIL NIL) + +(multiple-value-bind (a b) + (gethash 'a *m*) + (list a b)) +; => (1 T) + + +;;;----------------------------------------------------------------------------- +;;; 3. Функции +;;;----------------------------------------------------------------------------- + +;;; Для создания анонимных функций используйте LAMBDA. Функций всегда возвращают +;;; значение последнего своего выражения. Как выглядит функция при выводе в консоль +;;; зависит от реализации. + +(lambda () "Привет Мир") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}> + +;;; Для вызова анонимной функции пользуйтесь FUNCALL + +(funcall (lambda () "Привет Мир")) ; => "Привет мир" +(funcall #'+ 1 2 3) ; => 6 + +;;; FUNCALL сработает и тогда, когда анонимная функция стоит в начале +;;; неэкранированного списка + +((lambda () "Привет Мир")) ; => "Привет Мир" +((lambda (val) val) "Привет Мир") ; => "Привет Мир" + +;;; FUNCALL используется, когда аргументы заранее известны. +;;; В противном случае используйте APPLY + +(apply #'+ '(1 2 3)) ; => 6 +(apply (lambda () "Привет Мир") nil) ; => "Привет Мир" + +;;; Для обычной функции с именем используйте DEFUN + +(defun hello-world () "Привет Мир") +(hello-world) ; => "Привет Мир" + +;;; Выше видно пустой список (), это место для определения аргументов + +(defun hello (name) (format nil "Hello, ~A" name)) +(hello "Григорий") ; => "Привет, Григорий" + +;;; Можно указать необязательные аргументы. По умолчанию они будут NIL + +(defun hello (name &optional from) + (if from + (format t "Приветствие для ~A от ~A" name from) + (format t "Привет, ~A" name))) + +(hello "Георгия" "Василия") ; => Приветствие для Георгия от Василия + +;;; Можно явно задать значения по умолчанию + +(defun hello (name &optional (from "Мира")) + (format nil "Приветствие для ~A от ~A" name from)) + +(hello "Жоры") ; => Приветствие для Жоры от Мира +(hello "Жоры" "альпаки") ; => Приветствие для Жоры от альпаки + +;;; Можно также задать именованные параметры + +(defun generalized-greeter (name &key (from "Мира") (honorific "Господин")) + (format t "Здравствуйте, ~A ~A, от ~A" honorific name from)) + +(generalized-greeter "Григорий") +; => Здравствуйте, Господин Григорий, от Мира + +(generalized-greeter "Григорий" :from "альпаки" :honorific "гражданин") +; => Здравствуйте, Гражданин Григорий, от альпаки + + +;;;----------------------------------------------------------------------------- +;;; 4. Равенство или эквивалентность +;;;----------------------------------------------------------------------------- + +;;; У CL сложная система эквивалентности. Взглянем одним глазом. + +;;; Для чисел используйте `=' +(= 3 3.0) ; => T +(= 2 1) ; => NIL + +;;; Для идентичености объектов используйте EQL +(eql 3 3) ; => T +(eql 3 3.0) ; => NIL +(eql (list 3) (list 3)) ; => NIL + +;;; Для списков, строк, и битовых векторов - EQUAL +(equal (list 'a 'b) (list 'a 'b)) ; => T +(equal (list 'a 'b) (list 'b 'a)) ; => NIL + + +;;;----------------------------------------------------------------------------- +;;; 5. Циклы и ветвления +;;;----------------------------------------------------------------------------- + +;;; Ветвления + +(if t ; проверямое значение + "случилась истина" ; если, оно было истинно + "случилась ложь") ; иначе, когда оно было ложно +; => "случилась истина" + +;;; В форме ветвления if, все не-NIL значения это `истина` + +(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO) +(if (member 'Groucho '(Harpo Groucho Zeppo)) + 'yep + 'nope) +; => 'YEP + +;;; COND это цепочка проверок для нахождения искомого +(cond ((> 2 2) (error "мимо!")) + ((< 2 2) (error "опять мимо!")) + (t 'ok)) ; => 'OK + +;;; TYPECASE выбирает ветку исходя из типа выражения +(typecase 1 + (string :string) + (integer :int)) +; => :int + + +;;; Циклы + +;;; С рекурсией + +(defun fact (n) + (if (< n 2) + 1 + (* n (fact(- n 1))))) + +(fact 5) ; => 120 + +;;; И без + +(defun fact (n) + (loop :for result = 1 :then (* result i) + :for i :from 2 :to n + :finally (return result))) + +(fact 5) ; => 120 + +(loop :for x :across "abc" :collect x) +; => (#\a #\b #\c #\d) + +(dolist (i '(1 2 3 4)) + (format t "~A" i)) +; => 1234 + + +;;;----------------------------------------------------------------------------- +;;; 6. Установка значений в переменные (и не только) +;;;----------------------------------------------------------------------------- + +;;; Для присвоения переменной нового значения используйте SETF. Это уже было +;;; при работе с хеш таблицами. + +(let ((variable 10)) + (setf variable 2)) +; => 2 + +;;; Для функционального подхода в программировании, старайтесь избегать измений +;;; в переменных. + +;;;----------------------------------------------------------------------------- +;;; 7. Классы и объекты +;;;----------------------------------------------------------------------------- + +;;; Никаких больше животных в примерах. Берем устройства приводимые в движение +;;; мускульной силой человека. + +(defclass human-powered-conveyance () + ((velocity + :accessor velocity + :initarg :velocity) + (average-efficiency + :accessor average-efficiency + :initarg :average-efficiency)) + (:documentation "Устройство движимое человеческой силой")) + +;;; Аргументы DEFCLASS: +;;; 1. Имя класса +;;; 2. Список родительских классов +;;; 3. Список полей +;;; 4. Необязательная метаинформация + +;;; Если родительские классы не заданы, используется "стандартный" класс +;;; Это можно *изменить*, но хорошенько подумайте прежде. Если все-таки +;;; решились вам поможет "Art of the Metaobject Protocol" + +(defclass bicycle (human-powered-conveyance) + ((wheel-size + :accessor wheel-size + :initarg :wheel-size + :documentation "Diameter of the wheel.") + (height + :accessor height + :initarg :height))) + +(defclass recumbent (bicycle) + ((chain-type + :accessor chain-type + :initarg :chain-type))) + +(defclass unicycle (human-powered-conveyance) nil) + +(defclass canoe (human-powered-conveyance) + ((number-of-rowers + :accessor number-of-rowers + :initarg :number-of-rowers))) + +;;; Если вызвать DESCRIBE для HUMAN-POWERED-CONVEYANCE то получите следующее: + +(describe 'human-powered-conveyance) + +; COMMON-LISP-USER::HUMAN-POWERED-CONVEYANCE +; [symbol] +; +; HUMAN-POWERED-CONVEYANCE names the standard-class #<STANDARD-CLASS +; HUMAN-POWERED-CONVEYANCE>: +; Documentation: +; A human powered conveyance +; Direct superclasses: STANDARD-OBJECT +; Direct subclasses: UNICYCLE, BICYCLE, CANOE +; Not yet finalized. +; Direct slots: +; VELOCITY +; Readers: VELOCITY +; Writers: (SETF VELOCITY) +; AVERAGE-EFFICIENCY +; Readers: AVERAGE-EFFICIENCY +; Writers: (SETF AVERAGE-EFFICIENCY) + +;;; CL задизайнен как интерактивная система. В рантайме доступна информация о +;;; типе объектов. + +;;; Давайте посчитаем расстояние, которое пройдет велосипед за один оборот колеса +;;; по формуле C = d * pi + +(defmethod circumference ((object bicycle)) + (* pi (wheel-size object))) + +;;; PI - это константа в CL + +;;; Предположим мы нашли, что критерий эффективности логарифмически связан +;;; с гребцами каноэ. Тогда вычисление можем сделать сразу при инициализации. + +;;; Инициализируем объект после его создания: + +(defmethod initialize-instance :after ((object canoe) &rest args) + (setf (average-efficiency object) (log (1+ (number-of-rowers object))))) + + +;;; Давайте проверим что получилось с этой самой эффективностью... + +(average-efficiency (make-instance 'canoe :number-of-rowers 15)) +; => 2.7725887 + + +;;;----------------------------------------------------------------------------- +;;; 8. Макросы +;;;----------------------------------------------------------------------------- + +;;; Макросы позволяют расширить синаксис языка. В CL нет например цикла WHILE, +;;; но его проще простого реализовать на макросах. Если мы отбросим наши +;;; ассемблерные (или алгольные) инстинкты, мы взлетим на крыльях: + +(defmacro while (condition &body body) + "Пока `условие` истинно, выполняется `тело`. +`Условие` проверяется перед каждым выполнением `тела`" + (let ((block-name (gensym)) (done (gensym))) + `(tagbody + ,block-name + (unless ,condition + (go ,done)) + (progn + ,@body) + (go ,block-name) + ,done))) + +;;; Взглянем на более высокоуровневую версию этого макроса: + +(defmacro while (condition &body body) + "Пока `условие` истинно, выполняется `тело`. +`Условие` проверяется перед каждым выполнением `тела`" + `(loop while ,condition + do + (progn + ,@body))) + +;;; В современных комиляторах LOOP так же эффективен как и приведенный +;;; выше код. Поэтому используйте его, его проще читать. + +;;; В макросах используются символы ```, `,` и `@`. ``` - это оператор +;;; квазиквотирования - это значит что форма исполнятся не будет, а вернется +;;; как данные. Оператаор `,` позволяет исполнить форму внутри +;;; квазиквотирования. Оператор `@` исполняет форму внутри квазиквотирования +;;; но полученный список вклеивает по месту. + +;;; GENSYM создаёт уникальный символ, который гарантировано больше нигде в +;;; системе не используется. Так надо потому, что макросы разворачиваются +;;; во время компиляции и переменные объявленные в макросе могут совпасть +;;; по имени с переменными в обычном коде. + +;;; Дальнйешую информацию о макросах ищите в книгах Practical Common Lisp +;;; и On Lisp +``` + +## Для чтения + +На русском +- [Practical Common Lisp](http://www.gigamonkeys.com/book/) + +На английском +- [Practical Common Lisp](http://www.gigamonkeys.com/book/) +- [Common Lisp: A Gentle Introduction to Symbolic Computation](https://www.cs.cmu.edu/~dst/LispBook/book.pdf) + + +## Дополнительная информация + +На русском + +- [Lisper.ru](http://lisper.ru/) + +На английском + +- [CLiki](http://www.cliki.net/) +- [common-lisp.net](https://common-lisp.net/) +- [Awesome Common Lisp](https://github.com/CodyReichert/awesome-cl) +- [Lisp Lang](http://lisp-lang.org/) + + +## Благодарности в английской версии + +Спасибо людям из Scheme за отличную статью, взятую за основу для +Common Lisp. + + +- [Paul Khuong](https://github.com/pkhuong) за хорошую вычитку. diff --git a/ru-ru/jquery-ru.html.markdown b/ru-ru/jquery-ru.html.markdown new file mode 100644 index 00000000..471b4e24 --- /dev/null +++ b/ru-ru/jquery-ru.html.markdown @@ -0,0 +1,127 @@ +--- +category: tool +tool: jquery +contributors: + - ["Sawyer Charles", "https://github.com/xssc"] +translators: + - ["Ev Bogdanov", "https://github.com/evbogdanov"] +lang: ru-ru +filename: jquery-ru.js +--- + +jQuery — это библиотека JavaScript, которая помогает "делать больше, писать меньше". Она выполняет множество типичных JavaScript-задач, упрощая написание кода. jQuery используется крупными компаниями и разработчиками со всего мира. Она упрощает и ускоряет работу с AJAX, с событиями, с DOM и со многим другим. + +Поскольку jQuery является библиотекой JavaScript, вам следует начать с [изучения JavaScript](https://learnxinyminutes.com/docs/ru-ru/javascript-ru/). + +```js + + +/////////////////////////////////// +// 1. Селекторы + +// Для получения элемента в jQuery используются селекторы +var page = $(window); // Получить страницу целиком + +// В качестве селектора может выступать CSS-селектор +var paragraph = $('p'); // Получить все <p> элементы +var table1 = $('#table1'); // Получить элемент с идентификатором 'table1' +var squares = $('.square'); // Получить все элементы с классом 'square' +var square_p = $('p.square') // Получить <p> элементы с классом 'square' + + +/////////////////////////////////// +// 2. События и эффекты +// jQuery прекрасно справляется с обработкой событий +// Часто используемое событие — это событие документа 'ready' +// Вы можете использовать метод 'ready', который сработает, как только документ полностью загрузится +$(document).ready(function(){ + // Код не выполнится до тех пор, пока документ не будет загружен +}); +// Обработку события можно вынести в отдельную функцию +function onAction() { + // Код выполнится, когда произойдёт событие +} +$('#btn').click(onAction); // Обработчик события сработает при клике + +// Другие распространённые события: +$('#btn').dblclick(onAction); // Двойной клик +$('#btn').hover(onAction); // Наведение курсора +$('#btn').focus(onAction); // Фокус +$('#btn').blur(onAction); // Потеря фокуса +$('#btn').submit(onAction); // Отправка формы +$('#btn').select(onAction); // Когда выбрали элемент +$('#btn').keydown(onAction); // Когда нажали клавишу +$('#btn').keyup(onAction); // Когда отпустили клавишу +$('#btn').keypress(onAction); // Когда нажали символьную клавишу (нажатие привело к появлению символа) +$('#btn').mousemove(onAction); // Когда переместили курсор мыши +$('#btn').mouseenter(onAction); // Когда навели курсор на элемент +$('#btn').mouseleave(onAction); // Когда сдвинули курсор с элемента + + +// Вы можете не только обрабатывать события, но и вызывать их +$('#btn').dblclick(); // Вызвать двойной клик на элементе + +// Для одного селектора возможно назначить несколько обработчиков событий +$('#btn').on( + {dblclick: myFunction1} // Обработать двойной клик + {blur: myFunction1} // Обработать исчезновение фокуса +); + +// Вы можете перемещать и прятать элементы с помощью методов-эффектов +$('.table').hide(); // Спрятать элемент(ы) + +// Обратите внимание: вызов функции в этих методах всё равно спрячет сам элемент +$('.table').hide(function(){ + // Сначала спрятать элемент, затем вызвать функцию +}); + +// Вы можете хранить селекторы в переменных +var tables = $('.table'); + +// Некоторые основные методы для манипуляций с документом: +tables.hide(); // Спрятать элемент(ы) +tables.show(); // Показать элемент(ы) +tables.toggle(); // Спрятать/показать +tables.fadeOut(); // Плавное исчезновение +tables.fadeIn(); // Плавное появление +tables.fadeToggle(); // Плавное исчезновение или появление +tables.fadeTo(0.5); // Изменение прозрачности +tables.slideUp(); // Свернуть элемент +tables.slideDown(); // Развернуть элемент +tables.slideToggle(); // Свернуть или развернуть + +// Все эти методы принимают скорость (в миллисекундах) и функцию обратного вызова +tables.hide(1000, myFunction); // Анимация длится 1 секунду, затем вызов функции + +// В методе 'fadeTo' вторым параметром обязательно идёт прозрачность +tables.fadeTo(2000, 0.1, myFunction); // Прозрачность меняется в течение 2 секунд до 0.1, затем вызывается функция + +// Метод 'animate' позволяет делать более продвинутую анимацию +tables.animate({"margin-top": "+=50", height: "100px"}, 500, myFunction); + + +/////////////////////////////////// +// 3. Манипуляции + +// Манипуляции похожи на эффекты, но позволяют добиться большего +$('div').addClass('taming-slim-20'); // Добавить класс 'taming-slim-20' ко всем <div> элементам + +// Часто встречающиеся методы манипуляций +$('p').append('Hello world'); // Добавить в конец элемента +$('p').attr('class'); // Получить атрибут +$('p').attr('class', 'content'); // Установить атрибут +$('p').hasClass('taming-slim-20'); // Проверить наличие класса +$('p').height(); // Получить или установить высоту элемента + + +// Во многих методах вам доступна информация ТОЛЬКО о первом элементе из выбранных +$('p').height(); // Вы получите высоту только для первого <p> элемента + +// Метод 'each' позволяет это исправить и пройтись по всем выбранным вами элементам +var heights = []; +$('p').each(function() { + heights.push($(this).height()); // Добавить высоту всех <p> элементов в массив +}); + + +``` diff --git a/ru-ru/markdown-ru.html.markdown b/ru-ru/markdown-ru.html.markdown index ff7a0cc3..579a9a20 100644 --- a/ru-ru/markdown-ru.html.markdown +++ b/ru-ru/markdown-ru.html.markdown @@ -36,13 +36,14 @@ lang: ru-ru Markdown является надмножеством HTML, поэтому любой HTML-файл является корректным документом Markdown. - ```markdown + ```md <!-- Это позволяет использовать напрямую любые элементы HTML-разметки, такие, например, как этот комментарий. Встроенные в документ HTML-элементы не затрагиваются парсером Markdown и попадают в итоговый HTML без изменений. Однако следует понимать, что эта же особенность не позволяет использовать разметку Markdown внутри HTML-элементов --> +``` ## Заголовки @@ -50,7 +51,7 @@ HTML-элементы от <h1> до <h6> размечаются очень пр текст, который должен стать заголовком, предваряется соответствующим количеством символов "#": -```markdown +```md # Это заголовок h1 ## Это заголовок h2 ### Это заголовок h3 @@ -60,7 +61,7 @@ HTML-элементы от <h1> до <h6> размечаются очень пр ``` Markdown позволяет размечать заголовки <h1> и <h2> ещё одним способом: -```markdown +```md Это заголовок h1 ================ @@ -72,7 +73,7 @@ Markdown позволяет размечать заголовки <h1> и <h2> Текст легко сделать полужирным и/или курсивным: -```markdown +```md *Этот текст будет выведен курсивом.* _Так же, как этот._ @@ -87,7 +88,7 @@ __И этот тоже.__ В Github Flavored Markdown, стандарте, который используется в Github, текст также можно сделать зачёркнутым: -```markdown +```md ~~Зачёркнутый текст.~~ ``` @@ -96,7 +97,7 @@ __И этот тоже.__ Абзацами являются любые строки, следующие друг за другом. Разделяются же абзацы одной или несколькими пустыми строками: -```markdown +```md Это абзац. Я печатаю в абзаце, разве это не прикольно? А тут уже абзац №2. @@ -108,7 +109,7 @@ __И этот тоже.__ Для вставки принудительных переносов можно завершить абзац двумя дополнительными пробелами: -```markdown +```md Эта строка завершается двумя пробелами (выделите, чтобы увидеть!). Над этой строкой есть <br />! @@ -116,7 +117,7 @@ __И этот тоже.__ Цитаты размечаются с помощью символа «>»: -```markdown +```md > Это цитата. В цитатах можно > принудительно переносить строки, вставляя «>» в начало каждой следующей строки. А можно просто оставлять их достаточно длинными, и такие длинные строки будут перенесены автоматически. > Разницы между этими двумя подходами к переносу строк нет, коль скоро @@ -133,7 +134,7 @@ __И этот тоже.__ одного из символов «*», «+» или «-»: (символ должен быть одним и тем же для всех элементов) -```markdown +```md * Список, * Размеченный * Звёздочками @@ -154,7 +155,7 @@ __И этот тоже.__ В нумерованных списках каждая строка начинается с числа и точки вслед за ним: -```markdown +```md 1. Первый элемент 2. Второй элемент 3. Третий элемент @@ -164,7 +165,7 @@ __И этот тоже.__ любое число в начале каждого элемента, и парсер пронумерует элементы сам! Правда, злоупотреблять этим не стоит :) -```markdown +```md 1. Первый элемент 1. Второй элемент 1. Третий элемент @@ -173,7 +174,7 @@ __И этот тоже.__ Списки могут быть вложенными: -```markdown +```md 1. Введение 2. Начало работы 3. Примеры использования @@ -184,7 +185,7 @@ __И этот тоже.__ Можно даже делать списки задач. Блок ниже создаёт HTML-флажки. -```markdown +```md Для отметки флажка используйте «x» - [ ] Первая задача - [ ] Вторая задача @@ -197,7 +198,7 @@ __И этот тоже.__ Фрагменты исходного кода (обычно отмечаемые тегом `<code>`) выделяются просто: каждая строка блока должна иметь отступ в четыре пробела либо в один символ табуляции. -```markdown +```md Это код, причём многострочный ``` @@ -205,7 +206,7 @@ __И этот тоже.__ Вы также можете делать дополнительные отступы, добавляя символы табуляции или по четыре пробела: -```markdown +```md my_array.each do |item| puts item end @@ -215,7 +216,7 @@ __И этот тоже.__ не выделяя код в блок. Для этого фрагменты кода нужно обрамлять символами «`»: -```markdown +```md Ваня даже не знал, что делает функция `go_to()`! ``` @@ -237,7 +238,7 @@ end Разделители (`<hr>`) добавляются вставкой строки из трёх и более (одинаковых) символов «*» или «-», с пробелами или без них: -```markdown +```md *** --- - - - @@ -251,18 +252,18 @@ end текст ссылки, заключив его в квадратные скобки, и сразу после — URL-адрес, заключенный в круглые -```markdown +```md [Ссылка!](http://test.com/) ``` Также для ссылки можно указать всплывающую подсказку (`title`), используя кавычки внутри круглых скобок: -```markdown +```md [Ссылка!](http://test.com/ "Ссылка на Test.com") ``` Относительные пути тоже возможны: -```markdown +```md [Перейти к музыке](/music/). ``` @@ -290,7 +291,7 @@ Markdown также позволяет размечать ссылку в вид Разметка изображений очень похожа на разметку ссылок. Нужно всего лишь добавить перед ссылкой восклицательный знак! -```markdown +```md ![Альтернативный текст для изображения](http://imgur.com/myimage.jpg "Подсказка") ``` Изображения тоже могут быть оформлены, как сноски. @@ -301,20 +302,20 @@ Markdown также позволяет размечать ссылку в вид ## Разное ### Автоссылки -```markdown +```md Ссылка вида <http://testwebsite.com/> эквивалентна [http://testwebsite.com/](http://testwebsite.com/) ``` ### Автоссылки для адресов электронной почты -```markdown +```md <foo@bar.com> ``` ### Экранирование символов -```markdown +```md Я хочу напечатать *текст, заключённый в звёздочки*, но я не хочу, чтобы он был курсивным. Тогда я делаю так: \*Текст, заключённый в звёздочки\* @@ -324,7 +325,7 @@ Markdown также позволяет размечать ссылку в вид В Github Flavored Markdown для представления клавиш на клавиатуре вы можете использовать тег `<kbd>`. -```markdown +```md Ваш компьютер завис? Попробуйте нажать <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd> ``` @@ -334,7 +335,7 @@ Markdown также позволяет размечать ссылку в вид да и синтаксис имеют не слишком удобный. Но если очень нужно, размечайте таблицы так: -```markdown +```md | Столбец 1 | Столбец 2 | Столбец 3 | | :----------- | :----------: | -----------: | | Выравнивание | Выравнивание | Выравнивание | @@ -342,7 +343,7 @@ Markdown также позволяет размечать ссылку в вид ``` Или более компактно -```markdown +```md Столбец 1|Столбец 2|Столбец 3 :--|:-:|--: Выглядит|это|страшновато... diff --git a/ru-ru/rust-ru.html.markdown b/ru-ru/rust-ru.html.markdown new file mode 100644 index 00000000..7bd2809a --- /dev/null +++ b/ru-ru/rust-ru.html.markdown @@ -0,0 +1,316 @@ +--- +language: rust + +filename: learnrust-ru.rs +contributors: + - ["P1start", "http://p1start.github.io/"] +translators: + - ["Anatolii Kosorukov", "https://github.com/java1cprog"] +lang: ru-ru + +--- + +Rust сочетает в себе низкоуровневый контроль над производительностью с удобством высокого уровня и предоставляет гарантии +безопасности. +Он достигает этих целей, не требуя сборщика мусора или времени выполнения, что позволяет использовать библиотеки Rust как замену +для C-библиотек. + +Первый выпуск Rust, 0.1, произошел в январе 2012 года, и в течение 3 лет развитие продвигалось настолько быстро, что до +недавнего времени использование стабильных выпусков было затруднено, и вместо этого общий совет заключался в том, чтобы +использовать последние сборки. + +15 мая 2015 года был выпущен Rust 1.0 с полной гарантией обратной совместимости. Усовершенствования времени компиляции и +других аспектов компилятора в настоящее время доступны в ночных сборках. Rust приняла модель выпуска на поезде с регулярными выпусками каждые шесть недель. Rust 1.1 beta был доступен одновременно с выпуском Rust 1.0. + +Хотя Rust является языком относительно низкого уровня, Rust имеет некоторые функциональные концепции, которые обычно +встречаются на языках более высокого уровня. Это делает Rust не только быстрым, но и простым и эффективным для ввода кода. + + +```rust +// Это однострочный комментарии +// + +/// Так выглядит комментарий для документации +/// # Examples +/// +/// +/// let seven = 7 +/// + +/////////////// +// 1. Основы // +/////////////// + +// Функции +// `i32` это целочисленный знаковый тип 32-bit +#[allow(dead_code)] +fn add2(x: i32, y: i32) -> i32 { + // метод возвращает сумму x и y + x + y +} + +// Главная функция программы +#[allow(unused_variables)] +#[allow(unused_assignments)] +#[allow(dead_code)] +fn main() { + // Числа // + + // неизменяемая переменная + let x: i32 = 1; + + // Суффиксы целое/дробное + let y: i32 = 13i32; + let f: f64 = 1.3f64; + + // Автоматическое выявление типа данных + // В большинстве случаев компилятор Rust может вычислить + // тип переменной, поэтому + // вам не нужно писать явные аннотации типа. + + let implicit_x = 1; + let implicit_f = 1.3; + + // Арифметика + let sum = x + y + 13; + + // Изменяемая переменная + let mut mutable = 1; + mutable = 4; + mutable += 2; + + // Строки // + + // Строковые литералы + let x: &str = "hello world!"; + + // Печать на консоль + println!("{} {}", f, x); // 1.3 hello world + + // `String` – изменяемя строка + let s: String = "hello world".to_string(); + + // Строковый срез - неизменяемый вид в строки + // Это в основном неизменяемая пара указателей на строку - + // Это указатель на начало и конец строкового буфера + + let s_slice: &str = &s; + + println!("{} {}", s, s_slice); // hello world hello world + + // Vectors/arrays // + + // фиксированный массив + let four_ints: [i32; 4] = [1, 2, 3, 4]; + + // динамический массив + let mut vector: Vec<i32> = vec![1, 2, 3, 4]; + vector.push(5); + + // Срез - неизменяемое представление значений вектора + let slice: &[i32] = &vector; + + // Используйте шаблон `{:?}`для печати отладочной информации структур с данными + println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] + + // Кортежи // + + // Кортеж - это фиксированный набор. + // В нём могут находиться значения разных типов данных. + let x: (i32, &str, f64) = (1, "hello", 3.4); + + // Инициализация группы переменных `let` + let (a, b, c) = x; + println!("{} {} {}", a, b, c); // 1 hello 3.4 + + // Доступ по индексу + println!("{}", x.1); // hello + + ////////////// + // 2. Типы // + ////////////// + + // Struct + struct Point { + x: i32, + y: i32, + } + + let origin: Point = Point { x: 0, y: 0 }; + + // Структуры могут быть с безымянными полями ‘tuple struct’ + struct Point2(i32, i32); + + let origin2 = Point2(0, 0); + + // Перечисление + enum Direction { + Left, + Right, + Up, + Down, + } + + let up = Direction::Up; + + // Перечисление с полями + enum OptionalI32 { + AnI32(i32), + Nothing, + } + + let two: OptionalI32 = OptionalI32::AnI32(2); + let nothing = OptionalI32::Nothing; + + // Обобщенные типы данных // + + struct Foo<T> { bar: T } + + // Частоиспользуемое перечисление стандартной библиотеки `Option` + enum Optional<T> { + SomeVal(T), + NoVal, + } + + // Методы // + + impl<T> Foo<T> { + fn get_bar(self) -> T { + self.bar + } + } + + let a_foo = Foo { bar: 1 }; + println!("{}", a_foo.get_bar()); // 1 + + // Типаж + + trait Frobnicate<T> { + fn frobnicate(self) -> Option<T>; + } + + impl<T> Frobnicate<T> for Foo<T> { + fn frobnicate(self) -> Option<T> { + Some(self.bar) + } + } + + let another_foo = Foo { bar: 1 }; + println!("{:?}", another_foo.frobnicate()); // Some(1) + + ///////////////////////// + // 3. Поиск по шаблону // + ///////////////////////// + + let foo = OptionalI32::AnI32(1); + match foo { + OptionalI32::AnI32(n) => println!("it’s an i32: {}", n), + OptionalI32::Nothing => println!("it’s nothing!"), + } + + // Более сложный пример + struct FooBar { x: i32, y: OptionalI32 } + let bar = FooBar { x: 15, y: OptionalI32::AnI32(32) }; + + match bar { + FooBar { x: 0, y: OptionalI32::AnI32(0) } => + println!("The numbers are zero!"), + FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m => + println!("The numbers are the same"), + FooBar { x: n, y: OptionalI32::AnI32(m) } => + println!("Different numbers: {} {}", n, m), + FooBar { x: _, y: OptionalI32::Nothing } => + println!("The second number is Nothing!"), + } + + ///////////////////// + // 4. Управление ходом выполнения программы // + ///////////////////// + + // `for` loops/iteration + let array = [1, 2, 3]; + for i in array.iter() { + println!("{}", i); + } + + // Отрезки + for i in 0u32..10 { + print!("{} ", i); + } + println!(""); + // prints `0 1 2 3 4 5 6 7 8 9 ` + + // `if` + if 1 == 1 { + println!("Maths is working!"); + } else { + println!("Oh no..."); + } + + // `if` as expression + let value = if true { + "good" + } else { + "bad" + }; + + // `while` loop + while 1 == 1 { + println!("The universe is operating normally."); + break; + } + + // Infinite loop + loop { + println!("Hello!"); + break; + } + + ///////////////////////////////// + // 5. Защита памяти и указатели // + ///////////////////////////////// + + // Владеющий указатель – такой указатель может быть только один + // Это значит, что при вызоде из блока переменная автоматически становится недействительной. + let mut mine: Box<i32> = Box::new(3); + *mine = 5; // dereference + // Здесь, `now_its_mine` получает во владение `mine`. Т.е. `mine` была перемещена. + let mut now_its_mine = mine; + *now_its_mine += 2; + + println!("{}", now_its_mine); // 7 + // println!("{}", mine); + + // Ссылки - это неизменяемые указатели + // Если ссылка получает значения, то говорят, что она заимствует это значение. + // Такое значение не может быть изменено или перемещено. + let mut var = 4; + var = 3; + let ref_var: &i32 = &var; + + println!("{}", var); + println!("{}", *ref_var); + // var = 5; // не скомпилируется + // *ref_var = 6; // и это + + // Изменяемые ссылки + // + let mut var2 = 4; + let ref_var2: &mut i32 = &mut var2; + *ref_var2 += 2; // '*' используется для изменения значения + + println!("{}", *ref_var2); // 6 , // var2 would not compile. + // ref_var2 имеет тип &mut i32, т.е. он содержит ссылку на i32, а не значение. + // var2 = 2; // не скомпилируется, т.к. эта переменная уже была заимствована ранее +} + +``` + +## Более подробная информация о языке + +Уже есть хорошие книги для изучающих Rust. Основным источником остаётся +[The Rust Programming Language](http://doc.rust-lang.org/book/index.html) + +Для компиляции программ при изучении языка весьма удобно использовать +[Rust playpen](http://play.rust-lang.org). +Множество ресурсов на разных языках можно найти в [этом проекте](https://github.com/ctjhoa/rust-learning). diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown index 50eedcd0..3c80075b 100644 --- a/ruby-ecosystem.html.markdown +++ b/ruby-ecosystem.html.markdown @@ -10,6 +10,16 @@ contributors: People using Ruby generally have a way to install different Ruby versions, manage their packages (or gems), and manage their gem dependencies. +## Ruby Versions + +Ruby was created by Yukihiro "Matz" Matsumoto, who remains somewhat of a +[BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life), although +that is changing recently. As a result, the reference implementation of Ruby is +called MRI (Matz' Reference Implementation), and when you hear a Ruby version, +it is referring to the release version of MRI. + +New major versions of Ruby are traditionally released on Christmas Day. The current major version (25 December 2017) is 2.5. The most popular stable versions are 2.4.4 and 2.3.7 (both released 28 March 2018). + ## Ruby Managers Some platforms have Ruby pre-installed or available as a package. Most rubyists @@ -29,28 +39,6 @@ The following are the popular Ruby environment managers: * [chruby](https://github.com/postmodern/chruby) - Only switches between rubies. Similar in spirit to rbenv. Unopinionated about how rubies are installed. -## Ruby Versions - -Ruby was created by Yukihiro "Matz" Matsumoto, who remains somewhat of a -[BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life), although -that is changing recently. As a result, the reference implementation of Ruby is -called MRI (Matz' Reference Implementation), and when you hear a Ruby version, -it is referring to the release version of MRI. - -The three major version of Ruby in use are: - -* 2.0.0 - Released in February 2013. Most major libraries and frameworks support - 2.0.0. -* 1.9.3 - Released in October 2011. This is the version most rubyists use - currently. Also [retired](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/) -* 1.8.7 - Ruby 1.8.7 has been - [retired](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/). - -The change between 1.8.7 to 1.9.x is a much larger change than 1.9.3 to 2.0.0. -For instance, the 1.9 series introduced encodings and a bytecode VM. There -are projects still on 1.8.7, but they are becoming a small minority, as most of -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 diff --git a/ruby.html.markdown b/ruby.html.markdown index e0a6bb6e..2f4d0934 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -15,32 +15,29 @@ contributors: - ["Gabriel Halley", "https://github.com/ghalley"] - ["Persa Zula", "http://persazula.com"] - ["Jake Faris", "https://github.com/farisj"] + - ["Corey Ward", "https://github.com/coreyward"] + - ["Jannik Siebert", "https://github.com/janniks"] --- ```ruby # This is a comment -=begin -This is a multiline comment -No-one uses them -You shouldn't either -=end +# In Ruby, (almost) everything is an object. +# This includes numbers... +3.class #=> Integer -# First and foremost: Everything is an object. - -# Numbers are objects - -3.class #=> Fixnum - -3.to_s #=> "3" +# ...and strings... +"Hello".class #=> String +# ...and even methods! +"Hello".method(:class).class #=> Method # Some basic arithmetic 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 -2**5 #=> 32 +2 ** 5 #=> 32 5 % 3 #=> 2 # Bitwise operators @@ -52,6 +49,7 @@ You shouldn't either # for calling a method on an object 1.+(3) #=> 4 10.* 5 #=> 50 +100.methods.include?(:/) #=> true # Special values are objects nil # equivalent to null in other languages @@ -70,11 +68,12 @@ false.class #=> FalseClass 1 != 1 #=> false 2 != 1 #=> true -# apart from false itself, nil is the only other 'falsey' value +# Apart from false itself, nil is the only other 'falsey' value -!nil #=> true -!false #=> true -!0 #=> false +!!nil #=> false +!!false #=> false +!!0 #=> true +!!"" #=> true # More comparisons 1 < 10 #=> true @@ -82,7 +81,8 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true -# Combined comparison operator +# Combined comparison operator (returns `1` when the first argument is greater, +# `-1` when the second argument is greater, and `0` otherwise) 1 <=> 10 #=> -1 10 <=> 1 #=> 1 1 <=> 1 #=> 0 @@ -90,7 +90,6 @@ false.class #=> FalseClass # Logical operators true && false #=> false true || false #=> true -!true #=> false # There are alternate versions of the logical operators with much lower # precedence. These are meant to be used as flow-control constructs to chain @@ -101,61 +100,54 @@ do_something() and do_something_else() # `log_error` only called if `do_something` fails. do_something() or log_error() - -# Strings are objects - -'I am a string'.class #=> String -"I am a string too".class #=> String +# String interpolation placeholder = 'use string interpolation' "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" -# Prefer single quoted strings to double quoted ones where possible -# Double quoted strings perform additional inner calculations - -# Combine strings, but not with numbers +# You can combine strings using `+`, but not with other types 'hello ' + 'world' #=> "hello world" 'hello ' + 3 #=> TypeError: can't convert Fixnum into String 'hello ' + 3.to_s #=> "hello 3" +"hello #{3}" #=> "hello 3" -# Combine strings and operators +# ...or combine strings and operators 'hello ' * 3 #=> "hello hello hello " -# Append to string +# ...or append to string 'hello' << ' world' #=> "hello world" -# print to the output with a newline at the end +# You can print to the output with a newline at the end puts "I'm printing!" #=> I'm printing! #=> nil -# print to the output without a newline +# ...or print to the output without a newline print "I'm printing!" -#=> I'm printing! => nil +#=> "I'm printing!" => nil # Variables x = 25 #=> 25 x #=> 25 -# Note that assignment returns the value assigned -# This means you can do multiple assignment: +# Note that assignment returns the value assigned. +# This means you can do multiple assignment. x = y = 10 #=> 10 x #=> 10 y #=> 10 -# By convention, use snake_case for variable names +# By convention, use snake_case for variable names. snake_case = true # Use descriptive variable names path_to_project_root = '/good/name/' -path = '/bad/name/' +m = '/bad/name/' -# Symbols (are objects) # Symbols are immutable, reusable constants represented internally by an # integer value. They're often used instead of strings to efficiently convey -# specific, meaningful values +# specific, meaningful values. :pending.class #=> Symbol @@ -167,77 +159,82 @@ status == 'pending' #=> false status == :approved #=> false +# Strings can be converted into symbols and vice versa. +status.to_s #=> "pending" +"argon".to_sym #=> :argon + # Arrays -# This is an array +# This is an array. array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] -# Arrays can contain different types of items - +# Arrays can contain different types of items. [1, 'hello', false] #=> [1, "hello", false] -# Arrays can be indexed -# From the front +# Arrays can be indexed. +# From the front... array[0] #=> 1 array.first #=> 1 array[12] #=> nil -# Like arithmetic, [var] access -# is just syntactic sugar -# for calling a method [] on an object -array.[] 0 #=> 1 -array.[] 12 #=> nil - -# From the end +# ...or from the back... array[-1] #=> 5 array.last #=> 5 -# With a start index and length +# ...or with a start index and length... array[2, 3] #=> [3, 4, 5] -# Reverse an Array -a=[1,2,3] +# ...or with a range... +array[1..3] #=> [2, 3, 4] + +# You can reverse an Array. +a = [1,2,3] a.reverse! #=> [3,2,1] -# Or with a range -array[1..3] #=> [2, 3, 4] +# Like arithmetic, [var] access is just syntactic sugar +# for calling a method '[]' on an object. +array.[] 0 #=> 1 +array.[] 12 #=> nil -# Add to an array like this +# You can add to an array... 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 +# ...and check if an item exists in an array array.include?(1) #=> true # Hashes are Ruby's primary dictionary with key/value pairs. -# Hashes are denoted with curly braces: +# Hashes are denoted with curly braces. hash = { 'color' => 'green', 'number' => 5 } hash.keys #=> ['color', 'number'] -# Hashes can be quickly looked up by key: -hash['color'] #=> 'green' +# Hashes can be quickly looked up by key. +hash['color'] #=> "green" hash['number'] #=> 5 -# Asking a hash for a key that doesn't exist returns nil: +# Asking a hash for a key that doesn't exist returns nil. hash['nothing here'] #=> nil -# Since Ruby 1.9, there's a special syntax when using symbols as keys: +# When using symbols for keys in a hash, you can use an alternate syntax. -new_hash = { defcon: 3, action: true } +hash = { :defcon => 3, :action => true } +hash.keys #=> [:defcon, :action] -new_hash.keys #=> [:defcon, :action] +hash = { defcon: 3, action: true } +hash.keys #=> [:defcon, :action] # Check existence of keys and values in hash -new_hash.key?(:defcon) #=> true -new_hash.value?(3) #=> true +hash.key?(:defcon) #=> true +hash.value?(3) #=> true -# Tip: Both Arrays and Hashes are Enumerable -# They share a lot of useful methods such as each, map, count, and more +# Tip: Both Arrays and Hashes are Enumerable! +# They share a lot of useful methods such as each, map, count, and more. # Control structures +# Conditionals if true 'if statement' elsif false @@ -246,35 +243,26 @@ else 'else, also optional' end +# Loops +# In Ruby, traditional `for` loops aren't very common. Instead, these +# basic loops are implemented using enumerable, which hinges on `each`. +(1..5).each do |counter| + puts "iteration #{counter}" +end + +# Which is roughly equivalent to the following, which is unusual to see in Ruby. for counter in 1..5 puts "iteration #{counter}" end -#=> iteration 1 -#=> iteration 2 -#=> iteration 3 -#=> iteration 4 -#=> iteration 5 -# HOWEVER, No-one uses for loops. -# Instead you should use the "each" method and pass it a block. -# A block is a bunch of code that you can pass to a method like "each". -# It is analogous to lambdas, anonymous functions or closures in other -# programming languages. +# The `do |variable| ... end` construct above is called a 'block'. Blocks are similar +# to lambdas, anonymous functions or closures in other programming languages. They can +# be passed around as objects, called, or attached as methods. # -# The "each" method of a range runs the block once for each element of the range. +# The 'each' method of a range runs the block once for each element of the range. # The block is passed a counter as a parameter. -# Calling the "each" method with a block looks like this: -(1..5).each do |counter| - puts "iteration #{counter}" -end -#=> iteration 1 -#=> iteration 2 -#=> iteration 3 -#=> iteration 4 -#=> iteration 5 - -# You can also surround blocks in curly brackets: +# You can also surround blocks in curly brackets. (1..5).each { |counter| puts "iteration #{counter}" } # The contents of data structures can also be iterated using each. @@ -285,8 +273,8 @@ hash.each do |key, value| puts "#{key} is #{value}" end -# If you still need an index you can use "each_with_index" and define an index -# variable +# If you still need an index you can use 'each_with_index' and define an index +# variable. array.each_with_index do |element, index| puts "#{element} is number #{index} in the array" end @@ -302,9 +290,9 @@ end #=> iteration 4 #=> iteration 5 -# There are a bunch of other helpful looping functions in Ruby, -# for example "map", "reduce", "inject", the list goes on. Map, -# for instance, takes the array it's looping over, does something +# There are a bunch of other helpful looping functions in Ruby. +# For example: 'map', 'reduce', 'inject', the list goes on. +# Map, for instance, takes the array it's looping over, does something # to it as defined in your block, and returns an entirely new array. array = [1,2,3,4,5] doubled = array.map do |element| @@ -315,6 +303,7 @@ puts doubled puts array #=> [1,2,3,4,5] +# Case construct grade = 'B' case grade @@ -333,7 +322,7 @@ else end #=> "Better luck next time" -# cases can also use ranges +# Cases can also use ranges grade = 82 case grade when 90..100 @@ -345,9 +334,9 @@ else end #=> "OK job" -# exception handling: +# Exception handling begin - # code here that might raise an exception + # Code here that might raise an exception raise NoMemoryError, 'You ran out of memory.' rescue NoMemoryError => exception_variable puts 'NoMemoryError was raised', exception_variable @@ -365,10 +354,10 @@ def double(x) x * 2 end -# Methods (and all blocks) implicitly return the value of the last statement +# Methods (and blocks) implicitly return the value of the last statement. double(2) #=> 4 -# Parentheses are optional where the result is unambiguous +# Parentheses are optional where the interpretation is unambiguous. double 3 #=> 6 double double 3 #=> 12 @@ -377,15 +366,14 @@ def sum(x, y) x + y end -# Method arguments are separated by a comma +# Method arguments are separated by a comma. sum 3, 4 #=> 7 sum sum(3, 4), 5 #=> 12 # yield -# All methods have an implicit, optional block parameter -# it can be called with the 'yield' keyword - +# All methods have an implicit, optional block parameter. +# Tt can be called with the 'yield' keyword. def surround puts '{' yield @@ -394,46 +382,78 @@ end surround { puts 'hello world' } -# { -# hello world -# } +#=> { +#=> hello world +#=> } - -# You can pass a block to a method -# "&" marks a reference to a passed block +# Blocks can be converted into a 'proc' object, which wraps the block +# and allows it to be passed to another method, bound to a different scope, +# or manipulated otherwise. This is most common in method parameter lists, +# where you frequently see a trailing '&block' parameter that will accept +# the block, if one is given, and convert it to a 'Proc'. The naming here is +# convention; it would work just as well with '&pineapple'. def guests(&block) - block.call 'some_argument' + block.class #=> Proc + block.call(4) end -# You can pass a list of arguments, which will be converted into an array -# That's what splat operator ("*") is for +# The 'call' method on the Proc is similar to calling 'yield' when a block is +# present. The arguments passed to 'call' will be forwarded to the block as arugments. + +guests { |n| "You have #{n} guests." } +# => "You have 4 guests." + +# You can pass a list of arguments, which will be converted into an array. +# That's what splat operator ("*") is for. def guests(*array) array.each { |guest| puts guest } end -# If a method returns an array, you can use destructuring assignment -def foods - ['pancake', 'sandwich', 'quesadilla'] +# Destructuring + +# Ruby will automatically destructure arrays on assignment to multiple variables. +a, b, c = [1, 2, 3] +a #=> 1 +b #=> 2 +c #=> 3 + +# In some cases, you will want to use the splat operator: `*` to prompt destructuring +# of an array into a list. +ranked_competitors = ["John", "Sally", "Dingus", "Moe", "Marcy"] + +def best(first, second, third) + puts "Winners are #{first}, #{second}, and #{third}." +end + +best *ranked_competitors.first(3) #=> Winners are John, Sally, and Dingus. + +# The splat operator can also be used in parameters. +def best(first, second, third, *others) + puts "Winners are #{first}, #{second}, and #{third}." + puts "There were #{others.count} other participants." end -breakfast, lunch, dinner = foods -breakfast #=> 'pancake' -dinner #=> 'quesadilla' -# By convention, all methods that return booleans end with a question mark -5.even? # false -5.odd? # true +best *ranked_competitors +#=> Winners are John, Sally, and Dingus. +#=> There were 2 other participants. -# And if a method ends with an exclamation mark, it does something destructive +# By convention, all methods that return booleans end with a question mark. +5.even? #=> false +5.odd? #=> true + +# By convention, if a method name ends with an exclamation mark, it does something destructive # like mutate the receiver. Many methods have a ! version to make a change, and -# a non-! version to just return a new changed version +# a non-! version to just return a new changed version. company_name = "Dunder Mifflin" company_name.upcase #=> "DUNDER MIFFLIN" company_name #=> "Dunder Mifflin" -company_name.upcase! # we're mutating company_name this time! +# We're mutating company_name this time. +company_name.upcase! #=> "DUNDER MIFFLIN" company_name #=> "DUNDER MIFFLIN" +# Classes -# Define a class with the class keyword +# You can define a class with the 'class' keyword. class Human # A class variable. It is shared by all instances of this class. @@ -441,7 +461,7 @@ class Human # Basic initializer def initialize(name, age = 0) - # Assign the argument to the "name" instance variable for the instance + # Assign the argument to the 'name' instance variable for the instance. @name = name # If no age given, we will fall back to the default in the arguments list. @age = age @@ -457,10 +477,10 @@ class Human @name end - # The above functionality can be encapsulated using the attr_accessor method as follows + # The above functionality can be encapsulated using the attr_accessor method as follows. attr_accessor :name - # Getter/setter methods can also be created individually like this + # Getter/setter methods can also be created individually like this. attr_reader :name attr_writer :name @@ -475,13 +495,11 @@ class Human end end - -# Instantiate a class +# Instantiating of a class jim = Human.new('Jim Halpert') - dwight = Human.new('Dwight K. Schrute') -# Let's call a couple of methods +# You can call the methods of the generated object. jim.species #=> "H. sapiens" jim.name #=> "Jim Halpert" jim.name = "Jim Halpert II" #=> "Jim Halpert II" @@ -489,30 +507,30 @@ jim.name #=> "Jim Halpert II" dwight.species #=> "H. sapiens" dwight.name #=> "Dwight K. Schrute" -# Call the class method +# Calling of a class method Human.say('Hi') #=> "Hi" # Variable's scopes are defined by the way we name them. -# Variables that start with $ have global scope +# Variables that start with $ have global scope. $var = "I'm a global var" defined? $var #=> "global-variable" -# Variables that start with @ have instance scope +# Variables that start with @ have instance scope. @var = "I'm an instance var" defined? @var #=> "instance-variable" -# Variables that start with @@ have class scope +# Variables that start with @@ have class scope. @@var = "I'm a class var" defined? @@var #=> "class variable" -# Variables that start with a capital letter are constants +# Variables that start with a capital letter are constants. Var = "I'm a constant" defined? Var #=> "constant" -# Class is also an object in ruby. So class can have instance variables. -# Class variable is shared among the class and all of its descendants. +# Class is also an object in ruby. So a class can have instance variables. +# A class variable is shared among the class and all of its descendants. -# base class +# Base class class Human @@foo = 0 @@ -525,18 +543,17 @@ class Human end end -# derived class +# Derived class class Worker < Human end -Human.foo # 0 -Worker.foo # 0 +Human.foo #=> 0 +Worker.foo #=> 0 -Human.foo = 2 # 2 -Worker.foo # 2 - -# Class instance variable is not shared by the class's descendants. +Human.foo = 2 +Worker.foo #=> 2 +# A class instance variable is not shared by the class's descendants. class Human @bar = 0 @@ -552,8 +569,8 @@ end class Doctor < Human end -Human.bar # 0 -Doctor.bar # nil +Human.bar #=> 0 +Doctor.bar #=> nil module ModuleExample def foo @@ -561,9 +578,8 @@ module ModuleExample end end -# Including modules binds their methods to the class instances -# Extending modules binds their methods to the class itself - +# Including modules binds their methods to the class instances. +# Extending modules binds their methods to the class itself. class Person include ModuleExample end @@ -572,13 +588,12 @@ class Book extend ModuleExample end -Person.foo # => NoMethodError: undefined method `foo' for Person:Class -Person.new.foo # => 'foo' -Book.foo # => 'foo' -Book.new.foo # => NoMethodError: undefined method `foo' +Person.foo #=> NoMethodError: undefined method `foo' for Person:Class +Person.new.foo #=> "foo" +Book.foo #=> "foo" +Book.new.foo #=> NoMethodError: undefined method `foo' # Callbacks are executed when including and extending a module - module ConcernExample def self.included(base) base.extend(ClassMethods) @@ -602,10 +617,10 @@ class Something include ConcernExample end -Something.bar # => 'bar' -Something.qux # => NoMethodError: undefined method `qux' -Something.new.bar # => NoMethodError: undefined method `bar' -Something.new.qux # => 'qux' +Something.bar #=> "bar" +Something.qux #=> NoMethodError: undefined method `qux' +Something.new.bar #=> NoMethodError: undefined method `bar' +Something.new.qux #=> "qux" ``` ## Additional resources diff --git a/rust.html.markdown b/rust.html.markdown index 6b75fa87..71bc16b5 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -41,6 +41,7 @@ Rust not only fast, but also easy and efficient to code in. // 1. Basics // /////////////// +#[allow(dead_code)] // Functions // `i32` is the type for 32-bit signed integers fn add2(x: i32, y: i32) -> i32 { @@ -48,6 +49,9 @@ fn add2(x: i32, y: i32) -> i32 { x + y } +#[allow(unused_variables)] +#[allow(unused_assignments)] +#[allow(dead_code)] // Main function fn main() { // Numbers // @@ -256,11 +260,16 @@ fn main() { // `while` loop while 1 == 1 { println!("The universe is operating normally."); + // break statement gets out of the while loop. + // It avoids useless iterations. + break } // Infinite loop loop { println!("Hello!"); + // break statement gets out of the loop + break } ///////////////////////////////// diff --git a/scala.html.markdown b/scala.html.markdown index 016e2b4f..28424684 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -716,7 +716,7 @@ import scala.collection.immutable.{Map => _, Set => _, _} // Java classes can also be imported. Scala syntax can be used import java.swing.{JFrame, JWindow} -// Your programs entry point is defined in an scala file using an object, with a +// Your programs entry point is defined in a scala file using an object, with a // single method, main: object Application { def main(args: Array[String]): Unit = { diff --git a/solidity.html.markdown b/solidity.html.markdown index a0f8cd40..004c225e 100644 --- a/solidity.html.markdown +++ b/solidity.html.markdown @@ -134,7 +134,7 @@ uint constant VERSION_ID = 0x123A1; // A hex constant // All state variables (those outside a function) // are by default 'internal' and accessible inside contract // and in all contracts that inherit ONLY -// Need to explicitly set to 'public' to allow external contracts to access +// Need to explicitly set to 'public' to allow external contracts to access int256 public a = 8; // For int and uint, can explicitly set space in steps of 8 up to 256 @@ -352,7 +352,7 @@ function increment(uint x) constant returns (uint x) { // y is a state variable, and can't be changed in a constant function } -// 'pure' is more strict than 'constant', and does not +// 'pure' is more strict than 'constant', and does not // even allow reading of state vars // The exact rules are more complicated, so see more about // constant/pure: @@ -384,7 +384,7 @@ function depositEther() public payable { // Prefer loops to recursion (max call stack depth is 1024) -// Also, don't setup loops that you haven't bounded, +// Also, don't setup loops that you haven't bounded, // as this can hit the gas limit // B. Events @@ -399,11 +399,11 @@ function depositEther() public payable { event LogSent(address indexed from, address indexed to, uint amount); // note capital first letter // Call -Sent(from, to, amount); +LogSent(from, to, amount); -// For an external party (a contract or external entity), to watch using +// For an external party (a contract or external entity), to watch using // the Web3 Javascript library: -Coin.Sent().watch({}, '', function(error, result) { +Coin.LogSent().watch({}, '', function(error, result) { if (!error) { console.log("Coin transfer: " + result.args.amount + " coins were sent from " + result.args.from + @@ -709,7 +709,7 @@ contract CrowdFunder { return contributions.length - 1; // return id } - function checkIfFundingCompleteOrExpired() + function checkIfFundingCompleteOrExpired() public { if (totalRaised > minimumToRaise) { @@ -829,7 +829,6 @@ someContractAddress.callcode('function_name'); ## Additional resources - [Solidity Docs](https://solidity.readthedocs.org/en/latest/) - [Smart Contract Best Practices](https://github.com/ConsenSys/smart-contract-best-practices) -- [Solidity Style Guide](https://ethereum.github.io/solidity//docs/style-guide/): Ethereum's style guide is heavily derived from Python's [pep8](https://www.python.org/dev/peps/pep-0008/) style guide. - [EthFiddle - The JsFiddle for Solidity](https://ethfiddle.com/) - [Browser-based Solidity Editor](https://remix.ethereum.org/) - [Gitter Solidity Chat room](https://gitter.im/ethereum/solidity) @@ -850,9 +849,10 @@ someContractAddress.callcode('function_name'); - [Hacking Distributed Blog](http://hackingdistributed.com/) ## Style -- Python's [PEP8](https://www.python.org/dev/peps/pep-0008/) is used as the baseline style guide, including its general philosophy +- [Solidity Style Guide](http://solidity.readthedocs.io/en/latest/style-guide.html): Ethereum's style guide is heavily derived from Python's [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guide. ## Editors +- [Emacs Solidity Mode](https://github.com/ethereum/emacs-solidity) - [Vim Solidity](https://github.com/tomlion/vim-solidity) - Editor Snippets ([Ultisnips format](https://gist.github.com/nemild/98343ce6b16b747788bc)) diff --git a/tcl.html.markdown b/tcl.html.markdown index 40d9111a..f48d5271 100644 --- a/tcl.html.markdown +++ b/tcl.html.markdown @@ -328,6 +328,7 @@ proc greet {greeting name} { # the third argument to "proc", is a string. The previous command # can be rewritten using no braces: proc greet greeting\ name return\ \"\$greeting,\ \$name!\" +# " diff --git a/toml.html.markdown b/toml.html.markdown index 980563f9..814e57e7 100755 --- a/toml.html.markdown +++ b/toml.html.markdown @@ -12,7 +12,7 @@ It is an alternative to YAML and JSON. It aims to be more human friendly than JS Be warned, TOML's spec is still changing a lot. Until it's marked as 1.0, you should assume that it is unstable and act accordingly. This document follows TOML v0.4.0. -```toml +``` # Comments in TOML look like this. ################ @@ -102,9 +102,10 @@ boolMustBeLowercase = true # Datetime # ############ -date1 = 1979-05-27T07:32:00Z # follows the RFC 3339 spec -date2 = 1979-05-27T07:32:00 # without offset -date3 = 1979-05-27 # without offset nor time +date1 = 1979-05-27T07:32:00Z # UTC time, following RFC 3339/ISO 8601 spec +date2 = 1979-05-26T15:32:00+08:00 # with RFC 3339/ISO 8601 offset +date3 = 1979-05-27T07:32:00 # without offset +date4 = 1979-05-27 # without offset or time #################### # COLLECTION TYPES # diff --git a/tr-tr/c++-tr.html.markdown b/tr-tr/c++-tr.html.markdown new file mode 100644 index 00000000..2c841456 --- /dev/null +++ b/tr-tr/c++-tr.html.markdown @@ -0,0 +1,1077 @@ +--- +language: c++ +lang: tr-tr +filename: learncpp-tr.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"] + - ["Ankush Goyal", "http://github.com/ankushg07"] + - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] + - ["Adem Budak", "https://github.com/p1v0t"] +--- + +C++ +[yaratıcısı Bjarne Stroustrup'a göre](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote), + +- "daha iyi bir C" yapmak +- veri soyutlamayı desteklemek +- nesneye yönelik programlamayı deskteklemek +- tipten bağımsız programlamayı desteklemek + +için tasarlanmış bir sistem programlama dilir. + +Sözdizimi daha yeni dillerden daha zor veya karmaşık olsa da işlemcinin doğrudan çalıştırabileceği +native komutlara derlenerek, donanım üzerinde (C gibi) sıkı bir kontrol sağlar, bunu yaparken +tipten bağımsızlık, exception'lar ve sınıflar gibi yüksek-seviyeli özellikleri destekler. +Bu hız ve kullanışlılık C++'ı en çok kullanılan dillerden biri yapar. + +```c++ +////////////////////// +// C ile karşılaştırma +////////////////////// + +// C++ _neredeyse_ C'nin bir üstkümesidir, değişken tanımı, basit tipleri +// ve fonksiyonları için temelde aynı sözdizimini paylaşır. + +// Aynı C gibi, programın başlangıç noktası bir integer döndüren +// main fonksiyonudur. +// Bu değer programın bitiş statüsünü belli eder. +// Daha fazla bilgi için bknz http://en.wikipedia.org/wiki/Exit_status . + +int main(int argc, char** argv) +{ + // Komut satırı argümanları C'de olduğu gibi argv ve argc ile geçilir + // argc, argüman sayısını belli eder, + // argv, argümanları belli eden, C-stili string'lerin (char*) dizisidir. + // İlk argüman çağrılan programın adıdır. + // Eğer argümanları umursamıyorsan, argv ve argc kullanılmayabilir + // int main() gibi + + // 0 çıkış durumu başarıyı belirtir. + return 0; +} + +// Bunlara rağmen C++ aşağıdaki noktalarda farklılaşır: + +// C++'ta, karakterler char türündendir +sizeof('c') == sizeof(char) == 1 + +// C'de, karakterler int türündendir +sizeof('c') == sizeof(int) + + +// C++ katı bir prototip kuralına sahiptir +void func(); // fonksiyon argüman kabul etmez + +// C'de +void func(); // fonksiyon herhangi bir sayıda argüman kabul edebilir + +// C++'da NULL yerine nullptr kullanılır +int* ip = nullptr; + +// C standard başlıkları başına "c" eklenip, sondaki .h +// kullanılmadan C++'ta kullanılabilir +#include <cstdio> + +int main() +{ + printf("Hello, world!\n"); + return 0; +} + +////////////////////////////////// +// Fonksiyonun fazladan yüklenmesi +////////////////////////////////// + +// C++ herbir fonksiyonun farklı parametereler +// aldığı fonksiyon fazladan yüklenmesini desktekler + +void print(char const* myString) +{ + printf("String %s\n", myString); +} + +void print(int myInt) +{ + printf("My int is %d", myInt); +} + +int main() +{ + print("Hello"); // void print(const char*) fonksiyonunu çağırır. + print(15); // void print(int) fonksiyonunu çağırır. +} + +//////////////////////////////// +// Default fonksiyon argümanları +//////////////////////////////// + +// Eğer çağırıcı tarafından fonksiyona argüman sağlanmamışsa, +// fonksiyona default argüman verebilirsin + +void doSomethingWithInts(int a = 1, int b = 4) +{ + // Burada int'lerle birşeyler yap +} + +int main() +{ + doSomethingWithInts(); // a = 1, b = 4 + doSomethingWithInts(20); // a = 20, b = 4 + doSomethingWithInts(20, 5); // a = 20, b = 5 +} + +// Default argümanlar, argüman listesinin sonunda yer almalı. + +void invalidDeclaration(int a = 1, int b) // Hata! +{ +} + + +///////////////////////// +// Namespace(İsim uzayı) +///////////////////////// + +// Namespace'ler değişken, fonksiyon ve diğer bildirimlerin +// kapsama alanını ayırır. +// Namespace'ler içiçe geçebilir. + +namespace First { + namespace Nested { + void foo() + { + printf("This is First::Nested::foo\n"); + } + } // Nested namespace'inin sonu +} // First namespace'inin sonu + +namespace Second { + void foo() + { + printf("This is Second::foo\n"); + } +} + +void foo() +{ + printf("This is global foo\n"); +} + +int main() +{ + // Second namespace'i içinideki tüm sembolleri mevcut kapsama alanına dahil eder. + // Dikkat edersen artık yalnızca foo() çağrısı çalışmayacaktır çünkü hangi + // namespace'ten çağrıldığı açık değildir. + using namespace Second; + + Second::foo(); // "This is Second::foo" yazdırıır + First::Nested::foo(); // "This is First::Nested::foo" yazdırır + ::foo(); // "This is global foo" yazdırır. +} + +/////////////// +// Input/Output +/////////////// + +// C++'ta input ve output stream'leri kullanır. +// cin, cout ve cerr,sırasıyla, stdin, stdout, ve stderr'i temsil eder. +// << araya ekleme ve >> aradan çıkarma operatörüdür. + +#include <iostream> // I/O stream'lerini dahil etmek için + +using namespace std; // Streamler std namespace'i içindedir(standard kütüphane) + +int main() +{ + int myInt; + + // stdout (veya terminal/screen)'ta çıktı verir + cout << "Enter your favorite number:\n"; + // Girdiyi alır + cin >> myInt; + + // cout ayrıca formatlanabilir + cout << "Your favorite number is " << myInt << "\n"; + // prints "Your favorite number is <myInt>" + + cerr << "Used for error messages"; +} + +////////////// +// String'ler +///////////// + +// String'ler C++'ta nesnedir ve pek çok üye fonksiyonu vardır +#include <string> + +using namespace std; // String'ler de std namespace'i içindedir. (standard kütüphane) + +string myString = "Hello"; +string myOtherString = " World"; + +// + eklemek için kullanıldır +cout << myString + myOtherString; // "Hello World" + +cout << myString + " You"; // "Hello You" + +// C++'ta stringler are mutable'dır (değişebilir). +myString.append(" Dog"); +cout << myString; // "Hello Dog" + + +/////////////////////// +// Reference (Referans) +/////////////////////// + +// C'deki pointer'lara ek olarak +// C++ _reference_'lara sahiptir. +// Bunlar bir kere atandınğında tekrardan atanamayan pointer'dır +// ve null olamaz. +// Değişkenin kendisiyle aynı sözdizimine sahiptir: +// Değerine ulaşmak için * ihtiyaç yoktur ve +// atama için & (address of) kullanılmaz. + +using namespace std; + +string foo = "I am foo"; +string bar = "I am bar"; + + +string& fooRef = foo; // Bu foo'nun reference'ını oluşturur. +fooRef += ". Hi!"; // foo'yu reference'ı üzerinden değiştirir. +cout << fooRef; // "I am foo. Hi!" yazdırır. + +// "fooRef"e yeniden atama yapmaz. Bu "foo = bar" denktir ve bu satırdan sonra +// foo == "I am bar" olur +cout << &fooRef << endl; // foo'un adresini yazdırır +fooRef = bar; +cout << &fooRef << endl; //Hala foo'nun adresini yazdırır +cout << fooRef; //"I am bar" yazdırır + +// fooRef'in adresi aynı kalır yani hala foo'nun adresidir. + +const string& barRef = bar; // bar'a const reference oluşturur +// C'de olduğu gibi, const değerler (pointer'lar ve reference'ler) değiştirilemez. +barRef += ". Hi!"; // Hata, const reference'ler değiştirilemez. + +// Kısa bir ekleme: reference'lere devam etmeden önce, geçici nesne konseptinden +// bahsetmeliyiz. Mesela aşadaki gibi bir kod var: +string tempObjectFun() { ... } +string retVal = tempObjectFun(); + +// Bu iki satırda aslında ne oluyor: +// - tempObjectFun fonksiyonundan bir string nesnesi dönüyor +// - dönmüş olan nesneyle yeni bir string oluşturuyor +// - dönmüş olan nesne yok ediliyor +// İşte bu dönen nesneye geçici nesne denir. Geçici nesneler fonksiyon nesne +// döndürdüğünde oluşturulur ve ifade işini bitirdiğinde yok edilir (Aslında, +// standard'ın söylediği şey bu ama derleyiciler bu davranışı değiştirmemize +// izin veriyor. Daha fazla detay için "return value optimization" diye +// aratabilirsin. Sonuç olarak aşağıdaki kodda: +foo(bar(tempObjectFun())) + +// foo ve bar'ın varolduğunu kabul ediyoruz, tempObjectFun'dan dönen nesne +// bar'a geçti ve foo çağrılmadan önce yokedildir. + +// Şimdi reference'lara dönelim. "ifadenin sonunda" kuralının bir istisnası +// eğer geçici nesne const reference'a geçildiyse oratya çıkar, bu durumda +// nesnenin ömrü mevcut kapsama alanına kadar uzar: + +void constReferenceTempObjectFun() { + // constRef geçici nesneyi alır ve bu durum fonksiyonun sonuna kadar geçerlidir. + const string& constRef = tempObjectFun(); + ... +} + +// C++11 ile gelen diğer bir reference geçici nesnelere özeldir. Bu türden birden +// bir tip tanımlayamazsın ama aşırı yüklenme sırasında bu tipler öncelik alır: +void someFun(string& s) { ... } // Regular reference +void someFun(string&& s) { ... } // Geçici nesneye reference + +string foo; +someFun(foo); // regular reference'ı çağırır +someFun(tempObjectFun()); // geçici reference'ı çağırır + +///////////////////// +// Enum +///////////////////// + +// Enum'lar sabit değerler yapmak için kullanılır ve çoğunlukla kodun daha okunaklı +// olması için kullanılır + +enum ECarTypes +{ + Sedan, + Hatchback, + SUV, + Wagon +}; + +ECarTypes GetPreferredCarType() +{ + return ECarTypes::Hatchback; +} + +// C++11 ile beraber bir tipi enum'a atamanın kolay bir yolu var, bu enum'un istenen +// tipe dönüştürmek için kullanışlı bir yöntem +enum ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // enum uint8_t tipine dönüştürüldü + WriteByteToFile(InputCarType); +} + +// Diğer yandan enum'ların yanlışlıkla integer tipini veya diğer enumlara dönüşmesini +// istemiyorsan enum class olarak tanımlayabilirsin +enum class ECarTypes : uint8_t +{ + Sedan, // 0 + Hatchback, // 1 + SUV = 254, // 254 + Hybrid // 255 +}; + +void WriteByteToFile(uint8_t InputValue) +{ + // Serialize the InputValue to a file +} + +void WritePreferredCarTypeToFile(ECarTypes InputCarType) +{ + // ECarTypes, uint8_t tipinde olmasına rağmen, "enum class" olarak + // tanımlandığından derlenmeyecektir! + WriteByteToFile(InputCarType); +} + +/////////////////////////////////////////// +// Sınıflar ve nesneye yönelik proglamalama +/////////////////////////////////////////// + +// Sınıflara(class) ilk örnek +#include <iostream> + +// Sınıfı tanımla. +// Sınıflar genelde header (.h veya .hpp) dosyalarında tanımlanır. +class Dog { + // Üye değişkenler ve fonksiyonlar default olarak private'dir. + std::string name; + int weight; + +// Aşağıda, "private:" veya "protected:" bulunana kadar +// bütün üyeler public'tir. +public: + + // Default constructor + Dog(); + + // Üye fonksiyon bildirimi (gerçeklenimi aşağıda) + // Dikkat ederseniz using namespace std; yerine + // std::string kullandık. + // Hiçbir zaman header dosyasında "using namespace std;" kullanma. + void setName(const std::string& dogsName); + + void setWeight(int dogsWeight); + + // Nesnenin durumunu değiştirmeyen fonksiyonlar const ile işaretlenmelidir + + // Türetilen sınıflarda fonksiyonu override edebilmek için başına + // _virtual_ eklenmelidir. + // Fonksiyonlar, performanslar ilgili nedenlerden ötürü default olarak virtual değildir + virtual void print() const; + + // Fonksiyonlar class içinde de tanımlanabilir. + // Bu şekille tanımlanan fonksiyonlar otomatik olarak inline olur. + void bark() const { std::cout << name << " barks!\n"; } + + // C++ constructor'ların yanında destructor'da sağlar. + // Bunlar nesne silindiğinde veya scope'un dışına çıktığında çağrılır. + // Bu RAII gibi güçlü paradigmaları etkin kılar. + // (aşağıda açıklandı) + // Eğer sınıf kendisinden türetiliyorsa, destructor virtual olmalıdır, + // eğer virtual değilse, türetilmiş sınıfın destructor'ı nesne, ana sınıf + // referans'ı veya pointer'ı üzerinden yok edildiğinde, çağrılmayacaktır. + virtual ~Dog(); + +}; // class tanımının sonuda noktalı virgül(;) olmalıdır. + +// Sınıfın üye fonksiyonları genelde .cpp dosyaları içinde gerçeklenir. +Dog::Dog() +{ + std::cout << "A dog has been constructed\n"; +} + +// Objects (such as strings) should be passed by reference +// Nesneler (string gibi) reference ile fonksiyonlara geçilmelidir +// Eğer nesneleri değiştirilecekse reference ile fonksiyonlara geçilmelidir, +// değiştirilmeyecekse const reference ile geçilmelidir. +void Dog::setName(const std::string& dogsName) +{ + name = dogsName; +} + +void Dog::setWeight(int dogsWeight) +{ + weight = dogsWeight; +} + +// Dikkat edersen "virtual" yalnızca bildirimde gerekli, tanımlamada değil. +void Dog::print() const +{ + std::cout << "Dog is " << name << " and weighs " << weight << "kg\n"; +} + +Dog::~Dog() +{ + std::cout << "Goodbye " << name << "\n"; +} + +int main() { + Dog myDog; // "A dog has been constructed" yazdırır + myDog.setName("Barkley"); + myDog.setWeight(10); + myDog.print(); // "Dog is Barkley and weighs 10 kg" yazdırır. + return 0; +} // "Goodbye Barkley" yazdırır. + +// Inheritance(Miras) + +// Bu sınıf, Dog sınıfında public ve protected olan herşeyi miras alır, +// private olanları da miras alır ama, public ve protected sınıflar aracılıyla +// yapılmıyorsa, doğrudan erişemez. +class OwnedDog : public Dog { + +public: + void setOwner(const std::string& dogsOwner); + + // print fonksiyonunun davranışını bütün OwnedDogs sınıfı için override eder + // (üstünden geçer, kendine uyarlar). + // bknz http://en.wikipedia.org/wiki/Polymorphism_(computer_science) + // override anahtar sözcüpü kullanılma da olur ama kullanılması aslında bir temel + // temel sınıf fonksiyonunun üzerinden geçtiğimizi gösterir. + void print() const override; + +private: + std::string owner; +}; + +// Bu arada takip eden .cpp dosyasında: + +void OwnedDog::setOwner(const std::string& dogsOwner) +{ + owner = dogsOwner; +} + +void OwnedDog::print() const +{ + Dog::print(); // Ana dog sınıfındaki print fonksiyonunu çağırır + std::cout << "Dog is owned by " << owner << "\n"; + // "Dog is <name> and weights <weight>" + // "Dog is owned by <owner>" + // yazdırır +} + +///////////////////////////////////////////////////// +// ilk değer atama ve Operatörün fazladan yüklenmesi +///////////////////////////////////////////////////// + +// C++ dilinde +, -, *, /, gibi operatörlerin davranışını fazladan yükleyebilirsiniz. +// Bu, operator her kullandınıldığında çağrılan bir fonksiyon tanımlamasıyla yapılır. + +#include <iostream> +using namespace std; + +class Point { +public: + // Üye değişkenkenlere default değer atanabilir. + double x = 0; + double y = 0; + + // Default constructor + Point() { }; + + Point (double a, double b) : + x(a), + y(b) + { /* İlk değer atama dışında birşey yapma */ } + + // + operatorünün fazladan yükle. + Point operator+(const Point& rhs) const; + + // += operatorünü fazladan yükle + Point& operator+=(const Point& rhs); + + // - ve -= operatorleri fazladan yüklemek de mantıklı olurdu + // ama kısa tutmak için burda değinmedik. +}; + +Point Point::operator+(const Point& rhs) const +{ + // yeni bir nokta oluştur ve bunu rhs ile topla + 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); + // Bu Point + operatorünü çağırır + Point result = up + right; + // "Result is upright (1,1)" yazdırır. + cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; + return 0; +} + +//////////////////////// +// Şablonlar (Templates) +//////////////////////// + +// Şablonlar C++ dilinde tipten bağımsız programlama için kullanılır. + +// Zaten aşina olduğun tipten bağımsız programlamayla başladık. Bir tip parametresi +// alan fonksiyon veya sınıf tanımlamaık için: +template<class T> +class Box { +public: + // Bu sınıfta T, herhangi bir tip için kullanılabilir. + void insert(const T&) { ... } +}; + +// Derleme esnasında derleyici aslında, parametreleri yerine konmuş şekilde herbir şablonu üretir, +// bu yüzden sınıfın tam tanımı her çağrılma sırasında var olmak zorundadır. Bu nedenle şablon sınıflarını +// tamamen header dosyalarında görürsün. + +// Stack'ta şablon sınıfın bir örneğini oluşturmak için: +Box<int> intBox; + +// ve, anladığın gibi, kullanabilirsin: +intBox.insert(123); + +// Tabi, şablonları içiçe geçirebilirsin: +Box<Box<int> > boxOfBox; +boxOfBox.insert(intBox); + +// C++11'den önce iki '>' arasına boşluk koymak zorundaydık yoksa sağa kaydırma +// operatörü olarak algılanabilirdi. + +// Bazen şunu da görebilirsin +// template<typename T> +// 'class' ve 'typename' anahtar sözcükleri çoğunlukla +// birbirlerinin yerine kullanılabilir. Tam açıklama için, bknz. +// http://en.wikipedia.org/wiki/Typename +// (evet, bu anahtar sözcüğün kendi Wikipedia sayfası var). + +// Benzer şekilde, bir şablon fonksiyon: +template<class T> +void barkThreeTimes(const T& input) +{ + input.bark(); + input.bark(); + input.bark(); +} + +// Dikkat edersen tip parametresi hakkında birşey belirtilmedi. Derleyici bunları üretecek +// ve her parametre geçişinde tip-kontrolü yapacaktır, bu nedenle de fonksiyon herhangi bir T +// tipi için çalışacaktır! + +Dog fluffy; +fluffy.setName("Fluffy") +barkThreeTimes(fluffy); // Üç kere "Fluffy barks" yazdırır. + +// Şablonun parametresi sınıf olmak zorunda değildir: +template<int Y> +void printMessage() { + cout << "Learn C++ in " << Y << " minutes!" << endl; +} + +// Ve template'i daha etkili kod için dışarıdan özelleştirebilirsin. +// Tabiki gerçek-dünya kullanımlarında özelleştirme bunun kadar kolay değildir. +// Dikkat edersen, bütün parametreleri dıştan özelleştirmiş olsak bile +// hala fonksiyonu (veya sınıfı( template olarak tanımlamamız gerekli. +template<> +void printMessage<10>() { + cout << "Learn C++ faster in only 10 minutes!" << endl; +} + +printMessage<20>(); // "Learn C++ in 20 minutes!" yazdırır +printMessage<10>(); // "Learn C++ faster in only 10 minutes!" yazdırır + + +/////////////////////////////////////////////// +// İstisnai Durum Yönetimi (Exception Handling) +/////////////////////////////////////////////// + +// Standard kütüphane bazı istisnai tipler sağlar +// (bknz http://en.cppreference.com/w/cpp/error/exception) +// ama herhangi bir tip de istisnai durum fırlatabilir + +#include <exception> +#include <stdexcept> + +// _try_ bloğu içinde fırlatılan bütün istisnai durumlar, takip eden, _catch_ ile +// yakalanabilir. +try { + // _new_ kullanarak heap'ten istisnai durumlar için yer ayırma + throw std::runtime_error("A problem occurred"); +} + +// istisnai durumlar nesne ise const reference ile yakala +catch (const std::exception& ex) +{ + std::cout << ex.what(); +} + +// Bir önceki _catch_ bloğundan kaçan istisnai durum burda yakala +catch (...) +{ + std::cout << "Unknown exception caught"; + throw; // Tekrardan istisnai durum fırlatır +} + +/////// +// RAII +/////// + +// RAII, "Resource Acquisition Is Initialization" kelimelerinin kısaltmasıdır. +// Bu Türkçe, "Kaynak alımı aynı zamanda ilk değer atamasıdır." olarak çevrilebilir. +// Bunu basitçe constructor ile ayrılan hafızanın destructor ile iade edilmesi olarak +// düşünebiliriz. + +// Bunun ne şekilde kullanışlı olduğunu görmek için +// bir C dosyasının, dosya işleme biçimine bakabiliriz: +void doSomethingWithAFile(const char* filename) +{ + // Başlangıçta herşeyin yolunda gittiğini düşünelim + + FILE* fh = fopen(filename, "r"); // Dosyayı okuma modunda aç + + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + + fclose(fh); // Dosyayı kapat +} + +// Malesef hatalarla başa çıkmaya çalışırken işler hızlıca karmaşıklaşır. +// Mesela fopen'ın başarısız olduğunu varsayalım, ve doSoomethingWithTheFile ve +// doSomethingWithIt hata kodları gönderdi. +// (İstisnai durumlar yonetimi, hata koduna tercih ediler bir yöntemdir, ama bazı +// programcılar, özellikle C arkaplanı olanlar, aynı fikirde değildir. +// Bu durumda her bir fonksiyon çağrısını kontrol etmeli ve bir problem oluştuysa +// dosyayı kapatmalıyız. + +bool doSomethingWithAFile(const char* filename) +{ + FILE* fh = fopen(filename, "r"); // Dosyayı okuma modunda aç + if (fh == nullptr) // Başarısız olma durumunda dönen değer null olur + return false; // Başarısız olma durumunu çağırıcıya bildir + + // Başarısız olma durumunda her iki fonksiyonun da false döndürdüğünü kabul edelim + if (!doSomethingWithTheFile(fh)) { + fclose(fh); // Dosyayı kapatalım, akıntı olmasın. + return false; // Hatayı bildir + } + if (!doSomethingElseWithIt(fh)) { + fclose(fh); // Dosyayı kapatalım, akıntı olmasın. + return false; // Hatayı bildir + } + + fclose(fh); // Dosyayı kapat + return true; // Başarı durumunu ifade eder +} + +// C programcıları biraz goto kullanarak bu durumu temizler +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); // Dosyayı kapat + return true; // Başarı durumunu ifade eder + +failure: + fclose(fh); + return false; // Hatayı bildir +} + +// Eğer fonksiyon istisnai durum yönetimi araçlarını kullanırsa +// işler daha temiz olur ama hala en iyi durumun altında kalır. +void doSomethingWithAFile(const char* filename) +{ + FILE* fh = fopen(filename, "r"); + if (fh == nullptr) + throw std::runtime_error("Could not open the file."); + + try { + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + } + catch (...) { + fclose(fh); // Hata durumunda dosyayı kapattığından emin ol + throw; // Sonra, tekrardan istisnai durum fırlat + } + + fclose(fh); // Dosyayı kapat + // Herşey başarılı +} + +// Şimdi aynı şeyi C++'ın dosya stream sınıfıyla (fstream) karşılaştıralım +// fstream, dosyayı kapatmak için kendi destructor'ını kullanır. +// Destructor'ın, nesne scope dışına çıktığında otomatik olarak çağrıldığını +// hatırlayın. +void doSomethingWithAFile(const std::string& filename) +{ + std::ifstream fh(filename); // Dosyayı aç + + // Dosyayla birşeyler yap + doSomethingWithTheFile(fh); + doSomethingElseWithIt(fh); + +} // Dosya, destructor tarafından otomatik olarak kapatıldı + +// Bunun _çok büyük_ avantajları var: +// 1. Ne olursa olursun, +// kaynak (bu örnekte dosya tutucusu) temizlenecektir. +// Destructor doğru yazıldığında, +// Tutucuyu kapatmayı unutma ve kaynak akıntısı _imkansız_dır. +// 2. Kodun çok daha temiz olduğuna dikkat edin. +// Destructor, dosyayı kapatma işini, endilenmemize gerek kalmadan +// arka planda halleder. +// 3. Kod, istisnai durumlara karşı korunaklıdır. +// İstisnai durum fonksiyonun herhangi bir yerinde fırlatılabilir ve +// temizleme işi gene de yapılır. + +// Bütün C++ kodu deyimleri RAII prensibini tüm kaynakları için kullanır. +// Ek örnekler şunlardır: +// - unique_ptr ve shared_ptr ile hafıza kullanımı +// - Tutucular - standard kütüphane linked list, +// vector (yani kendiliğinden boyu ayarlanan dizi), hash map vs. +// scope'un dışına çıktığında içerini otomatik olarak yok eden tüm yapılar. +// - lock_guard ve unique_lock kullanan mutex'ler + +/////////////////////////////////////// +// Lambda İfadeleri (C++11 ve yukarısı) +/////////////////////////////////////// + +// lambda'lar, tam olarak çağrıldığı yerde bir anonim fonksiyon tanımlamak +// veya fonksiyona argüman geçmek için uygun bir yoldur. + +// Mesela, pair'lardan oluşan bir vector'u, pair'ın ikinci değerine +// göre sıralamak isteyelim + +vector<pair<int, int> > tester; +tester.push_back(make_pair(3, 6)); +tester.push_back(make_pair(1, 9)); +tester.push_back(make_pair(5, 0)); + +// sort fonksiyonuna üçüncü argüman olarak lambda ifadesini geç +// sort, <algorithm> başlığında tanımlı + +sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) { + return lhs.second < rhs.second; + }); + +// Lambda ifadesinin söz dizimine dikkat edin, +// lambda'daki [], değişkenleri "tutmak" için kullanılır +// "Tutma listesi", fonksiyon gövdesinde nelerin, ne şekilde erişilebilir olduğunu tanımlar +// Şunlardan biri olabilir: +// 1. bir değer : [x] +// 2. bir referans : [&x] +// 3. mevcut scope içindeki herhangi bir değişkene referans ile [&] +// 4. 3 ile aynı, ama değer ile [=] +// Mesela: +vector<int> dog_ids; +// number_of_dogs = 3; +for(int i = 0; i < 3; i++) { + dog_ids.push_back(i); +} + +int weight[3] = {30, 50, 10}; + +// Mesela dog_ids vector'unu dog'ların ağırlıklarına göre sıralamak isteyelim +// Yani en sonunda şöyle olmalı: [2, 0, 1] + +// Burada lambda ifadesi oldukça kullanışlıdır + +sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) { + return weight[lhs] < weight[rhs]; + }); +// Dikkat edersen "weight" dizisini referans ile aldık. +// C++'da lambdalar hakkında daha fazla bilgi için : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 + +////////////////////////////////// +// Akıllı For (C++11 ve yukarısı) +////////////////////////////////// + +// Akıllı for döngüsünü bir tutucuyu dolaşmak için kullanabilirsin +int arr[] = {1, 10, 3}; + +for(int elem: arr){ + cout << elem << endl; +} + +// Tutucunun elemanlarının tipi için endişe etmeden "auto" kullanabilirsin +// Mesela: + +for(auto elem: arr) { + // arr dizisinin elemanlarıyla ilgili bir şeyler yap +} + +//////////////// +// Güzel Şeyler +//////////////// + +// C++ dilinin bakış açısı yeni başlayanlar için (hatta dili iyi bilenler için bile) +// şaşırtıcı olabilir. +// Bu bölüm, ne yazık ki, büyük ölçüde tam değil; C++ kendi ayağına ateş edilebilecek kolay +// dillerden biridir. + +// private metodları override edebilirsin! +class Foo { + virtual void bar(); +}; +class FooSub : public Foo { + virtual void bar(); // Foo::bar fonksiyonu override edilir! +}; + + +// 0 == false == NULL (çoğu zaman)! +bool* pt = new bool; +*pt = 0; // 'pt'nin gösterdiği değere false atar. +pt = 0; // 'pt'ye null pointer atar. Her iki satır uyarısız derlenir. + +// nullptr'ın bu meselenin bazılarını çözmesi beklenmiştir: +int* pt2 = new int; +*pt2 = nullptr; // Derlenmez. +pt2 = nullptr; // pt2'ye null atar. + +// bool tipleri için bir istisna vardır. +// Bu null pointer'ları if(!ptr) ile test etmek içindir. +// ama sonuç olarak bir bool değerine nullptr atayabilirsin! +*pt = nullptr; // '*pt' değeri bir boll olmasına rağmen, hala derlenir! + + +// '=' != '=' != '='! +// Calls Foo::Foo(const Foo&) or some variant (see move semantics) copy +// Foo::Foo(const Foo&) çağrısını veya kopyalama constructor'ının bir çeşidinin çağrısınıyapar(taşıma semantiklerine bknz.) +Foo f2; +Foo f1 = f2; + +// Foo::operator=(Foo&) çağrısını yapar. +Foo f1; +f1 = f2; + + +/////////////////////////////////////// +// Tuple (C++11 ve yukarısı) +/////////////////////////////////////// + +#include<tuple> + +// Ana fikir olarak, Tuple, eski veri yapılarına (C'deki struct'lar) benzer ama isimli veri üyeleri yerine +// elemanlarına tuple içindeki sırasına göre erişilir. + +// Tuple'ı inşa ederek başlayalım +// değişkenleri tuple içinde paketliyoruz +auto first = make_tuple(10, 'A'); +const int maxN = 1e9; +const int maxL = 15; +auto second = make_tuple(maxN, maxL); + +// 'first' tuple'ının değerlerini yazdırma +cout << get<0>(first) << " " << get<1>(first) << "\n"; // 10 A yazdırır + +// 'second' tuple'ının değerlerini yazdırma +cout << get<0>(second) << " " << get<1>(second) << "\n"; // 1000000000 15 yazdırır + +// Değişkenleri tuple'dan çıkarma + +int first_int; +char first_char; +tie(first_int, first_char) = first; +cout << first_int << " " << first_char << "\n"; // 10 A yazdırır + +// Ayrıca şu şekide de tuple oluşturabiliriz. + +tuple<int, char, double> third(11, 'A', 3.14141); +// tuple_size, tuple'daki eleman sayısını (constexpr olarak) döndürür + +cout << tuple_size<decltype(third)>::value << "\n"; // 3 yazdırır + +// tuple_cat, tuple'daki tüm elemanları aynı sırada birleştirir. + +auto concatenated_tuple = tuple_cat(first, second, third); +// concatenated_tuple = (10, 'A', 1e9, 15, 11, 'A', 3.14141) olur + +cout << get<0>(concatenated_tuple) << "\n"; // 10 yazdırır +cout << get<3>(concatenated_tuple) << "\n"; // 15 yazdırır +cout << get<5>(concatenated_tuple) << "\n"; // 'A' yazdırır + + +///////////////////// +// Tutucular +///////////////////// + +// Tutucular veya Standard Şablon Kütüphanesi(STL) önceden tanımlanmış şablonlar sunar. +// Bunlar elemanları için ayrılan hafıza alanını yönetir +// ve onlara erişim ve değiştirmek için üye fonksiyonlar sağlar + +// Bazı tutucular şunlardır: + +// Vector (Dinamik Dizi) +// koşma anında nesne dizisi veya list oluşturmamızı sağlar +#include <vector> +string val; +vector<string> my_vector; // vector'ü tanımla +cin >> val; +my_vector.push_back(val); // val değerini my_vector vectörüne push edecektir +my_vector.push_back(val); // val değerini yeniden push edecektir (şu an iki elemanı var) + +// vector içinde dolaşmak için iki seçenek var: +// ya klasik döngüyle (0. index'ten son index'e kadar iterasyon yaparak) +for (int i = 0; i < my_vector.size(); i++) { + cout << my_vector[i] << endl; // vector'ün elemanlarına uşamak için [] operatörünü kullanabiliriz +} + +// ya da iteratör kulllanarak: +vector<string>::iterator it; // vector için iterator tanımla +for (it = my_vector.begin(); it != my_vector.end(); ++it) { + cout << *it << endl; +} + +// Set(Küme) +// Set'ler benzersiz(unique) elemanları belirli bir sırada saklayan tutuculardır. +// Set, benzersiz değerleri, herhangi bir fonksiyon veya kod gerektirmeksizin, sıralı olarak + +#include<set> +set<int> ST; // int tipi için set tanımlar +ST.insert(30); // ST kümesini 30 değerini dahil eder +ST.insert(10); // ST kümesini 10 değerini dahil eder +ST.insert(20); // ST kümesini 20 değerini dahil eder +ST.insert(30); // ST kümesini 30 değerini dahil eder +// Şimdi kümedeki elemanlar aşağıdaki gibidir +// 10 20 30 + +// Bir eleman silmek için: +ST.erase(20); // 20 değerine sahip elemanı siler +// Set ST: 10 30 +// Iterator kullanarak Set içinde iterasyon yapmak için: +set<int>::iterator it; +for(it=ST.begin();it<ST.end();it++) { + cout << *it << endl; +} +// Output: +// 10 +// 30 + +// Tutucuyu tamamen silmek için Tutucu_Adi.clear() kullanırız +ST.clear(); +cout << ST.size(); // ST kümesinin eleman sayısı(size)nı yazdırır. +// Output: 0 + +// NOTE: Aynı elemanlari içerebilen kümle için multiset kullanırız + +// Map(Harita) +// Map, elemanları anahtar değer, haritalanmış değer şeklinde özel bir sırada saklar. +// anahtar_değer -> haritalanmış_değer + +#include<map> +map<char, int> mymap; // Anahtar char ve değer int olacak şekilde map tanımlar + +mymap.insert(pair<char,int>('A',1)); +// 1 değeri için A anahtar değerini ekler +mymap.insert(pair<char,int>('Z',26)); +// 26 değeri için Z anahtar değerini ekler + +// Map'te dolaşma +map<char,int>::iterator it; +for (it=mymap.begin(); it!=mymap.end(); ++it) + std::cout << it->first << "->" << it->second << '\n'; +// Output: +// A->1 +// Z->26 + +// Anahtar'a atanmış değeri bulmak için +it = mymap.find('Z'); +cout << it->second; + +// Output: 26 + + +///////////////////////////////////////////// +// Mantıksal ve Bit seviyesindeki operatörler +///////////////////////////////////////////// + +// Pek çok C++ operatörleri diğer dillerdekiyle aynıdır + +// Mantıksal operatörler + +// C++, bool ifadelerinde Kısa-devre değerlendirmesini kullanır yani ikinci argüman yalnızca ilk argüman +// ifadenin değerine karar vermek için yeterli değilse çalıştırılır + +true && false // **mantıksal ve** işlemi yapılır ve yanlış sonucu üretilir +true || false // **mantıksal veya** işlemi yapılır ve true sonucu üretilir +! true // **mantıksal değil** işlemi yapılır ve yalnış sonucu üretilir + +// Sembolleri kullanmak yerine onlara karşılık gelen anahtar kelimeler kullanılabilir +true and false // **mantıksal ve** işlemi yapılır ve yanlış sonucu üretilir +true or false // **mantıksal veya** işlemi yapılır ve true sonucu üretilir +not true // **mantıksal değil** işlemi yapılır ve yalnış sonucu üretilir + +// Bit seviyesindeki operatörler + +// **<<** Sola kaydırma operatörü +// << bitleri sola kaydırır +4 << 1 // 4'ün bitlerini 1 sola kaydırır ve 8 sonucunu verir +// x << n, x * 2^n olarak düşünülebilir + + +// **>>** Sağa kaydırma operatörü +// >> bitleri sağa kaydırır +4 >> 1 // 4'ün bitlerini 1 sağa kaydırır ve 2 sonucunu verir +// x >> n, x / 2^n olarak düşünülebilir + +~4 // Bit seviyesinde değil işlemini gerçekleştirir +4 | 3 // Bit seviyesinde veya işlemini gerçekleştirir +4 & 3 // Bit seviyesinde ve işlemini gerçekleştirir +4 ^ 3 // Bit seviyesinde xor işlemini gerçekleştirir + +// Eşdeğer anahtar kelimeler +compl 4 // Bit seviyesinde değil işlemini gerçekleştirir +4 bitor 3 // Bit seviyesinde veya işlemini gerçekleştiri +4 bitand 3 // Bit seviyesinde ve işlemini gerçekleştirir +4 xor 3 // Bit seviyesinde xor işlemini gerçekleştirir + + +``` +İleri okuma: + +Güncel bir referans +<http://cppreference.com/w/cpp> adresinde bulunabilir + +Ek kaynaklar <http://cplusplus.com> adresinde bulunabilir diff --git a/tr-tr/git-tr.html.markdown b/tr-tr/git-tr.html.markdown new file mode 100644 index 00000000..87c1820c --- /dev/null +++ b/tr-tr/git-tr.html.markdown @@ -0,0 +1,596 @@ +--- +category: tool +lang: tr-tr +tool: git +contributors: + - ["Jake Prather", "http://github.com/JakeHP"] + - ["Leo Rudberg" , "http://github.com/LOZORD"] + - ["Betsy Lorton" , "http://github.com/schbetsy"] + - ["Bruno Volcov", "http://github.com/volcov"] + - ["Andrew Taylor", "http://github.com/andrewjt71"] + - ["Jason Stathopulos", "http://github.com/SpiritBreaker226"] + - ["Milo Gilad", "http://github.com/Myl0g"] + - ["Adem Budak", "https://github.com/p1v0t"] + +filename: LearnGit-tr.txt +--- + +Git dağınık versiyon kontrol ve kaynak kod yönetim sistemidir. + +Bunu projenin bir seri anlık durumunu kaydederek yapar ve bu anlık durumları +kullanarak versiyon ve kaynak kodu yönetmeni sağlar. + +## Versiyonlama Konseptleri + +### Versiyon kontrol nedir? + +Versiyon kontrol, zaman içerisinde dosya(lar)daki değişikliği kaydeden sistemdir. + +### Merkezi Versiyonlama vs. Dağınık Versiyonlama + +* Merkezi versiyon kontrolü dosyaların eşitlenmesine, takibine ve yedeklenmesine odaklanır. +* Dağınık versiyon kontrolü değişimin paylaşılmasına odaklanır. Her değişiminin benzersiz bir adı vardır. +* Dağınık sistemlerin belirlenmiş bir yapısı yoktur. Git ile kolayca SVN'deki gibi merkezi bir sistem elde edebilirsin. + +[Daha fazla bilgi](http://git-scm.com/book/en/Getting-Started-About-Version-Control) + +### Neden Git? + +* Çevrimdışı çalışabilir +* Diğerleriyle beraber çalışmak kolaydır! +* Dallanma kolaydır! +* Dallanma hızlıdır! +* Git hızlıdır +* Git esnektir + +## Git Mimarisi + +### Repository + +Bir grup dosya, dizin, geriye dönük kayıt, commit, head. Bunları kaynak kodun veri +yapısı gibi düşünebilirsin, herbir kaynak kod "elemanı" seni kendi revizyon geçmişine +eriştirir. + +Bir git repo'su .git dizini ve çalışma ağacından oluşur. + +### .git Dizini (repository bileşeni) + +.git dizini bütün konfigrasyon, log, dallanma, HEAD ve daha fazlasını tutar. +[detaylı liste](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html) + +### Çalışma Ağacı (repository bileşeni) + +Temelde repo'daki dizinlerin ve dosyalarındır. Sıkça çalışma ağacın olarak anılır. + +### Index (.git dizininin birleşeni) + +Index git'in evreleme alanıdır (staging area). Temelde çalışma ağacını Git repo'sundan +ayıran bir katmandır. Bu geliştiricilere neyin Git repo'suna gönderileceği hakkında daha +fazla güç verir. + +### Commit + +Bir git commit'i Çalışma Ağacındaki bir takım değişiklerdir. Mesela 5 tane dosya +eklemişsindir ve diğer 2 tanesini silmişindir, bu değişikler commit'te (anlık kayıtta) +tutulacaktır. Bu commit daha sonra diğer repo'lara bastırılabilir (pushed) ve bastırılmaz! + +### Branch + +Bir branch esasen yaptığın son commit'e göstericidir(pointer). Commit'lemeye devam ettiğinde, +bu gösterici otomatik olarak son commit'e güncellenir. + +### Tag + +Bir tag, tarihteki belirli bir noktanın işaretidir. İnsanlar bunu genelde +sürüm notları için kullanır (v1.0 vs.) + +### HEAD ve head (.git dizininin birleşenleri) + +HEAD mevcut branch'a bir göstericidir. Bir repository yalnızca 1 *aktif* +HEAD'e sahiptir. +head, commit'e bir göstericidir. Bir repository herhangi bir sayıda head'e sahip olabilir. + +### Git'in Stage'leri +* Modified - Dosyada değişikler yapıldı ama henüz Git Veritabanına commit yapılmadı. +* Staged - Modified edilmiş bir dosyayı, sonraki commit'e gitmek üzere işaretler. +* Committed - Dosyalar Git Veritabanına commit'lendi. + +### Kavramsal Kaynaklar + +* [Bilgisayar Bilimciler için Git](http://eagain.net/articles/git-for-computer-scientists/) +* [Tasarımcılar için Git](http://hoth.entp.com/output/git_for_designers.html) + +## Komutlar + +### init + +Boş bir Git repository'si oluştur. Git repository'sinin ayarları, depolanmış +bilgileri ve daha fazlası ".git" adlı dizinde (bir klasör) tutulur. + +```bash +$ git init +``` + +### config + +Ayarları yapılandırmak için. Repository, sistemin kendisi veya global yapılandırmalar +için olarabilir. (global yapılandırma dosyası `~/.gitconfig`). + +```bash +# Print & Set Some Basic Config Variables (Global) +$ git config --global user.email "MyEmail@Zoho.com" +$ git config --global user.name "My Name" +``` + +[git config hakkında daha fazla bilgi için.](http://git-scm.com/docs/git-config) + +### help + +Her bir komutun detaylı kılavuzuna hızlı bir erişim için. Ya da sadece bazı şeylerin +anlamı için hızlı bir hatırlatıcı için. + +```bash +# Quickly check available commands +$ git help + +# Check all available commands +$ git help -a + +# Command specific help - user manual +# git help <command_here> +$ git help add +$ git help commit +$ git help init +# or git <command_here> --help +$ git add --help +$ git commit --help +$ git init --help +``` + +### dosyaları ignore etme + +git'in bazı dosya(ları) ve klasör(leri) kasıtlı olarak takip etmemesi için. Genel +olarak,repository'de ne de olsa paylaşılacak, private ve temp dosyaları için. + +```bash +$ echo "temp/" >> .gitignore +$ echo "private_key" >> .gitignore +``` + +### status + +index dosyası(temelde çalıştığın repo) ve mevcut HEAD commit arasındaki farkı göstermek için. + +```bash +# Will display the branch, untracked files, changes and other differences +$ git status + +# To learn other "tid bits" about git status +$ git help status +``` + +### add + +Dosyaları staging area'ya eklemek için. Eğer yeni dosyaları staging area'ya `git add` +yapmazsanız, commit'lere eklenmez! + +```bash +# add a file in your current working directory +$ git add HelloWorld.java + +# add a file in a nested dir +$ git add /path/to/file/HelloWorld.c + +# Regular Expression support! +$ git add ./*.java + +# You can also add everything in your working directory to the staging area. +$ git add -A +``` +Bu yalnızca dosyayı staging area'a/index'e ekler, çalışılan dizine/repo'ya commit etmez. + +### branch + +Branch'ları yönetir. Bu komutu kullanarak, branch'ları görebilir, düzenleyebilir, oluşturabilir, silebilirsin. + +```bash +# list existing branches & remotes +$ git branch -a + +# create a new branch +$ git branch myNewBranch + +# delete a branch +$ git branch -d myBranch + +# rename a branch +# git branch -m <oldname> <newname> +$ git branch -m myBranchName myNewBranchName + +# edit a branch's description +$ git branch myBranchName --edit-description +``` + +### tag + +tag'leri yönetir + +```bash +# List tags +$ git tag + +# Create a annotated tag +# The -m specifies a tagging message, which is stored with the tag. +# If you don’t specify a message for an annotated tag, +# Git launches your editor so you can type it in. +$ git tag -a v2.0 -m 'my version 2.0' + +# Show info about tag +# That shows the tagger information, the date the commit was tagged, +# and the annotation message before showing the commit information. +$ git show v2.0 + +# Push a single tag to remote +$ git push origin v2.0 + +# Push a lot of tags to remote +$ git push origin --tags +``` + +### checkout + +index'in versiyonun eşlemek için çalışma ağacındaki,veya belirtilen ağactaki, tüm dosyaları günceller. + +```bash +# Checkout a repo - defaults to master branch +$ git checkout + +# Checkout a specified branch +$ git checkout branchName + +# Create a new branch & switch to it +# equivalent to "git branch <name>; git checkout <name>" + +$ git checkout -b newBranch +``` + +### clone + +Varolan bir repository'i yeni bir dizine clone'lar veya kopyalar. +Ayrıca clone'lanmış repodaki her bir branch için, uzak branch'a bastırmana izin veren, +uzak takip branch'ları ekler. + +```bash +# Clone learnxinyminutes-docs +$ git clone https://github.com/adambard/learnxinyminutes-docs.git + +# shallow clone - faster cloning that pulls only latest snapshot +$ git clone --depth 1 https://github.com/adambard/learnxinyminutes-docs.git + +# clone only a specific branch +$ git clone -b master-cn https://github.com/adambard/learnxinyminutes-docs.git --single-branch +``` + +### commit + +index'in mevcut içeriğini yeni bir "commit"te saklar. Bu commit, kullanıcının oluşturduğu +bir mesajı ve yapılan değişiklikleri saklar. + +```bash +# commit with a message +$ git commit -m "Added multiplyNumbers() function to HelloWorld.c" + +# signed commit with a message (user.signingkey must have been set +# with your GPG key e.g. git config --global user.signingkey 5173AAD5) +$ git commit -S -m "signed commit message" + +# automatically stage modified or deleted files, except new files, and then commit +$ git commit -a -m "Modified foo.php and removed bar.php" + +# change last commit (this deletes previous commit with a fresh commit) +$ git commit --amend -m "Correct message" +``` + +### diff + +Shows differences between a file in the working directory, index and commits. +Bir dosyanın, çalışma ağacı, index ve commit'ler arasındaki farklarını göster. + +```bash +# Show difference between your working dir and the index +$ git diff + +# Show differences between the index and the most recent commit. +$ git diff --cached + +# Show differences between your working dir and the most recent commit +$ git diff HEAD +``` + +### grep + +Bir repository'de hızlıca arama yapmana izin verir. + +İsteğe Bağlı Yapılandırmalar: + +```bash +# Thanks to Travis Jeffery for these +# Set line numbers to be shown in grep search results +$ git config --global grep.lineNumber true + +# Make search results more readable, including grouping +$ git config --global alias.g "grep --break --heading --line-number" +``` + +```bash +# Search for "variableName" in all java files +$ git grep 'variableName' -- '*.java' + +# Search for a line that contains "arrayListName" and, "add" or "remove" +$ git grep -e 'arrayListName' --and \( -e add -e remove \) +``` + +Daha fazla örnek için +[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja) + +### log + +Repository'deki commitleri gösterir. + +```bash +# Show all commits +$ git log + +# Show only commit message & ref +$ git log --oneline + +# Show merge commits only +$ git log --merges + +# Show all commits represented by an ASCII graph +$ git log --graph +``` + +### merge + +Dış commit'lerdeki değişiklikleri mevcut branch'a "merge" et (birleştir). + +```bash +# Merge the specified branch into the current. +$ git merge branchName + +# Always generate a merge commit when merging +$ git merge --no-ff branchName +``` + +### mv + +Bir dosyayı yeniden taşı veya yeniden adlandır + +```bash +# Renaming a file +$ git mv HelloWorld.c HelloNewWorld.c + +# Moving a file +$ git mv HelloWorld.c ./new/path/HelloWorld.c + +# Force rename or move +# "existingFile" already exists in the directory, will be overwritten +$ git mv -f myFile existingFile +``` + +### pull + +Bir repository'den çeker ve diğer branch'a merge eder. + +```bash +# Update your local repo, by merging in new changes +# from the remote "origin" and "master" branch. +# git pull <remote> <branch> +$ git pull origin master + +# By default, git pull will update your current branch +# by merging in new changes from its remote-tracking branch +$ git pull + +# Merge in changes from remote branch and rebase +# branch commits onto your local repo, like: "git fetch <remote> <branch>, git +# rebase <remote>/<branch>" +$ git pull origin master --rebase +``` + +### push + +Bir branch'taki değişikleri, uzak branch'a bastır ve birleştir. + +```bash +# Push and merge changes from a local repo to a +# remote named "origin" and "master" branch. +# git push <remote> <branch> +$ git push origin master + +# By default, git push will push and merge changes from +# the current branch to its remote-tracking branch +$ git push + +# To link up current local branch with a remote branch, add -u flag: +$ git push -u origin master +# Now, anytime you want to push from that same local branch, use shortcut: +$ git push +``` + +### stash + +Stash'leme çalışma dizinindeki kirli durumu alır ve bitmemiş değişiklikler +yığınına kaydeder. Bu değişikleri istediğin zaman tekrar uygulayabilirsin. + +Mesela git repo'nda bazı işler yaptın ama remote'dan pull yapmak istiyorsun. +Bazı dosyalarında kirli (commit'lenmemiş) değişiklikler olduğundan `git pull` +yapamazsın. Onun yerine önce `git stash` ile değişikliklerini yığına kaydet! + +(stash, sözlük anlamı: bir şeyi, özel bir yere güvenli biçimde saklamak) + +```bash +$ git stash +Saved working directory and index state \ + "WIP on master: 049d078 added the index file" + HEAD is now at 049d078 added the index file + (To restore them type "git stash apply") +``` + +Şimdi pull yapabilirsin! + +```bash +git pull +``` +`...changes apply...` + +Herşeyin tamam olduğunu kontrol et + +```bash +$ git status +# On branch master +nothing to commit, working directory clean +``` +Şu ana kadar neleri stash'lediğini `git stash list` kullanarak görebilirsin. +Stash'lenen şeyler Son-Giren-İlk-Çıkar şeklinde tutulduğundan en son değişim +en üste olacaktır. + +```bash +$ git stash list +stash@{0}: WIP on master: 049d078 added the index file +stash@{1}: WIP on master: c264051 Revert "added file_size" +stash@{2}: WIP on master: 21d80a5 added number to log +``` +Şimdi de kirli değişiklileri yığından çıkarıp uygulayalım. + +```bash +$ git stash pop +# On branch master +# Changes not staged for commit: +# (use "git add <file>..." to update what will be committed) +# +# modified: index.html +# modified: lib/simplegit.rb +# +``` + +`git stash apply` da aynı şeyi yapar + +Şimdi kendi işine dönmeye hazırsın! + +[Ek Okuma.](http://git-scm.com/book/en/v1/Git-Tools-Stashing) + +### rebase (dikkat) + +Branch'ta commit'lenen tüm değişimleri al ve onları başka bir branch'ta tekrar oynat +*Public repo'ya push edilmiş commit'leri rebase etme* + +```bash +# Rebase experimentBranch onto master +# git rebase <basebranch> <topicbranch> +$ git rebase master experimentBranch +``` + +[Ek Okuma.](http://git-scm.com/book/en/Git-Branching-Rebasing) + +### reset (dikkat) + +Reset the current HEAD to the specified state. This allows you to undo merges, +pulls, commits, adds, and more. It's a great command but also dangerous if you +don't know what you are doing. + +HEAD'i belirtilen duruma resetle. Bu merge'leri, pull'ları, commit'leri, add'leri +ve daha fazlasını geriye almanı sağlar. Muhteşem bir komuttur ama aynı zamanda, ne +yaptığını bilmiyorsan, tehlikelidir. + +```bash +# Reset the staging area, to match the latest commit (leaves dir unchanged) +$ git reset + +# Reset the staging area, to match the latest commit, and overwrite working dir +$ git reset --hard + +# Moves the current branch tip to the specified commit (leaves dir unchanged) +# all changes still exist in the directory. +$ git reset 31f2bb1 + +# Moves the current branch tip backward to the specified commit +# and makes the working dir match (deletes uncommitted changes and all commits +# after the specified commit). +$ git reset --hard 31f2bb1 +``` + +### reflog (dikkat) + +Reflog, verilen zaman içinde,default olarak 90 gündür, yaptığın git komutlarını listeler. + +Bu sana beklemediğin şekilde yanlış giden komutları geriye çevirme şansı verir. +(mesela, eğer bir rebase uygulamanı kırdıysa) + +Şu şekilde yapıbilirsin: + +1. `git reflog` rebase için tüm git komutlarını listele + +``` +38b323f HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/add_git_reflog +38b323f HEAD@{1}: rebase -i (pick): Clarify inc/dec operators +4fff859 HEAD@{2}: rebase -i (pick): Update java.html.markdown +34ed963 HEAD@{3}: rebase -i (pick): [yaml/en] Add more resources (#1666) +ed8ddf2 HEAD@{4}: rebase -i (pick): pythonstatcomp spanish translation (#1748) +2e6c386 HEAD@{5}: rebase -i (start): checkout 02fb96d +``` +2. Nereye reset'leyeceğini seç, şu durumda `2e6c386` veya `HEAD@{5}` +3. 'git reset --hard HEAD@{5}' bu repo'nu seçilen head'e eşitler +4. Rebase'e yeniden başlayabilir veya onu yalnız bırakabilirsin. + +[Ek Okuma.](https://git-scm.com/docs/git-reflog) + +### revert + +Revert commit'leri geri almada kullanılır. Projenin durumunu önceki bir noktaya +alan reset ile karıştırılmamalıdır. Revert, belirtilen commit'in tersine yeni bir +commit ekleyecektir. + +```bash +# Revert a specified commit +$ git revert <commit> +``` + +### rm + +git add'in tersine, git rm çalışma ağacından dosyaları kaldırır. + +```bash +# remove HelloWorld.c +$ git rm HelloWorld.c + +# Remove a file from a nested dir +$ git rm /pather/to/the/file/HelloWorld.c +``` + +## Daha Fazla Bilgi + +* [tryGit - Git'i öğrenmek için eğlenceli interaktif bir yol](http://try.github.io/levels/1/challenges/1) + +* [Git Dallanmayı Öğren - Git'i web üzerinde öğrenmek için en görsel ve interaktif yol](http://learngitbranching.js.org/) + +* [Udemy Git Tutorial: Kapsayıcı bir kılavuz](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/) + +* [Git Immersion - Git'in temelinden başlayan bir tur](http://gitimmersion.com/) + +* [git-scm - Video Tutorial](http://git-scm.com/videos) + +* [git-scm - Dökümantasyon](http://git-scm.com/docs) + +* [Atlassian Git - Tutorial & Workflow](https://www.atlassian.com/git/) + +* [SalesForce Kopya Kağıdı](http://res.cloudinary.com/hy4kyit2a/image/upload/SF_git_cheatsheet.pdf) + +* [GitGuys](http://www.gitguys.com/) + +* [Git - Basit bir kılavuz](http://rogerdudler.github.io/git-guide/index.html) + +* [Pro Git](http://www.git-scm.com/book/en/v2) + +* [Yeni başlayanlar için Git ve Github](http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners) diff --git a/tr-tr/markdown-tr.html.markdown b/tr-tr/markdown-tr.html.markdown index b8f11e39..6caba1da 100644 --- a/tr-tr/markdown-tr.html.markdown +++ b/tr-tr/markdown-tr.html.markdown @@ -11,7 +11,7 @@ filename: markdown-tr.md Markdown, 2004 yılında John Gruber tarafından oluşturuldu. Asıl amacı kolay okuma ve yazmayı sağlamakla beraber kolayca HTML (artık bir çok diğer formatlara) dönüşüm sağlamaktır. -```markdown +```md <!-- Markdown, HTML'i kapsar, yani her HTML dosyası geçerli bir Markdown dosyasıdır, bu demektir ki Markdown içerisinde HTML etiketleri kullanabiliriz, örneğin bu yorum elementi, ve markdown işleyicisinde etki etmezler. Fakat, markdown dosyası içerisinde HTML elementi oluşturursanız, diff --git a/uk-ua/java-ua.html.markdown b/uk-ua/java-ua.html.markdown index 1d600400..df642f73 100644 --- a/uk-ua/java-ua.html.markdown +++ b/uk-ua/java-ua.html.markdown @@ -30,7 +30,7 @@ JavaDoc-коментар виглядає так. Використовуєтьс // Імпорт класу ArrayList з пакета java.util import java.util.ArrayList; -// Імпорт усіх класів з пакета java.security +// Імпорт усіх класів з пакета java.security import java.security.*; // Кожний .java файл містить один зовнішній публічний клас, ім’я якого співпадає @@ -99,13 +99,13 @@ public class LearnJava { // Примітка: Java не має беззнакових типів. - // Float — 32-бітне число з рухомою комою одиничної точності за стандартом IEEE 754 + // Float — 32-бітне число з рухомою комою одиничної точності за стандартом IEEE 754 // 2^-149 <= float <= (2-2^-23) * 2^127 float fooFloat = 234.5f; // f або F використовується для позначення того, що змінна має тип float; // інакше трактується як double. - // Double — 64-бітне число з рухомою комою подвійної точності за стандартом IEEE 754 + // Double — 64-бітне число з рухомою комою подвійної точності за стандартом IEEE 754 // 2^-1074 <= x <= (2-2^-52) * 2^1023 double fooDouble = 123.4; @@ -130,13 +130,13 @@ public class LearnJava { // байтів, операції над ними виконуються функціями, які мають клас BigInteger // // BigInteger можна ініціалізувати, використовуючи масив байтів чи рядок. - + BigInteger fooBigInteger = new BigInteger(fooByteArray); // BigDecimal — Незмінні знакові дробові числа довільної точності // - // BigDecimal складається з двох частин: цілого числа довільної точності + // BigDecimal складається з двох частин: цілого числа довільної точності // з немасштабованим значенням та 32-бітного масштабованого цілого числа // // BigDecimal дозволяє розробникам контролювати десяткове округлення. @@ -147,10 +147,10 @@ public class LearnJava { // чи немасштабованим значенням (BigInteger) і масштабованим значенням (int). BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt); - + // Для дотримання заданої точності рекомендується використовувати - // конструктор, який приймає String - + // конструктор, який приймає String + BigDecimal tenCents = new BigDecimal("0.1"); @@ -295,7 +295,7 @@ public class LearnJava { // Виконається 10 разів, fooFor 0->9 } System.out.println("Значення fooFor: " + fooFor); - + // Вихід із вкладеного циклу через мітку outer: for (int i = 0; i < 10; i++) { @@ -306,7 +306,7 @@ public class LearnJava { } } } - + // Цикл For Each // Призначений для перебору масивів та колекцій int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9}; @@ -318,7 +318,7 @@ public class LearnJava { // Оператор вибору Switch Case // Оператор вибору працює з типами даних byte, short, char, int. - // Також працює з переліками Enum, + // Також працює з переліками Enum, // класом String та класами-обгортками примітивних типів: // Character, Byte, Short та Integer. int month = 3; @@ -334,7 +334,7 @@ public class LearnJava { break; } System.out.println("Результат Switch Case: " + monthString); - + // Починаючи з Java 7 і далі, вибір рядкових змінних здійснюється так: String myAnswer = "можливо"; switch(myAnswer) { @@ -398,7 +398,7 @@ public class LearnJava { // toString повертає рядкове представлення об’єкту. System.out.println("Інформація про об’єкт trek: " + trek.toString()); - + // У Java немає синтаксису для явного створення статичних колекцій. // Це можна зробити так: @@ -554,7 +554,7 @@ public interface Digestible { // Можна створити клас, що реалізує обидва інтерфейси. public class Fruit implements Edible, Digestible { - + @Override public void eat() { // ... @@ -694,41 +694,41 @@ public abstract class Mammal() public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, - THURSDAY, FRIDAY, SATURDAY + THURSDAY, FRIDAY, SATURDAY } // Перелік Day можна використовувати так: public class EnumTest { - + // Змінна того же типу, що й перелік Day day; - + public EnumTest(Day day) { this.day = day; } - + public void tellItLikeItIs() { switch (day) { case MONDAY: - System.out.println("Понеділкі важкі."); + System.out.println("Понеділки важкі."); break; - + case FRIDAY: System.out.println("П’ятниці краще."); break; - - case SATURDAY: + + case SATURDAY: case SUNDAY: System.out.println("Вихідні найліпші."); break; - + default: System.out.println("Середина тижня так собі."); break; } } - + public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); // => Понеділки важкі. @@ -737,7 +737,7 @@ public class EnumTest { } } -// Переліки набагато потужніші, ніж тут показано. +// Переліки набагато потужніші, ніж тут показано. // Тіло переліків може містити методи та інші змінні. // Дивіться більше тут: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html diff --git a/uk-ua/javascript-ua.html.markdown b/uk-ua/javascript-ua.html.markdown index 397b1c5e..6a64a623 100644 --- a/uk-ua/javascript-ua.html.markdown +++ b/uk-ua/javascript-ua.html.markdown @@ -45,7 +45,7 @@ doStuff() 3; // = 3 1.5; // = 1.5 -// Деякі прості арифметичні операції працють так, як ми очікуємо. +// Деякі прості арифметичні операції працюють так, як ми очікуємо. 1 + 1; // = 2 0.1 + 0.2; // = 0.30000000000000004 (а деякі - ні) 8 - 1; // = 7 @@ -106,7 +106,7 @@ null == undefined; // = true // ... але приведення не виконується при === "5" === 5; // = false -null === undefined; // = false +null === undefined; // = false // ... приведення типів може призвести до дивних результатів 13 + !0; // 14 @@ -171,7 +171,7 @@ myArray[3] = "світ"; // Об’єкти в JavaScript схожі на словники або асоціативні масиви в інших мовах var myObj = {key1: "Hello", key2: "World"}; -// Ключі - це рядки, але лапки не обов’язкі, якщо ключ задовольняє +// Ключі - це рядки, але лапки не обов’язкові, якщо ключ задовольняє // правилам формування назв змінних. Значення можуть бути будь-яких типів. var myObj = {myKey: "myValue", "my other key": 4}; @@ -258,7 +258,7 @@ function myFunction(thing) { return thing.toUpperCase(); } myFunction("foo"); // = "FOO" - + // Зверніть увагу, що значення яке буде повернено, повинно починатися на тому ж // рядку, що і ключове слово return, інакше завжди буде повертатися значення undefined // через автоматичну вставку крапки з комою @@ -332,7 +332,7 @@ var myObj = { }; myObj.myFunc(); // = "Hello, world!" -// Функції, що прикріплені до об’єктів мають доступ до поточного об’єкта за +// Функції, що прикріплені до об’єктів мають доступ до поточного об’єкта за // допомогою ключового слова this. myObj = { myString: "Hello, world!", @@ -348,7 +348,7 @@ myObj.myFunc(); // = "Hello, world!" var myFunc = myObj.myFunc; myFunc(); // = undefined -// Функція може бути присвоєна іншому об’єкту. Тоді вона матиме доступ до +// Функція може бути присвоєна іншому об’єкту. Тоді вона матиме доступ до // цього об’єкта через this var myOtherFunc = function() { return this.myString.toUpperCase(); @@ -371,7 +371,7 @@ Math.min(42, 6, 27); // = 6 Math.min([42, 6, 27]); // = NaN (Ой-ой!) Math.min.apply(Math, [42, 6, 27]); // = 6 -// Але call і apply — тимчасові. Коли ми хочемо зв’язати функцію і об’єкт +// Але call і apply — тимчасові. Коли ми хочемо зв’язати функцію і об’єкт // використовують bind var boundFunc = anotherFunc.bind(myObj); boundFunc(" Hello!"); // = "Hello world, Hello!" @@ -475,7 +475,7 @@ if (Object.create === undefined) { // не перезаписуємо метод // Створюємо правильний конструктор з правильним прототипом var Constructor = function(){}; Constructor.prototype = proto; - + return new Constructor(); } } diff --git a/uk-ua/python-ua.html.markdown b/uk-ua/python-ua.html.markdown new file mode 100644 index 00000000..2406678d --- /dev/null +++ b/uk-ua/python-ua.html.markdown @@ -0,0 +1,818 @@ +--- +language: python +lang: uk-ua +contributors: + - ["Louie Dinh", "http://ldinh.ca"] + - ["Amin Bandali", "https://aminb.org"] + - ["Andre Polykanine", "https://github.com/Oire"] + - ["evuez", "http://github.com/evuez"] + - ["asyne", "https://github.com/justblah"] + - ["habi", "http://github.com/habi"] +translators: + - ["Oleg Gromyak", "https://github.com/ogroleg"] +filename: learnpython-ua.py +--- + +Мову Python створив Гвідо ван Россум на початку 90-х. Наразі це одна з +найбільш популярних мов. Я закохався у Python завдяки простому і зрозумілому +синтаксису. Це майже як виконуваний псевдокод. + +З вдячністю чекаю ваших відгуків: [@louiedinh](http://twitter.com/louiedinh) +або louiedinh [at] [поштовий сервіс від Google] + +Примітка: Ця стаття стосується Python 2.7, проте має працювати і +у інших версіях Python 2.x. Python 2.7 підходить до кінця свого терміну, +його підтримку припинять у 2020, тож наразі краще починати вивчення Python +з версії 3.x. +Аби вивчити Python 3.x, звертайтесь до статті по Python 3. + +```python +# Однорядкові коментарі починаються з символу решітки. + +""" Текст, що займає декілька рядків, + може бути записаний з використанням 3 знаків " і + зазвичай використовується у якості + вбудованої документації +""" + +#################################################### +## 1. Примітивні типи даних та оператори +#################################################### + +# У вас є числа +3 # => 3 + +# Математика працює досить передбачувано +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7 + +# А ось з діленням все трохи складніше. Воно цілочисельне і результат +# автоматично округлюється у меншу сторону. +5 / 2 # => 2 + +# Аби правильно ділити, спершу варто дізнатися про числа +# з плаваючою комою. +2.0 # Це число з плаваючою комою +11.0 / 4.0 # => 2.75 ох... Так набагато краще + +# Результат цілочисельного ділення округлюється у меншу сторону +# як для додатніх, так і для від'ємних чисел. +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 # Працює і для чисел з плаваючою комою +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 + +# Зверніть увагу, що ми також можемо імпортувати модуль для ділення, +# див. розділ Модулі +# аби звичне ділення працювало при використанні лише '/'. +from __future__ import division + +11 / 4 # => 2.75 ...звичне ділення +11 // 4 # => 2 ...цілочисельне ділення + +# Залишок від ділення +7 % 3 # => 1 + +# Піднесення до степеня +2 ** 4 # => 16 + +# Приорітет операцій вказується дужками +(1 + 3) * 2 # => 8 + +# Логічні оператори +# Зверніть увагу: ключові слова «and» і «or» чутливі до регістру букв +True and False # => False +False or True # => True + +# Завважте, що логічні оператори також використовуються і з цілими числами +0 and 2 # => 0 +-5 or 0 # => -5 +0 == False # => True +2 == True # => False +1 == True # => True + +# Для заперечення використовується not +not True # => False +not 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 + +# Порівняння можуть бути записані ланцюжком! +1 < 2 < 3 # => True +2 < 3 < 2 # => False + +# Рядки позначаються символом " або ' +"Це рядок." +'Це теж рядок.' + +# І рядки також можна додавати! +"Привіт " + "світ!" # => "Привіт світ!" +# Рядки можна додавати і без '+' +"Привіт " "світ!" # => "Привіт світ!" + +# ... або множити +"Привіт" * 3 # => "ПривітПривітПривіт" + +# З рядком можна працювати як зі списком символів +"Це рядок"[0] # => 'Ц' + +# Ви можете дізнатися довжину рядка +len("Це рядок") # => 8 + +# Символ % використовується для форматування рядків, наприклад: +"%s можуть бути %s" % ("рядки", "інтерпольовані") + +# Новий спосіб форматування рядків — використання методу format. +# Це бажаний спосіб. +"{} є {}".format("Це", "заповнювач") +"{0} можуть бути {1}".format("рядки", "форматовані") +# Якщо ви не хочете рахувати, то можете скористатися ключовими словами. +"{name} хоче з'істи {food}".format(name="Боб", food="лазанью") + +# None - це об'єкт +None # => None + +# Не використовуйте оператор рівності '=='' для порівняння +# об'єктів з None. Використовуйте для цього «is» +"etc" is None # => False +None is None # => True + +# Оператор 'is' перевіряє ідентичність об'єктів. Він не +# дуже корисний при роботі з примітивними типами, проте +# незамінний при роботі з об'єктами. + +# None, 0 і порожні рядки/списки рівні False. +# Всі інші значення рівні True +bool(0) # => False +bool("") # => False + + +#################################################### +## 2. Змінні та колекції +#################################################### + +# В Python є оператор print +print "Я Python. Приємно познайомитись!" # => Я Python. Приємно познайомитись! + +# Отримати дані з консолі просто +input_string_var = raw_input( + "Введіть щось: ") # Повертає дані у вигляді рядка +input_var = input("Введіть щось: ") # Працює з даними як з кодом на python +# Застереження: будьте обережні при використанні методу input() + +# Оголошувати змінні перед ініціалізацією не потрібно. +some_var = 5 # За угодою використовується нижній_регістр_з_підкресленнями +some_var # => 5 + +# При спробі доступу до неініціалізованої змінної +# виникне виняткова ситуація. +# Див. розділ Потік управління, аби дізнатись про винятки більше. +some_other_var # Помилка в імені + +# if може використовуватися як вираз +# Такий запис еквівалентний тернарному оператору '?:' у мові С +"yahoo!" if 3 > 2 else 2 # => "yahoo!" + +# Списки зберігають послідовності +li = [] +# Можна одразу створити заповнений список +other_li = [4, 5, 6] + +# Об'єкти додаються у кінець списку за допомогою методу append +li.append(1) # li тепер дорівнює [1] +li.append(2) # li тепер дорівнює [1, 2] +li.append(4) # li тепер дорівнює [1, 2, 4] +li.append(3) # li тепер дорівнює [1, 2, 4, 3] +# І видаляються з кінця методом pop +li.pop() # => повертає 3 і li стає рівним [1, 2, 4] +# Повернемо елемент назад +li.append(3) # li тепер знову дорівнює [1, 2, 4, 3] + +# Поводьтесь зі списком як зі звичайним масивом +li[0] # => 1 +# Присвоюйте нові значення вже ініціалізованим індексам за допомогою = +li[0] = 42 +li[0] # => 42 +li[0] = 1 # Зверніть увагу: повертаємось до попереднього значення +# Звертаємось до останнього елементу +li[-1] # => 3 + +# Спроба вийти за границі масиву призводить до помилки в індексі +li[4] # помилка в індексі + +# Можна звертатися до діапазону, використовуючи так звані зрізи +# (Для тих, хто любить математику: це називається замкнуто-відкритий інтервал). +li[1:3] # => [2, 4] +# Опускаємо початок +li[2:] # => [4, 3] +# Опускаємо кінець +li[:3] # => [1, 2, 4] +# Вибираємо кожен другий елемент +li[::2] # => [1, 4] +# Перевертаємо список +li[::-1] # => [3, 4, 2, 1] +# Використовуйте суміш вищеназваного для більш складних зрізів +# li[початок:кінець:крок] + +# Видаляємо довільні елементи зі списку оператором del +del li[2] # li тепер [1, 2, 3] + +# Ви можете додавати списки +li + other_li # => [1, 2, 3, 4, 5, 6] +# Зверніть увагу: значення li та other_li при цьому не змінились. + +# Поєднувати списки можна за допомогою методу extend +li.extend(other_li) # Тепер li дорівнює [1, 2, 3, 4, 5, 6] + +# Видалити перше входження значення +li.remove(2) # Тепер li дорівнює [1, 3, 4, 5, 6] +li.remove(2) # Помилка значення, оскільки у списку li немає 2 + +# Вставити елемент за вказаним індексом +li.insert(1, 2) # li знову дорівнює [1, 2, 3, 4, 5, 6] + +# Отримати індекс першого знайденого елементу +li.index(2) # => 1 +li.index(7) # Помилка значення, оскільки у списку li немає 7 + +# Перевірити елемент на входження у список можна оператором in +1 in li # => True + +# Довжина списку обчислюється за допомогою функції len +len(li) # => 6 + +# Кортежі схожі на списки, лише незмінні +tup = (1, 2, 3) +tup[0] # => 1 +tup[0] = 3 # Виникає помилка типу + +# Все те ж саме можна робити і з кортежами +len(tup) # => 3 +tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) +tup[:2] # => (1, 2) +2 in tup # => True + +# Ви можете розпаковувати кортежі (або списки) у змінні +a, b, c = (1, 2, 3) # a == 1, b == 2 и c == 3 +d, e, f = 4, 5, 6 # дужки можна опустити +# Кортежі створюються за замовчуванням, якщо дужки опущено +g = 4, 5, 6 # => (4, 5, 6) +# Дивіться, як легко обміняти значення двох змінних +e, d = d, e # тепер d дорівнює 5, а e дорівнює 4 + +# Словники містять асоціативні масиви +empty_dict = {} +# Ось так описується попередньо заповнений словник +filled_dict = {"one": 1, "two": 2, "three": 3} + +# Значення можна отримати так само, як і зі списку +filled_dict["one"] # => 1 + +# Можна отримати всі ключі у виді списку за допомогою методу keys +filled_dict.keys() # => ["three", "two", "one"] +# Примітка: збереження порядку ключів у словників не гарантується +# Ваші результати можуть не співпадати з цими. + +# Можна отримати і всі значення у вигляді списку, використовуйте метод values +filled_dict.values() # => [3, 2, 1] +# Те ж зауваження щодо порядку ключів діє і тут + +# Отримуйте всі пари ключ-значення у вигляді списку кортежів +# за допомогою "items()" +filled_dict.items() # => [("one", 1), ("two", 2), ("three", 3)] + +# За допомогою оператору in можна перевіряти ключі на входження у словник +"one" in filled_dict # => True +1 in filled_dict # => False + +# Спроба отримати значення за неіснуючим ключем викине помилку ключа +filled_dict["four"] # помилка ключа + +# Аби уникнути цього, використовуйте метод get() +filled_dict.get("one") # => 1 +filled_dict.get("four") # => None +# Метод get також приймає аргумент за замовчуванням, значення якого буде +# повернуто при відсутності вказаного ключа +filled_dict.get("one", 4) # => 1 +filled_dict.get("four", 4) # => 4 +# Зверніть увагу, що filled_dict.get("four") все ще => None +# (get не встановлює значення елементу словника) + +# Присвоюйте значення ключам так само, як і в списках +filled_dict["four"] = 4 # тепер filled_dict["four"] => 4 + +# Метод setdefault() вставляє пару ключ-значення лише +# за відсутності такого ключа +filled_dict.setdefault("five", 5) # filled_dict["five"] повертає 5 +filled_dict.setdefault("five", 6) # filled_dict["five"] все ще повертає 5 + + +# Множини містять... ну, загалом, множини +# (які схожі на списки, проте в них не може бути елементів, які повторюються) +empty_set = set() +# Ініціалізація множини набором значень +some_set = set([1,2,2,3,4]) # some_set тепер дорівнює set([1, 2, 3, 4]) + +# Порядок не гарантовано, хоча інколи множини виглядають відсортованими +another_set = set([4, 3, 2, 2, 1]) # another_set тепер set([1, 2, 3, 4]) + +# Починаючи з Python 2.7, ви можете використовувати {}, аби створити множину +filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4} + +# Додавання нових елементів у множину +filled_set.add(5) # filled_set тепер дорівнює {1, 2, 3, 4, 5} + +# Перетин множин: & +other_set = {3, 4, 5, 6} +filled_set & other_set # => {3, 4, 5} + +# Об'єднання множин: | +filled_set | other_set # => {1, 2, 3, 4, 5, 6} + +# Різниця множин: - +{1,2,3,4} - {2,3,5} # => {1, 4} + +# Симетрична різниця множин: ^ +{1, 2, 3, 4} ^ {2, 3, 5} # => {1, 4, 5} + +# Перевіряємо чи множина зліва є надмножиною множини справа +{1, 2} >= {1, 2, 3} # => False + +# Перевіряємо чи множина зліва є підмножиною множини справа +{1, 2} <= {1, 2, 3} # => True + +# Перевірка на входження у множину: in +2 in filled_set # => True +10 in filled_set # => False + + +#################################################### +## 3. Потік управління +#################################################### + +# Для початку створимо змінну +some_var = 5 + +# Так виглядає вираз if. Відступи у python дуже важливі! +# результат: «some_var менше, ніж 10» +if some_var > 10: + print("some_var набагато більше, ніж 10.") +elif some_var < 10: # Вираз elif є необов'язковим. + print("some_var менше, ніж 10.") +else: # Це теж необов'язково. + print("some_var дорівнює 10.") + + +""" +Цикли For проходять по спискам + +Результат: + собака — це ссавець + кішка — це ссавець + миша — це ссавець +""" +for animal in ["собака", "кішка", "миша"]: + # Можете використовувати оператор {0} для інтерполяції форматованих рядків + print "{0} — це ссавець".format(animal) + +""" +"range(число)" повертає список чисел +від нуля до заданого числа +Друкує: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print(i) +""" +"range(нижня_границя, верхня_границя)" повертає список чисел +від нижньої границі до верхньої +Друкує: + 4 + 5 + 6 + 7 +""" +for i in range(4, 8): + print i + +""" +Цикли while продовжуються до тих пір, поки вказана умова не стане хибною. +Друкує: + 0 + 1 + 2 + 3 +""" +x = 0 +while x < 4: + print(x) + x += 1 # Короткий запис для x = x + 1 + +# Обробляйте винятки блоками try/except + +# Працює у Python 2.6 і вище: +try: + # Аби створити виняток, використовується raise + raise IndexError("Помилка у індексі!") +except IndexError as e: + pass # pass — оператор, який нічого не робить. Зазвичай тут відбувається + # відновлення після помилки. +except (TypeError, NameError): + pass # Винятки можна обробляти групами, якщо потрібно. +else: # Необов'язковий вираз. Має слідувати за останнім блоком except + print("Все добре!") # Виконається лише якщо не було ніяких винятків +finally: # Виконується у будь-якому випадку + print "Тут ми можемо звільнити ресурси" + +# Замість try/finally для звільнення ресурсів +# ви можете використовувати вираз with +with open("myfile.txt") as f: + for line in f: + print line + + +#################################################### +## 4. Функції +#################################################### + +# Використовуйте def для створення нових функцій +def add(x, y): + print "x дорівнює {0}, а y дорівнює {1}".format(x, y) + return x + y # Повертайте результат за допомогою ключового слова return + + +# Виклик функції з аргументами +add(5, 6) # => друкує «x дорівнює 5, а y дорівнює 6» і повертає 11 + +# Інший спосіб виклику функції — виклик з іменованими аргументами +add(y=6, x=5) # Іменовані аргументи можна вказувати у будь-якому порядку + + +# Ви можете визначити функцію, яка приймає змінну кількість аргументів, +# які будуть інтерпретовані як кортеж, за допомогою * +def varargs(*args): + return args + + +varargs(1, 2, 3) # => (1,2,3) + + +# А також можете визначити функцію, яка приймає змінне число +# іменованих аргументів, котрі будуть інтерпретовані як словник, за допомогою ** +def keyword_args(**kwargs): + return kwargs + + +# Давайте подивимось що з цього вийде +keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"} + +# Якщо хочете, можете використовувати обидва способи одночасно +def all_the_args(*args, **kwargs): + print(args) + print(kwargs) + + +""" +all_the_args(1, 2, a=3, b=4) друкує: + (1, 2) + {"a": 3, "b": 4} +""" + +# Коли викликаєте функції, то можете зробити навпаки! +# Використовуйте символ * аби розпакувати позиційні аргументи і +# ** для іменованих аргументів +args = (1, 2, 3, 4) +kwargs = {"a": 3, "b": 4} +all_the_args(*args) # еквівалентно foo(1, 2, 3, 4) +all_the_args(**kwargs) # еквівалентно foo(a=3, b=4) +all_the_args(*args, **kwargs) # еквівалентно foo(1, 2, 3, 4, a=3, b=4) + +# ви можете передавати довільне число позиційних або іменованих аргументів +# іншим функціям, які їх приймають, розпаковуючи за допомогою +# * або ** відповідно +def pass_all_the_args(*args, **kwargs): + all_the_args(*args, **kwargs) + print varargs(*args) + print keyword_args(**kwargs) + + +# Область визначення функцій +x = 5 + + +def set_x(num): + # Локальна змінна x - не те ж саме, що глобальна змінна x + x = num # => 43 + print x # => 43 + + +def set_global_x(num): + global x + print x # => 5 + x = num # глобальна змінна x тепер дорівнює 6 + print x # => 6 + + +set_x(43) +set_global_x(6) + +# В Python функції є об'єктами першого класу +def create_adder(x): + def adder(y): + return x + y + + return adder + + +add_10 = create_adder(10) +add_10(3) # => 13 + +# Також є і анонімні функції +(lambda x: x > 2)(3) # => True +(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5 + +# Присутні вбудовані функції вищого порядку +map(add_10, [1, 2, 3]) # => [11, 12, 13] +map(max, [1, 2, 3], [4, 2, 1]) # => [4, 2, 3] + +filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] + +# Для зручного відображення і фільтрації можна використовувати +# включення у вигляді списків +[add_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] + +# Ви також можете скористатися включеннями множин та словників +{x for x in 'abcddeef' if x in 'abc'} # => {'a', 'b', 'c'} +{x: x ** 2 for x in range(5)} # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} + + +#################################################### +## 5. Класи +#################################################### + +# Аби отримати клас, ми наслідуємо object. +class Human(object): + # Атрибут класу. Він розділяється всіма екземплярами цього класу. + species = "H. sapiens" + + # Звичайний конструктор, буде викликаний при ініціалізації екземпляру класу + # Зверніть увагу, що подвійне підкреслення на початку та наприкінці імені + # використовується для позначення об'єктів та атрибутів, + # які використовуються Python, але знаходяться у просторах імен, + # якими керує користувач. Не варто вигадувати для них імена самостійно. + def __init__(self, name): + # Присвоєння значення аргумента атрибуту класу name + self.name = name + + # Ініціалізуємо властивість + self.age = 0 + + # Метод екземпляру. Всі методи приймають self у якості першого аргументу + def say(self, msg): + return "%s: %s" % (self.name, msg) + + # Методи класу розділяються між усіма екземплярами + # Вони викликаються з вказанням викликаючого класу + # у якості першого аргументу + @classmethod + def get_species(cls): + return cls.species + + # Статичний метод викликається без посилання на клас або екземпляр + @staticmethod + def grunt(): + return "*grunt*" + + # Властивість. + # Перетворює метод age() в атрибут тільки для читання + # з таким же ім'ям. + @property + def age(self): + return self._age + + # Це дозволяє змінювати значення властивості + @age.setter + def age(self, age): + self._age = age + + # Це дозволяє видаляти властивість + @age.deleter + def age(self): + del self._age + + +# Створюємо екземпляр класу +i = Human(name="Данило") +print(i.say("привіт")) # Друкує: «Данило: привіт» + +j = Human("Меланка") +print(j.say("Привіт")) # Друкує: «Меланка: привіт» + +# Виклик методу класу +i.get_species() # => "H. sapiens" + +# Зміна розділюваного атрибуту +Human.species = "H. neanderthalensis" +i.get_species() # => "H. neanderthalensis" +j.get_species() # => "H. neanderthalensis" + +# Виклик статичного методу +Human.grunt() # => "*grunt*" + +# Оновлюємо властивість +i.age = 42 + +# Отримуємо значення +i.age # => 42 + +# Видаляємо властивість +del i.age +i.age # => виникає помилка атрибуту + +#################################################### +## 6. Модулі +#################################################### + +# Ви можете імпортувати модулі +import math + +print(math.sqrt(16)) # => 4 + +# Ви можете імпортувати окремі функції з модуля +from math import ceil, floor + +print(ceil(3.7)) # => 4.0 +print(floor(3.7)) # => 3.0 + +# Можете імпортувати всі функції модуля. +# Попередження: краще так не робіть +from math import * + +# Можете скорочувати імена модулів +import math as m + +math.sqrt(16) == m.sqrt(16) # => True +# Ви також можете переконатися, що функції еквівалентні +from math import sqrt + +math.sqrt == m.sqrt == sqrt # => True + +# Модулі в Python — це звичайні Python-файли. Ви +# можете писати свої модулі та імпортувати їх. Назва +# модуля співпадає з назвою файлу. + +# Ви можете дізнатися, які функції та атрибути визначені +# в модулі +import math + +dir(math) + + +# Якщо у вас є Python скрипт з назвою math.py у тій же папці, що +# і ваш поточний скрипт, то файл math.py +# може бути завантажено замість вбудованого у Python модуля. +# Так трапляється, оскільки локальна папка має перевагу +# над вбудованими у Python бібліотеками. + +#################################################### +## 7. Додатково +#################################################### + +# Генератори +# Генератор "генерує" значення тоді, коли вони запитуються, замість того, +# щоб зберігати все одразу + +# Метод нижче (*НЕ* генератор) подвоює всі значення і зберігає їх +# в `double_arr`. При великих розмірах може знадобитися багато ресурсів! +def double_numbers(iterable): + double_arr = [] + for i in iterable: + double_arr.append(i + i) + return double_arr + + +# Тут ми спочатку подвоюємо всі значення, потім повертаємо їх, +# аби перевірити умову +for value in double_numbers(range(1000000)): # `test_non_generator` + print value + if value > 5: + break + + +# Натомість ми можемо скористатися генератором, аби "згенерувати" +# подвійне значення, як тільки воно буде запитане +def double_numbers_generator(iterable): + for i in iterable: + yield i + i + + +# Той самий код, але вже з генератором, тепер дозволяє нам пройтися по +# значенням і подвоювати їх одне за одним якраз тоді, коли вони обробляються +# за нашою логікою, одне за одним. А як тільки ми бачимо, що value > 5, ми +# виходимо з циклу і більше не подвоюємо більшість значень, +# які отримали на вхід (НАБАГАТО ШВИДШЕ!) +for value in double_numbers_generator(xrange(1000000)): # `test_generator` + print value + if value > 5: + break + +# Між іншим: ви помітили використання `range` у `test_non_generator` і +# `xrange` у `test_generator`? +# Як `double_numbers_generator` є версією-генератором `double_numbers`, так +# і `xrange` є аналогом `range`, але у вигляді генератора. +# `range` поверне нам масив з 1000000 значень +# `xrange`, у свою чергу, згенерує 1000000 значень для нас тоді, +# коли ми їх запитуємо / будемо проходитись по ним. + +# Аналогічно включенням у вигляді списків, ви можете створювати включення +# у вигляді генераторів. +values = (-x for x in [1, 2, 3, 4, 5]) +for x in values: + print(x) # друкує -1 -2 -3 -4 -5 + +# Включення у вигляді генератора можна явно перетворити у список +values = (-x for x in [1, 2, 3, 4, 5]) +gen_to_list = list(values) +print(gen_to_list) # => [-1, -2, -3, -4, -5] + +# Декоратори +# Декоратор – це функція вищого порядку, яка приймає та повертає функцію. +# Простий приклад використання – декоратор add_apples додає елемент 'Apple' в +# список fruits, який повертає цільова функція get_fruits. +def add_apples(func): + def get_fruits(): + fruits = func() + fruits.append('Apple') + return fruits + return get_fruits + +@add_apples +def get_fruits(): + return ['Banana', 'Mango', 'Orange'] + +# Друкуємо список разом з елементом 'Apple', який знаходиться в ньому: +# Banana, Mango, Orange, Apple +print ', '.join(get_fruits()) + +# У цьому прикладі beg обертає say +# Beg викличе say. Якщо say_please дорівнюватиме True, то повідомлення, +# що повертається, буде змінено. +from functools import wraps + + +def beg(target_function): + @wraps(target_function) + def wrapper(*args, **kwargs): + msg, say_please = target_function(*args, **kwargs) + if say_please: + return "{} {}".format(msg, "Будь ласка! Я бідний :(") + return msg + + return wrapper + + +@beg +def say(say_please=False): + msg = "Ви можете купити мені пива?" + return msg, say_please + + +print say() # Ви можете купити мені пива? +print say(say_please=True) # Ви можете купити мені пива? Будь ласка! Я бідний :( +``` + +## Готові до більшого? + +### Безкоштовні онлайн-матеріали + +* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +* [Dive Into Python](http://www.diveintopython.net/) +* [Официальная документация](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) + +### Платні + +* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) +* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) +* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) + diff --git a/vi-vn/markdown-vi.html.markdown b/vi-vn/markdown-vi.html.markdown index 0ba267f9..89b59253 100644 --- a/vi-vn/markdown-vi.html.markdown +++ b/vi-vn/markdown-vi.html.markdown @@ -28,7 +28,7 @@ Markdown có sự khác biệt trong cách cài đặt giữa các trình phân ## Phần tử HTML Markdown là tập cha của HTML, vì vậy bất cứ file HTML nào đều là Markdown đúng. -```markdown +```md <!-- Điều này đồng nghĩa ta có thể sử dụng các phần tử HTML trong Markdown, ví dụ như phần tử chú thích/comment. Tuy nhiên, nếu sử dụng một phần tử HTML trong file Markdown, @@ -40,7 +40,7 @@ ta không thể sử dụng cú pháp Markdown cho nội dung bên trong phần Ta có thể tạo các phần tử đầu mục HTML từ `<h1>` cho đến `<h6>` dễ dàng bằng cách thêm số lượng dấu thăng (#) đằng trước chuỗi cần tạo đầu mục. -```markdown +```md # Đây là đầu mục <h1> ## Đây là đầu mục <h2> ### Đây là đầu mục <h3> @@ -50,7 +50,7 @@ bằng cách thêm số lượng dấu thăng (#) đằng trước chuỗi cần ``` Markdown còn cung cấp cách khác để tạo đầu mục hạng nhất h1 và hạng nhì h2. -```markdown +```md Đây là đầu mục h1 ============= @@ -62,7 +62,7 @@ Markdown còn cung cấp cách khác để tạo đầu mục hạng nhất h1 v Văn bản có thể được định dạng dễ dàng như in nghiêng hay làm đậm sử dụng Markdown. -```markdown +```md *Đoạn văn bản này được in nghiêng.* _Và đoạn này cũng như vậy._ @@ -76,7 +76,7 @@ __Và đoạn này cũng vậy.__ Trong cài đặt Markdown để hiển thị file của GitHub,ta còn có gạch ngang: -```markdown +```md ~~Đoạn văn bản này được gạch ngang.~~ ``` ## Đoạn văn @@ -84,7 +84,7 @@ Trong cài đặt Markdown để hiển thị file của GitHub,ta còn có gạ Đoạn văn bao gồm một hay nhiều dòng văn bản liên tiếp nhau được phân cách bởi một hay nhiều dòng trống. -```markdown +```md Đây là đoạn văn thứ nhất. Đây là đoạn văn thứ hai. @@ -97,7 +97,7 @@ Dòng này vẫn thuộc đoạn văn thứ hai, do không có cách dòng. Nếu cần chèn thêm thẻ ngắt dòng `<br />` của HTML, ta có thể kết thúc đoạn văn bản bằng cách thêm vào từ 2 dấu cách (space) trở lên và bắt đầu đoạn văn bản mới. -```markdown +```md Dòng này kết thúc với 2 dấu cách (highlight để nhìn thấy). Có phần tử <br /> ở bên trên. @@ -105,7 +105,7 @@ Có phần tử <br /> ở bên trên. Khối trích dẫn được sử dụng với kí tự > -```markdown +```md > Đây là khối trích dẫn. Ta có thể > ngắt dòng thủ công và thêm kí tự `>` trước mỗi dòng hoặc ta có thể để dòng tự ngắt nếu cần thiệt khi quá dài. > Không có sự khác biệt nào, chỉ cần nó bắt đầu với kí tự `>` @@ -120,7 +120,7 @@ Khối trích dẫn được sử dụng với kí tự > Danh sách không có thứ tự có thể được tạo sử dụng dấu sao, dấu cộng hay dấu trừ đầu dòng. -```markdown +```md * Một mục * Một mục * Một mục nữa @@ -140,7 +140,7 @@ hay Danh sách có thứ tự được tạo bởi một số theo sau bằng một dấu chấm. -```markdown +```md 1. Mục thứ nhất 2. Mục thứ hai 3. Mục thứ ba @@ -148,7 +148,7 @@ Danh sách có thứ tự được tạo bởi một số theo sau bằng một Ta không nhất thiết phải điền số thứ thự cho chỉ mục đúng mà Markdown sẽ tự hiển thị danh sách theo thứ tự đã được sắp xếp, tuy nhiên cách làm này không tốt! -```markdown +```md 1. Mục thứ nhất 1. Mục thứ hai 1. Mục thứ ba @@ -157,7 +157,7 @@ Ta không nhất thiết phải điền số thứ thự cho chỉ mục đúng Ta còn có thể sử dụng danh sách con -```markdown +```md 1. Mục thứ nhất 2. Mục thứ hai 3. Mục thứ ba @@ -168,7 +168,7 @@ Ta còn có thể sử dụng danh sách con Markdown còn cung cấp danh mục (checklist). Nó sẽ hiển thị ra hộp đánh dấu dạng HTML. -```markdown +```md Boxes below without the 'x' are unchecked HTML checkboxes. - [ ] First task to complete. - [ ] Second task that needs done @@ -180,14 +180,14 @@ This checkbox below will be a checked HTML checkbox. Ta có thể đánh dấu một đoạn code (tương tự sử dụng phần tử HTML `<code>`) bằng việc thụt đầu dòng sử dụng bốn dấu cách (space) hoặc một dấu nhảy (tab) -```markdown +```md This is code So is this ``` Ta còn có thể thêm dấu nhảy (hoặc thêm vào bốn dấu cách nữa) để căn chỉnh phần bên trong đoạn code -```markdown +```md my_array.each do |item| puts item end @@ -195,7 +195,7 @@ Ta còn có thể thêm dấu nhảy (hoặc thêm vào bốn dấu cách nữa) Code hiển thị cùng dòng có thể được đánh dấu sử dụng cặp ``. -```markdown +```md John didn't even know what the `go_to()` function did! ``` @@ -217,7 +217,7 @@ highlighting of the language you specify after the \`\`\` Dòng kẻ ngang (`<hr />`) có thể được thêm vào dễ dàng sử dụng từ 3 kí tự sao (*) hoặc gạch ngang (-), không quan trọng có khoảng cách giữa các kí tự hay không. -```markdown +```md *** --- - - - @@ -228,17 +228,17 @@ Dòng kẻ ngang (`<hr />`) có thể được thêm vào dễ dàng sử dụng Một trong những thứ tốt nhất khi làm việc với Markdown là khả năng tạo liên kết hết sức dễ dàng. Đoạn text hiển thị được đóng trong cặp ngoặc vuông [] kèm theo đường dẫn url trong cặp ngoặc tròn (). -```markdown +```md [Click me!](http://test.com/) ``` Ta còn có thể tạo tiêu đề cho liên kết sử dụng cặp ngoặc nháy bên trong cặp ngoặc tròn -```markdown +```md [Click me!](http://test.com/ "Link to Test.com") ``` Đường dẫn tương đối cũng hoạt động. -```markdown +```md [Go to music](/music/). ``` @@ -264,7 +264,7 @@ Nhưng nó không được sử dụng rộng rãi. Hiển thị ảnh tương tự như liên kết nhưng có thêm dấu chấm than đằng trước -```markdown +```md ![Thuộc tính alt cho ảnh](http://imgur.com/myimage.jpg "Tiêu đề tùy chọn") ``` @@ -278,20 +278,20 @@ Và kiểu tham chiếu cũng hoạt động như vậy. ### Tự động đặt liên kết -```markdown +```md <http://testwebsite.com/> tương đương với [http://testwebsite.com/](http://testwebsite.com/) ``` ### Tự động đặt liên kết cho email -```markdown +```md <foo@bar.com> ``` ### Hiển thị Kí tự đặc biệt -```markdown +```md Khi ta muốn viết *đoạn văn bản này có dấu sao bao quanh* nhưng ta không muốn nó bị in nghiêng, ta có thể sử dụng: \*đoạn văn bản này có dấu sao bao quanh\*. ``` @@ -299,7 +299,7 @@ Khi ta muốn viết *đoạn văn bản này có dấu sao bao quanh* nhưng ta Trong Markdown của Github, ta có thể sử dụng thẻ `<kbd>` để thay cho phím trên bàn phím. -```markdown +```md Máy treo? Thử bấm tổ hợp <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd> ``` @@ -307,7 +307,7 @@ Máy treo? Thử bấm tổ hợp Bảng biểu được hỗ trợ trên Markdown của GitHub, Jira, Trello, v.v và khá khó viết: -```markdown +```md | Cột 1 | Cột2 | Cột 3 | | :----------- | :------: | ------------: | | Căn trái | Căn giữa | Căn phải | @@ -315,7 +315,7 @@ Bảng biểu được hỗ trợ trên Markdown của GitHub, Jira, Trello, v.v ``` Hoặc có thể sử dụng kết quả dưới đây -```markdown +```md Cột 1 | Cột 2 | Cột 3 :-- | :-: | --: blah | blah | blah diff --git a/vim.html.markdown b/vim.html.markdown index d5c4e865..0eb2c37e 100644 --- a/vim.html.markdown +++ b/vim.html.markdown @@ -34,6 +34,11 @@ specific points in the file, and for fast editing. k # Move up one line l # Move right one character + Ctrl+B # Move back one full screen + Ctrl+F # Move forward one full screen + Ctrl+D # Move forward 1/2 a screen + Ctrl+U # Move back 1/2 a screen + # Moving within the line 0 # Move to beginning of line diff --git a/visualbasic.html.markdown b/visualbasic.html.markdown index 041641d3..63f224b7 100644 --- a/visualbasic.html.markdown +++ b/visualbasic.html.markdown @@ -5,7 +5,7 @@ contributors: filename: learnvisualbasic.vb --- -```vbnet +``` Module Module1 Sub Main() diff --git a/zh-cn/awk-cn.html.markdown b/zh-cn/awk-cn.html.markdown new file mode 100644 index 00000000..3fea782b --- /dev/null +++ b/zh-cn/awk-cn.html.markdown @@ -0,0 +1,326 @@ +--- +language: awk +contributors: + - ["Marshall Mason", "http://github.com/marshallmason"] +translators: + - ["Tian Zhipeng", "https://github.com/tianzhipeng-git"] +filename: learnawk-cn.awk +lang: zh-cn +--- + +AWK是POSIX兼容的UNIX系统中的标准工具. 它像简化版的Perl, 非常适用于文本处理任务和其他脚本类需求. +它有着C风格的语法, 但是没有分号, 没有手动内存管理, 没有静态类型. +他擅长于文本处理, 你可以通过shell脚本调用AWK, 也可以用作独立的脚本语言. + +为什么使用AWK而不是Perl, 大概是因为AWK是UNIX的一部分, 你总能依靠它, 而Perl已经前途未卜了. +AWK比Perl更易读. 对于简单的文本处理脚本, 特别是按行读取文件, 按分隔符分隔处理, AWK极可能是正确的工具. + +```awk +#!/usr/bin/awk -f + +# 注释使用井号 + +# AWK程序由一系列 模式(patterns) 和 动作(actions) 组成. +# 最重要的模式叫做 BEGIN. 动作由大括号包围. +BEGIN { + + # BEGIN在程序最开始运行. 在这里放一些在真正处理文件之前的准备和setup的代码. + # 如果没有文本文件要处理, 那就把BEGIN作为程序的主入口吧. + + # 变量是全局的. 直接赋值使用即可, 无需声明. + count = 0 + + # 运算符和C语言系一样 + a = count + 1 + b = count - 1 + c = count * 1 + d = count / 1 # 整数除法 + e = count % 1 # 取余 + f = count ^ 1 # 取幂 + + a += 1 + b -= 1 + c *= 1 + d /= 1 + e %= 1 + f ^= 1 + + # 自增1, 自减1 + a++ + b-- + + # 前置运算, 返回增加之后的值 + ++a + --b + + # 注意, 不需要分号之类的标点来分隔语句 + + # 控制语句 + if (count == 0) + print "Starting with count of 0" + else + print "Huh?" + + # 或者三目运算符 + print (count == 0) ? "Starting with count of 0" : "Huh?" + + # 多行的代码块用大括号包围 + while (a < 10) { + print "String concatenation is done" " with a series" " of" + " space-separated strings" + print a + + a++ + } + + for (i = 0; i < 10; i++) + print "Good ol' for loop" + + # 标准的比较运算符 + a < b # 小于 + a <= b # 小于或等于 + a != b # 不等于 + a == b # 等于 + a > b # 大于 + a >= b # 大于或等于 + + # 也有逻辑运算符 + a && b # 且 + a || b # 或 + + # 并且有超实用的正则表达式匹配 + if ("foo" ~ "^fo+$") + print "Fooey!" + if ("boo" !~ "^fo+$") + print "Boo!" + + # 数组 + arr[0] = "foo" + arr[1] = "bar" + # 不幸的是, 没有其他方式初始化数组. 必须像这样一行一行的赋值. + + # 关联数组, 类似map或dict的用法. + assoc["foo"] = "bar" + assoc["bar"] = "baz" + + # 多维数组. 但是有一些局限性这里不提了. + multidim[0,0] = "foo" + multidim[0,1] = "bar" + multidim[1,0] = "baz" + multidim[1,1] = "boo" + + # 可以检测数组包含关系 + if ("foo" in assoc) + print "Fooey!" + + # 可以使用in遍历数组 + for (key in assoc) + print assoc[key] + + # 命令行参数是一个叫ARGV的数组 + for (argnum in ARGV) + print ARGV[argnum] + + # 可以从数组中移除元素 + # 在 防止awk把文件参数当做数据来处理 时delete功能很有用. + delete ARGV[1] + + # 命令行参数的个数是一个叫ARGC的变量 + print ARGC + + # AWK有很多内置函数, 分为三类, 会在接下来定义的各个函数中介绍. + + return_value = arithmetic_functions(a, b, c) + string_functions() + io_functions() +} + +# 定义函数 +function arithmetic_functions(a, b, c, d) { + + # 或许AWK最让人恼火的地方是没有局部变量, 所有东西都是全局的, + # 对于短的脚本还好, 对于长一些的就会成问题. + + # 这里有一个技巧, 函数参数是对函数局部可见的, 并且AWK允许定义多余的参数, + # 因此可以像上面那样把局部变量插入到函数声明中. + # 为了方便区分普通参数(a,b,c)和局部变量(d), 可以多键入一些空格. + + # 现在介绍数学类函数 + + # 多数AWK实现中包含标准的三角函数 + localvar = sin(a) + localvar = cos(a) + localvar = atan2(a, b) # arc tangent of b / a + + # 对数 + localvar = exp(a) + localvar = log(a) + + # 平方根 + localvar = sqrt(a) + + # 浮点型转为整型 + localvar = int(5.34) # localvar => 5 + + # 随机数 + srand() # 接受随机种子作为参数, 默认使用当天的时间 + localvar = rand() # 0到1之间随机 + + # 函数返回 + return localvar +} + +function string_functions( localvar, arr) { + + # AWK, 作为字符处理语言, 有很多字符串相关函数, 其中大多数都严重依赖正则表达式. + + # 搜索并替换, 第一个出现的 (sub) or 所有的 (gsub) + # 都是返回替换的个数 + localvar = "fooooobar" + sub("fo+", "Meet me at the ", localvar) # localvar => "Meet me at the bar" + gsub("e+", ".", localvar) # localvar => "m..t m. at th. bar" + + # 搜索匹配正则的字符串 + # index() 也是搜索, 不支持正则 + match(localvar, "t") # => 4, 't'在4号位置. + # (译者注: awk是1开始计数的,不是常见的0-base) + + # 按分隔符分隔 + split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"] + + # 其他有用的函数 + sprintf("%s %d %d %d", "Testing", 1, 2, 3) # => "Testing 1 2 3" + substr("foobar", 2, 3) # => "oob" + substr("foobar", 4) # => "bar" + length("foo") # => 3 + tolower("FOO") # => "foo" + toupper("foo") # => "FOO" +} + +function io_functions( localvar) { + + # 你已经见过的print函数 + print "Hello world" + + # 也有printf + printf("%s %d %d %d\n", "Testing", 1, 2, 3) + + # AWK本身没有文件句柄, 当你使用需要文件的东西时会自动打开文件, + # 做文件I/O时, 字符串就是打开的文件句柄. 这看起来像Shell + print "foobar" >"/tmp/foobar.txt" + + # 现在"/tmp/foobar.txt"字符串是一个文件句柄, 你可以关闭它 + close("/tmp/foobar.txt") + + # 在shell里运行一些东西 + system("echo foobar") # => prints foobar + + # 从标准输入中读一行, 并存储在localvar中 + getline localvar + + # 从管道中读一行, 并存储在localvar中 + "echo foobar" | getline localvar # localvar => "foobar" + close("echo foobar") + + # 从文件中读一行, 并存储在localvar中 + getline localvar <"/tmp/foobar.txt" + close("/tmp/foobar.txt") +} + +# 正如开头所说, AWK程序由一系列模式和动作组成. 你已经看见了重要的BEGIN pattern, +# 其他的pattern在你需要处理来自文件或标准输入的的数据行时才用到. +# +# 当你给AWK程序传参数时, 他们会被视为要处理文件的文件名, 按顺序全部会处理. +# 可以把这个过程看做一个隐式的循环, 遍历这些文件中的所有行. +# 然后这些模式和动作就是这个循环里的switch语句一样 + +/^fo+bar$/ { + + # 这个动作会在匹配这个正则(/^fo+bar$/)的每一行上执行. 不匹配的则会跳过. + # 先让我们打印它: + print + + # 哦, 没有参数, 那是因为print有一个默认参数 $0. + # $0 是当前正在处理的行, 自动被创建好了. + + # 你可能猜到有其他的$变量了. + # 每一行在动作执行前会被分隔符分隔. 像shell中一样, 每个字段都可以用$符访问 + + # 这个会打印这行的第2和第4个字段 + print $2, $4 + + # AWK自动定义了许多其他的变量帮助你处理行. 最常用的是NF变量 + # 打印这一行的字段数 + print NF + + # 打印这一行的最后一个字段 + print $NF +} + +# 每一个模式其实是一个true/false判断, 上面那个正则其实也是一个true/false判断, 只不过被部分省略了. +# 没有指定时默认使用当前处理的整行($0)进行匹配. 因此, 完全版本是这样: + +$0 ~ /^fo+bar$/ { + print "Equivalent to the last pattern" +} + +a > 0 { + # 只要a是整数, 这块会在每一行上执行. +} + +# 就是这样, 处理文本文件, 一次读一行, 对行做一些操作. +# 按分隔符分隔, 这在UNIX中很常见, awk都帮你做好了. +# 你所需要做的是基于自己的需求写一些模式和动作. + +# 这里有一个快速的例子, 展示了AWK所擅长做的事. +# 它从标准输入读一个名字, 打印这个first name下所有人的平均年龄. +# 示例数据: +# +# Bob Jones 32 +# Jane Doe 22 +# Steve Stevens 83 +# Bob Smith 29 +# Bob Barker 72 +# +# 示例脚本: + +BEGIN { + + # 首先, 问用户要一个名字 + print "What name would you like the average age for?" + + # 从标准输入获取名字 + getline name <"/dev/stdin" +} + +# 然后, 用给定的名字匹配每一行的第一个字段. +$1 == name { + + # 这里我们要使用几个有用的变量, 已经提前为我们加载好的: + # $0 是整行 + # $3 是第三个字段, 就是我们所感兴趣的年龄 + # NF 字段数, 这里是3 + # NR 至此为止的行数 + # FILENAME 在处理的文件名 + # FS 在使用的字段分隔符, 这里是空格" " + # ...等等, 还有很多, 在帮助文档中列出. + + # 跟踪 总和以及行数 + sum += $3 + nlines++ +} + +# 另一个特殊的模式叫END. 它会在处理完所有行之后运行. 不像BEGIN, 它只会在有输入的时候运行. +# 它在所有文件依据给定的模式和动作处理完后运行, 目的通常是输出一些最终报告, 做一些数据聚合操作. + +END { + if (nlines) + print "The average age for " name " is " sum / nlines +} + +``` +更多: + +* [Awk 教程](http://www.grymoire.com/Unix/Awk.html) +* [Awk 手册](https://linux.die.net/man/1/awk) +* [The GNU Awk 用户指南](https://www.gnu.org/software/gawk/manual/gawk.html) GNU Awk在大多数Linux中预装 diff --git a/zh-cn/c-cn.html.markdown b/zh-cn/c-cn.html.markdown index 1e10416e..8566e811 100644 --- a/zh-cn/c-cn.html.markdown +++ b/zh-cn/c-cn.html.markdown @@ -41,7 +41,7 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; void function_1(char c); void function_2(void); -// 如果函数出现在main()之后,那么必须在main()之前 +// 如果函数调用在main()之后,那么必须声明在main()之前 // 先声明一个函数原型 int add_two_ints(int x1, int x2); // 函数原型 diff --git a/zh-cn/fortran95-cn.html.markdown b/zh-cn/fortran95-cn.html.markdown new file mode 100644 index 00000000..e28d309f --- /dev/null +++ b/zh-cn/fortran95-cn.html.markdown @@ -0,0 +1,435 @@ +--- +language: Fortran +filename: learnfortran-cn.f95 +contributors: + - ["Robert Steed", "https://github.com/robochat"] +translators: + - ["Corvusnest", "https://github.com/Corvusnest"] +lang: zh-cn +--- + +Fortran 是最古老的计算机语言之一。它由IBM开发于1950年用于数值运算(Fortran 为 "Formula +Translation" 的缩写)。虽然该语言已年代久远,但目前仍用于高性能计算,如天气预报。 +该语言仍在持续发展,并且基本保持向下兼容。知名的版本为 Fortran 77, Fortran 90, +Fortran 95, Fortran 2003, Fortran 2008 与 Fortran 2015。 + +这篇概要将讨论 Fortran 95 的一些特征。因为它是目前所广泛采用的标准版本,并且与最新版本的内容 +也基本相同(而 Fortran 77 则是一个非常不同的版本)。 + +```fortran + +! 这是一行注释 + + +program example !声明一个叫做 example 的程序 + + ! 代码只能放在程序、函数、子程序或者模块内部 + ! 推荐使用缩进,但不是必须的。 + + ! 声明变量 + ! =================== + + ! 所有的声明必须放在语句与表达式之前 + + implicit none !阻止变量的隐式声明 (推荐!) + ! Implicit none 必须在每一个 函数/程序/模块 中进行声明 + + ! 重要 - Fortran 对大小写不敏感 + real z + REAL Z2 + + real :: v,x ! 警告: 默认值取决于编译器! + real :: a = 3, b=2E12, c = 0.01 + integer :: i, j, k=1, m + real, parameter :: PI = 3.1415926535897931 !声明一个常量 + logical :: y = .TRUE. , n = .FALSE. !布尔值 + complex :: w = (0,1) !sqrt(-1) (译注: 定义复数,此为-1的平方根) + character (len=3) :: month !长度为3的字符串 + + real :: array(6) !声明长度为6的浮点数数组 + real, dimension(4) :: arrayb !声明数组的另一种方法 + integer :: arrayc(-10:10) !有着自定义索引的数组 + real :: array2d(3,2) !多维数组 + + ! 分隔符 '::' 并不总是必要的,但推荐使用 + + ! 还存在很多其他的变量特征: + real, pointer :: p !声明一个指针 + + integer, parameter :: LP = selected_real_kind(20) + real (kind = LP) :: d !长精度变量 + + ! 警告:在声明期间初始化变量将导致在函数内发生问题,因为这将自动具备了 “save” 属性, + ! 因此变量的值在函数的多次调用期间将被存储。一般来说,除了常量,应分开声明与初始化! + + ! 字符串 + ! ======= + + character :: a_char = 'i' + character (len = 6) :: a_str = "qwerty" + character (len = 30) :: str_b + character (len = *), parameter :: a_long_str = "This is a long string." + !可以通过使用 (len=*) 来自动判断长度,但只对常量有效 + + str_b = a_str // " keyboard" !通过 // 操作符来连接字符串 + + + ! 任务与计算 + ! ======================= + + Z = 1 !向之前声明的变量 z 赋值 (大小写不敏感). + j = 10 + 2 - 3 + a = 11.54 / (2.3 * 3.1) + b = 2**3 !幂 + + + ! 控制流程语句 与 操作符 + ! =================================== + + !单行 if 语句 + if (z == a) b = 4 !判别句永远需要放在圆括号内 + + if (z /= a) then !z 不等于 a + ! 其他的比较运算符: < > <= >= == /= + b = 4 + else if (z .GT. a) then !z 大于(Greater) a + ! 文本形式的比较运算符: .LT. .GT. .LE. .GE. .EQ. .NE. + b = 6 + else if (z < a) then !'then' 必须放在该行 + b = 5 !执行部分必须放在新的一行里 + else + b = 10 + end if !结束语句需要 'if' (也可以用 'endif'). + + + if (.NOT. (x < c .AND. v >= a .OR. z == z)) then !布尔操作符 + inner: if (.TRUE.) then !可以为 if 结构命名 + b = 1 + endif inner !接下来必须命名 endif 语句. + endif + + + i = 20 + select case (i) + case (0) !当 i == 0 + j=0 + case (1:10) !当 i 为 1 到 10 之内 ( 1 <= i <= 10 ) + j=1 + case (11:) !当 i>=11 + j=2 + case default + j=3 + end select + + + month = 'jan' + ! 状态值可以为整数、布尔值或者字符类型 + ! Select 结构同样可以被命名 + monthly: select case (month) + case ("jan") + j = 0 + case default + j = -1 + end select monthly + + do i=2,10,2 !从2到10(包含2和10)以2为步进值循环 + innerloop: do j=1,3 !循环同样可以被命名 + exit !跳出循环 + end do innerloop + cycle !重复跳入下一次循环 + enddo + + + ! Goto 语句是存在的,但强烈不建议使用 + goto 10 + stop 1 !立即停止程序 (返回一个设定的状态码). +10 j = 201 !这一行被标注为 10 行 (line 10) + + + ! 数组 + ! ====== + array = (/1,2,3,4,5,6/) + array = [1,2,3,4,5,6] !当使用 Fortran 2003 版本. + arrayb = [10.2,3e3,0.41,4e-5] + array2d = reshape([1.0,2.0,3.0,4.0,5.0,6.0], [3,2]) + + ! Fortran 数组索引起始于 1 + ! (默认下如此,也可以为数组定义不同的索引起始) + v = array(1) !获取数组的第一个元素 + v = array2d(2,2) + + print *, array(3:5) !打印从第3到第五5之内的所有元素 + print *, array2d(1,:) !打印2维数组的第一列 + + array = array*3 + 2 !可为数组设置数学表达式 + array = array*array !数组操作支持元素级(操作) (element-wise) + !array = array*array2d !这两类数组并不是同一个维度的 + + ! 有很多内置的数组操作函数 + c = dot_product(array,array) !点乘 (点积) + ! 用 matmul() 来进行矩阵运算. + c = sum(array) + c = maxval(array) + print *, minloc(array) + c = size(array) + print *, shape(array) + m = count(array > 0) + + ! 遍历一个数组 (一般使用 Product() 函数). + v = 1 + do i = 1, size(array) + v = v*array(i) + end do + + ! 有条件地执行元素级操作 + array = [1,2,3,4,5,6] + where (array > 3) + array = array + 1 + elsewhere (array == 2) + array = 1 + elsewhere + array = 0 + end where + + ! 隐式DO循环可以很方便地创建数组 + array = [ (i, i = 1,6) ] !创建数组 [1,2,3,4,5,6] + array = [ (i, i = 1,12,2) ] !创建数组 [1,3,5,7,9,11] + array = [ (i**2, i = 1,6) ] !创建数组 [1,4,9,16,25,36] + array = [ (4,5, i = 1,3) ] !创建数组 [4,5,4,5,4,5] + + + ! 输入/输出 + ! ============ + + print *, b !向命令行打印变量 'b' + + ! 我们可以格式化输出 + print "(I6)", 320 !打印 ' 320' + print "(I6.4)", 3 !打印 ' 0003' + print "(F6.3)", 4.32 !打印 ' 4.320' + + + ! 该字母与数值规定了给定的数值与字符所用于打印输出的类型与格式 + ! 字母可为 I (整数), F (浮点数), E (工程格式), + ! L (逻辑/布尔值), A (字符) ... + print "(I3)", 3200 !如果数值无法符合格式将打印 '***' + + ! 可以同时设定多种格式 + print "(I5,F6.2,E6.2)", 120, 43.41, 43.41 + print "(3I5)", 10, 20, 30 !连续打印3个整数 (字段宽度 = 5). + print "(2(I5,F6.2))", 120, 43.42, 340, 65.3 !连续分组格式 + + ! 我们也可以从终端读取输入 + read *, v + read "(2F6.2)", v, x !读取2个数值 + + ! 读取文件 + open(unit=11, file="records.txt", status="old") + ! 文件被引用带有一个单位数 'unit', 为一个取值范围在9-99的整数 + ! 'status' 可以为 {'old','replace','new'} 其中之一 + read(unit=11, fmt="(3F10.2)") a, b, c + close(11) + + ! 写入一个文件 + open(unit=12, file="records.txt", status="replace") + write(12, "(F10.2,F10.2,F10.2)") c, b, a + close(12) + ! 在讨论范围之外的还有更多的细节与可用功能,并于老版本的 Fortran 保持兼容 + + + ! 内置函数 + ! ================== + + ! Fortran 拥有大约 200 个内置函数/子程序 + ! 例子 + call cpu_time(v) !以秒为单位设置时间 + k = ior(i,j) !2个整数的位或运算 + v = log10(x) !以10为底的log运算 + i = floor(b) !返回一个最接近的整数小于或等于x (地板数) + v = aimag(w) !复数的虚数部分 + + + ! 函数与子程序 + ! ======================= + + ! 一个子程序会根据输入值运行一些代码并会导致副作用 (side-effects) 或修改输入值 + ! (译者注: 副作用是指对子程序/函数外的环境产生影响,如修改变量) + + call routine(a,c,v) !调用子程序 + + ! 一个函数会根据输入的一系列数值来返回一个单独的值 + ! 但输入值仍然可能被修改以及产生副作用 + + m = func(3,2,k) !调用函数 + + ! 函数可以在表达式内被调用 + Print *, func2(3,2,k) + + ! 一个纯函数不会去修改输入值或产生副作用 + m = func3(3,2,k) + + +contains ! 用于定义程序内部的副程序(sub-programs)的区域 + + ! Fortran 拥有一些不同的方法去定义函数 + + integer function func(a,b,c) !一个返回一个整数的函数 + implicit none !最好也在函数内将含蓄模式关闭 (implicit none) + integer :: a,b,c !输入值类型定义在函数内部 + if (a >= 2) then + func = a + b + c !返回值默认为函数名 + return !可以在函数内任意时间返回当前值 + endif + func = a + c + ! 在函数的结尾不需要返回语句 + end function func + + + function func2(a,b,c) result(f) !将返回值声明为 'f' + implicit none + integer, intent(in) :: a,b !可以声明让变量无法被函数修改 + integer, intent(inout) :: c + integer :: f !函数的返回值类型在函数内声明 + integer :: cnt = 0 !注意 - 隐式的初始化变量将在函数的多次调用间被存储 + f = a + b - c + c = 4 !变动一个输入变量的值 + cnt = cnt + 1 !记录函数的被调用次数 + end function func2 + + + pure function func3(a,b,c) !一个没有副作用的纯函数 + implicit none + integer, intent(in) :: a,b,c + integer :: func3 + func3 = a*b*c + end function func3 + + + subroutine routine(d,e,f) + implicit none + real, intent(inout) :: f + real, intent(in) :: d,e + f = 2*d + 3*e + f + end subroutine routine + + +end program example ! 函数定义完毕 ----------------------- + +! 函数与子程序的外部声明对于生成程序清单来说,需要一个接口声明(即使它们在同一个源文件内)(见下) +! 使用 'contains' 可以很容易地在模块或程序内定义它们 + +elemental real function func4(a) result(res) +! 一个元函数(elemental function) 为一个纯函数使用一个标量输入值 +! 但同时也可以用在一个数组并对其中的元素分别处理,之后返回一个新的数组 + real, intent(in) :: a + res = a**2 + 1.0 +end function func4 + + +! 模块 +! ======= + +! 模块十分适合于存放与复用相关联的一组声明、函数与子程序 + +module fruit + real :: apple + real :: pear + real :: orange +end module fruit + + +module fruity + + ! 声明必须按照顺序: 模块、接口、变量 + ! (同样可在程序内声明模块和接口) + + use fruit, only: apple, pear ! 使用来自于 fruit 模块的 apple 和 pear + implicit none !在模块导入后声明 + + private !使得模块内容为私有(private)(默认为公共 public) + ! 显式声明一些变量/函数为公共 + public :: apple,mycar,create_mycar + ! 声明一些变量/函数为私有(在当前情况下没必要)(译注: 因为前面声明了模块全局 private) + private :: func4 + + ! 接口 + ! ========== + ! 在模块内显式声明一个外部函数/程序 + ! 一般最好将函数/程序放进 'contains' 部分内 + interface + elemental real function func4(a) result(res) + real, intent(in) :: a + end function func4 + end interface + + ! 重载函数可以通过已命名的接口来定义 + interface myabs + ! 可以通过使用 'module procedure' 关键词来包含一个已在模块内定义的函数 + module procedure real_abs, complex_abs + end interface + + ! 派生数据类型 + ! ================== + ! 可创建自定义数据结构 + type car + character (len=100) :: model + real :: weight !(公斤 kg) + real :: dimensions(3) !例: 长宽高(米) + character :: colour + end type car + + type(car) :: mycar !声明一个自定义类型的变量 + ! 用法具体查看 create_mycar() + + ! 注: 模块内没有可执行的语句 + +contains + + subroutine create_mycar(mycar) + ! 展示派生数据类型的使用 + implicit none + type(car),intent(out) :: mycar + + ! 通过 '%' 操作符来访问(派生数据)类型的元素 + mycar%model = "Ford Prefect" + mycar%colour = 'r' + mycar%weight = 1400 + mycar%dimensions(1) = 5.0 !索引默认起始值为 1 ! + mycar%dimensions(2) = 3.0 + mycar%dimensions(3) = 1.5 + + end subroutine + + real function real_abs(x) + real :: x + if (x<0) then + real_abs = -x + else + real_abs = x + end if + end function real_abs + + real function complex_abs(z) + complex :: z + ! 过长的一行代码可通过延续符 '&' 来换行 + complex_abs = sqrt(real(z)**2 + & + aimag(z)**2) + end function complex_abs + + +end module fruity + +``` + +### 更多资源 + +了解更多的 Fortran 信息: + ++ [wikipedia](https://en.wikipedia.org/wiki/Fortran) ++ [Fortran_95_language_features](https://en.wikipedia.org/wiki/Fortran_95_language_features) ++ [fortranwiki.org](http://fortranwiki.org) ++ [www.fortran90.org/](http://www.fortran90.org) ++ [list of Fortran 95 tutorials](http://www.dmoz.org/Computers/Programming/Languages/Fortran/FAQs%2C_Help%2C_and_Tutorials/Fortran_90_and_95/) ++ [Fortran wikibook](https://en.wikibooks.org/wiki/Fortran) ++ [Fortran resources](http://www.fortranplus.co.uk/resources/fortran_resources.pdf) ++ [Mistakes in Fortran 90 Programs That Might Surprise You](http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html) diff --git a/zh-cn/julia-cn.html.markdown b/zh-cn/julia-cn.html.markdown index 1f91d52c..b350b6dc 100644 --- a/zh-cn/julia-cn.html.markdown +++ b/zh-cn/julia-cn.html.markdown @@ -2,16 +2,24 @@ language: Julia filename: learn-julia-zh.jl contributors: - - ["Jichao Ouyang", "http://oyanglul.us"] + - ["Leah Hanson", "http://leahhanson.us"] + - ["Pranit Bauva", "https://github.com/pranitbauva1997"] + - ["Daniel YC Lin", "https://github.com/dlintw"] translators: - ["Jichao Ouyang", "http://oyanglul.us"] + - ["woclass", "https://github.com/inkydragon"] lang: zh-cn --- -```ruby -# 单行注释只需要一个井号 +Julia 是一种新的同像函数式编程语言(homoiconic functional language),它专注于科学计算领域。 +虽然拥有同像宏(homoiconic macros)、一级函数(first-class functions)和底层控制等全部功能,但 Julia 依旧和 Python 一样易于学习和使用。 + +示例代码基于 Julia 1.0.0 + +```julia +# 单行注释只需要一个井号「#」 #= 多行注释 - 只需要以 '#=' 开始 '=#' 结束 + 只需要以「#=」开始「=#」结束 还可以嵌套. =# @@ -19,41 +27,41 @@ lang: zh-cn ## 1. 原始类型与操作符 #################################################### -# Julia 中一切皆是表达式。 - -# 这是一些基本数字类型. -3 # => 3 (Int64) -3.2 # => 3.2 (Float64) -2 + 1im # => 2 + 1im (Complex{Int64}) -2//3 # => 2//3 (Rational{Int64}) - -# 支持所有的普通中缀操作符。 -1 + 1 # => 2 -8 - 1 # => 7 -10 * 2 # => 20 -35 / 5 # => 7.0 -5 / 2 # => 2.5 # 用 Int 除 Int 永远返回 Float -div(5, 2) # => 2 # 使用 div 截断小数点 -5 \ 35 # => 7.0 -2 ^ 2 # => 4 # 次方, 不是二进制 xor -12 % 10 # => 2 +# Julia 中一切皆为表达式 + +# 这是一些基本数字类型 +typeof(3) # => Int64 +typeof(3.2) # => Float64 +typeof(2 + 1im) # => Complex{Int64} +typeof(2 // 3) # => Rational{Int64} + +# 支持所有的普通中缀操作符 +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7.0 +10 / 2 # => 5.0 # 整数除法总是返回浮点数 +div(5, 2) # => 2 # 使用 div 可以获得整除的结果 +5 \ 35 # => 7.0 +2^2 # => 4 # 幂运算,不是异或 (xor) +12 % 10 # => 2 # 用括号提高优先级 (1 + 3) * 2 # => 8 -# 二进制操作符 -~2 # => -3 # 非 -3 & 5 # => 1 # 与 -2 | 4 # => 6 # 或 -2 $ 4 # => 6 # 异或 -2 >>> 1 # => 1 # 逻辑右移 -2 >> 1 # => 1 # 算术右移 -2 << 1 # => 4 # 逻辑/算术 右移 - -# 可以用函数 bits 查看二进制数。 -bits(12345) +# 位操作符 +~2 # => -3 # 按位非 (not) +3 & 5 # => 1 # 按位与 (and) +2 | 4 # => 6 # 按位或 (or) +xor(2, 4) # => 6 # 按位异或 (xor) +2 >>> 1 # => 1 # 逻辑右移 +2 >> 1 # => 1 # 算术右移 +2 << 1 # => 4 # 逻辑/算术左移 + +# 可以用函数 bitstring 查看二进制数。 +bitstring(12345) # => "0000000000000000000000000000000000000000000000000011000000111001" -bits(12345.0) +bitstring(12345.0) # => "0100000011001000000111001000000000000000000000000000000000000000" # 布尔值是原始类型 @@ -61,40 +69,50 @@ true false # 布尔操作符 -!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 -# 比较可以串联 +!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 + +# 链式比较 1 < 2 < 3 # => true 2 < 3 < 2 # => false -# 字符串可以由 " 创建 +# 字符串可以由「"」创建 "This is a string." -# 字符字面量可用 ' 创建 +# 字符字面量可用「'」创建 'a' +# 字符串使用 UTF-8 编码 # 可以像取数组取值一样用 index 取出对应字符 -"This is a string"[1] # => 'T' # Julia 的 index 从 1 开始 :( -# 但是对 UTF-8 无效, -# 因此建议使用遍历器 (map, for loops, 等). +ascii("This is a string")[1] +# => 'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase) +# Julia 的 index 从 1 开始 :( +# 但只有在字符串仅由 ASCII 字符构成时,字符串才能够被安全的引索 +# 因此建议使用遍历器 (map, for loops, 等) # $ 可用于字符插值: "2 + 2 = $(2 + 2)" # => "2 + 2 = 4" # 可以将任何 Julia 表达式放入括号。 -# 另一种格式化字符串的方式是 printf 宏. -@printf "%d is less than %f" 4.5 5.3 # 5 is less than 5.300000 +# 另一种输出格式化字符串的方法是使用标准库 Printf 中的 Printf 宏 +using Printf +@printf "%d is less than %f\n" 4.5 5.3 # => 5 is less than 5.300000 # 打印字符串很容易 -println("I'm Julia. Nice to meet you!") +println("I'm Julia. Nice to meet you!") # => I'm Julia. Nice to meet you! + +# 字符串可以按字典序进行比较 +"good" > "bye" # => true +"good" == "good" # => true +"1 + 2 = 3" == "1 + 2 = $(1 + 2)" # => true #################################################### ## 2. 变量与集合 @@ -106,12 +124,12 @@ some_var # => 5 # 访问未声明变量会抛出异常 try - some_other_var # => ERROR: some_other_var not defined + some_other_var # => ERROR: UndefVarError: some_other_var not defined catch e println(e) end -# 变量名需要以字母开头. +# 变量名必须以下划线或字母开头 # 之后任何字母,数字,下划线,叹号都是合法的。 SomeOtherVar123! = 6 # => 6 @@ -122,66 +140,93 @@ SomeOtherVar123! = 6 # => 6 # 注意 Julia 的命名规约: # -# * 变量名为小写,单词之间以下划线连接('\_')。 +# * 名称可以用下划线「_」分割。 +# 不过一般不推荐使用下划线,除非不用变量名就会变得难于理解 # -# * 类型名以大写字母开头,单词以 CamelCase 方式连接。 +# * 类型名以大写字母开头,单词以 CamelCase 方式连接,无下划线。 # # * 函数与宏的名字小写,无下划线。 # -# * 会改变输入的函数名末位为 !。 +# * 会改变输入的函数名末位为「!」。 # 这类函数有时被称为 mutating functions 或 in-place functions. -# 数组存储一列值,index 从 1 开始。 -a = Int64[] # => 0-element Int64 Array +# 数组存储一列值,index 从 1 开始 +a = Int64[] # => 0-element Array{Int64,1} + +# 一维数组可以以逗号分隔值的方式声明 +b = [4, 5, 6] # => 3-element Array{Int64,1}: [4, 5, 6] +b = [4; 5; 6] # => 3-element Array{Int64,1}: [4, 5, 6] +b[1] # => 4 +b[end] # => 6 -# 一维数组可以以逗号分隔值的方式声明。 -b = [4, 5, 6] # => 包含 3 个 Int64 类型元素的数组: [4, 5, 6] -b[1] # => 4 -b[end] # => 6 +# 二维数组以分号分隔维度 +matrix = [1 2; 3 4] # => 2×2 Array{Int64,2}: [1 2; 3 4] -# 二维数组以分号分隔维度。 -matrix = [1 2; 3 4] # => 2x2 Int64 数组: [1 2; 3 4] +# 指定数组的类型 +b = Int8[4, 5, 6] # => 3-element Array{Int8,1}: [4, 5, 6] # 使用 push! 和 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] +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] -# 用 pop 弹出末尾元素 -pop!(b) # => 6 and b is now [4,5] +# 用 pop 弹出尾部的元素 +pop!(b) # => 6 +b # => [4,5] -# 可以再放回去 -push!(b,6) # b 又变成了 [4,5,6]. +# 再放回去 +push!(b, 6) # => [4,5,6] +b # => [4,5,6] -a[1] # => 1 # 永远记住 Julia 的 index 从 1 开始! +a[1] # => 1 # 永远记住 Julia 的引索从 1 开始!而不是 0! -# 用 end 可以直接取到最后索引. 可用作任何索引表达式 +# 用 end 可以直接取到最后索引。它可以用在任何索引表达式中 a[end] # => 6 -# 还支持 shift 和 unshift -shift!(a) # => 返回 1,而 a 现在时 [2,4,3,4,5,6] -unshift!(a,7) # => [7,2,4,3,4,5,6] +# 数组还支持 popfirst! 和 pushfirst! +popfirst!(a) # => 1 +a # => [2,4,3,4,5,6] +pushfirst!(a, 7) # => [7,2,4,3,4,5,6] +a # => [7,2,4,3,4,5,6] # 以叹号结尾的函数名表示它会改变参数的值 -arr = [5,4,6] # => 包含三个 Int64 元素的数组: [5,4,6] -sort(arr) # => [4,5,6]; arr 还是 [5,4,6] -sort!(arr) # => [4,5,6]; arr 现在是 [4,5,6] +arr = [5,4,6] # => 3-element Array{Int64,1}: [5,4,6] +sort(arr) # => [4,5,6] +arr # => [5,4,6] +sort!(arr) # => [4,5,6] +arr # => [4,5,6] -# 越界会抛出 BoundsError 异常 +# 数组越界会抛出 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: attempt to access 7-element Array{Int64,1} at + # index [0] + # => Stacktrace: + # => [1] getindex(::Array{Int64,1}, ::Int64) at .\array.jl:731 + # => [2] top-level scope at none:0 + # => [3] ... + # => in expression starting at ...\LearnJulia.jl:203 + a[end + 1] + # => ERROR: BoundsError: attempt to access 7-element Array{Int64,1} at + # index [8] + # => Stacktrace: + # => [1] getindex(::Array{Int64,1}, ::Int64) at .\array.jl:731 + # => [2] top-level scope at none:0 + # => [3] ... + # => in expression starting at ...\LearnJulia.jl:211 catch e println(e) end -# 错误会指出发生的行号,包括标准库 -# 如果你有 Julia 源代码,你可以找到这些地方 +# 报错时错误会指出出错的文件位置以及行号,标准库也一样 +# 你可以在 Julia 安装目录下的 share/julia 文件夹里找到这些标准库 # 可以用 range 初始化数组 -a = [1:5] # => 5-element Int64 Array: [1,2,3,4,5] +a = [1:5;] # => 5-element Array{Int64,1}: [1,2,3,4,5] +# 注意!分号不可省略 +a2 = [1:5] # => 1-element Array{UnitRange{Int64},1}: [1:5] # 可以切割数组 a[1:3] # => [1, 2, 3] @@ -189,11 +234,13 @@ a[2:end] # => [2, 3, 4, 5] # 用 splice! 切割原数组 arr = [3,4,5] -splice!(arr,2) # => 4 ; arr 变成了 [3,5] +splice!(arr, 2) # => 4 +arr # => [3,5] # 用 append! 连接数组 b = [1,2,3] -append!(a,b) # a 变成了 [1, 2, 3, 4, 5, 1, 2, 3] +append!(a, b) # => [1, 2, 3, 4, 5, 1, 2, 3] +a # => [1, 2, 3, 4, 5, 1, 2, 3] # 检查元素是否在数组中 in(1, a) # => true @@ -201,240 +248,258 @@ in(1, a) # => true # 用 length 获得数组长度 length(a) # => 8 -# Tuples 是 immutable 的 -tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple. +# 元组(Tuples)是不可变的 +tup = (1, 2, 3) # => (1,2,3) +typeof(tup) # => Tuple{Int64,Int64,Int64} tup[1] # => 1 -try: - tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64) +try + tup[1] = 3 + # => ERROR: MethodError: no method matching + # setindex!(::Tuple{Int64,Int64,Int64}, ::Int64, ::Int64) catch e println(e) end -# 大多数组的函数同样支持 tuples +# 大多数组的函数同样支持元组 length(tup) # => 3 -tup[1:2] # => (1,2) -in(2, tup) # => true +tup[1:2] # => (1,2) +in(2, tup) # => true -# 可以将 tuples 元素分别赋给变量 -a, b, c = (1, 2, 3) # => (1,2,3) # a is now 1, b is now 2 and c is now 3 +# 可以将元组的元素解包赋给变量 +a, b, c = (1, 2, 3) # => (1,2,3) +a # => 1 +b # => 2 +c # => 3 # 不用括号也可以 -d, e, f = 4, 5, 6 # => (4,5,6) +d, e, f = 4, 5, 6 # => (4,5,6) +d # => 4 +e # => 5 +f # => 6 # 单元素 tuple 不等于其元素值 (1,) == 1 # => false -(1) == 1 # => true +(1) == 1 # => true # 交换值 -e, d = d, e # => (5,4) # d is now 5 and e is now 4 +e, d = d, e # => (5,4) +d # => 5 +e # => 4 -# 字典Dictionaries store mappings -empty_dict = Dict() # => Dict{Any,Any}() +# 字典用于储存映射(mappings)(键值对) +empty_dict = Dict() # => Dict{Any,Any} with 0 entries # 也可以用字面量创建字典 -filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3] -# => Dict{ASCIIString,Int64} +filled_dict = Dict("one" => 1, "two" => 2, "three" => 3) +# => Dict{String,Int64} with 3 entries: +# => "two" => 2, "one" => 1, "three" => 3 # 用 [] 获得键值 filled_dict["one"] # => 1 # 获得所有键 keys(filled_dict) -# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) +# => Base.KeySet for a Dict{String,Int64} with 3 entries. Keys: +# => "two", "one", "three" # 注意,键的顺序不是插入时的顺序 # 获得所有值 values(filled_dict) -# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) +# => Base.ValueIterator for a Dict{String,Int64} with 3 entries. Values: +# => 2, 1, 3 # 注意,值的顺序也一样 # 用 in 检查键值是否已存在,用 haskey 检查键是否存在 -in(("one", 1), filled_dict) # => true -in(("two", 3), filled_dict) # => false -haskey(filled_dict, "one") # => true -haskey(filled_dict, 1) # => false +in(("one" => 1), filled_dict) # => true +in(("two" => 3), filled_dict) # => false +haskey(filled_dict, "one") # => true +haskey(filled_dict, 1) # => false # 获取不存在的键的值会抛出异常 try - filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489 + filled_dict["four"] # => ERROR: KeyError: key "four" not found catch e println(e) end # 使用 get 可以提供默认值来避免异常 # get(dictionary,key,default_value) -get(filled_dict,"one",4) # => 1 -get(filled_dict,"four",4) # => 4 +get(filled_dict, "one", 4) # => 1 +get(filled_dict, "four", 4) # => 4 -# 用 Sets 表示无序不可重复的值的集合 -empty_set = Set() # => Set{Any}() -# 初始化一个 Set 并定义其值 -filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4) +# Set 表示无序不可重复的值的集合 +empty_set = Set() # => Set(Any[]) +# 初始化一个带初值的 Set +filled_set = Set([1, 2, 2, 3, 4]) # => Set([4, 2, 3, 1]) -# 添加值 -push!(filled_set,5) # => Set{Int64}(5,4,2,3,1) +# 新增值 +push!(filled_set, 5) # => Set([4, 2, 3, 5, 1]) -# 检查是否存在某值 -in(2, filled_set) # => true -in(10, filled_set) # => false +# 检查 Set 中是否存在某值 +in(2, filled_set) # => true +in(10, filled_set) # => false # 交集,并集,差集 -other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3) -intersect(filled_set, other_set) # => Set{Int64}(3,4,5) -union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6) -setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4) - +other_set = Set([3, 4, 5, 6]) # => Set([4, 3, 5, 6]) +intersect(filled_set, other_set) # => Set([4, 3, 5]) +union(filled_set, other_set) # => Set([4, 2, 3, 5, 6, 1]) +setdiff(Set([1,2,3,4]), Set([2,3,5])) # => Set([4, 1]) #################################################### -## 3. 控制流 +## 3. 控制语句 #################################################### # 声明一个变量 some_var = 5 -# 这是一个 if 语句,缩进不是必要的 +# 这是一个 if 语句块,其中的缩进不是必须的 if some_var > 10 println("some_var is totally bigger than 10.") -elseif some_var < 10 # elseif 是可选的. +elseif some_var < 10 # elseif 是可选的 println("some_var is smaller than 10.") -else # else 也是可选的. +else # else 也是可选的 println("some_var is indeed 10.") end -# => prints "some var is smaller than 10" +# => some_var is smaller than 10. # For 循环遍历 -# Iterable 类型包括 Range, Array, Set, Dict, 以及 String. -for animal=["dog", "cat", "mouse"] +# 可迭代的类型包括:Range, Array, Set, Dict 和 AbstractString +for animal = ["dog", "cat", "mouse"] println("$animal is a mammal") - # 可用 $ 将 variables 或 expression 转换为字符串into strings + # 你可以用 $ 将变量或表达式插入字符串中 end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => dog is a mammal +# => cat is a mammal +# => mouse is a mammal -# You can use 'in' instead of '='. +# 你也可以不用「=」而使用「in」 for animal in ["dog", "cat", "mouse"] println("$animal is a mammal") end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => dog is a mammal +# => cat is a mammal +# => mouse is a mammal -for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"] - println("$(a[1]) is a $(a[2])") +for pair in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal") + from, to = pair + println("$from is a $to") end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => mouse is a mammal +# => cat is a mammal +# => dog is a mammal +# 注意!这里的输出顺序和上面的不同 -for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"] +for (k, v) in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal") println("$k is a $v") end -# prints: -# dog is a mammal -# cat is a mammal -# mouse is a mammal +# => mouse is a mammal +# => cat is a mammal +# => dog is a mammal # While 循环 -x = 0 -while x < 4 - println(x) - x += 1 # x = x + 1 +let x = 0 + while x < 4 + println(x) + x += 1 # x = x + 1 的缩写 + end end -# prints: -# 0 -# 1 -# 2 -# 3 +# => 0 +# => 1 +# => 2 +# => 3 # 用 try/catch 处理异常 try - error("help") + error("help") catch e - println("caught it $e") + println("caught it $e") end # => caught it ErrorException("help") - #################################################### ## 4. 函数 #################################################### -# 用关键字 'function' 可创建一个新函数 -#function name(arglist) -# body... -#end +# 关键字 function 用于定义函数 +# function name(arglist) +# body... +# end function add(x, y) println("x is $x and y is $y") - # 最后一行语句的值为返回 + # 函数会返回最后一行的值 x + y end -add(5, 6) # => 在 "x is 5 and y is 6" 后会打印 11 +add(5, 6) +# => x is 5 and y is 6 +# => 11 + +# 更紧凑的定义函数 +f_add(x, y) = x + y # => f_add (generic function with 1 method) +f_add(3, 4) # => 7 + +# 函数可以将多个值作为元组返回 +fn(x, y) = x + y, x - y # => fn (generic function with 1 method) +fn(3, 4) # => (7, -1) # 还可以定义接收可变长参数的函数 function varargs(args...) return args - # 关键字 return 可在函数内部任何地方返回 + # 使用 return 可以在函数内的任何地方返回 end # => varargs (generic function with 1 method) varargs(1,2,3) # => (1,2,3) -# 省略号 ... 被称为 splat. +# 省略号「...」称为 splat # 刚刚用在了函数定义中 -# 还可以用在函数的调用 -# Array 或者 Tuple 的内容会变成参数列表 -Set([1,2,3]) # => Set{Array{Int64,1}}([1,2,3]) # 获得一个 Array 的 Set -Set([1,2,3]...) # => Set{Int64}(1,2,3) # 相当于 Set(1,2,3) +# 在调用函数时也可以使用它,此时它会把数组或元组解包为参数列表 +add([5,6]...) # 等价于 add(5,6) -x = (1,2,3) # => (1,2,3) -Set(x) # => Set{(Int64,Int64,Int64)}((1,2,3)) # 一个 Tuple 的 Set -Set(x...) # => Set{Int64}(2,3,1) +x = (5, 6) # => (5,6) +add(x...) # 等价于 add(5,6) - -# 可定义可选参数的函数 -function defaults(a,b,x=5,y=6) +# 可定义带可选参数的函数 +function defaults(a, b, x=5, y=6) return "$a $b and $x $y" end +# => defaults (generic function with 3 methods) -defaults('h','g') # => "h g and 5 6" -defaults('h','g','j') # => "h g and j 6" -defaults('h','g','j','k') # => "h g and j k" +defaults('h', 'g') # => "h g and 5 6" +defaults('h', 'g', 'j') # => "h g and j 6" +defaults('h', 'g', 'j', 'k') # => "h g and j k" try - defaults('h') # => ERROR: no method defaults(Char,) - defaults() # => ERROR: no methods defaults() + defaults('h') # => ERROR: MethodError: no method matching defaults(::Char) + defaults() # => ERROR: MethodError: no method matching defaults() catch e println(e) end -# 还可以定义键值对的参数 -function keyword_args(;k1=4,name2="hello") # note the ; - return ["k1"=>k1,"name2"=>name2] +# 还可以定义带关键字参数的函数 +function keyword_args(;k1=4, name2="hello") # 注意分号 ';' + return Dict("k1" => k1, "name2" => name2) end +# => keyword_args (generic function with 1 method) -keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4] -keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"] -keyword_args() # => ["name2"=>"hello","k1"=>4] +keyword_args(name2="ness") # => ["name2"=>"ness", "k1"=>4] +keyword_args(k1="mine") # => ["name2"=>"hello", "k1"=>"mine"] +keyword_args() # => ["name2"=>"hello", "k1"=>4] -# 可以组合各种类型的参数在同一个函数的参数列表中 +# 可以在一个函数中组合各种类型的参数 function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo") println("normal arg: $normal_arg") println("optional arg: $optional_positional_arg") println("keyword arg: $keyword_arg") end +# => all_the_args (generic function with 2 methods) all_the_args(1, 3, keyword_arg=4) -# prints: -# normal arg: 1 -# optional arg: 3 -# keyword arg: 4 +# => normal arg: 1 +# => optional arg: 3 +# => keyword arg: 4 # Julia 有一等函数 function create_adder(x) @@ -443,14 +508,16 @@ function create_adder(x) end return adder end +# => create_adder (generic function with 1 method) # 这是用 "stabby lambda syntax" 创建的匿名函数 (x -> x > 2)(3) # => true -# 这个函数和上面的 create_adder 一模一样 +# 这个函数和上面的 create_adder 是等价的 function create_adder(x) y -> x + y end +# => create_adder (generic function with 1 method) # 你也可以给内部函数起个名字 function create_adder(x) @@ -459,18 +526,19 @@ function create_adder(x) end adder end +# => create_adder (generic function with 1 method) -add_10 = create_adder(10) -add_10(3) # => 13 - +add_10 = create_adder(10) # => (::getfield(Main, Symbol("#adder#11")){Int64}) + # (generic function with 1 method) +add_10(3) # => 13 # 内置的高阶函数有 -map(add_10, [1,2,3]) # => [11, 12, 13] -filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] +map(add_10, [1,2,3]) # => [11, 12, 13] +filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] -# 还可以使用 list comprehensions 替代 map -[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13] -[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] +# 还可以使用 list comprehensions 让 map 更美观 +[add_10(i) for i = [1, 2, 3]] # => [11, 12, 13] +[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] #################################################### ## 5. 类型 @@ -482,248 +550,315 @@ filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] typeof(5) # => Int64 # 类型是一等值 -typeof(Int64) # => DataType -typeof(DataType) # => DataType +typeof(Int64) # => DataType +typeof(DataType) # => DataType # DataType 是代表类型的类型,也代表他自己的类型 -# 类型可用作文档化,优化,以及调度 -# 并不是静态检查类型 +# 类型可用于文档化代码、执行优化以及多重派分(dispatch) +# Julia 并不只是静态的检查类型 # 用户还可以自定义类型 -# 跟其他语言的 records 或 structs 一样 -# 用 `type` 关键字定义新的类型 +# 就跟其它语言的 record 或 struct 一样 +# 用 `struct` 关键字定义新的类型 -# type Name +# struct Name # field::OptionalType # ... # end -type Tiger - taillength::Float64 - coatcolor # 不附带类型标注的相当于 `::Any` +struct Tiger + taillength::Float64 + coatcolor # 不带类型标注相当于 `::Any` end -# 构造函数参数是类型的属性 -tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange") +# 默认构造函数的参数是类型的属性,按类型定义中的顺序排列 +tigger = Tiger(3.5, "orange") # => Tiger(3.5, "orange") # 用新类型作为构造函数还会创建一个类型 -sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire") +sherekhan = typeof(tigger)(5.6, "fire") # => Tiger(5.6, "fire") -# struct 类似的类型被称为具体类型 -# 他们可被实例化但不能有子类型 +# 类似 struct 的类型被称为具体类型 +# 它们可被实例化,但不能有子类型 # 另一种类型是抽象类型 -# abstract Name -abstract Cat # just a name and point in the type hierarchy +# 抽象类型名 +abstract type Cat end # 仅仅是指向类型结构层次的一个名称 -# 抽象类型不能被实例化,但是可以有子类型 +# 抽象类型不能被实例化,但可以有子类型 # 例如,Number 就是抽象类型 -subtypes(Number) # => 6-element Array{Any,1}: - # Complex{Float16} - # Complex{Float32} - # Complex{Float64} - # Complex{T<:Real} - # ImaginaryUnit - # Real -subtypes(Cat) # => 0-element Array{Any,1} - -# 所有的类型都有父类型; 可以用函数 `super` 得到父类型. +subtypes(Number) # => 2-element Array{Any,1}: + # => Complex + # => Real +subtypes(Cat) # => 0-element Array{Any,1} + +# AbstractString,类如其名,也是一个抽象类型 +subtypes(AbstractString) # => 4-element Array{Any,1}: + # => String + # => SubString + # => SubstitutionString + # => Test.GenericString + +# 所有的类型都有父类型。可以用函数 `supertype` 得到父类型 typeof(5) # => Int64 -super(Int64) # => Signed -super(Signed) # => Real -super(Real) # => Number -super(Number) # => Any -super(super(Signed)) # => Number -super(Any) # => Any -# 所有这些类型,除了 Int64, 都是抽象类型. - -# <: 是类型集成操作符 -type Lion <: Cat # Lion 是 Cat 的子类型 - mane_color - roar::String +supertype(Int64) # => Signed +supertype(Signed) # => Integer +supertype(Integer) # => Real +supertype(Real) # => Number +supertype(Number) # => Any +supertype(supertype(Signed)) # => Real +supertype(Any) # => Any +# 除了 Int64 外,其余的类型都是抽象类型 +typeof("fire") # => String +supertype(String) # => AbstractString +supertype(AbstractString) # => Any +supertype(SubString) # => AbstractString + +# <: 是子类型化操作符 +struct Lion <: Cat # Lion 是 Cat 的子类型 + mane_color + roar::AbstractString end # 可以继续为你的类型定义构造函数 -# 只需要定义一个同名的函数 -# 并调用已有的构造函数设置一个固定参数 -Lion(roar::String) = Lion("green",roar) -# 这是一个外部构造函数,因为他再类型定义之外 - -type Panther <: Cat # Panther 也是 Cat 的子类型 - eye_color - Panther() = new("green") - # Panthers 只有这个构造函数,没有默认构造函数 +# 只需要定义一个与类型同名的函数,并调用已有的构造函数得到正确的类型 +Lion(roar::AbstractString) = Lion("green", roar) # => Lion +# 这是一个外部构造函数,因为它在类型定义之外 + +struct Panther <: Cat # Panther 也是 Cat 的子类型 + eye_color + Panther() = new("green") + # Panthers 只有这个构造函数,没有默认构造函数 end -# 使用内置构造函数,如 Panther,可以让你控制 -# 如何构造类型的值 -# 应该尽可能使用外部构造函数而不是内部构造函数 +# 像 Panther 一样使用内置构造函数,让你可以控制如何构建类型的值 +# 应该尽量使用外部构造函数,而不是内部构造函数 #################################################### ## 6. 多分派 #################################################### -# 在Julia中, 所有的具名函数都是类属函数 -# 这意味着他们都是有很大小方法组成的 -# 每个 Lion 的构造函数都是类属函数 Lion 的方法 +# Julia 中所有的函数都是通用函数,或者叫做泛型函数(generic functions) +# 也就是说这些函数都是由许多小方法组合而成的 +# Lion 的每一种构造函数都是通用函数 Lion 的一个方法 # 我们来看一个非构造函数的例子 +# 首先,让我们定义一个函数 meow -# Lion, Panther, Tiger 的 meow 定义为 +# Lion, Panther, Tiger 的 meow 定义分别为 function meow(animal::Lion) - animal.roar # 使用点符号访问属性 + animal.roar # 使用点记号「.」访问属性 end +# => meow (generic function with 1 method) function meow(animal::Panther) - "grrr" + "grrr" end +# => meow (generic function with 2 methods) function meow(animal::Tiger) - "rawwwr" + "rawwwr" end +# => meow (generic function with 3 methods) # 试试 meow 函数 -meow(tigger) # => "rawwr" -meow(Lion("brown","ROAAR")) # => "ROAAR" +meow(tigger) # => "rawwwr" +meow(Lion("brown", "ROAAR")) # => "ROAAR" meow(Panther()) # => "grrr" -# 再看看层次结构 -issubtype(Tiger,Cat) # => false -issubtype(Lion,Cat) # => true -issubtype(Panther,Cat) # => true +# 回顾类型的层次结构 +Tiger <: Cat # => false +Lion <: Cat # => true +Panther <: Cat # => true -# 定义一个接收 Cats 的函数 +# 定义一个接收 Cat 类型的函数 function pet_cat(cat::Cat) - println("The cat says $(meow(cat))") + println("The cat says $(meow(cat))") end +# => pet_cat (generic function with 1 method) -pet_cat(Lion("42")) # => prints "The cat says 42" +pet_cat(Lion("42")) # => The cat says 42 try - pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,) + pet_cat(tigger) # => ERROR: MethodError: no method matching pet_cat(::Tiger) catch e println(e) end # 在面向对象语言中,通常都是单分派 -# 这意味着分派方法是通过第一个参数的类型决定的 -# 在Julia中, 所有参数类型都会被考虑到 +# 这意味着使用的方法取决于第一个参数的类型 +# 而 Julia 中选择方法时会考虑到所有参数的类型 -# 让我们定义有多个参数的函数,好看看区别 -function fight(t::Tiger,c::Cat) - println("The $(t.coatcolor) tiger wins!") +# 让我们定义一个有更多参数的函数,这样我们就能看出区别 +function fight(t::Tiger, c::Cat) + println("The $(t.coatcolor) tiger wins!") end # => fight (generic function with 1 method) -fight(tigger,Panther()) # => prints The orange tiger wins! -fight(tigger,Lion("ROAR")) # => prints The orange tiger wins! +fight(tigger, Panther()) # => The orange tiger wins! +fight(tigger, Lion("ROAR")) # => fight(tigger, Lion("ROAR")) -# 让我们修改一下传入具体为 Lion 类型时的行为 -fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!") +# 让我们修改一下传入 Lion 类型时的行为 +fight(t::Tiger, l::Lion) = println("The $(l.mane_color)-maned lion wins!") # => fight (generic function with 2 methods) -fight(tigger,Panther()) # => prints The orange tiger wins! -fight(tigger,Lion("ROAR")) # => prints The green-maned lion wins! +fight(tigger, Panther()) # => The orange tiger wins! +fight(tigger, Lion("ROAR")) # => The green-maned lion wins! -# 把 Tiger 去掉 -fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))") +# 我们不需要一只老虎参与战斗 +fight(l::Lion, c::Cat) = println("The victorious cat says $(meow(c))") # => fight (generic function with 3 methods) -fight(Lion("balooga!"),Panther()) # => prints The victorious cat says grrr +fight(Lion("balooga!"), Panther()) # => The victorious cat says grrr try - fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion) -catch + fight(Panther(), Lion("RAWR")) + # => ERROR: MethodError: no method matching fight(::Panther, ::Lion) + # => Closest candidates are: + # => fight(::Tiger, ::Lion) at ... + # => fight(::Tiger, ::Cat) at ... + # => fight(::Lion, ::Cat) at ... + # => ... +catch e + println(e) end -# 在试试让 Cat 在前面 -fight(c::Cat,l::Lion) = println("The cat beats the Lion") -# => Warning: New definition -# fight(Cat,Lion) at none:1 -# is ambiguous with -# fight(Lion,Cat) at none:2. -# Make sure -# fight(Lion,Lion) -# is defined first. -#fight (generic function with 4 methods) - -# 警告说明了无法判断使用哪个 fight 方法 -fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The victorious cat says rarrr -# 结果在老版本 Julia 中可能会不一样 - -fight(l::Lion,l2::Lion) = println("The lions come to a tie") -fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The lions come to a tie - - -# Under the hood -# 你还可以看看 llvm 以及生成的汇编代码 - -square_area(l) = l * l # square_area (generic function with 1 method) - -square_area(5) #25 - -# 给 square_area 一个整形时发生什么 -code_native(square_area, (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(square_area, (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(square_area, (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 - # -# 注意 只要参数中又浮点类型,Julia 就使用浮点指令 +# 试试把 Cat 放在前面 +fight(c::Cat, l::Lion) = println("The cat beats the Lion") +# => fight (generic function with 4 methods) + +# 由于无法判断该使用哪个 fight 方法,而产生了错误 +try + fight(Lion("RAR"), Lion("brown", "rarrr")) + # => ERROR: MethodError: fight(::Lion, ::Lion) is ambiguous. Candidates: + # => fight(c::Cat, l::Lion) in Main at ... + # => fight(l::Lion, c::Cat) in Main at ... + # => Possible fix, define + # => fight(::Lion, ::Lion) + # => ... +catch e + println(e) +end +# 在不同版本的 Julia 中错误信息可能有所不同 + +fight(l::Lion, l2::Lion) = println("The lions come to a tie") +# => fight (generic function with 5 methods) +fight(Lion("RAR"), Lion("brown", "rarrr")) # => The lions come to a tie + + +# 深入编译器之后 +# 你还可以看看 llvm 以及它生成的汇编代码 + +square_area(l) = l * l # => square_area (generic function with 1 method) +square_area(5) # => 25 + +# 当我们喂给 square_area 一个整数时会发生什么? +code_native(square_area, (Int32,), syntax = :intel) + # .text + # ; Function square_area { + # ; Location: REPL[116]:1 # 函数序言 (Prologue) + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: int.jl:54 + # imul ecx, ecx # 求 l 的平方,并把结果放在 ECX 中 + # ;} + # mov eax, ecx + # pop rbp # 还原旧的基址指针(base pointer) + # ret # 返回值放在 EAX 中 + # nop dword ptr [rax + rax] + # ;} +# 使用 syntax 参数指定输出语法。默认为 AT&T 格式,这里指定为 Intel 格式 + +code_native(square_area, (Float32,), syntax = :intel) + # .text + # ; Function square_area { + # ; Location: REPL[116]:1 + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: float.jl:398 + # vmulss xmm0, xmm0, xmm0 # 标量双精度乘法 (AVX) + # ;} + # pop rbp + # ret + # nop word ptr [rax + rax] + # ;} + +code_native(square_area, (Float64,), syntax = :intel) + # .text + # ; Function square_area { + # ; Location: REPL[116]:1 + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm0, xmm0, xmm0 # 标量双精度乘法 (AVX) + # ;} + # pop rbp + # ret + # nop word ptr [rax + rax] + # ;} + +# 注意!只要参数中有浮点数,Julia 就会使用浮点指令 # 让我们计算一下圆的面积 -circle_area(r) = pi * r * r # circle_area (generic function with 1 method) -circle_area(5) # 78.53981633974483 - -code_native(circle_area, (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 - # - -code_native(circle_area, (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 - # +circle_area(r) = pi * r * r # => circle_area (generic function with 1 method) +circle_area(5) # => 78.53981633974483 + +code_native(circle_area, (Int32,), syntax = :intel) + # .text + # ; Function circle_area { + # ; Location: REPL[121]:1 + # push rbp + # mov rbp, rsp + # ; Function *; { + # ; Location: operators.jl:502 + # ; Function *; { + # ; Location: promotion.jl:314 + # ; Function promote; { + # ; Location: promotion.jl:284 + # ; Function _promote; { + # ; Location: promotion.jl:261 + # ; Function convert; { + # ; Location: number.jl:7 + # ; Function Type; { + # ; Location: float.jl:60 + # vcvtsi2sd xmm0, xmm0, ecx # 从内存中读取整数 r + # movabs rax, 497710928 # 读取 pi + # ;}}}}} + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm1, xmm0, qword ptr [rax] # pi * r + # vmulsd xmm0, xmm1, xmm0 # (pi * r) * r + # ;}} + # pop rbp + # ret + # nop dword ptr [rax] + # ;} + +code_native(circle_area, (Float64,), syntax = :intel) + # .text + # ; Function circle_area { + # ; Location: REPL[121]:1 + # push rbp + # mov rbp, rsp + # movabs rax, 497711048 + # ; Function *; { + # ; Location: operators.jl:502 + # ; Function *; { + # ; Location: promotion.jl:314 + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm1, xmm0, qword ptr [rax] + # ;}}} + # ; Function *; { + # ; Location: float.jl:399 + # vmulsd xmm0, xmm1, xmm0 + # ;} + # pop rbp + # ret + # nop dword ptr [rax + rax] + # ;} ``` + +## 拓展阅读材料 + +你可以在 [Julia 中文文档](http://docs.juliacn.com/latest/) / [Julia 文档(en)](https://docs.julialang.org/) +中获得关于 Julia 的更多细节。 + +如果有任何问题可以去 [Julia 中文社区](http://discourse.juliacn.com/) / [官方社区(en)](https://discourse.julialang.org/) 提问,大家对待新手都非常的友好。 diff --git a/zh-cn/matlab-cn.html.markdown b/zh-cn/matlab-cn.html.markdown index 2fbccfc4..d215755c 100644 --- a/zh-cn/matlab-cn.html.markdown +++ b/zh-cn/matlab-cn.html.markdown @@ -10,9 +10,12 @@ lang: zh-cn ---
-MATLAB 是 MATrix LABoratory (矩阵实验室)的缩写,它是一种功能强大的数值计算语言,在工程和数学领域中应用广泛。
+MATLAB 是 MATrix LABoratory(矩阵实验室)的缩写。
+它是一种功能强大的数值计算语言,在工程和数学领域中应用广泛。
-如果您有任何需要反馈或交流的内容,请联系本教程作者[@the_ozzinator](https://twitter.com/the_ozzinator)、[osvaldo.t.mendoza@gmail.com](mailto:osvaldo.t.mendoza@gmail.com)。
+如果您有任何需要反馈或交流的内容,请联系本教程作者:
+[@the_ozzinator](https://twitter.com/the_ozzinator)
+或 [osvaldo.t.mendoza@gmail.com](mailto:osvaldo.t.mendoza@gmail.com)。
```matlab
% 以百分号作为注释符
@@ -45,7 +48,7 @@ edit('myfunction.m') % 在编辑器中打开指定函数或脚本 type('myfunction.m') % 在命令窗口中打印指定函数或脚本的源码
profile on % 打开 profile 代码分析工具
-profile of % 关闭 profile 代码分析工具
+profile off % 关闭 profile 代码分析工具
profile viewer % 查看 profile 代码分析工具的分析结果
help command % 在命令窗口中显示指定命令的帮助文档
@@ -113,7 +116,7 @@ b(2) % ans = 符 % 元组(cell 数组)
a = {'one', 'two', 'three'}
a(1) % ans = 'one' - 返回一个元组
-char(a(1)) % ans = one - 返回一个字符串
+a{1} % ans = one - 返回一个字符串
% 结构体
@@ -210,8 +213,8 @@ size(A) % 返回矩阵的行数和列数,ans = 3 3 A(1, :) =[] % 删除矩阵的第 1 行
A(:, 1) =[] % 删除矩阵的第 1 列
-transpose(A) % 矩阵转置,等价于 A'
-ctranspose(A) % 矩阵的共轭转置(对矩阵中的每个元素取共轭复数)
+transpose(A) % 矩阵(非共轭)转置,等价于 A.' (注意!有个点)
+ctranspose(A) % 矩阵的共轭转置(对矩阵中的每个元素取共轭复数),等价于 A'
% 元素运算 vs. 矩阵运算
@@ -219,18 +222,20 @@ ctranspose(A) % 矩阵的共轭转置(对矩阵中的每个元素取共轭复 % 在运算符加上英文句点就是对矩阵中的元素进行元素计算
% 示例如下:
A * B % 矩阵乘法,要求 A 的列数等于 B 的行数
-A .* B % 元素乘法,要求 A 和 B 形状一致(A 的行数等于 B 的行数, A 的列数等于 B 的列数)
-% 元素乘法的结果是与 A 和 B 形状一致的矩阵,其每个元素等于 A 对应位置的元素乘 B 对应位置的元素
+A .* B % 元素乘法,要求 A 和 B 形状一致,即两矩阵行列数完全一致
+ % 元素乘法的结果是与 A 和 B 形状一致的矩阵
+ % 其每个元素等于 A 对应位置的元素乘 B 对应位置的元素
% 以下函数中,函数名以 m 结尾的执行矩阵运算,其余执行元素运算:
exp(A) % 对矩阵中每个元素做指数运算
expm(A) % 对矩阵整体做指数运算
sqrt(A) % 对矩阵中每个元素做开方运算
-sqrtm(A) % 对矩阵整体做开放运算(即试图求出一个矩阵,该矩阵与自身的乘积等于 A 矩阵)
+sqrtm(A) % 对矩阵整体做开方运算(即试图求出一个矩阵,该矩阵与自身的乘积等于 A 矩阵)
% 绘图
-x = 0:.10:2*pi; % 生成一向量,其元素从 0 开始,以 0.1 的间隔一直递增到 2*pi(pi 就是圆周率)
+x = 0:0.1:2*pi; % 生成一向量,其元素从 0 开始,以 0.1 的间隔一直递增到 2*pi
+ % 其中 pi 为圆周率
y = sin(x);
plot(x,y)
xlabel('x axis')
@@ -288,7 +293,10 @@ clf clear % 清除图形窗口中的图像,并重置图像属性 % 也可以用 gcf 函数返回当前图像的句柄
h = plot(x, y); % 在创建图像时显式地保存图像句柄
set(h, 'Color', 'r')
-% 颜色代码:'y' 黄色,'m' 洋红色,'c' 青色,'r' 红色,'g' 绿色,'b' 蓝色,'w' 白色,'k' 黑色
+% 颜色代码:
+% 'y' 黄色,'m' 洋红,'c' 青色
+% 'r' 红色,'g' 绿色,'b' 蓝色
+% 'w' 白色,'k' 黑色
set(h, 'Color', [0.5, 0.5, 0.4])
% 也可以使用 RGB 值指定颜色
set(h, 'LineStyle', '--')
@@ -328,7 +336,8 @@ load('myFileName.mat') % 将指定文件中的变量载入到当前工作空间 % 与脚本文件类似,同样以 .m 作为后缀名
% 但函数文件可以接受用户输入的参数并返回运算结果
% 并且函数拥有自己的工作空间(变量域),不必担心变量名称冲突
-% 函数文件的名称应当与其所定义的函数的名称一致(比如下面例子中函数文件就应命名为 double_input.m)
+% 函数文件的名称应当与其所定义的函数的名称一致
+% 比如下面例子中函数文件就应命名为 double_input.m
% 使用 'help double_input.m' 可返回函数定义中第一行注释信息
function output = double_input(x)
% double_input(x) 返回 x 的 2 倍
@@ -463,14 +472,16 @@ triu(x) % 返回 x 的上三角这部分 tril(x) % 返回 x 的下三角这部分
cross(A, B) % 返回 A 和 B 的叉积(矢量积、外积)
dot(A, B) % 返回 A 和 B 的点积(数量积、内积),要求 A 和 B 必须等长
-transpose(A) % A 的转置,等价于 A'
+transpose(A) % 矩阵(非共轭)转置,等价于 A.' (注意!有个点)
fliplr(A) % 将一个矩阵左右翻转
flipud(A) % 将一个矩阵上下翻转
% 矩阵分解
-[L, U, P] = lu(A) % LU 分解:PA = LU,L 是下三角阵,U 是上三角阵,P 是置换阵
-[P, D] = eig(A) % 特征值分解:AP = PD,D 是由特征值构成的对角阵,P 的各列就是对应的特征向量
-[U, S, V] = svd(X) % 奇异值分解:XV = US,U 和 V 是酉矩阵,S 是由奇异值构成的半正定实数对角阵
+[L, U, P] = lu(A) % LU 分解:PA = LU,L 是下三角阵,U 是上三角阵,P 是置换阵
+[P, D] = eig(A) % 特征值分解:AP = PD
+ % D 是由特征值构成的对角阵,P 的各列就是对应的特征向量
+[U, S, V] = svd(X) % 奇异值分解:XV = US
+ % U 和 V 是酉矩阵,S 是由奇异值构成的半正定实数对角阵
% 常用向量函数
max % 最大值
@@ -489,5 +500,5 @@ perms(x) % x 元素的全排列 ## 相关资料
-* 官方网页:[http://http://www.mathworks.com/products/matlab/](http://www.mathworks.com/products/matlab/)
-* 官方论坛:[http://www.mathworks.com/matlabcentral/answers/](http://www.mathworks.com/matlabcentral/answers/)
+* 官方网页:[MATLAB - 技术计算语言 - MATLAB & Simulink](https://ww2.mathworks.cn/products/matlab.html)
+* 官方论坛:[MATLAB Answers - MATLAB Central](https://ww2.mathworks.cn/matlabcentral/answers/)
diff --git a/zh-cn/python3-cn.html.markdown b/zh-cn/python3-cn.html.markdown index 76455a46..fd962305 100644 --- a/zh-cn/python3-cn.html.markdown +++ b/zh-cn/python3-cn.html.markdown @@ -10,13 +10,13 @@ filename: learnpython3-cn.py lang: zh-cn --- -Python是由吉多·范罗苏姆(Guido Van Rossum)在90年代早期设计。它是如今最常用的编程 -语言之一。它的语法简洁且优美,几乎就是可执行的伪代码。 +Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计。 +它是如今最常用的编程语言之一。它的语法简洁且优美,几乎就是可执行的伪代码。 -欢迎大家斧正。英文版原作Louie Dinh [@louiedinh](http://twitter.com/louiedinh) -或着Email louiedinh [at] [谷歌的信箱服务]。中文翻译Geoff Liu。 +欢迎大家斧正。英文版原作 Louie Dinh [@louiedinh](http://twitter.com/louiedinh) +邮箱 louiedinh [at] [谷歌的信箱服务]。中文翻译 Geoff Liu。 -注意:这篇教程是特别为Python3写的。如果你想学旧版Python2,我们特别有另一篇教程。 +注意:这篇教程是基于 Python 3 写的。如果你想学旧版 Python 2,我们特别有[另一篇教程](http://learnxinyminutes.com/docs/python/)。 ```python @@ -70,15 +70,15 @@ not True # => False not False # => True # 逻辑运算符,注意and和or都是小写 -True and False #=> False -False or True #=> True +True and False # => False +False or True # => True # 整数也可以当作布尔值 -0 and 2 #=> 0 --5 or 0 #=> -5 -0 == False #=> True -2 == True #=> False -1 == True #=> True +0 and 2 # => 0 +-5 or 0 # => -5 +0 == False # => True +2 == True # => False +1 == True # => True # 用==判断相等 1 == 1 # => True @@ -113,10 +113,11 @@ False or True #=> True # 可以重复参数以节省时间 "{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick") -#=> "Jack be nimble, Jack be quick, Jack jump over the candle stick" +# => "Jack be nimble, Jack be quick, Jack jump over the candle stick" # 如果不想数参数,可以用关键字 -"{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna" +"{name} wants to eat {food}".format(name="Bob", food="lasagna") +# => "Bob wants to eat lasagna" # 如果你的Python3程序也要在Python2.5以下环境运行,也可以用老式的格式化语法 "%s can be %s the %s way" % ("strings", "interpolated", "old") @@ -132,8 +133,8 @@ None is None # => True # 所有其他值都是True bool(0) # => False bool("") # => False -bool([]) #=> False -bool({}) #=> False +bool([]) # => False +bool({}) # => False #################################################### @@ -233,7 +234,8 @@ filled_dict = {"one": 1, "two": 2, "three": 3} filled_dict["one"] # => 1 -# 用keys获得所有的键。因为keys返回一个可迭代对象,所以在这里把结果包在list里。我们下面会详细介绍可迭代。 +# 用 keys 获得所有的键。 +# 因为 keys 返回一个可迭代对象,所以在这里把结果包在 list 里。我们下面会详细介绍可迭代。 # 注意:字典键的顺序是不定的,你得到的结果可能和以下不同。 list(filled_dict.keys()) # => ["three", "two", "one"] @@ -261,7 +263,7 @@ filled_dict.setdefault("five", 5) # filled_dict["five"]设为5 filled_dict.setdefault("five", 6) # filled_dict["five"]还是5 # 字典赋值 -filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4} +filled_dict.update({"four":4}) # => {"one": 1, "two": 2, "three": 3, "four": 4} filled_dict["four"] = 4 # 另一种赋值方法 # 用del删除 @@ -362,7 +364,7 @@ else: # else语句是可选的,必须在所有的except之后 filled_dict = {"one": 1, "two": 2, "three": 3} our_iterable = filled_dict.keys() -print(our_iterable) # => range(1,10) 是一个实现可迭代接口的对象 +print(our_iterable) # => dict_keys(['one', 'two', 'three']),是一个实现可迭代接口的对象 # 可迭代对象可以遍历 for i in our_iterable: @@ -376,17 +378,17 @@ our_iterator = iter(our_iterable) # 迭代器是一个可以记住遍历的位置的对象 # 用__next__可以取得下一个元素 -our_iterator.__next__() #=> "one" +our_iterator.__next__() # => "one" # 再一次调取__next__时会记得位置 -our_iterator.__next__() #=> "two" -our_iterator.__next__() #=> "three" +our_iterator.__next__() # => "two" +our_iterator.__next__() # => "three" # 当迭代器所有元素都取出后,会抛出StopIteration our_iterator.__next__() # 抛出StopIteration # 可以用list一次取出迭代器所有的元素 -list(filled_dict.keys()) #=> Returns ["one", "two", "three"] +list(filled_dict.keys()) # => Returns ["one", "two", "three"] @@ -568,13 +570,14 @@ def double_numbers(iterable): yield i + i # 生成器只有在需要时才计算下一个值。它们每一次循环只生成一个值,而不是把所有的 -# 值全部算好。这意味着double_numbers不会生成大于15的数字。 +# 值全部算好。 # # range的返回值也是一个生成器,不然一个1到900000000的列表会花很多时间和内存。 # # 如果你想用一个Python的关键字当作变量名,可以加一个下划线来区分。 range_ = range(1, 900000000) # 当找到一个 >=30 的结果就会停 +# 这意味着 `double_numbers` 不会生成大于30的数。 for i in double_numbers(range_): print(i) if i >= 30: diff --git a/zh-cn/swift-cn.html.markdown b/zh-cn/swift-cn.html.markdown index c25b2918..18bc52ed 100644 --- a/zh-cn/swift-cn.html.markdown +++ b/zh-cn/swift-cn.html.markdown @@ -110,7 +110,7 @@ anyObjectVar = "Changed value to a string, not good practice, but possible." // -// Mark: 数组与字典(关联数组) +// MARK: 数组与字典(关联数组) // /* @@ -214,9 +214,9 @@ func greet(name: String, day: String) -> String { } greet("Bob", day: "Tuesday") -// 第一个参数表示外部参数名和内部参数名使用同一个名称。 +// 第一个参数`_`表示不使用外部参数名,忽略`_`表示外部参数名和内部参数名使用同一个名称。 // 第二个参数表示外部参数名使用 `externalParamName` ,内部参数名使用 `localParamName` -func greet2(requiredName requiredName: String, externalParamName localParamName: String) -> String { +func greet2(_ requiredName: String, externalParamName localParamName: String) -> String { return "Hello \(requiredName), the day is \(localParamName)" } greet2(requiredName:"John", externalParamName: "Sunday") // 调用时,使用命名参数来指定参数的值 @@ -250,7 +250,7 @@ var increment = makeIncrementer() increment(7) // 强制进行指针传递 (引用传递),使用 `inout` 关键字修饰函数参数 -func swapTwoInts(inout a: Int, inout b: Int) { +func swapTwoInts(a: inout Int, b: inout Int) { let tempA = a a = b b = tempA @@ -521,7 +521,7 @@ class MyShape: Rect { // 在 optional 属性,方法或下标运算符后面加一个问号,可以优雅地忽略 nil 值,返回 nil。 // 这样就不会引起运行时错误 (runtime error) - if let reshape = self.delegate?.canReshape?() where reshape { + if let reshape = self.delegate?.canReshape?() { // 注意语句中的问号 self.delegate?.reshape?() } @@ -575,10 +575,10 @@ print(foundAtIndex == 2) // true // 自定义运算符可以以下面的字符打头: // / = - + * % < > ! & | ^ . ~ // 甚至是 Unicode 的数学运算符等 -prefix operator !!! {} +prefix operator !!! // 定义一个前缀运算符,使矩形的边长放大三倍 -prefix func !!! (inout shape: Square) -> Square { +prefix func !!! (shape: inout Square) -> Square { shape.sideLength *= 3 return shape } @@ -591,8 +591,8 @@ print(mySquare.sideLength) // 4 print(mySquare.sideLength) // 12 // 运算符也可以是泛型 -infix operator <-> {} -func <-><T: Equatable> (inout a: T, inout b: T) { +infix operator <-> +func <-><T: Equatable> (a: inout T, b: inout T) { let c = a a = b b = c diff --git a/zh-cn/typescript-cn.html.markdown b/zh-cn/typescript-cn.html.markdown index 032f89e4..5d6153da 100644 --- a/zh-cn/typescript-cn.html.markdown +++ b/zh-cn/typescript-cn.html.markdown @@ -9,97 +9,103 @@ filename: learntypescript-cn.ts lang: zh-cn --- -TypeScript是一门为开发大型JavaScript应用而设计的语言。TypeScript在JavaScript的基础上增加了类、模块、接口、泛型和静态类型(可选)等常见的概念。它是JavaScript的一个超集:所有JavaScript代码都是有效的TypeScript代码,所以任何JavaScript项目都可以无缝引入TypeScript. TypeScript编译器会把TypeScript代码编译成JavaScript代码。 +TypeScript 是一门为开发大型 JavaScript 应用而设计的语言。TypeScript 在 JavaScript 的基础上增加了类、模块、接口、泛型和静态类型(可选)等常见的概念。它是 JavaScript 的超集:所有 JavaScript 代码都是有效的 TypeScript 代码,因此任何 JavaScript 项目都可以无缝引入 TypeScript,TypeScript 编译器最终会把 TypeScript 代码编译成 JavaScript 代码。 -本文只关注TypeScript额外增加的区别于[JavaScript](../javascript-cn/)的语法,. +本文只关注 TypeScript 额外增加的区别于 [JavaScript](../javascript-cn/) 的语法,. -如需测试TypeScript编译器,你可以在[Playground](http://www.typescriptlang.org/Playground)码代码,它会自动编译成JavaScript代码然后直接显示出来。 +如需测试 TypeScript 编译器,你可以到 [Playground](https://www.typescriptlang.org/play/) 编写代码,它会自动将你编写的 TypeScript 代码编译成 JavaScript 代码后,在右侧即时展示出来。 -```js -// TypeScript有三种基本类型 -var isDone: boolean = false; -var lines: number = 42; -var name: string = "Anders"; +```ts +// TypeScript 有三种基本类型,布尔类型、数值类型、字符串类型 +let isDone: boolean = false; +let lines: number = 42; +let name: string = 'Anders'; -// 如果不知道是什么类型,可以使用"any"(任意)类型 -var notSure: any = 4; -notSure = "maybe a string instead"; -notSure = false; // 亦可,定义为布尔型 +// 如果不知道是什么类型,可以使用 "any" (任意)类型 +let notSure: any = 4; +notSure = '可以重新赋值,转换为字符串类型'; +notSure = false; // 亦可,重新定义为布尔类型 -// 对于集合的声明, 有类型化数组和泛型数组 -var list: number[] = [1, 2, 3]; -// 另外一种,使用泛型数组 -var list: Array<number> = [1, 2, 3]; +// 使用 const 关键字将一个字面量修饰为常量 +const numLivesForCat = 9; +numLivesForCat = 1; // 常量不能重新被赋值,所以这里会报错 + +// TypeScript 中的 collection 有两种表示形式, 一种是有类型的数组,另一种是泛型数组 +let list: number[] = [1, 2, 3]; +// 或者,使用泛型数组 +let list: Array<number> = [1, 2, 3]; // 枚举: -enum Color {Red, Green, Blue}; -var c: Color = Color.Green; +enum Color {Red, Green, Blue} +let c: Color = Color.Green; -// 最后,"void"用于函数没有任何返回的特殊情况下 +// 最后是 "void",它用于表明函数没有任何返回值的特殊情况 function bigHorribleAlert(): void { - alert("I'm a little annoying box!"); + alert('我是个烦人的弹出框!'); } -// 函数是"第一等公民"(first class citizens), 支持使用箭头表达式和类型推断 - -// 以下是相等的,TypeScript编译器会把它们编译成相同的JavaScript代码 -var f1 = function(i: number): number { return i * i; } -// 返回推断类型的值 -var f2 = function(i: number) { return i * i; } -var f3 = (i: number): number => { return i * i; } -// 返回推断类型的值 -var f4 = (i: number) => { return i * i; } -// 返回推断类型的值, 单行程式可以不需要return关键字和大括号 -var f5 = (i: number) => i * i; - -// 接口是结构化的,任何具有这些属性的对象都与该接口兼容 +// 函数是"一等公民"(first class citizens), 支持使用 lambda 胖箭头表达式和类型推断 + +// 以下 f1-f5 五个函数是等价的,TypeScript 编译器会把它们编译成相同的 JavaScript 代码(可以到 Playground 验证) +// 一般的函数 +let f1 = function(i: number): number { return i * i; }; +// 根据返回值推断函数返回类型 +let f2 = function(i: number) { return i * i; }; +// 胖箭头表达式 +let f3 = (i: number): number => { return i * i; }; +// 根据返回值推断返回类型的胖箭头表达式 +let f4 = (i: number) => { return i * i; }; +// 根据返回值推断返回类型的胖箭头表达式, 省略花括号的同时,可以同时省去 return 关键字 +let f5 = (i: number) => i * i; + +// 接口是结构化的,任何具备接口中声明的全部属性的对象,都与该接口兼容 interface Person { name: string; - // 可选属性,使用"?"标识 + // 使用 "?" 标识,表明该属性是一个非必需属性 age?: number; // 函数 move(): void; } -// 实现"Person"接口的对象,当它有了"name"和"move"方法之后可被视为一个"Person" -var p: Person = { name: "Bobby", move: () => {} }; -// 带了可选参数的对象 -var validPerson: Person = { name: "Bobby", age: 42, move: () => {} }; -// 因为"age"不是"number"类型所以这不是一个"Person" -var invalidPerson: Person = { name: "Bobby", age: true }; +// 实现 "Person" 接口的对象,当它具备 "name" 属性和 "move" 方法之后可被视为一个 "Person" +let p: Person = { name: 'Bobby', move: () => {} }; +// 带可选属性的对象 +let validPerson: Person = { name: 'Bobby', age: 42, move: () => {} }; +// 由于该对象 "age" 属性的类型不是 "number" ,所以这不是一个 "Person" +let invalidPerson: Person = { name: 'Bobby', age: true }; // 接口同样可以描述一个函数的类型 interface SearchFunc { (source: string, subString: string): boolean; } -// 参数名并不重要,参数类型才是重要的 -var mySearch: SearchFunc; +// 参数名并不重要,参数类型才是最重要的 +let mySearch: SearchFunc; mySearch = function(src: string, sub: string) { - return src.search(sub) != -1; -} + return src.search(sub) !== -1; +}; -// 类 - 成员默认为公共的(public) +// 类 - 成员访问权限默认都是公共的 (public) class Point { - // 属性 + // 成员属性 x: number; - // 构造器 - 这里面的public/private关键字会为属性生成样板代码和初始化值 - // 这个例子中,y会被同x一样定义,不需要额外代码 - // 同样支持默认值 + // 构造器 - 在构造器中使用 public/private 关键字修饰的变量,会被声明为类的成员属性。 + // 下面这个例子中,y 会像 x 一样被声明定义为类成员属性,而不再需要额外代码 + // 声明时,同样支持指定默认值 constructor(x: number, public y: number = 0) { this.x = x; } - // 函数 + // 成员函数 dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } // 静态成员 static origin = new Point(0, 0); } -var p1 = new Point(10 ,20); -var p2 = new Point(25); //y为0 +let p1 = new Point(10 , 20); +let p2 = new Point(25); // y 为构造器中指定的默认值:0 // 继承 class Point3D extends Point { @@ -107,14 +113,14 @@ class Point3D extends Point { super(x, y); // 必须显式调用父类的构造器 } - // 重写 + // 重写父类中的 dist() 函数 dist() { - var d = super.dist(); + let d = super.dist(); return Math.sqrt(d * d + this.z * this.z); } } -// 模块, "."可以作为子模块的分隔符 +// 模块, "." 符号可以作为子模块的分隔符 module Geometry { export class Square { constructor(public sideLength: number = 0) { @@ -125,12 +131,12 @@ module Geometry { } } -var s1 = new Geometry.Square(5); +let s1 = new Geometry.Square(5); -// 引入模块并定义本地别名 +// 为模块创建一个本地别名 import G = Geometry; -var s2 = new G.Square(10); +let s2 = new G.Square(10); // 泛型 // 类 @@ -146,21 +152,21 @@ interface Pair<T> { } // 以及函数 -var pairToTuple = function<T>(p: Pair<T>) { +let pairToTuple = function<T>(p: Pair<T>) { return new Tuple(p.item1, p.item2); }; -var tuple = pairToTuple({ item1:"hello", item2:"world"}); +let tuple = pairToTuple({ item1: 'hello', item2: 'world'}); // 引用定义文件 /// <reference path="jquery.d.ts" /> // 模板字符串(使用反引号的字符串) // 嵌入变量的模板字符串 -var name = 'Tyrone'; -var greeting = `Hi ${name}, how are you?` +let name = 'Tyrone'; +let greeting = `Hi ${name}, how are you?`; // 有多行内容的模板字符串 -var multiline = `This is an example +let multiline = `This is an example of a multiline string`; ``` diff --git a/zh-cn/visualbasic-cn.html.markdown b/zh-cn/visualbasic-cn.html.markdown index cdc2d808..e30041b3 100644 --- a/zh-cn/visualbasic-cn.html.markdown +++ b/zh-cn/visualbasic-cn.html.markdown @@ -8,7 +8,7 @@ lang: zh-cn filename: learnvisualbasic-cn.vb --- -```vbnet +``` Module Module1 Sub Main() |