diff options
278 files changed, 26549 insertions, 3207 deletions
diff --git a/CHICKEN.html.markdown b/CHICKEN.html.markdown index 080527a9..3f7cc2db 100644 --- a/CHICKEN.html.markdown +++ b/CHICKEN.html.markdown @@ -8,7 +8,7 @@ contributors: CHICKEN is an implementation of Scheme programming language that can compile Scheme programs to C code as well as interpret them. CHICKEN -supports RSR5 and RSR7 (work in progress) standards and many extensions. +supports R5RS and R7RS (work in progress) standards and many extensions. ```scheme @@ -81,7 +81,7 @@ supports RSR5 and RSR7 (work in progress) standards and many extensions. (string-append "pine" "apple") ;; => "pineapple" (string-ref "tapioca" 3) ;; => #\i;; character 'i' is at index 3 (string->list "CHICKEN") ;; => (#\C #\H #\I #\C #\K #\E #\N) -(string->intersperse '("1" "2") ":") ;; => "1:2" +(string-intersperse '("1" "2") ":") ;; => "1:2" (string-split "1:2:3" ":") ;; => ("1" "2" "3") @@ -235,12 +235,12 @@ sqr ;; => #<procedure (sqr x)> (= 2 1) ;; => #f ;; 'eq?' returns #t if two arguments refer to the same object in memory -;; In other words, it's a simple pointer comparision. +;; In other words, it's a simple pointer comparison. (eq? '() '()) ;; => #t ;; there's only one empty list in memory (eq? (list 3) (list 3)) ;; => #f ;; not the same object (eq? 'yes 'yes) ;; => #t (eq? 3 3) ;; => #t ;; don't do this even if it works in this case -(eq? 3 3.0) ;; => #f ;; it's better to use '=' for number comparisions +(eq? 3 3.0) ;; => #f ;; it's better to use '=' for number comparisons (eq? "Hello" "Hello") ;; => #f ;; 'eqv?' is same as 'eq?' all datatypes except numbers and characters @@ -255,7 +255,7 @@ sqr ;; => #<procedure (sqr x)> (equal? '(1 2 3) '(1 2 3)) ;; => #t (equal? #(a b c) #(a b c)) ;; => #t (equal? 'a 'a) ;; => #t -(equal? "abc" "abc") ;; => #f +(equal? "abc" "abc") ;; => #t ;; In Summary: ;; eq? tests if objects are identical @@ -510,7 +510,7 @@ sqr ;; => #<procedure (sqr x)> ``` ## Further Reading * [CHICKEN User's Manual](http://wiki.call-cc.org/man/4/The%20User%27s%20Manual). -* [RSR5 standards](http://www.schemers.org/Documents/Standards/R5RS) +* [R5RS standards](http://www.schemers.org/Documents/Standards/R5RS) ## Extra Info diff --git a/CONTRIBUTING.markdown b/CONTRIBUTING.markdown index 5fa1d03d..455c3256 100644 --- a/CONTRIBUTING.markdown +++ b/CONTRIBUTING.markdown @@ -71,3 +71,10 @@ contributors: lang: ep-ep --- ``` + +### Should I add myself as a Contributor? + +If you want to add yourself to contributors, keep in mind that contributors get +equal billing, and the first contributor usually wrote the whole article. Please +use your judgement when deciding if your contribution constitutes a substantial +addition or not. 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/LOLCODE.html.markdown b/LOLCODE.html.markdown index d7ed10c9..217b7b7d 100644 --- a/LOLCODE.html.markdown +++ b/LOLCODE.html.markdown @@ -7,7 +7,7 @@ contributors: LOLCODE is an esoteric programming language designed to resemble the speech of [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257). -```LOLCODE +``` BTW This is an inline comment BTW All code must begin with `HAI <language version>` and end with `KTHXBYE` diff --git a/angularjs.html.markdown b/angularjs.html.markdown index 737b99c7..9156490e 100644 --- a/angularjs.html.markdown +++ b/angularjs.html.markdown @@ -699,10 +699,10 @@ app.controller('myCtrl', function($scope) { **Examples** -- http://www.w3schools.com/angular/angular_examples.asp +- [http://www.w3schools.com/angular/angular_examples.asp](http://www.w3schools.com/angular/angular_examples.asp) **References** -- http://www.w3schools.com/angular/angular_ref_directives.asp -- http://www.w3schools.com/angular/default.asp -- https://teamtreehouse.com/library/angular-basics/ +- [http://www.w3schools.com/angular/angular_ref_directives.asp](http://www.w3schools.com/angular/angular_ref_directives.asp) +- [http://www.w3schools.com/angular/default.asp](http://www.w3schools.com/angular/default.asp) +- [https://teamtreehouse.com/library/angular-basics/](https://teamtreehouse.com/library/angular-basics/) diff --git a/asciidoc.html.markdown b/asciidoc.html.markdown index 8326c581..51d0d7c9 100644 --- a/asciidoc.html.markdown +++ b/asciidoc.html.markdown @@ -2,6 +2,7 @@ language: asciidoc contributors: - ["Ryan Mavilia", "http://unoriginality.rocks/"] + - ["Abel Salgado Romero", "https://twitter.com/abelsromero"] filename: asciidoc.md --- @@ -81,10 +82,6 @@ Section Titles ===== Level 4 <h5> -====== Level 5 <h6> - -======= Level 6 <h7> - ``` Lists @@ -120,3 +117,16 @@ You can nest lists by adding extra asterisks or periods up to five times. .... foo 4 ..... foo 5 ``` + +## Further Reading + +There are two tools to process AsciiDoc documents: + +1. [AsciiDoc](http://asciidoc.org/): original Python implementation available in the main Linux distributions. Stable and currently in maintenance mode. +2. [Asciidoctor](http://asciidoctor.org/): alternative Ruby implementation, usable also from Java and JavaScript. Under active development, it aims to extend the AsciiDoc syntax with new features and output formats. + +Following links are related to `Asciidoctor` implementation: + +* [Markdown - AsciiDoc syntax comparison](http://asciidoctor.org/docs/user-manual/#comparison-by-example): side-by-side comparison of common Markdown and AsciiDoc elements. +* [Getting started](http://asciidoctor.org/docs/#get-started-with-asciidoctor): installation and quick start guides to render simple documents. +* [Asciidoctor User Manual](http://asciidoctor.org/docs/user-manual/): complete single-document manual with syntax reference, examples, rendering tools, amongst others. 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 90f88b1a..de26c0a1 100644 --- a/awk.html.markdown +++ b/awk.html.markdown @@ -2,8 +2,8 @@ language: awk filename: learnawk.awk contributors: - - ["Marshall Mason", "http://github.com/marshallmason"] -lang: en + - ["Marshall Mason", "http://github.com/marshallmason"] + --- AWK is a standard tool on every POSIX-compliant UNIX system. It's like a @@ -38,7 +38,7 @@ BEGIN { a = count + 1 b = count - 1 c = count * 1 - d = count / 1 + d = count / 1 # integer division e = count % 1 # modulus f = count ^ 1 # exponentiation @@ -143,7 +143,7 @@ BEGIN { } # Here's how you define a function -function arithmetic_functions(a, b, c, localvar) { +function arithmetic_functions(a, b, c, d) { # Probably the most annoying part of AWK is that there are no local # variables. Everything is global. For short scripts, this is fine, even @@ -161,7 +161,7 @@ function arithmetic_functions(a, b, c, localvar) { # Most AWK implementations have some standard trig functions localvar = sin(a) localvar = cos(a) - localvar = atan2(a, b) # arc tangent of b / a + localvar = atan2(b, a) # arc tangent of b / a # And logarithmic stuff localvar = exp(a) @@ -264,7 +264,7 @@ function io_functions( localvar) { # automatically for you. # You can probably guess there are other $ variables. Every line is - # implicitely split before every action is called, much like the shell + # implicitly split before every action is called, much like the shell # 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 diff --git a/bash.html.markdown b/bash.html.markdown index 14366e4c..cb805da7 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -15,6 +15,7 @@ contributors: - ["Leo Rudberg", "https://github.com/LOZORD"] - ["Betsy Lorton", "https://github.com/schbetsy"] - ["John Detter", "https://github.com/jdetter"] + - ["Harry Mumford-Turner", "https://github.com/harrymt"] filename: LearnBash.sh --- @@ -24,65 +25,87 @@ 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. # Simple hello world example: -echo Hello world! +echo Hello world! # => Hello world! # Each command starts on a new line, or after semicolon: echo 'This is the first line'; echo 'This is the second line' +# => This is the first line +# => This is the second line # Declaring a variable looks like this: Variable="Some string" # But not like this: -Variable = "Some string" +Variable = "Some string" # => returns error "Variable: command not found" # Bash will decide that Variable is a command it must execute and give an error # because it can't be found. # Or like this: -Variable= 'Some string' +Variable= 'Some string' # => returns error: "Some string: command not found" # Bash will decide that 'Some string' is a command it must execute and give an # error because it can't be found. (In this case the 'Variable=' part is seen # as a variable assignment valid only for the scope of the 'Some string' # command.) # Using the variable: -echo $Variable -echo "$Variable" -echo '$Variable' +echo $Variable # => Some string +echo "$Variable" # => Some string +echo '$Variable' # => $Variable # When you use the variable itself — assign it, export it, or else — you write # its name without $. If you want to use the variable's value, you should use $. # Note that ' (single quote) won't expand the variables! # Parameter expansion ${ }: -echo ${Variable} +echo ${Variable} # => Some string # This is a simple usage of parameter expansion # Parameter Expansion gets a value from a variable. It "expands" or prints the value # During the expansion time the value or parameter are able to be modified # Below are other modifications that add onto this expansion # String substitution in variables -echo ${Variable/Some/A} +echo ${Variable/Some/A} # => A string # This will substitute the first occurrence of "Some" with "A" # Substring from a variable Length=7 -echo ${Variable:0:Length} +echo ${Variable:0:$Length} # => Some st # This will return only the first 7 characters of the value # Default value for variable -echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} +echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} +# => DefaultValueIfFooIsMissingOrEmpty # This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0. # Note that it only returns default value and doesn't change variable value. +# Declare an array with 6 elements +array0=(one two three four five six) +# Print first element +echo $array0 # => "one" +# Print first element +echo ${array0[0]} # => "one" +# Print all elements +echo ${array0[@]} # => "one two three four five six" +# Print number of elements +echo ${#array0[@]} # => "6" +# Print number of characters in third element +echo ${#array0[2]} # => "5" +# Print 2 elements starting from forth +echo ${array0[@]:3:2} # => "four five" +# Print all elements. Each of them on new line. +for i in "${array0[@]}"; do + echo "$i" +done + # Brace Expansion { } # Used to generate arbitrary strings -echo {1..10} -echo {a..z} +echo {1..10} # => 1 2 3 4 5 6 7 8 9 10 +echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z # This will output the range from the start value to the end value # Builtin variables: @@ -121,6 +144,7 @@ then else echo "Your name is your username" fi +# True if the value of $Name is not equal to the current user's login username # NOTE: if $Name is empty, bash sees the above condition as: if [ != $USER ] @@ -133,7 +157,11 @@ if [ "" != $USER ] ... # There is also conditional execution echo "Always executed" || echo "Only executed if first command fails" +# => Always executed echo "Always executed" && echo "Only executed if first command does NOT fail" +# => Always executed +# => Only executed if first command does NOT fail + # To use && and || with if statements, you need multiple pairs of square brackets: if [ "$Name" == "Steve" ] && [ "$Age" -eq 15 ] @@ -146,13 +174,20 @@ then echo "This will run if $Name is Daniya OR Zach." fi +# Redefine command 'ping' as alias to send only 5 packets +alias ping='ping -c 5' +# Escape alias and use command with this name instead +\ping 192.168.1.1 +# Print all aliases +alias -p + # Expressions are denoted with the following format: -echo $(( 10 + 5 )) +echo $(( 10 + 5 )) # => 15 # Unlike other programming languages, bash is a shell so it works in the context # of a current directory. You can list files and directories in the current # directory with the ls command: -ls +ls # Lists the files and subdirectories contained in the current directory # These commands have options that control their execution: ls -l # Lists every file and directory on a separate line @@ -169,7 +204,10 @@ cat file.txt # We can also read the file using `cat`: Contents=$(cat file.txt) -echo "START OF FILE\n$Contents\nEND OF FILE" +echo "START OF FILE\n$Contents\nEND OF FILE" # "\n" prints a new line character +# => START OF FILE +# => [contents of file.txt] +# => END OF FILE # Use `cp` to copy files or directories from one place to another. # `cp` creates NEW versions of the sources, @@ -203,6 +241,8 @@ pwd # still in first directory mkdir myNewDir # The `-p` flag causes new intermediate directories to be created as necessary. mkdir -p myNewDir/with/intermediate/directories +# if the intermediate directories didn't already exist, running the above +# command without the `-p` flag would return an error # You can redirect command input and output (stdin, stdout, and stderr). # Read from stdin until ^EOF$ and overwrite hello.py with the lines @@ -217,12 +257,13 @@ for line in sys.stdin: print(line, file=sys.stdout) EOF -# Run hello.py with various stdin, stdout, and stderr redirections: -python hello.py < "input.in" -python hello.py > "output.out" -python hello.py 2> "error.err" -python hello.py > "output-and-error.log" 2>&1 -python hello.py > /dev/null 2>&1 +# Run the hello.py Python script with various stdin, stdout, and +# stderr redirections: +python hello.py < "input.in" # pass input.in as input to the script +python hello.py > "output.out" # redirect output from the script to output.out +python hello.py 2> "error.err" # redirect error output to error.err +python hello.py > "output-and-error.log" 2>&1 # redirect both output and errors to output-and-error.log +python hello.py > /dev/null 2>&1 # redirect all output and errors to the black hole, /dev/null, i.e., no output # The output error will overwrite the file if it exists, # if you want to append instead, use ">>": python hello.py >> "output.out" 2>> "error.err" @@ -269,12 +310,19 @@ for Variable in {1..3} do echo "$Variable" done +# => 1 +# => 2 +# => 3 + # Or write it the "traditional for loop" way: for ((a=1; a <= 3; a++)) do echo $a done +# => 1 +# => 2 +# => 3 # They can also be used to act on files.. # This will run the command 'cat' on file1 and file2 @@ -296,6 +344,7 @@ do echo "loop body here..." break done +# => loop body here... # You can also define functions # Definition: @@ -306,6 +355,11 @@ function foo () echo "This is a function" return 0 } +# Call the function `foo` with two arguments, arg1 and arg2: +foo arg1 arg2 +# => Arguments work just like script arguments: arg1 arg2 +# => And: arg1 arg2... +# => This is a function # or simply bar () @@ -313,6 +367,8 @@ bar () echo "Another way to declare functions!" return 0 } +# Call the function `bar` with no arguments: +bar # => Another way to declare functions! # Calling your function foo "My name is" $Name @@ -320,25 +376,35 @@ foo "My name is" $Name # There are a lot of useful commands you should learn: # prints last 10 lines of file.txt tail -n 10 file.txt + # prints first 10 lines of file.txt head -n 10 file.txt + # sort file.txt's lines sort file.txt + # report or omit repeated lines, with -d it reports them uniq -d file.txt + # prints only the first column before the ',' character cut -d ',' -f 1 file.txt -# replaces every occurrence of 'okay' with 'great' in file.txt, (regex compatible) + +# replaces every occurrence of 'okay' with 'great' in file.txt +# (regex compatible) sed -i 's/okay/great/g' file.txt + # print to stdout all lines of file.txt which match some regex # The example prints lines which begin with "foo" and end in "bar" grep "^foo.*bar$" file.txt + # pass the option "-c" to instead print the number of lines matching the regex grep -c "^foo.*bar$" file.txt + # Other useful options are: grep -r "^foo.*bar$" someDir/ # recursively `grep` grep -n "^foo.*bar$" file.txt # give line numbers grep -rI "^foo.*bar$" someDir/ # recursively `grep`, but ignore binary files + # perform the same initial search, but filter out the lines containing "baz" grep "^foo.*bar$" file.txt | grep -v "baz" @@ -346,8 +412,9 @@ grep "^foo.*bar$" file.txt | grep -v "baz" # and not the regex, use fgrep (or grep -F) fgrep "foobar" file.txt -# trap command allows you to execute a command when a signal is received by your script. -# Here trap command will execute rm if any one of the three listed signals is received. +# The trap command allows you to execute a command whenever your script +# receives a signal. Here, trap will execute `rm` if it receives any of the +# three listed signals. trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM # `sudo` is used to perform commands as the superuser diff --git a/bf.html.markdown b/bf.html.markdown index 9c603303..1e415a4d 100644 --- a/bf.html.markdown +++ b/bf.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: bf.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/bg-bg/perl-bg.html.markdown b/bg-bg/perl-bg.html.markdown index babd2d4d..2ae7a8fd 100644 --- a/bg-bg/perl-bg.html.markdown +++ b/bg-bg/perl-bg.html.markdown @@ -2,7 +2,7 @@ name: perl category: language language: perl -filename: learnperl.pl +filename: learnperl-bg.pl contributors: - ["Korjavin Ivan", "http://github.com/korjavin"] - ["Dan Book", "http://github.com/Grinnz"] diff --git a/c++.html.markdown b/c++.html.markdown index 1461c93e..8d1c7a26 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -8,7 +8,6 @@ contributors: - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] -lang: en --- C++ is a systems programming language that, @@ -474,6 +473,7 @@ int main() { // without a public or protected method for doing so class OwnedDog : public Dog { +public: void setOwner(const std::string& dogsOwner); // Override the behavior of the print function for all OwnedDogs. See @@ -1000,24 +1000,24 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' // Vector (Dynamic array) // Allow us to Define the Array or list of objects at run time -#include<vector> -vector<Data_Type> Vector_name; // used to initialize the vector +#include <vector> +string val; +vector<string> my_vector; // initialize the vector cin >> val; -Vector_name.push_back(val); // will push the value of variable into array - -// To iterate through vector, we have 2 choices: -// Normal looping -for(int i=0; i<Vector_name.size(); i++) -// It will iterate through the vector from index '0' till last index - -// Iterator -vector<Data_Type>::iterator it; // initialize the iterator for vector -for(it=vector_name.begin(); it!=vector_name.end();++it) +my_vector.push_back(val); // will push the value of 'val' into vector ("array") my_vector +my_vector.push_back(val); // will push the value into the vector again (now having two elements) -// For accessing the element of the vector -// Operator [] -var = vector_name[index]; // Will assign value at that index to var +// To iterate through a vector we have 2 choices: +// Either classic looping (iterating through the vector from index 0 to its last index): +for (int i = 0; i < my_vector.size(); i++) { + cout << my_vector[i] << endl; // for accessing a vector's element we can use the operator [] +} +// or using an iterator: +vector<string>::iterator it; // initialize the iterator for vector +for (it = my_vector.begin(); it != my_vector.end(); ++it) { + cout << *it << endl; +} // Set // Sets are containers that store unique elements following a specific order. diff --git a/c.html.markdown b/c.html.markdown index 18503eab..684d330a 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -8,6 +8,7 @@ contributors: - ["Marco Scannadinari", "https://marcoms.github.io"] - ["Zachary Ferguson", "https://github.io/zfergus2"] - ["himanshu", "https://github.com/himanshu81494"] + - ["Joshua Li", "https://github.com/JoshuaRLi"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -16,6 +17,16 @@ C is the lowest-level language most programmers will ever use, but 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: +> +> `-Wall -Wextra -Werror -O2 -std=c99 -pedantic` +> +> For information on what these flags do as well as other flags, consult the man page for your C compiler (e.g. `man 1 gcc`) or just search online. + ```c // Single-line comments start with // - only available in C99 and later. @@ -90,7 +101,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; @@ -302,7 +313,7 @@ int main (int argc, char** argv) // branching with multiple choices: switch() switch (a) { - case 0: // labels need to be integral *constant* expressions + case 0: // labels need to be integral *constant* expressions (such as enums) printf("Hey, 'a' equals 0!\n"); break; // if you don't break, control flow falls over labels case 1: @@ -324,7 +335,7 @@ int main (int argc, char** argv) using "goto" in C */ typedef enum { false, true } bool; - // for C don't have bool as data type :( + // for C don't have bool as data type before C99 :( bool disaster = false; int i, j; for(i=0;i<100;++i) @@ -336,10 +347,10 @@ int main (int argc, char** argv) goto error; } error : - printf("Error occured at i = %d & j = %d.\n", i, j); + printf("Error occurred at i = %d & j = %d.\n", i, j); /* https://ideone.com/GuPhd6 - this will print out "Error occured at i = 52 & j = 99." + this will print out "Error occurred at i = 51 & j = 99." */ /////////////////////////////////////// @@ -438,17 +449,25 @@ 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)); // Note that there is no standard way to get the length of a // dynamically allocated array in C. Because of this, if your arrays are // going to be passed around your program a lot, you need another variable // to keep track of the number of elements (size) of an array. See the // functions section for more info. - int size = 10; - int *my_arr = malloc(sizeof(int) * size); + size_t size = 10; + int *my_arr = calloc(size, sizeof(int)); // Add an element to the array size++; my_arr = realloc(my_arr, sizeof(int) * size); + if (my_arr == NULL) { + //Remember to check for realloc failure! + return + } my_arr[10] = 5; // Dereferencing memory that you haven't allocated gives @@ -546,7 +565,7 @@ array in C. */ // Size must be passed! // Otherwise, this function has no way of knowing how big the array is. -void printIntArray(int *arr, int size) { +void printIntArray(int *arr, size_t size) { int i; for (i = 0; i < size; i++) { printf("arr[%d] is: %d\n", i, arr[i]); @@ -559,7 +578,7 @@ printIntArray(my_arr, size); // will print "arr[0] is: 1" etc */ -// if referring to external variables outside function, must use extern keyword. +// if referring to external variables outside function, you should use the extern keyword. int i = 0; void testFunc() { extern int i; //i here is now using external variable i @@ -656,6 +675,7 @@ typedef void (*my_fnp_type)(char *); // ... // my_fnp_type f; + //Special characters: /* '\a'; // alert (bell) character @@ -738,14 +758,20 @@ as the C file. /* Like c source files macros can be defined in headers and used in files */ /* that include this header file. */ #define EXAMPLE_NAME "Dennis Ritchie" -/* Function macros can also be defined. */ -#define ADD(a, b) (a + b) + +/* Function macros can also be defined. */ +#define ADD(a, b) ((a) + (b)) + +/* Notice the parenthesis surrounding the arguments -- this is important to */ +/* ensure that a and b don't get expanded in an unexpected way (e.g. consider */ +/* MUL(x, y) (x * y); MUL(1 + 2, 3) would expand to (1 + 2 * 3), yielding an */ +/* incorrect result) */ /* Structs and typedefs can be used for consistency between files. */ -typedef struct node +typedef struct Node { int val; - struct node *next; + struct Node *next; } Node; /* So can enumerations. */ diff --git a/ca-es/asciidoc-ca.html.markdown b/ca-es/asciidoc-ca.html.markdown new file mode 100644 index 00000000..a88aba52 --- /dev/null +++ b/ca-es/asciidoc-ca.html.markdown @@ -0,0 +1,134 @@ +--- +language: asciidoc +contributors: + - ["Ryan Mavilia", "http://unoriginality.rocks/"] +translators: + - ["Abel Salgado Romero", "https://twitter.com/abelsromero"] +lang: ca-es +filename: asciidoc-ca.md +--- + +AsciiDoc és un llenguatge de marques similar a Markdown i que pot ser usat per qualsevol ús, des de llibres fins a blogs. +Creat al 2002 per Stuart Rackham, és un llenguatge simple però permet un gran nivell de personalització. + +Capçalera de document + +La capçalera és opcional i no pot contenir línies buides. Ha d'estar separada del contingut per com a mínim una línia en blanc. + +Només títol + +``` += Títol del document + +Primer contingut del document. +``` + +Títol i autor + +``` += Títol del document +Nom Cognom(s) <nom.cognom@learnxinyminutes.com> + +Inici d'aquest document. +``` + +Múltiples autors + +``` += Títol del document +John Doe <john@go.com>; Jane Doe<jane@yo.com>; Black Beard <beardy@pirate.com> + +Inici d'un document amb múltiples autors. +``` + +Línia de versió (requereix línia d'autor) + +``` += Títol del document V1 +Potato Man <chip@crunchy.com> +v1.0, 2016-01-13 + +Aquest article sobre patates fregides serà genial. +``` + +Paràgraf + +``` +No necessites res especial per un paràgraf. + +Insereix una línia buida entre cada paràgraf per separar-los. + +Per inserir un salt de línia, afegeix només un + +i ja ho tens! +``` + +Donant format al text + +``` +_guió baix per cursiva_ +*asteriscs per negreta* +*_combina'ls i veuràs_* +`usa cometes invertides per monospace` +`*combina per negreta monospace*` +``` + +Títols de secció + +``` += Nivell 0 (usa'l només pel títol del document) + +== Nivell 1 <h2> + +=== Nivell 2 <h3> + +==== Nivell 3 <h4> + +===== Nivell 4 <h5> +``` + +Llistes + +Per crear una llista sense ordre usa asteriscs. + +``` +* foo +* bar +* baz +``` + +Per crear una llista numerada usa punts. + +``` +. element 1 +. element 2 +. element 3 +``` + +Pots crear fins a 5 subnivells de llista afegint asteriscs o punts. + +``` +* foo 1 +** foo 2 +*** foo 3 +**** foo 4 +***** foo 5 + +. foo 1 +.. foo 2 +... foo 3 +.... foo 4 +..... foo 5 +``` + +## Referències + +Existeixen dues eines per processar documentació en AsciiDoc: + +1. [AsciiDoc](http://asciidoc.org/): implementació original per Python, disponible a las principals distribucions Linux. Versió estable actualment en mode manteniment. +2. [Asciidoctor](http://asciidoctor.org/): implementació alternativa per Ruby, usable també des de Java i JavaScript. Implementació completa en evolució, té com a objectiu ampliar AsciiDoc amb noves funcionalitats i conversors de sortida. + +Els següents enllaços pertanyen a `Asciidoctor` (documentació en anglès): + +* [Comparació de sintaxi Markdown - AsciiDoc](http://asciidoctor.org/docs/user-manual/#comparison-by-example): comparativa d'elements comuns entre Markdown i AsciiDoc. +* [Primeres pases](http://asciidoctor.org/docs/#get-started-with-asciidoctor): manuals d'instal·lació i inici per convertir documents simples. +* [Manual d'usuari d'Asciidoctor](http://asciidoctor.org/docs/user-manual/): referència completa en un únic document, conté exemples, guies d'eines, etc. diff --git a/ca-es/groovy-ca.html.markdown b/ca-es/groovy-ca.html.markdown index 57674970..f0a9adbe 100644 --- a/ca-es/groovy-ca.html.markdown +++ b/ca-es/groovy-ca.html.markdown @@ -233,10 +233,12 @@ for (i in array) { //Itera per un mapa def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] -x = 0 +x = "" for ( e in map ) { x += e.value + x += " " } +assert x.equals("Roberto Grails Groovy ") /* Operadors diff --git a/chapel.html.markdown b/chapel.html.markdown index 68ce49cd..354cd832 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -2,11 +2,11 @@ language: chapel filename: learnchapel.chpl contributors: - - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] - - ["Ben Harshbarger", "http://github.com/benharsh/"] + - ["Ian J. Bertolacci", "https://www.cs.arizona.edu/~ianbertolacci/"] + - ["Ben Harshbarger", "https://github.com/benharsh/"] --- -You can read all about Chapel at [Cray's official Chapel website](http://chapel.cray.com). +You can read all about Chapel at [Cray's official Chapel website](https://chapel-lang.org). In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. @@ -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 @@ -188,7 +188,7 @@ if 10 < 100 then if -1 < 1 then writeln("Continuing to believe reality"); else - writeln("Send mathematician, something's wrong"); + writeln("Send mathematician, something is wrong"); // You can use parentheses if you prefer. if (10 > 100) { @@ -213,7 +213,7 @@ if a % 3 == 0 { var maximum = if thisInt < thatInt then thatInt else thisInt; // select statements are much like switch statements in other languages. -// However, select statements don't cascade like in C or Java. +// However, select statements do not cascade like in C or Java. var inputOption = "anOption"; select inputOption { when "anOption" do writeln("Chose 'anOption'"); @@ -223,7 +223,7 @@ select inputOption { } otherwise { writeln("Any other Input"); - writeln("the otherwise case doesn't need a do if the body is one line"); + writeln("the otherwise case does not need a do if the body is one line"); } } @@ -242,7 +242,7 @@ do { } while (j <= 10000); writeln(jSum); -// for loops are much like those in python in that they iterate over a +// for loops are much like those in Python in that they iterate over a // range. Ranges (like the 1..10 expression below) are a first-class object // in Chapel, and as such can be stored in variables. for i in 1..10 do write(i, ", "); @@ -282,7 +282,7 @@ var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 var reverse2to10by2 = 2..10 by -2; // 10, 8, 6, 4, 2 var trapRange = 10..1 by -1; // Do not be fooled, this is still an empty range -writeln("Size of range '", trapRange, "' = ", trapRange.length); +writeln("Size of range ", trapRange, " = ", trapRange.length); // Note: range(boundedType= ...) and range(stridable= ...) are only // necessary if we explicitly type the variable. @@ -425,7 +425,7 @@ var thisPlusThat = thisArray + thatArray; writeln(thisPlusThat); // Moving on, arrays and loops can also be expressions, where the loop -// body's expression is the result of each iteration. +// body expression is the result of each iteration. var arrayFromLoop = for i in 1..10 do i; writeln(arrayFromLoop); @@ -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. @@ -1064,14 +1063,14 @@ proc main() { } } -// Heres an example using atomics and a sync variable to create a +// Here's an example using atomics and a sync variable to create a // count-down mutex (also known as a multiplexer). var count: atomic int; // our counter var lock$: sync bool; // the mutex lock count.write(2); // Only let two tasks in at a time. lock$.writeXF(true); // Set lock$ to full (unlocked) - // Note: The value doesnt actually matter, just the state + // Note: The value doesn't actually matter, just the state // (full:unlocked / empty:locked) // Also, writeXF() fills (F) the sync var regardless of its state (X) @@ -1124,16 +1123,16 @@ This tutorial is for people who want to learn the ropes of chapel without having to hear about what fiber mixture the ropes are, or how they were braided, or how the braid configurations differ between one another. It won't teach you how to develop amazingly performant code, and it's not exhaustive. -Refer to the [language specification](http://chapel.cray.com/language.html) and -the [module documentation](http://chapel.cray.com/docs/latest/) for more +Refer to the [language specification](https://chapel-lang.org/docs/latest/language/spec.html) and +the [module documentation](https://chapel-lang.org/docs/latest/) for more details. -Occasionally check back here and on the [Chapel site](http://chapel.cray.com) +Occasionally check back here and on the [Chapel site](https://chapel-lang.org) to see if more topics have been added or more tutorials created. ### What this tutorial is lacking: - * Exposition of the [standard modules](http://chapel.cray.com/docs/latest/modules/modules.html) + * Exposition of the [standard modules](https://chapel-lang.org/docs/latest/modules/standard.html) * Multiple Locales (distributed memory system) * Records * Parallel iterators @@ -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.15.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.15.0.tar.gz` - 2. `cd chapel-1.15.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/clojure-macros.html.markdown b/clojure-macros.html.markdown index 89066faf..3864f676 100644 --- a/clojure-macros.html.markdown +++ b/clojure-macros.html.markdown @@ -131,7 +131,7 @@ You'll want to be familiar with Clojure. Make sure you understand everything in ; However, we'll need to make it a macro if we want it to be run at compile time (defmacro inline-2 [form] - (inline-2-helper form))) + (inline-2-helper form)) (macroexpand '(inline-2 (1 + (3 / 2) - (1 / 2) + 1))) ; -> (+ (- (+ 1 (/ 3 2)) (/ 1 2)) 1) diff --git a/cmake.html.markdown b/cmake.html.markdown index 45cf0585..c705beea 100644 --- a/cmake.html.markdown +++ b/cmake.html.markdown @@ -28,8 +28,8 @@ are used in the usual way. # - cmake .. # - make # -# With those steps, we will follow the best pratice to compile into a subdir -# and the second line will request to CMake to generate a new OS-dependant +# With those steps, we will follow the best practice to compile into a subdir +# and the second line will request to CMake to generate a new OS-dependent # Makefile. Finally, run the native Make command. #------------------------------------------------------------------------------ diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index 9a23bc26..76e7735b 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -4,82 +4,91 @@ language: "Common Lisp" filename: commonlisp.lisp contributors: - ["Paul Nathan", "https://github.com/pnathan"] + - ["Rommel Martinez", "https://ebzzry.io"] --- -ANSI Common Lisp is a general purpose, multi-paradigm programming -language suited for a wide variety of industry applications. It is -frequently referred to as a programmable programming language. +Common Lisp is a general-purpose, multi-paradigm programming language suited for a wide variety of +industry applications. It is frequently referred to as a programmable programming language. -The classic starting point is [Practical Common Lisp and freely available.](http://www.gigamonkeys.com/book/) +The classic starting point is [Practical Common Lisp](http://www.gigamonkeys.com/book/). Another +popular and recent book is [Land of Lisp](http://landoflisp.com/). A new book about best practices, +[Common Lisp Recipes](http://weitz.de/cl-recipes/), was recently published. -Another popular and recent book is -[Land of Lisp](http://landoflisp.com/). +```lisp -```common_lisp - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;----------------------------------------------------------------------------- ;;; 0. Syntax -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;----------------------------------------------------------------------------- -;;; General form. +;;; General form -;; Lisp has two fundamental pieces of syntax: the ATOM and the -;; S-expression. Typically, grouped S-expressions are called `forms`. +;;; CL has two fundamental pieces of syntax: ATOM and S-EXPRESSION. +;;; Typically, grouped S-expressions are called `forms`. -10 ; an atom; it evaluates to itself +10 ; an atom; it evaluates to itself +:thing ; another atom; evaluating to the symbol :thing +t ; another atom, denoting true +(+ 1 2 3 4) ; an s-expression +'(4 :foo t) ; another s-expression -:THING ;Another atom; evaluating to the symbol :thing. -t ; another atom, denoting true. +;;; Comments -(+ 1 2 3 4) ; an s-expression +;;; Single-line comments start with a semicolon; use four for file-level +;;; comments, three for section descriptions, two inside definitions, and one +;;; for single lines. For example, -'(4 :foo t) ;another one +;;;; life.lisp +;;; Foo bar baz, because quu quux. Optimized for maximum krakaboom and umph. +;;; Needed by the function LINULUKO. -;;; Comments +(defun meaning (life) + "Return the computed meaning of LIFE" + (let ((meh "abc")) + ;; Invoke krakaboom + (loop :for x :across meh + :collect x))) ; store values into x, then return it -;; Single line comments start with a semicolon; use two for normal -;; comments, three for section comments, and four for file-level -;; comments. +;;; Block comments, on the other hand, allow for free-form comments. They are +;;; delimited with #| and |# -#| Block comments - can span multiple lines and... +#| This is a block comment which + can span multiple lines and #| they can be nested! |# |# -;;; Environment. -;; A variety of implementations exist; most are -;; standard-conformant. CLISP is a good starting one. +;;; Environment -;; Libraries are managed through Quicklisp.org's Quicklisp system. +;;; A variety of implementations exist; most are standards-conformant. SBCL +;;; is a good starting point. Third party libraries can be easily installed with +;;; Quicklisp -;; Common Lisp is usually developed with a text editor and a REPL -;; (Read Evaluate Print Loop) running at the same time. The REPL -;; allows for interactive exploration of the program as it is "live" -;; in the system. +;;; CL is usually developed with a text editor and a Real Eval Print +;;; Loop (REPL) running at the same time. The REPL allows for interactive +;;; exploration of the program while it is running "live". -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;; 1. Primitive Datatypes and Operators -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;----------------------------------------------------------------------------- +;;; 1. Primitive datatypes and operators +;;;----------------------------------------------------------------------------- ;;; Symbols 'foo ; => FOO Notice that the symbol is upper-cased automatically. -;; Intern manually creates a symbol from a string. +;;; INTERN manually creates a symbol from a string. -(intern "AAAA") ; => AAAA - -(intern "aaa") ; => |aaa| +(intern "AAAA") ; => AAAA +(intern "aaa") ; => |aaa| ;;; Numbers + 9999999999999999999999 ; integers #b111 ; binary => 7 #o111 ; octal => 73 @@ -89,313 +98,362 @@ t ; another atom, denoting true. 1/2 ; ratios #C(1 2) ; complex numbers +;;; Function application are written as (f x y z ...) where f is a function and +;;; x, y, z, ... are the arguments. + +(+ 1 2) ; => 3 + +;;; If you want to create literal data, use QUOTE to prevent it from being +;;; evaluated + +(quote (+ 1 2)) ; => (+ 1 2) +(quote a) ; => A + +;;; The shorthand for QUOTE is ' + +'(+ 1 2) ; => (+ 1 2) +'a ; => A + +;;; Basic arithmetic operations + +(+ 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) + +;;; Booleans + +t ; true; any non-NIL value is true +nil ; false; also, the empty list: () +(not nil) ; => T +(and 0 t) ; => T +(or 0 nil) ; => 0 + +;;; Characters + +#\A ; => #\A +#\λ ; => #\GREEK_SMALL_LETTER_LAMDA +#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA + +;;; Strings are fixed-length arrays of characters -;; Function application is written (f x y z ...) -;; where f is a function and x, y, z, ... are operands -;; If you want to create a literal list of data, use ' to stop it from -;; being evaluated - literally, "quote" the data. -'(+ 1 2) ; => (+ 1 2) -;; You can also call a function manually: -(funcall #'+ 1 2 3) ; => 6 -;; Some arithmetic operations -(+ 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) - - ;;; Booleans -t ; for true (any not-nil value is true) -nil ; for false - and the empty list -(not nil) ; => t -(and 0 t) ; => t -(or 0 nil) ; => 0 - - ;;; Characters -#\A ; => #\A -#\λ ; => #\GREEK_SMALL_LETTER_LAMDA -#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA - -;;; Strings are fixed-length arrays of characters. "Hello, world!" "Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character -;; Strings can be concatenated too! -(concatenate 'string "Hello " "world!") ; => "Hello world!" +;;; Strings can be concatenated + +(concatenate 'string "Hello, " "world!") ; => "Hello, world!" + +;;; A string can be treated like a sequence of characters -;; A string can be treated like a sequence of characters (elt "Apple" 0) ; => #\A -;; format can be used to format strings: -(format nil "~a can be ~a" "strings" "formatted") +;;; FORMAT is used to create formatted output, which ranges from simple string +;;; interpolation to loops and conditionals. The first argument to FORMAT +;;; determines where will the formatted string go. If it is NIL, FORMAT +;;; simply returns the formatted string as a value; if it is T, FORMAT outputs +;;; to the standard output, usually the screen, then it returns NIL. + +(format nil "~A, ~A!" "Hello" "world") ; => "Hello, world!" +(format t "~A, ~A!" "Hello" "world") ; => NIL -;; Printing is pretty easy; ~% is the format specifier for newline. -(format t "Common Lisp is groovy. Dude.~%") +;;;----------------------------------------------------------------------------- +;;; 2. Variables +;;;----------------------------------------------------------------------------- -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 2. Variables -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; You can create a global (dynamically scoped) using defparameter -;; a variable name can use any character except: ()",'`;#|\ +;;; You can create a global (dynamically scoped) variable using DEFVAR and +;;; DEFPARAMETER. The variable name can use any character except: ()",'`;#|\ -;; Dynamically scoped variables should have earmuffs in their name! +;;; The difference between DEFVAR and DEFPARAMETER is that re-evaluating a +;;; DEFVAR expression doesn't change the value of the variable. DEFPARAMETER, +;;; on the other hand, does. + +;;; By convention, dynamically scoped variables have earmuffs in their name. (defparameter *some-var* 5) *some-var* ; => 5 -;; You can also use unicode characters. +;;; You can also use unicode characters. (defparameter *AΛB* nil) +;;; Accessing a previously unbound variable is an undefined behavior, but +;;; possible. Don't do it. + +;;; You can create local bindings with LET. In the following snippet, `me` is +;;; bound to "dance with you" only within the (let ...). LET always returns +;;; the value of the last `form` in the LET form. -;; Accessing a previously unbound variable is an -;; undefined behavior (but possible). Don't do it. +(let ((me "dance with you")) me) ; => "dance with you" -;; Local binding: `me` is bound to "dance with you" only within the -;; (let ...). Let always returns the value of the last `form` in the -;; let form. +;;;-----------------------------------------------------------------------------; +;;; 3. Structs and collections +;;;-----------------------------------------------------------------------------; -(let ((me "dance with you")) - me) -;; => "dance with you" -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 3. Structs and Collections -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Structs -;; Structs (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*) ; => true #| -p signifies "predicate". It's used to - check if *rover* is an instance of dog. |# +*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5) +(dog-p *rover*) ; => T (dog-name *rover*) ; => "rover" -;; Dog-p, make-dog, and dog-name are all created by defstruct! +;;; DOG-P, MAKE-DOG, and DOG-NAME are all automatically created by DEFSTRUCT + ;;; Pairs -;; `cons' constructs pairs, `car' and `cdr' extract the first -;; and second elements -(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB) -(car (cons 'SUBJECT 'VERB)) ; => SUBJECT -(cdr (cons 'SUBJECT 'VERB)) ; => VERB + +;;; CONS constructs pairs. CAR and CDR return the head and tail of a CONS-pair. + +(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB) +(car (cons 'SUBJECT 'VERB)) ; => SUBJECT +(cdr (cons 'SUBJECT 'VERB)) ; => VERB + ;;; Lists -;; Lists are linked-list data structures, made of `cons' pairs and end -;; with a `nil' (or '()) to mark the end of the list -(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3) -;; `list' is a convenience variadic constructor for lists -(list 1 2 3) ; => '(1 2 3) -;; and a quote can also be used for a literal list value -'(1 2 3) ; => '(1 2 3) +;;; Lists are linked-list data structures, made of CONS pairs and end with a +;;; NIL (or '()) to mark the end of the list + +(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3) + +;;; LIST is a convenience variadic constructor for lists + +(list 1 2 3) ; => '(1 2 3) -;; Can still use `cons' to add an item to the beginning of a list -(cons 4 '(1 2 3)) ; => '(4 1 2 3) +;;; When the first argument to CONS is an atom and the second argument is a +;;; list, CONS returns a new CONS-pair with the first argument as the first +;;; item and the second argument as the rest of the CONS-pair -;; Use `append' to - surprisingly - append lists together -(append '(1 2) '(3 4)) ; => '(1 2 3 4) +(cons 4 '(1 2 3)) ; => '(4 1 2 3) -;; Or use concatenate - +;;; Use APPEND to join lists -(concatenate 'list '(1 2) '(3 4)) +(append '(1 2) '(3 4)) ; => '(1 2 3 4) + +;;; Or CONCATENATE + +(concatenate 'list '(1 2) '(3 4)) ; => '(1 2 3 4) + +;;; Lists are a very central type, so there is a wide variety of functionality for +;;; them, a few examples: -;; Lists are a very central type, so there is a wide variety of functionality for -;; them, a few examples: (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 +(every #'evenp '(1 2 3 4)) ; => NIL (some #'oddp '(1 2 3 4)) ; => T (butlast '(subject verb object)) ; => (SUBJECT VERB) ;;; Vectors -;; Vector's literals are fixed-length arrays -#(1 2 3) ; => #(1 2 3) - -;; Use concatenate to add vectors together -(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6) +;;; Vector's literals are fixed-length arrays -;;; Arrays +#(1 2 3) ; => #(1 2 3) -;; Both vectors and strings are special-cases of arrays. +;;; Use CONCATENATE to add vectors together -;; 2D arrays +(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6) -(make-array (list 2 2)) -;; (make-array '(2 2)) works as well. +;;; Arrays -; => #2A((0 0) (0 0)) +;;; Both vectors and strings are special-cases of arrays. -(make-array (list 2 2 2)) +;;; 2D arrays -; => #3A(((0 0) (0 0)) ((0 0) (0 0))) +(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))) -;; Caution- the default initial values are -;; implementation-defined. Here's how to define them: +;;; Caution: the default initial values of MAKE-ARRAY are implementation-defined. +;;; To explicitly specify them: -(make-array '(2) :initial-element 'unset) +(make-array '(2) :initial-element 'unset) ; => #(UNSET UNSET) -; => #(UNSET UNSET) +;;; To access the element at 1, 1, 1: -;; And, to access the element at 1,1,1 - -(aref (make-array (list 2 2 2)) 1 1 1) +(aref (make-array (list 2 2 2)) 1 1 1) ; => 0 -; => 0 ;;; Adjustable vectors -;; Adjustable vectors have the same printed representation -;; as fixed-length vector's literals. +;;; Adjustable vectors have the same printed representation as +;;; fixed-length vector's literals. (defparameter *adjvec* (make-array '(3) :initial-contents '(1 2 3) - :adjustable t :fill-pointer t)) - + :adjustable t :fill-pointer t)) *adjvec* ; => #(1 2 3) -;; Adding new element: -(vector-push-extend 4 *adjvec*) ; => 3 - -*adjvec* ; => #(1 2 3 4) +;;; Adding new elements +(vector-push-extend 4 *adjvec*) ; => 3 +*adjvec* ; => #(1 2 3 4) -;;; Naively, sets are just lists: +;;; Sets, naively, are just lists: -(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) +(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) -;; But you'll want to use a better data structure than a linked list -;; for performant work! +;;; However, you'll need a better data structure than linked lists when working +;;; with larger data sets ;;; Dictionaries are implemented as hash tables. -;; Create a hash table +;;; Create a hash table + (defparameter *m* (make-hash-table)) -;; set a value +;;; Set value + (setf (gethash 'a *m*) 1) -;; Retrieve a value -(gethash 'a *m*) ; => 1, t +;;; Retrieve value + +(gethash 'a *m*) ; => 1, T + +;;; CL expressions have the ability to return multiple values. + +(values 1 2) ; => 1, 2 + +;;; which can be bound with MULTIPLE-VALUE-BIND + +(multiple-value-bind (x y) + (values 1 2) + (list y x)) + +; => '(2 1) -;; Detail - Common Lisp has multiple return values possible. gethash -;; returns t in the second value if anything was found, and nil if -;; not. +;;; GETHASH is an example of a function that returns multiple values. The first +;;; value it return is the value of the key in the hash table; if the key is +;;; not found it returns NIL. -;; Retrieving a non-present value returns nil - (gethash 'd *m*) ;=> nil, nil +;;; The second value determines if that key is indeed present in the hash +;;; table. If a key is not found in the table it returns NIL. This behavior +;;; allows us to check if the value of a key is actually NIL. + +;;; Retrieving a non-present value returns nil + +(gethash 'd *m*) ;=> NIL, NIL + +;;; You can provide a default value for missing keys -;; You can provide a default value for missing keys (gethash 'd *m* :not-found) ; => :NOT-FOUND -;; Let's handle the multiple return values here in code. +;;; Let's handle the multiple return values here in code. -(multiple-value-bind - (a b) +(multiple-value-bind (a b) (gethash 'd *m*) (list a b)) ; => (NIL NIL) -(multiple-value-bind - (a b) +(multiple-value-bind (a b) (gethash 'a *m*) (list a b)) ; => (1 T) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 3. Functions -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Use `lambda' to create anonymous functions. -;; A function always returns the value of its last expression. -;; The exact printable representation of a function will vary... +;;;----------------------------------------------------------------------------- +;;; 3. Functions +;;;----------------------------------------------------------------------------- + +;;; Use LAMBDA to create anonymous functions. Functions always returns the +;;; value of the last expression. The exact printable representation of a +;;; function varies between implementations. (lambda () "Hello World") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}> -;; Use funcall to call lambda functions -(funcall (lambda () "Hello World")) ; => "Hello World" +;;; Use FUNCALL to call anonymous functions + +(funcall (lambda () "Hello World")) ; => "Hello World" +(funcall #'+ 1 2 3) ; => 6 -;; Or Apply +;;; A call to FUNCALL is also implied when the lambda expression is the CAR of +;;; an unquoted list + +((lambda () "Hello World")) ; => "Hello World" +((lambda (val) val) "Hello World") ; => "Hello World" + +;;; FUNCALL is used when the arguments are known beforehand. Otherwise, use APPLY + +(apply #'+ '(1 2 3)) ; => 6 (apply (lambda () "Hello World") nil) ; => "Hello World" -;; De-anonymize the function -(defun hello-world () - "Hello World") +;;; To name a function, use DEFUN + +(defun hello-world () "Hello World") (hello-world) ; => "Hello World" -;; The () in the above is the list of arguments for the function -(defun hello (name) - (format nil "Hello, ~a" name)) +;;; The () in the definition above is the list of arguments +(defun hello (name) (format nil "Hello, ~A" name)) (hello "Steve") ; => "Hello, Steve" -;; Functions can have optional arguments; they default to nil +;;; Functions can have optional arguments; they default to NIL (defun hello (name &optional from) - (if from - (format t "Hello, ~a, from ~a" name from) - (format t "Hello, ~a" name))) - - (hello "Jim" "Alpacas") ;; => Hello, Jim, from Alpacas + (if from + (format t "Hello, ~A, from ~A" name from) + (format t "Hello, ~A" name))) -;; And the defaults can be set... -(defun hello (name &optional (from "The world")) - (format t "Hello, ~a, from ~a" name from)) +(hello "Jim" "Alpacas") ; => Hello, Jim, from Alpacas -(hello "Steve") -; => Hello, Steve, from The world +;;; The default values can also be specified -(hello "Steve" "the alpacas") -; => Hello, Steve, from the alpacas +(defun hello (name &optional (from "The world")) + (format nil "Hello, ~A, from ~A" name from)) +(hello "Steve") ; => Hello, Steve, from The world +(hello "Steve" "the alpacas") ; => Hello, Steve, from the alpacas -;; And of course, keywords are allowed as well... usually more -;; flexible than &optional. +;;; Functions also have keyword arguments to allow non-positional arguments (defun generalized-greeter (name &key (from "the world") (honorific "Mx")) - (format t "Hello, ~a ~a, from ~a" honorific name from)) + (format t "Hello, ~A ~A, from ~A" honorific name from)) -(generalized-greeter "Jim") ; => Hello, Mx Jim, from the world +(generalized-greeter "Jim") +; => Hello, Mx Jim, from the world (generalized-greeter "Jim" :from "the alpacas you met last summer" :honorific "Mr") ; => Hello, Mr Jim, from the alpacas you met last summer -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 4. Equality -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Common Lisp has a sophisticated equality system. A couple are covered here. +;;;----------------------------------------------------------------------------- +;;; 4. Equality +;;;----------------------------------------------------------------------------- + +;;; CL has a sophisticated equality system. Some are covered here. -;; for numbers use `=' -(= 3 3.0) ; => t -(= 2 1) ; => nil +;;; For numbers, use `=' +(= 3 3.0) ; => T +(= 2 1) ; => NIL -;; for object identity (approximately) use `eql` -(eql 3 3) ; => t -(eql 3 3.0) ; => nil -(eql (list 3) (list 3)) ; => nil +;;; For object identity (approximately) use EQL +(eql 3 3) ; => T +(eql 3 3.0) ; => NIL +(eql (list 3) (list 3)) ; => NIL -;; for lists, strings, and bit-vectors use `equal' -(equal (list 'a 'b) (list 'a 'b)) ; => t -(equal (list 'a 'b) (list 'b 'a)) ; => nil +;;; for lists, strings, and bit-vectors use EQUAL +(equal (list 'a 'b) (list 'a 'b)) ; => T +(equal (list 'a 'b) (list 'b 'a)) ; => NIL -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 5. Control Flow -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;;----------------------------------------------------------------------------- +;;; 5. Control Flow +;;;----------------------------------------------------------------------------- ;;; Conditionals @@ -404,71 +462,75 @@ nil ; for false - and the empty list "this is false") ; else expression ; => "this is true" -;; In conditionals, all non-nil values are treated as true +;;; In conditionals, all non-NIL values are treated as true + (member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO) (if (member 'Groucho '(Harpo Groucho Zeppo)) 'yep 'nope) ; => 'YEP -;; `cond' chains a series of tests to select a result +;;; COND chains a series of tests to select a result (cond ((> 2 2) (error "wrong!")) ((< 2 2) (error "wrong again!")) (t 'ok)) ; => 'OK -;; Typecase switches on the type of the value +;;; TYPECASE switches on the type of the value (typecase 1 (string :string) (integer :int)) - ; => :int -;;; Iteration -;; Of course recursion is supported: +;;; Looping -(defun walker (n) - (if (zerop n) - :walked - (walker (- n 1)))) +;;; Recursion -(walker 5) ; => :walked +(defun fact (n) + (if (< n 2) + 1 + (* n (fact(- n 1))))) -;; Most of the time, we use DOLIST or LOOP +(fact 5) ; => 120 +;;; Iteration -(dolist (i '(1 2 3 4)) - (format t "~a" i)) +(defun fact (n) + (loop :for result = 1 :then (* result i) + :for i :from 2 :to n + :finally (return result))) -; => 1234 +(fact 5) ; => 120 -(loop for i from 0 below 10 - collect i) +(loop :for x :across "abc" :collect x) +; => (#\a #\b #\c #\d) -; => (0 1 2 3 4 5 6 7 8 9) +(dolist (i '(1 2 3 4)) + (format t "~A" i)) +; => 1234 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 6. Mutation -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;----------------------------------------------------------------------------- +;;; 6. Mutation +;;;----------------------------------------------------------------------------- -;; Use `setf' to assign a new value to an existing variable. This was -;; demonstrated earlier in the hash table example. +;;; Use SETF to assign a new value to an existing variable. This was +;;; demonstrated earlier in the hash table example. (let ((variable 10)) (setf variable 2)) - ; => 2 +; => 2 +;;; Good Lisp style is to minimize the use of destructive functions and to avoid +;;; mutation when reasonable. -;; Good Lisp style is to minimize destructive functions and to avoid -;; mutation when reasonable. -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 7. Classes and Objects -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;----------------------------------------------------------------------------- +;;; 7. Classes and objects +;;;----------------------------------------------------------------------------- -;; No more Animal classes, let's have Human-Powered Mechanical -;; Conveyances. +;;; No more animal classes. Let's have Human-Powered Mechanical +;;; Conveyances. (defclass human-powered-conveyance () ((velocity @@ -479,14 +541,16 @@ nil ; for false - and the empty list :initarg :average-efficiency)) (:documentation "A human powered conveyance")) -;; defclass, followed by name, followed by the superclass list, -;; followed by slot list, followed by optional qualities such as -;; :documentation. +;;; The arguments to DEFCLASS, in order are: +;;; 1. class name +;;; 2. superclass list +;;; 3. slot list +;;; 4. optional specifiers -;; When no superclass list is set, the empty list defaults to the -;; standard-object class. This *can* be changed, but not until you -;; know what you're doing. Look up the Art of the Metaobject Protocol -;; for more information. +;;; When no superclass list is set, the empty list defaults to the +;;; standard-object class. This *can* be changed, but not until you +;;; know what you're doing. Look up the Art of the Metaobject Protocol +;;; for more information. (defclass bicycle (human-powered-conveyance) ((wheel-size @@ -500,7 +564,7 @@ nil ; for false - and the empty list (defclass recumbent (bicycle) ((chain-type :accessor chain-type - :initarg :chain-type))) + :initarg :chain-type))) (defclass unicycle (human-powered-conveyance) nil) @@ -509,8 +573,7 @@ nil ; for false - and the empty list :accessor number-of-rowers :initarg :number-of-rowers))) - -;; Calling DESCRIBE on the human-powered-conveyance class in the REPL gives: +;;; Calling DESCRIBE on the HUMAN-POWERED-CONVEYANCE class in the REPL gives: (describe 'human-powered-conveyance) @@ -532,47 +595,42 @@ nil ; for false - and the empty list ; Readers: AVERAGE-EFFICIENCY ; Writers: (SETF AVERAGE-EFFICIENCY) -;; Note the reflective behavior available to you! Common Lisp is -;; designed to be an interactive system +;;; Note the reflective behavior available. CL was designed to be an +;;; interactive system -;; To define a method, let's find out what our circumference of the -;; bike wheel turns out to be using the equation: C = d * pi +;;; To define a method, let's find out what our circumference of the +;;; bike wheel turns out to be using the equation: C = d * pi (defmethod circumference ((object bicycle)) (* pi (wheel-size object))) -;; pi is defined in Lisp already for us! +;;; PI is defined as a built-in in CL -;; Let's suppose we find out that the efficiency value of the number -;; of rowers in a canoe is roughly logarithmic. This should probably be set -;; in the constructor/initializer. +;;; Let's suppose we find out that the efficiency value of the number +;;; of rowers in a canoe is roughly logarithmic. This should probably be set +;;; in the constructor/initializer. -;; Here's how to initialize your instance after Common Lisp gets done -;; constructing it: +;;; To initialize your instance after CL gets done constructing it: (defmethod initialize-instance :after ((object canoe) &rest args) (setf (average-efficiency object) (log (1+ (number-of-rowers object))))) -;; Then to construct an instance and check the average efficiency... +;;; Then to construct an instance and check the average efficiency... (average-efficiency (make-instance 'canoe :number-of-rowers 15)) ; => 2.7725887 +;;;----------------------------------------------------------------------------- +;;; 8. Macros +;;;----------------------------------------------------------------------------- - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 8. Macros -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Macros let you extend the syntax of the language - -;; Common Lisp doesn't come with a WHILE loop- let's add one. -;; If we obey our assembler instincts, we wind up with: +;;; Macros let you extend the syntax of the language. CL doesn't come +;;; with a WHILE loop, however, it's trivial to write one. If we obey our +;;; assembler instincts, we wind up with: (defmacro while (condition &body body) "While `condition` is true, `body` is executed. - `condition` is tested prior to each execution of `body`" (let ((block-name (gensym)) (done (gensym))) `(tagbody @@ -584,47 +642,47 @@ nil ; for false - and the empty list (go ,block-name) ,done))) -;; Let's look at the high-level version of this: - +;;; Let's look at the high-level version of this: (defmacro while (condition &body body) "While `condition` is true, `body` is executed. - `condition` is tested prior to each execution of `body`" `(loop while ,condition do (progn ,@body))) -;; However, with a modern compiler, this is not required; the LOOP -;; form compiles equally well and is easier to read. +;;; However, with a modern compiler, this is not required; the LOOP form +;;; compiles equally well and is easier to read. -;; Note that ``` is used, as well as `,` and `@`. ``` is a quote-type operator -;; known as quasiquote; it allows the use of `,` . `,` allows "unquoting" -;; variables. @ interpolates lists. +;;; Note that ``` is used, as well as `,` and `@`. ``` is a quote-type operator +;;; known as quasiquote; it allows the use of `,` . `,` allows "unquoting" +;;; variables. @ interpolates lists. -;; Gensym creates a unique symbol guaranteed to not exist elsewhere in -;; the system. This is because macros are expanded at compile time and -;; variables declared in the macro can collide with variables used in -;; regular code. +;;; GENSYM creates a unique symbol guaranteed to not exist elsewhere in +;;; the system. This is because macros are expanded at compile time and +;;; variables declared in the macro can collide with variables used in +;;; regular code. -;; See Practical Common Lisp for more information on macros. +;;; See Practical Common Lisp and On Lisp for more information on macros. ``` -## Further Reading +## Further reading + +- [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) -* [Keep moving on to the Practical Common Lisp book.](http://www.gigamonkeys.com/book/) -* [A Gentle Introduction to...](https://www.cs.cmu.edu/~dst/LispBook/book.pdf) +## Extra information -## Extra Info +- [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/) -* [CLiki](http://www.cliki.net/) -* [common-lisp.net](https://common-lisp.net/) -* [Awesome Common Lisp](https://github.com/CodyReichert/awesome-cl) -## Credits. +## Credits Lots of thanks to the Scheme people for rolling up a great starting point which could be easily moved to Common Lisp. diff --git a/crystal.html.markdown b/crystal.html.markdown index 1449ff81..8210b443 100644 --- a/crystal.html.markdown +++ b/crystal.html.markdown @@ -215,7 +215,7 @@ Range.new(1, 10).class #=> Range(Int32, Int32) # possibly different types. {1, "hello", 'x'}.class #=> Tuple(Int32, String, Char) -# Acces tuple's value by its index +# Access tuple's value by its index tuple = {:key1, :key2} tuple[1] #=> :key2 tuple[2] #=> syntax error : Index out of bound @@ -301,7 +301,6 @@ end (1..3).each do |index| puts "Index: #{index}" end -# Index: 0 # Index: 1 # Index: 2 # Index: 3 @@ -422,7 +421,7 @@ 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 propery method as follows property :name # Getter/setter methods can also be created individually like this diff --git a/cs-cz/elm.html.markdown b/cs-cz/elm.html.markdown index f19f9e8b..42ec89e5 100644 --- a/cs-cz/elm.html.markdown +++ b/cs-cz/elm.html.markdown @@ -75,8 +75,8 @@ List.head [] -- Nothing -- K získání hodnot z dvojice použijte funkce first a second. -- (Toto je pouze zkratka. Brzy si ukážeme, jak na to "správně".) -fst ("elm", 42) -- "elm" -snd ("elm", 42) -- 42 +Tuple.first ("elm", 42) -- "elm" +Tuple.second ("elm", 42) -- 42 -- Prázná n-tice, neboli "unit", se občas používá jako zástupný symbol. -- Je to jediná hodnota svého typu, který se také nazývá "Unit". 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/csharp.html.markdown b/csharp.html.markdown index cca99fb0..f27adf18 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -132,6 +132,12 @@ namespace Learning.CSharp DateTime fooDate = DateTime.Now; Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); + // Verbatim String + // You can use the @ symbol before a string literal to escape all characters in the string + string path = "C:\\Users\\User\\Desktop"; + string verbatimPath = @"C:\Users\User\Desktop"; + Console.WriteLine(path == verbatimPath); // => true + // You can split a string over two lines with the @ symbol. To escape " use "" string bazString = @"Here's some stuff on a new line! ""Wow!"", the masses cried"; @@ -593,7 +599,7 @@ on a new line! ""Wow!"", the masses cried"; Console.WriteLine(bikeSummary.Name); // ASPARALLEL - // And this is where things get wicked - combines linq and parallel operations + // And this is where things get wicked - combine linq and parallel operations var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name); // this will happen in parallel! Threads will automagically be spun up and the // results divvied amongst them! Amazing for large datasets when you have lots of @@ -613,7 +619,7 @@ on a new line! ""Wow!"", the masses cried"; .ThenBy(b => b.Name) .Select(b => b.Name); // still no query run - // Now the query runs, but opens a reader, so only populates are you iterate through + // Now the query runs, but opens a reader, so only populates as you iterate through foreach (string bike in query) Console.WriteLine(result); @@ -634,6 +640,54 @@ on a new line! ""Wow!"", the masses cried"; } } + + // DELEGATES AND EVENTS + public class DelegateTest + { + public static int count = 0; + public static int Increment() + { + // increment count then return it + return ++count; + } + + // A delegate is a reference to a method + // To reference the Increment method, + // first declare a delegate with the same signature + // ie. takes no arguments and returns an int + public delegate int IncrementDelegate(); + + // An event can also be used to trigger delegates + // Create an event with the delegate type + public static event IncrementDelegate MyEvent; + + static void Main(string[] args) + { + // Refer to the Increment method by instantiating the delegate + // and passing the method itself in as an argument + IncrementDelegate inc = new IncrementDelegate(Increment); + Console.WriteLine(inc()); // => 1 + + // Delegates can be composed with the + operator + IncrementDelegate composedInc = inc; + composedInc += inc; + composedInc += inc; + + // composedInc will run Increment 3 times + Console.WriteLine(composedInc()); // => 4 + + + // Subscribe to the event with the delegate + MyEvent += new IncrementDelegate(Increment); + MyEvent += new IncrementDelegate(Increment); + + // Trigger the event + // ie. run all delegates subscribed to this event + Console.WriteLine(MyEvent()); // => 6 + } + } + + // Class Declaration Syntax: // <public/private/protected/internal> class <class name>{ // //data fields, constructors, functions all inside. @@ -711,7 +765,7 @@ on a new line! ""Wow!"", the masses cried"; // Before .NET 4: (aBike.Accessories & Bicycle.BikeAccessories.Bell) == Bicycle.BikeAccessories.Bell public BikeAccessories Accessories { get; set; } - // Static members belong to the type itself rather then specific object. + // Static members belong to the type itself rather than specific object. // You can access them without a reference to any object: // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); public static int BicyclesCreated { get; set; } @@ -949,6 +1003,7 @@ on a new line! ""Wow!"", the masses cried"; // String interpolation by prefixing the string with $ // and wrapping the expression you want to interpolate with { braces } + // You can also combine both interpolated and verbatim strings with $@ public class Rectangle { public int Length { get; set; } @@ -961,6 +1016,9 @@ on a new line! ""Wow!"", the masses cried"; { Rectangle rect = new Rectangle { Length = 5, Width = 3 }; Console.WriteLine($"The length is {rect.Length} and the width is {rect.Width}"); + + string username = "User"; + Console.WriteLine($@"C:\Users\{username}\Desktop"); } } @@ -1095,6 +1153,147 @@ namespace Learning.More.CSharp } } } + +//New C# 7 Feature +//Install Microsoft.Net.Compilers Latest from Nuget +//Install System.ValueTuple Latest from Nuget +using System; +namespace Csharp7 +{ + // TUPLES, DECONSTRUCTION AND DISCARDS + class TuplesTest + { + public (string, string) GetName() + { + // Fields in tuples are by default named Item1, Item2... + var names1 = ("Peter", "Parker"); + Console.WriteLine(names1.Item2); // => Parker + + // Fields can instead be explicitly named + // Type 1 Declaration + (string FirstName, string LastName) names2 = ("Peter", "Parker"); + + // Type 2 Declaration + var names3 = (First:"Peter", Last:"Parker"); + + Console.WriteLine(names2.FirstName); // => Peter + Console.WriteLine(names3.Last); // => Parker + + return names3; + } + + public string GetLastName() { + var fullName = GetName(); + + // Tuples can be deconstructed + (string firstName, string lastName) = fullName; + + // Fields in a deconstructed tuple can be discarded by using _ + var (_, last) = fullName; + return last; + } + + // Any type can be deconstructed in the same way by + // specifying a Deconstruct method + public int randomNumber = 4; + public int anotherRandomNumber = 10; + + public void Deconstruct(out int randomNumber, out int anotherRandomNumber) + { + randomNumber = this.randomNumber; + anotherRandomNumber = this.anotherRandomNumber; + } + + static void Main(string[] args) + { + var tt = new TuplesTest(); + (int num1, int num2) = tt; + Console.WriteLine($"num1: {num1}, num2: {num2}"); // => num1: 4, num2: 10 + + Console.WriteLine(tt.GetLastName()); + } + } + + // PATTERN MATCHING + class PatternMatchingTest + { + public static (string, int)? CreateLogMessage(object data) + { + switch(data) + { + // Additional filtering using when + case System.Net.Http.HttpRequestException h when h.Message.Contains("404"): + return (h.Message, 404); + case System.Net.Http.HttpRequestException h when h.Message.Contains("400"): + return (h.Message, 400); + case Exception e: + return (e.Message, 500); + case string s: + return (s, s.Contains("Error") ? 500 : 200); + case null: + return null; + default: + return (data.ToString(), 500); + } + } + } + + // REFERENCE LOCALS + // Allow you to return a reference to an object instead of just its value + class RefLocalsTest + { + // note ref in return + public static ref string FindItem(string[] arr, string el) + { + for(int i=0; i<arr.Length; i++) + { + if(arr[i] == el) { + // return the reference + return ref arr[i]; + } + } + throw new Exception("Item not found"); + } + + public static void SomeMethod() + { + string[] arr = {"this", "is", "an", "array"}; + + // note refs everywhere + ref string item = ref FindItem(arr, "array"); + item = "apple"; + Console.WriteLine(arr[3]); // => apple + } + } + + // LOCAL FUNCTIONS + class LocalFunctionTest + { + private static int _id = 0; + public int id; + public LocalFunctionTest() + { + id = generateId(); + + // This local function can only be accessed in this scope + int generateId() + { + return _id++; + } + } + + public static void AnotherMethod() + { + var lf1 = new LocalFunctionTest(); + var lf2 = new LocalFunctionTest(); + Console.WriteLine($"{lf1.id}, {lf2.id}"); // => 0, 1 + + int id = generateId(); + // error CS0103: The name 'generateId' does not exist in the current context + } + } +} + ``` ## Topics Not Covered 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 44db26ae..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)``` -It's a *node* refered by the variable **n**, reusable in the query. It begins with lowercase and uses camelCase. +`(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]``` -The same *relationship*, refered by the variable **k**, reusable in the query, but it's not necessary. +`[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. @@ -244,6 +244,6 @@ Special hints --- - There is just single-line comments in Cypher, with double-slash : // Comments -- You can execute a Cypher script stored in a **.cql** file directly in Neo4j (it's an import). However, you can't have multiple statements in this file (separed by **;**). +- You can execute a Cypher script stored in a **.cql** file directly in Neo4j (it's an import). However, you can't have multiple statements in this file (separated by **;**). - Use the Neo4j shell to write Cypher, it's really awesome. - The Cypher will be the standard query language for all graph databases (known as **OpenCypher**). diff --git a/d.html.markdown b/d.html.markdown index c682cfb5..d2a57cae 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -3,7 +3,7 @@ language: D filename: learnd.d contributors: - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"] -lang: en + --- ```d diff --git a/dart.html.markdown b/dart.html.markdown index 5027dc3e..76857306 100644 --- a/dart.html.markdown +++ b/dart.html.markdown @@ -500,8 +500,8 @@ main() { Dart has a comprehensive web-site. It covers API reference, tutorials, articles and more, including a useful Try Dart online. -http://www.dartlang.org/ -http://try.dartlang.org/ +[https://www.dartlang.org](https://www.dartlang.org) +[https://try.dartlang.org](https://try.dartlang.org) diff --git a/de-de/LOLCODE-de.html.markdown b/de-de/LOLCODE-de.html.markdown new file mode 100644 index 00000000..57eb0ff8 --- /dev/null +++ b/de-de/LOLCODE-de.html.markdown @@ -0,0 +1,188 @@ +--- +language: LOLCODE +filename: learnLOLCODE-de.lol +contributors: + - ["abactel", "https://github.com/abactel"] +translators: + - ["Henrik Jürges", "http://github.com/santifa"] +lang: de-de +--- + +LOLCODE ist eine esoterische Programmiersprache die die Sprache der [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257) nachahmt. + +``` +BTW Das ist ein Kommentar +BTW Das Programm muss mit `HAI <language version>` beginnen und mit `KTHXBYE` enden. + +HAI 1.3 +CAN HAS STDIO? BTW Standard Header importieren + +OBTW + ========================================================================== + ============================== Grundlegendes ============================= + ========================================================================== +TLDR + +BTW Texte anzeigen: +VISIBLE "HELLO WORLD" + +BTW Variablen deklarieren: +I HAS A MESSAGE ITZ "CATZ ARE GOOD" +VISIBLE MESSAGE + +OBTW + Variablen sind dynamisch typisiert und der Typ muss nicht explizit + angegeben werden. Die möglichen Typen sind: +TLDR + +I HAS A STRING ITZ "DOGZ ARE GOOOD" BTW Typ ist YARN +I HAS A INTEGER ITZ 42 BTW Typ ist NUMBR +I HAS A FLOAT ITZ 3.1415 BTW Typ ist NUMBAR +I HAS A BOOLEAN ITZ WIN BTW Typ ist TROOF +I HAS A UNTYPED BTW Typ ist NOOB + +BTW Eingaben von Nutzern: +I HAS A AGE +GIMMEH AGE +BTW Die Variable wird als YARN gespeichert und kann in eine +BTW NUMBR konvertiert werden: +AGE IS NOW A NUMBR + +OBTW + ========================================================================== + ================================== MATHE ================================= + ========================================================================== +TLDR + +BTW LOLCODE benutzt polnische Notation für Mathe. + +BTW grundlegende mathematische Notationen: + +SUM OF 21 AN 33 BTW 21 + 33 +DIFF OF 90 AN 10 BTW 90 - 10 +PRODUKT OF 12 AN 13 BTW 12 * 13 +QUOSHUNT OF 32 AN 43 BTW 32 / 43 +MOD OF 43 AN 64 BTW 43 modulo 64 +BIGGR OF 23 AN 53 BTW max(23, 53) +SMALLR OF 53 AN 45 BTW min(53, 45) + +BTW binäre Notation: + +BOTH OF WIN AN WIN BTW und: WIN if x=WIN, y=WIN +EITHER OF FAIL AN WIN BTW oder: FAIL if x=FAIL, y=FAIL +WON OF WIN AN FAIL BTW exklusives oder: FAIL if x=y +NOT FAIL BTW unäre Negation: WIN if x=FAIL +ALL OF WIN AN WIN MKAY BTW beliebige Stelligkeit bei AND +ANY OF WIN AN FAIL MKAY BTW beliebige Stelligkeit bei OR + +BTW Vergleiche: + +BOTH SAEM "CAT" AN "DOG" BTW WIN wenn x == y +DIFFRINT 732 AN 184 BTW WIN wenn x != y +BOTH SAEM 12 AN BIGGR OF 12 AN 4 BTW x >= y +BOTH SAEM 43 AN SMALLR OF 43 AN 56 BTW x <= y +DIFFRINT 64 AN SMALLR OF 64 AN 2 BTW x > y +DIFFRINT 75 AN BIGGR OF 75 AN 643 BTW x < y + +OBTW + ========================================================================== + ============================= Flusskontrolle ============================= + ========================================================================== +TLDR + +BTW If/then Statement: +I HAS A ANIMAL +GIMMEH ANIMAL +BOTH SAEM ANIMAL AN "CAT", O RLY? + YA RLY + VISIBLE "YOU HAV A CAT" + MEBBE BOTH SAEM ANIMAL AN "MAUS" + VISIBLE "NOM NOM NOM. I EATED IT." + NO WAI + VISIBLE "AHHH IS A WOOF WOOF" +OIC + +BTW Case Statement: +I HAS A COLOR +GIMMEH COLOR +COLOR, WTF? + OMG "R" + VISIBLE "RED FISH" + GTFO + OMG "Y" + VISIBLE "YELLOW FISH" + BTW Weil hier kein `GTFO` ist wird auch das nächste Statement überprüft + OMG "G" + OMG "B" + VISIBLE "FISH HAS A FLAVOR" + GTFO + OMGWTF + VISIBLE "FISH IS TRANSPARENT OHNO WAT" +OIC + +BTW For Schleife: +I HAS A TEMPERATURE +GIMMEH TEMPERATURE +TEMPERATURE IS NOW A NUMBR +IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN TEMPERATURE + VISIBLE ITERATOR +IM OUTTA YR LOOP + +BTW While Schleife: +IM IN YR LOOP NERFIN YR ITERATOR WILE DIFFRINT ITERATOR AN -10 + VISIBLE ITERATOR +IM OUTTA YR LOOP + +OBTW + ========================================================================= + ================================ Strings ================================ + ========================================================================= +TLDR + +BTW Zeilenumbrüche: +VISIBLE "FIRST LINE :) SECOND LINE" + +BTW Tabulatoren: +VISIBLE ":>SPACES ARE SUPERIOR" + +BTW Bell (macht beep): +VISIBLE "NXT CUSTOMER PLS :o" + +BTW Anführungszeichen in Strings: +VISIBLE "HE SAID :"I LIKE CAKE:"" + +BTW Doppelpunkte in Strings : +VISIBLE "WHERE I LIVE:: CYBERSPACE" + +OBTW + ========================================================================= + =============================== Funktionen ============================== + ========================================================================= +TLDR + +BTW Definieren einer neuen Funktion: +HOW IZ I SELECTMOVE YR MOVE BTW `MOVE` ist ein Argument + BOTH SAEM MOVE AN "ROCK", O RLY? + YA RLY + VISIBLE "YOU HAV A ROCK" + NO WAI + VISIBLE "OH NO IS A SNIP-SNIP" + OIC + GTFO BTW Gibt NOOB zurück +IF U SAY SO + +BTW Eine Funktion deklarieren und einen Wert zurückgeben: +HOW IZ I IZYELLOW + FOUND YR "YELLOW" +IF U SAY SO + +BTW Eine Funktion aufrufen: +I IZ IZYELLOW MKAY + +KTHXBYE +``` + +## Weiterführende Informationen: + +- [LCI compiler](https://github.com/justinmeza/lci) +- [Official spec](https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md) diff --git a/de-de/asciidoc-de.html.markdown b/de-de/asciidoc-de.html.markdown index 1a8cdd5a..60f8fa61 100644 --- a/de-de/asciidoc-de.html.markdown +++ b/de-de/asciidoc-de.html.markdown @@ -84,10 +84,6 @@ Abteilungstitel ===== Level 4 <h5> -====== Level 5 <h6> - -======= Level 6 <h7> - ``` Listen 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/dynamic-programming-de.html.markdown b/de-de/dynamic-programming-de.html.markdown new file mode 100644 index 00000000..801d2514 --- /dev/null +++ b/de-de/dynamic-programming-de.html.markdown @@ -0,0 +1,77 @@ +--- +category: Algorithms & Data Structures +name: Dynamic Programming +contributors: + - ["Akashdeep Goel", "http://github.com/akashdeepgoel"] +translators: + - ["Henrik Jürges", "http://github.com/santifa"] +lang: de-de +--- + +# Dynamische Programmierung + +## Einführung +Dynamische Programmierung ist eine leistungsfähige Technik, die zur Lösung +einer bestimmten Klasse von Problemen verwendet wird. +Die Idee ist sehr einfach, wenn Sie ein Problem mit der gegebenen Eingabe +gelöst haben, dann speichern Sie das Ergebnis für die spätere Referenz, um zu +vermeiden, das gleiche Problem noch einmal zu lösen. + +Denken Sie immer daran! +"Diejenigen, die sich nicht an die Vergangenheit erinnern können, +sind dazu verdammt, sie zu wiederholen." + +## Wege zur Lösung solcher Probleme + +1. *Top-Down*: Lösen Sie das gegebene Problem, indem Sie es aufteilen. +Wenn Sie sehen, dass das Problem bereits gelöst ist, geben Sie einfach die +gespeicherte Antwort zurück. Wenn es nicht gelöst wurde, lösen Sie es und +speichern Sie die Antwort. Dieser Ansatz ist leicht zu verfolgen und sehr +intuitiv. Er wird als Memoization bezeichnet. + +2. *Bottom-Up*: Analysieren Sie das Problem und beobachten Sie, in welcher +Reihenfolge die Teilprobleme gelöst werden können. Beginnen Sie mit der +Lösung vom trivialen Teilproblem bis zum gegebenen Problem. Dabei wird +sichergestellt, dass die Teilprobleme vor der Problemlösung gelöst werden. +Dies wird als Dynamische Programmierung bezeichnet. + +## Ein Beispiel für Dynamische Programmierung + +Das Problem mit der längsten ansteigenden Subsequenz besteht darin, +die längste ansteigende Subsequenz einer gegebenen Sequenz zu finden. +Gegeben die Sequenz `S= {a1, a2, a3, a3, a4,..............., an-1, an }`, +müssen wir die größte Teilmenge finden, so daß für alle `j` und `i`, `j<i` +in der Teilmenge `aj<ai` gilt. +Zuerst müssen wir bei jedem Index i den Wert der längsten Subsequenzen (LSi) +finden, wobei das letzte Element der Sequenz ai ist. Dann wäre die größte LSi +die längste Subsequenz in der gegebenen Sequenz. Am Anfang wird der LSi mit +eins belegt, da ai ein Element der Sequenz (Letztes Element) ist. +Dann ist für alle `j` mit `j<i` und `aj<ai`, so dass wir den größten LSj finden +und zum LSi hinzufügen. Der Algorithmus hat eine Laufzeit von *O(n2)*. + +Pseudocode zur Bestimmung der Länge der am längsten ansteigenden Subsequenz: +Die Komplexität des Algorithmus könnte durch eine bessere Datenstruktur anstelle +von Arrays reduziert werden. Das Speichern von Vorgänger-Array's und Variablen +wie `largest_sequences_so_far` und dessen Index würde eine Menge Zeit sparen. + +Ein ähnliches Konzept könnte auch bei der Suche nach dem längsten Weg +in gerichteten azyklischen Graphen angewandt werden. +```python +for i=0 to n-1 + LS[i]=1 + for j=0 to i-1 + if (a[i] > a[j] and LS[i]<LS[j]) + LS[i] = LS[j]+1 +for i=0 to n-1 + if (largest < LS[i]) +``` + +### Einige bekannte DP Probleme + +- Floyd Warshall Algorithm - [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) +- Integer Knapsack Problem - [Tutorial and C Program source code](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem) +- Longest Common Subsequence - [Tutorial and C Program source code](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence) + +## Online Ressourcen + +* [codechef](https://www.codechef.com/wiki/tutorial-dynamic-programming) diff --git a/de-de/edn-de.html.markdown b/de-de/edn-de.html.markdown new file mode 100644 index 00000000..2434d1bd --- /dev/null +++ b/de-de/edn-de.html.markdown @@ -0,0 +1,112 @@ +--- +language: edn +filename: learnedn-de.edn +contributors: + - ["Jason Yeo", "https://github.com/jsyeo"] + - ["Jonathan D Johnston", "https://github.com/jdjohnston"] +translators: + - ["Dennis Keller", "https://github.com/denniskeller"] +lang: de-de +--- + +Extensible Data Notation (EDN) ist ein Format für serialisierte Daten. + +EDN ist ein Subset der von Clojure verwendeten Syntax. Das Lesen von Daten, die durch EDN definiert werden, ist +sicherer als das, was durch die vollständige Clojure-Syntax definiert wird, insbesondere von nicht +vertrauenswürdigen Quellen. EDN ist beschränkt auf Daten, kein Code. Es ist ähnlich in seinen Zielen zu JSON. +Obwohl es mehr in Clojure verwendet wird, gibt es verschiedene Implementationen von EDN in vielen +verschiedenen anderen Sprachen. + +Der Hauptvorteil von EDN im Gegensatz zu JSON und YAML ist, dass es erweiterbar ist. +Wir werden später sehen wie es erweitert werden kann. + +```clojure +; Kommentare starten mit einem Semikolon. +; Alles nach dem Semikolon wird ignoriert. + +;;;;;;;;;;;;;;;;;;; +;;; Basistypen ;;; +;;;;;;;;;;;;;;;;;;; + +nil ; auch bekannt in anderen Sprachen als null + +; Booleans +true +false + +; Strings werden in Gänsefüßchen eingeschlossen. +"hungarian breakfast" +"farmer's cheesy omelette" + +; Charaktere werden einem Backslash vorangestellt +\g \r \a \c \e + +; Schlüsselwörter beginnen mit einem Doppelpunkt. Sie verhalten sich wie Enums. +; Ähnlich, wie Symbole in Ruby. +:eggs +:cheese +:olives + +; Symbole werden verwendet um Identifier zu repräsentieren. Sie beginnen mit einem #. +; Du kannst einen Namespace für Symbole nutzen, wenn du / verwendest. Egal was / vorangestellt wird +; ist der Namespace dieses Namens. +#spoon +#kitchen/spoon ; nicht das selbe, wie #spoon +#kitchen/fork +#github/fork ; damit kannst du nicht essen + +; Integers und Floats +42 +3.14159 + +; Listen sind Sequenzen von Werten +(:bun :beef-patty 9 "yum!") + +; Vektoren erlauben zufälligen Zugriff +[:gelato 1 2 -2] + +; Maps sind assoziative Datenstrukturen, die einen Schlüssel mit einem Wert verbinden. +{:eggs 2 + :lemon-juice 3.5 + :butter 1} + +; Du bist nicht beschränkt ausschließlich Schlüsselwörter als Schlüssel zu verwenden. +{[1 2 3 4] "tell the people what she wore", + [5 6 7 8] "the more you see the more you hate"} + +; Du kannst Kommas für eine bessere Lesbarkeit verwenden. Sie werden wie Leerraum behandelt. +; Sets sind Sammlungen, die eindeutige Elemente enthalten. +#{:a :b 88 "huat"} + +;;;;;;;;;;;;;;;;;;;;;;;;; +;;; markierte Elemente ;;; +;;;;;;;;;;;;;;;;;;;;;;;;; + +; EDN kann erweitert werden, indem Elemente mit # Symbolen makiert werden. + +#MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10} + +; Lass mich das mit einem Clojure Beispiel erklären. +; Angenommen ich möchte dieses Stück EDM in einen MenuItem record umwandeln. +(defrecord MenuItem [name rating]) + +; Um EDN in clojure Werte umzuwandeln, muss ich den eingebauten EDN Leser +; edn/read-string verwenden + +(edn/read-string "{:eggs 2 :butter 1 :flour 5}") +; -> {:eggs 2 :butter 1 :flour 5} + +; Definiere die Leserfunktion, um markierte Elemente zu transformieren +; und übergebe eine Map, die Tags den Lesefunktionen als edn / read-string zuweisen + +(edn/read-string {:readers {'MyYelpClone/MenuItem map->menu-item}} + "#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}") +; -> #user.MenuItem{:name "eggs-benedict", :rating 10} + +``` + +# Referenzen + +- [EDN spec](https://github.com/edn-format/edn) +- [Implementationen](https://github.com/edn-format/edn/wiki/Implementations) +- [makierte Elemente](http://www.compoundtheory.com/clojure-edn-walkthrough/) diff --git a/de-de/git-de.html.markdown b/de-de/git-de.html.markdown index 61f7bb67..a0ed120f 100644 --- a/de-de/git-de.html.markdown +++ b/de-de/git-de.html.markdown @@ -205,6 +205,12 @@ Speichert die aktuellen Inhalte des Index in einen neuen *Commit*. Dieser Commit ```bash # Commit mit Beschreibung erstellen. $ git commit -m "Added multiplyNumbers() function to HelloWorld.c" + +# Alle veränderten oder gelöschten Dateien außer neue Dateien werden gestaged und dann wird ein Commit erstellt. +$ git commit -a -m "Modified foo.php and removed bar.php" + +# Ändert den letzten Commit (der letzte Commit wird mit einem neuen Commit ersetzt) +$ git commit --amend -m "Correct message" ``` ### diff diff --git a/de-de/go-de.html.markdown b/de-de/go-de.html.markdown index 817cb4ae..9409e181 100644 --- a/de-de/go-de.html.markdown +++ b/de-de/go-de.html.markdown @@ -94,7 +94,7 @@ Zeilenumbrüche beinhalten.` // Selber Zeichenketten-Typ // Arrays haben bei Kompile-Zeit festgelegte Größen var a4 [4]int // Ein Array mit 4 ints, alle mit Initialwert 0 - a3 := [...]int{3, 1, 5} // Ein Array mit 4 ints, Initialwerte wie angezeigt + a3 := [...]int{3, 1, 5} // Ein Array mit 3 ints, Initialwerte wie angezeigt // "slices" haben eine dynamische Größe. Arrays und Slices haben beide ihre // Vorzüge, aber slices werden viel häufiger verwendet diff --git a/de-de/html-de.html.markdown b/de-de/html-de.html.markdown index 2ee18129..0bf58f9c 100644 --- a/de-de/html-de.html.markdown +++ b/de-de/html-de.html.markdown @@ -9,7 +9,7 @@ lang: de-de --- HTML steht für HyperText Markup Language (Hypertext-Auszeichnungssprache). -Sie ist eine Sprache, um Seiten für das World Wide Web zu schreiben.. +Sie ist eine Sprache, um Seiten für das World Wide Web zu schreiben. Es ist eine Auszeichnugssprache, die es uns ermöglicht Webseiten mithilfe des Codes zu schreiben, der kennzeichnet wie Text und Daten angezeigt werden sollen. Eigentlich sind HTML Dateien nur einfache Textdateien. Was sind das für Auszeichnungen? Es ist eine Methode, um die Daten der Website zu organisieren mithilfe von Start- und Endtags. Diese Auszeichnung dient dazu dem Text Bedeutung zu geben, welchen sie umschließt. @@ -111,7 +111,7 @@ Dieser Artikel ist bedacht darauf, nur HTML Syntax und nützliche Tipps zu geben ## Verwendung -HTML Dateien enden mit `.html`. +HTML Dateien enden mit `.html` oder mit `.htm`. Der Mime Typ ist meist `text/html`. ## Um mehr zu lernen diff --git a/de-de/java-de.html.markdown b/de-de/java-de.html.markdown index 934819f9..e8ac5bda 100644 --- a/de-de/java-de.html.markdown +++ b/de-de/java-de.html.markdown @@ -1,6 +1,6 @@ --- language: java -filename: LearnJavaDe.java +filename: LearnJavaDe-de.java contributors: - ["Jake Prather", "http://github.com/JakeHP"] - ["Jakukyo Friel", "http://weakish.github.io"] diff --git a/de-de/make-de.html.markdown b/de-de/make-de.html.markdown index 22c14a69..bc5c7bcb 100644 --- a/de-de/make-de.html.markdown +++ b/de-de/make-de.html.markdown @@ -2,6 +2,7 @@ language: make
contributors:
- ["Robert Steed", "https://github.com/robochat"]
+ - ["Stephan Fuhrmann", "https://github.com/sfuhrm"]
translators:
- ["Martin Schimandl", "https://github.com/Git-Jiro"]
filename: Makefile-de
@@ -58,7 +59,7 @@ file2.txt file3.txt: file0.txt file1.txt touch file3.txt
# Make wird sich beschweren wenn es mehrere Rezepte für die gleiche Regel gibt.
-# Leere Rezepte zählen nicht und können dazu verwendet werden weitere
+# Leere Rezepte zählen nicht und können dazu verwendet werden weitere
# Voraussetzungen hinzuzufügen.
#-----------------------------------------------------------------------
@@ -182,9 +183,9 @@ echo: name2 = Sara # Wahr innerhalb der passenden Regel und auch innerhalb # Ein paar Variablen die von Make automatisch definiert werden.
echo_inbuilt:
echo $(CC)
- echo ${CXX)}
+ echo ${CXX}
echo $(FC)
- echo ${CFLAGS)}
+ echo ${CFLAGS}
echo $(CPPFLAGS)
echo ${CXXFLAGS}
echo $(LDFLAGS)
diff --git a/de-de/markdown-de.html.markdown b/de-de/markdown-de.html.markdown index 2c838660..cccf5e68 100644 --- a/de-de/markdown-de.html.markdown +++ b/de-de/markdown-de.html.markdown @@ -14,7 +14,7 @@ Syntax, in der sich Dokumente leicht schreiben *und* lesen lassen. Außerdem sollte Markdown sich leicht nach HTML (und in andere Formate) konvertieren lassen. -```markdown +```md <!-- Markdown ist eine Obermenge von HTML - jede valide HTML-Datei ist also automatisch valides Markdown - was heisst dass wir jedes HTML-Element (also auch Kommentare) in Markdown benutzen können, ohne dass der Parser sie verändert. @@ -253,4 +253,4 @@ Ganz schön hässlich | vielleicht doch lieber | wieder aufhören Mehr Informationen gibt es in [John Gruber's offiziellem Blog-Post](http://daringfireball.net/projects/markdown/syntax) und bei Adam Pritchards [grandiosem Cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet). -Infos zu GitHub Flavored Markdown [gibt es hier](https://help.github.com/articles/github-flavored-markdown).
\ No newline at end of file +Infos zu GitHub Flavored Markdown [gibt es hier](https://help.github.com/articles/github-flavored-markdown). diff --git a/de-de/nix-de.html.markdown b/de-de/nix-de.html.markdown new file mode 100644 index 00000000..79b60d20 --- /dev/null +++ b/de-de/nix-de.html.markdown @@ -0,0 +1,358 @@ +--- +language: nix +filename: learnnix-de.nix +contributors: + - ["Chris Martin", "http://chris-martin.org/"] +translators: + - ["Dennis Keller", "https://github.com/denniskeller"] +lang: de-de +--- + +Nix ist eine simple funktionale Programmiersprache, die für den +[Nix package manager](https://nixos.org/nix/) und +[NixOS](https://nixos.org/) entwickelt wurde. + +Du kannst Nix Ausdrücke evaluieren mithilfe von +[nix-instantiate](https://nixos.org/nix/manual/#sec-nix-instantiate) +oder [`nix-repl`](https://github.com/edolstra/nix-repl). + +``` +with builtins; [ + + # Kommentare + #========================================= + + # Inline Kommentare sehen so aus. + + /* Multizeilen Kommentare + sehen so aus. */ + + + # Booleans + #========================================= + + (true && false) # Und + #=> false + + (true || false) # Oder + #=> true + + (if 3 < 4 then "a" else "b") # Bedingungen + #=> "a" + + + # Integers + #========================================= + + # Integers sind die einzigen numerischen Typen. + + 1 0 42 (-3) # Einige integers + + (4 + 6 + 12 - 2) # Addition + #=> 20 + + (7 / 2) # Division + #=> 3 + + + # Strings + #========================================= + + "String Literale sind in Anführungszeichen." + + " + String Literale können mehrere + Zeilen umspannen. + " + + '' + Dies wird als Literal mit eingerückten String bezeichnet. + Es entfernt intelligent führende Leerzeichen. + '' + + '' + a + b + '' + #=> "a\n b" + + ("ab" + "cd") # String Konkatenation + #=> "abcd" + + # Mit Antiquotation kannst du Werte in Strings einbetten. + ("Dein Homeverzeichnis ist ${getEnv "HOME"}") + #=> "Dein Homeverzeichnis ist /home/alice" + + + # Paths + #========================================= + + # Nix besitzt einen primitiven Datentyp für Pfade + /tmp/tutorials/learn.nix + + # Ein relativer Pfad wird beim Parsing zu einem absoluten Pfad aufgelöst, + # relativ zu der Datei in der es auftritt. + tutorials/learn.nix + #=> /the-base-path/tutorials/learn.nix + + # Ein Pfad muss mindestens einen Schrägstrich enthalten. Ein Pfad für eine + # Datei im selben Verzeichnis benötigt ein ./ Präfix. + ./learn.nix + #=> /the-base-path/learn.nix + + # Der / Operator muss von Leerraum umgeben sein wenn du dividieren möchtest. + 7/2 # Das ist ein Pfadliteral + (7 / 2) # Das ist ein Integerliteral + + + # Importe + #========================================= + + # Eine nix Datei besitzt einen einzelnen top-level Ausdruck mit keinen freien Variablen. + # Ein Import-Ausdruck wird zum Wert der Datei, die importiert wird, ausgewertet. + (import /tmp/foo.nix) + + # Importe können ebenso mit Strings spezifiziert werden. + (import "/tmp/foo.nix") + + # Import Pfade müssen absolut sein. Pfadliterale + # sind automatisch aufgelöst, das ist ein Ordnung. + (import ./foo.nix) + + # Jedoch passiert dies nicht mit Strings. + (import "./foo.nix") + #=> error: string ‘foo.nix’ doesn't represent an absolute path + + + # Let + #========================================= + + # `let` Blöcke erlauben es uns Werte zu Variablen zu binden. + (let x = "a"; in + x + x + x) + #=> "aaa" + + # Bindungen können auf sich gegenseitig verweisen. Die Reihenfolge spielt + # keine Rolle. + (let y = x + "b"; + x = "a"; in + y + "c") + #=> "abc" + + # Innere Bindungen überschatten Äußere. + (let a = 1; in + let a = 2; in + a) + #=> 2 + + + # Funktionen + #========================================= + + (n: n + 1) # Funktion, die 1 addiert + + ((n: n + 1) 5) # Dieselbe Funktion angewendet auf 5. + #=> 6 + + # Es gibt keine spezielle Syntax für benannte Funktionen, aber sie + # können mit `let` Blöcken, wie jeder andere Wert auch, gebunden werden. + (let succ = (n: n + 1); in succ 5) + #=> 6 + + # Eine Funktion hat genau ein Argument. + # Mehrere Argumente können erreicht werden mithilfe von Currying. + ((x: y: x + "-" + y) "a" "b") + #=> "a-b" + + # Benannte Funktionsargumente gibt es auch. Diese werden wir einführen, nachdem wir uns Sets + # angeschaut haben. + + # Listen + #========================================= + + # Listen werden durch eckige Klammern gekennzeichnet. + + (length [1 2 3 "x"]) + #=> 4 + + ([1 2 3] ++ [4 5]) + #=> [1 2 3 4 5] + + (concatLists [[1 2] [3 4] [5]]) + #=> [1 2 3 4 5] + + (head [1 2 3]) + #=> 1 + (tail [1 2 3]) + #=> [2 3] + + (elemAt ["a" "b" "c" "d"] 2) + #=> "c" + + (elem 2 [1 2 3]) + #=> true + (elem 5 [1 2 3]) + #=> false + + (filter (n: n < 3) [1 2 3 4]) + #=> [ 1 2 ] + + + # Sets + #========================================= + + # Ein "Set" ist eine ungeordnete Zuordnung mit Stringschlüsseln. + { foo = [1 2]; bar = "x"; } + + # Der . Operator nimmt einen Wert aus dem Set. + { a = 1; b = 2; }.a + #=> 1 + + # Der ? Operator testet, ob der Schlüssel in dem Set vorhanden ist. + ({ a = 1; b = 2; } ? a) + #=> true + ({ a = 1; b = 2; } ? c) + #=> false + + # Der // Operator mergt zwei Sets. + ({ a = 1; } // { b = 2; }) + #=> { a = 1; b = 2; } + + # Werte auf der rechten Seite überschreiben die Werte auf der linken Seite. + ({ a = 1; b = 2; } // { a = 3; c = 4; }) + #=> { a = 3; b = 2; c = 4; } + + # Das Schlüsselwort rec bezeichenet ein "rekursives Set", in dem sich Attribute + # aufeinander beziehen können. + (let a = 1; in { a = 2; b = a; }.b) + #=> 1 + (let a = 1; in rec { a = 2; b = a; }.b) + #=> 2 + + # Verschachtelte Sets können stückweise definiert werden. + { + a.b = 1; + a.c.d = 2; + a.c.e = 3; + }.a.c + #=> { d = 2; e = 3; } + + # Die Nachkommen eines Attributs können in diesem Feld nicht zugeordnet werden, wenn + # das Attribut selbst nicht zugewiesen wurde. + { + a = { b = 1; }; + a.c = 2; + } + #=> error: attribute ‘a’ already defined + + + # With + #========================================= + + # Der Körper eines Sets Blocks wird mit der Zuordnung eines Satzes an die Variablen gebunden. + (with { a = 1; b = 2; }; + a + b) + # => 3 + + # Innere Bindungen überschatten äußere Bindungen. + (with { a = 1; b = 2; }; + (with { a = 5; }; + a + b)) + #=> 7 + + # Die erste Linie diese Tutorials startet mit "with builtins;", + # weil builtins ein Set mit allen eingebauten + # Funktionen (length, head, tail, filter, etc.) umfasst. + # Das erspart uns beispielsweise "builtins.length" zu schreiben, + # anstatt nur "length". + + + # Set patterns + #========================================= + + # Sets sind nützlich, wenn du mehrere Werte einer Funktion + # übergeben musst. + (args: args.x + "-" + args.y) { x = "a"; y = "b"; } + #=> "a-b" + + # Dies kann mit Hilfe von Set patterns deutlicher geschrieben werden. + ({x, y}: x + "-" + y) { x = "a"; y = "b"; } + #=> "a-b" + + # Standardmäßig schlägt das Muster bei Sets mit zusätzlichen Schlüsseln fehl. + ({x, y}: x + "-" + y) { x = "a"; y = "b"; z = "c"; } + #=> error: anonymous function called with unexpected argument ‘z’ + + # Durch Hinzufügen von ", ..." können zusätzliche Schlüssel ignoriert werden. + ({x, y, ...}: x + "-" + y) { x = "a"; y = "b"; z = "c"; } + #=> "a-b" + + + # Errors + #========================================= + + # `throw` bewirkt, dass die Auswertung mit einer Fehlermeldung abgebrochen wird. + (2 + (throw "foo")) + #=> error: foo + + # `tryEval` fängt geworfene Fehler. + (tryEval 42) + #=> { success = true; value = 42; } + (tryEval (2 + (throw "foo"))) + #=> { success = false; value = false; } + + # `abort` ist ähnlich wie throw, aber es ist fatal. Es kann nicht gefangen werden. + (tryEval (abort "foo")) + #=> error: evaluation aborted with the following error message: ‘foo’ + + # `assert` evaluiert zu dem gegebenen Wert, wenn die Bedingung wahr ist, sonst + # löst es eine abfangbare Exception aus. + (assert 1 < 2; 42) + #=> 42 + (assert 1 > 2; 42) + #=> error: assertion failed at (string):1:1 + (tryEval (assert 1 > 2; 42)) + #=> { success = false; value = false; } + + + # Impurity + #========================================= + + # Da die Wiederholbarkeit von Builds für den Nix Packetmanager entscheidend ist, + # werden in der Nix Sprache reine funktionale Elemente betont. Es gibt aber ein paar + # unreine Elemente. + # Du kannst auf Umgebungsvariablen verweisen. + (getEnv "HOME") + #=> "/home/alice" + + # Die trace Funktion wird zum Debugging verwendet. Sie gibt das erste Argument zu stderr aus + # und evaluiert das zweite Argument. + (trace 1 2) + #=> trace: 1 + #=> 2 + + # Du kannst Dateien in den Nix Store schreiben. Obwohl unrein, kannst du dir relativ sicher sein, + # dass es sicher ist, da der Dateiname aus dem Hash des Inhalts abgeleitet wird. + # Du kannst Dateien von überall lesen. In diesem Beispiel schreiben wir Dateien in den Store + # und lesen wieder davon. + (let filename = toFile "foo.txt" "hello!"; in + [filename (builtins.readFile filename)]) + #=> [ "/nix/store/ayh05aay2anx135prqp0cy34h891247x-foo.txt" "hello!" ] + + # Außerdem können wir Dateien in den Nix Store herunterladen. + (fetchurl "https://example.com/package-1.2.3.tgz") + #=> "/nix/store/2drvlh8r57f19s9il42zg89rdr33m2rm-package-1.2.3.tgz" + +] +``` + +### Weitere Ressourcen + +* [Nix Manual - Nix expression language] + (https://nixos.org/nix/manual/#ch-expression-language) + +* [James Fisher - Nix by example - Part 1: The Nix expression language] + (https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55) + +* [Susan Potter - Nix Cookbook - Nix By Example] + (http://funops.co/nix-cookbook/nix-by-example/) diff --git a/de-de/pyqt-de.html.markdown b/de-de/pyqt-de.html.markdown new file mode 100644 index 00000000..93ee20d4 --- /dev/null +++ b/de-de/pyqt-de.html.markdown @@ -0,0 +1,89 @@ +--- +category: tool +tool: PyQT +filename: learnpyqt-de.py +contributors: + - ["Nathan Hughes", "https://github.com/sirsharpest"] +translators: + - ["Dennis Keller", "https://github.com/denniskeller"] +lang: de-de +--- + +**Qt** ist eine weit bekanntes Framework mit den man plattformunabhängige Programme schreiben kann, +die auf verschiedenen Sotfware und Hardware Plattformen laufen mit kleinen oder keinen Änderungen im Code. +Dabei besitzen sie trozdem die Power und Geschwindigkeit von nativen Anwendungen. +**Qt** wurde ursprünglich in *C++** geschrieben. + +Das ist eine Adaption von dem C++ Intro für QT von [Aleksey Kholovchuk](https://github.com/vortexxx192), +manche der Codebeispiele sollte in der selben Funktionalität resultieren. +Diese Version wurde in pyqt erstellt. + +```python +import sys +from PyQt4 import QtGui + +def window(): + # Erschafft ein Anwendungsobjekt. + app = QtGui.QApplication(sys.argv) + # Erschafft ein Widget, auf dem unser Label platziert wird. + w = QtGui.QWidget() + # Fügt ein Label zu dem Widget hinzu. + b = QtGui.QLabel(w) + # Setzt einen Text für das Label. + b.setText("Hello World!") + # Setzt die Größe und die Platzierungsinfomationen. + w.setGeometry(100, 100, 200, 50) + b.move(50, 20) + # Setzt unserem Fenster einen schönen Titel. + w.setWindowTitle("PyQt") + # Lässt alles anzeigen. + w.show() + # Führe alles aus, nachdem wir alles aufgebaut haben. + sys.exit(app.exec_()) + +if __name__ == '__main__': + window() + +``` + +Damit wir weitere fortgeschrittene Funktionen in **pyqt** verwenden können, +müssen wir anfangen zusätzliche Elemente zu bauen. +Hier zeigen wir wie man eine Dialog Popup Box einführt. +Diese ist nützlich, um den Benutzer eine Entscheidung zu bestätigen oder um Informationen anzuzeigen. + +```Python +import sys +from PyQt4.QtGui import * +from PyQt4.QtCore import * + + +def window(): + app = QApplication(sys.argv) + w = QWidget() + # Erschafft einen Knopf und fügt das Widget w hinzu + b = QPushButton(w) + b.setText("drücke mich") + b.move(50, 50) + # Wenn b gedrückt wird, wird diese Funktion aufgerufen. + # Bemerke das Fehlen von () bei dem Funktionsaufruf. + b.clicked.connect(showdialog) + w.setWindowTitle("PyQt Dialog") + w.show() + sys.exit(app.exec_()) + +# Diese Funktion soll ein Dialogfenster mit einem Knopf erschaffen. +# Der Knopf wartet bis er geklickt wird und beendet das Programm +def showdialog(): + d = QDialog() + b1 = QPushButton("ok", d) + b1.move(50, 50) + d.setWindowTitle("Dialog") + # Diese Modalität sagt dem Popup, dass es den Parent blocken soll, solange es aktiv ist. + d.setWindowModality(Qt.ApplicationModal) + # Beim klicken möchte ich, dass der gesamte Prozess beendet wird. + b1.clicked.connect(sys.exit) + d.exec_() + +if __name__ == '__main__': + window() +``` diff --git a/de-de/python3-de.html.markdown b/de-de/python3-de.html.markdown index ef1786c8..b313727c 100644 --- a/de-de/python3-de.html.markdown +++ b/de-de/python3-de.html.markdown @@ -146,13 +146,13 @@ bool({}) #=> False #################################################### # Textausgabe ist sehr einfach -print "Ich bin Python. Schön, dich kennenzulernen!" +print("Ich bin Python. Schön, dich kennenzulernen!") # Es gibt keinen Grund, Variablen vor der Zuweisung zu deklarieren. 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 @@ -307,11 +307,11 @@ some_var = 5 # Hier eine if-Anweisung. Die Einrückung ist in Python wichtig! # gibt "some_var ist kleiner als 10" aus if some_var > 10: - print "some_var ist viel größer als 10." + print("some_var ist viel größer als 10.") elif some_var < 10: # Dieser elif-Absatz ist optional. - print "some_var ist kleiner als 10." + print("some_var ist kleiner als 10.") else: # Das hier ist auch optional. - print "some_var ist tatsächlich 10." + print("some_var ist tatsächlich 10.") """ @@ -334,7 +334,7 @@ Ausgabe: 3 """ for i in range(4): - print i + print(i) """ "range(unten, oben)" gibt eine Liste von der unteren Zahl bis zur oberen Zahl aus @@ -357,7 +357,7 @@ Ausgabe: """ x = 0 while x < 4: - print x + print(x) x += 1 # Kurzform für x = x + 1 # Ausnahmebehandlung mit einem try/except-Block @@ -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 @@ -418,7 +418,7 @@ list(filled_dict.keys()) #=> ["one", "two", "three"] # Mit def neue Funktionen erstellen def add(x, y): - print "x ist %s und y ist %s" % (x, y) + print("x ist %s und y ist %s" % (x, y)) return x + y # Werte werden mit return zurückgegeben # Funktionen mit Parametern aufrufen @@ -442,10 +442,10 @@ 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 + print(args) + print(kwargs) """ all_the_args(1, 2, a=3, b=4) Ausgabe: (1, 2) @@ -532,10 +532,10 @@ class Human(object): # Eine Instanz einer Klasse erstellen i = Human(name="Ian") -print i.say("hi") # gibt "Ian: hi" aus +print(i.say("hi")) # gibt "Ian: hi" aus j = Human("Joel") -print j.say("hello") #gibt "Joel: hello" aus +print(j.say("hello")) #gibt "Joel: hello" aus # Rufen wir mal unsere Klassenmethode auf i.get_species() #=> "H. sapiens" @@ -555,12 +555,12 @@ Human.grunt() #=> "*grunt*" # Wir können Module importieren import math -print math.sqrt(16) #=> 4 +print(math.sqrt(16)) #=> 4 # Wir können auch nur spezielle Funktionen eines Moduls importieren from math import ceil, floor -print ceil(3.7) #=> 4.0 -print floor(3.7) #=> 3.0 +print(ceil(3.7)) #=> 4.0 +print(floor(3.7)) #=> 3.0 # Wir können auch alle Funktionen eines Moduls importieren # Warnung: Dies wird nicht empfohlen diff --git a/de-de/qt-de.html.markdown b/de-de/qt-de.html.markdown new file mode 100644 index 00000000..480030fe --- /dev/null +++ b/de-de/qt-de.html.markdown @@ -0,0 +1,175 @@ +--- +category: tool +tool: Qt Framework +language: c++ +filename: learnqt-de.cpp +contributors: + - ["Aleksey Kholovchuk", "https://github.com/vortexxx192"] +translators: + - ["Dennis Keller", "https://github.com/denniskeller"] +lang: de-de +--- + +**Qt** ist ein weithin bekanntes Framework zum Entwickeln von cross-platform Software, +die auf verschiedenen Hard- und Softwareplatformen mit wenig oder keinen Veränderungen im Code läuft. +Dabei besitzt man die Power und Geschiwindigkeit von nativen Anwendungen. +Obwohl **Qt** ursprünglich in *C++* geschrieben wurde, +gibt es verschiedene Ports für andere Sprachen: *[PyQt](https://learnxinyminutes.com/docs/pyqt/)*, *QtRuby*, *PHP-Qt*, etc. + +**Qt** eignet sich hervorragend zum Erstellen von Anwendungen mit grafischer Benutzeroberfläche (GUI). +Dieses Tutorial zeigt, wie man das in *C++* macht. + +```c++ +/* + * Lass uns klassisch starten + */ + +// Alle Header vom Qt Framework starten mit dem Großbuchstaben 'Q'. +#include <QApplication> +#include <QLineEdit> + +int main(int argc, char *argv[]) { + // Erstellt ein Objekt um applikationsweit die Resourcen zu managen. + QApplication app(argc, argv); + + // Erstellt ein Line edit Widget und zeigt es auf dem Bildschirm + QLineEdit lineEdit("Hello world!"); + lineEdit.show(); + + // Startet die Event Loop der Anwendung. + return app.exec(); +} +``` + +Die GUI bezogene Teile von **Qt** bestehen aus *Widgets* und den *Verbindungen* +dazwischen. + +[Lies mehr über Widgets](http://doc.qt.io/qt-5/qtwidgets-index.html) + +```c++ +/* + * Lass uns Label und einen Button machen. + * Ein Label soll auftauchen, wenn der Button gedrückt wird. + * + * Der Qt Code spricht für sich selbst. + */ + +#include <QApplication> +#include <QDialog> +#include <QVBoxLayout> +#include <QPushButton> +#include <QLabel> + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + + QDialog dialogWindow; + dialogWindow.show(); + + // Füge ein vertikales Layout hinzu + QVBoxLayout layout; + dialogWindow.setLayout(&layout); + + QLabel textLabel("Danke für das Knopf drücken"); + layout.addWidget(&textLabel); + textLabel.hide(); + + QPushButton button("Drück mich"); + layout.addWidget(&button); + + // Zeigt verstecktes Label, wenn der Button gedrückt wird. + QObject::connect(&button, &QPushButton::pressed, + &textLabel, &QLabel::show); + + return app.exec(); +} +``` + +Beachte den *QObject::connect* Teil. Diese Methode wird verwendet, +um *Signale* eines Objekts mit den *Slots* eines Objektes zu verbinden. + +**Signale** werden ausgegeben, wenn bestimmte Dinge mit Objekten passieren. +Beispielsweise wird das *pressed* Signal ausgegeben, +wenn der Benutzer auf das QPushButton Objekt drückt. + +**Slots** sind Aktionen, die als Reaktion auf empfangene Signale ausgeführt werden können. + +[Lies mehr über Slots und Signale](http://doc.qt.io/qt-5/signalsandslots.html) + + +Als Nächstes lernen wir, dass wir nicht nur Standard Widgets verwenden können, +sondern auch ihr Verhalten mithilfe von Vererbung verändern können. +Lass uns einen Button erschaffen, der zählt, wie häufig er gedrückt wird. +Dafür definieren wir unsere eigene Klasse *CounterLabel*. +Diese muss wegen der speziellen Qt Architektur in einer seperaten Datei deklariert werden. + +```c++ +// counterlabel.hpp + +#ifndef COUNTERLABEL +#define COUNTERLABEL + +#include <QLabel> + +class CounterLabel : public QLabel { + Q_OBJECT // Qt definiertes Makro, welches in jedem modifizierten Widget vorhanden sein muss. + +public: + CounterLabel() : counter(0) { + setText("Zähler wurde noch nicht erhöht."); // Methode von QLabel + } + +public slots: + // Aktion, die ausgeführt wird, wenn der Button gedrückt wird. + void increaseCounter() { + setText(QString("Zähler Wert: %1").arg(QString::number(++counter))); + } + +private: + int counter; +}; + +#endif // Zähllabel +``` + +```c++ +// main.cpp +// Fast das Gleiche, wie das vorherige Beispiel + +#include <QApplication> +#include <QDialog> +#include <QVBoxLayout> +#include <QPushButton> +#include <QString> +#include "counterlabel.hpp" + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + + QDialog dialogWindow; + dialogWindow.show(); + + QVBoxLayout layout; + dialogWindow.setLayout(&layout); + + CounterLabel counterLabel; + layout.addWidget(&counterLabel); + + QPushButton button("Drück mich nochmal."); + layout.addWidget(&button); + QObject::connect(&button, &QPushButton::pressed, + &counterLabel, &CounterLabel::increaseCounter); + + return app.exec(); +} +``` + +Das wars! Natürlich ist das Qt Framework erheblich größer, als der der Teil der in diesem Tutorial behandelt wurde. +Das heißt, es gibt viel zu lesen und zu üben. + +## Further reading + +- [Qt 4.8 tutorials](http://doc.qt.io/qt-4.8/tutorials.html) +- [Qt 5 tutorials](http://doc.qt.io/qt-5/qtexamplesandtutorials.html) + +Viel Erfolg und viel Spaß! diff --git a/de-de/scala-de.html.markdown b/de-de/scala-de.html.markdown index 456403a2..a8344e6a 100644 --- a/de-de/scala-de.html.markdown +++ b/de-de/scala-de.html.markdown @@ -8,89 +8,90 @@ contributors: - ["Dennis Keller", "github.com/denniskeller"] translators: - ["Christian Albrecht", "https://github.com/coastalchief"] + - ["Jonas Grote", "https://github.com/exic"] filename: learnscala-de.scala lang: de-de --- -Scala ist eine funktionale und objektorientierte Programmiersprache -für die Java Virtual Machine (JVM), um allgemeine Programmieraufgaben -zu erledigen. Scala hat einen akademischen Hintergrund und wurde an +Scala ist eine funktionale und objektorientierte Programmiersprache +für die Java Virtual Machine (JVM), um allgemeine Programmieraufgaben +zu erledigen. Scala hat einen akademischen Hintergrund und wurde an der EPFL (Lausanne / Schweiz) unter der Leitung von Martin Odersky entwickelt. -```scala +```scala /* -Scala Umgebung einrichten: +Scala-Umgebung einrichten: 1. Scala binaries herunterladen- http://www.scala-lang.org/downloads 2. Unzip/untar in ein Verzeichnis -3. das bin Unterverzeichnis der `PATH` Umgebungsvariable hinzufügen +3. das Unterverzeichnis `bin` der `PATH`-Umgebungsvariable hinzufügen 4. Mit dem Kommando `scala` wird die REPL gestartet und zeigt als Prompt: scala> -Die REPL (Read-Eval-Print Loop) ist der interaktive Scala Interpreter. -Hier kann man jeden Scala Ausdruck verwenden und das Ergebnis wird direkt -ausgegeben. -Als nächstes beschäftigen wir uns mit ein paar Scala Basics. +Die REPL (Read-Eval-Print Loop) ist der interaktive Scala Interpreter. +Hier kann man jeden Scala Ausdruck verwenden und das Ergebnis wird direkt +ausgegeben. +Als nächstes beschäftigen wir uns mit ein paar Scala-Grundlagen. */ ///////////////////////////////////////////////// -// 1. Basics +// 1. Grundlagen ///////////////////////////////////////////////// -// Einzeilige Kommentare beginnen mit zwei Slashes +// Einzeilige Kommentare beginnen mit zwei Schrägstrichen. /* - Mehrzeilige Kommentare, starten - mit einem Slash-Stern und enden mit einem Stern-Slash + Mehrzeilige Kommentare starten mit Schrägstrich und Stern + und enden mit Stern und Schrägstrich. */ -// Einen Wert, und eine zusätzliche neue Zeile ausgeben +// Einen Wert und eine zusätzliche neue Zeile ausgeben: -println("Hello world!") +println("Hallo Welt!") println(10) -// Einen Wert, ohne eine zusätzliche neue Zeile ausgeben +// Einen Wert ohne eine zusätzliche neue Zeile ausgeben: -print("Hello world") +print("Hallo Welt") /* - Variablen werden entweder mit var oder val deklariert. - Deklarationen mit val sind immutable, also unveränderlich - Deklarationen mit var sind mutable, also veränderlich - Immutability ist gut. + Variablen werden entweder mit var oder val deklariert. + Deklarationen mit val sind immutable, also unveränderlich. + Deklarationen mit var sind mutable, also veränderlich. + Immutability ist gut. */ val x = 10 // x ist 10 -x = 20 // error: reassignment to val +x = 20 // Error: reassignment to val (Fehler: neue Zuweisung zu einem unveränderlichen Wert) var y = 10 y = 20 // y ist jetzt 20 /* -Scala ist eine statisch getypte Sprache, auch wenn wir in dem o.g. Beispiel -keine Typen an x und y geschrieben haben. -In Scala ist etwas eingebaut, was sich Type Inference nennt. Das heißt das der -Scala Compiler in den meisten Fällen erraten kann, von welchen Typ eine Variable ist, -so dass der Typ nicht jedes mal angegeben werden muss. -Einen Typ gibt man bei einer Variablendeklaration wie folgt an: +Scala ist eine statisch getypte Sprache, auch wenn wir in dem o.g. Beispiel +keine Typen an x und y geschrieben haben. +In Scala ist etwas eingebaut, was sich Type Inference nennt. Das heißt, dass der +Scala Compiler in den meisten Fällen erraten kann, von welchem Typ eine Variable ist, +so dass der Typ nicht jedes mal angegeben werden muss. +Einen Typ gibt man bei einer Variablendeklaration wie folgt an: */ val z: Int = 10 val a: Double = 1.0 -// Bei automatischer Umwandlung von Int auf Double wird aus 10 eine 10.0 +// Bei automatischer Umwandlung von Int auf Double wird aus 10 eine 10.0: val b: Double = 10 -// Boolean Werte +// Boolean-Werte: true false -// Boolean Operationen +// Boolean-Operationen: !true // false !false // true @@ -98,7 +99,7 @@ true == false // false 10 > 5 // true -// Mathematische Operationen sind wie gewohnt +// Mathematische Operationen sind wie gewohnt: 1 + 1 // 2 2 - 1 // 1 @@ -108,88 +109,87 @@ true == false // false 6.0 / 4 // 1.5 -// Die Auswertung eines Ausdrucks in der REPL gibt den Typ -// und das Ergebnis zurück. +// Die Auswertung eines Ausdrucks in der REPL gibt den Typ +// und das Ergebnis zurück: scala> 1 + 7 res29: Int = 8 /* -Das bedeutet, dass das Resultat der Auswertung von 1 + 7 ein Objekt -von Typ Int ist und einen Wert 0 hat. -"res29" ist ein sequentiell generierter name, um das Ergebnis des -Ausdrucks zu speichern. Dieser Wert kann bei Dir anders sein... +Das bedeutet, dass das Resultat der Auswertung von 1 + 7 ein Objekt +von Typ Int ist und einen Wert 8 hat. +"res29" ist ein sequentiell generierter Name, um das Ergebnis des +Ausdrucks zu speichern. Dieser Wert kann bei Dir anders sein... */ -"Scala strings werden in doppelten Anführungszeichen eingeschlossen" -'a' // A Scala Char -// 'Einzeln ge-quotete strings gibt es nicht!' <= This causes an error - -// Für Strings gibt es die üblichen Java Methoden +"Scala strings werden in doppelten Anführungszeichen eingeschlossen" +'a' // Ein Scala Char +// 'Einzeln ge-quotete strings gibt es nicht!' <= Das erzeugt einen Fehler! -"hello world".length -"hello world".substring(2, 6) -"hello world".replace("C", "3") +// Für Strings gibt es die üblichen Java-Methoden: +"Hallo Welt".length +"Hallo Welt".substring(2, 6) +"Hallo Welt".replace("C", "3") -// Zusätzlich gibt es noch extra Scala Methoden -// siehe: scala.collection.immutable.StringOps -"hello world".take(5) -"hello world".drop(5) +// Zusätzlich gibt es noch extra Scala-Methoden +// siehe: scala.collection.immutable.StringOps +"Hallo Welt".take(5) +"Hallo Welt".drop(5) -// String interpolation: prefix "s" + +// String-Interpolation: prefix "s": val n = 45 -s"We have $n apples" // => "We have 45 apples" +s"Wir haben $n Äpfel" // => "Wir haben 45 Äpfel" -// Ausdrücke im Innern von interpolierten Strings gibt es auch +// Ausdrücke im Innern von interpolierten Strings gibt es auch: val a = Array(11, 9, 6) -val n = 100 -s"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old." -s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples." -s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4" +s"Meine zweite Tochter ist ${a(0) - a(2)} Jahre alt." // => "Meine zweite Tochter ist 5 Jahre alt." +s"Wir haben das Doppelte von ${n / 2.0} an Äpfeln." // => "Wir haben das Doppelte von 22.5 an Äpfeln." +s"2 im Quadrat: ${math.pow(2, 2)}" // => "2 im Quadrat: 4" -// Formatierung der interpolierten Strings mit dem prefix "f" +// Formatierung der interpolierten Strings mit dem prefix "f": -f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25" -f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454" +f"5 im Quadrat: ${math.pow(5, 2)}%1.0f" // "5 im Quadrat: 25" +f"Quadratwurzel von 122: ${math.sqrt(122)}%1.4f" // "Quadratwurzel von 122: 11.0454" -// Raw Strings, ignorieren Sonderzeichen. +// Raw Strings ignorieren Sonderzeichen: raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r." -// Manche Zeichen müssen "escaped" werden, z.B. -// ein doppeltes Anführungszeichen in innern eines Strings. +// Manche Zeichen müssen "escaped" werden, z.B. +// ein doppeltes Anführungszeichen im Innern eines Strings: -"They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown"" +"Sie standen vor der \"Rose and Crown\"" // => "Sie standen vor der "Rose and Crown"" -// Dreifache Anführungszeichen erlauben es, dass ein String über mehrere Zeilen geht -// und Anführungszeichen enthalten kann. +// Dreifache Anführungszeichen erlauben es, dass ein String über mehrere Zeilen geht +// und Anführungszeichen enthalten kann: -val html = """<form id="daform"> - <p>Press belo', Joe</p> +val html = """<form id="dieform"> + <p>Drück belo', Joe</p> <input type="submit"> </form>""" - + ///////////////////////////////////////////////// // 2. Funktionen ///////////////////////////////////////////////// -// Funktionen werden so definiert -// -// def functionName(args...): ReturnType = { body... } -// -// Beachte: Es gibt kein return Schlüsselwort. In Scala ist der letzte Ausdruck -// in einer Funktion der Rückgabewert. +// Funktionen werden so definiert: +// +// def functionName(args...): ReturnType = { body... } +// +// Beachte: Es wird hier kein Schlüsselwort "return" verwendet. +// In Scala ist der letzte Ausdruck in einer Funktion der Rückgabewert. def sumOfSquares(x: Int, y: Int): Int = { val x2 = x * x @@ -198,118 +198,118 @@ def sumOfSquares(x: Int, y: Int): Int = { } -// Die geschweiften Klammern können weggelassen werden, wenn -// die Funktion nur aus einem einzigen Ausdruck besteht: +// Die geschweiften Klammern können weggelassen werden, wenn +// die Funktion nur aus einem einzigen Ausdruck besteht: def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y -// Syntax für Funktionsaufrufe: +// Syntax für Funktionsaufrufe: sumOfSquares(3, 4) // => 25 -// In den meisten Fällen (mit Ausnahme von rekursiven Funktionen), können -// Rückgabetypen auch weggelassen werden, da dieselbe Typ Inference, wie bei -// Variablen, auch bei Funktionen greift: +// In den meisten Fällen (mit Ausnahme von rekursiven Funktionen) können +// Rückgabetypen auch weggelassen werden, da dieselbe Typ-Inferenz, wie bei +// Variablen, auch bei Funktionen greift: def sq(x: Int) = x * x // Compiler errät, dass der return type Int ist -// Funktionen können default parameter haben: +// Funktionen können Default-Parameter haben: def addWithDefault(x: Int, y: Int = 5) = x + y addWithDefault(1, 2) // => 3 addWithDefault(1) // => 6 -// Anonyme Funktionen sehen so aus: +// Anonyme Funktionen sehen so aus: (x: Int) => x * x -// Im Gegensatz zu def bei normalen Funktionen, kann bei anonymen Funktionen -// sogar der Eingabetyp weggelassen werden, wenn der Kontext klar ist. -// Beachte den Typ "Int => Int", dies beschreibt eine Funktion, -// welche Int als Parameter erwartet und Int zurückgibt. +// Im Gegensatz zu def bei normalen Funktionen, kann bei anonymen Funktionen +// sogar der Eingabetyp weggelassen werden, wenn der Kontext klar ist. +// Beachte den Typ "Int => Int", dies beschreibt eine Funktion, +// welche Int als Parameter erwartet und Int zurückgibt. val sq: Int => Int = x => x * x -// Anonyme Funktionen benutzt man ganz normal: +// Anonyme Funktionen benutzt man ganz normal: sq(10) // => 100 -// Wenn ein Parameter einer anonymen Funktion nur einmal verwendet wird, -// bietet Scala einen sehr kurzen Weg diesen Parameter zu benutzen, -// indem die Parameter als Unterstrich "_" in der Parameterreihenfolge -// verwendet werden. Diese anonymen Funktionen werden sehr häufig -// verwendet. +// Wenn ein Parameter einer anonymen Funktion nur einmal verwendet wird, +// bietet Scala einen sehr kurzen Weg diesen Parameter zu benutzen, +// indem die Parameter als Unterstrich "_" in der Parameterreihenfolge +// verwendet werden. Diese anonymen Funktionen werden sehr häufig +// verwendet. -val addOne: Int => Int = _ + 1 -val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3) -addOne(5) // => 6 -weirdSum(2, 4) // => 16 +val addOne: Int => Int = _ + 1 +val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3) +addOne(5) // => 6 +weirdSum(2, 4) // => 16 -// Es gibt einen keyword return in Scala. Allerdings ist seine Verwendung -// nicht immer ratsam und kann fehlerbehaftet sein. "return" gibt nur aus -// dem innersten def, welches den return Ausdruck umgibt, zurück. -// "return" hat keinen Effekt in anonymen Funktionen: +// Es gibt ein Schlüsselwort "return" in Scala. Allerdings ist seine Verwendung +// nicht immer ratsam und kann fehlerbehaftet sein. "return" gibt nur aus +// dem innersten def, welches den return Ausdruck umgibt, zurück. +// "return" hat keinen Effekt in anonymen Funktionen: -def foo(x: Int): Int = { - val anonFunc: Int => Int = { z => - if (z > 5) - return z // Zeile macht z zum return Wert von foo - else - z + 2 // Zeile ist der return Wert von anonFunc - } - anonFunc(x) // Zeile ist der return Wert von foo -} +def foo(x: Int): Int = { + val anonFunc: Int => Int = { z => + if (z > 5) + return z // Zeile macht z zum return Wert von foo + else + z + 2 // Zeile ist der return Wert von anonFunc + } + anonFunc(x) // Zeile ist der return Wert von foo +} ///////////////////////////////////////////////// -// 3. Flow Control +// 3. Flusskontrolle ///////////////////////////////////////////////// -// Wertebereiche und Schleifen +// Wertebereiche und Schleifen: 1 to 5 val r = 1 to 5 r.foreach(println) r foreach println (5 to 1 by -1) foreach (println) - -// Scala ist syntaktisch sehr großzügig, Semikolons am Zeilenende -// sind optional, beim Aufruf von Methoden können die Punkte -// und Klammern entfallen und Operatoren sind im Grunde austauschbare Methoden -// while Schleife +// Scala ist syntaktisch sehr großzügig; Semikolons am Zeilenende +// sind optional, beim Aufruf von Methoden können die Punkte +// und Klammern entfallen und Operatoren sind im Grunde austauschbare Methoden. + +// while Schleife: var i = 0 while (i < 10) { println("i " + i); i += 1 } i // i ausgeben, res3: Int = 10 -// Beachte: while ist eine Schleife im klassischen Sinne - -// Sie läuft sequentiell ab und verändert die loop-Variable. -// While in Scala läuft schneller ab als in Java und die o.g. -// Kombinatoren und Zusammenlegungen sind einfacher zu verstehen -// und zu parellelisieren. +// Beachte: while ist eine Schleife im klassischen Sinne - +// Sie läuft sequentiell ab und verändert die loop-Variable. +// "while" in Scala läuft schneller ab als in Java und die o.g. +// Kombinatoren und Zusammenlegungen sind einfacher zu verstehen +// und zu parallelisieren. -// Ein do while Schleife +// Ein do while Schleife do { - println("x ist immer noch weniger wie 10") + println("x ist immer noch weniger als 10") x += 1 } while (x < 10) -// Endrekursionen sind ideomatisch um sich wiederholende -// Dinge in Scala zu lösen. Rekursive Funtionen benötigen explizit einen -// return Typ, der Compiler kann ihn nicht erraten. -// Unit, in diesem Beispiel. +// Endrekursionen sind idiomatisch um sich wiederholende +// Aufgaben in Scala zu lösen. Rekursive Funtionen benötigen explizit einen +// Rückgabe-Typ, der Compiler kann ihn nicht erraten. +// Der Rückgabe-Typ in diesem Beispiel ist Unit: def showNumbersInRange(a: Int, b: Int): Unit = { print(a) @@ -331,7 +331,7 @@ val text = if (x == 10) "yeah" else "nope" ///////////////////////////////////////////////// -// 4. Daten Strukturen (Array, Map, Set, Tuples) +// 4. Datenstrukturen (Array, Map, Set, Tupel) ///////////////////////////////////////////////// // Array @@ -344,11 +344,11 @@ a(21) // Exception // Map - Speichert Key-Value-Paare -val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo") +val m = Map("fork" -> "Gabel", "spoon" -> "Löffel", "knife" -> "Messer") m("fork") m("spoon") m("bottle") // Exception -val safeM = m.withDefaultValue("no lo se") +val safeM = m.withDefaultValue("unbekannt") safeM("bottle") // Set - Speichert Unikate, unsortiert (sortiert -> SortedSet) @@ -359,8 +359,8 @@ s(1) //true val s = Set(1,1,3,3,7) s: scala.collection.immutable.Set[Int] = Set(1, 3, 7) -// Tuple - Speichert beliebige Daten und "verbindet" sie miteinander -// Ein Tuple ist keine Collection. +// Tupel - Speichert beliebige Daten und "verbindet" sie miteinander +// Ein Tupel ist keine Collection. (1, 2) (4, 3, 2) @@ -368,15 +368,15 @@ s: scala.collection.immutable.Set[Int] = Set(1, 3, 7) (a, 2, "three") -// Hier ist der Rückgabewert der Funktion ein Tuple -// Die Funktion gibt das Ergebnis, so wie den Rest zurück. +// Hier ist der Rückgabewert der Funktion ein Tupel +// Die Funktion gibt das Ergebnis sowie den Rest zurück. val divideInts = (x: Int, y: Int) => (x / y, x % y) divideInts(10, 3) -// Um die Elemente eines Tuples anzusprechen, benutzt man diese -// Notation: _._n wobei n der index des Elements ist (Index startet bei 1) +// Um die Elemente eines Tupels anzusprechen, benutzt man diese +// Notation: _._n wobei n der index des Elements ist (Index startet bei 1) val d = divideInts(10, 3) d._1 @@ -388,32 +388,32 @@ d._2 ///////////////////////////////////////////////// /* - Bislang waren alle gezeigten Sprachelemente einfache Ausdrücke, welche zwar - zum Ausprobieren und Lernen in der REPL gut geeignet sind, jedoch in - einem Scala file selten alleine zu finden sind. - Die einzigen Top-Level Konstrukte in Scala sind nämlich: + Bislang waren alle gezeigten Sprachelemente einfache Ausdrücke, welche zwar + zum Ausprobieren und Lernen in der REPL gut geeignet sind, jedoch in + einer Scala-Datei selten alleine zu finden sind. + Die einzigen Top-Level-Konstrukte in Scala sind nämlich: - Klassen (classes) - Objekte (objects) - case classes - traits - Diesen Sprachelemente wenden wir uns jetzt zu. + Diesen Sprachelemente wenden wir uns jetzt zu. */ // Klassen -// Zum Erstellen von Objekten benötigt man eine Klasse, wie in vielen -// anderen Sprachen auch. +// Zum Erstellen von Objekten benötigt man eine Klasse, wie in vielen +// anderen Sprachen auch. -// erzeugt Klasse mit default Konstruktor +// erzeugt Klasse mit default Konstruktor: class Hund scala> val t = new Hund t: Hund = Hund@7103745 -// Der Konstruktor wird direkt hinter dem Klassennamen deklariert. +// Der Konstruktor wird direkt hinter dem Klassennamen deklariert. class Hund(sorte: String) scala> val t = new Hund("Dackel") @@ -421,8 +421,8 @@ t: Hund = Hund@14be750c scala> t.sorte //error: value sorte is not a member of Hund -// Per val wird aus dem Attribut ein unveränderliches Feld der Klasse -// Per var wird aus dem Attribut ein veränderliches Feld der Klasse +// Per val wird aus dem Attribut ein unveränderliches Feld der Klasse +// Per var wird aus dem Attribut ein veränderliches Feld der Klasse class Hund(val sorte: String) scala> val t = new Hund("Dackel") @@ -431,14 +431,14 @@ scala> t.sorte res18: String = Dackel -// Methoden werden mit def geschrieben +// Methoden werden mit def geschrieben -def bark = "Woof, woof!" +def bark = "Wuff, wuff!" -// Felder und Methoden können public, protected und private sein -// default ist public -// private ist nur innerhalb des deklarierten Bereichs sichtbar +// Felder und Methoden können public, protected und private sein +// default ist public +// private ist nur innerhalb des deklarierten Bereichs sichtbar class Hund { private def x = ... @@ -446,8 +446,8 @@ class Hund { } -// protected ist nur innerhalb des deklarierten und aller -// erbenden Bereiche sichtbar +// protected ist nur innerhalb des deklarierten und aller +// erbenden Bereiche sichtbar class Hund { protected def x = ... @@ -457,12 +457,12 @@ class Dackel extends Hund { } // Object -// Wird ein Objekt ohne das Schlüsselwort "new" instanziert, wird das sog. -// "companion object" aufgerufen. Mit dem "object" Schlüsselwort wird so -// ein Objekt (Typ UND Singleton) erstellt. Damit kann man dann eine Klasse -// benutzen ohne ein Objekt instanziieren zu müssen. -// Ein gültiges companion Objekt einer Klasse ist es aber erst dann, wenn -// es genauso heisst und in derselben Datei wie die Klasse definiert wurde. +// Wird ein Objekt ohne das Schlüsselwort "new" instanziert, wird das sog. +// "companion object" aufgerufen. Mit dem "object" Schlüsselwort wird so +// ein Objekt (Typ UND Singleton) erstellt. Damit kann man dann eine Klasse +// verwenden, ohne ein Objekt instanziieren zu müssen. +// Ein gültiges companion Objekt einer Klasse ist es aber erst dann, wenn +// es genauso heißt und in derselben Datei wie die Klasse definiert wurde. object Hund { def alleSorten = List("Pitbull", "Dackel", "Retriever") @@ -470,50 +470,51 @@ object Hund { } // Case classes -// Fallklassen bzw. Case classes sind Klassen die normale Klassen um extra -// Funktionalität erweitern. Mit Case Klassen bekommt man ein paar -// Dinge einfach dazu, ohne sich darum kümmern zu müssen. Z.B. -// ein companion object mit den entsprechenden Methoden, -// Hilfsmethoden wie toString(), equals() und hashCode() und auch noch -// Getter für unsere Attribute (das Angeben von val entfällt dadurch) +// Fallklassen bzw. Case classes sind Klassen, die normale Klassen um +// zusätzliche Funktionalität erweitern. +// Mit Case-Klassen bekommt man ein paar +// Dinge einfach dazu, ohne sich darum kümmern zu müssen. Z.B. +// ein companion object mit den entsprechenden Methoden, +// Hilfsmethoden wie toString(), equals() und hashCode() und auch noch +// Getter für unsere Attribute (das Angeben von val entfällt dadurch) class Person(val name: String) class Hund(val sorte: String, val farbe: String, val halter: Person) -// Es genügt das Schlüsselwort case vor die Klasse zu schreiben. +// Es genügt, das Schlüsselwort case vor die Klasse zu schreiben: case class Person(name: String) case class Hund(sorte: String, farbe: String, halter: Person) -// Für neue Instanzen brauch man kein "new" +// Für neue Instanzen braucht man kein "new": val dackel = Hund("dackel", "grau", Person("peter")) val dogge = Hund("dogge", "grau", Person("peter")) - -// getter + +// getter dackel.halter // => Person = Person(peter) - -// equals + +// equals dogge == dackel // => false - -// copy -// otherGeorge == Person("george", "9876") + +// copy +// otherGeorge == Person("george", "9876") val otherGeorge = george.copy(phoneNumber = "9876") // Traits -// Ähnlich wie Java interfaces, definiert man mit traits einen Objekttyp -// und Methodensignaturen. Scala erlaubt allerdings das teilweise -// implementieren dieser Methoden. Konstruktorparameter sind nicht erlaubt. -// Traits können von anderen Traits oder Klassen erben, aber nur von -// parameterlosen. +// Ähnlich wie Java interfaces, definiert man mit traits einen Objekttyp +// und Methodensignaturen. Scala erlaubt allerdings das teilweise +// Implementieren dieser Methoden. Konstruktorparameter sind nicht erlaubt. +// Traits können von anderen Traits oder Klassen erben, aber nur von +// parameterlosen. trait Hund { def sorte: String @@ -527,22 +528,23 @@ class Bernhardiner extends Hund{ def beissen = false } - -scala> b -res0: Bernhardiner = Bernhardiner@3e57cd70 -scala> b.sorte -res1: String = Bernhardiner -scala> b.bellen -res2: Boolean = true -scala> b.beissen -res3: Boolean = false +scala> b +res0: Bernhardiner = Bernhardiner@3e57cd70 +scala> b.sorte +res1: String = Bernhardiner +scala> b.bellen +res2: Boolean = true +scala> b.beissen +res3: Boolean = false -// Traits können auch via Mixins (Schlüsselwort "with") eingebunden werden +// Ein Trait kann auch als Mixin eingebunden werden. Die Klasse erbt vom +// ersten Trait mit dem Schlüsselwort "extends", während weitere Traits +// mit "with" verwendet werden können. trait Bellen { - def bellen: String = "Woof" + def bellen: String = "Wuff" } trait Hund { def sorte: String @@ -555,17 +557,17 @@ class Bernhardiner extends Hund with Bellen{ scala> val b = new Bernhardiner b: Bernhardiner = Bernhardiner@7b69c6ba scala> b.bellen -res0: String = Woof +res0: String = Wuff ///////////////////////////////////////////////// -// 6. Pattern Matching +// 6. Mustervergleich (Pattern Matching) ///////////////////////////////////////////////// -// Pattern matching in Scala ist ein sehr nützliches und wesentlich -// mächtigeres Feature als Vergleichsfunktionen in Java. In Scala -// benötigt ein case Statement kein "break", ein fall-through gibt es nicht. -// Mehrere Überprüfungen können mit einem Statement gemacht werden. -// Pattern matching wird mit dem Schlüsselwort "match" gemacht. +// Pattern matching in Scala ist ein sehr nützliches und wesentlich +// mächtigeres Feature als Vergleichsfunktionen in Java. In Scala +// benötigt ein case Statement kein "break", ein fall-through gibt es nicht. +// Mehrere Überprüfungen können mit einem Statement gemacht werden. +// Pattern matching wird mit dem Schlüsselwort "match" gemacht. val x = ... x match { @@ -575,19 +577,19 @@ x match { } -// Pattern Matching kann auf beliebige Typen prüfen +// Pattern Matching kann auf beliebige Typen prüfen val any: Any = ... val gleicht = any match { case 2 | 3 | 5 => "Zahl" - case "woof" => "String" + case "wuff" => "String" case true | false => "Boolean" case 45.35 => "Double" case _ => "Unbekannt" } -// und auf Objektgleichheit +// und auf Objektgleichheit def matchPerson(person: Person): String = person match { case Person("George", nummer) => "George! Die Nummer ist " + number @@ -596,31 +598,31 @@ def matchPerson(person: Person): String = person match { } -// Und viele mehr... +// Und viele mehr... val email = "(.*)@(.*)".r // regex def matchEverything(obj: Any): String = obj match { // Werte: - case "Hello world" => "Got the string Hello world" + case "Hallo Welt" => "string Hallo Welt gefunden" // Typen: - case x: Double => "Got a Double: " + x - // Conditions: - case x: Int if x > 10000 => "Got a pretty big number!" - // Case Classes: - case Person(name, number) => s"Got contact info for $name!" + case x: Double => "Double gefunden: " + x + // Bedingungen: + case x: Int if x > 10000 => "Ziemlich große Zahl gefunden!" + // Case-Klassen: + case Person(name, number) => s"Kontaktinformationen für $name gefunden!" // RegEx: - case email(name, domain) => s"Got email address $name@$domain" - // Tuples: - case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c" + case email(name, domain) => s"E-Mail-Adresse $name@$domain gefunden" + // Tupel: + case (a: Int, b: Double, c: String) => s"Tupel gefunden: $a, $b, $c" // Strukturen: - case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c" - // Patterns kann man ineinander schachteln: - case List(List((1, 2, "YAY"))) => "Got a list of list of tuple" + case List(1, b, c) => s"Liste aus drei Elementen gefunden, startend mit 1: 1, $b, $c" + // Pattern kann man ineinander schachteln: + case List(List((1, 2, "YAY"))) => "Liste von Tupeln gefunden" } -// Jedes Objekt mit einer "unapply" Methode kann per Pattern geprüft werden -// Ganze Funktionen können Patterns sein +// Jedes Objekt mit einer "unapply" Methode kann per Pattern geprüft werden. +// Ganze Funktionen können Patterns sein: val patternFunc: Person => String = { case Person("George", number) => s"George's number: $number" @@ -629,58 +631,58 @@ val patternFunc: Person => String = { ///////////////////////////////////////////////// -// 37. Higher-order functions +// 7. "Higher-order"-Funktionen ///////////////////////////////////////////////// -Scala erlaubt, das Methoden und Funktion wiederum Funtionen und Methoden -als Aufrufparameter oder Return Wert verwenden. Diese Methoden heissen -higher-order functions -Es gibt zahlreiche higher-order functions nicht nur für Listen, auch für -die meisten anderen Collection Typen, sowie andere Klassen in Scala -Nennenswerte sind: -"filter", "map", "reduce", "foldLeft"/"foldRight", "exists", "forall" +// Scala erlaubt, dass Methoden und Funktionen wiederum Funtionen und Methoden +// als Aufrufparameter oder Rückgabewert verwenden. Diese Methoden heißen +// higher-order functions. +// Es gibt zahlreiche higher-order-Funtionen nicht nur für Listen, auch für +// die meisten anderen Collection-Typen, sowie andere Klassen in Scala. +// Nennenswerte sind: +// "filter", "map", "reduce", "foldLeft"/"foldRight", "exists", "forall" -## List +// List -def isGleichVier(a:Int) = a == 4 +def istGleichVier(a:Int) = a == 4 val list = List(1, 2, 3, 4) val resultExists4 = list.exists(isEqualToFour) -## map -// map nimmt eine Funktion und führt sie auf jedem Element aus und erzeugt -// eine neue Liste - -// Funktion erwartet ein Int und returned ein Int +// map +// map nimmt eine Funktion und führt sie auf jedem Element aus und erzeugt +// eine neue Liste -val add10: Int => Int = _ + 10 +// Funktion erwartet einen Int und gibt einen Int zurück: +val add10: Int => Int = _ + 10 -// add10 wird auf jedes Element angewendet + +// add10 wird auf jedes Element angewendet: List(1, 2, 3) map add10 // => List(11, 12, 13) -// Anonyme Funktionen können anstatt definierter Funktionen verwendet werden +// Anonyme Funktionen können anstatt definierter Funktionen verwendet werden: List(1, 2, 3) map (x => x + 10) -// Der Unterstrich wird anstelle eines Parameters einer anonymen Funktion -// verwendet. Er wird an die Variable gebunden. +// Der Unterstrich wird anstelle eines Parameters einer anonymen Funktion +// verwendet. Er wird an die Variable gebunden: List(1, 2, 3) map (_ + 10) -// Wenn der anonyme Block und die Funtion beide EIN Argument erwarten, -// kann sogar der Unterstrich weggelassen werden. +// Wenn der anonyme Block und die Funktion beide EIN Argument erwarten, +// kann sogar der Unterstrich weggelassen werden. List("Dom", "Bob", "Natalia") foreach println // filter -// filter nimmt ein Prädikat (eine Funktion von A -> Boolean) und findet -// alle Elemente die auf das Prädikat passen +// filter nimmt ein Prädikat (eine Funktion von A -> Boolean) und findet +// alle Elemente, die auf das Prädikat passen: List(1, 2, 3) filter (_ > 2) // => List(3) case class Person(name: String, age: Int) @@ -691,19 +693,19 @@ List( // reduce -// reduce nimmt zwei Elemente und kombiniert sie zu einem Element, -// und zwar solange bis nur noch ein Element da ist. +// reduce nimmt zwei Elemente und kombiniert sie zu einem Element, +// und zwar so lange, bis nur noch ein Element da ist. // foreach -// foreach gibt es für einige Collections +// foreach gibt es für einige Collections val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100) aListOfNumbers foreach (x => println(x)) aListOfNumbers foreach println // For comprehensions -// Eine for-comprehension definiert eine Beziehung zwischen zwei Datensets. -// Dies ist keine for-Schleife. +// Eine for-comprehension definiert eine Beziehung zwischen zwei Datensets. +// Dies ist keine for-Schleife. for { n <- s } yield sq(n) val nSquared2 = for { n <- s } yield sq(n) @@ -715,108 +717,108 @@ for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared // 8. Implicits ///////////////////////////////////////////////// -// **ACHTUNG:** +// **ACHTUNG:** // Implicits sind ein sehr mächtiges Sprachfeature von Scala. -// Es sehr einfach -// sie falsch zu benutzen und Anfänger sollten sie mit Vorsicht oder am -// besten erst dann benutzen, wenn man versteht wie sie funktionieren. -// Dieses Tutorial enthält Implicits, da sie in Scala an jeder Stelle -// vorkommen und man auch mit einer Lib die Implicits benutzt nichts sinnvolles -// machen kann. -// Hier soll ein Grundverständnis geschaffen werden, wie sie funktionieren. +// Es sehr einfach, +// sie falsch zu benutzen und Anfänger sollten sie mit Vorsicht oder am +// besten erst dann benutzen, wenn sie verstehen, wie sie funktionieren. +// Dieses Tutorial enthält Implicits, da sie in Scala an jeder Stelle +// vorkommen und man auch mit einer Bibliothek, die Implicits benutzt, sonst +// nichts sinnvolles machen kann. +// Hier soll ein Grundverständnis geschaffen werden, wie sie funktionieren. -// Mit dem Schlüsselwort implicit können Methoden, Werte, Funktion, Objekte -// zu "implicit Methods" werden. +// Mit dem Schlüsselwort implicit können Methoden, Werte, Funktion, Objekte +// zu "implicit Methods" werden. implicit val myImplicitInt = 100 implicit def myImplicitFunction(sorte: String) = new Hund("Golden " + sorte) -// implicit ändert nicht das Verhalten eines Wertes oder einer Funktion +// implicit ändert nicht das Verhalten eines Wertes oder einer Funktion myImplicitInt + 2 // => 102 myImplicitFunction("Pitbull").sorte // => "Golden Pitbull" -// Der Unterschied ist, dass diese Werte ausgewählt werden können, wenn ein -// anderer Codeteil einen implicit Wert benötigt, zum Beispiel innerhalb von -// implicit Funktionsparametern - -// Diese Funktion hat zwei Parameter: einen normalen und einen implicit +// Der Unterschied ist, dass diese Werte ausgewählt werden können, wenn ein +// anderer Codeteil einen implicit Wert benötigt, zum Beispiel innerhalb von +// implicit Funktionsparametern + +// Diese Funktion hat zwei Parameter: einen normalen und einen implicit def sendGreetings(toWhom: String)(implicit howMany: Int) = - s"Hello $toWhom, $howMany blessings to you and yours!" + s"Hallo $toWhom, $howMany Segenswünsche für Sie und Ihre Angehörigen!" + +// Werden beide Parameter gefüllt, verhält sich die Funktion wie erwartet -// Werden beide Parameter gefüllt, verhält sich die Funktion wie erwartet +sendGreetings("John")(1000) // => "Hallo John, 1000 Segenswünsche für Sie und Ihre Angehörigen!" -sendGreetings("John")(1000) // => "Hello John, 1000 blessings to you and yours!" +// Wird der implicit-Parameter jedoch weggelassen, wird ein anderer +// implicit-Wert vom gleichen Typ genommen. Der Compiler sucht im +// lexikalischen Scope und im companion object nach einem implicit-Wert, +// der vom Typ passt, oder nach einer implicit-Methode, mit der er in den +// geforderten Typ konvertieren kann. -// Wird der implicit Parameter jedoch weggelassen, wird ein anderer -// implicit Wert vom gleichen Typ genommen. Der Compiler sucht im -// lexikalischen Scope und im companion object nach einem implicit Wert, -// der vom Typ passt, oder nach einer implicit Methode mit der er in den -// geforderten Typ konvertieren kann. - -// Hier also: "myImplicitInt", da ein Int gesucht wird +// Hier also: "myImplicitInt", da ein Int gesucht wird -sendGreetings("Jane") // => "Hello Jane, 100 blessings to you and yours!" +sendGreetings("Jane") // => "Hallo Jane, 100 Segenswünsche für Sie und Ihre Angehörigen!" -// bzw. "myImplicitFunction" -// Der String wird erst mit Hilfe der Funktion in Hund konvertiert, und -// dann wird die Methode aufgerufen +// bzw. "myImplicitFunction" +// Der String wird erst mit Hilfe der Funktion in Hund konvertiert, +// dann wird die Methode aufgerufen: "Retriever".sorte // => "Golden Retriever" ///////////////////////////////////////////////// -// 19. Misc +// 9. Sonstiges ///////////////////////////////////////////////// // Importe import scala.collection.immutable.List -// Importiere alle Unterpackages +// Importiere alle Unterpackages import scala.collection.immutable._ -// Importiere verschiedene Klassen mit einem Statement +// Importiere verschiedene Klassen mit einem Statement import scala.collection.immutable.{List, Map} -// Einen Import kann man mit '=>' umbenennen +// Einen Import kann man mit '=>' umbenennen import scala.collection.immutable.{List => ImmutableList} -// Importiere alle Klasses, mit Ausnahem von.... -// Hier ohne: Map and Set: +// Importiere alle Klasses, mit Ausnahem von.... +// Hier ohne: Map and Set: import scala.collection.immutable.{Map => _, Set => _, _} -// Main +// Main object Application { def main(args: Array[String]): Unit = { - // Sachen kommen hierhin + // Zeugs hier rein. } } // I/O -// Eine Datei Zeile für Zeile lesen +// Eine Datei Zeile für Zeile lesen import scala.io.Source for(line <- Source.fromFile("myfile.txt").getLines()) println(line) -// Eine Datei schreiben +// Eine Datei schreiben val writer = new PrintWriter("myfile.txt") writer.write("Schreibe Zeile" + util.Properties.lineSeparator) @@ -825,13 +827,13 @@ writer.close() ``` -## Weiterführende Hinweise +## Weiterführende Hinweise -// DE +### DE * [Scala Tutorial](https://scalatutorial.wordpress.com) * [Scala Tutorial](http://scalatutorial.de) -// EN +### EN * [Scala for the impatient](http://horstmann.com/scala/) * [Twitter Scala school](http://twitter.github.io/scala_school/) * [The scala documentation](http://docs.scala-lang.org/) diff --git a/de-de/swift-de.html.markdown b/de-de/swift-de.html.markdown index b58a72d3..08f72a35 100644 --- a/de-de/swift-de.html.markdown +++ b/de-de/swift-de.html.markdown @@ -440,13 +440,13 @@ if let circle = myEmptyCircle { // Wie Klassen auch können sie Methoden haben enum Suit { - case Spades, Hearts, Diamonds, Clubs + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } @@ -455,35 +455,35 @@ enum Suit { // Enum-Werte können vereinfacht geschrieben werden, es muss nicht der Enum-Typ // genannt werden, wenn die Variable explizit deklariert wurde -var suitValue: Suit = .Hearts +var suitValue: Suit = .hearts // Nicht-Integer-Enums brauchen direkt zugewiesene "Rohwerte" enum BookName: String { - case John = "John" - case Luke = "Luke" + case john = "John" + case luke = "Luke" } -print("Name: \(BookName.John.rawValue)") +print("Name: \(BookName.john.rawValue)") // Enum mit assoziierten Werten enum Furniture { // mit Int assoziiert - case Desk(height: Int) + case desk(height: Int) // mit String und Int assoziiert - case Chair(String, Int) - + case chair(String, Int) + func description() -> String { switch self { - case .Desk(let height): + case .desk(let height): return "Desk with \(height) cm" - case .Chair(let brand, let height): + case .chair(let brand, let height): return "Chair of \(brand) with \(height) cm" } } } -var desk: Furniture = .Desk(height: 80) +var desk: Furniture = .desk(height: 80) print(desk.description()) // "Desk with 80 cm" -var chair = Furniture.Chair("Foo", 40) +var chair = Furniture.chair("Foo", 40) print(chair.description()) // "Chair of Foo with 40 cm" diff --git a/dynamic-programming.html.markdown b/dynamic-programming.html.markdown index 7df367e7..4db8e92e 100644 --- a/dynamic-programming.html.markdown +++ b/dynamic-programming.html.markdown @@ -26,7 +26,7 @@ The Longest Increasing Subsequence problem is to find the longest increasing sub First of all we have to find the value of the longest subsequences(LSi) at every index i with last element of sequence being ai. Then largest LSi would be the longest subsequence in the given sequence. To begin LSi is assigned to be one since ai is element of the sequence(Last element). Then for all `j` such that `j<i` and `aj<ai`, we find Largest LSj and add it to LSi. Then algorithm take *O(n2)* time. Pseudo-code for finding the length of the longest increasing subsequence: -This algorithms complexity could be reduced by using better data structure rather than array. Storing predecessor array and variable like largest_sequences_so_far and its index would save a lot time. +This algorithms complexity could be reduced by using better data structure rather than array. Storing predecessor array and variable like `largest_sequences_so_far` and its index would save a lot time. Similar concept could be applied in finding longest path in Directed acyclic graph. diff --git a/edn.html.markdown b/edn.html.markdown index ca04df89..f47853f0 100644 --- a/edn.html.markdown +++ b/edn.html.markdown @@ -3,13 +3,16 @@ language: edn filename: learnedn.edn contributors: - ["Jason Yeo", "https://github.com/jsyeo"] + - ["Jonathan D Johnston", "https://github.com/jdjohnston"] --- Extensible Data Notation (EDN) is a format for serializing data. -The notation is used internally by Clojure to represent programs. It is also -used as a data transfer format like JSON. Though it is more commonly used in -Clojure, there are implementations of EDN for many other languages. +EDN is a subset of the syntax used by Clojure. Reading data defined by EDN is +safer than that defined by the full Clojure syntax, especially from untrusted +sources. EDN is restricted to data, no code. It is similar in intent to JSON. +Though it is more commonly used in Clojure, there are implementations of EDN +for many other languages. The main benefit of EDN over JSON and YAML is that it is extensible. We will see how it is extended later on. @@ -32,7 +35,7 @@ false "hungarian breakfast" "farmer's cheesy omelette" -; Characters are preceeded by backslashes +; Characters are preceded by backslashes \g \r \a \c \e ; Keywords start with a colon. They behave like enums. Kind of @@ -41,13 +44,13 @@ false :cheese :olives -; Symbols are used to represent identifiers. They start with #. -; You can namespace symbols by using /. Whatever preceeds / is -; the namespace of the name. -#spoon -#kitchen/spoon ; not the same as #spoon -#kitchen/fork -#github/fork ; you can't eat with this +; Symbols are used to represent identifiers. +; You can namespace symbols by using /. Whatever precedes / is +; the namespace of the symbol. +spoon +kitchen/spoon ; not the same as spoon +kitchen/fork +github/fork ; you can't eat with this ; Integers and floats 42 @@ -81,22 +84,26 @@ false #MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10} -; Let me explain this with a clojure example. Suppose I want to transform that +; Let me explain this with a Clojure example. Suppose I want to transform that ; piece of EDN into a MenuItem record. (defrecord MenuItem [name rating]) -; To transform EDN to clojure values, I will need to use the built in EDN -; reader, edn/read-string +; defrecord defined, among other things, map->MenuItem which will take a map +; of field names (as keywords) to values and generate a user.MenuItem record -(edn/read-string "{:eggs 2 :butter 1 :flour 5}") +; To transform EDN to Clojure values, I will need to use the built-in EDN +; reader, clojure.edn/read-string + +(clojure.edn/read-string "{:eggs 2 :butter 1 :flour 5}") ; -> {:eggs 2 :butter 1 :flour 5} -; To transform tagged elements, define the reader function and pass a map -; that maps tags to reader functions to edn/read-string like so +; To transform tagged elements, pass to clojure.edn/read-string an option map +; with a :readers map that maps tag symbols to data-reader functions, like so -(edn/read-string {:readers {'MyYelpClone/MenuItem map->menu-item}} - "#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}") +(clojure.edn/read-string + {:readers {'MyYelpClone/MenuItem map->MenuItem}} + "#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}") ; -> #user.MenuItem{:name "eggs-benedict", :rating 10} ``` diff --git a/elisp.html.markdown b/elisp.html.markdown index bda5f00f..518ad985 100644 --- a/elisp.html.markdown +++ b/elisp.html.markdown @@ -9,8 +9,6 @@ filename: learn-emacs-lisp.el ```scheme ;; This gives an introduction to Emacs Lisp in 15 minutes (v0.2d) ;; -;; Author: Bastien / @bzg2 / https://bzg.fr -;; ;; First make sure you read this text by Peter Norvig: ;; http://norvig.com/21-days.html ;; @@ -303,7 +301,7 @@ filename: learn-emacs-lisp.el (hello-to-bonjour) -;; Let's colorize the names: +;; Let's boldify the names: (defun boldify-names () (switch-to-buffer-other-window "*test*") @@ -344,13 +342,4 @@ filename: learn-emacs-lisp.el ;; ;; To read an online introduction to Emacs Lisp: ;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html - -;; Thanks to these people for their feedback and suggestions: -;; - Wes Hardaker -;; - notbob -;; - Kevin Montuori -;; - Arne Babenhauserheide -;; - Alan Schmitt -;; - LinXitoW -;; - Aaron Meurer ``` diff --git a/elixir.html.markdown b/elixir.html.markdown index 9dfffc41..a74baa38 100644 --- a/elixir.html.markdown +++ b/elixir.html.markdown @@ -4,6 +4,7 @@ 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"] filename: learnelixir.ex --- @@ -127,7 +128,8 @@ rem(10, 3) #=> 1 # These operators expect a boolean as their first argument. true and true #=> true false or true #=> true -# 1 and true #=> ** (ArgumentError) argument error +# 1 and true +#=> ** (BadBooleanError) expected a boolean on left-side of "and", got: 1 # Elixir also provides `||`, `&&` and `!` which accept arguments of any type. # All values except `false` and `nil` will evaluate to true. diff --git a/elm.html.markdown b/elm.html.markdown index 99c23980..ad80adc9 100644 --- a/elm.html.markdown +++ b/elm.html.markdown @@ -72,8 +72,8 @@ List.head [] -- Nothing -- Access the elements of a pair with the first and second functions. -- (This is a shortcut; we'll come to the "real way" in a bit.) -fst ("elm", 42) -- "elm" -snd ("elm", 42) -- 42 +Tuple.first ("elm", 42) -- "elm" +Tuple.second ("elm", 42) -- 42 -- The empty tuple, or "unit", is sometimes used as a placeholder. -- It is the only value of its type, also called "Unit". @@ -286,7 +286,7 @@ leftmostElement tree = -- Put this at the top of the file. If omitted, you're in Main. module Name where --- By default, everything is exported. You can specify exports explicity. +-- By default, everything is exported. You can specify exports explicitly. module Name (MyType, myValue) where -- One common pattern is to export a union type but not its tags. This is known diff --git a/es-es/amd-es.html.markdown b/es-es/amd-es.html.markdown index 7a59ddd6..40aa6647 100644 --- a/es-es/amd-es.html.markdown +++ b/es-es/amd-es.html.markdown @@ -190,7 +190,7 @@ Para usar el fichero creado en producción, simplemente intercambia `data-main`: Un increíblemente detallado [resumen de opciones de generación](https://github.com/jrburke/r.js/blob/master/build/example.build.js) está disponible en el repositorio de GitHub. -### Tópicos no cubiertos en este tutorial +### Temas no cubiertos en este tutorial * [Cargador de plugins / transformaciones](http://requirejs.org/docs/plugins.html) * [Cargando y exportando estilos CommonJS](http://requirejs.org/docs/commonjs.html) * [Configuración avanzada](http://requirejs.org/docs/api.html#config) diff --git a/es-es/asciidoc-es.html.markdown b/es-es/asciidoc-es.html.markdown new file mode 100644 index 00000000..6e357915 --- /dev/null +++ b/es-es/asciidoc-es.html.markdown @@ -0,0 +1,134 @@ +--- +language: asciidoc +contributors: + - ["Ryan Mavilia", "http://unoriginality.rocks/"] +translators: + - ["Abel Salgado Romero", "https://twitter.com/abelsromero"] +lang: es-es +filename: asciidoc-es.md +--- + +AsciiDoc es un lenguaje de marcas similar a Markdown que puede ser usado para cualquier uso, desde libros a blogs. +Creado en 2002 por Stuart Rackham, el lenguaje es simple pero permite un gran nivel de personalización. + +Cabecera de documento + +La cabecera es opcional y no puede contener lineas vacías. Debe estar separada del contenido por al menos una línea en blanco. + +Solo título + +``` += Título de documento + +Primer contenido del documento. +``` + +Título y autor + +``` += Título del documento +Nombre Apellido(s) <nombre.apellido@learnxinyminutes.com> + +Inicio de este documento. +``` + +Múltiples autores + +``` += Título del documento +John Doe <john@go.com>; Jane Doe<jane@yo.com>; Black Beard <beardy@pirate.com> + +Inicio de un documento con múltiples autores. +``` + +Linea de versión (requiere línea de autor) + +``` += Título del documento V1 +Potato Man <chip@crunchy.com> +v1.0, 2016-01-13 + +Este artículo sobre patatas fritas será genial. +``` + +Párrafo + +``` +No necesitas nada especial para un párrafo. + +Inserta una línea vacía entre cada párrafo para separarlos. + +Para insertar un salto de línea, solo añade un + +y ya lo tienes! +``` + +Dando formato al texto + +``` +_guión bajo para cursiva_ +*asteriscos para negrita* +*_combínalos y verás_* +`usa comillas invertidas para monospace` +`*combina para negrita monospace*` +``` + +Títulos de sección + +``` += Nivel 0 (úsalo solo para el título del documento) + +== Nivel 1 <h2> + +=== Nivel 2 <h3> + +==== Nivel 3 <h4> + +===== Nivel 4 <h5> +``` + +Listas + +Para crear una lista sin orden usa asteriscos. + +``` +* foo +* bar +* baz +``` + +Para crear una lista numerada usa puntos. + +``` +. item 1 +. item 2 +. item 3 +``` + +Puedes crear hasta 5 subniveles en las listas añadiendo asteriscos o puntos. + +``` +* foo 1 +** foo 2 +*** foo 3 +**** foo 4 +***** foo 5 + +. foo 1 +.. foo 2 +... foo 3 +.... foo 4 +..... foo 5 +``` + +## Referencias + +Existen dos herramientas para procesar documentación en AsciiDoc: + +1. [AsciiDoc](http://asciidoc.org/): implementación original para Python, disponible en las principales distribuciones Linux. Versión estable actualmente en modo mantenimiento. +2. [Asciidoctor](http://asciidoctor.org/): implementación alternativa para Ruby, usable también desde Java y JavaScript. Implementación completa en evolución, su objetivo es ampliar AsciiDoc con nuevas funcionalidades y conversores de salida. + +Los siguientes enlaces pertenecen a `Asciidoctor` (documentación en inglés): + +* [Comparación de sintaxis Markdown - AsciiDoc](http://asciidoctor.org/docs/user-manual/#comparison-by-example): comparativa de elements comunes entre Markdown y AsciiDoc. +* [Primeros pasos](http://asciidoctor.org/docs/#get-started-with-asciidoctor): manuales de instalación e inicio para convertir documentos simples. +* [Manual de usuario de Asciidoctor](http://asciidoctor.org/docs/user-manual/): referencia completa en un único documento, contiene ejemplos, guías de herramientas, etc. diff --git a/es-es/awk-es.html.markdown b/es-es/awk-es.html.markdown new file mode 100644 index 00000000..0516ea92 --- /dev/null +++ b/es-es/awk-es.html.markdown @@ -0,0 +1,362 @@ +--- +language: awk +filename: learnawk-es.awk +contributors: + - ["Marshall Mason", "http://github.com/marshallmason"] +translators: + - ["Hugo Guillén-Ramírez", "http://github.com/HugoGuillen"] +lang: es-es +--- + +AWK es una herramienta estándar en cada sistema UNIX compatible con POSIX. +Es como un Perl restringido, perfecto para tareas de procesamiento de texto y +otras necesidades de scripting. Tiene una sintaxis similar a C, pero sin +puntos y comas, manejo manual de memoria y tipado estático. Puedes llamarlo +desde un script de shell o usarlo como un lenguaje stand-alone para scripting. + +¿Por qué elegir AWK sobre Perl? Principalmente, porque AWK es parte de UNIX. +Siempre puedes contar con él, mientras que el futuro de Perl está en duda. AWK +es más fácil de leer que Perl. Para scripts sencillos de procesamiento de texto, +particularmente si es para leer archivos línea a línea y dividir por +delimitadores, probablemente AWK es la herramienta correcta para el trabajo. + +```awk +#!/usr/bin/awk -f + +# Los comentarios tienen este aspecto. + +# Los programas AWK son una colección de patrones y acciones. El patrón más +# importante es BEGIN. Las acciones van en bloques delimitados por llaves. + +BEGIN { + + # BEGIN correrá al inicio del programa. Es donde pones todo el código + # preliminar antes de procesar los archivos de texto. Si no tienes archivos + # de texto, piensa en BEGIN como el punto de entrada principal del script. + + # Las variables son globales. Asígnalas o úsalas sin declararlas. + count = 0 + + # Los operadores son justo como en C (y amigos). + a = count + 1 + b = count - 1 + c = count * 1 + d = count / 1 + e = count % 1 # módulo + f = count ^ 1 # exponenciación + + a += 1 + b -= 1 + c *= 1 + d /= 1 + e %= 1 + f ^= 1 + + # Incremento y decremento en uno + a++ + b-- + + # Como un operador prefijo, regresa el valor modificado + ++a + --b + + # Nota que no hay puntación para terminar las instrucciones + + # Instrucciones de control + if (count == 0) + print "Iniciando count en 0" + else + print "Eh?" + + # O puedes usar el operador ternario + print (count == 0) ? "Iniciando count en 0" : "Eh?" + + # Bloques formados por múltiples líneas usan llaves + while (a < 10) { + print "La concatenación de strings se hace " " con series " + print " de" " strings separados por espacios" + print a + + a++ + } + + for (i = 0; i < 10; i++) + print "El viejo confiable ciclo for" + + # Los operaciones de comparación son estándar... + a < b # Menor que + a <= b # Menor o igual que + a != b # No igual + a == b # Igual + a > b # Mayor que + a >= b # Mayor o igual que + + # ...así como los operadores lógicos + a && b # AND + a || b # OR + + # Además están las expresiones regulares + if ("foo" ~ "^fo+$") + print "Fooey!" + if ("boo" !~ "^fo+$") + print "Boo!" + + # Arrays + arr[0] = "foo" + arr[1] = "bar" + # Desafortunadamente no hay otra manera de inicializar un array. + # Tienes que inicializar cada posición del array. + + # También hay arrays asociativos + assoc["foo"] = "bar" + assoc["bar"] = "baz" + + # Y arrays multidimensionales con limitaciones que no mencionaré aquí + multidim[0,0] = "foo" + multidim[0,1] = "bar" + multidim[1,0] = "baz" + multidim[1,1] = "boo" + + # Puedes probar pertenencia a un array + if ("foo" in assoc) + print "Fooey!" + + # También puedes usar el operador 'in' para iterar las claves de un array + for (key in assoc) + print assoc[key] + + # La terminal es un array especial llamado ARGV + for (argnum in ARGV) + print ARGV[argnum] + + # Puedes eliminar elementos de un array. + # Esto es útil para prevenir que AWK suponga que algunos argumentos + # son archivos por procesar. + delete ARGV[1] + + # El número de argumentos de la terminal está en la variable ARGC + print ARGC + + # AWK tiene tres categorías de funciones incluidas. + # Demostraré esas funciones posteriormente. + + return_value = arithmetic_functions(a, b, c) + string_functions() + io_functions() +} + +# Así se define una función +function arithmetic_functions(a, b, c, localvar) { + + # Probablemente la parte más molesta de AWK es que no hay variables locales + # Todo es global. No es problema en scripts pequeños, pero sí para + # scripts más grandes. + + # Hay un work-around (mmm... hack). Los argumentos de las funciones son + # locales para la función, y AWK permite definir más argumentos de función + # de los que necesita, por lo que define las variables locales en la + # declaración como en la función de arriba. Como convención, agrega + # espacios en blanco para distinguir los parámetros de la función de las + # variables locales. En este ejemplo, a, b y c son parámetros y localvar es una + # variable local. + + # Ahora, a demostrar las funciones aritméticas + + # La mayoría de las implementaciones de AWK tienen funciones + # trigonométricas estándar + localvar = sin(a) + localvar = cos(a) + localvar = atan2(b, a) # arcotangente de b / a + + # Y cosas logarítmicas + localvar = exp(a) + localvar = log(a) + + # Raíz cuadrada + localvar = sqrt(a) + + # Trucar un flotante a entero + localvar = int(5.34) # localvar => 5 + + # Números aleatorios + srand() # La semilla es el argumento. Por defecto usa el tiempo del sistema + localvar = rand() # Número aleatorio entre 0 y 1. + + # Y aquí se regresa el valor + return localvar +} + +function string_functions( localvar, arr) { + + # AWK tiene algunas funciones para procesamiento de strings, + # y muchas dependen fuertemente en expresiones regulares. + + # Buscar y remplazar, primer instancia (sub) o todas las instancias (gsub) + # Ambas regresan el número de matches remplazados. + 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" + + # Buscar una cadena que haga match con una expresión regular + # index() hace lo mismo, pero no permite expresiones regulares + match(localvar, "t") # => 4, dado que 't' es el cuarto caracter + + # Separar con base en un delimitador + split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"] + + # Otras funciones útiles + 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) { + + # Ya has visto print + print "Hello world" + + # También hay printf + printf("%s %d %d %d\n", "Testing", 1, 2, 3) + + # AWK no tiene handles de archivos en sí mismo. Automáticamente abrirá un + # handle de archivo cuando use algo que necesite uno. El string que usaste + # para esto puede ser tratada como un handle de archivo para propósitos de I/O. + # Esto lo hace similar al scripting de shell: + + print "foobar" >"/tmp/foobar.txt" + + # Ahora el string "/tmp/foobar.txt" es un handle. Puedes cerrarlo: + close("/tmp/foobar.txt") + + # Aquí está como correr algo en el shell + system("echo foobar") # => muestra foobar + + # Lee una línea de la entrada estándar (stdin) y lo guarda en localvar + getline localvar + + # Lee una línea desde un pipe + "echo foobar" | getline localvar # localvar => "foobar" + close("echo foobar") + + # Lee una línea desde un archivo y la guarda en localvar + getline localvar <"/tmp/foobar.txt" + close("/tmp/foobar.txt") +} + +# Como dije al inicio, los programas en AWK son una colección de patrones y +# acciones. Ya conociste el patrón BEGIN. otros patrones sólo se usan si estás +# procesando líneas desde archivos o stdin. + +# Cuando pasas argumentos a AWK, son tratados como nombres de archivos a +# procesar. Los va a procesar todos, en orden. Imagínalos como un ciclo for +# implícito, iterando sobre las líneas de estos archivos. Estos patrones y +# acciones son como instrucciones switch dentro del ciclo. + +/^fo+bar$/ { + + # Esta acción se ejecutará por cada línea que haga match con la expresión + # regular /^fo+bar$/, y será saltada por cualquier línea que no haga match. + # Vamos a sólo mostrar la línea: + + print + + # ¡Wow, sin argumento! Eso es porque print tiene uno por defecto: $0. + # $0 es el nombre de la línea actual que se está procesando. + # Se crea automáticamente para ti. + + # Probablemente puedas adivinar que hay otras variables $. Cada línea es + # separada implícitamente antes de que se llame cada acción, justo como lo + # hace shell. Y, como shell, cada campo puede ser accesado con $. + + # Esto mostrará el segundo y cuarto campos de la línea + print $2, $4 + + # AWK automáticamente define muchas otras variables que te ayudan a + # inspeccionar y procesar cada línea. La más importante es NF + + # Imprime el número de campos de esta línea + print NF + + # Imprime el último campo de esta línea + print $NF +} + +# Cada patrón es realmente un prueba de verdadero/falso. La expresión regular +# en el último patrón también es una prueba verdadero/falso, pero parte de eso +# estaba oculto. Si no le das un string a la prueba, supondrá $0, la línea que +# se está procesando. La versión completa de esto es: + +$0 ~ /^fo+bar$/ { + print "Equivalente al último patrón" +} + +a > 0 { + # Esto se ejecutará una vez por línea, mientras a sea positivo +} + +# Y ya te das una idea. Procesar archivos de texto, leyendo una línea a la vez, +# y haciendo algo con ella, particularmente separando en un deliminator, es tan +# común en UNIX que AWK es un lenguaje de scripting que hace todo eso por ti +# sin que tengas que pedirlo. Basta con escribir los patrones y acciones +# basados en lo que esperas de la entrada y lo quieras quieras hacer con ella. + +# Aquí está un ejemplo de un script simple, para lo que AWK es perfecto. +# El script lee un nombre de stdin y muestra el promedio de edad para todos los +# que tengan ese nombre. Digamos que como argumento pasamos el nombre de un +# archivo con este contenido: +# +# Bob Jones 32 +# Jane Doe 22 +# Steve Stevens 83 +# Bob Smith 29 +# Bob Barker 72 +# +# Éste es el script: + +BEGIN { + + # Primero, pedir al usuario el nombre + print "¿Para qué nombre quieres el promedio de edad?" + + # Recuperar una línea de stdin, no de archivos en la línea de comandos + getline name <"/dev/stdin" +} + +# Ahora, hacer match con cada línea cuyo primer campo es el nombre dado +$1 == name { + + # Aquí dentro tenemos acceso a variables útiles precargadas: + # $0 es toda la línea + # $3 es el tercer campo, la edad, que es lo que nos interesa + # NF es el número de campos, que debe ser 3 + # NR es el número de registros (líneas) vistos hasta ahora + # FILENAME es el nombre del archivo que está siendo procesado + # FS es el campo separador, " " en este caso + # Y muchas más que puedes conocer ejecutando 'man awk' en la terminal. + + # Llevar el registro de la suma y cuantas líneas han hecho match. + sum += $3 + nlines++ +} + +# Otro patrón especial es END. Va a ejecutarse después de procesar todos los +# archivos de texto. A diferencia de BEGIN, sólo se ejecuta si le das dado una +# entrada a procesar. Se ejecutará después de que todos los archivos hayan sido +# leídos y procesados según las reglas y acciones que programaste. El propósito +# es usualmente para mostrar un reporte final, o hacer algo con el agregado de +# los datos que has acumulado durante la ejecución del script. + +END { + if (nlines) + print "La edad promedio para " name " es " sum / nlines +} + +``` +Más información: + +* [Tutorial de AWK](http://www.grymoire.com/Unix/Awk.html) +* [Página man de AWK](https://linux.die.net/man/1/awk) +* [La guía del usuario de GNU Awk](https://www.gnu.org/software/gawk/manual/gawk.html): GNU Awk se encuentra en la mayoría de los sistemas Linux. diff --git a/es-es/bf-es.html.markdown b/es-es/bf-es.html.markdown index c93b8c3a..df1ae2e7 100644 --- a/es-es/bf-es.html.markdown +++ b/es-es/bf-es.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: bf-es.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/es-es/csharp-es.html.markdown b/es-es/csharp-es.html.markdown index ef26d8ce..72a0f90c 100644 --- a/es-es/csharp-es.html.markdown +++ b/es-es/csharp-es.html.markdown @@ -1,12 +1,13 @@ --- -language: c# +language: C#(C Sharp) +filename: LearnCSharp-es.cs contributors: - ["Irfan Charania", "https://github.com/irfancharania"] - ["Max Yankov", "https://github.com/golergka"] translators: - - ["Olfran Jiménez", "https://twitter.com/neslux"] -filename: LearnCSharp-es.cs + - ["Olfran Jiménez", "https://twitter.com/neslux"] lang: es-es + --- C# es un lenguaje orientado a objetos elegante y de tipado seguro que diff --git a/es-es/dynamic-programming-es.html.markdown b/es-es/dynamic-programming-es.html.markdown index 11930653..e613b722 100644 --- a/es-es/dynamic-programming-es.html.markdown +++ b/es-es/dynamic-programming-es.html.markdown @@ -8,47 +8,47 @@ translators: lang: es-es --- -# programación dinámica +# Programación Dinámica ## Introducción -La programación dinámica es una técnica poderosa usada para resolver una clase particular de problemas como veremos más adelante. La idea es muy simple, si usted ha solucionado un problema con la entrada dada, entonces , guardaremos el resultado para una futura referencia, con el fin de evitar la solución del mismo problema de nuevo. +La programación dinámica es una técnica poderosa usada para resolver una clase particular de problemas como veremos más adelante. +La idea es muy simple: si has solucionado un problema con la entrada dada, entonces, guardaremos el resultado para una futura referencia, con el fin de evitar la solución del mismo problema de nuevo. - -Recuerde siempre!! +Recuerda siempre: "Aquellos que no pueden recordar el pasado están condenados a repetirlo" ## Formas de resolver este tipo de problemas -1.) De arriba hacia abajo : Empezamos resolviendo el problema dado descomponiendolo. Si ves que el problema fue resuelto, entonces retorna la respuesta guardada. si no se ha resuelto, resuélvelo y guarda la respuesta. Esto suele ser fácil pensar y muy intuitivo. Esto se conoce como memorización. +1. *De arriba hacia abajo (Top-Down)* : Empezamos resolviendo el problema dado descomponiendolo. Si ves que el problema fue resuelto, entonces retorna la respuesta guardada. Si no se ha resuelto, resuélvelo y guarda la respuesta. Esto suele ser fácil de pensar y es muy intuitivo. A esto se le conoce como memoización. -2.) De abajo hacia arriba : Analiza el problema y mira el orden en que los subproblemas deben ser resueltos y empieza resolviendo el subproblema más trivial, hacia el problema dado.En este proceso, se garantiza que los subproblemas se resuelven antes de resolver el problema. Esto se conoce como programación dinámica. +2. *De abajo hacia arriba (Bottom-Up)* : Analiza el problema y ve el orden en que los subproblemas deben ser resueltos y empieza resolviendo el subproblema más trivial, hacia el problema dado. En este proceso, se garantiza que los subproblemas se resuelven antes de resolver el problema. Esto se conoce como Programación Dinámica. ## Ejemplo de Programación Dinámica -El problema de la subsecuencia creciente máxima consiste en encontrar la subsecuencia creciente máxima en una secuencia dada . Dada la secuencia S= {a1 , a2 , a3, a4, ............., an-1, an } tenemos que encontrar un subconjunto más largo tal que para todo j y i, j <i en el subconjunto aj <ai. -En primer lugar tenemos que encontrar el valor de las subsecuencias más largas (LSI) en cada índice con el último elemento de la secuencia que es ai. El mayor LSi sería la subsecuencia más larga de la secuencia dada. Para empezar LSI es asignado a uno ya que ai es un elemento de la secuencia(El último elemento).Entonces, para todo j tal que j <i aj <ai, nos encontramos con Lsj más grande y lo agregamos a la LSI. A continuación, el algoritmo toma un tiempo de O (n2). -Pseudocódigo para encontrar la longitud de la más larga subsecuencia creciente: -La complejidad de este algoritmos podría reducirse mediante el uso de una mejor estructura de datos en lugar de una array. Almacenamiento de una matriz predecesora y una variable como Secuencia_mas_Grande_hasta_ahora y su índice podría ahorrar mucho tiempo. -concepto similar se podría aplicar en encontrar el camino más largo de grafo acíclico dirigido. ---------------------------------------------------------------------------- - for i=0 to n-1 - LS[i]=1 - for j=0 to i-1 - if (a[i] > a[j] and LS[i]<LS[j]) - LS[i] = LS[j]+1 - for i=0 to n-1 - if (largest < LS[i]) - -### Algunos problemas famosos de Programación Dinámica (DP). +El problema de la subsecuencia creciente máxima consiste en encontrar la subsecuencia creciente máxima en una secuencia dada. Dada la secuencia `S= {a1 , a2 , a3, a4, ............., an-1, an }`, tenemos que encontrar un subconjunto más largo tal que para todo `j` y `i`, `j<i` en el subconjunto `aj<ai`. +En primer lugar tenemos que encontrar el valor de las subsecuencias más largas (LSi) en cada índice `i` con el último elemento de la secuencia que es `ai`. El mayor LSi sería la subsecuencia más larga de la secuencia dada. Para empezar, LSi=1 ya que `ai` es un elemento de la secuencia (el último elemento). Entonces, para todo `j` tal que `j<i` y `aj<ai`, encontramos el LSj más grande y lo agregamos al LSi, por lo que el algoritmo toma un tiempo de *O(n2)*. +Pseudocódigo para encontrar la longitud de la subsecuencia creciente máxima: +La complejidad de este algoritmo podría reducirse mediante el uso de una mejor estructura de datos que los arreglos. Guardar un arreglo de predecesores y una variable como `secuencia_mas_grande_hasta_ahora` y su índice podría ahorrar mucho tiempo. + +Un concepto similar se podría aplicar para encontrar la trayectoria más larga en un grafo acíclico dirigido (DAG). + +```python +for i=0 to n-1 + LS[i]=1 + for j=0 to i-1 + if (a[i] > a[j] and LS[i]<LS[j]) + LS[i] = LS[j]+1 +for i=0 to n-1 + if (largest < LS[i]) ``` -Algoritmo Floyd Warshall(EN) - Tutorial y código fuente del programa en C:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code - -Problema de la Mochila(EN) - Tutorial y código fuente del programa en C: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem +### Algunos problemas famosos de Programación Dinámica (DP). -Problema de Subsecuencia Común mas Larga(EN) - Tutorial y código fuente del programa en C : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence +- Algoritmo Floyd Warshall(EN) - [Tutorial y código fuente del programa en C](http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code) +- Problema de la Mochila(EN) - [Tutorial y código fuente del programa en C](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem) +- Problema de Subsecuencia Común mas Larga(EN) - [Tutorial y código fuente del programa en C](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence) ## Recursos en línea -* [codechef EN](https://www.codechef.com/wiki/tutorial-dynamic-programming)
\ No newline at end of file +* [codechef EN](https://www.codechef.com/wiki/tutorial-dynamic-programming) diff --git a/es-es/git-es.html.markdown b/es-es/git-es.html.markdown index 1a8e275a..749365d1 100644 --- a/es-es/git-es.html.markdown +++ b/es-es/git-es.html.markdown @@ -1,11 +1,11 @@ --- category: tool tool: git +filename: LearnGit-es.txt contributors: - ["Jake Prather", "http://github.com/JakeHP"] translator: - ["Raúl Ascencio", "http://rscnt.github.io"] -filename: LearnGit.txt lang: es-es --- diff --git a/es-es/go-es.html.markdown b/es-es/go-es.html.markdown index c41d693d..78267695 100644 --- a/es-es/go-es.html.markdown +++ b/es-es/go-es.html.markdown @@ -26,7 +26,7 @@ Es rápido compilando y rápido al ejecutar, añade una concurrencia fácil de entender para las CPUs de varios núcleos de hoy día, y tiene características que ayudan con la programación a gran escala. -Go viene con una biblioteca estándar muy buena y una entusiasta comunidad. +Go viene con una biblioteca estándar muy buena y una comunidad entusiasta. ```go // Comentario de una sola línea @@ -52,7 +52,7 @@ import ( // para el ejecutable. Te guste o no, Go utiliza llaves. func main() { // Println imprime una línea a stdout. - // Cualificalo con el nombre del paquete, fmt. + // Llámalo con el nombre del paquete, fmt. fmt.Println("¡Hola mundo!") // Llama a otra función de este paquete. @@ -90,12 +90,12 @@ saltos de línea.` // mismo tipo cadena g := 'Σ' // Tipo rune, un alias de int32, alberga un carácter unicode. f := 3.14195 // float64, el estándar IEEE-754 de coma flotante 64-bit. c := 3 + 4i // complex128, representado internamente por dos float64. - // Sintaxis Var con iniciadores. + // Sintaxis var con iniciadores. var u uint = 7 // Sin signo, pero la implementación depende del tamaño // como en int. var pi float32 = 22. / 7 - // Sintáxis de conversión con una declaración corta. + // Sintaxis de conversión con una declaración corta. n := byte('\n') // byte es un alias para uint8. // Los Arreglos tienen un tamaño fijo a la hora de compilar. @@ -377,8 +377,8 @@ func aprendeConcurrencia() { go func() { c <- 84 }() // Inicia una nueva rutinago solo para // enviar un valor. go func() { cs <- "verboso" }() // Otra vez, para cs en esta ocasión. - // Select tiene una sintáxis parecida a la instrucción switch pero cada - // caso involucra una operacion con un canal. Selecciona un caso de + // Select tiene una sintaxis parecida a la instrucción switch pero cada + // caso involucra una operación con un canal. Selecciona un caso de // forma aleatoria de los casos que están listos para comunicarse. select { case i := <-c: // El valor recibido se puede asignar a una variable, diff --git a/es-es/groovy-es.html.markdown b/es-es/groovy-es.html.markdown index 799fc609..262d5e6a 100644 --- a/es-es/groovy-es.html.markdown +++ b/es-es/groovy-es.html.markdown @@ -232,10 +232,12 @@ for (i in array) { // Iterando sobre un mapa def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] -x = 0 +x = "" for ( e in map ) { x += e.value + x += " " } +assert x.equals("Roberto Grails Groovy ") /* Operadores diff --git a/es-es/learnsmallbasic-es.html.markdown b/es-es/learnsmallbasic-es.html.markdown new file mode 100644 index 00000000..ff320afb --- /dev/null +++ b/es-es/learnsmallbasic-es.html.markdown @@ -0,0 +1,132 @@ +--- +language: SmallBASIC +filename: learnsmallbasic-es.bas +contributors: + - ["Chris Warren-Smith", "http://smallbasic.sourceforge.net"] +translators: + - ["José Juan Hernández García", "http://jjuanhdez.es"] +lang: es-es +--- + +## Acerca de + +SmallBASIC es un intérprete del lenguaje BASIC rápido y fácil de aprender, ideal para cálculos cotidianos, scripts y prototipos. SmallBASIC incluye funciones trigonométricas, matrices y álgebra, un IDE integrado, una potente librería de cadenas de texto, comandos de sistema, sonido y gráficos, junto con una sintaxis de programación estructurada. + +## Desarrollo + +SmallBASIC fue desarrollado originalmente por Nicholas Christopoulos a finales de 1999 para el Palm Pilot. El desarrollo del proyecto ha sido continuado por Chris Warren-Smith desde el año 2005. +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. + +``` +REM Esto es un comentario +' y esto tambien es un comentario + +REM Imprimir texto +PRINT "hola" +? "? es la abreviatura de PRINT" + +REM Estructuras de control +FOR index = 0 TO 10 STEP 2 + ? "Este es el numero de linea "; index +NEXT +J = 0 +REPEAT + J++ +UNTIL J = 10 +WHILE J > 0 + J-- +WEND + +REM Estructura Select Case +SELECT CASE "Cool" + CASE "null", 1, 2, 3, 4, 5, 6, 7, 8, "Cool", "blah" + CASE "No Cool" + PRINT "Fallo epico" + CASE ELSE + PRINT "Fallo" +END SELECT + +REM Captura de errores con TRY/CATCH +TRY + fn = Freefile + OPEN filename FOR INPUT As #fn +CATCH err + PRINT "No se pudo abrir" +END TRY + +REM Procedimientos y funciones definidas por el usuario +FUNC add2(x, y) + ' variables pueden declararse como locales en el ambito de una SUB o FUNC + LOCAL k + k = "k dejara de existir cuando retorne FUNC" + add2 = x + y +END +PRINT add2(5, 5) + +SUB print_it(it) + PRINT it +END +print_it "IT...." + +REM Visualizacion de lineas y pixeles +At 0, ymax / 2 + txth ("Q") +COLOR 1: ? "sin(x)": +COLOR 8: ? "cos(x)": +COLOR 12: ? "tan(x)" +LINE 0, ymax / 2, xmax, ymax / 2 +FOR i = 0 TO xmax + PSET i, ymax / 2 - SIN(i * 2 * pi / ymax) * ymax / 4 COLOR 1 + PSET i, ymax / 2 - COS(i * 2 * pi / ymax) * ymax / 4 COLOR 8 + PSET i, ymax / 2 - TAN(i * 2 * pi / ymax) * ymax / 4 COLOR 12 +NEXT +SHOWPAGE + +REM SmallBASIC es ideal para experimentar con fractales y otros efectos interesantes +DELAY 3000 +RANDOMIZE +ff = 440.03 +FOR j = 0 TO 20 + r = RND * 1000 % 255 + b = RND * 1000 % 255 + g = RND * 1000 % 255 + c = RGB(r, b, g) + ff += 9.444 + FOR i = 0 TO 25000 + ff += ff + x = MIN(xmax, -x + COS(f * i)) + y = MIN(ymax, -y + SIN(f * i)) + PSET x, y COLOR c + IF (i % 1000 == 0) THEN + SHOWPAGE + fi + NEXT +NEXT j + +REM Para historiadores de computadoras, SmallBASIC puede ejecutar programas +REM encontrados en los primeros libros de computacion y revistas, por ejemplo: +10 LET A = 9 +20 LET B = 7 +30 PRINT A * B +40 PRINT A / B + +REM SmallBASIC también tiene soporte para algunos conceptos modernos como JSON +aa = ARRAY("{\"cat\":{\"name\":\"harry\"},\"pet\":\"true\"}") +IF (ismap(aa) == false) THEN + THROW "no es un mapa" +END IF +PRINT aa + +PAUSE + +``` +## Artículos + +* [Primeros pasos](http://smallbasic.sourceforge.net/?q=node/1573) +* [Bienvenido a SmallBASIC](http://smallbasic.sourceforge.net/?q=node/838) + +## GitHub + +* [Código fuente](https://github.com/smallbasic/SmallBASIC) +* [Reference snapshot](http://smallbasic.github.io/) + 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/sass-es.html.markdown b/es-es/sass-es.html.markdown index 89e56ba5..d130fe8c 100644 --- a/es-es/sass-es.html.markdown +++ b/es-es/sass-es.html.markdown @@ -1,6 +1,6 @@ --- language: sass -filename: learnsass.scss +filename: learnsass-es.scss contributors: - ["Laura Kyle", "https://github.com/LauraNK"] - ["Sean Corrales", "https://github.com/droidenator"] diff --git a/es-es/swift-es.html.markdown b/es-es/swift-es.html.markdown index 8f63517a..22e3c532 100644 --- a/es-es/swift-es.html.markdown +++ b/es-es/swift-es.html.markdown @@ -446,48 +446,48 @@ if let circle = myEmptyCircle { // Al igual que las clases, pueden contener métodos enum Suit { - case Spades, Hearts, Diamonds, Clubs + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } // Los valores de enum permite la sintaxis corta, sin necesidad de poner // el tipo del enum cuando la variable es declarada de manera explícita -var suitValue: Suit = .Hearts +var suitValue: Suit = .hearts // Enums de tipo no-entero requiere asignaciones de valores crudas directas enum BookName: String { - case John = "John" - case Luke = "Luke" + case john = "John" + case luke = "Luke" } -print("Name: \(BookName.John.rawValue)") +print("Name: \(BookName.john.rawValue)") // Enum con valores asociados enum Furniture { // Asociación con Int - case Desk(height: Int) + case desk(height: Int) // Asociación con String e Int - case Chair(String, Int) + case chair(String, Int) func description() -> String { switch self { - case .Desk(let height): + case .desk(let height): return "Desk with \(height) cm" - case .Chair(let brand, let height): + case .chair(let brand, let height): return "Chair of \(brand) with \(height) cm" } } } -var desk: Furniture = .Desk(height: 80) +var desk: Furniture = .desk(height: 80) print(desk.description()) // "Desk with 80 cm" -var chair = Furniture.Chair("Foo", 40) +var chair = Furniture.chair("Foo", 40) print(chair.description()) // "Chair of Foo with 40 cm" diff --git a/es-es/tcl-es.html.markdown b/es-es/tcl-es.html.markdown new file mode 100644 index 00000000..5db72ae1 --- /dev/null +++ b/es-es/tcl-es.html.markdown @@ -0,0 +1,600 @@ +--- +language: Tcl +contributors: + - ["Poor Yorick", "https://pooryorick.com/"] +translators: + - ["Héctor Romojaro", "https://github.com/hromojaro"] +lang: es-es +filename: learntcl-es.tcl +--- + +Tcl fue creado por [John Ousterhout](https://wiki.tcl.tk/John%20Ousterout) como +un lenguaje reutilizable de scripting para herramientas de diseño de circuitos +de las que él era autor. En 1997 recibió el +[ACM Software System Award](https://en.wikipedia.org/wiki/ACM_Software_System_Award) +por Tcl. Tcl puede ser utilizado tanto como lenguaje de scripting embebido, +como lenguaje de programación general. Puede ser utilizado también como una +biblioteca portable de C, incluso en casos donde no se requieren capacidades +de scripting, ya que provee de estructuras de datos tales como cadenas (*string*) +de caracteres dinámicas, listas y tablas hash. La biblioteca de C también +provee funcionalidad portable para cargar bibliotecas dinámicas, formato de +cadenas y conversión de código, operaciones sobre el sistema de ficheros, +operaciones de red y más. Algunas características reseñables de Tcl: + +* Conveniente API de red multiplataforma + +* Sistema de ficheros totalmente virtualizado + +* Canales apilables de E/S + +* Asíncrono hasta el núcleo + +* Corrutinas completas + +* Un modelo de hebras reconocido como robusto y fácil de usar + + +Tcl tiene mucho en común con Lisp pero, en lugar de listas, Tcl utiliza cadenas +de caracteres como moneda de cambio del lenguaje. Todos los valores son cadenas. +Una lista es una cadena con un formato definido, y el cuerpo de un procedimiento +(un script) es también una cadena en lugar de un bloque. Para incrementar el +rendimiento, Tcl cachea internamente representaciones estructuradas de estos +valores. Las rutinas con listas, por ejemplo, operan en la representación interna +en caché, y Tcl se ocupa de actualizar la representación en cadenas si es realmente +necesario en el script. El diseño *copy-on-write* de Tcl permite a los autores +de scripts mover grandes volúmenes de datos sin incurrir en el consumo adicional +de memoria. Los procedimientos son automáticamente compilados (*byte-compiled*) +a no ser que utilicen rutinas dinámicas como "uplevel", "upvar" o "trace". + +Programar en Tcl es un placer. Le resultará atractivo a hackers que encuentren +atractivo Lisp, Forth o Smalltalk, y a ingenieros y científicos que simplemente +quieren ponerse a trabajar con una herramienta que se doblega a su voluntad. La +disciplina de exponer toda la funcionalidad programática como rutinas, incluyendo +cosas como iteraciones y operaciones matemáticas que normalmente están en la +sintaxis de otros lenguajes, permitiendo fundirse en el fondo de cualquier +funcionalidad específica del dominio que necesita un proyecto. Su sintaxis, +incluso más simple que la de lisp, simplemente se quita de en medio. + + + +```tcl +#! /bin/env tclsh + +############################################################################### +## 1. Directrices +############################################################################### + +# ¡Tcl no es ni Sh ni C! Es necesario decirlo porque el entrecomillado estándar +# de shell casi funciona en Tcl, y es común que la gente empiece con Tcl e +# intente utilizar sintaxis de otros lenguajes. Funciona al principio, pero +# rápidamente conduce a frustración cuando los scripts se vuelven más complejos. + +# Las llaves son un mecanismo de entrecomillado, no de sintaxis para la construcción +# de bloques de código o listas. Tcl no tiene ninguna de ellas. Las llaves se +# usan para escapar caracteres especiales, lo que las hace apropiadas para +# entrecomillar cuerpos de procedimientos y cadenas que deberían ser interpretadas +# como listas. + + +############################################################################### +## 2. Sintaxis +############################################################################### + +# Un script consiste en comandos delimitados por saltos de línea o puntos y coma. +# Cada comando es una llamada a una rutina. La primera palabra es el nombre de +# la rutina a llamar, y las siguientes palabras son argumentos de la rutina. +# Las palabras están delimitadas por espacios. Puesto que cada argumento es una +# palabra en el comando, y una cadena de caracteres, puede no ser entrecomillada: +set part1 Sal +set part2 ut; set part3 ations + + +# el símbolo del dólar introduce la sustitución de variables: +set greeting $part1$part2$part3 + + +# Cuando "set"recibe sólamente el nombre de una variable, devuelve su valor: +set part3 ;# Returns the value of the variable. + + +# Los corchetes delimitan un script que será evaluado y sustituido por su resultado: +set greeting $part1$part2[set part3] + + +# Un script incrustado puede estar compuesto de múltiples comandos, el último de +# los cuales devuelve el resultado de la sustitución: +set greeting $greeting[ + incr i + incr i + incr i +] +puts $greeting ;# La salida es "Salutations3" + +# Cada palabra en un comando es una cadena, incluyendo el nombre de la rutina, +# así que se pueden utilizar sustituciones allí también. Dada esta asignación +# de variable, + +set action pu + +# los siguientes tres comandos son equivalentes: +puts $greeting +${action}ts $greeting +[set action]ts $greeting + + +# La barra invertida suprime el significado especial de los caracteres: +set amount \$16.42 + + +# La barra invertida añade significado especial a ciertos caracteres: +puts lots\nof\n\n\n\n\n\nnewlines + + +# Una palabra encerrada entre llaves no está sujeta a interpretación especial o +# sustitución, excepto que una barra invertida antes de una llave no cuenta al +# buscar la llave de cierre: +set somevar { + This is a literal $ sign, and this \} escaped + brace remains uninterpreted +} + + +# En una palabra delimitada por comillas dobles, los espacios pierden su significado +# especial: +set name Neo +set greeting "Hello, $name" + + +# Un nombre de variable puede ser cualquier cadena: +set {first name} New + + +# La forma de sustitución de variables utilizando llaves permite nombres de +# variable más complejos: +set greeting "Hello, ${first name}" + + +# "set" puede utilizarse siempre en lugar de la sustitución de variables, y permite +# utilizar cualquier nombre de variable: +set greeting "Hello, [set {first name}]" + + +# Para desempaquetar una lista en un el comando, se utiliza el operador de expansión, +# "{*}". Estos dos comandos son equivalentes: +set name Neo +set {*}{name Neo} + + +# Un array es una variable especial que sirve como contenedor de otras variables. +set person(name) Neo +set person(destiny) {The One} +set greeting "Hello, $person(name)" + + +# "variable" se puede utilizar para declarar o asignar variables. Al contrario +# que "set", que utiliza el espacio de nombres global y el actual para resolver +# un nombre de variable, "variable" usa solamente el actual: +variable name New + + +# "namespace eval" crea un nuevo espacio de nombres en caso de no existir. +# Un espacio de nombres puede contener tanto rutinas como variables: +namespace eval people { + namespace eval person1 { + variable name Neo + } +} + + +# Use dos o más ":" para delimitar componentes del espacio de nombres en nombres +# de variables: +namespace eval people { + set greeting "Hello $person1::name" +} + +# Dos o más ":" también delimitan componentes del espacio de nombres en nombres +# de rutinas: +proc people::person1::speak {} { + puts {I am The One.} +} + +# Nombres completos comienzan con dos ":": +set greeting "Hello $::people::person1::name" + + + +############################################################################### +## 3. No más sintaxis +############################################################################### + +# El resto de funcionalidades se implementa mediante rutinas. Desde este punto, +# no hay nueva sintaxis. Todo lo que queda para aprender Tcl es acerca del +# comportamiento de rutinas individuales y el significado que asignan a sus +# argumentos. + + + +############################################################################### +## 4. Variables y espacios de nombres +############################################################################### + +# Cada variable y cada rutina están asociadas a algún espacio de nombres + +# Para terminar con un intérprete inútil, sólo hay que eliminar el espacio de +# nombres global. No es algo muy útil, pero sirve para ilustrar la naturaleza +# de Tcl. El nombre del espacio de nombres global es en realidad la cadena +# vacía, pero la única forma de representarlo es como un nombre completo. Para +# probarlo, se puede usar esta rutina. +proc delete_global_namespace {} { + namespace delete :: +} + +# Como "set" siempre mantiene su vista en los espacios de nombres global y actual, +# es más seguro utilizar "variable" para declarar o asignar un valor a una +# variable. Si una variable llamada "nombre" ya existe en el espacio de nombres +# global, usar "set" asignará un valor a la variable local en lugar de a la +# variable del espacio de nombres actual, mientras que "variable" opera en el +# espacio de nombres actual solamente. +namespace eval people { + namespace eval person1 { + variable name Neo + } +} + +# Una vez que una variable es declarada en un espacio de nombres, [set] la vé +# en lugar de una variable de idéntico nombre en el espacio de nombres global: +namespace eval people { + namespace eval person1 { + variable name + set name Neo + } +} + +# En cambio, si "set" tiene que crear una nueva variable, siempre lo hace en el +# espacio de nombres actual: +unset name +namespace eval people { + namespace eval person1 { + set name neo + } + +} +set people::person1::name + + +# Un nombre absoluto siempre comienza con el nombre del espacio de nombres global +# (cadena vacía), seguido de dos ":": +set ::people::person1::name Neo + + +# En el interior de un procedimiento, la variable enlaza una variable en el espacio +# de nombres actual en el ámbito local: +namespace eval people::person1 { + proc fly {} { + variable name + puts "$name is flying!" + } +} + + + + +############################################################################### +## 4. Rutinas incorporadas +############################################################################### + +# Las operaciones matemáticas se pueden hacer con "expr": +set a 3 +set b 4 +set c [expr {$a + $b}] + +# Como "expr" realiza sustituciones de variables por sí mismo, es necesario +# poner la expresión entre llaves para prevenir a Tcl sustituir las variables +# primero. Ver "http://wiki.tcl.tk/Brace%20your%20#%20expr-essions" para más +# detalles. + + +# "expr" entiende sustitución de variables y scripts: +set c [expr {$a + [set b]}] + + +# "expr" provee de un conjunto de funciones matemáticas: +set c [expr {pow($a,$b)}] + + +# Los operadores matemáticos están disponibles como rutinas en el espacio de +# nombres ::tcl::mathop +::tcl::mathop::+ 5 3 + +# Las rutinas pueden ser importadas desde otros espacios de nombres: +namespace import ::tcl::mathop::+ +set result [+ 5 3] + + +# Los valores no numéricos deben ser entrecomillados, y los operadores como "eq" +# pueden utilizarse para restringir la operación a una comparación de cadenas: +set name Neo +expr {{Bob} eq $name} + +# Los operadores generales recurren a la comparación de cadenas si una operación +# numérica no es factible. +expr {{Bob} == $name} + + +# "proc" crea nuevas rutinas: +proc greet name { + return "Hello, $name!" +} + +# Se pueden especificar múltiples parámetros: +proc greet {greeting name} { + return "$greeting, $name!" +} + + +# Como se dijo antes, las llaves no construyen un bloque de código. Cada valor, +# incluso el tercer argumento de "proc", es una cadena. El comando anterior +# puede ser reescrito sin usar llaves: +proc greet greeting\ name return\ \"\$greeting,\ \$name!\" + + + +# Cuando el último parámetro es el valor literal "args", todos los argumentos +# extra pasados a la rutina son recogidos en una lista y asignado a "args": +proc fold {cmd first args} { + foreach arg $args { + set first [$cmd $first $arg] + } + return $first +} +fold ::tcl::mathop::* 5 3 3 ;# -> 45 + + +# La ejecución condicional se implementa como una rutina: +if {3 > 4} { + puts {This will never happen} +} elseif {4 > 4} { + puts {This will also never happen} +} else { + puts {This will always happen} +} + + +# Los bucles se implementan como rutinas. Los primer y tercer argumentos de "for" +# son tratados como scripts, mientras que el segundo lo es como una expresión: +set res 0 +for {set i 0} {$i < 10} {incr i} { + set res [expr {$res + $i}] +} +unset res + + +# El primer argumento de "while" se trata también como una expresión: +set i 0 +while {$i < 10} { + incr i 2 +} + + +# Una lista es una cadena, y los elementos de la lista se delimitan con espacios +# en blanco: +set amounts 10\ 33\ 18 +set amount [lindex $amounts 1] + +# El espacio en blanco dentro de una lista debe ser entrecomillado: +set inventory {"item 1" item\ 2 {item 3}} + + +# Generalmente, es mejor idea usar rutinas de listas al modificarlas: +lappend inventory {item 1} {item 2} {item 3} + + +# Las llaves y barras invertidas pueden utilizarse para formatear valores más +# complejos en una lista. Una lista parece un script, excepto en que el carácter +# de nueva línea y el ":" pierden su significado especial, y no hay sustitución +# de variable o scripts. Esta característica hace Tcl homoicónico. Hay tres +# elementos en la siguiente lista: +set values { + + one\ two + + {three four} + + five\{six + +} + + +# Como, al igual que todos los valores, una lista es una cadena, operaciones de +# cadenas pueden ser realizadas sobre ellas, corriendo el riesgo de corromper +# el formato de la lista: +set values {one two three four} +set values [string map {two \{} $values] ;# $values is no-longer a \ + properly-formatted list + + +# La forma segura de conseguir una lista debidamente formateada es utilizando +# las rutinas propias de lista: +set values [list one \{ three four] +lappend values { } ;# add a single space as an item in the list + + +# Se puede utilizar "eval" para evaluar un valor como un script: +eval { + set name Neo + set greeting "Hello, $name" +} + + +# Una lista siempre puede ser pasada a "eval" como un script compuesto de un único +# comando: +eval {set name Neo} +eval [list set greeting "Hello, $name"] + + +# Por lo tanto, cuando se utiliza "eval", use "list" para construir el comando +# deseado: +set command {set name} +lappend command {Archibald Sorbisol} +eval $command + + +# Un error común es no usar funciones de listas al construir un comando: +set command {set name} +append command { Archibald Sorbisol} +try { + eval $command ;# El error es que "set" tiene demasiados argumentos en \ + {set name Archibald Sorbisol} +} on error {result eoptions} { + puts [list {received an error} $result] +} + +# Este error puede ocurrir fácilmente con "subst": + +set replacement {Archibald Sorbisol} +set command {set name $replacement} +set command [subst $command] +try { + eval $command ;# El mismo error que antes: demasiados argumentos a "set" en \ + {set name Archibald Sorbisol} +} trap {TCL WRONGARGS} {result options} { + puts [list {received another error} $result] +} + + +# "list" formatea correctamente un valor para su sustitución: +set replacement [list {Archibald Sorbisol}] +set command {set name $replacement} +set command [subst $command] +eval $command + + +# "list" se utiliza normalmente para formatear valores para su sustitución en +# scripts: Hay muchos ejemplos de esto más abajo. + + +# "apply" evalúa una lista de dos elementos como una rutina: +set cmd {{greeting name} { + return "$greeting, $name!" +}} +apply $cmd Whaddup Neo + +# Un tercer elemento puede ser utilizado para especificar el espacio de nombres +# donde aplicar la rutina: +set cmd [list {greeting name} { + return "$greeting, $name!" +} [namespace current]] +apply $cmd Whaddup Neo + + +# "uplevel" evalúa un script en un nivel superior de la pila de llamadas: +proc greet {} { + uplevel {puts "$greeting, $name"} +} + +proc set_double {varname value} { + if {[string is double $value]} { + uplevel [list variable $varname $value] + } else { + error [list {not a double} $value] + } +} + + +# "upvar" enlaza una variable en el nivel actual de la pila de llamadas a una +# variable en un nivel superior: +proc set_double {varname value} { + if {[string is double $value]} { + upvar 1 $varname var + set var $value + } else { + error [list {not a double} $value] + } +} + + +# Deshacerse de la rutina "while" incorporada, y utilizar "proc" para definir +# una nueva: +rename ::while {} +# la manipulación se deja como ejercicio: +proc while {condition script} { + if {[uplevel 1 [list expr $condition]]} { + uplevel 1 $script + tailcall [namespace which while] $condition $script + } +} + + +# "coroutine" crea una nueva pila de llamadas, una nueva rutina en la que +# introducir esa pila de llamadas, y luego llama a dicha rutina. "yield" suspende +# la evaluación en esa pila y devuelve el control a la pila que efectúa la llamada. +proc countdown count { + # devuelve algo al creador de la corrutina, efectivamente pausando esta + # pila de llamadas por ahora. + yield [info coroutine] + + while {$count > 1} { + yield [incr count -1] + } + return 0 +} +coroutine countdown1 countdown 3 +coroutine countdown2 countdown 5 +puts [countdown1] ;# -> 2 +puts [countdown2] ;# -> 4 +puts [countdown1] ;# -> 1 +puts [countdown1] ;# -> 0 +catch { + puts [coundown1] ;# -> invalid command name "countdown1" +} cres copts +puts $cres +puts [countdown2] ;# -> 3 + + +# Pilas de corrutinas pueden cederse el control entre sí: + +proc pass {whom args} { + return [yieldto $whom {*}$args] +} + +coroutine a apply {{} { + yield + set result [pass b {please pass the salt}] + puts [list got the $result] + set result [pass b {please pass the pepper}] + puts [list got the $result] +}} + +coroutine b apply {{} { + set request [yield] + while 1 { + set response [pass c $request] + puts [list [info coroutine] is now yielding] + set request [pass a $response] + } +}} + +coroutine c apply {{} { + set request [yield] + while 1 { + if {[string match *salt* $request]} { + set request [pass b salt] + } else { + set request [pass b huh?] + } + } +}} + +# Pon las cosas en marcha +a + + +``` + +## Reference + +[Documentación oficial de Tcl](http://www.tcl.tk/man/tcl/) + +[Tcl Wiki](http://wiki.tcl.tk) + +[Tcl Subreddit](http://www.reddit.com/r/Tcl) 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/es-es/xml-es.html.markdown b/es-es/xml-es.html.markdown index 2e9326cf..23831f3b 100644 --- a/es-es/xml-es.html.markdown +++ b/es-es/xml-es.html.markdown @@ -1,6 +1,6 @@ --- language: xml -filename: learnxml.xml +filename: learnxml-es.xml contributors: - ["João Farias", "https://github.com/JoaoGFarias"] translators: diff --git a/fa-ir/bf-fa.html.markdown b/fa-ir/bf-fa.html.markdown index bc5d8dc4..81c73980 100644 --- a/fa-ir/bf-fa.html.markdown +++ b/fa-ir/bf-fa.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: bf-fa.bf contributors: - ["Mohammad Valipour", "https://github.com/mvalipour"] lang: fa-ir diff --git a/fa-ir/java-fa.html.markdown b/fa-ir/java-fa.html.markdown index e8182e81..cb965fc4 100644 --- a/fa-ir/java-fa.html.markdown +++ b/fa-ir/java-fa.html.markdown @@ -54,7 +54,7 @@ public class LearnJava { /////////////////////////////////////// /* - * Ouput + * Output */ // Use System.out.println() to print lines. diff --git a/fi-fi/go-fi.html.markdown b/fi-fi/go-fi.html.markdown index 9ed4e0d2..af304099 100644 --- a/fi-fi/go-fi.html.markdown +++ b/fi-fi/go-fi.html.markdown @@ -84,7 +84,7 @@ func learnTypes() { // Lyhyt ilmoitus antaa yleensä haluamasi. str := "Opi Go!" // merkkijonotyyppi. - s2 := `"raaka" todellisarvoinen merrkijono + s2 := `"raaka" todellisarvoinen merkkijono voi sisältää rivinvaihtoja.` // Sama merkkijonotyyppi. // Ei-ASCII todellisarvo. Go-lähdekoodi on UTF-8. 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/forth.html.markdown b/forth.html.markdown index 09f3beb0..ff094017 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -59,7 +59,7 @@ Forth, but most of what is written here should work elsewhere. \ ---------------------- More Advanced Stack Manipulation ---------------------- -1 2 3 4 tuck \ duplicate the top item into the second slot: 1 2 4 3 4 ok +1 2 3 4 tuck \ duplicate the top item below the second slot: 1 2 4 3 4 ok 1 2 3 4 over \ duplicate the second item to the top: 1 2 3 4 3 ok 1 2 3 4 2 roll \ *move* the item at that position to the top: 1 3 4 2 ok 1 2 3 4 2 pick \ *duplicate* the item at that position to the top: 1 2 3 4 2 ok diff --git a/fortran95.html.markdown b/fortran95.html.markdown index 8479fef8..c256bb38 100644 --- a/fortran95.html.markdown +++ b/fortran95.html.markdown @@ -6,7 +6,7 @@ filename: learnfortran.f95 --- Fortran is one of the oldest computer languages. It was developed in the 1950s -by IBM for numeric calculations (Fortran is an abreviation of "Formula +by IBM for numeric calculations (Fortran is an abbreviation of "Formula Translation"). Despite its age, it is still used for high-performance computing such as weather prediction. However, the language has changed considerably over the years, although mostly maintaining backwards compatibility; well known @@ -242,7 +242,7 @@ program example !declare a program called example. close(12) ! There are more features available than discussed here and alternative - ! variants due to backwards compatability with older Fortran versions. + ! variants due to backwards compatibility with older Fortran versions. ! Built-in Functions diff --git a/fr-fr/asymptotic-notation-fr.html.markdown b/fr-fr/asymptotic-notation-fr.html.markdown new file mode 100644 index 00000000..491dc3c4 --- /dev/null +++ b/fr-fr/asymptotic-notation-fr.html.markdown @@ -0,0 +1,152 @@ +--- +category: Algorithms & Data Structures +name: Asymptotic Notation +contributors: + - ["Jake Prather", "http://github.com/JakeHP"] + - ["Divay Prakash", "http://github.com/divayprakash"] +translators: + - ["Agathe Begault", "https://github.com/begault"] +lang: fr-fr +--- + +# Notations Asymptotiques + +## Qu'est ce que c'est? + +Les notations asymptotiques sont des langages qui nous permettent d'analyser l'ordre de grandeur du temps d'exécution d'un algorithme en identifiant son comportement à mesure que les données d'entrée de l'algorithme augmentent. On appelle également cela le taux de croissance d'un algorithme. + +Est ce que l'algorithme devient soudainement extrêmement lent si on augmente sa taille d'entrée ? Est ce qu'il arrive à maintenir un temps d'exécution rapide ? La notation asymptotique nous permet simplement de répondre à ces questions et d'ainsi comparer différents algorithmes. + +## Y a t-il des alternatives pour répondre à ces problématiques ? + +Une première solution serait de compter le nombre d'opérations primitives, en fonction de la taille d'entrée. +Cette méthode est viable mais nécessite une quantité de travail trop importante par rapport à son utilisation, même sur des algorithmes simples. + +Une autre approche serait de mesurer physiquement le temps qu'un algorithme met pour traiter différentes quantités de données. Cependant, la précision et la relativité de cette méthode (les temps obtenus n'étant relatifs qu'à la machine sur laquelle ils ont été calculés) est liée à des variables environnementales comme les spécifications du matériel informatique utilisé, la puissance de traitement de la machine, etc. + +## Les types de Notations asymptotiques + +En première partie de ce document, nous avons décris comment une notation asymptotique identifie le comportement d'un algorithme à mesure que la taille des données d'entrée change. Imaginons un algorithme comme une fonction f, ayant comme taille de donnée d'entrée n, et f(n) étant le temps d'exécution. Ainsi, pour un algorithme donné f, avec une taille d'entrée n, nous obtenons en résultat un temps d'exécution f(n). Cela nous fournit un graphique où l'axe Y est le temps d'exécution, l'axe X est la taille d'entrée et la courbe tracée est le résultat du temps utilisé pour traiter chaque quantité de données. + +Vous pouvez décrire une fonction ou un algorithme avec une notation asymptotique de plusieurs manières. Par exemple, vous pouvez décrire un algorithme en partant du meilleur des cas, du pire ou d'un cas intermédiaire. Le plus courant est de commencer à analyser un algorithme avec le pire cas. Le meilleur cas n'est pas nécessaire car ce ne sont pas les conditions que vous envisagez. L'algorithme de tri est un très bon exemple, particulièrement en ajoutant des éléments à une structure arborescente. Le meilleur cas pour la plupart des algorithmes ne nécessite qu'une simple opération alors que dans la plupart des cas, l'élément à ajouter aura besoin d'être trié de manière appropriée à travers l'arbre de données. Cette action pourrait signifier l'examen d'une branche entière de l'arbre. C'est d'ailleurs le pire cas et celui que nous prévoyons. + +### Les types de fonctions, limites et simplifications + +``` +Fonction logarithmique - log n +Fonction linéaire - an + b +Fonction quadratique - an^2 + bn + c +Fonction polynomiale - an^z + . . . + an^2 + a*n^1 + a*n^0, où z est une constante +Fonction exponentielle - a^n, où a est une constante +``` + +Voici une classification de fonctions croissantes de base, utilisées dans de nombreuses notations. La liste commence par la plus lente des fonctions croissantes (logarithmique, le temps d'exécution le plus rapide) et finit avec la plus croissante des fonctions (exponentielle, le temps d'exécution le plus lent). Notez que lorsque 'n' ou les données d'entrée augmentent pour chacune de ces fonctions, le résultat augmente clairement plus rapidement avec les fonctions quadratique, polynomiale et exponentielle qu'avec les fonctions logarithmique et linéaire. + +Il est important de noter que les notations suivantes doivent être utilisées avec les termes les plus simples. Cela signifie d'ignorer les constantes et termes de l'ordre inférieur. En effet, puisque la taille d'entrée (ou n dans notre exemple f(n)) peut augmenter à l'infini (limites mathématiques), les termes et constantes de l'ordre inférieur sont insignifiants. Ceci dit, si vous avez une constante égale à 2^9001, ou toute autre valeur ridicule et inimaginable, dans ce cas la simplification nuira à votre précision de notation. + +Puisque nous voulons la forme la plus simple, modifions un peu notre table... + +``` +Logarithmique - log n +Linéaire - n +Quadratique - n^2 +Polynomiale - n^z, où z est une constante +Exponentielle - a^n, où a est une constante +``` + +### Big-O + +Big-O, couramment écris **O**, est une notation asymptotique pour le cas le plus mauvais (ou plafond de croissance) d'une fonction donnée. Il nous fournit une _**limite supérieure asymptotique**_ pour le taux de croissance du temps d'exécution d'un algorithme. + +Prenons 'f(n)' comme temps d'exécution de notre algorithme et 'g(n)' comme complexité de temps arbitraire que nous essayons d'appliquer à notre algorithme. 'f(n)' est O(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub>, 'f(n)' <= 'c g(n)' pour toute taille d'entrée n (n > n<sub>0</sub>). + +*Exemple 1* + +``` +f(n) = 3log n + 100 +g(n) = log n +``` + +Est-ce que `f(n)` O(g(n))? +Est-ce que `3 log n + 100` O(log n)? +Regardons maintenant la définition de Big-O. + +``` +3log n + 100 <= c * log n +``` + +Existe t-il une paire de constantes c, n<sub>0</sub> qui satisfait cela pour tout n > <sub>0</sub>? + +``` +3log n + 100 <= 150 * log n, n > 2 (Indéfini avec n = 1) +``` + +Oui ! La définition de Big-O a été satisfaite, donc `f(n)` is O(g(n)). + +*Exemple 2* + +``` +f(n) = 3*n^2 +g(n) = n +``` + +Est-ce que `f(n)` O(g(n))? +Est-ce que `3 * n^2` O(n)? +Regardons de nouveau la définition de Big-O. + +``` +3 * n^2 <= c * n +``` + +Existe t-il une paire de constantes c, n<sub>0</sub> qui satisfait cela pour tout n > <sub>0</sub>? +Non, il n'en existe pas. `f(n)` n'est pas égal à O(g(n)). + +### Big-Omega + +Big-Omega, courrament écris **Ω**, est une notation asymptotique pour le meilleur cas (ou limite de croissance basse) d'une fonction donnée. Il nous fournit une _**limite inférieure asymptotique**_ pour le taux de croissance du temps d'exécution d'un algorithme. + +Prenons 'f(n)' comme temps d'exécution de notre algorithme et 'g(n)' comme complexité de temps arbitraire que nous essayons d'appliquer à notre algorithme. 'f(n)' est Ω(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub>, 'f(n)' >= 'c g(n)' pour toute taille d'entrée n (n > n<sub>0</sub>). + +### Remarque + +Les taux de croissance asymptotiques fournis par les notations big-O et big-omega peuvent ou non être asymptotiquement serrés. Nous utilisons ainsi les notations small-o et small-omega pour désigner des limites qui ne sont pas asymptotiquement serrées. + +### Small-o +Small-o, couramment écris **o**, est une notation asymptotique pour désigner la limite supérieure (ce qui n'est pas asymptotiquement serré) du taux de croissance du temps d'exécution d'un algorithme. + +`f(n)` est o(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` < `c g(n)` +pour toute taille d'entrée n (n > n<sub>0</sub>). + +Les définitions de O-notation et o-notation sont similaires. La principale différence est visible quand f(n) = O(g(n)). Dans ce cas, la limite f(n) <= g(n) est appliquée pour _**quelques**_ constantes c > 0. Lorsque f(n) = o(g(n)), la limite f(n) < c g(n) est appliquée pour _**toute**_ constante c > 0. + +### Small-omega +Small-omega, couramment écris **ω**, est une notation asymptotique pour désigner la limite inférieure (ce qui n'est pas asymptotiquement serré) du taux de croissance du temps d'exécution d'un algorithme. + +`f(n)` est ω(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` > `c g(n)` +pour toute taille d'entrée n (n > n<sub>0</sub>). + +Les définitions de Ω-notation et ω-notation sont similaires. La principale différence est visible quand f(n) = Ω(g(n)). Dans ce cas, la limite f(n) >= g(n) est appliquée pour _**quelques**_ constantes c > 0. Lorsque f(n) = ω(g(n)), la limite f(n) > c g(n) est appliquée pour _**toute**_ constante c > 0. + +### Theta +Theta, couramment écris **Θ**, est une notation asymptotique pour désigner la _**borne asymptotique sous contrainte**_ du taux de croissance du temps d'exécution d'un algorithme. + +`f(n)` est Θ(g(n)), si pour certaines constantes réelles c1, c2 et n<sub>0</sub> (c1 > 0, c2 > 0, n<sub>0</sub> > 0), `c1 g(n)` < `f(n)` < `c2 g(n)` pour toute taille d'entrée n (n > n<sub>0</sub>). + +∴ `f(n)` est Θ(g(n)) implique que `f(n)` est égal à O(g(n)) autant que `f(n)` est égal à Ω(g(n)). + +N'hésitez pas à trouver de plus amples informations à ce sujet. Big-O est la notation la plus couramment utilisée pour le calcul de complexité du temps d'un algorithme. + +### Notes de fin +Il est difficile de traiter ce type de sujets dans un article court tant les exemples, méthodes et informations sont nombreuses. C'est pourquoi nous vous invitons à jeter un oeil aux livres et liens listés ci-dessous. +Ces ressources apportent plus de détails avec des exemples et des définitions. + +## Livres + +* [Algorithmes](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X) +* [Conception algorithmique](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651) + +## Ressources en ligne + +* [MIT](http://web.mit.edu/16.070/www/lecture/big_o.pdf) +* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation) +* [Big-O Cheatsheet](http://bigocheatsheet.com/) - Structures, opérations, et algorithmes communs, classés par complexité. diff --git a/fr-fr/crystal-fr.html.markdown b/fr-fr/crystal-fr.html.markdown index 2c4e3dad..2bb17fc5 100644 --- a/fr-fr/crystal-fr.html.markdown +++ b/fr-fr/crystal-fr.html.markdown @@ -305,7 +305,6 @@ end (1..3).each do |index| puts "Index: #{index}" end -# Index: 0 # Index: 1 # Index: 2 # Index: 3 diff --git a/fr-fr/css-fr.html.markdown b/fr-fr/css-fr.html.markdown index 35673c47..74a49c9a 100644 --- a/fr-fr/css-fr.html.markdown +++ b/fr-fr/css-fr.html.markdown @@ -1,5 +1,6 @@ --- language: css +filename: cascading-fr.css contributors: - ["Mohammad Valipour", "https://github.com/mvalipour"] - ["Marco Scannadinari", "https://github.com/marcoms"] diff --git a/fr-fr/d.html.markdown b/fr-fr/d-fr.html.markdown index bfb9f2ce..8d98f9dc 100644 --- a/fr-fr/d.html.markdown +++ b/fr-fr/d-fr.html.markdown @@ -54,7 +54,7 @@ void main() { } while(n > 0); // For et while sont très utiles, mais en D, on préfère foreach. - // Les deux points : '..', créent un intervalle continue de valeurs + // Les deux points : '..', créent un intervalle continu de valeurs // incluant la première mais excluant la dernière. foreach(i; 1..1_000_000) { if(n % 2 == 0) @@ -72,7 +72,7 @@ void main() { } ``` On peut définir de nouveaux types avec les mots-clés `struct`, `class`, -`union` et `enum`. Ces types sont passés au fonction par valeur (ils sont copiés) +`union` et `enum`. Ces types sont passés à la fonction par valeur (ils sont copiés) De plus, on peut utiliser les templates pour rendre toutes ces abstractions génériques. ```c @@ -88,7 +88,7 @@ struct LinkedList(T) { class BinTree(T) { T data = null; - // Si il n'y a qu'un seul paramètre de template, + // S'il n'y a qu'un seul paramètre de template, // on peut s'abstenir de mettre des parenthèses. BinTree!T left; BinTree!T right; @@ -152,7 +152,7 @@ class MyClass(T, U) { T _data; U _other; - // Les constructeurs s'apellent toujours 'this'. + // Les constructeurs s'appellent toujours 'this'. this(T t, U u) { // Ceci va appeller les setters ci-dessous. data = t; @@ -197,8 +197,8 @@ void main() { writefln("Later: data = %d, str = %s", mc.data, mc.other); } ``` -Avec les propriétés, on peut constuire nos setters et nos getters -comme on le souhaite, tout en gardant un syntaxe très propre, +Avec les propriétés, on peut construire nos setters et nos getters +comme on le souhaite, tout en gardant une syntaxe très propre, comme si on accédait directement à des membres de la classe. Les autres fonctionnalités orientées objets à notre disposition @@ -208,8 +208,8 @@ d'une seule classe et implémenter autant d'interface que voulu. Nous venons d'explorer les fonctionnalités objet du D, mais changeons un peu de domaine. D permet la programmation fonctionelle, avec les fonctions -de premier ordre, les fonctions `pure` et les données immuables. -De plus, tout vos algorithmes fonctionelles favoris (map, reduce, filter) +de premier ordre, les fonctions `pures` et les données immuables. +De plus, tout vos algorithmes fonctionels favoris (map, reduce, filter) sont disponibles dans le module `std.algorithm`. ```c @@ -217,11 +217,11 @@ import std.algorithm : map, filter, reduce; import std.range : iota; // construit un intervalle excluant la dernière valeur. void main() { - // On veut un algorithm qui affiche la somme de la listes des carrés + // On veut un algorithme qui affiche la somme de la liste des carrés // des entiers paires de 1 à 100. Un jeu d'enfant ! - // On se content de passer des expressions lambda en paramètre à des templates. - // On peut fournier au template n'importe quelle fonction, mais dans notre + // On se contente de passer des expressions lambda en paramètre à des templates. + // On peut fournir au template n'importe quelle fonction, mais dans notre // cas, les lambdas sont pratiques. auto num = iota(1, 101).filter!(x => x % 2 == 0) .map!(y => y ^^ 2) @@ -231,15 +231,15 @@ void main() { } ``` -Vous voyez comme on a calculé `num` comme on le ferait en haskell par exemple ? -C'est grâce à une innvoation de D qu'on appelle "Uniform Function Call Syntax". -Avec l'UFCS, on peut choisir d'écrire un appelle à une fonction de manière -classique, ou comme un appelle à une méthode. Walter Brighter a écrit un +Vous voyez qu'on a calculé `num` comme on le ferait en haskell par exemple ? +C'est grâce à une innovation de D qu'on appelle "Uniform Function Call Syntax". +Avec l'UFCS, on peut choisir d'écrire un appel à une fonction de manière +classique, ou comme un appel à une méthode. Walter Brighter a écrit un article en anglais sur l'UFCS [ici.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394) Pour faire court, on peut appeller une fonction dont le premier paramètre est de type A, comme si c'était une méthode de A. -J'aime le parallélisme. Vous aimez les parallélisme ? Bien sur que vous aimez ça +J'aime le parallélisme. Vous aimez le parallélisme ? Bien sûr que vous aimez ça. Voyons comment on le fait en D ! ```c @@ -248,7 +248,7 @@ import std.parallelism : parallel; import std.math : sqrt; void main() { - // On veut calculer la racine carré de tous les nombres + // On veut calculer la racine carrée de tous les nombres // dans notre tableau, et profiter de tous les coeurs // à notre disposition. auto arr = new double[1_000_000]; 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/haml-fr.html.markdown b/fr-fr/haml-fr.html.markdown index 24be8bf9..f5d096fe 100644 --- a/fr-fr/haml-fr.html.markdown +++ b/fr-fr/haml-fr.html.markdown @@ -1,6 +1,6 @@ --- language: haml -filename: learnhaml.haml +filename: learnhaml-fr.haml contributors: - ["Simon Neveu", "https://github.com/sneveu"] - ["Thibault", "https://github.com/iTech-"] diff --git a/fr-fr/haskell.html.markdown b/fr-fr/haskell-fr.html.markdown index a34dc098..a34dc098 100644 --- a/fr-fr/haskell.html.markdown +++ b/fr-fr/haskell-fr.html.markdown diff --git a/fr-fr/java-fr.html.markdown b/fr-fr/java-fr.html.markdown new file mode 100644 index 00000000..d6c68343 --- /dev/null +++ b/fr-fr/java-fr.html.markdown @@ -0,0 +1,939 @@ +--- +language: java +contributors: + - ["Jake Prather", "https://github.com/JakeHP"] + - ["Jakukyo Friel", "https://weakish.github.io"] + - ["Madison Dickson", "https://github.com/mix3d"] + - ["Simon Morgan", "https://sjm.io/"] + - ["Zachary Ferguson", "https://github.com/zfergus2"] + - ["Cameron Schermerhorn", "https://github.com/cschermerhorn"] + - ["Rachel Stiyer", "https://github.com/rstiyer"] + - ["Michael Dähnert", "https://github.com/JaXt0r"] + - ["Rob Rose", "https://github.com/RobRoseKnows"] + - ["Sean Nam", "https://github.com/seannam"] +filename: java-fr.java +translators: + - ['Mathieu Gemard', 'https://github.com/mgemard'] +lang: fr-fr +--- +Java est un langage orienté objet, concurrent et très facilement portable. Java +est inspiré du C++ mais ne reprend pas tous les concepts comme par exemple les +pointeurs et en ajoute de nouveaux comme les interfaces. +[En savoir plus.](https://fr.wikipedia.org/wiki/Java_(langage)) + +```java +// Les commentaires sur une seule ligne commencent par // + +/* +Les commentaires sur plusieurs lignes ressemblent à ceci. +*/ + +/** + * Les commentaires de la JavaDoc ressemblent à ceci. Ils sont utilisés pour + * décrire la classe et ses différents attributs. + * Attributs principaux: + * + * @author Nom (et information de contact comme l'email) de(s) auteur(s). + * @version Version actuelle du programme. + * @since Date à laquelle cette partie du programme a été ajouté. + * @param Décrit les différents paramètres pour d'une méthode. + * @return Décrit le retour de la méthode. + * @deprecated Indique si le code est déprécié ou ne doit plus être utilisé. + * @see Lien vers une autre partie de la documentation. +*/ + +// Importe la classe ArrayList qui se trouve dans le package java.util +import java.util.ArrayList; +// Importe toutes les classes qui se trouvent dans le package java.security +import java.security.*; + +// Chaque fichier .java doit contenir une classe public portant le même nom que +le fichier. +public class JavaFr { + + // Pour exécuter un programme Java, celui-ci doit posséder une méthode main + // qui fournir un point d'entrée. + public static void main(String[] args) { + + /////////////////////////////////////// + // Entrée/Sortie + /////////////////////////////////////// + + /* + * Sortie + */ + + // Utilisez System.out.println() pour afficher un texte dans la console. + System.out.println("Hello World!"); + System.out.println( + "Integer: " + 10 + + " Double: " + 3.14 + + " Boolean: " + true); + + // Pour afficher sans retour à la ligne, on utilise System.out.print(). + System.out.print("Hello "); + System.out.print("World"); + + // Utilisez System.out.printf() pour formatter les données à afficher. + System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159 + + /* + * Entrée + */ + + // Utilisez Scanner pour lire l'entrée + // Nécessite: import java.util.Scanner; + Scanner scanner = new Scanner(System.in); + + // Lire une chaîne de caractères + String name = scanner.next(); + + // Lire un byte + byte numByte = scanner.nextByte(); + + // Lire un entier + int numInt = scanner.nextInt(); + + // Lire une entrée de type long + float numFloat = scanner.nextFloat(); + + // Lire une entrée de type double + double numDouble = scanner.nextDouble(); + + // Lire une entrée de type boolean + boolean bool = scanner.nextBoolean(); + + /////////////////////////////////////// + // Variables + /////////////////////////////////////// + + /* + * Déclaration de variable + */ + // Déclarez une variable avec la forme <type> <name> + int fooInt; + // Declarez plusieurs variables du même type <type> <name1>, <name2>, + // <name3> + int fooInt1, fooInt2, fooInt3; + + /* + * Initialisation de variable + */ + + // Initialisez une variable sous la forme <type> <name> = <val> + int barInt = 1; + // Initialisez plusieurs variables du même type et avec la même valeur + // sous la forme + // <type> <name1>, <name2>, <name3> + // <name1> = <name2> = <name3> = <val> + int barInt1, barInt2, barInt3; + barInt1 = barInt2 = barInt3 = 1; + + /* + * Types de variable + */ + // byte - Entier signé utilisant la notation en complément à deux sur + // 8 bits + // (-128 <= byte <= 127) + byte fooByte = 100; + + // Si vous voulez interpréter un byte en entier non-signé, cette simple + // opération peut vous aider + int unsignedIntLessThan256 = 0xff & fooByte; + // cela contraste avec une conversion qui peut être négative. + int signedInt = (int) fooByte; + + // short - Entier signé utilisant la notation en complément à deux sur + // 16 bits + // (-32,768 <= short <= 32,767) + short fooShort = 10000; + + // int - Entier signé utilisant la notation en complément à deux sur + // 32 bits + // (-2,147,483,648 <= int <= 2,147,483,647) + int bazInt = 1; + + // long - Entier signé utilisant la notation en complément à deux sur + // 64 bits + // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) + long fooLong = 100000L; + // L est utilisé pour indiquer que la variable est de type long; + // le nombre serait traité comme un int sans le L + + // Note: byte, short, int et long sont signés. Ils peuvent avoir des + // valeurs positives et négatives. + // Il n'existe pas de variantes non-signées. + // char, toutefois, est non-signé sur 16 bits + + // float - nombre à virgule flottante selon la norme IEEE 754 utilisant + // le format simple précision sur 32 bits + // 2^-149 <= float <= (2-2^-23) * 2^127 + float fooFloat = 234.5f; + // f ou F sont utilisés pour indiquer que la variable est de type float; + // autrement elle serait traitée comme un double. + + // double - nombre à virgule flottante selon la norme IEEE 754 utilisant + // le format double précision sur 64 bits + // 2^-1074 <= x <= (2-2^-52) * 2^1023 + double fooDouble = 123.4; + + // boolean - vrai & faux + boolean fooBoolean = true; + boolean barBoolean = false; + + // char - un caractère Unicode sur 16 bits + char fooChar = 'A'; + + // les variables final ne peuvent pas être réassignés à un autre objet, + final int HOURS_I_WORK_PER_WEEK = 9001; + // mais ils peuvent être initialisés plus tard. + final double E; + E = 2.71828; + + // BigInteger - entier immuable de taille arbitraire + // + // BigInteger est un type de donné qui autorise les développeurs à + // manipuler des entiers au delà de 64 bits. Les entiers sont stockés + // dans un tableau de bytes et sont manipulés grâce à des functions + // de la classe BigIntger + // + // BigInteger peut être initialiser en utilisant un tableau de bytes ou + // une chaîne de caractère. + BigInteger fooBigInteger = new BigInteger(fooByteArray); + + // BigDecimal - entier immuable et positif de taille arbitraire + // + // BigDecimal comprend deux parties: une entier de taille arbitraire + // (BigInteger) et un entier de 32 bits représantant la position de la + // virgule. + // + // BigDecimal donne aux développeurs un contrôle total pour l'arrondie + // à la décimale. Il est recommandé de l'utiliser pour les valeurs + // monétaires et pour les cas où la value exacte de l'arondie à la + // décimale est requis. + // + // BigInteger peut être initialiser en utilisant un int, long, double ou + // String. + // On peut également utiliser un BigInteger et un int pour la + // position de la virgule. + BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt); + + // Sachez que la création d'un BigDecimal avec un float ou + // un double prendra en compte l'inexactitude des représention en float + // ou double. + // Préférez String pour une représention exacte. + BigDecimal tenCents = new BigDecimal("0.1"); + + // String - Chaîne de caractères + String fooString = "My String Is Here!"; + + // \n est un caractère d'échappement qui indique une nouvelle ligne + String barString = "Printing on a new line?\nNo Problem!"; + // \t est un caractère d'échappement qui indique une tabulation + String bazString = "Do you want to add a tab?\tNo Problem!"; + System.out.println(fooString); + System.out.println(barString); + System.out.println(bazString); + + // Construction de chaînes de caractères + // #1 - avec l'opérateur + + // C'est la manière la plus simple et optimisé par le compilateur + String plusConcatenated = "Strings can " + "be concatenated " + "via + operator."; + System.out.println(plusConcatenated); + // Affiche: Strings can be concatenated via + operator. + + // #2 - avec StringBuilder + // Cette méthode ne nécessite pas d'objet String intermédiaire. Elle + // stocke juste les différentes chaînes de caractères et les assemble + // lorsque la méthode toString() est appelée. + // Attention: Cette classe n'est pas thread-safe (l'objet ne peut pas être partagé + // entre les threads). Une alternative + // (avec un impact sur les performances) thread-safe est d'utiliser la + // classe StringBuffer. + StringBuilder builderConcatenated = new StringBuilder(); + builderConcatenated.append("You "); + builderConcatenated.append("can use "); + builderConcatenated.append("the StringBuilder class."); + System.out.println(builderConcatenated.toString()); // only now is the string built + // Affiche: You can use the StringBuilder class. + + // StringBuffer est efficace quand la chaîne de caractères n'est pas + // utilisée avec la fin de sa construction. + StringBuilder stringBuilder = new StringBuilder(); + String inefficientString = ""; + for (int i = 0 ; i < 10; i++) { + stringBuilder.append(i).append(" "); + inefficientString += i + " "; + } + System.out.println(inefficientString); + System.out.println(stringBuilder.toString()); + // inefficientString est moins performant car une chaîne de caractères + // est créée à chaque itération de la boucle. + // Les concaténations avec + sont compilés en un StringBuilder et + // toString(). + // Evitez les concaténations de string dans les boucles. + + // #3 - avec la méthode format() de la classe String. + // Une autre alternative. Rapide et lisible. + String.format("%s may prefer %s.", "Or you", "String.format()"); + // Affiche: Or you may prefer String.format(). + + // Tableau + // La taille du tableau doit être précisée à l'instantiation + // Les formats suivant sont possibles pour déclarer un tableau + // <datatype>[] <var name> = new <datatype>[<array size>]; + // <datatype> <var name>[] = new <datatype>[<array size>]; + int[] intArray = new int[10]; + String[] stringArray = new String[1]; + boolean boolArray[] = new boolean[100]; + + // Une autre manière de déclarer et initialiser un tableau + int[] y = {9000, 1000, 1337}; + String names[] = {"Bob", "John", "Fred", "Juan Pedro"}; + boolean bools[] = {true, false, false}; + + // Accéder à un élément + System.out.println("intArray @ 0: " + intArray[0]); + + // Les tableaus commencent à 0 et sont muables + intArray[1] = 1; + System.out.println("intArray @ 1: " + intArray[1]); // => 1 + + // Les autres types de donnés utiles sont + // ArrayList - Identique aux tableaux mais avec plus de fonctionnalités + // et de taille muable. + // LinkedList - Implémentation de listes doublement chaînées. Toutes Les + // opérations éffectuées le sont comme attendue pour une + // liste doublement chaînée. + // Map - Une collection d'objets qui fait correspondre une valeur à une + // clé. Map est une interface et ne peut pas être instantiée. Le + // type des clés et des valeurs doit être précisés à + // l'instantiation. Chaque clé doit correspondre à une seule + // valeur et chaque clé doit être unique (pas de clés dupliquées). + // HashMap - Cette classe utilise une table de hachage pour implémenter + // l'interface Map. Cela garantie que le temps d'exécution des + // opérations basiques, comme get (récuper une valeur) et + // insert (insérer une valeur), reste constant quelque soit la + // la taille. + // TreeMap - Cette classe utilise une structure en arbre et est + // ordonnée. Elle implémente un arbre bicolore (ou arbre rouge + // et noir) et ordonne les éléments en se basant sur la clé ou + // en utilisant un comparateur fournit à la création. + + /////////////////////////////////////// + // Opérateurs + /////////////////////////////////////// + System.out.println("\n->Operators"); + + int i1 = 1, i2 = 2; // Raccourcis pour des déclarations multiples + + // L'arithmétique + System.out.println("1+2 = " + (i1 + i2)); // => 3 + System.out.println("2-1 = " + (i2 - i1)); // => 1 + System.out.println("2*1 = " + (i2 * i1)); // => 2 + System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int returns int) + System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5 + + // Le modulo + System.out.println("11%3 = "+(11 % 3)); // => 2 + + // Opérateurs de comparaison + System.out.println("3 == 2? " + (3 == 2)); // => faux + System.out.println("3 != 2? " + (3 != 2)); // => vrai + System.out.println("3 > 2? " + (3 > 2)); // => vrai + System.out.println("3 < 2? " + (3 < 2)); // => faux + System.out.println("2 <= 2? " + (2 <= 2)); // => vrai + System.out.println("2 >= 2? " + (2 >= 2)); // => vrai + + // Opérateurs boolean + System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false + System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true + System.out.println("!(3 == 2)? " + (!(3 == 2))); // => true + + // Opérateurs sur les bits + /* + ~ Complément à un + << Décalage des bits vers la gauche + >> Décalage des bits vers la droite, le signe est conservé + >>> Décalage des bits vers la droite, zéro est utilisé pour les bits + les plus à gauche + & Opérateur ET + ^ Opérateur OU exlusif + | Opérateur OU inclusif + */ + + // Opérateurs d'incrémentation + int i = 0; + System.out.println("\n->Inc/Dec-rementation"); + // Les opérateurs ++ et -- incrémentent et décrémentent respectivement + // de 1. + // S'ils sont placés avant la variable, ils incrémentent la variable puis + // retournent la valeur. Placés après la varible, ils retournent la variable + // puis l'incrémentent. + System.out.println(i++); // i = 1, affiche 0 (pré-incrément) + System.out.println(++i); // i = 2, affiche 2 (post-incrément) + System.out.println(i--); // i = 1, affiche 2 (post-incrément) + System.out.println(--i); // i = 0, affiche 0 (pré-incrément) + + /////////////////////////////////////// + // Structures de contôles + /////////////////////////////////////// + System.out.println("\n->Control Structures"); + + // Les instructions conditionnelle sont identiques aux langage C + int j = 10; + if (j == 10) { + System.out.println("I get printed"); + } else if (j > 10) { + System.out.println("I don't"); + } else { + System.out.println("I also don't"); + } + + // Bouble while + int fooWhile = 0; + while(fooWhile < 100) { + System.out.println(fooWhile); + // Incrémente le compteur + // Itéré 100 fois, fooWhile 0,1,2...99 + fooWhile++; + } + System.out.println("fooWhile Value: " + fooWhile); + + // Boucle do-while + int fooDoWhile = 0; + do { + System.out.println(fooDoWhile); + // Incrémente le compteur + // Itéré 99 fois, fooDoWhile 0->99 + fooDoWhile++; + } while(fooDoWhile < 100); + System.out.println("fooDoWhile Value: " + fooDoWhile); + + // Boucle for + // De la forme for(<start_statement>; <conditional>; <step>) + for (int fooFor = 0; fooFor < 10; fooFor++) { + System.out.println(fooFor); + // Itéré 10 fois, fooFor 0->9 + } + System.out.println("fooFor Value: " + fooFor); + + // Fin d'une boucle for avec un label + outer: + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + if (i == 5 && j ==5) { + break outer; + // termine l'itération de la boucle englobante avec le label outer + } + } + } + + // Boucle for-each + // La boucle for est également capable d'itérer aussi bien sur un + // tableau que sur des objets qui implémentent l'interface Iterable. + int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + // De la forme: for (<object> : <iterable>) + // Lu comme: "Pour chaque élément du tableau" + // note: le type doit correspondre à celui de l'objet itérable + for (int bar : fooList) { + System.out.println(bar); + //Itère 9 fois et affiche les chiffres de 1 à 9 + } + + // Le switch-case + // Un switch fonctionne avec les données de type byte, short, char et + // int. + // On peut également utiliser le type Enum, la classe String et les + // classes spéciales qui englobent les types primitifs (Character, Byte, + // Short et Integer). + // Depuis Java 7, on peut utiliser le type String. + int month = 3; + String monthString; + switch (month) { + case 1: monthString = "January"; + break; + case 2: monthString = "February"; + break; + case 3: monthString = "March"; + break; + default: monthString = "Some other month"; + break; + } + System.out.println("Switch Case Result: " + monthString); + + // try-with-resources (Java 7+) + // Le mécanisme de gestion des erreurs try-catch-finally peut être + // utilisé mais depuis Java 7 il est également possible d'utiliser + // try-with-ressources. + // try-with-resources simplifie try-catch-finally en fermant + // automatiquement les ressources + + // Pour utiliser un try-with-resources, il suffit d'inclure l'instance + // d'une classe qui implémente l'interface java.lang.AutoCloseable + try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) { + // Ici, vous pouvez essayer de faire quelque chose qui lance une + // exception. + System.out.println(br.readLine()); + // Avec Java 7, la ressource sera toujours fermé, même si elle lance + // une exception. + } catch (Exception ex) { + // La ressource sera fermé avant que le catch s'exécute. + System.out.println("readLine() failed."); + } + // Il n'y a pas besoin de finally dans ce cas, l'objet BufferedReader + // sera déjà fermé. Cela peut être utile dans certains cas spécifiques + // où le code contenu dans finally ne serait pas exécuté. + // Consulter la documention Oracle pour en savoir plus (en anglais) : + // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html + + + // Expression ternaire + // Vous pouvez utiliser l'opérateur ternaire '?' pour faire un + // assignement rapide avec une condition logique. + // Il faut lire "Si la (condition) est vraie alors utiliser la + // <première valeur> sinon utilisez la <deuxième valeur>". + int foo = 5; + String bar = (foo < 10) ? "A" : "B"; + System.out.println("bar : " + bar); // Affiche "bar : A", car la condition est vraie + // Ou alors plus simplement + System.out.println("bar : " + (foo < 10 ? "A" : "B")); // Affiche également "bar : A" + + //////////////////////////////////////// + // Conversion de type + //////////////////////////////////////// + + // Autoboxing + + // Convertir un objet String en un objet Integer + Integer.parseInt("123"); // retourne un le type primitif int de 123 + + // Convert Integer To String + Integer.toString(123); // retourne un object String correspondant à"123" + + // Pour les autres conversions, référer vous aux classes suivantes: + // Double + // Long + // String + + /////////////////////////////////////// + // Classes et fonctions + /////////////////////////////////////// + + System.out.println("\n->Classes & Functions"); + + // (voir plus loin pour la définition de la classe Bicycle) + + // Utilisez new pour instancier une classe + Bicycle trek = new Bicycle(); + + // Pour appeler une méthode de l'objet + trek.speedUp(3); // !! Il est conseillé de passer par une méthode pour + // changer la valeur d'une variable. + trek.setCadence(100); + + // toString retourne une représentation de l'objet en chaîne de caractères. + System.out.println("trek info: " + trek.toString()); + + // Initialisation avec double accolades + // Le langage Java ne permet pas de créer des collections statiques d'une + // manière simple. Généralement, on utilise la forme suivante: + private static final Set<String> COUNTRIES = new HashSet<String>(); + static { + COUNTRIES.add("DENMARK"); + COUNTRIES.add("SWEDEN"); + COUNTRIES.add("FINLAND"); + } + + // Mais on peut le faire d'une manière plus habile, dite initialisation + // avec double semi-colonnes + private static final Set<String> COUNTRIES = new HashSet<String>() {{ + add("DENMARK"); + add("SWEDEN"); + add("FINLAND"); + }} + + // La première semi-colonne crée une classe anonyme et la deuxième est + // un bloc d'initialisation du bloc. Ce dernier est appelé lorsque Copyright (c) + // classe anonyme est crée. Cela ne fonctionne pas uniquement pour les + // collections mais également pour toutes les classes n'étant pas + // déclarées comme final. + + } // Fin de la méthode main +} // Fin de la class JavaFr + +// Vous pouvez inclure des classes qui ne sont pas publics dans un fichier Java. +// Cependant, il est préférable de séparer les +// classes dans des fichiers différents. + +// Syntaxe de déclaration des classes: +// <public/private/protected> class <Nom de la classe> { +// // Les attributs, les constructeurs et les méthodes de la classe vont ici. +// // Les functions de classes sont appelées méthode. +// } + +class Bicycle { + + // Attributs et variables de la classe Bicycle + public int cadence; // Public: Peut être accesible depuis n'importe où + private int speed; // Private: Accisible depuis la classe + protected int gear; // Protected: Accisible depuis la classe et ses sous- + // classes + String name; // default: Uniquement accesible depuis ce package + static String className; // Variable de classe static + + // Bloc static + // Java n'a pas d'implémentation pour les constructeurs statiques mais + // possède le bloc static qui peut être utilisé pour initialiser les + // variables de classe. + // Ce bloc sera appelé lorsque la classe sera chargée. + static { + className = "Bicycle"; + } + + // Les constructeurs sont un moyen de créer les classe + // Ceci est le constructeur de la classe Bicycle + public Bicycle() { + // Vous pouvez aussie appeler un autre constructeur. Par exemple en + // appelant le constructeur de la classe mère (voir héritage): + // this(1, 50, 5, "Bontrager"); + gear = 1; + cadence = 50; + speed = 5; + name = "Bontrager"; + } + // Le constructeur peut prendre plusieurs arguments + public Bicycle(int startCadence, int startSpeed, int startGear, + String name) { + this.gear = startGear; + this.cadence = startCadence; + this.speed = startSpeed; + this.name = name; + } + + // Syntaxe d'une méthode : + // <public/private/protected> <type de retour> <nom de la fonction>( + // <arguments>) + + // Les classes Java possèdent souvent des accesseurs (getters) et mutateurs + // (setters) pour leurs attributs. + + public int getCadence() { + return cadence; + } + + // Les méthodes void ne retourne aucune valeur + public void setCadence(int newValue) { + cadence = newValue; + } + public void setGear(int newValue) { + gear = newValue; + } + public void speedUp(int increment) { + speed += increment; + } + public void slowDown(int decrement) { + speed -= decrement; + } + public void setName(String newName) { + name = newName; + } + public String getName() { + return name; + } + + // Méthode pour afficher la valeur des attributs de l'objet. @Override est + // une annotation (voir plus loin). + @Override //On dit ici qu'on remplace la méthode de la classe Objet. + public String toString() { + return "gear: " + gear + " cadence: " + cadence + " speed: " + speed + + " name: " + name; + } +} // Fin de la classe Bicycle + +// PennyFarthing est une sous-classe de Bicycle +class PennyFarthing extends Bicycle { + // (Les Penny Farthings sont des bicyclette avec une grande roue avant. + // Il n'y a pas de roue libre, le cycliste est obligé de pédaler en + // permanence.) + + public PennyFarthing(int startCadence, int startSpeed) { + // Appelez le constructeur parent avec la méthode super() + super(startCadence, startSpeed, 0, "PennyFarthing"); + } + + // Ici nous modifions la méthode setGear() de la classe mère. Il faut donc + // utiliser l'annotation @Overide. Pour en savoir plus sur les annotations, + // consulter la documention officiel (en anglais) : + // out: http://docs.oracle.com/javase/tutorial/java/annotations/ + @Override + public void setGear(int gear) { + this.gear = 0; + } +} + +// Polymorphisme (cast d'objets) +// Comme la classe PennyFarthing héritent de la classe Bicycle, on peut dire +// qu'un PennyFarthing est un Bicycle (un vélo en anglais) et écrire : +// Bicycle bicycle = new PennyFarthing(); +// Le polymorphisme est la capacité d'un objet de se faire passer pour un autre. +// Vous pouvez consulter la documentation Oracle pour plus de détails et +// concepts (en anglais) : +// https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html + +// Interfaces +// Déclaration d'une interface +// <niveau d'accès> interface <nom de l'interface> extends <nom de l'interface +// mère> { +// // Constantes +// // Délaration des méthodes +// } + +// Exemple - Toute nourriture peut être mangée et digégée différemment +// L'interface Edible (traduction : comestible) décrit l'action de manger +public interface Edible { + public void eat(); // Toute classe qui implémente cette interface doit + // implémenter cette méthode +} + +// L'interface Digestible décrit l'action de digérer +public interface Digestible { + public void digest(); + // Depuis Java 8, les interfaces peuvent avoir des méthodes par défaut. + public void defaultMethod() { + System.out.println("Hi from default method ..."); + } +} + +// On peut maintenant créer une classe qui implémente chacune de ces interfaces. +public class Fruit implements Edible, Digestible { + @Override + public void eat() { + // ... + } + + @Override + public void digest() { + // ... + } +} + +// En Java, on peut hériter uniquement d'une classe mais on peut implémenter +// plusieurs interfaces: +public class ExampleClass extends ExampleClassParent implements InterfaceOne, + InterfaceTwo { + @Override + public void InterfaceOneMethod() { + } + + @Override + public void InterfaceTwoMethod() { + } + +} + +// Classes abstraites + +// Syntaxe de déclaration: +// <niveau d'accès> abstract class <nom de la classe abstraite> extends <nom de la +// classe mère abstraite> { +// // Constantes et variables +// // Méthodes +// } + +// Une classe abstraite contient au moins une méthode abstraite qui doit être +// définee dans la classe fille. Comme les interfaces, les classes abstraites ne +// peuvent pas être instanciées mais doivent être étendues avec les méthodes +// abstraites implémentées. À la différence des interfaces, une classe abstraite +// peut contenir des méthodes abstraites ou non-abstraites. Les méthodes dans une +// interfaces ne peuvent pas être implémentées à l'exception des méthodes static. +// Les variables d'une classe abstraite sont déclarées comme final par défaut à +// l'opposé des interfaces. Finalement les classes abstraites peuvent avoir une +// méthode main. +public abstract class Animal +{ + public abstract void makeSound(); + + // Les méthodes peuvent avoir une implémentation dans une classe abstraite. + public void eat() + { + System.out.println("I am an animal and I am Eating."); + // Note: On peut accéder à une variable privée ici. + age = 30; + } + + // On n'a pas besoin d'initialiser les variables dans les classe abstraites. + // Cependant, dans une interfaces, les variables sont implicitement + // déclarées comme final et doivent donc être initialisées. + private int age; + + public void printAge() + { + System.out.println(age); + } + + // Les classes abstraites peuvent avoir une fonction main. + public static void main(String[] args) + { + System.out.println("I am abstract"); + } +} + +class Dog extends Animal +{ + // On doit également utiliser l'annotation @Override lors de la surchage de + // la méthode abstraite d'une classe abstraite. + @Override + public void makeSound() + { + System.out.println("Bark"); + // age = 30; ==> ERREUR! age est privé et n'est pas accesible. + } + + // NOTE: Vous obtiendrez une erreur si vous utilisé l'annotation @Override + // ici car Java n'autorise pas la surcharge de méthodes statiques. Ce qui ce + // passe est appelé "method hiding". Si vous voulez en savoir plus, + // consultez cette discussion (en anglais) : + // http://stackoverflow.com/questions/16313649/ + public static void main(String[] args) + { + Dog pluto = new Dog(); + pluto.makeSound(); + pluto.eat(); + pluto.printAge(); + } +} + +// Classes finales + +// Syntaxe de déclaration +// <niveau d'accès> final <nom de la classe final> { +// // Constantes et variables +// // Méthodes déclarations +// } + +// Les classe déclarées comme final ne peuvent pas avoir de classe fille. Elles +// peuvent être considérées comme l'opposé des classes abstraites. +public final class SaberToothedCat extends Animal +{ + // On doit également utiliser l'annotation @Override lors de la surchage de + // la méthode abstraite d'une classe abstraite. + @Override + public void makeSound() + { + System.out.println("Roar"); + } +} + +// Méthodes final +public abstract class Mammal() +{ + // Syntaxe: + // <niveau d'accès> final <type de retour> <nom de la fonction>(<arguments>) + + // Les méthodes déclarées comme final ne peuvent pas être surchargées par + // une classe fille et en sont donc l'implémentation finale. + public final boolean isWarmBlooded() + { + return true; + } +} + +// Enumérations +// +// Le type enum est un type de donnée spécial qui permet à une variable de ne +// prendre que certaines valeurs prédéfinies. La variable doit être égales à une +// des valeurs pédéfinies pour celle-ci. En Java, les variables constantes sont +// notées en majuscules. +// On définie un type enum en utilisant le mot clé enum. Par exemple pour les +// jours de l'année: +public enum Day { + SUNDAY, MONDAY, TUESDAY, WEDNESDAY, + THURSDAY, FRIDAY, SATURDAY +} + +// On l'utilise ainsi: +public class EnumTest { + // On utilise notre énumération + Day day; + + public EnumTest(Day day) { + this.day = day; + } + + public void tellItLikeItIs() { + switch (day) { + case MONDAY: + System.out.println("Mondays are bad."); + break; + case FRIDAY: + System.out.println("Fridays are better."); + break; + case SATURDAY: + case SUNDAY: + System.out.println("Weekends are best."); + break; + default: + System.out.println("Midweek days are so-so."); + break; + } + } + + public static void main(String[] args) { + EnumTest firstDay = new EnumTest(Day.MONDAY); + firstDay.tellItLikeItIs(); // => affiche "Mondays are bad" + EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); + thirdDay.tellItLikeItIs(); // => affiche "Midweek days are so-so" + } +} + +// Le type enum permet de faire bien plus que ce qui est montré ici. Il ne se +// limite pas à une liste de constante mais peut inclure des champs et méthodes. +// Vous pouvez en savoir plus ici (en anglais): +//https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html + +``` + +## Pour aller plus loin (en anglais) + +Les liens ci-dessous sont données si vous souhaitez approfondir sur le sujet, +n'hésitez pas à consulter Google pour trouver des exemples spécifiques. + +**Guides officiels d'Oracle**: + +* [Java Tutorial Trail from Sun / Oracle](https://docs.oracle.com/javase/tutorial/index.html) + +* [Java Access level modifiers](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) + +* [Object-Oriented Programming Concepts](https://docs.oracle.com/javase/tutorial/java/concepts/index.html): + * [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) + * [Polymorphism](https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html) + * [Abstraction](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) + +* [Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html) + +* [Interfaces](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html) + +* [Generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html) + +* [Java Code Conventions](https://www.oracle.com/technetwork/java/codeconvtoc-136057.html) + +* Nouvelles fonctionnalités Java 8: + * [Lambda expressions (functional programming)](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html) + * [Date and time API (java.time package)](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) + +**Pratiquer en ligne et tutoriels** + +* [Learneroo.com - Learn Java](http://www.learneroo.com) + +* [Codingbat.com](http://codingbat.com/java) + +**Livres**: + +* [Head First Java](http://www.headfirstlabs.com/books/hfjava/) + +* [Thinking in Java](http://www.mindview.net/Books/TIJ/) + +* [Objects First with Java](https://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660) + +* [Java The Complete Reference](https://www.amazon.com/gp/product/0071606300) 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/markdown.html.markdown b/fr-fr/markdown-fr.html.markdown index 2e4e8461..8518f35c 100644 --- a/fr-fr/markdown.html.markdown +++ b/fr-fr/markdown-fr.html.markdown @@ -14,7 +14,7 @@ Faites moi autant de retours que vous voulez! Sentez vous libre de "forker" et envoyer des pull request! -```markdown +```md <!-- Markdown est une sorte de cousin du HTML, si bien que tout document HTML est un document Markdown valide. Autrement dit, vous pouvez utiliser des balises HTML dans un fichier Markdown, comme la balise commentaire dans diff --git a/fr-fr/php.html.markdown b/fr-fr/php-fr.html.markdown index f4eaf396..45a02d75 100644 --- a/fr-fr/php.html.markdown +++ b/fr-fr/php-fr.html.markdown @@ -1,14 +1,16 @@ --- language: PHP +filename: php-fr.php contributors: - ["Malcolm Fell", "http://emarref.net/"] - ["Trismegiste", "https://github.com/Trismegiste"] translators: - ["Pascal Boutin", "http://pboutin.net/"] + - ["Julien M'Poy", "https://github.com/groovytron"] lang: fr-fr --- -This document describes PHP 5+. +Ce document décrit PHP 5+. ```php // Le code PHP doit être placé à l'intérieur de balises '<?php' @@ -47,7 +49,7 @@ Hello World Again! // Un nom de variable valide commence par une lettre ou un souligné, // suivi de n'importe quelle lettre, nombre ou de soulignés. -// Les valeurs booléenes ne sont pas sensibles à la casse +// Les valeurs booléennes ne sont pas sensibles à la casse $boolean = true; // ou TRUE ou True $boolean = false; // ou FALSE ou False @@ -83,30 +85,30 @@ $number /= $float; // Divise et assigne le quotient à $number $sgl_quotes = '$String'; // => '$String' // Évitez les guillemets sauf pour inclure le contenu d'une autre variable -$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' +$dbl_quotes = "Ceci est une $sgl_quotes."; // => 'Ceci est une $String.' // Les caractères spéciaux sont seulement échappés avec des guillemets -$escaped = "This contains a \t tab character."; -$unescaped = 'This just contains a slash and a t: \t'; +$escaped = "Ceci contient \t une tabulation."; +$unescaped = 'Ceci contient juste un slash et un t: \t'; // En cas de besoin, placez la variable dans des accolades -$money = "I have $${number} in the bank."; +$money = "J'ai $${number} sur mon compte en banque."; // Depuis PHP 5.3, Nowdoc peut être utilisé pour faire des chaînes // multi-lignes non-interprétées $nowdoc = <<<'END' -Multi line -string +String +mutli-lignes END; // Heredoc peut être utilisé pour faire des chaînes multi-lignes interprétées $heredoc = <<<END -Multi line $sgl_quotes +multi-lignes END; // La concaténation de chaînes se fait avec un . -echo 'This string ' . 'is concatenated'; +echo 'Cette string ' . 'est concatenée'; // => 'Cette string est concaténée' /******************************** @@ -121,7 +123,7 @@ echo 'This string ' . 'is concatenated'; define("FOO", "something"); // on peut accéder à une constante en utilisant directement son nom -echo 'This outputs '.FOO; +echo 'Ceci affiche ' . FOO; /******************************** @@ -148,6 +150,14 @@ $array[] = 'Four'; // Retrait d'un élément du tableau unset($array[3]); +// Depuis PHP 7, il est possible de déclarer des tableaux constants en +// utilisant 'define'. +define('ANIMAUX', [ + 'chien', + 'chat', + 'oiseau', +]); + /******************************** * Affichage */ @@ -158,11 +168,13 @@ echo('Hello World!'); print('Hello World!'); // Pareil à "écho" -// Pour écho, vous n'avez pas besoin des parenthèses +// 'echo' et 'print' sont des language constructs. +// Il n'ont pas besoin de parenthèses car ils sont traités comme +// des opérateurs unaires. echo 'Hello World!'; -print 'Hello World!'; // Pour print non plus +print 'Hello World!'; -$paragraph = 'paragraph'; +$paragraph = 'paragraphe'; echo 100; // Affichez un scalaire directement echo $paragraph; // ou des variables @@ -201,7 +213,8 @@ $b = '0'; $c = '1'; $d = '1'; -// assert affiche un avertissement dans son argument n'est pas vrai +// assert affiche un avertissement quand l'expression booléenne passée +// en argument n'est pas vraie. // Ces comparaisons vont toujours être vraies, même si leurs // types ne sont pas les mêmes. @@ -314,7 +327,7 @@ if ($x === '0') { switch ($x) { case '0': print 'Les switch font du transtypage implicite'; - break; // Il est important de déclaré un 'break', sinon les cas + break; // Il est important de déclarer un 'break', sinon les cas // 'two' et 'three' seront évalués case 'two': case 'three': @@ -389,9 +402,10 @@ function my_function () { echo my_function(); // => "Hello" -// Les noms de fonction débutent par le symbole $ -// Un nom de variable valide commence par une lettre ou un souligné, +// Un nom de fonction valide commence par une lettre ou un souligné, // suivi de n'importe quelle lettre, nombre ou de soulignés. +// Les noms des arguments d'une fonction doivent respecter le même format que +// celui des variables. function add ($x, $y = 1) { // $y est facultatif et sa valeur par défaut est 1 $result = $x + $y; @@ -518,7 +532,7 @@ class MyClass public static function myStaticMethod() { - print 'I am static'; + print 'Je suis static'; } } @@ -526,7 +540,7 @@ class MyClass echo MyClass::MY_CONST; // Outputs 'value'; echo MyClass::$staticVar; // Retourne 'static'; -MyClass::myStaticMethod(); // Retourne 'I am static'; +MyClass::myStaticMethod(); // Retourne 'Je suis static'; // On peut instancier une classe en utilisant le mot clé 'new' $my_class = new MyClass('An instance property'); @@ -583,7 +597,7 @@ echo $x->property; // Va utiliser la méthode __get() $x->property = 'Something'; // Va utiliser la méthode __set() // Les classes peuvent être abstraites (en utilisant le mot clé 'abstract'), ou -// elle peuvent implémenter une interface (en utilisant le mot clé 'implement'). +// elle peuvent implémenter une interface (en utilisant le mot clé 'implements'). // Une interface peut être déclarée avec le mot clé 'interface' @@ -636,6 +650,35 @@ class SomeOtherClass implements InterfaceOne, InterfaceTwo } } +// Il est possible de déclarer des classes internes anonymes depuis PHP 7 + +interface Logger { + public function log(string $msg); +} + +class Application { + private $logger; + + public function getLogger(): Logger { + return $this->logger; + } + + public function setLogger(Logger $logger) { + $this->logger = $logger; + } +} + +$app = new Application; + +$app->setLogger(new class implements Logger { + public function log(string $msg) { + echo $msg; + } +}); + +var_dump($app->getLogger()); // => 'object(class@anonymous)#2 (0) {}' + + /******************************** * Espaces de noms (namespaces) */ diff --git a/fr-fr/python3-fr.html.markdown b/fr-fr/python3-fr.html.markdown index 3d60157c..7112cd90 100644 --- a/fr-fr/python3-fr.html.markdown +++ b/fr-fr/python3-fr.html.markdown @@ -7,12 +7,13 @@ contributors: - ["Zachary Ferguson", "http://github.com/zfergus2"] translators: - ["Gnomino", "https://github.com/Gnomino"] + - ["Julien M'Poy", "http://github.com/groovytron"] filename: learnpython3-fr.py lang: fr-fr --- Python a été créé par Guido Van Rossum au début des années 90. C'est maintenant un des -langages les populaires. Je suis tombé amoureux de Python pour la clarté de sa syntaxe. +langages les plus populaires. Je suis tombé amoureux de Python pour la clarté de sa syntaxe. C'est tout simplement du pseudo-code exécutable. L'auteur original apprécierait les retours (en anglais): vous pouvez le contacter sur Twitter à [@louiedinh](http://twitter.com/louiedinh) ou par mail à l'adresse louiedinh [at] [google's email service] @@ -131,6 +132,10 @@ b == a # => True, les objets a et b ne pointent pas sur le même objet # On peut aussi utiliser des mots clés pour éviter de devoir compter. "{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna" +# Il est également possible d'utiliser les f-strings depuis Python 3.6 (https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals) +name = "Fred" +f"Il a dit que son nom est {name}." #=> "Il a dit que son nom est Fred." + # Si votre code doit aussi être compatible avec Python 2.5 et moins, # vous pouvez encore utiliser l'ancienne syntaxe : "Les %s peuvent être %s avec la %s méthode" % ("chaînes", "interpolées", "vieille") @@ -220,7 +225,7 @@ del li[2] # li is now [1, 2, 3] li + other_li # => [1, 2, 3, 4, 5, 6] # Concaténer des listes avec "extend()" -li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] +li.extend(other_li) # Maintenant li contient [1, 2, 3, 4, 5, 6] # Vérifier la présence d'un objet dans une liste avec "in" 1 in li # => True @@ -476,7 +481,7 @@ def add(x, y): # Appel d'une fonction avec des paramètres : add(5, 6) # => affiche "x est 5 et y est 6" et retourne 11 -# Une autre manière d'appeller une fonction : avec des arguments +# Une autre manière d'appeler une fonction : avec des arguments add(y=6, x=5) # Les arguments peuvent être dans n'importe quel ordre. # Définir une fonction qui prend un nombre variable d'arguments @@ -570,7 +575,7 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] #################################################### -# On utilise l'opérateur "classe" pour définir une classe +# On utilise l'opérateur "class" pour définir une classe class Human: # Un attribut de la classe. Il est partagé par toutes les instances de la classe. @@ -662,7 +667,7 @@ def double_numbers(iterable): # Un générateur crée des valeurs à la volée. # Au lieu de générer et retourner toutes les valeurs en une fois, il en crée une à chaque -# itération. Cela signifie que les valeurs supérieures à 15 ne seront pas traîtées par +# itération. Cela signifie que les valeurs supérieures à 30 ne seront pas traîtées par # double_numbers. # Note : range est un générateur aussi. # Créer une liste 1-900000000 prendrait beaucoup de temps @@ -716,6 +721,10 @@ print(say(say_please=True)) # affiche Can you buy me a beer? Please! I am poor * [Python Course](http://www.python-course.eu/index.php) * [First Steps With Python](https://realpython.com/learn/python-first-steps/) +### En ligne et gratuit (en français) + +* [Le petit guide des batteries à découvrir](https://he-arc.github.io/livre-python/) + ### Livres (en anglais) * [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) diff --git a/fr-fr/ruby-ecosystem-fr.html.markdown b/fr-fr/ruby-ecosystem-fr.html.markdown index 9b52069a..edc69068 100644 --- a/fr-fr/ruby-ecosystem-fr.html.markdown +++ b/fr-fr/ruby-ecosystem-fr.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Rafal Chmiel", "http://github.com/rafalchmiel"] translators: - ["Xuan-thi Nguyen", "http://github.com/mellenguyen"] + - ["Sylvain Abélard", "http://github.com/abelards"] lang: fr-fr --- @@ -17,9 +18,9 @@ dépendances des gems. ## Ruby Managers Quelques plateformes possèdent Ruby pré-installé ou disponible en tant que -paquet. La plupart des rubyists ne les utilisent pas, ou si c'est le cas, ne -les utilise que pour faire démarrer un autre installateur ou implémentation de -Ruby. Les rubyists tendent plutôt à installer un manager Ruby pour installer +paquet. La plupart des rubyistes ne les utilisent pas, ou si c'est le cas, ne +les utilisent que pour faire démarrer un autre installateur ou implémentation de +Ruby. Les rubyistes tendent plutôt à installer un gestionnaire en Ruby pour installer et changer entre les différentes et nombreuses versions de Ruby et les environnements de leurs projets Ruby. @@ -47,14 +48,14 @@ Implementation). Les trois versions majeures de Ruby actuellement utilisées sont : -* 2.0.0 - Sortie en février 2013. La plupart des librairies et frameworks +* 2.0.0 - Sortie en février 2013. La plupart des bibliothèques et frameworks gèrent la versions 2.0.0. * 1.9.3 - Sortie en octobre 2011. Il s'agit de la version que la majorité des rubyists utilisent actuellement. [Fin de vie](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/) * 1.8.7 - Sortie en juin 2006. [Fin de vie](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/). -Les changements entre 1.8.7 à 1.9.x sont bien plus grands qu'entre 1.9.3 -jusqu'à 2.0.0. Par exemple, les versions 1.9 ont introduit le support des +Les changements entre 1.8.7 et 1.9.x sont bien plus grands qu'entre 1.9.3 +et 2.0.0. Par exemple, les versions 1.9 ont introduit le support des encodages et d'une VM bytecode ([YARV](https://fr.wikipedia.org/wiki/YARV)). Il y a toujours des projets sur 1.8.7, mais ils deviennent minoritaires, étant donné que la majorité de la communauté a migré vers au moins 1.9.2 ou 1.9.3. @@ -89,7 +90,7 @@ Mpyennement mature/compatible: * [Maglev](http://maglev.github.io/) - Basée sur Gemstone, une VM Smalltalk. Smalltalk possède quelques outils impressionnants, et ce projet tente de les apporter dans le développement Ruby. -* [RubyMotion](http://www.rubymotion.com/) - Ruby pour développement iOS. +* [RubyMotion](http://www.rubymotion.com/) - Ruby pour développement iOS et Android. * [Opal](http://opalrb.org/) - Compile le Ruby en Javascript Les implémentations de Ruby peuvent avoir leurs propres numéros de versions, @@ -122,17 +123,17 @@ l(es) auteur(s) et la/les licence(s). ## Bundler -[Bundler](http://bundler.io/) est un résolveur de dépendances des gems. Il -utilise le Gemfile d'un projet ppur trouver les dépendances, et récupère +[Bundler](http://bundler.io/) est un outil de résolution de dépendances des gems. Il +utilise le Gemfile d'un projet pour en trouver les dépendances, et récupère ensuite les dépendances de ces dépendances récursivement. Il déroule cet algorithme jusqu'à ce que toutes les dépendances soient résolues et téléchargées, ou s'arrête si un conflit est trouvé. Bundler lèvera une erreur s'il trouve des conflits de dépendances. Par exemple, si la gem A recquiert la version 3 ou plus de gem Z, mais que gem B recquiert -seulement la version 2, Bundler vous notifiera ce conflict. Cela devient +seulement la version 2 de la même gem Z, Bundler vous notifiera ce conflit. Cela devient extrêmement utile, étant donné que beaucoup de gems font référence à d'autres -gems (qui se réfèrent à d'autres gems), ce qui peut former un large graphe de +gems (qui se réfèrent à d'autres gems et ainsi de suite), ce qui peut former un large graphe de dépendance à résoudre. # Les tests @@ -151,4 +152,4 @@ différents. La communauté Ruby est fière d'être une communauté ouverte, riche et accueillante. Matz lui-même est extrêmement sociable, et la générosité des -rubyists est généralement remarquable. +rubyistes est généralement remarquable. diff --git a/fr-fr/ruby-fr.html.markdown b/fr-fr/ruby-fr.html.markdown index 1564d2b6..6b487a07 100644 --- a/fr-fr/ruby-fr.html.markdown +++ b/fr-fr/ruby-fr.html.markdown @@ -7,9 +7,11 @@ contributors: - ["Luke Holder", "http://twitter.com/lukeholder"] - ["Tristan Hume", "http://thume.ca/"] - ["Nick LaMuro", "https://github.com/NickLaMuro"] + translators: - ["Geoffrey Roguelon", "https://github.com/GRoguelon"] - ["Nami-Doc", "https://github.com/Nami-Doc"] + - ["Sylvain Abélard", "http://github.com/abelards"] lang: fr-fr --- @@ -22,19 +24,23 @@ Personne ne les utilise Vous devriez en faire de même =end -# Tout d'abord : Tout est un objet. +# Tout d'abord : tout est un objet. # Les nombres sont des objets -3.class #=> Fixnum +3.class #=> Fixnum # on voit que c'est une classe Ruby et non un "type spécial" -3.to_s #=> "3" +3.to_s #=> "3" # on peut appeler des méthodes sur ces objets, comme `to_s` (transforme en texte) # Les opérateurs de base 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 -35 / 5 #=> 7 +22 / 7 #=> 3 # si les deux éléments sont entiers, c'est une division entière +22.0 / 7 #=> 3.142857142857143 +22 / 7.0 #=> 3.142857142857143 +2**10 #=> 1024 # exposant +1024 % 10 #=> 4 # modulo (reste de la division euclidienne) # Les opérateurs sont juste des raccourcis # pour appeler une méthode sur un objet @@ -42,9 +48,9 @@ Vous devriez en faire de même 10.* 5 #=> 50 # Les valeurs spéciales sont des objets -nil # Nul -true # Vrai -false # Faux +nil # nul, vide ou indéfini +true # vrai +false # faux nil.class #=> NilClass true.class #=> TrueClass @@ -60,8 +66,7 @@ false.class #=> FalseClass !true #=> false !false #=> true -# à part false lui-même, nil est la seule autre valeur 'false' - +# à part false lui-même, nil est la seule autre valeur "considérée comme fausse" !nil #=> true !false #=> true !0 #=> false @@ -73,7 +78,6 @@ false.class #=> FalseClass 2 >= 2 #=> true # Les chaînes de caractères sont des objets - 'Je suis une chaîne de caractères'.class #=> String "Je suis également une chaîne de caractères".class #=> String @@ -84,6 +88,15 @@ placeholder = "utiliser l'interpolation de chaîne de caractères" # Affichez un message puts "J'affiche à l'écran!" +# Il y a quelques autres raccourcis pour les chaînes de caractères +rb = "Ruby" +interpolation = "Bonjour Ruby" # s'écrit aussi %Q[Bonjour #{rb}] %Q{Bonjour #{rb}} avec l'interpolation +literal = "Bonjour \#{rb}" # avec le backslash, le dièse est un "vrai dièse" (le slash ne s'affiche que pour le debug) +literal == %q[Bonjour #{rb}] # le Q majuscule fait l'interpolation, le q minuscule ne la fait pas +multi = %Q[Cette chaîne +est sur plusieurs +lignes] # => "Cette chaîne\nest sur plusieurs\nlignes" # le caractère \n signifie retour à la ligne + # Variables x = 25 #=> 25 x #=> 25 @@ -131,7 +144,7 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] [1, "hello", false] #=> [1, "hello", false] # Les tableaux peuvent être indexés -# Du début +# Trouver la valeur en donnant la position en partant du début (le premier élément est à l'index 0) array[0] #=> 1 array[12] #=> nil @@ -140,13 +153,13 @@ array[12] #=> nil array.[] 0 #=> 1 array.[] 12 #=> nil -# Depuis la fin +# On peut compter en partant de la fin avec un index négatif (le dernier élément est à l'index -1) array[-1] #=> 5 -# Avec un index de début et de fin -array[2, 4] #=> [3, 4, 5] +# Avec un index de début et un nombre d'éléments [position, nombre] +array[1, 3] #=> [2, 3, 4] -# Ou avec un intervalle +# Ou avec un intervalle [index de début .. index de fin] array[1..3] #=> [2, 3, 4] # Ajoutez un élément au tableau comme ceci @@ -180,11 +193,11 @@ new_hash.keys #=> [:defcon, :action] # Structures de contrôle if true - "si instruction" + "si l'instruction est vraie" elsif false - "autrement si, facultatif" + "si l'instruction de départ n'était pas vraie, et que cette nouvelle condition est vraie (facultatif)" else - "autrement, également facultatif" + "tous les autres cas (il est également facultatif de faire une clause else)" end for compteur in 1..5 diff --git a/fr-fr/scala.html.markdown b/fr-fr/scala-fr.html.markdown index c6d06361..c6a61745 100644 --- a/fr-fr/scala.html.markdown +++ b/fr-fr/scala-fr.html.markdown @@ -276,7 +276,7 @@ i // Montre la valeur de i. Notez que while est une boucle au sens classique. i = 0 // La boucle do while do { - println("x is still less then 10"); + println("x is still less than 10"); i += 1 } while (i < 10) diff --git a/fr-fr/vim.html.markdown b/fr-fr/vim-fr.html.markdown index 54c00b26..b2f1d24d 100644 --- a/fr-fr/vim.html.markdown +++ b/fr-fr/vim-fr.html.markdown @@ -1,7 +1,7 @@ --- category: tool tool: vim -filename: LearnVim.txt +filename: LearnVim-fr.txt contributors: - ["RadhikaG", "https://github.com/RadhikaG"] translators: diff --git a/fr-fr/yaml-fr.html.markdown b/fr-fr/yaml-fr.html.markdown index 1e8296d3..c7cb9421 100644 --- a/fr-fr/yaml-fr.html.markdown +++ b/fr-fr/yaml-fr.html.markdown @@ -1,6 +1,6 @@ --- language: yaml -filename: learnyaml.yaml +filename: learnyaml-fr.yaml contributors: - ["Andrei Curelaru", "http://www.infinidad.fr"] lang: fr-fr diff --git a/fsharp.html.markdown b/fsharp.html.markdown index bbf477ba..dd85552d 100644 --- a/fsharp.html.markdown +++ b/fsharp.html.markdown @@ -306,7 +306,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/git.html.markdown b/git.html.markdown index 01dc92c1..582f8863 100644 --- a/git.html.markdown +++ b/git.html.markdown @@ -8,6 +8,7 @@ contributors: - ["Bruno Volcov", "http://github.com/volcov"] - ["Andrew Taylor", "http://github.com/andrewjt71"] - ["Jason Stathopulos", "http://github.com/SpiritBreaker226"] + - ["Milo Gilad", "http://github.com/Myl0g"] filename: LearnGit.txt --- @@ -23,7 +24,7 @@ manage your source code. Version control is a system that records changes to a file(s), over time. -### Centralized Versioning VS Distributed Versioning +### Centralized Versioning vs. Distributed Versioning * Centralized version control focuses on synchronizing, tracking, and backing up files. @@ -157,6 +158,7 @@ $ git init --help To intentionally untrack file(s) & folder(s) from git. Typically meant for private & temp files which would otherwise be shared in the repository. + ```bash $ echo "temp/" >> .gitignore $ echo "private_key" >> .gitignore @@ -189,6 +191,9 @@ $ 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 ``` This only adds a file to the staging area/index, it doesn't commit it to the @@ -226,7 +231,7 @@ Manage your tags $ git tag # Create a annotated tag -# The -m specifies a tagging message,which is stored with the 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' @@ -287,6 +292,10 @@ contains the changes made and a message created by the user. # 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" @@ -427,7 +436,7 @@ Stashing takes the dirty state of your working directory and saves it on a stack of unfinished changes that you can reapply at any time. Let's say you've been doing some work in your git repo, but you want to pull -from the remote. Since you have dirty (uncommited) changes to some files, you +from the remote. Since you have dirty (uncommitted) changes to some files, you are not able to run `git pull`. Instead, you can run `git stash` to save your changes onto a stack! @@ -516,7 +525,7 @@ $ git reset --hard $ git reset 31f2bb1 # Moves the current branch tip backward to the specified commit -# and makes the working dir match (deletes uncommited changes and all commits +# and makes the working dir match (deletes uncommitted changes and all commits # after the specified commit). $ git reset --hard 31f2bb1 ``` @@ -526,12 +535,13 @@ $ git reset --hard 31f2bb1 Reflog will list most of the git commands you have done for a given time period, default 90 days. -This give you the a change to reverse any git commands that have gone wrong -for instance if a rebase is has broken your application. +This give you the chance to reverse any git commands that have gone wrong +(for instance, if a rebase has broken your application). You can do this: 1. `git reflog` to list all of the git commands for the rebase + ``` 38b323f HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/add_git_reflog 38b323f HEAD@{1}: rebase -i (pick): Clarify inc/dec operators diff --git a/go.html.markdown b/go.html.markdown index f097caeb..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. @@ -99,12 +99,12 @@ can include line breaks.` // Same string type. // Arrays have size fixed at compile time. var a4 [4]int // An array of 4 ints, initialized to all 0. - a3 := [...]int{3, 1, 5} // An array initialized with a fixed size of three - // elements, with values 3, 1, and 5. + a5 := [...]int{3, 1, 5, 10, 100} // An array initialized with a fixed size of five + // elements, with values 3, 1, 5, 10, and 100. // Slices have dynamic size. Arrays and slices each have advantages // but use cases for slices are much more common. - s3 := []int{4, 5, 9} // Compare to a3. No ellipsis here. + s3 := []int{4, 5, 9} // Compare to a5. No ellipsis here. s4 := make([]int, 4) // Allocates slice of 4 ints, initialized to all 0. var d2 [][]float64 // Declaration only, nothing allocated here. bs := []byte("a slice") // Type conversion syntax. @@ -134,7 +134,7 @@ can include line breaks.` // Same string type. // Unused variables are an error in Go. // The underscore lets you "use" a variable but discard its value. - _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs + _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a5, s4, bs // Usually you use it to ignore one of the return values of a function // For example, in a quick and dirty script you might ignore the // error value returned from os.Create, and expect that the file @@ -180,7 +180,7 @@ func learnFlowControl() { if true { fmt.Println("told ya") } - // Formatting is standardized by the command line command "go fmt." + // Formatting is standardized by the command line command "go fmt". if false { // Pout. } else { @@ -295,7 +295,7 @@ type pair struct { x, y int } -// Define a method on type pair. Pair now implements Stringer. +// Define a method on type pair. Pair now implements Stringer because Pair has defined all the methods in the interface. func (p pair) String() string { // p is called the "receiver" // Sprintf is another public function in package fmt. // Dot syntax references fields of p. diff --git a/groovy.html.markdown b/groovy.html.markdown index a3a45757..efbb2b32 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -230,10 +230,12 @@ for (i in array) { //Iterate over a map def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] -x = 0 +x = "" for ( e in map ) { x += e.value + x += " " } +assert x.equals("Roberto Grails Groovy ") /* Operators diff --git a/hack.html.markdown b/hack.html.markdown index b3d19f8e..fb6af8e1 100644 --- a/hack.html.markdown +++ b/hack.html.markdown @@ -257,7 +257,7 @@ class InvalidFooSubclass extends ConsistentFoo // ... } - // Using the __Override annotation on a non-overriden method will cause a + // Using the __Override annotation on a non-overridden method will cause a // type checker error: // // "InvalidFooSubclass::otherMethod() is marked as override; no non-private @@ -299,7 +299,7 @@ $cat instanceof KittenInterface === true; // True ## More Information Visit the [Hack language reference](http://docs.hhvm.com/manual/en/hacklangref.php) -for detailed explainations of the features Hack adds to PHP, or the [official Hack website](http://hacklang.org/) +for detailed explanations of the features Hack adds to PHP, or the [official Hack website](http://hacklang.org/) for more general information. Visit the [official HHVM website](http://hhvm.com/) for HHVM installation instructions. diff --git a/haml.html.markdown b/haml.html.markdown index 0948e9ef..bb8bdc54 100644 --- a/haml.html.markdown +++ b/haml.html.markdown @@ -3,6 +3,7 @@ language: haml filename: learnhaml.haml contributors: - ["Simon Neveu", "https://github.com/sneveu"] + - ["Vasiliy Petrov", "https://github.com/Saugardas"] --- Haml is a markup language predominantly used with Ruby that cleanly and simply describes the HTML of any web document without the use of inline code. It is a popular alternative to using Rails templating language (.erb) and allows you to embed Ruby code into your markup. @@ -11,7 +12,9 @@ It aims to reduce repetition in your markup by closing tags for you based on the You can also use Haml on a project independent of Ruby, by installing the Haml gem on your machine and using the command line to convert it to html. +```shell $ haml input_file.haml output_file.html +``` ```haml @@ -36,7 +39,7 @@ $ haml input_file.haml output_file.html To write a multi line comment, indent your commented code to be wrapped by the forward slash --# This is a silent comment, which means it wont be rendered into the doc at all +-# This is a silent comment, which means it won't be rendered into the doc at all / ------------------------------------------- @@ -55,8 +58,17 @@ $ haml input_file.haml output_file.html </header> </body> -/ The div tag is the default element, so they can be written simply like this -.foo +/ + The div tag is the default element, so it can be omitted. + You can define only class/id using . or # + For example + +%div.my_class + %div#my_id + +/ Can be written +.my_class + #my_id / To add content to a tag, add the text directly after the declaration %h1 Headline copy @@ -97,6 +109,15 @@ $ haml input_file.haml output_file.html / To write data-attributes, use the :data key with its value as another hash %div{:data => {:attribute => 'foo'}} +/ For Ruby version 1.9 or higher you can use Ruby's new hash syntax +%div{ data: { attribute: 'foo' } } + +/ Also you can use HTML-style attribute syntax. +%a(href='#' title='bar') + +/ And both syntaxes together +%a(href='#'){ title: @my_class.title } + / ------------------------------------------- / Inserting Ruby @@ -120,7 +141,7 @@ $ haml input_file.haml output_file.html - books.shuffle.each_with_index do |book, index| %h1= book - if book do + - if book do %p This is a book / Adding ordered / unordered list @@ -166,12 +187,33 @@ $ haml input_file.haml output_file.html / ------------------------------------------- / - Use the colon to define Haml filters, one example of a filter you can - use is :javascript, which can be used for writing inline js + Filters pass the block to another filtering program and return the result in Haml + To use a filter, type a colon and the name of the filter + +/ Markdown filter +:markdown + # Header + Text **inside** the *block* + +/ The code above is compiled into +<h1>Header</h1> + +<p>Text <strong>inside</strong> the <em>block</em></p> + +/ Javascript filter :javascript console.log('This is inline <script>'); +/ is compiled into +<script> + console.log('This is inline <script>'); +</script> + +/ + There are many types of filters (:markdown, :javascript, :coffee, :css, :ruby and so on) + Also you can define your own filters using Haml::Filters + ``` ## Additional resources diff --git a/haskell.html.markdown b/haskell.html.markdown index 4e254070..90d47c27 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -1,5 +1,6 @@ --- language: Haskell +filename: learnhaskell.hs contributors: - ["Adit Bhargava", "http://adit.io"] --- @@ -123,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 ---------------------------------------------------- @@ -158,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 @@ -202,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 @@ -222,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 @@ -443,5 +447,6 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater There are two popular ways to install Haskell: The traditional [Cabal-based installation](http://www.haskell.org/platform/), and the newer [Stack-based process](https://www.stackage.org/install). You can find a much gentler introduction from the excellent -[Learn you a Haskell](http://learnyouahaskell.com/) or +[Learn you a Haskell](http://learnyouahaskell.com/), +[Happy Learn Haskell Tutorial](http://www.happylearnhaskelltutorial.com/) or [Real World Haskell](http://book.realworldhaskell.org/). diff --git a/haxe.html.markdown b/haxe.html.markdown index 03b3099e..df2a1e78 100644 --- a/haxe.html.markdown +++ b/haxe.html.markdown @@ -466,7 +466,7 @@ class LearnHaxe3{ The untyped keyword operates on entire *blocks* of code, skipping any type checks that might be otherwise required. This keyword should be used very sparingly, such as in limited conditionally-compiled - situations where type checking is a hinderance. + situations where type checking is a hindrance. In general, skipping type checks is *not* recommended. Use the enum, inheritance, or structural type models in order to help ensure @@ -770,8 +770,8 @@ class UsingExample { ``` We're still only scratching the surface here of what Haxe can do. For a formal -overiew of all Haxe features, checkout the [online -manual](http://haxe.org/manual), the [online api](http://api.haxe.org/), and +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/). For more advanced topics, consider checking out: diff --git a/html.html.markdown b/html.html.markdown index 3c855b5c..04b9f501 100644 --- a/html.html.markdown +++ b/html.html.markdown @@ -1,6 +1,6 @@ --- language: html -filename: learnhtml.html +filename: learnhtml.txt contributors: - ["Christophe THOMAS", "https://github.com/WinChris"] translators: @@ -111,7 +111,7 @@ This article is concerned principally with HTML syntax and some useful tips. ## Usage -HTML is written in files ending with `.html`. +HTML is written in files ending with `.html` or `.htm`. The mime type is `text/html`. ## To Learn More diff --git a/hu-hu/python-hu.html.markdown b/hu-hu/python-hu.html.markdown new file mode 100644 index 00000000..9b55f8e2 --- /dev/null +++ b/hu-hu/python-hu.html.markdown @@ -0,0 +1,816 @@ +--- +language: python +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: + - ["Tamás Diószegi", "https://github.com/ditam"] +filename: learnpython-hu.py +lang: hu-hu +--- + +A Python nyelvet Guido Van Rossum alkotta meg a 90-es évek elején. Manapság az +egyik legnépszerűbb programozási nyelv. Én a tiszta szintaxisa miatt szerettem +bele. Tulajdonképpen futtatható pszeudokód. + +Szívesen fogadok visszajelzéseket! Elérsz itt: [@louiedinh](http://twitter.com/louiedinh) +vagy pedig a louiedinh [kukac] [google email szolgáltatása] címen. + +Figyelem: ez a leírás a Python 2.7 verziójára vonatkozok, illetve +általánosságban a 2.x verziókra. A Python 2.7 azonban már csak 2020-ig lesz +támogatva, ezért kezdőknek ajánlott, hogy a Python 3-mal kezdjék az +ismerkedést. A Python 3.x verzióihoz a [Python 3 bemutató](http://learnxinyminutes.com/docs/python3/) +ajánlott. + +Lehetséges olyan Python kódot írni, ami egyszerre kompatibilis a 2.7 és a 3.x +verziókkal is, a Python [`__future__` imports](https://docs.python.org/2/library/__future__.html) használatával. +A `__future__` import használata esetén Python 3-ban írhatod a kódot, ami +Python 2 alatt is futni fog, így ismét a fenti Python 3 bemutató ajánlott. + +```python + +# Az egysoros kommentek kettőskereszttel kezdődnek + +""" Többsoros stringeket három darab " közé + fogva lehet írni, ezeket gyakran használják + több soros kommentként. +""" + +#################################################### +# 1. Egyszerű adattípusok és operátorok +#################################################### + +# Használhatsz számokat +3 # => 3 + +# Az alapműveletek meglepetésektől mentesek +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7 + +# Az osztás kicsit trükkös. Egész osztást végez, és a hányados alsó egész része +# lesz az eredmény +5 / 2 # => 2 + +# Az osztás kijavításához a (lebegőpontos) float típust kell használnunk +2.0 # Ez egy float +11.0 / 4.0 # => 2.75 áh... máris jobb + +# Az egész osztás a negatív számok esetén is az alsó egész részt eredményezi +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 # floatok esetén is +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 + +# Ha importáljuk a division modult (ld. 6. Modulok rész), +# akkor a '/' jellel pontos osztást tudunk végezni. +from __future__ import division + +11 / 4 # => 2.75 ...sima osztás +11 // 4 # => 2 ...egész osztás + +# Modulo művelet +7 % 3 # => 1 + +# Hatványozás (x az y. hatványra) +2 ** 4 # => 16 + +# A precedencia zárójelekkel befolyásolható +(1 + 3) * 2 # => 8 + +# Logikai operátorok +# Megjegyzés: az "and" és "or" csak kisbetűkkel helyes +True and False # => False +False or True # => True + +# A logikai operátorok egészeken is használhatóak +0 and 2 # => 0 +-5 or 0 # => -5 +0 == False # => True +2 == True # => False +1 == True # => True + +# Negálni a not kulcsszóval lehet +not True # => False +not False # => True + +# Egyenlőségvizsgálat == +1 == 1 # => True +2 == 1 # => False + +# Egyenlőtlenség != +1 != 1 # => False +2 != 1 # => True + +# További összehasonlítások +1 < 10 # => True +1 > 10 # => False +2 <= 2 # => True +2 >= 2 # => True + +# Az összehasonlítások láncolhatóak! +1 < 2 < 3 # => True +2 < 3 < 2 # => False + +# Stringeket " vagy ' jelek közt lehet megadni +"Ez egy string." +'Ez egy másik string.' + +# A stringek összeadhatóak! +"Hello " + "world!" # => "Hello world!" +# '+' jel nélkül is összeadhatóak +"Hello " "world!" # => "Hello world!" + +# ... illetve szorozhatóak +"Hello" * 3 # => "HelloHelloHello" + +# Kezelhető karakterek indexelhető listájaként +"This is a string"[0] # => 'T' + +# A string hosszát a len függvény adja meg +len("This is a string") # => 16 + +# String formázáshoz a % jel használható +# A Python 3.1-gyel a % már deprecated jelölésű, és később eltávolításra fog +# kerülni, de azért jó tudni, hogyan működik. +x = 'alma' +y = 'citrom' +z = "A kosárban levő elemek: %s és %s" % (x, y) + +# A string formázás újabb módja a format metódus használatával történik. +# Jelenleg ez a javasolt megoldás. +"{} egy {} szöveg".format("Ez", "helytartó") +"A {0} pedig {1}".format("string", "formázható") +# Ha nem akarsz számolgatni, nevesíthetőek a pozíciók. +"{name} kedvence a {food}".format(name="Bob", food="lasagna") + +# None egy objektum +None # => None + +# A None-nal való összehasonlításhoz ne használd a "==" jelet, +# használd az "is" kulcsszót helyette +"etc" is None # => False +None is None # => True + +# Az 'is' operátor objektum egyezést vizsgál. +# Primitív típusok esetén ez nem túl hasznos, +# objektumok esetén azonban annál inkább. + +# Bármilyen objektum használható logikai kontextusban. +# A következő értékek hamis-ra értékelődnek ki (ún. "falsey" értékek): +# - None +# - bármelyik szám típus 0 értéke (pl. 0, 0L, 0.0, 0j) +# - üres sorozatok (pl. '', (), []) +# - üres konténerek (pl., {}, set()) +# - egyes felhasználó által definiált osztályok példányai bizonyos szabályok szerint, +# ld: https://docs.python.org/2/reference/datamodel.html#object.__nonzero__ +# +# Minden egyéb érték "truthy" (a bool() függvénynek átadva igazra értékelődnek ki) +bool(0) # => False +bool("") # => False + + +#################################################### +# 2. Változók és kollekciók +#################################################### + +# Létezik egy print utasítás +print "I'm Python. Nice to meet you!" # => I'm Python. Nice to meet you! + +# Így lehet egyszerűen bemenetet kérni a konzolról: +input_string_var = raw_input( + "Enter some data: ") # Visszatér a megadott stringgel +input_var = input("Enter some data: ") # Kiértékeli a bemenetet python kódként +# Vigyázat: a fentiek miatt az input() metódust körültekintően kell használni +# Megjegyzés: Python 3-ban az input() már deprecated, és a raw_input() lett input()-ra átnevezve + +# A változókat nem szükséges a használat előtt deklarálni +some_var = 5 # Konvenció szerint a névben kisbetu_es_alulvonas +some_var # => 5 + +# Érték nélküli változóra hivatkozás hibát dob. +# Lásd a Control Flow szekciót a kivételkezelésről. +some_other_var # name error hibát dob + +# az if használható kifejezésként +# a C nyelv '?:' ternáris operátorával egyenértékűen +"yahoo!" if 3 > 2 else 2 # => "yahoo!" + +# A listákban sorozatok tárolhatóak +li = [] +# Már inicializáláskor megadhatóak elemek +other_li = [4, 5, 6] + +# A lista végére az append metódus rak új elemet +li.append(1) # li jelenleg [1] +li.append(2) # li jelenleg [1, 2] +li.append(4) # li jelenleg [1, 2, 4] +li.append(3) # li jelenleg [1, 2, 4, 3] +# A végéről a pop metódus távolít el elemet +li.pop() # => 3 és li jelenleg [1, 2, 4] +# Rakjuk vissza +li.append(3) # li jelenleg [1, 2, 4, 3], újra. + +# A lista elemeket tömb indexeléssel lehet hivatkozni +li[0] # => 1 +# A már inicializált értékekhez a = jellel lehet új értéket rendelni +li[0] = 42 +li[0] # => 42 +li[0] = 1 # csak visszaállítjuk az eredeti értékére +# Így is lehet az utolsó elemre hivatkozni +li[-1] # => 3 + +# A túlindexelés eredménye IndexError +li[4] # IndexError hibát dob + +# A lista részeit a slice szintaxissal lehet kimetszeni +# (Matekosoknak ez egy zárt/nyitott intervallum.) +li[1:3] # => [2, 4] +# A lista eleje kihagyható így +li[2:] # => [4, 3] +# Kihagyható a vége +li[:3] # => [1, 2, 4] +# Minden második elem kiválasztása +li[::2] # =>[1, 4] +# A lista egy másolata, fordított sorrendben +li[::-1] # => [3, 4, 2, 1] +# A fentiek kombinációival bonyolultabb slice parancsok is képezhetőek +# li[start:end:step] + +# Listaelemek a "del" paranccsal törölhetőek +del li[2] # li jelenleg [1, 2, 3] + +# A listák összeadhatóak +li + other_li # => [1, 2, 3, 4, 5, 6] +# Megjegyzés: az eredeti li és other_li értékei változatlanok + +# Összefőzhetőek (konkatenálhatóak) az "extend()" paranccsal +li.extend(other_li) # li jelenleg [1, 2, 3, 4, 5, 6] + +# Egy elem első előfordulásának eltávolítása +li.remove(2) # li jelenleg [1, 3, 4, 5, 6] +li.remove(2) # ValueError hibát dob, mivel a 2 nem szerepel már a listában + +# Elemek beszúrhatóak tetszőleges helyre +li.insert(1, 2) # li jelenleg [1, 2, 3, 4, 5, 6], ismét + +# Egy elem első előfordulási helye +li.index(2) # => 1 +li.index(7) # ValueError hibát dob, mivel a 7 nem szerepel a listában + +# Egy listában egy elem előfordulása az "in" szóval ellenőrizhető +1 in li # => True + +# A lista hossza a "len()" függvénnyel +len(li) # => 6 + +# Az N-esek ("tuple") hasonlítanak a listákhoz, de nem módosíthatóak +tup = (1, 2, 3) +tup[0] # => 1 +tup[0] = 3 # TypeError hibát dob + +# Az összes lista-műveletet ezeken is használható +len(tup) # => 3 +tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) +tup[:2] # => (1, 2) +2 in tup # => True + +# Az N-esek (és listák) kicsomagolhatóak külön változókba +a, b, c = (1, 2, 3) # az a így 1, a b 2 és a c pedig 3 +d, e, f = 4, 5, 6 # a zárójel elhagyható +# Ha elhagyod a zárójeleket, alapértelmezés szerint tuple képződik +g = 4, 5, 6 # => (4, 5, 6) +# Nézd, milyen egyszerű két értéket megcserélni +e, d = d, e # d most már 5 és az e 4 + +# A Dictionary típusokban hozzárendelések (kulcs-érték párok) tárolhatók +empty_dict = {} +# Ez pedig rögtön értékekkel van inicializálva +filled_dict = {"one": 1, "two": 2, "three": 3} + +# Egy dictionary értékei [] jelek közt indexelhetőek +filled_dict["one"] # => 1 + +# A "keys()" metódus visszatér a kulcsok listájával +filled_dict.keys() # => ["three", "two", "one"] +# Megjegyzés: egy dictionary párjainak sorrendje nem garantált +# Lehet, hogy már a fenti példán is más sorrendben kaptad meg az elemeket. + +# Az értékek listája a "values()" metódussal kérhető le +filled_dict.values() # => [3, 2, 1] +# ld. a fenti megjegyzést az elemek sorrendjéről. + +# Az összes kulcs-érték pár megkapható N-esek listájaként az "items()" metódussal +filled_dict.items() # => [("one", 1), ("two", 2), ("three", 3)] + +# Az "in" kulcssszóval ellenőrizhető, hogy egy kulcs szerepel-e a dictionary-ben +"one" in filled_dict # => True +1 in filled_dict # => False + +# Nem létező kulcs hivatkozása KeyError hibát dob +filled_dict["four"] # KeyError + +# A "get()" metódus használatával elkerülhető a KeyError +filled_dict.get("one") # => 1 +filled_dict.get("four") # => None +# A metódusnak megadható egy alapértelmezett visszatérési érték is, hiányzó értékek esetén +filled_dict.get("one", 4) # => 1 +filled_dict.get("four", 4) # => 4 +# Megjegyzés: ettől még filled_dict.get("four") => None +# (vagyis a get nem állítja be az alapértelmezett értéket a dictionary-ben) + +# A kulcsokhoz értékek a listákhoz hasonló szintaxissal rendelhetőek: +filled_dict["four"] = 4 # ez után filled_dict["four"] => 4 + +# A "setdefault()" metódus csak akkor állít be egy értéket, ha az adott kulcshoz még nem volt más megadva +filled_dict.setdefault("five", 5) # filled_dict["five"] beállítva 5-re +filled_dict.setdefault("five", 6) # filled_dict["five"] még mindig 5 + +# Egy halmaz ("set") olyan, mint egy lista, de egy elemet csak egyszer tárolhat +empty_set = set() +# Inicializáljuk ezt a halmazt néhány elemmel +some_set = set([1, 2, 2, 3, 4]) # some_set jelenleg set([1, 2, 3, 4]) + +# A sorrend itt sem garantált, még ha néha rendezettnek is tűnhet +another_set = set([4, 3, 2, 2, 1]) # another_set jelenleg set([1, 2, 3, 4]) + +# Python 2.7 óta már {} jelek közt is lehet halmazt definiálni +filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4} + +# Új halmaz-elemek hozzáadása +filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} + +# Halmaz metszés a & operátorral +other_set = {3, 4, 5, 6} +filled_set & other_set # => {3, 4, 5} + +# Halmaz unió | operátorral +filled_set | other_set # => {1, 2, 3, 4, 5, 6} + +# Halmaz különbség - +{1, 2, 3, 4} - {2, 3, 5} # => {1, 4} + +# Szimmetrikus differencia ^ +{1, 2, 3, 4} ^ {2, 3, 5} # => {1, 4, 5} + +# Vizsgáljuk, hogy a bal oldali halmaz magában foglalja-e a jobb oldalit +{1, 2} >= {1, 2, 3} # => False + +# Vizsgáljuk, hogy a bal oldali halmaz részhalmaza-e a jobb oldalinak +{1, 2} <= {1, 2, 3} # => True + +# Halmazbeli elemek jelenléte az in kulcssszóval vizsgálható +2 in filled_set # => True +10 in filled_set # => False + + +#################################################### +# 3. Control Flow +#################################################### + +# Legyen egy változónk +some_var = 5 + +# Ez egy if elágazás. A behúzás mértéke (az indentáció) jelentéssel bír a nyelvben! +# Ez a kód ezt fogja kiírni: "some_var kisebb 10-nél" +if some_var > 10: + print "some_var nagyobb, mint 10." +elif some_var < 10: # Az elif kifejezés nem kötelező az if szerkezetben. + print "some_var kisebb 10-nél" +else: # Ez sem kötelező. + print "some_var kereken 10." + +""" +For ciklusokkal végigiterálhatunk listákon +a kimenet: + A(z) kutya emlős + A(z) macska emlős + A(z) egér emlős +""" +for animal in ["kutya", "macska", "egér"]: + # A {0} kifejezéssel formázzuk a stringet, ld. korábban. + print "A(z) {0} emlős".format(animal) + +""" +"range(number)" visszatér számok listájával 0-től number-ig +a kimenet: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print i + +""" +"range(lower, upper)" visszatér a lower és upper közti számok listájával +a kimenet: + 4 + 5 + 6 + 7 +""" +for i in range(4, 8): + print i + +""" +A while ciklus a feltétel hamissá válásáig fut. +a kimenet: + 0 + 1 + 2 + 3 +""" +x = 0 +while x < 4: + print x + x += 1 # Rövidítés az x = x + 1 kifejezésre + +# A kivételek try/except blokkokkal kezelhetőek + +# Python 2.6-tól felfele: +try: + # A "raise" szóval lehet hibát dobni + raise IndexError("Ez egy index error") +except IndexError as e: + pass # A pass egy üres helytartó művelet. Itt hívnánk a hibakezelő kódunkat. +except (TypeError, NameError): + pass # Ha szükséges, egyszerre több hiba típus is kezelhető +else: # Az except blokk után opcionálisan megadható + print "Minden rendben!" # Csak akkor fut le, ha fentebb nem voltak hibák +finally: # Mindenképpen lefut + print "Itt felszabadíthatjuk az erőforrásokat például" + +# Az erőforrások felszabadításához try/finally helyett a with használható +with open("myfile.txt") as f: + for line in f: + print line + + +#################################################### +# 4. Függvények +#################################################### + +# A "def" szóval hozhatunk létre új függvényt +def add(x, y): + print "x is {0} and y is {1}".format(x, y) + return x + y # A return szóval tudunk értékeket visszaadni + + +# Így hívunk függvényt paraméterekkel +add(5, 6) # => a konzol kimenet "x is 5 and y is 6", a visszatérési érték 11 + +# Nevesített paraméterekkel (ún. "keyword arguments") is hívhatunk egy függvényt +add(y=6, x=5) # Ez esetben a sorrendjük nem számít + + +# Változó számú paramétert fogadó függvény így definiálható. +# A * használatával a paramétereket egy N-esként kapjuk meg. +def varargs(*args): + return args + + +varargs(1, 2, 3) # => (1, 2, 3) + + +# Változó számú nevesített paramétert fogadó függvény is megadható, +# a ** használatával a paramétereket egy dictionary-ként kapjuk meg +def keyword_args(**kwargs): + return kwargs + + +# Nézzük meg, mi történik +keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"} + + +# A két módszer egyszerre is használható +def all_the_args(*args, **kwargs): + print args + print kwargs + + +""" +all_the_args(1, 2, a=3, b=4) kimenete: + (1, 2) + {"a": 3, "b": 4} +""" + +# Függvények hívásakor a fenti args és kwargs módszerek inverze használható +# A * karakter kifejt egy listát külön paraméterekbe, a ** egy dictionary-t nevesített paraméterekbe. +args = (1, 2, 3, 4) +kwargs = {"a": 3, "b": 4} +all_the_args(*args) # egyenértékű: foo(1, 2, 3, 4) +all_the_args(**kwargs) # egyenértékű: foo(a=3, b=4) +all_the_args(*args, **kwargs) # egyenértékű: foo(1, 2, 3, 4, a=3, b=4) + + +# A fenti arg és kwarg paraméterek továbbadhatóak egyéb függvényeknek, +# a * illetve ** operátorokkal kifejtve +def pass_all_the_args(*args, **kwargs): + all_the_args(*args, **kwargs) + print varargs(*args) + print keyword_args(**kwargs) + + +# Függvény scope +x = 5 + + +def set_x(num): + # A lokális x változó nem ugyanaz, mint a globális x + x = num # => 43 + print x # => 43 + + +def set_global_x(num): + global x + print x # => 5 + x = num # a globális x-et 6-ra állítjuk + print x # => 6 + + +set_x(43) +set_global_x(6) + + +# A pythonban a függvény elsőrendű (ún. "first class") típus +def create_adder(x): + def adder(y): + return x + y + + return adder + + +add_10 = create_adder(10) +add_10(3) # => 13 + +# Névtelen függvények is definiálhatóak +(lambda x: x > 2)(3) # => True +(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5 + +# Léteznek beépített magasabb rendű függvények +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] + +# A listaképző kifejezések ("list comprehensions") jól használhatóak a map és filter függvényekkel +[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] + +# halmaz és dictionary képzők is léteznek +{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. Osztályok +#################################################### + +# Az object osztály egy alosztályát képezzük +class Human(object): + # Osztály szintű mező: az osztály összes példányában azonos + species = "H. sapiens" + + # Ez a függvény meghívódik az osztály példányosításakor. + # Megjegyzés: a dupla aláhúzás a név előtt és után egy konvenció a python + # előre definiált, a nyelv által belsőleg használt, de a felhasználó által + # is látható objektumok és mezők neveire. + # Ne vezessünk be új, ilyen elnevezési sémát használó neveket! + def __init__(self, name): + # A paramétert értékül adjuk a példány name attribútumának + self.name = name + + # Inicializálunk egy mezőt + self.age = 0 + + # Példány metódus. Minden metódus első paramétere a "self", a példány maga + def say(self, msg): + return "{0}: {1}".format(self.name, msg) + + # Egy osztálymetódus az osztály összes példány közt meg van osztva. + # Hívásukkor az első paraméter mindig a hívó osztály. + @classmethod + def get_species(cls): + return cls.species + + # Egy statikus metódus osztály és példányreferencia nélkül hívódik + @staticmethod + def grunt(): + return "*grunt*" + + # Egy property jelölésű függvény olyan, mint egy getter. + # Használatával az age mező egy csak-olvasható attribútummá válik. + @property + def age(self): + return self._age + + # Így lehet settert megadni egy mezőhöz + @age.setter + def age(self, age): + self._age = age + + # Így lehet egy mező törlését engedélyezni + @age.deleter + def age(self): + del self._age + + +# Példányosítsuk az osztályt +i = Human(name="Ian") +print i.say("hi") # kimenet: "Ian: hi" + +j = Human("Joel") +print j.say("hello") # kimenet: "Joel: hello" + +# Hívjuk az osztály metódusunkat +i.get_species() # => "H. sapiens" + +# Változtassuk meg az osztály szintű attribútumot +Human.species = "H. neanderthalensis" +i.get_species() # => "H. neanderthalensis" +j.get_species() # => "H. neanderthalensis" + +# Hívjuk meg a statikus metódust +Human.grunt() # => "*grunt*" + +# Adjunk új értéket a mezőnek +i.age = 42 + +# Kérjük le a mező értékét +i.age # => 42 + +# Töröljük a mezőt +del i.age +i.age # => AttributeError hibát dob + +#################################################### +# 6. Modulok +#################################################### + +# Modulokat így lehet importálni +import math + +print math.sqrt(16) # => 4 + +# Lehetséges csak bizonyos függvényeket importálni egy modulból +from math import ceil, floor + +print ceil(3.7) # => 4.0 +print floor(3.7) # => 3.0 + +# Egy modul összes függvénye is importálható +# Vigyázat: ez nem ajánlott. +from math import * + +# A modulok nevei lerövidíthetőek +import math as m + +math.sqrt(16) == m.sqrt(16) # => True +# Meggyőződhetünk róla, hogy a függvények valóban azonosak +from math import sqrt + +math.sqrt == m.sqrt == sqrt # => True + +# A Python modulok egyszerű fájlok. +# Írhatsz sajátot és importálhatod is. +# A modul neve azonos a tartalmazó fájl nevével. + +# Így lehet megtekinteni, milyen mezőket és függvényeket definiál egy modul. +import math + +dir(math) + + +# Ha van egy math.py nevű Python scripted a jelenleg futó scripttel azonos +# mappában, a math.py fájl lesz betöltve a beépített Python modul helyett. +# A lokális mappa prioritást élvez a beépített könyvtárak felett. + + +#################################################### +# 7. Haladóknak +#################################################### + +# Generátorok +# Egy generátor értékeket "generál" amikor kérik, a helyett, hogy előre eltárolná őket. + +# A következő metódus (ez még NEM egy generátor) megduplázza a kapott iterable elemeit, +# és eltárolja őket. Nagy méretű iterable esetén ez nagyon sok helyet foglalhat! +def double_numbers(iterable): + double_arr = [] + for i in iterable: + double_arr.append(i + i) + return double_arr + + +# A következő kód futtatásakor az összes szám kétszeresét kiszámítanánk, és visszaadnánk +# ezt a nagy listát a ciklus vezérléséhez. +for value in double_numbers(range(1000000)): # `test_non_generator` + print value + if value > 5: + break + + +# Használjunk inkább egy generátort, ami "legenerálja" a soron következő elemet, +# amikor azt kérik tőle +def double_numbers_generator(iterable): + for i in iterable: + yield i + i + + +# A lenti kód mindig csak a soron következő számot generálja a logikai vizsgálat előtt. +# Így amikor az érték eléri a > 5 határt, megszakítjuk a ciklust, és a lista számainak +# nagy részénél megspóroltuk a duplázás műveletet (ez sokkal gyorsabb így!). +for value in double_numbers_generator(xrange(1000000)): # `test_generator` + print value + if value > 5: + break + +# Feltűnt, hogy a `test_non_generator` esetén `range`, a `test_generator` esetén +# pedig `xrange` volt a segédfüggvény neve? Ahogy `double_numbers_generator` a +# generátor változata a `double_numbers` függvénynek, úgy az `xrange` a `range` +# generátor megfelelője, csak akkor generálja le a következő számot, amikor kérjük +# - esetünkben a ciklus következő iterációjakor + +# A lista képzéshez hasonlóan generátor képzőket is használhatunk +# ("generator comprehensions"). +values = (-x for x in [1, 2, 3, 4, 5]) +for x in values: + print(x) # kimenet: -1 -2 -3 -4 -5 + +# Egy generátor összes generált elemét listaként is elkérhetjük: +values = (-x for x in [1, 2, 3, 4, 5]) +gen_to_list = list(values) +print(gen_to_list) # => [-1, -2, -3, -4, -5] + +# Dekorátorok +# A dekorátor egy magasabb rendű függvény, aminek bemenete és kimenete is egy függvény. +# A lenti egyszerű példában az add_apples dekorátor a dekorált get_fruits függvény +# kimenetébe beszúrja az 'Apple' elemet. +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'] + +# A kimenet tartalmazza az 'Apple' elemet: +# Banana, Mango, Orange, Apple +print ', '.join(get_fruits()) + +# Ebben a példában a beg dekorátorral látjuk el a say függvényt. +# Beg meghívja say-t. Ha a say_please paraméter igaz, akkor +# megváltoztatja az eredmény mondatot. +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, "Please! I am poor :(") + return msg + + return wrapper + + +@beg +def say(say_please=False): + msg = "Can you buy me a beer?" + return msg, say_please + + +print say() # Can you buy me a beer? +print say(say_please=True) # Can you buy me a beer? Please! I am poor :( +``` + +## Még több érdekel? + +### Ingyenes online tartalmak + +* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com) +* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +* [Dive Into Python](http://www.diveintopython.net/) +* [The Official Docs](http://docs.python.org/2/) +* [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) +* [First Steps With Python](https://realpython.com/learn/python-first-steps/) +* [LearnPython](http://www.learnpython.org/) +* [Fullstack Python](https://www.fullstackpython.com/) + +### Könyvek + +* [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/hy.html.markdown b/hy.html.markdown index 79c16c23..1287095f 100644 --- a/hy.html.markdown +++ b/hy.html.markdown @@ -102,7 +102,7 @@ True ; => True (apply something-fancy ["My horse" "amazing"] { "mane" "spectacular" }) ; anonymous functions are created using `fn' or `lambda' constructs -; which are similiar to `defn' +; which are similar to `defn' (map (fn [x] (* x x)) [1 2 3 4]) ;=> [1 4 9 16] ;; Sequence operations diff --git a/id-id/asciidoc-id.html.markdown b/id-id/asciidoc-id.html.markdown index 8da8af38..ceb56b92 100644 --- a/id-id/asciidoc-id.html.markdown +++ b/id-id/asciidoc-id.html.markdown @@ -84,10 +84,6 @@ Judul bagian ===== Level 4 <h5> -====== Level 5 <h6> - -======= Level 6 <h7> - ``` Daftar diff --git a/id-id/bf-id.html.markdown b/id-id/bf-id.html.markdown new file mode 100644 index 00000000..bf2f6a09 --- /dev/null +++ b/id-id/bf-id.html.markdown @@ -0,0 +1,86 @@ +--- +language: bf +filename: brainfuck-id.bf +contributors: + - ["Prajit Ramachandran", "http://prajitr.github.io/"] + - ["Mathias Bynens", "http://mathiasbynens.be/"] +translators: + - ["Muhammad Rifqi Fatchurrahman", "http://muhrifqii.github.io/"] +lang: id-id +--- + +Brainfuck (tidak dalam huruf kapital kecuali pada awal kalimat) adalah sebuah +bahasa pemrograman Turing-complete yang sangat minim yang hanya memiliki 8 perintah. + +Anda bisa mencoba brainfuck pada browser dengan menggunakan [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/). + +```bf +Karakter apapun selain "><+-.,[]" (tanda kutip tidak termasuk) diabaikan. + +Brainfuck direpresentasikan dengan sebuah array yang memiliki 30,000 cell yang +diinisialisasi dengan nol dan pointer data yang menunjuk ke current cell. + +Terdapat delapan perintah: ++ : Menaikkan nilai pada current cell sebesar satu. +- : Menurunkan nilai pada current cell sebesar satu. +> : Menggeser pointer data ke cell selanjutnya (cell sebelah kanan). +< : Menggeser pointer data ke cell sebelumnya (cell sebelah kiri). +. : Mencetak nilai ASCII pada current cell (misal 65 = 'A'). +, : Membaca sebuah karakter masukan tunggal ke dalam current cell. +[ : Jika nilai pada current cell bernilai nol, lewati hingga mencapai ] yang sesuai. + Jika tidak, pindah ke instruksi berikutnya. +] : Jika nilai pada current cell bernilai nol, pindah ke instruksi berikutnya. + Jika tidak, mundur pada instruksi hingga mencapai [ yang sesuai. + +[ dan ] membentuk sebuah rekursi while. Tentu saja mereka harus seimbang. + +Mari kita lihat beberapa program brainfuck dasar. + +++++++ [ > ++++++++++ < - ] > +++++ . + +Program ini mencetak huruf 'A'. Mula-mula, cell #1 dinaikkan ke 6. +Cell #1 akan digunakan untuk rekursi. Lalu, masuk ke rekursi ([) dan pindah +ke cell #2. Cell #2 dinaikkan 10 kali, mundur ke cell #1, dan menurunkan +cell #1. Rekursi ini berlangsung 6 kali (melakukan 6 penurunan nilai untuk +cell #1 hingga mencapai 0, di titik mana dia melewati hingga mencapai ] dan +terus berlanjut). + +Pada titik ini, kita berada pada cell #1, yang memiliki nilai 0, sedangkan cell #2 +memiliki sebuah nilai 60. Kita berpindah ke cell #2, menaikkan nilai 5 kali, memunculkan +nilai 65, lalu cetak nilai pada cell #2. 65 adalah 'A' pada ASCII, jadi 'A' +dicetak ke terminal. + +, [ > + < - ] > . + +Program ini membaca sebuah karakter dari masukan user dan menyalin karakternya ke +cell #1. Setelah itu rekursi dimulai. Geser ke cell #2, menaikkan nilai pada cell #2, +mundur ke cell #1, dan menurunkan nilai pada cell #1. Hal ini berlanjut sampai cell #1 +bernilai 0, dan cell #2 menyimpan nilai lama dari cell #1. Karena kita berada di cell #1 +saat ujung rekursi, geser ke cell #2, lalu cetak nilai dalam bentuk ASCII. + +Perlu diingat bahwa spasi itu murni untuk memudahkan membaca. Anda bisa +menuliskannya dengan mudah seperti: + +,[>+<-]>. + +Coba dan cari tahu apa yang program ini lakukan: + +,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> + +Program ini menerima dua buah angka sebagai input, lalu mengalikannya. + +Intinya adalah membaca dua masukan. Lalu mulai pada rekursi terluar yang +kondisinya pada cell #1. Lalu pindah ke cell #2, dan mulai rekursi terdalam +yang kondisinya ada pada cell #2, menaikkan nilai pada cell #3. Namun, +ada suatu masalah: Pada akhir dari rekursi terdalam, cell #2 bernilai nol. +Pada kasus tersebut, rekursi terdalam tidak dapat bekerja lagi mulai setelah ini. +Untuk menyelesaikan masalah tersebut, kita juga menaikkan cell #4, dan menyalin +ulang cell #4 ke cell #2. Maka cell #3 adalah hasilnya. +``` + +Dan itulah brainfuck. Tidak terlalu sulit kan? Hanya untuk iseng-iseng, anda +bisa menuliskan porgram brainfuck anda sendiri, atau anda bisa menuliskan interpreter +brainfuck pada bahasa lain. Interpreternya tidak begitu sulit untuk diimplementasikan, +tapi jika anda seorang masokis, cobalah menulis sebuah interpreter brainfuck... dalam +brainfuck. + diff --git a/id-id/hq9+-id.html.markdown b/id-id/hq9+-id.html.markdown new file mode 100644 index 00000000..46abcf42 --- /dev/null +++ b/id-id/hq9+-id.html.markdown @@ -0,0 +1,45 @@ +--- +language: HQ9+ +filename: hq9+-id.html +contributors: + - ["Alexey Nazaroff", "https://github.com/rogaven"] +translators: + - ["Haydar Ali Ismail", "http://github.com/haydarai"] +lang: id-id +--- + +HQ9+ adalah bahasa pemrograman gurauan yang dibuat oleh Cliff Biffle. Bahasa +ini hanya memiliki empat perintah dan tidak memenuhi Turing-complete. + +``` +Hanya ada 4 perintah, masing-masing direpresentasikan oleh karakter berikut +H: mencetak "Hello, world!" +Q: mencetak kode sumber dari program ini (Quine) +9: mencetak lirik dari lagu "99 Bottles of Beer" ++: menambah nilai satu ke akumulator (nilai dari akumulator tidak dapat + diakses) +Karakter lain akan dihiraukan. + +Ok. Mari kita menulis beberapa program: + HQ9 + +Hasil: + Hello world! + HQ9 + +HQ9+ sangat sederhana, tetapi membuat anda bisa melakukan hal yang sangat sulit +dilakukan di bahasa lain. Sebagai contoh, berikut sebuah program yang +menciptakan tiga salinan dirinya sendiri ke layar: + QQQ + +Ini menghasilakn: + QQQ + QQQ + QQQ +``` + +Dan itu semuanya. Ada banyak interpreters untuk HQ9+. Kamu bisa menemukannya di +bawah + ++ [Salah satu interpreter online](https://almnet.de/esolang/hq9plus.php) ++ [Website resmi HQ9+](http://cliffle.com/esoterica/hq9plus.html) 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/id-id/rst-id.html.markdown b/id-id/rst-id.html.markdown new file mode 100644 index 00000000..06d80089 --- /dev/null +++ b/id-id/rst-id.html.markdown @@ -0,0 +1,122 @@ +--- +language: restructured text (RST) +filename: rst-id.html +contributors: + - ["DamienVGN", "https://github.com/martin-damien"] + - ["Andre Polykanine", "https://github.com/Oire"] +translators: + - ["Haydar Ali Ismail", "https://github.com/haydarai"] +lang: id-id +--- + +RST adalah sebual format file yang dibuat oleh komunitas Python untuk menulis +dokumentasi (dan menjadi bagian dari Docutils). + +File-file RST adalah sebuah file-file teks simpel dengan sintaks yang ringan +(dibandingkan dengan HTML). + + +## Pemasangan + +Untuk menggunakan RST, anda harus memasang [Python](http://www.python.org) dan +paket `docutils` terlebih dahulu. + +`docutils` bisa dipasang menggunakan command berikut: + +```bash +$ easy_install docutils +``` + +Jika sistem anda sudah mempunyai `pip`, anda bisa menggunakannya juga: + +```bash +$ pip install docutils +``` + + +## Sintaks file + +Sebuah contoh sederhana dari sintaks file: + +``` +.. Baris yang dimulai dengan dua titik adalah perintah spesial. Tetapi jika +perintah tidak ditemukan, maka baris tersebut akan dianggap sebagai komentar + +=============================================================================== +Judul utama ditulis menggunakan rentetan tanda sama dengan di atas dan bawahnya +=============================================================================== + +Ingat bahwa jumlah tanda sama dengan harus sama panjangnya dengan total +karakter judul + +Judul juga digarisbawahi dengan tanda sama dengan juga +====================================================== + +Sub-judul dengan menggunakan dash +--------------------------------- + +Dan sub-sub-judul dengan menggunakan tilde +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Anda bisa menulis teks dalam *italik* atau *tebal*, anda bisa "menandai" teks +sebagai kode dengan menggunakan backquote ganda ``: ``print()``. + +Membuat daftar semudah seperti dalam Markdown: + +- Barang pertama +- Barang kedua + - Sub barang + +atau + +* Barang pertama +* Barang kedua + * Sub barang + +Tabel sangat mudah untuk ditulis: + +=========== ======== +Negara Ibu Kota +=========== ======== +Prancis Paris +Jepang Tokyo +=========== ======== + +Tabel yang lebih kompleks bisa dibuat dengan mudah (kolom tergabung atau/dan +baris) tetapi saya menyarankan anda untuk membaca dokumentasi lengkap tentang +ini :) + +Ada berbagai macam cara untuk membuat tautan: + +- Dengan menambahkan garis bawah setelah sebuah huruf : Github_ dan dengan +menambahkan URL target setelah teks (cara ini mempunyai kelebihan dengan tidak +memasukkan URL yang tidak penting ke dalam teks yang bisa dibaca). +- Dengan mengetik URL lengkap yang dapat dipahami : https://github.com (akan +otomatis diubah menjadi sebuah link) +- Dengan membuat link seperti di Markdown: `Github <https://github.com/>`_ . + +.. _Github https://github.com/ + +``` + + +## Bagaimana Cara Menggunakannya + +RST hadir dengan docutils di mana anda mempunyai `rst2html`, sebagai contoh: + +```bash +$ rst2html fileku.rst hasil.html +``` + +*Catatan : Di beberapa sistem, perintah tersebut bisa menjadi rst2html.py* + +Tetapi ada beberapa aplikasi kompleks yang menggunakan format RST: + +- [Pelican](http://blog.getpelican.com/), Generator web statik +- [Sphinx](http://sphinx-doc.org/), Generator dokumnetasi +- dan masih banyak lainnya + + +## Bacaan + +- [Referensi singkat resmi](http://docutils.sourceforge.net/docs/user/rst/quickref.html) diff --git a/it-it/bf-it.html.markdown b/it-it/bf-it.html.markdown index a79710d0..97e5b88e 100644 --- a/it-it/bf-it.html.markdown +++ b/it-it/bf-it.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: learnbf-it.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/it-it/go-it.html.markdown b/it-it/go-it.html.markdown index e005f2dc..e49ccd79 100644 --- a/it-it/go-it.html.markdown +++ b/it-it/go-it.html.markdown @@ -26,14 +26,14 @@ Aggiunge la concorrenza in maniera diretta e semplice da capire, per far forza sulle CPU multi-core di oggigiorno. Presenta caratteristiche utili per la programmazione in larga scala. -Go comes with a great standard library and an enthusiastic community. +Go include un'ottima libreria standard e ha una community entusiasta. ```go // Commento su riga singola /* Commento su riga multipla */ -// In cima a ogni file è necessario specificare il package. +// In cima ad ogni file è necessario specificare il package. // Main è un package speciale che identifica un eseguibile anziché una libreria. package main @@ -65,19 +65,19 @@ func oltreIlCiaoMondo() { x = 3 // Assegnazione di una variabile. // E' possibile la dichiarazione "rapida" := per inferire il tipo, dichiarare e assegnare contemporaneamente. y := 4 - // Una funzione che ritorna due valori. - somma, prod := imparaMoltepliciValoriDiRitorno(x, y) + // Una funzione che restituisce due valori. + somma, prod := imparaMoltepliciValoriRestituiti(x, y) fmt.Println("somma:", somma, "prodotto:", prod) // Semplice output. imparaTipi() // < y minuti, devi imparare ancora! } /* <- commento su righe multiple -Le funzioni possono avere parametri e ritornare (molteplici!) valori. -Qua, x e y sono gli argomenti, mentre somma e prod sono i valori ritornati. +Le funzioni possono avere parametri e restituire (molteplici!) valori. +In questo esempio, x e y sono gli argomenti, mentre somma e prod sono i valori restituiti. Da notare il fatto che x e somma vengono dichiarati come interi. */ -func imparaMoltepliciValoriDiRitorno(x, y int) (somma, prod int) { - return x + y, x * y // Ritorna due valori. +func imparaMoltepliciValoriRestituiti(x, y int) (somma, prod int) { + return x + y, x * y // Restituisce due valori. } // Ecco alcuni tipi presenti in Go @@ -86,7 +86,7 @@ func imparaTipi() { str := "Impara il Go!" // Tipo stringa. s2 := `Una stringa letterale -puo' includere andata a capo.` // Sempre di tipo stringa. +può includere andata a capo.` // Sempre di tipo stringa. // Stringa letterale non ASCII. I sorgenti Go sono in UTF-8. g := 'Σ' // Il tipo runa, alias per int32, è costituito da un code point unicode. @@ -144,20 +144,20 @@ puo' includere andata a capo.` // Sempre di tipo stringa. imparaControlloDiFlusso() // Torniamo in carreggiata. } -// In Go è possibile associare dei nomi ai valori di ritorno di una funzione. -// Assegnare un nome al tipo di dato ritornato permette di fare return in vari +// In Go è possibile associare dei nomi ai valori restituiti da una funzione. +// Assegnare un nome al tipo di dato restituito permette di fare return in vari // punti all'interno del corpo della funzione, ma anche di usare return senza -// specificare in modo esplicito che cosa ritornare. -func imparaValoriDiRitornoConNome(x, y int) (z int) { +// specificare in modo esplicito che cosa restituire. +func imparaValoriRestituitiConNome(x, y int) (z int) { z = x * y return // z è implicito, perchè compare nella definizione di funzione. } // Go è dotato di garbage collection. Ha i puntatori, ma non l'aritmetica dei -// puntatori. Puoi fare errori coi puntatori a nil, ma non puoi direttamente -// incrementare un puntatore. +// puntatori. Puoi commettere errori a causa di puntatori nulli, ma non puoi +// incrementare un puntatore direttamente. func imparaLaMemoria() (p, q *int) { - // I valori di ritorno (con nome) p e q sono puntatori a int. + // I valori restituiti (con nome) p e q sono puntatori a int. p = new(int) // La funzione new si occupa di allocare memoria. // L'int allocato viene inizializzato a 0, dunque p non è più nil. s := make([]int, 20) // Alloca 20 int come un singolo blocco di memoria. @@ -207,14 +207,14 @@ func imparaControlloDiFlusso() { } // x == 42 qua. - // Il for è l'unica istruzione per ciclare in Go, ma ha varie forme. + // Il for è l'unica istruzione per i loop in Go, ma ha varie forme. for { // Ciclo infinito. break // Scherzavo. continue // Non si arriva qua. } - // Puoi usare range per ciclare su un vettore, slice, stringa, mappa o canale. - // range ritorna uno (per i canali) o due valori (vettore, slice, stringa, mappa). + // Puoi usare range per iterare lungo un vettore, slice, stringa, mappa o canale. + // range restituisce uno (per i canali) o due valori (vettore, slice, stringa, mappa). for chiave, valore := range map[string]int{"uno": 1, "due": 2, "tre": 3} { // per ogni coppia dentro la mappa, stampa chiave e valore fmt.Printf("chiave=%s, valore=%d\n", chiave, valore) @@ -236,7 +236,7 @@ func imparaControlloDiFlusso() { // Inoltre le funzioni letterali possono essere definite e chiamate // inline, col ruolo di parametri di funzione, a patto che: // a) la funzione letterale venga chiamata subito (), - // b) il valore ritornato è in accordo con il tipo dell'argomento. + // b) il valore restituito è in accordo con il tipo dell'argomento. fmt.Println("Somma e raddoppia due numeri: ", func(a, b int) int { return (a + b) * 2 @@ -247,7 +247,7 @@ func imparaControlloDiFlusso() { goto amore amore: - imparaFabbricaDiFunzioni() // Una funzione che ritorna un'altra funzione è divertente! + imparaFabbricaDiFunzioni() // Una funzione che restituisce un'altra funzione è divertente! imparaDefer() // Un tour veloce di una parola chiave importante. imparaInterfacce() // Arriva la roba buona! } @@ -271,7 +271,7 @@ func fabbricaDiFrasi(miaStringa string) func(prima, dopo string) string { func imparaDefer() (ok bool) { // Le istruzioni dette "deferred" (rinviate) sono eseguite - // appena prima che la funzione ritorni. + // appena prima che la funzione abbia termine. defer fmt.Println("le istruzioni 'deferred' sono eseguite in ordine inverso (LIFO).") defer fmt.Println("\nQuesta riga viene stampata per prima perché") // defer viene usato di solito per chiudere un file, così la funzione che diff --git a/it-it/html-it.html.markdown b/it-it/html-it.html.markdown new file mode 100644 index 00000000..471019a1 --- /dev/null +++ b/it-it/html-it.html.markdown @@ -0,0 +1,121 @@ +--- +language: html +filename: learnhtml-it.html +contributors: + - ["Christophe THOMAS", "https://github.com/WinChris"] +translators: + - ["Ale46", "http://github.com/Ale46/"] +lang: it-it +--- + +HTML sta per HyperText Markup Language (linguaggio a marcatori per ipertesti). +È un linguaggio che consente di scrivere pagine web per il world wide web. +È un linguaggio di markup, che permette di scrivere pagine web usando del codice che indica come il testo ed i dati devono essere mostrati. +Infatti, i files html sono semplici file di testo. +Cos'è il markup? È un metodo per organizzare i dati della pagina circondandoli con tag di apertura e tag di chiusura. +Questo markup serve a dare significato al testo che racchiude. +Come altri linguaggi di programmazione, HTML ha molte versioni. Qui discuteremo di HTML5. + +**NOTA :** Puoi testare i differenti tags ed elementi man mano che prosegui nel tutorial in un sito come [codepen](http://codepen.io/pen/) per vedere i loro effetti, capire come lavorano e familiarizzare con il linguaggio. +Questo articolo riguarda principalmente la sintassi HTML ed alcuni suggerimenti utili. + + +```html +<!-- I commenti sono racchiusi come in questa riga! --> + +<!-- #################### I Tags #################### --> + +<!-- Ecco un esempio di file HTML che andremo ad analizzare. --> + +<!doctype html> + <html> + <head> + <title>Il mio sito</title> + </head> + <body> + <h1>Ciao, mondo!</h1> + <a href = "http://codepen.io/anon/pen/xwjLbZ">Vieni a vedere ciò che mostra</a> + <p>Questo è un paragrafo.</p> + <p>Questo è un altro paragrafo.</p> + <ul> + <li>Questo è un elemento di un elenco non numerato (elenco puntato)</li> + <li>Questo è un altro elemento</li> + <li>E questo è l'ultimo elemento dell'elenco</li> + </ul> + </body> + </html> + +<!-- Un file HTML inizia sempre indicando al browser che la pagina è HTML. --> +<!doctype html> + +<!-- Dopo questo, inizia aprendo un tag <html>. --> +<html> + +<!-- che sarà chiuso alla fine del file con </html>. --> +</html> + +<!-- Nulla dovrebbe apparire dopo questo tag finale. --> + +<!-- All'interno (tra i tag di apertura e chiusura <html> </html>) troviamo: --> + +<!-- Un'intestazione definita da <head> (deve essere chiusa con </head>). --> +<!-- L'intestazione contiene alcune descrizioni e informazioni aggiuntive non visualizzate; questi sono i metadati. --> + +<head> + <title>Il mio sito</title> <!-- Il tag <title> indica al browser il titolo da mostrare nella barra del titolo della finestra del browser e nel nome della scheda. --> +</head> + +<!-- Dopo la sezione <head>, troviamo il tag - <body> --> +<!-- Fino a questo punto, niente di ciò che abbiamo descritto verrà visualizzato nella finestra del browser. --> +<!-- Dobbiamo riempire il corpo con il contenuto da visualizzare. --> + +<body> + <h1>Ciao, mondo!</h1> <!-- Il tag h1 crea un titolo. --> + <!-- Ci sono anche sottotitoli a <h1> dal più importante (h2) al più preciso (h6). --> + <a href = "http://codepen.io/anon/pen/xwjLbZ">Vieni a vedere ciò che mostra</a> <!-- un collegamento ipertestuale all'URL fornito dall'attributo href="" --> + <p>Questo è un paragrafo.</p> <!-- Il tag <p> ci permette di includere del testo nella pagina html. --> + <p>Questo è un altro paragrafo.</p> + <ul> <!-- Il tag <ul> crea un elenco puntato. --> + <!-- Per avere un elenco numerato, invece, usiamo <ol> che restituisce 1. per il primo elemento, 2. per il secondo, etc. --> + <li>Questo è un elemento in un elenco non elencato (elenco puntato)</li> + <li>Questo è un altro elemento</li> + <li>E questo è l'ultimo elemento dell'elenco</li> + </ul> +</body> + +<!-- E questo è tutto, creare un file HTML può essere molto semplice. --> + +<!-- Ma è possibile aggiungere molti altri tipi di tag HTML. --> + +<!-- Per inserire un'immagine. --> +<img src="http://i.imgur.com/XWG0O.gif"/> <!-- La fonte dell'immagine viene indicata usando l'attributo src="" --> +<!-- La fonte può essere un URL o persino un percorso di un file sul tuo computer. --> + +<!-- È anche possibile creare una tabella. --> + +<table> <!-- Apriamo un elemento <table>. --> + <tr> <!-- <tr> ci permette di creare una riga. --> + <th>Prima intestazione</th> <!-- <th> ci permette di dare un titolo ad una colonna della tabella. --> + <th>Seconda intestazione</th> + </tr> + <tr> + <td>prima riga, prima colonna</td> <!-- <td> ci permette di creare una cella della tabella. --> + <td>prima riga, seconda colonna</td> + </tr> + <tr> + <td>seconda riga, prima colonna</td> + <td>seconda riga, seconda colonna</td> + </tr> +</table> + +``` + +## Uso + +HTML è scritto in files che finiscono con `.html`. + +## Per saperne di più + +* [wikipedia](https://it.wikipedia.org/wiki/HTML) +* [HTML tutorial](https://developer.mozilla.org/it/docs/Web/HTML) +* [W3School](http://www.w3schools.com/html/html_intro.asp) diff --git a/it-it/json-it.html.markdown b/it-it/json-it.html.markdown index 379bad73..e1e16c42 100644 --- a/it-it/json-it.html.markdown +++ b/it-it/json-it.html.markdown @@ -1,5 +1,6 @@ --- language: json +filename: learnjson-it.json contributors: - ["Anna Harren", "https://github.com/iirelu"] - ["Marco Scannadinari", "https://github.com/marcoms"] diff --git a/it-it/markdown.html.markdown b/it-it/markdown.html.markdown index b006dbb4..b0a123f1 100644 --- a/it-it/markdown.html.markdown +++ b/it-it/markdown.html.markdown @@ -2,41 +2,65 @@ language: markdown contributors: - ["Dan Turkel", "http://danturkel.com/"] + - ["Jacob Ward", "http://github.com/JacobCWard/"] translators: - ["Jacopo Andrea Giola", "http://geekpanda.net"] + - ["Ale46", "https://github.com/Ale46"] filename: markdown-it.md lang: it-it --- Markdown è stato creato da John Gruber nel 2004. Il suo scopo è quello di essere una sintassi facile da leggere e scrivere, e che può essere convertita in HTML (ad oggi anche in molti altri formati). -Mandate tutto il feedback che volete! / Sentitevi liberi di forkare o di mandare pull request! - +Markdown varia nelle sue implementazioni da un parser all'altro. Questa guida cercherà di chiarire quali caratteristiche esistono a livello globale o quando sono disponibili solo per un determinato parser. + +- [Elementi HTML](#elementi-html) +- [Titoli](#titoli) +- [Stili di testo semplici](#stili-di-testo-semplici) +- [Paragrafi](#paragrafi) +- [Liste](#liste) +- [Estratti di codice](#estratti-di-codice) +- [Linea orizzontale](#linea-orizzontale) +- [Links](#links) +- [Immagini](#immagini) +- [Miscellanea](#miscellanea) + +## Elementi HTML +Markdown è un superset di HTML, quindi ogni file HTML è a sua volta un file Markdown valido. + +```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 +all'interno del contenuto dell'elemento. --> +``` -```markdown -<!-- Markdown è un superset di HTML, quindi ogni file HTML è a sua volta un file Markdown valido. 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 all'interno del contenuto dell'elemento. --> +## Titoli -<!-- L'implementazione di Markdown inoltre cambia da parser a parser. In questa guida cercheremo di indicare quando una feature è universale e quando sono specifiche ad un certo parser. --> +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 -<!-- Titoli --> -<!-- Potete creare gli elementi HTML da <h1> ad <h6> facilmente, basta che inseriate un egual numero di caratteri cancelletto (#) prima del testo che volete all'interno dell'elemento --> +```md # Questo è un <h1> ## Questo è un <h2> ### Questo è un <h3> #### Questo è un <h4> ##### Questo è un <h5> ###### Questo è un <h6> +``` +Markdown inoltre fornisce due alternative per indicare gli elementi h1 e h2 -<!-- Markdown inoltre fornisce due alternative per indicare gli elementi h1 e h2 --> +```md Questo è un h1 ============== Questo è un h2 -------------- +``` -<!-- Stili di testo semplici --> -<!-- Il testo può essere stilizzato in corsivo o grassetto usando markdown --> +## Stili di testo semplici +Il testo può essere stilizzato in corsivo o grassetto usando markdown +```md *Questo testo è in corsivo.* _Come pure questo._ @@ -46,30 +70,38 @@ __Come pure questo.__ ***Questo testo è stilizzato in entrabmi i modi.*** **_Come questo!_** *__E questo!__* +``` -<!-- In Github Flavored Markdown, che è utilizzato per renderizzare i file markdown su -Github, è presente anche lo stile barrato --> +In Github Flavored Markdown, che è utilizzato per renderizzare i file markdown su Github, è presente anche lo stile barrato: +```md ~~Questo testo è barrato.~~ +``` +## Paragrafi -<!-- I paragrafi sono uno o più linee di testo addiacenti separate da una o più righe vuote. --> +```md +I paragrafi sono una o più linee di testo adiacenti separate da una o più righe vuote. -Qeusto è un paragrafo. Sto scrivendo in un paragrafo, non è divertente? +Questo è un paragrafo. Sto scrivendo in un paragrafo, non è divertente? Ora sono nel paragrafo 2. Anche questa linea è nel paragrafo 2! 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. --> +Se volete inserire l'elemento HTML `<br />`, potete terminare la linea con due o più spazi e poi iniziare un nuovo paragrafo. +```md Questa frase finisce con due spazi (evidenziatemi per vederli). C'è un <br /> sopra di me! +``` -<!-- Le citazioni sono semplici da inserire, basta usare il carattere >. --> +Le citazioni sono semplici da inserire, basta usare il carattere >. +```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 `>`. @@ -78,9 +110,12 @@ C'è un <br /> sopra di me! >> di indentazione! > Quanto è comodo? -<!-- Liste --> -<!-- Le liste non ordinate possono essere inserite usando gli asterischi, il simbolo più o dei trattini --> +``` +## Liste +Le liste non ordinate possono essere inserite usando gli asterischi, il simbolo più o dei trattini + +```md * Oggetto * Oggetto * Altro oggetto @@ -96,149 +131,180 @@ oppure - Oggetto - Oggetto - Un ultimo oggetto +``` -<!-- Le liste ordinate invece, sono inserite con un numero seguito da un punto. --> +Le liste ordinate invece, sono inserite con un numero seguito da un punto. +```md 1. Primo oggetto 2. Secondo oggetto 3. Terzo oggetto +``` -<!-- Non dovete nemmeno mettere i numeri nell'ordine giusto, markdown li visualizzerà comunque nell'ordine corretto, anche se potrebbe non essere una buona idea. --> +Non dovete nemmeno mettere i numeri nell'ordine giusto, markdown li visualizzerà comunque nell'ordine corretto, anche se potrebbe non essere una buona idea. +```md 1. Primo oggetto 1. Secondo oggetto 1. Terzo oggetto -<!-- (Questa lista verrà visualizzata esattamente come quella dell'esempio prima) --> +``` +(Questa lista verrà visualizzata esattamente come quella dell'esempio prima) -<!-- Potete inserire anche sotto liste --> +Potete inserire anche sotto liste +```md 1. Primo oggetto 2. Secondo oggetto 3. Terzo oggetto * Sotto-oggetto * Sotto-oggetto 4. Quarto oggetto +``` -<!-- Sono presenti anche le task list. In questo modo è possibile creare checkbox in HTML. --> +Sono presenti anche le task list. In questo modo è possibile creare checkbox in HTML. +```md I box senza la 'x' sono checkbox HTML ancora da completare. - [ ] Primo task da completare. - [ ] Secondo task che deve essere completato. Il box subito sotto è una checkbox HTML spuntata. - [x] Questo task è stato completato. +``` +## Estratti di codice -<!-- Estratti di codice --> -<!-- Potete inserire un estratto di codice (che utilizza l'elemento <code>) indentando una linea con quattro spazi oppure con un carattere tab --> +Potete inserire un estratto di codice (che utilizza l'elemento `<code>`) indentando una linea con quattro spazi oppure con un carattere tab. +```md Questa è una linea di codice Come questa +``` -<!-- Potete inoltre inserire un altro tab (o altri quattro spazi) per indentare il vostro codice --> +Potete inoltre inserire un altro tab (o altri quattro spazi) per indentare il vostro codice +```md my_array.each do |item| puts item end +``` -<!-- Codice inline può essere inserito usando il carattere backtick ` --> +Codice inline può essere inserito usando il carattere backtick ` +```md Giovanni non sapeva neppure a cosa servisse la funzione `go_to()`! +``` -<!-- In Github Flavored Markdown, potete inoltre usare una sintassi speciale per il codice --> - -\`\`\`ruby <!-- In realtà dovete rimuovere i backslash, usate solo ```ruby ! --> +In Github Flavored Markdown, potete inoltre usare una sintassi speciale per il codice +<pre> +<code class="highlight">```ruby def foobar puts "Hello world!" end -\`\`\` <!-- Anche qui, niente backslash, solamente ``` --> - -<!-- Se usate questa sintassi, il testo non richiederà di essere indentanto, inoltre Github userà la syntax highlighting del linguaggio specificato dopo i ``` iniziali --> +```</code></pre> +Se usate questa sintassi, il testo non richiederà di essere indentato, inoltre Github userà l'evidenziazione della sintassi del linguaggio specificato dopo i \`\`\` iniziali -<!-- Linea orizzontale (<hr />) --> -<!-- Le linee orizzontali sono inserite facilemtne usanto tre o più asterischi o trattini senza spazi consecutivi e senza spazi. --> +## Linea orizzontale +Le linee orizzontali (`<hr/>`) sono inserite facilmente usanto tre o più asterischi o trattini, con o senza spazi. +```md *** --- - - - **************** +``` -<!-- Link --> -<!-- 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 () --> +## 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 () +```md [Cliccami!](http://test.com/) +``` -<!-- Potete inoltre aggiungere al link un titolo mettendolo fra doppie apici dopo il link --> +Potete inoltre aggiungere al link un titolo mettendolo fra doppi apici dopo il link +```md [Cliccami!](http://test.com/ "Link a Test.com") +``` -<!-- La sintassi funziona anche i path relativi. --> +La sintassi funziona anche con i path relativi. +```md [Vai a musica](/music/). +``` -<!-- Markdown supporta inoltre anche la possibilità di aggiungere i link facendo riferimento ad altri punti del testo --> - +Markdown supporta inoltre anche la possibilità di aggiungere i link facendo riferimento ad altri punti del testo. +```md [Apri questo link][link1] per più informazioni! [Guarda anche questo link][foobar] se ti va. [link1]: http://test.com/ "Bello!" [foobar]: http://foobar.biz/ "Va bene!" +``` +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. -<!-- Il 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 --> - +Esiste anche un "identificativo implicito" che vi permette di usare il testo del link come id. +```md [Questo][] è un link. [Questo]: http://thisisalink.com/ +``` +Ma non è comunemente usato. -<!-- Ma non è comunemente usato. --> - -<!-- Immagini --> -<!-- Le immagini sono inserite come i link ma con un punto esclamativo inserito prima delle parentesi quadre! --> +## Immagini +Le immagini sono inserite come i link ma con un punto esclamativo inserito prima delle parentesi quadre! +```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 --> +E la modalità a riferimento funziona esattamente come ci si aspetta +```md ![Questo è il testo alternativo.][myimage] [myimage]: relative/urls/cool/image.jpg "Se vi serve un titolo, lo mettete qui" +``` +## Miscellanea +### Auto link -<!-- Miscellanea --> -<!-- Auto link --> - +```md <http://testwebsite.com/> è equivalente ad [http://testwebsite.com/](http://testwebsite.com/) +``` +### Auto link per le email -<!-- Auto link per le email --> - +```md <foo@bar.com> +``` +### Caratteri di escaping -<!-- Caratteri di escaping --> - +```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 --> +### Combinazioni di tasti +In Github Flavored Markdown, potete utilizzare il tag `<kbd>` per raffigurare i tasti della tastiera. +```md Il tuo computer è crashato? Prova a premere <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Canc</kbd> +``` -<!-- Tabelle --> -<!-- Le tabelle sono disponibili solo in Github Flavored Markdown e sono leggeremente complesse, ma se proprio volete inserirle fate come segue: --> +### Tabelle +Le tabelle sono disponibili solo in Github Flavored Markdown e sono leggeremente complesse, ma se proprio volete inserirle fate come segue: +```md | Col1 | Col2 | Col3 | | :------------------- | :------: | -----------------: | | Allineato a sinistra | Centrato | Allineato a destra | | blah | blah | blah | +``` +oppure, per lo stesso risultato -<!-- oppure, per lo stesso risultato --> - +```md Col 1 | Col2 | Col3 :-- | :-: | --: È una cosa orrenda | fatela | finire in fretta - -<!-- Finito! --> - ``` +--- Per altre informazioni, leggete il post ufficiale di John Gruber sulla sintassi [qui](http://daringfireball.net/projects/markdown/syntax) e il magnifico cheatsheet di Adam Pritchard [qui](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet). diff --git a/it-it/matlab-it.html.markdown b/it-it/matlab-it.html.markdown index aeb42658..8d6d4385 100644 --- a/it-it/matlab-it.html.markdown +++ b/it-it/matlab-it.html.markdown @@ -217,7 +217,7 @@ A .* B % Moltiplica ogni elemento di A per il corrispondente elemento di B % l'altra (il cui nome termina con m) agisce sull'intera matrice. exp(A) % Calcola l'esponenziale di ogni elemento expm(A) % Calcola la matrice esponenziale -sqrt(A) % Calcola la radice quadrata di ogni elementotake the square root of each element +sqrt(A) % Calcola la radice quadrata di ogni elemento sqrtm(A) % Trova la matrice di cui A nè è la matrice quadrata diff --git a/it-it/python-it.html.markdown b/it-it/python-it.html.markdown index 71f6dc1c..e81d1676 100644 --- a/it-it/python-it.html.markdown +++ b/it-it/python-it.html.markdown @@ -1,11 +1,11 @@ --- language: python +filename: learnpython-it.py contributors: - ["Louie Dinh", "http://ldinh.ca"] - ["Amin Bandali", "http://aminbandali.com"] - ["Andre Polykanine", "https://github.com/Oire"] - ["evuez", "http://github.com/evuez"] -filename: learnpython.py translators: - ["Ale46", "http://github.com/Ale46/"] - ["Tommaso Pifferi", "http://github.com/neslinesli93/"] 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/it-it/rst-it.html.markdown b/it-it/rst-it.html.markdown new file mode 100644 index 00000000..a834e899 --- /dev/null +++ b/it-it/rst-it.html.markdown @@ -0,0 +1,111 @@ +--- +language: restructured text (RST) +filename: restructuredtext-it.rst +contributors: + - ["DamienVGN", "https://github.com/martin-damien"] + - ["Andre Polykanine", "https://github.com/Oire"] +translators: + - ["Ale46", "https://github.com/Ale46"] + - ["Chris54721", "https://chris54721.net"] +lang: it-it +--- + +RST (Restructured Text) è un formato di file inizialmente creato dalla comunità Python +per la documentazione (per questo motivo appartiene a Docutils). + +I file RST sono semplici file di testo con una sintassi leggera (in confronto all'HTML). + +## Installazione + +Per usare Restructured Text, sarà necessario installare [Python](http://www.python.org) ed il pacchetto `docutils`. + +`docutils` può essere installato da riga di comando: + +```bash +$ easy_install docutils +``` + +Oppure, se hai `pip` installato sul tuo sistema: + +```bash +$ pip install docutils +``` + + +## Sintassi del file + +Ecco un semplice esempio della sintassi RST: + +``` +.. Le righe che iniziano con due punti sono comandi speciali. Ma se non è possibile trovare alcun comando, la riga viene considerata come un commento + +=============================================================================== +I titoli principali sono scritti utilizzando caratteri di uguale, sopra e sotto +=============================================================================== + +Si noti che devono esserci tanti caratteri di uguale quanti caratteri del titolo. + +Anche i titoli normali usano caratteri di uguale, ma solo sotto +=============================================================== + +I sottotitoli usano i trattini +------------------------------ + +E i sotto-sottotitoli le tildi +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Puoi inserire il testo in *corsivo* o in **grassetto**, puoi "contrassegnare" il testo come codice con un doppio apice ``: `` print () ``. + +Le liste sono semplici come in Markdown: + +- primo articolo +- Secondo elemento + - Sottoelemento + +oppure + +* Primo elemento +* Secondo elemento + * Sottoelemento + +Le tabelle sono molto semplici da inserire: + +=========== ======== +Stato Capitale +=========== ======== +Francia Parigi +Giappone Tokio +=========== ======== + +Anche le tabelle più complesse possono essere inserite facilmente (colonne e/o righe unite) ma ti suggerisco di leggere la documentazione completa per questo :) + +Esistono diversi modi per creare collegamenti: + +- Aggiungendo un underscore dopo una parola: Github_ e aggiungendo l'URL di destinazione dopo il testo (questo metodo ha il vantaggio di non inserire URL non necessari all'interno del testo leggibile). +- Digitando un URL completo: https://github.com/ (verrà automaticamente convertito in un collegamento) +- Utilizzando una sintassi simile a Markdown: `Github <https://github.com/>`_ . + +.. _Github https://github.com/ + +``` + +## Come usarlo + +RST viene fornito con docutils, che dispone di `rst2html`, per esempio: + +```bash +$ rst2html miofile.rst output.html +``` + +*Nota : In alcuni sistemi il comando potrebbe essere rst2html.py* + +Ma ci sono applicazioni più complesse che utilizzano il formato RST: + +- [Pelican](http://blog.getpelican.com/), un generatore di siti statici +- [Sphinx](http://sphinx-doc.org/), un generatore di documentazione +- e molti altri + + +## Letture + +- [Riferimento ufficiale rapido](http://docutils.sourceforge.net/docs/user/rst/quickref.html) diff --git a/ja-jp/php-jp.html.markdown b/ja-jp/php-jp.html.markdown index 112916f4..a02ae56a 100644 --- a/ja-jp/php-jp.html.markdown +++ b/ja-jp/php-jp.html.markdown @@ -119,7 +119,7 @@ echo 'Multiple', 'Parameters', 'Valid'; define("FOO", "something"); // 定義した名前をそのまま($はつけずに)使用することで、定数にアクセスできます -// access to a constant is possible by direct using the choosen name +// access to a constant is possible by direct using the chosen name echo 'This outputs '.FOO; diff --git a/java.html.markdown b/java.html.markdown index e8c15234..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 --- @@ -44,20 +45,18 @@ import java.util.ArrayList; // Import all classes inside of java.security package import java.security.*; -// Each .java file contains one outer-level public class, with the same name -// as the file. public class LearnJava { - // In order to run a java program, it must have a main method as an entry + // In order to run a java program, it must have a main method as an entry // point. - public static void main (String[] args) { + public static void main(String[] args) { /////////////////////////////////////// // Input/Output /////////////////////////////////////// /* - * Ouput + * Output */ // Use System.out.println() to print lines. @@ -109,7 +108,7 @@ public class LearnJava { */ // Declare a variable using <type> <name> int fooInt; - // Declare multiple variables of the same + // Declare multiple variables of the same // type <type> <name1>, <name2>, <name3> int fooInt1, fooInt2, fooInt3; @@ -119,8 +118,9 @@ public class LearnJava { // Initialize a variable using <type> <name> = <val> int barInt = 1; - // Initialize multiple variables of same type with same - // value <type> <name1>, <name2>, <name3> = <val> + // Initialize multiple variables of same type with same + // value <type> <name1>, <name2>, <name3> + // <name1> = <name2> = <name3> = <val> int barInt1, barInt2, barInt3; barInt1 = barInt2 = barInt3 = 1; @@ -130,7 +130,7 @@ public class LearnJava { // Byte - 8-bit signed two's complement integer // (-128 <= byte <= 127) byte fooByte = 100; - + // If you would like to interpret a byte as an unsigned integer // then this simple operation can help int unsignedIntLessThan256 = 0xff & fooByte; @@ -172,7 +172,7 @@ public class LearnJava { // Char - A single 16-bit Unicode character char fooChar = 'A'; - // final variables can't be reassigned to another object, + // final variables can't be reassigned, final int HOURS_I_WORK_PER_WEEK = 9001; // but they can be initialized later. final double E; @@ -184,12 +184,12 @@ public class LearnJava { // integers longer than 64-bits. Integers are stored as an array of // of bytes and are manipulated using functions built into BigInteger // - // BigInteger can be initialized using an array of bytes or a string. + // BigInteger can be initialized using an array of bytes or a string. BigInteger fooBigInteger = new BigInteger(fooByteArray); // BigDecimal - Immutable, arbitrary-precision signed decimal number // - // A BigDecimal takes two parts: an arbitrary precision integer + // A BigDecimal takes two parts: an arbitrary precision integer // unscaled value and a 32-bit integer scale // // BigDecimal allows the programmer complete control over decimal @@ -199,7 +199,7 @@ public class LearnJava { // BigDecimal can be initialized with an int, long, double or String // or by initializing the unscaled value (BigInteger) and scale (int). BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt); - + // Be wary of the constructor that takes a float or double as // the inaccuracy of the float/double will be copied in BigDecimal. // Prefer the String constructor when you need an exact value. @@ -231,13 +231,13 @@ public class LearnJava { builderConcatenated.append("You "); builderConcatenated.append("can use "); builderConcatenated.append("the StringBuilder class."); - System.out.println(builderConcatenated.toString()); // only now is the string built + System.out.println(builderConcatenated.toString()); // only now is the string built // Output: You can use the StringBuilder class. - + // StringBuilder is efficient when the fully constructed String is not required until the end of some processing. StringBuilder stringBuilder = new StringBuilder(); String inefficientString = ""; - for(int i = 0 ; i < 10; i++){ + for (int i = 0 ; i < 10; i++) { stringBuilder.append(i).append(" "); inefficientString += i + " "; } @@ -246,12 +246,12 @@ public class LearnJava { // inefficientString requires a lot more work to produce, as it generates a String on every loop iteration. // Simple concatenation with + is compiled to a StringBuilder and toString() // Avoid string concatenation in loops. - + // #3 - with String formatter // Another alternative way to create strings. Fast and readable. String.format("%s may prefer %s.", "Or you", "String.format()"); // Output: Or you may prefer String.format(). - + // Arrays // The array size must be decided upon instantiation // The following formats work for declaring an array @@ -279,7 +279,7 @@ public class LearnJava { // LinkedLists - Implementation of doubly-linked list. All of the // operations perform as could be expected for a // doubly-linked list. - // Maps - A set of objects that map keys to values. Map is + // Maps - A mapping of key Objects to value Objects. Map is // an interface and therefore cannot be instantiated. // The type of keys and values contained in a Map must // be specified upon instantiation of the implementing @@ -288,10 +288,16 @@ public class LearnJava { // HashMaps - This class uses a hashtable to implement the Map // interface. This allows the execution time of basic // operations, such as get and insert element, to remain - // constant even for large sets. - // TreeMap - This class is a sorted tree structure. It implements a red - // black tree and sorts the entries based on the key value or - // the comparator provided while creating the object + // constant-amortized even for large sets. + // TreeMap - A Map that is sorted by its keys. Each modification + // maintains the sorting defined by either a Comparator + // supplied at instantiation, or comparisons of each Object + // if they implement the Comparable interface. + // Failure of keys to implement Comparable combined with failure to + // supply a Comparator will throw ClassCastExceptions. + // Insertion and removal operations take O(log(n)) time + // so avoid using this data structure unless you are taking + // advantage of the sorting. /////////////////////////////////////// // Operators @@ -305,7 +311,7 @@ public class LearnJava { System.out.println("2-1 = " + (i2 - i1)); // => 1 System.out.println("2*1 = " + (i2 * i1)); // => 2 System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int returns int) - System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5 + System.out.println("1/2.0 = " + (i1 / (double)i2)); // => 0.5 // Modulo System.out.println("11%3 = "+(11 % 3)); // => 2 @@ -387,7 +393,7 @@ public class LearnJava { // Iterated 10 times, fooFor 0->9 } System.out.println("fooFor Value: " + fooFor); - + // Nested For Loop Exit with Label outer: for (int i = 0; i < 10; i++) { @@ -398,7 +404,7 @@ public class LearnJava { } } } - + // For Each Loop // The for loop is also able to iterate over arrays as well as objects // that implement the Iterable interface. @@ -416,6 +422,9 @@ public class LearnJava { // It also works with enumerated types (discussed in Enum Types), the // String class, and a few special classes that wrap primitive types: // Character, Byte, Short, and Integer. + // Starting in Java 7 and above, we can also use the String type. + // Note: Do remember that, not adding "break" at end any particular case ends up in + // executing the very next case(given it satisfies the condition provided) as well. int month = 3; String monthString; switch (month) { @@ -429,38 +438,21 @@ public class LearnJava { break; } System.out.println("Switch Case Result: " + monthString); - - // Starting in Java 7 and above, switching Strings works like this: - String myAnswer = "maybe"; - switch(myAnswer) { - case "yes": - System.out.println("You answered yes."); - break; - case "no": - System.out.println("You answered no."); - break; - case "maybe": - System.out.println("You answered maybe."); - break; - default: - System.out.println("You answered " + myAnswer); - break; - } - - + + // Try-with-resources (Java 7+) // Try-catch-finally statements work as expected in Java but in Java 7+ // the try-with-resources statement is also available. Try-with-resources // simplifies try-catch-finally statements by closing resources // automatically. - + // In order to use a try-with-resources, include an instance of a class // in the try statement. The class must implement java.lang.AutoCloseable. - try(BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) { + try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) { // You can attempt to do something that could throw an exception. System.out.println(br.readLine()); // In Java 7, the resource will always be closed, even if it throws - // an Exception. + // an Exception. } catch (Exception ex) { //The resource will be closed before the catch statement executes. System.out.println("readLine() failed."); @@ -470,18 +462,22 @@ public class LearnJava { // a finally statement might not be called. // To learn more: // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html - - + + // Conditional Shorthand // You can use the '?' operator for quick assignments or logic forks. // Reads as "If (statement) is true, use <first value>, otherwise, use // <second value>" int foo = 5; String bar = (foo < 10) ? "A" : "B"; - System.out.println(bar); // Prints A, because the statement is true + System.out.println("bar : " + bar); // Prints "bar : A", because the + // statement is true. + // Or simply + System.out.println("bar : " + (foo < 10 ? "A" : "B")); + //////////////////////////////////////// - // Converting Data Types And Typecasting + // Converting Data Types //////////////////////////////////////// // Converting data @@ -497,11 +493,6 @@ public class LearnJava { // Long // String - // Typecasting - // You can also cast Java objects, there's a lot of details and deals - // with some more intermediate concepts. Feel free to check it out here: - // https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html - /////////////////////////////////////// // Classes And Functions /////////////////////////////////////// @@ -566,10 +557,10 @@ class Bicycle { String name; // default: Only accessible from within this package static String className; // Static class variable - // Static block + // Static block // Java has no implementation of static constructors, but - // has a static block that can be used to initialize class variables - // (static variables). + // has a static block that can be used to initialize class variables + // (static variables). // This block will be called when the class is loaded. static { className = "Bicycle"; @@ -652,6 +643,14 @@ class PennyFarthing extends Bicycle { } } +// Object casting +// Since the PennyFarthing class is extending the Bicycle class, we can say +// a PennyFarthing is a Bicycle and write : +// Bicycle bicycle = new PennyFarthing(); +// This is called object casting where an object is taken for another one. There +// are lots of details and deals with some more intermediate concepts here: +// https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html + // Interfaces // Interface declaration syntax // <access-level> interface <interface-name> extends <super-interfaces> { @@ -667,10 +666,10 @@ public interface Edible { public interface Digestible { public void digest(); - // In Java 8, interfaces can have default method. - // public void digest() { - // System.out.println("digesting ..."); - // } + // Since Java 8, interfaces can have default method. + public default void defaultMethod() { + System.out.println("Hi from default method ..."); + } } // We can now create a class that implements both of these interfaces. @@ -703,20 +702,27 @@ public class ExampleClass extends ExampleClassParent implements InterfaceOne, // Abstract Classes // Abstract Class declaration syntax -// <access-level> abstract <abstract-class-name> extends <super-abstract-classes> { +// <access-level> abstract class <abstract-class-name> extends +// <super-abstract-classes> { // // Constants and variables // // Method declarations // } -// Marking a class as abstract means that it contains abstract methods that -// must be defined in a child class. Similar to interfaces, abstract classes -// cannot be instantiated, but instead must be extended and the abstract -// methods defined. Different from interfaces, abstract classes can contain a -// mixture of concrete and abstract methods. Methods in an interface cannot -// have a body, unless the method is static, and variables are final by default, -// unlike an abstract class. Also abstract classes CAN have the "main" method. +// Abstract Classes cannot be instantiated. +// Abstract classes may define abstract methods. +// Abstract methods have no body and are marked abstract +// Non-abstract child classes must @Override all abstract methods +// from their super-classes. +// Abstract classes can be useful when combining repetitive logic +// with customised behavior, but as Abstract classes require +// inheritance, they violate "Composition over inheritance" +// so consider other approaches using composition. +// https://en.wikipedia.org/wiki/Composition_over_inheritance + public abstract class Animal { + private int age; + public abstract void makeSound(); // Method can have a body @@ -727,17 +733,12 @@ public abstract class Animal age = 30; } - // No need to initialize, however in an interface - // a variable is implicitly final and hence has - // to be initialized. - private int age; - public void printAge() { - System.out.println(age); + System.out.println(age); } - // Abstract classes can have main function. + // Abstract classes can have main method. public static void main(String[] args) { System.out.println("I am abstract"); @@ -816,18 +817,18 @@ public abstract class Mammal() // you would specify a days-of-the-week enum type as: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, - THURSDAY, FRIDAY, SATURDAY + THURSDAY, FRIDAY, SATURDAY } // We can use our enum Day like that: public class EnumTest { // Variable Enum Day day; - + public EnumTest(Day day) { this.day = day; } - + public void tellItLikeItIs() { switch (day) { case MONDAY: @@ -835,17 +836,17 @@ public class EnumTest { break; case FRIDAY: System.out.println("Fridays are better."); - break; - case SATURDAY: + break; + case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); - break; + break; default: System.out.println("Midweek days are so-so."); break; } } - + public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); // => Mondays are bad. @@ -854,10 +855,112 @@ public class EnumTest { } } -// Enum types are much more powerful than we show above. +// Enum types are much more powerful than we show above. // 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 @@ -893,6 +996,8 @@ The links provided here below are just to get an understanding of the topic, fee * [Codingbat.com](http://codingbat.com/java) +* [Codewars - Java Katas](https://www.codewars.com/?language=java) + **Books**: * [Head First Java](http://www.headfirstlabs.com/books/hfjava/) diff --git a/javascript.html.markdown b/javascript.html.markdown index 85c8a52d..ecaf02c5 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -103,7 +103,7 @@ false; // ... which works with more than just strings "1, 2, " + 3; // = "1, 2, 3" -"Hello " + ["world", "!"] // = "Hello world,!" +"Hello " + ["world", "!"]; // = "Hello world,!" // and are compared with < and > "a" < "b"; // = true @@ -180,6 +180,24 @@ myArray.length; // = 4 // Add/Modify at specific index myArray[3] = "Hello"; +// Add and remove element from front or back end of an array +myArray.unshift(3); // Add as the first element +someVar = myArray.shift(); // Remove first element and return it +myArray.push(3); // Add as the last element +someVar = myArray.pop(); // Remove last element and return it + +// Join all elements of an array with semicolon +var myArray0 = [32,false,"js",12,56,90]; +myArray0.join(";") // = "32;false;js;12;56;90" + +// Get subarray of elements from index 1 (include) to 4 (exclude) +myArray0.slice(1,4); // = [false,"js",12] + +// Remove 4 elements starting from index 2, and insert there strings +// "hi","wr" and "ld"; return removed subarray +myArray0.splice(2,4,"hi","wr","ld"); // = ["js",12,56,90] +// myArray0 === [32,false,"hi","wr","ld"] + // JavaScript's objects are equivalent to "dictionaries" or "maps" in other // languages: an unordered collection of key-value pairs. var myObj = {key1: "Hello", key2: "World"}; @@ -222,7 +240,7 @@ while (true){ var input; do { input = getInput(); -} while (!isValid(input)) +} while (!isValid(input)); // The `for` loop is the same as C and Java: // initialization; continue condition; iteration. @@ -248,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"; @@ -293,7 +320,7 @@ myFunction("foo"); // = "FOO" // automatic semicolon insertion. Watch out for this when using Allman style. function myFunction(){ return // <- semicolon automatically inserted here - {thisIsAn: 'object literal'} + {thisIsAn: 'object literal'}; } myFunction(); // = undefined @@ -388,7 +415,7 @@ myFunc(); // = undefined // through `this`, even if it wasn't attached when it was defined. var myOtherFunc = function(){ return this.myString.toUpperCase(); -} +}; myObj.myOtherFunc = myOtherFunc; myObj.myOtherFunc(); // = "HELLO WORLD!" @@ -397,7 +424,7 @@ myObj.myOtherFunc(); // = "HELLO WORLD!" var anotherFunc = function(s){ return this.myString + s; -} +}; anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!" // The `apply` function is nearly identical, but takes an array for an argument @@ -420,7 +447,7 @@ boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!" // `bind` can also be used to partially apply (curry) a function. -var product = function(a, b){ return a * b; } +var product = function(a, b){ return a * b; }; var doubler = product.bind(this, 2); doubler(8); // = 16 @@ -430,11 +457,11 @@ doubler(8); // = 16 var MyConstructor = function(){ this.myNumber = 5; -} +}; myNewObj = new MyConstructor(); // = {myNumber: 5} myNewObj.myNumber; // = 5 -// Unlike most other popular object-oriented languages, JavaScript has no +// Unlike most other popular object-oriented languages, JavaScript has no // concept of 'instances' created from 'class' blueprints; instead, JavaScript // combines instantiation and inheritance into a single concept: a 'prototype'. @@ -451,7 +478,7 @@ var myObj = { var myPrototype = { meaningOfLife: 42, myFunc: function(){ - return this.myString.toLowerCase() + return this.myString.toLowerCase(); } }; @@ -515,7 +542,7 @@ MyConstructor.prototype = { }; var myNewObj2 = new MyConstructor(); myNewObj2.getMyNumber(); // = 5 -myNewObj2.myNumber = 6 +myNewObj2.myNumber = 6; myNewObj2.getMyNumber(); // = 6 // Built-in types like strings and numbers also have constructors that create @@ -540,7 +567,7 @@ if (new Number(0)){ // you can actually add functionality to a string, for instance. String.prototype.firstCharacter = function(){ return this.charAt(0); -} +}; "abc".firstCharacter(); // = "a" // This fact is often used in "polyfilling", which is implementing newer @@ -556,7 +583,7 @@ if (Object.create === undefined){ // don't overwrite it if it exists Constructor.prototype = proto; // then use it to create a new, appropriately-prototyped object return new Constructor(); - } + }; } ``` @@ -582,13 +609,13 @@ 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. +[Javascript:Info][11] is a modern javascript tutorial covering the basics (core language and working with a browser) +as well as advanced topics with concise explanations. + + In addition to direct contributors to this article, some content is adapted from Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the Mozilla Developer Network. @@ -602,5 +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/json.html.markdown b/json.html.markdown index a612cffe..cd42d42d 100644 --- a/json.html.markdown +++ b/json.html.markdown @@ -11,6 +11,7 @@ contributors: JSON is an extremely simple data-interchange format. As [json.org](http://json.org) says, it is easy for humans to read and write and for machines to parse and generate. A piece of JSON must represent either: + * A collection of name/value pairs (`{ }`). In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. * An ordered list of values (`[ ]`). In various languages, this is realized as an array, vector, list, or sequence. an array/list/sequence (`[ ]`) or a dictionary/object/associated array (`{ }`). diff --git a/julia.html.markdown b/julia.html.markdown index 85033aa6..891a0a00 100644 --- a/julia.html.markdown +++ b/julia.html.markdown @@ -2,16 +2,17 @@ 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. -This is based on Julia 0.4. +This is based on Julia 1.0.0 -```ruby +```julia # Single line comments start with a hash (pound) symbol. #= Multiline comments can be written @@ -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}) +3 # => 3 (Int64) +3.2 # => 3.2 (Float64) +2 + 1im # => 2 + 1im (Complex{Int64}) +2 // 3 # => 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 +5 / 2 # => 2.5 # 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,40 +66,38 @@ 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' # 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 # 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!") @@ -106,29 +105,29 @@ println("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,165 +146,168 @@ 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 Int64 Array # 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 Int64 Array: [4, 5, 6] +b = [4; 5; 6] # => 3-element Int64 Array: [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] # => 2x2 Int64 Array: [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 Int8 Array: [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 and b is now [4,5] # Let's put it back -push!(b,6) # b is now [4,5,6] again. +push!(b, 6) # b is now [4,5,6] again. -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 and a is now [2,4,3,4,5,6] +pushfirst!(a, 7) # => [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 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] # 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] + # => BoundsError: attempt to access 7-element Array{Int64,1} at index [0] + a[end + 1] + # => BoundsError: attempt to access 7-element Array{Int64,1} at index [8] 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 Int64 Array: [1,2,3,4,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 is now [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) # Now a is [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] # => 1 -try: - tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64) +tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple. +tup[1] # => 1 +try + tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64) catch e println(e) end -# Many list functions also work on tuples -length(tup) # => 3 -tup[1:2] # => (1,2) -in(2, tup) # => true +# Many array functions also work on tuples +length(tup) # => 3 +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 is now 1, b is now 2 and c is now 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) # A 1-element tuple is distinct from the value it contains -(1,) == 1 # => false -(1) == 1 # => true +(1,) == 1 # => false +(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 is now 5 and e is now 4 # Dictionaries store mappings -empty_dict = Dict() # => Dict{Any,Any}() +empty_dict = Dict() # => Dict{Any,Any}() # 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} # 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{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"] # => 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]) #################################################### @@ -328,7 +330,7 @@ end # 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 @@ -346,15 +348,16 @@ end # 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 -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: @@ -363,10 +366,11 @@ end # mouse 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 @@ -376,9 +380,9 @@ end # 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") @@ -398,15 +402,15 @@ function add(x, y) x + y end -add(5, 6) # => 11 after printing out "x is 5 and y is 6" +add(5, 6) # => 11 after printing out "x is 5 and y is 6" # 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 (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(3, 4) # => (7, -1) # You can define functions that take a variable number of # positional arguments @@ -416,41 +420,41 @@ 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('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: no method defaults(Char,) + defaults() # => ERROR: no methods 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(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") # => ["k1"=>"mine","name2"=>"hello"] +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") @@ -474,7 +478,7 @@ function create_adder(x) end # 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) @@ -490,16 +494,17 @@ function create_adder(x) end add_10 = create_adder(10) -add_10(3) # => 13 +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] +# 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 +513,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 +525,77 @@ 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. +using InteractiveUtils # defines the subtype and supertype function # For example, Number is an abstract type -subtypes(Number) # => 2-element Array{Any,1}: +subtypes(Number) # => 2-element Array{Any,1}: # Complex{T<:Real} # Real -subtypes(Cat) # => 0-element Array{Any,1} +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. -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 +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 +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 +613,35 @@ 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(Panther()) # => "grrr" +meow(tigger) # => "rawwr" +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(Lion("42")) # => prints "The cat says 42" +pet_cat(Lion("42")) # => prints "The cat says 42" try - pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,) + pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,) catch e println(e) end @@ -649,130 +651,132 @@ 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()) # => prints The orange tiger wins! +fight(tigger, Lion("ROAR")) # => prints 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()) # => prints The orange tiger wins! +fight(tigger, Lion("ROAR")) # => prints 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()) # => prints The victorious cat says grrr try - fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion) -catch + fight(Panther(), Lion("RAWR")) +catch e + println(e) + # => MethodError(fight, (Panther("green"), Lion("green", "RAWR")), + # 0x000000000000557b) 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") # 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")) + # => prints The victorious cat says rarrr +catch e + println(e) + # => MethodError(fight, (Lion("green", "RAR"), Lion("brown", "rarrr")), + # 0x000000000000557c) +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(Lion("RAR"), Lion("brown", "rarrr")) # => prints 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 + # .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 + # .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 - # + # .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 + # # 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 +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 - # + # .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 - # + # .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 + # ``` ## 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 099d1529..027b6571 100644 --- a/kdb+.html.markdown +++ b/kdb+.html.markdown @@ -6,7 +6,7 @@ contributors: filename: learnkdb.q --- -The q langauge and its database component kdb+ were developed by Arthur Whitney +The q language and its database component kdb+ were developed by Arthur Whitney and released by Kx systems in 2003. q is a descendant of APL and as such is very terse and a little strange looking for anyone from a "C heritage" language background. Its expressiveness and vector oriented nature make it well suited @@ -76,7 +76,7 @@ floor 3.14159 / => 3 / ...getting the absolute value... abs -3.14159 / => 3.14159 / ...and many other things -/ see http://code.kx.com/wiki/Reference for more +/ see http://code.kx.com/q/ref/card/ for more / q has no operator precedence, everything is evaluated right to left / so results like this might take some getting used to @@ -174,7 +174,7 @@ t - 00:10:00.000 / => 00:50:00.000 d.year / => 2015i d.mm / => 12i d.dd / => 25i -/ see http://code.kx.com/wiki/JB:QforMortals2/atoms#Temporal_Data for more +/ see http://code.kx.com/q4m3/2_Basic_Data_Types_Atoms/#25-temporal-data for more / q also has an infinity value so div by zero will not throw an error 1%0 / => 0w @@ -183,7 +183,7 @@ d.dd / => 25i / And null types for representing missing values 0N / => null int 0n / => null float -/ see http://code.kx.com/wiki/JB:QforMortals2/atoms#Null_Values for more +/ see http://code.kx.com/q4m3/2_Basic_Data_Types_Atoms/#27-nulls for more / q has standard control structures / if is as you might expect (; separates the condition and instructions) @@ -301,7 +301,7 @@ l:1+til 9 / til is a useful shortcut for generating ranges -5#l / => 5 6 7 8 9 / drop the last 5 -5_l / => 1 2 3 4 -/ find the first occurance of 4 +/ find the first occurrence of 4 l?4 / => 3 l[3] / => 4 @@ -316,7 +316,7 @@ key d / => `a`b`c / and value the second value d / => 1 2 3 -/ Indexing is indentical to lists +/ Indexing is identical to lists / with the first list as a key instead of the position d[`a] / => 1 d[`b] / => 2 @@ -406,7 +406,7 @@ k!t / We can also use this shortcut for defining keyed tables kt:([id:1 2 3]c1:1 2 3;c2:4 5 6;c3:7 8 9) -/ Records can then be retreived based on this key +/ Records can then be retrieved based on this key kt[1] / => c1| 1 / => c2| 4 @@ -428,7 +428,7 @@ kt[`id!1] f:{x+x} f[2] / => 4 -/ Functions can be annonymous and called at point of definition +/ Functions can be anonymous and called at point of definition {x+x}[2] / => 4 / By default the last expression is returned @@ -440,7 +440,7 @@ f[2] / => 4 / Function arguments can be specified explicitly (separated by ;) {[arg1;arg2] arg1+arg2}[1;2] / => 3 -/ or if ommited will default to x, y and z +/ or if omitted will default to x, y and z {x+y+z}[1;2;3] / => 6 / Built in functions are no different, and can be called the same way (with []) @@ -472,7 +472,7 @@ a / => 1 / Functions cannot see nested scopes (only local and global) {local:1;{:local}[]}[] / throws error as local is not defined in inner function -/ A function can have one or more of it's arguments fixed (projection) +/ A function can have one or more of its arguments fixed (projection) f:+[4] f[4] / => 8 f[5] / => 9 @@ -483,7 +483,7 @@ f[6] / => 10 ////////// q-sql ////////// //////////////////////////////////// -/ q has it's own syntax for manipulating tables, similar to standard SQL +/ q has its own syntax for manipulating tables, similar to standard SQL / This contains the usual suspects of select, insert, update etc. / and some new functionality not typically available / q-sql has two significant differences (other than syntax) to normal SQL: @@ -642,7 +642,7 @@ kt upsert ([]name:`Thomas`Chester;age:33 58;height:175 179;sex:`f`m) / => Thomas 32 175 m / Most of the standard SQL joins are present in q-sql, plus a few new friends -/ see http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#Joins +/ see http://code.kx.com/q4m3/9_Queries_q-sql/#99-joins / the two most important (commonly used) are lj and aj / lj is basically the same as SQL LEFT JOIN @@ -669,7 +669,7 @@ aj[`time`sym;trades;quotes] / => 10:01:04 ge 150 / for each row in the trade table, the last (prevailing) quote (px) for that sym / is joined on. -/ see http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#Asof_Join +/ see http://code.kx.com/q4m3/9_Queries_q-sql/#998-as-of-joins //////////////////////////////////// ///// Extra/Advanced ////// @@ -682,21 +682,21 @@ aj[`time`sym;trades;quotes] / where possible functionality should be vectorized (i.e. operations on lists) / adverbs supplement this, modifying the behaviour of functions / and providing loop type functionality when required -/ (in q functions are sometimes refered to as verbs, hence adverbs) +/ (in q functions are sometimes referred to as verbs, hence adverbs) / the "each" adverb modifies a function to treat a list as individual variables first each (1 2 3;4 5 6;7 8 9) / => 1 4 7 / 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 @@ -716,7 +716,7 @@ first each (1 2 3;4 5 6;7 8 9) {x + y}/[1 2 3 4 5] / => 15 (only the final result) / There are other adverbs and uses, this is only intended as quick overview -/ http://code.kx.com/wiki/JB:QforMortals2/functions#Adverbs +/ http://code.kx.com/q4m3/6_Functions/#67-adverbs ////// Scripts ////// / q scripts can be loaded from a q session using the "\l" command @@ -756,21 +756,21 @@ select from splayed / (the columns are read from disk on request) / => 1 1 / => 2 2 / => 3 3 -/ see http://code.kx.com/wiki/JB:KdbplusForMortals/contents for more +/ see http://code.kx.com/q4m3/14_Introduction_to_Kdb+/ for more ////// Frameworks ////// / kdb+ is typically used for data capture and analysis. / This involves using an architecture with multiple processes / working together. kdb+ frameworks are available to streamline the setup -/ and configuration of this architecuture and add additional functionality +/ and configuration of this architecture and add additional functionality / such as disaster recovery, logging, access, load balancing etc. / https://github.com/AquaQAnalytics/TorQ ``` ## Want to know more? -* [*q for mortals* q language tutorial](http://code.kx.com/wiki/JB:QforMortals2/contents) -* [*kdb for mortals* on disk data tutorial](http://code.kx.com/wiki/JB:KdbplusForMortals/contents) -* [q language reference](http://code.kx.com/wiki/Reference) +* [*q for mortals* q language tutorial](http://code.kx.com/q4m3/) +* [*Introduction to Kdb+* on disk data tutorial](http://code.kx.com/q4m3/14_Introduction_to_Kdb+/) +* [q language reference](http://code.kx.com/q/ref/card/) * [Online training courses](http://training.aquaq.co.uk/) * [TorQ production framework](https://github.com/AquaQAnalytics/TorQ) diff --git a/ko-kr/bf-kr.html.markdown b/ko-kr/bf-kr.html.markdown index 3d366d7c..5849d2fd 100644 --- a/ko-kr/bf-kr.html.markdown +++ b/ko-kr/bf-kr.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: learnbf-kr.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] 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 ca11ee3c..0c787d7e 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -65,7 +65,7 @@ fun helloWorld(val name : String) { A template expression starts with a dollar sign ($). */ val fooTemplateString = "$fooString has ${fooString.length} characters" - println(fooTemplateString) + println(fooTemplateString) // => My String Is Here! has 18 characters /* For a variable to hold null it must be explicitly specified as nullable. diff --git a/lambda-calculus.html.markdown b/lambda-calculus.html.markdown new file mode 100644 index 00000000..72ed78ba --- /dev/null +++ b/lambda-calculus.html.markdown @@ -0,0 +1,214 @@ +--- +category: Algorithms & Data Structures +name: Lambda Calculus +contributors: + - ["Max Sun", "http://github.com/maxsun"] + - ["Yan Hui Hang", "http://github.com/yanhh0"] +--- + +# Lambda Calculus + +Lambda calculus (λ-calculus), originally created by +[Alonzo Church](https://en.wikipedia.org/wiki/Alonzo_Church), +is the world's smallest programming language. +Despite not having numbers, strings, booleans, or any non-function datatype, +lambda calculus can be used to represent any Turing Machine! + +Lambda calculus is composed of 3 elements: **variables**, **functions**, and +**applications**. + + +| Name | Syntax | Example | Explanation | +|-------------|------------------------------------|-----------|-----------------------------------------------| +| Variable | `<name>` | `x` | a variable named "x" | +| Function | `λ<parameters>.<body>` | `λx.x` | a function with parameter "x" and body "x" | +| Application | `<function><variable or function>` | `(λx.x)a` | calling the function "λx.x" with argument "a" | + +The most basic function is the identity function: `λx.x` which is equivalent to +`f(x) = x`. The first "x" is the function's argument, and the second is the +body of the function. + +## Free vs. Bound Variables: + +- In the function `λx.x`, "x" is called a bound variable because it is both in +the body of the function and a parameter. +- In `λx.y`, "y" is called a free variable because it is never declared before hand. + +## Evaluation: + +Evaluation is done via +[β-Reduction](https://en.wikipedia.org/wiki/Lambda_calculus#Beta_reduction), +which is essentially lexically-scoped substitution. + +When evaluating the +expression `(λx.x)a`, we replace all occurences of "x" in the function's body +with "a". + +- `(λx.x)a` evaluates to: `a` +- `(λx.y)a` evaluates to: `y` + +You can even create higher-order functions: + +- `(λx.(λy.x))a` evaluates to: `λy.a` + +Although lambda calculus traditionally supports only single parameter +functions, we can create multi-parameter functions using a technique called +[currying](https://en.wikipedia.org/wiki/Currying). + +- `(λx.λy.λz.xyz)` is equivalent to `f(x, y, z) = x(y(z))` + +Sometimes `λxy.<body>` is used interchangeably with: `λx.λy.<body>` + +---- + +It's important to recognize that traditional **lambda calculus doesn't have +numbers, characters, or any non-function datatype!** + +## Boolean Logic: + +There is no "True" or "False" in lambda calculus. There isn't even a 1 or 0. + +Instead: + +`T` is represented by: `λx.λy.x` + +`F` is represented by: `λx.λy.y` + +First, we can define an "if" function `λbtf` that +returns `t` if `b` is True and `f` if `b` is False + +`IF` is equivalent to: `λb.λt.λf.b t f` + +Using `IF`, we can define the basic boolean logic operators: + +`a AND b` is equivalent to: `λab.IF a b F` + +`a OR b` is equivalent to: `λab.IF a T b` + +`a NOT b` is equivalent to: `λa.IF a F T` + +*Note: `IF a b c` is essentially saying: `IF(a(b(c)))`* + +## Numbers: + +Although there are no numbers in lambda calculus, we can encode numbers using +[Church numerals](https://en.wikipedia.org/wiki/Church_encoding). + +For any number n: <code>n = λf.f<sup>n</sup></code> so: + +`0 = λf.λx.x` + +`1 = λf.λx.f x` + +`2 = λf.λx.f(f x)` + +`3 = λf.λx.f(f(f x))` + +To increment a Church numeral, +we use the successor function `S(n) = n + 1` which is: + +`S = λn.λf.λx.f((n f) x)` + +Using successor, we can define add: + +`ADD = λab.(a S)n` + +**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) +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/latex.html.markdown b/latex.html.markdown index a3866892..c9b1d8fb 100644 --- a/latex.html.markdown +++ b/latex.html.markdown @@ -255,7 +255,7 @@ There exists two main types of links: visible URL \\ % You can not add extra-spaces or special symbols into shadowing text since it % will cause mistakes during the compilation -This package also produces list of tumbnails in the output pdf document and +This package also produces list of thumbnails in the output pdf document and active links in the table of contents. \section{End} diff --git a/lfe.html.markdown b/lfe.html.markdown new file mode 100644 index 00000000..413de36e --- /dev/null +++ b/lfe.html.markdown @@ -0,0 +1,459 @@ +--- + +language: "Lisp Flavoured Erlang(LFE)" +filename: lispflavourederlang.lfe +contributors: + - ["Pratik Karki", "https://github.com/prertik"] +--- + +Lisp Flavoured Erlang(LFE) is a functional, concurrent, general-purpose programming +language and Lisp dialect(Lisp-2) built on top of Core Erlang and the Erlang Virtual Machine(BEAM). + +LFE can be obtained from [LFE](https://github.com/rvirding/lfe) + +The classic starting point is [LFE DOCS.](http://docs.lfe.io) + +Another new site is being built to replace it.[LFE DEV.](http://docs.lfe.io/dev) + + + +```lisp + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; 0. Syntax +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; General form. + +;; Lisp comprises of two syntax called: the ATOM and the S-expression. +;; `forms` are known as grouped S-expressions. + +8 ; an atom; it evaluates to itself + +:ERLANG ;Atom; evaluates to the symbol :ERLANG. + +t ; another atom which denotes true. + +(* 2 21) ; an S- expression + +'(8 :foo t) ;another one + + +;;; Comments + +;; Single line comments start with a semicolon; use two for normal +;; comments, three for section comments, and four fo file-level +;; comments. + +;; Block Comment + + #| comment text |# + +;;; Environment + +;; LFE is the de-facto standard. + +;; Libraries can be used directly from the Erlang ecosystem. Rebar3 is the build tool. + +;; LFE is usually developed with a text editor(preferably Emacs) and a REPL +;; (Read Evaluate Print Loop) running at the same time. The REPL +;; allows for interactive exploration of the program as it is "live" +;; in the system. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; 1. Literals and Special Syntactic Rules +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; Integers + +1234 -123 ; Regular decimal notation +#b0 #b10101 ; Binary notation +#0 #10101 ; Binary notation (alternative form) +#o377 #o-111 ; Octal notation +#d123456789 #d+123 ; Explicitly decimal notation +#xc0ffe 0x-01 ; Hexadecimal notation +#2r1010 #8r377 ;Notation with explicit base (up to 36) +#\a #$ #\ä #\🐭 ;Character notation (the value is the Unicode code point of the character) +#\x1f42d; ;Character notation with the value in hexadecimal + +;;; Floating point numbers +1.0 +2.0 -1.5 1.0e10 1.111e-10 + +;;; Strings + +"any text between double quotes where \" and other special characters like \n can be escaped". +; List String +"Cat: \x1f639;" ; writing unicode in string for regular font ending with semicolon. + +#"This is a binary string \n with some \"escaped\" and quoted (\x1f639;) characters" +; Binary strings are just strings but function different in the VM. +; Other ways of writing it are: #B("a"), #"a", and #B(97). + + +;;; Character escaping + +\b ; => Backspace +\t ; => Tab +\n ; => Newline +\v ; => Vertical tab +\f ; => Form Feed +\r ; => Carriage Return +\e ; => Escape +\s ; => Space +\d ; => Delete + +;;; Binaries +;; It is used to create binaries with any contents. +#B((#"a" binary) (#"b" binary)) ; #"ab" (Evaluated form) + +;;; Lists are: () or (foo bar baz) + +;;; Tuples are written in: #(value1 value2 ...). Empty tuple #() is also valid. + +;;; Maps are written as: #M(key1 value1 key2 value2 ...). Empty map #M() is also valid. + +;;; Symbols: Things that cannot be parsed. Eg: foo, Foo, foo-bar, :foo +| foo | ; explicit construction of symbol by wrapping vertical bars. + +;;; Evaluation + +;; #.(... some expression ...). E.g. '#.(+ 1 1) will evaluate the (+ 1 1) while it ;; reads the expression and then be effectively '2. + +;; List comprehension in LFE REPL + +lfe> (list-comp + ((<- x '(0 1 2 3))) + (trunc (math:pow 3 x))) + (1 3 9 27) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 2. Core forms +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; These forms are same as those found at Common Lisp and Scheme. + +(quote e) +(cons head tail) +(car e) +(cdr e) +(list e ... ) +(tuple e ... ) +(binary seg ... ) +(map key val ...), (map-get m k), (map-set m k v ...), (map-update m k v ...) + +(lambda (arg ...) ...) + (match-lambda + ((arg ... ) {{(when e ...)}} ...) ; Matches clauses + ... ) +(let ((pat {{(when e ...)}} e) + ...) + ... ) +(let-function ((name lambda|match-lambda) ; Only define local + ... ) ; functions + ... ) +(letrec-function ((name lambda|match-lambda) ; Only define local + ... ) ; functions + ... ) +(let-macro ((name lambda-match-lambda) ; Only define local + ...) ; macros + ...) +(progn ... ) +(if test true-expr {{false-expr}}) +(case e + (pat {{(when e ...)}} ...) + ... )) +(receive + (pat {{(when e ...)}} ... ) + ... + (after timeout ... )) +(catch ... ) +(try + e + {{(case ((pat {{(when e ...)}} ... ) + ... ))}} + {{(catch + ; Next must be tuple of length 3! + (((tuple type value ignore) {{(when e ...)}} + ... ) + ... )}} + {{(after ... )}}) + +(funcall func arg ... ) +(call mod func arg ... ) - Call to Erlang Mod:Func(Arg, ... ) +(define-module name declaration ... ) +(extend-module declaration ... ) - Define/extend module and declarations. +(define-function name lambda|match-lambda) +(define-macro name lambda|match-lambda) - Define functions/macros at top-level. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. Macros +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Macros are part of the language to allow you to create abstractions +;; on top of the core language and standard library that move you closer +;; toward being able to directly express the things you want to express. + +;; Top-level function + +(defun name (arg ...) ...) + +;; Adding comments in functions + +(defun name + "Toplevel function with pattern-matching arguments" + ((argpat ...) ...) + ...) + +;; Top-level macro + +(defmacro name (arg ...) ...) +(defmacro name arg ...) + +;; Top-level macro with pattern matching arguments + +(defmacro name + ((argpat ...) ...) + ...) + +;; Top-level macro using Scheme inspired syntax-rules format + +(defsyntax name + (pat exp) + ...) + +;;; Local macros in macro or syntax-rule format + +(macrolet ((name (arg ... ) ... ) + ... ) + ... ) + +(syntaxlet ((name (pat exp) ...) + ...) + ...) + +;; Like CLISP + +(prog1 ...) +(prog2 ...) + +;; Erlang LFE module + +(defmodule name ...) + +;; Erlang LFE record + +(defrecord name ...) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 4. Patterns and Guards +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Using patterns in LFE compared to that of Erlang + +;; Erlang ;; LFE +;; {ok, X} (tuple 'ok x) +;; error 'error +;; {yes, [X|Xs]} (tuple 'yes (cons x xs)) +;; <<34,F/float>> (binary 34 (f float)) +;; [P|Ps]=All (= (cons p ps) all) + + _ ; => is don't care while pattern matching + + (= pattern1 pattern2) ; => easier, better version of pattern matching + +;; Guards + +;; Whenever pattern occurs(let, case, receive, lc, etc) it can be followed by an optional +;; guard which has the form (when test ...). + +(progn gtest ...) ;; => Sequence of guard tests +(if gexpr gexpr gexpr) +(type-test e) +(guard-bif ...) ;; => Guard BIFs, arithmetic, boolean and comparison operators + +;;; REPL + +lfe>(set (tuple len status msg) #(8 ok "Trillian")) + #(8 ok "Trillian") +lfe>msg + "Trillian" + +;;; Program illustrating use of Guards + +(defun right-number? + ((x) (when (orelse (== x 42) (== x 276709))) + 'true) + ((_) 'false)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 5. Functions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; A simple function using if. + +(defun max (x y) + "The max function." + (if (>= x y) x y)) + +;; Same function using more clause + +(defun max + "The max function." + ((x y) (when (>= x y)) x) + ((x y) y)) + +;; Same function using similar style but using local functions defined by flet or fletrec + +(defun foo (x y) + "The max function." + (flet ((m (a b) "Local comment." + (if (>= a b) a b))) + (m x y))) + +;; LFE being Lisp-2 has separate namespaces for variables and functions +;; Both variables and function/macros are lexically scoped. +;; Variables are bound by lambda, match-lambda and let. +;; Functions are bound by top-level defun, flet and fletrec. +;; Macros are bound by top-level defmacro/defsyntax and by macrolet/syntaxlet. + +;; (funcall func arg ...) like CL to call lambdas/match-lambdas +;; (funs) bound to variables are used. + +;; separate bindings and special for apply. +apply _F (...), +apply _F/3 ( a1, a2, a3 ) + +;; Cons'ing in function heads +(defun sum (l) (sum l 0)) + (defun sum + (('() total) total) + (((cons h t) total) (sum t (+ h total)))) + +;; ``cons`` literal instead of constructor form + (defun sum (l) (sum l 0)) + (defun sum + (('() total) total) + ((`(,h . ,t) total) (sum t (+ h total)))) + +;; Matching records in function heads + +(defun handle_info + (('ping (= (match-state remote-pid 'undefined) state)) + (gen_server:cast (self) 'ping) + `#(noreply ,state)) + (('ping state) + `#(noreply ,state))) + +;; Receiving Messages + (defun universal-server () + (receive + ((tuple 'become func) + (funcall func)))) + +;; another way for receiving messages + + (defun universal-server () + (receive + (`#(become ,func) + (funcall func)))) + +;; Composing a complete function for specific tasks + +(defun compose (f g) + (lambda (x) + (funcall f + (funcall g x)))) + +(defun check () + (let* ((sin-asin (compose #'sin/1 #'asin/1)) + (expected (sin (asin 0.5))) + (compose-result (funcall sin-asin 0.5))) + (io:format "Expected answer: ~p~n" (list expected)) + (io:format "Answer with compose: ~p~n" (list compose-result)))) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 6. Concurrency +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Message passing as done by Erlang's light-weight "processes". + +(defmodule messenger-back + (export (print-result 0) (send-message 2))) + +(defun print-result () + (receive + ((tuple pid msg) + (io:format "Received message: '~s'~n" (list msg)) + (io:format "Sending message to process ~p ...~n" (list pid)) + (! pid (tuple msg)) + (print-result)))) + +(defun send-message (calling-pid msg) + (let ((spawned-pid (spawn 'messenger-back 'print-result ()))) + (! spawned-pid (tuple calling-pid msg)))) + +;; Multiple simultaneous HTTP Requests: + +(defun parse-args (flag) + "Given one or more command-line arguments, extract the passed values. + + For example, if the following was passed via the command line: + + $ erl -my-flag my-value-1 -my-flag my-value-2 + + One could then extract it in an LFE program by calling this function: + + (let ((args (parse-args 'my-flag))) + ... + ) + In this example, the value assigned to the arg variable would be a list + containing the values my-value-1 and my-value-2." + (let ((`#(ok ,data) (init:get_argument flag))) + (lists:merge data))) + +(defun get-pages () + "With no argument, assume 'url parameter was passed via command line." + (let ((urls (parse-args 'url))) + (get-pages urls))) + +(defun get-pages (urls) + "Start inets and make (potentially many) HTTP requests." + (inets:start) + (plists:map + (lambda (x) + (get-page x)) urls)) + +(defun get-page (url) + "Make a single HTTP request." + (let* ((method 'get) + (headers '()) + (request-data `#(,url ,headers)) + (http-options ()) + (request-options '(#(sync false)))) + (httpc:request method request-data http-options request-options) + (receive + (`#(http #(,request-id #(error ,reason))) + (io:format "Error: ~p~n" `(,reason))) + (`#(http #(,request-id ,result)) + (io:format "Result: ~p~n" `(,result)))))) + + +;; Check out Erlang's documentation for more concurrency and OTP docs. +``` + +## Further Reading + +* [LFE DOCS](http://docs.lfe.io) +* [LFE GitBook](https://lfe.gitbooks.io/reference-guide/index.html) +* [LFE Wiki](https://en.wikipedia.org/wiki/LFE_(programming_language)) + +## Extra Info +* [LFE PDF](http://www.erlang-factory.com/upload/presentations/61/Robertvirding-LispFlavouredErlang.pdf) +* [LFE mail](https://groups.google.com/d/msg/lisp-flavoured-erlang/XA5HeLbQQDk/TUHabZCHXB0J) + +## Credits + +Lots of thanks to Robert Virding for creating LFE, Duncan McGreggor for documenting it and other LFE contributors who made LFE awesome. + diff --git a/lt-lt/tmux-lt.html.markdown b/lt-lt/tmux-lt.html.markdown index b5b3de05..f05f55e2 100644 --- a/lt-lt/tmux-lt.html.markdown +++ b/lt-lt/tmux-lt.html.markdown @@ -1,12 +1,13 @@ --- category: tool tool: tmux -filename: tmux.json -lang: lt-lt +filename: tmux-lt.json contributors: - ["mdln", "https://github.com/mdln"] translators: - ["Zygimantus", "https://github.com/zygimantus"] +lang: lt-lt + --- 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/make.html.markdown b/make.html.markdown index ab128475..45d020e9 100644 --- a/make.html.markdown +++ b/make.html.markdown @@ -2,6 +2,7 @@ language: make
contributors:
- ["Robert Steed", "https://github.com/robochat"]
+ - ["Stephan Fuhrmann", "https://github.com/sfuhrm"]
filename: Makefile
---
@@ -11,7 +12,7 @@ target to the most recent version of the source. Famously written over a weekend by Stuart Feldman in 1976, it is still widely used (particularly
on Unix and Linux) despite many competitors and criticisms.
-There are many varieties of make in existence, however this article
+There are many varieties of make in existence, however this article
assumes that we are using GNU make which is the standard on Linux.
```make
@@ -168,9 +169,9 @@ echo: name2 = Sara # True within the matching rule # Some variables defined automatically by make.
echo_inbuilt:
echo $(CC)
- echo ${CXX)}
+ echo ${CXX}
echo $(FC)
- echo ${CFLAGS)}
+ echo ${CFLAGS}
echo $(CPPFLAGS)
echo ${CXXFLAGS}
echo $(LDFLAGS)
diff --git a/markdown.html.markdown b/markdown.html.markdown index a1f5173b..cf4286e2 100644 --- a/markdown.html.markdown +++ b/markdown.html.markdown @@ -29,7 +29,7 @@ specific to a certain parser. ## HTML Elements Markdown is a superset of HTML, so any HTML file is valid Markdown. -```markdown +```md <!--This means we can use HTML elements in Markdown, such as the comment element, and they won't be affected by a markdown parser. However, if you create an HTML element in your markdown file, you cannot use markdown syntax @@ -41,7 +41,7 @@ within that element's contents.--> You can create HTML elements `<h1>` through `<h6>` easily by prepending the text you want to be in that element by a number of hashes (#). -```markdown +```md # This is an <h1> ## This is an <h2> ### This is an <h3> @@ -51,7 +51,7 @@ text you want to be in that element by a number of hashes (#). ``` Markdown also provides us with two alternative ways of indicating h1 and h2. -```markdown +```md This is an h1 ============= @@ -63,7 +63,7 @@ This is an h2 Text can be easily styled as italic or bold using markdown. -```markdown +```md *This text is in italics.* _And so is this text._ @@ -78,7 +78,7 @@ __And so is this text.__ In GitHub Flavored Markdown, which is used to render markdown files on GitHub, we also have strikethrough: -```markdown +```md ~~This text is rendered with strikethrough.~~ ``` ## Paragraphs @@ -86,7 +86,7 @@ GitHub, we also have strikethrough: Paragraphs are a one or multiple adjacent lines of text separated by one or multiple blank lines. -```markdown +```md This is a paragraph. I'm typing in a paragraph isn't this fun? Now I'm in paragraph 2. @@ -99,7 +99,7 @@ I'm in paragraph three! Should you ever want to insert an HTML `<br />` tag, you can end a paragraph with two or more spaces and then begin a new paragraph. -```markdown +```md I end with two spaces (highlight me to see them). There's a <br /> above me! @@ -107,7 +107,7 @@ There's a <br /> above me! Block quotes are easy and done with the > character. -```markdown +```md > This is a block quote. You can either > manually wrap your lines and put a `>` before every line or you can let your lines get really long and wrap on their own. > It doesn't make a difference so long as they start with a `>`. @@ -121,7 +121,7 @@ Block quotes are easy and done with the > character. ## Lists Unordered lists can be made using asterisks, pluses, or hyphens. -```markdown +```md * Item * Item * Another item @@ -141,7 +141,7 @@ or Ordered lists are done with a number followed by a period. -```markdown +```md 1. Item one 2. Item two 3. Item three @@ -150,7 +150,7 @@ Ordered lists are done with a number followed by a period. You don't even have to label the items correctly and Markdown will still render the numbers in order, but this may not be a good idea. -```markdown +```md 1. Item one 1. Item two 1. Item three @@ -159,7 +159,7 @@ render the numbers in order, but this may not be a good idea. You can also use sublists -```markdown +```md 1. Item one 2. Item two 3. Item three @@ -170,7 +170,7 @@ You can also use sublists There are even task lists. This creates HTML checkboxes. -```markdown +```md Boxes below without the 'x' are unchecked HTML checkboxes. - [ ] First task to complete. - [ ] Second task that needs done @@ -183,7 +183,7 @@ This checkbox below will be a checked HTML checkbox. You can indicate a code block (which uses the `<code>` element) by indenting a line with four spaces or a tab. -```markdown +```md This is code So is this ``` @@ -191,15 +191,15 @@ a line with four spaces or a tab. You can also re-tab (or add an additional four spaces) for indentation inside your code -```markdown +```md my_array.each do |item| puts item end ``` -Inline code can be created using the backtick character ` +Inline code can be created using the backtick character `` ` `` -```markdown +```md John didn't even know what the `go_to()` function did! ``` @@ -220,7 +220,7 @@ highlighting of the language you specify after the \`\`\` Horizontal rules (`<hr/>`) are easily added with three or more asterisks or hyphens, with or without spaces. -```markdown +```md *** --- - - - @@ -232,17 +232,17 @@ hyphens, with or without spaces. One of the best things about markdown is how easy it is to make links. Put the text to display in hard brackets [] followed by the url in parentheses () -```markdown +```md [Click me!](http://test.com/) ``` You can also add a link title using quotes inside the parentheses. -```markdown +```md [Click me!](http://test.com/ "Link to Test.com") ``` Relative paths work too. -```markdown +```md [Go to music](/music/). ``` @@ -269,7 +269,7 @@ But it's not that commonly used. ## Images Images are done the same way as links but with an exclamation point in front! -```markdown +```md ![This is the alt-attribute for my image](http://imgur.com/myimage.jpg "An optional title") ``` @@ -281,20 +281,20 @@ And reference style works as expected. ## Miscellany ### Auto-links -```markdown +```md <http://testwebsite.com/> is equivalent to [http://testwebsite.com/](http://testwebsite.com/) ``` ### Auto-links for emails -```markdown +```md <foo@bar.com> ``` ### Escaping characters -```markdown +```md I want to type *this text surrounded by asterisks* but I don't want it to be in italics, so I do this: \*this text surrounded by asterisks\*. ``` @@ -304,7 +304,7 @@ in italics, so I do this: \*this text surrounded by asterisks\*. In GitHub Flavored Markdown, you can use a `<kbd>` tag to represent keyboard keys. -```markdown +```md Your computer crashed? Try sending a <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd> ``` @@ -313,7 +313,7 @@ Your computer crashed? Try sending a Tables are only available in GitHub Flavored Markdown and are slightly cumbersome, but if you really want it: -```markdown +```md | Col1 | Col2 | Col3 | | :----------- | :------: | ------------: | | Left-aligned | Centered | Right-aligned | @@ -321,7 +321,7 @@ cumbersome, but if you really want it: ``` or, for the same results -```markdown +```md Col 1 | Col2 | Col3 :-- | :-: | --: Ugh this is so ugly | make it | stop diff --git a/matlab.html.markdown b/matlab.html.markdown index 86b116c6..b88b1c03 100644 --- a/matlab.html.markdown +++ b/matlab.html.markdown @@ -353,7 +353,7 @@ double_input(6) % ans = 12 % anonymous function. Useful when quickly defining a function to pass to % another function (eg. plot with fplot, evaluate an indefinite integral % with quad, find roots with fzero, or find minimum with fminsearch). -% Example that returns the square of it's input, assigned to the handle sqr: +% Example that returns the square of its input, assigned to the handle sqr: sqr = @(x) x.^2; sqr(10) % ans = 100 doc function_handle % find out more @@ -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/nim.html.markdown b/nim.html.markdown index 5d00304d..1e17d8f0 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -12,6 +12,19 @@ that gives the programmer power without compromises on runtime efficiency. Nim is efficient, expressive, and elegant. ```nim +# Single-line comments start with a # + +#[ + This is a multiline comment. + In Nim, multiline comments can be nested, beginning with #[ + ... and ending with ]# +]# + +discard """ +This can also work as a multiline comment. +Or for unparsable, broken code +""" + var # Declare (and assign) variables, letter: char = 'n' # with or without type annotations lang = "N" & "im" @@ -35,10 +48,6 @@ when compileBadCode: # `when` is a compile time `if` discard 1 > 2 # Note: The compiler will complain if the result of an expression # is unused. `discard` bypasses this. -discard """ -This can work as a multiline comment. -Or for unparsable, broken code -""" # # Data Structures @@ -76,7 +85,7 @@ let myDrink = drinks[2] # static typing powerful and useful. type - Name = string # A type alias gives you a new type that is interchangable + Name = string # A type alias gives you a new type that is interchangeable Age = int # with the old type but is more descriptive. Person = tuple[name: Name, age: Age] # Define data structures too. AnotherSyntax = tuple @@ -109,7 +118,7 @@ when compileBadCode: type Color = enum cRed, cBlue, cGreen - Direction = enum # Alternative formating + Direction = enum # Alternative formatting dNorth dWest dEast diff --git a/nix.html.markdown b/nix.html.markdown index ef59a135..d078395a 100644 --- a/nix.html.markdown +++ b/nix.html.markdown @@ -3,6 +3,7 @@ language: nix filename: learn.nix contributors: - ["Chris Martin", "http://chris-martin.org/"] + - ["Rommel Martinez", "https://ebzzry.io"] --- Nix is a simple functional language developed for the @@ -208,6 +209,12 @@ with builtins; [ { a = 1; b = 2; }.a #=> 1 + # The ? operator tests whether a key is present in a set. + ({ a = 1; b = 2; } ? a) + #=> true + ({ a = 1; b = 2; } ? c) + #=> false + # The // operator merges two sets. ({ a = 1; } // { b = 2; }) #=> { a = 1; b = 2; } @@ -350,5 +357,5 @@ with builtins; [ * [James Fisher - Nix by example - Part 1: The Nix expression language] (https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55) -* [Susan Potter - Nix Cookbook - Nix By Example] - (http://funops.co/nix-cookbook/nix-by-example/) +* [Rommel Martinez - A Gentle Introduction to the Nix Family] + (https://ebzzry.io/en/nix/#nix) diff --git a/nl-nl/bash-nl.html.markdown b/nl-nl/bash-nl.html.markdown index da47e2a9..af4a8cc8 100644 --- a/nl-nl/bash-nl.html.markdown +++ b/nl-nl/bash-nl.html.markdown @@ -17,8 +17,8 @@ lang: nl-nl filename: LearnBash-nl.sh --- -Bash is de naam van den unix shell, deze wordt gebruikt voor het GNU operating system en is de standaard shell op Linux en Mac OS X. -Bijna alle voorbeelden hier onder kunnen deel uitmaken van een shell script of kunnen uitgevoerd worden in de shell. +Bash is de naam van de unix shell, deze wordt gebruikt voor het GNU operating system en is de standaard shell op Linux en Mac OS X. +Bijna alle voorbeelden hieronder kunnen deel uitmaken van een shell script of kunnen uitgevoerd worden in de shell. [Lees er meer over hier.](http://www.gnu.org/software/bash/manual/bashref.html) @@ -28,23 +28,23 @@ Bijna alle voorbeelden hier onder kunnen deel uitmaken van een shell script of k # het script uitgevoerd moet worden: http://en.wikipedia.org/wiki/Shebang_(Unix) # Zoals je kan zien wordt # gebruikt om een commentaar lijn te starten. -# Simpel hello world voorbeeld: +# Een simpel hello world voorbeeld: echo Hello world! -# Elke command start op een nieuwe lijn, of achter een puntkomma (;): +# Elk commando start op een nieuwe lijn, of achter een puntkomma (;): echo 'Dit is de eerste lijn'; echo 'Dit is de tweede lijn' -# Een varialbe declareren gebeurt op volgende manier: +# Een variabele declareren gebeurt op volgende manier: Variabele="Een string" # Maar niet op deze manier: Variabele = "Een string" -# Bash ziet variable als een commando en zal een error geven omdat dit commando +# Bash ziet variabelen als een commando en zal een error geven omdat dit commando # niet bestaat. # Of op deze manier: Variabele= 'Een string' -# Bash zal 'Een string' zien als een commanda en een error geven omdat het niet +# Bash zal 'Een string' zien als een commando en een error geven omdat het niet # gevonden kan worden. # Variabelen gebruiken: @@ -83,7 +83,7 @@ echo "Wat is uw naam?" read Naam # Merk op dat we geen variabele gedeclareerd hebben echo Hallo, $Naam! -# We hebben ook if structuren +# We hebben ook logische if structuren # Gebruik 'man test' voor meer informatie over condities. if [ $Naam -ne $USER ] then diff --git a/nl-nl/bf.html.markdown b/nl-nl/bf.html.markdown index 016e2ba2..8cb06471 100644 --- a/nl-nl/bf.html.markdown +++ b/nl-nl/bf.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: learnbf-nl.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/nl-nl/html-nl.html.markdown b/nl-nl/html-nl.html.markdown new file mode 100644 index 00000000..54c81ed6 --- /dev/null +++ b/nl-nl/html-nl.html.markdown @@ -0,0 +1,98 @@ +--- +language: html +filename: learnhtml-nl.html +contributors: + - ["Christophe THOMAS", "https://github.com/WinChris"] +translators: + - ["Robert Steed", "https://github.com/robochat"] + - ["Jeroen Deviaene", "https://github.com/jerodev"] +lang: nl-nl +--- + +HTML staat voor HyperText Markup Language. +Het is een taal die je toe staat pagina's te maken op het World Wide Web. +Het is een opmaak taal, dit staat de gebruiker toe om webpagina's te maken in code en zo aan te duiden hoe de pagina moet weergegeven worden. +Eigenlijk zijn html files zelfs simpele tekst bestanden. +Wat is deze opmaak? Het is een manier om de structuur en data op de pagina weer te geven met speciale html tags. +Deze tags dienen om de betekenis te geven aan de tekst die het bevat. +Zoals vele computer talen heeft html vele verschillende versies. Hier zullen we HTML5 bespreken. + +**Merk op:** Je kan de verschillende tags en elementen testen terwijl je door de tutorial gaat met een website zoals [codepen](http://codepen.io/pen/), zo kan je de effecten hier van live zien. +Dit artikel gaat vooral over de HTML syntax en enkele handige tips + + +```html +<!-- Commentaren worden toegevoegd zoals deze lijn --> + +<!-- #################### De Tags #################### --> + +<!-- Hier is een voorbeeld HTML bestand dat we zullen analyseren. --> + +<!doctype html> + <html> + <head> + <title>Mijn Website</title> + </head> + <body> + <h1>Hello, world!</h1> + <a href="http://codepen.io/anon/pen/xwjLbZ">Neem een kijkje op deze link</a> + <p>Dit is een paragraaf.</p> + <p>Dit is nog een paragraaf.</p> + <ul> + <li>Dit is een item in een niet genummerde lijst</li> + <li>Dit is nog zo een item</li> + <li>En dit is het laatste item van de lijst</li> + </ul> + </body> + </html> + +<!-- Een HTML bestand start altijd met een tag die aan de browser laat weten dat we HTML gebruiken --> +<!doctype html> + +<!-- Daarna openen we de root van het bestand met de <html> tag --> +<html> + +<!-- Deze tag moet ook gesloten worden op het einde van het bestand --> +</html> + +<!-- Niets mag nog na deze tag komen! --> + +<!-- Binnenin (tussen de html tags <html></html>) vinden we: --> + +<!-- Een header, gedefigneerd door <head> (Deze moet gesloten worden met </head>) --> +<!-- De header bevat beschrijvingen en externe data die niet zichtbaar is op de website; Dit is metadata --> + +<head> + <title>Mijn Website</title><!-- De <title> tag geeft de tekst aan die in de titelbar van de browser moet weergegeven worden. --> +</head> + +<!-- Achter de <head> sectie vinden we bijna altijd <body> --> +<!-- Tot op dit punt is nog niets verschenen in het browser venster. --> +<!-- In de body plaatsen we wat zichtbaar moet zijn in de browser --> + +<body> + <h1>Hello, world!</h1> <!-- De h1 tag maakt een titel. --> + <!-- Er zijn ook sub titels voor <h1> van belangrijk <h2> tot minder belangrijk <h6>. --> + <a href="http://codepen.io/anon/pen/xwjLbZ">Neem een kijkje op deze link</a> <!-- een hyperlink naar de aangegeven link waar op geklikt kan worden in de browser --> + <p>This is a paragraph.</p> <!-- De tag <p> laat ons tekst toevoegen. --> + <p>This is another paragraph.</p> + <ul> <!-- De tag <ul> maakt een lijst met puntjes. --> + <!-- Om een genummerde lijst te hebben gebruik je <ol>, hiermee worden de elementen <li> automatisch genummerd --> + <li>This is an item in a non-enumerated list (bullet list)</li> + <li>This is another item</li> + <li>And this is the last item on the list</li> + </ul> +</body> + +<!-- En dat is het! Zo gemakkelijk is het om een html bestand te maken. --> +``` + +## Gebruik + +HTML wordt altijd opgeslagen in bestanden die eindigen in `.html`. + +## Meer weten + +* [wikipedia](https://nl.wikipedia.org/wiki/HTML) +* [HTML tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML) +* [W3School](http://www.w3schools.com/html/html_intro.asp) 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 2b599378..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. @@ -786,7 +789,7 @@ MyClass *newVar = [classVar retain]; // If classVar is released, object is still // Automatic Reference Counting (ARC) // Because memory management can be a pain, Xcode 4.2 and iOS 4 introduced Automatic Reference Counting (ARC). // ARC is a compiler feature that inserts retain, release, and autorelease automatically for you, so when using ARC, -// you must not use retain, relase, or autorelease +// you must not use retain, release, or autorelease MyClass *arcMyClass = [[MyClass alloc] init]; // ... code using arcMyClass // Without ARC, you will need to call: [arcMyClass release] after you're done using arcMyClass. But with ARC, diff --git a/ocaml.html.markdown b/ocaml.html.markdown index 59ead9ec..74eb7993 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -1,5 +1,6 @@ --- language: OCaml +filename: learnocaml.ml contributors: - ["Daniil Baturin", "http://baturin.org/"] --- @@ -374,4 +375,3 @@ sum_int_list t ;; * Visit the official website to get the compiler and read the docs: <http://ocaml.org/> * Try interactive tutorials and a web-based interpreter by OCaml Pro: <http://try.ocamlpro.com/> -* Read "OCaml for the skeptical" course: <http://www2.lib.uchicago.edu/keith/ocaml-class/home.html> diff --git a/pascal.html.markdown b/pascal.html.markdown new file mode 100644 index 00000000..6877afef --- /dev/null +++ b/pascal.html.markdown @@ -0,0 +1,96 @@ +--- +language: Pascal +filename: learnpascal.pas +contributors: + - ["Ganesha Danu", "http://github.com/blinfoldking"] +--- + + +>Pascal is an imperative and procedural programming language, which Niklaus Wirth designed in 1968–69 and published in 1970, as a small, efficient language intended to encourage good programming practices using structured programming and data structuring. It is named in honor of the French mathematician, philosopher and physicist Blaise Pascal. +source : [wikipedia](https://en.wikipedia.org/wiki/Pascal_(programming_language)) + + + +to compile and run a pascal program you could use a free pascal compiler. [Download Here](https://www.freepascal.org/) + +```pascal +//Anathomy of a Pascal Program +//this is a comment +{ + this is a + multiline comment +} + +//name of the program +program learn_pascal; //<-- dont forget a semicolon + +type + { + this is where you should delcare a custom + data-types + } +var + { + this is where you should declare a variable + } + +//main program area +begin + { + area to declare your instruction + } +end. // End of a main program area should required a "." symbol +``` + +```pascal +//declaring variable +//you can do this +var a:integer; +var b:integer; +//or this +var + a : integer; + b : integer; +//or this +var a,b : integer; +``` +```pascal +program Learn_More; +//Lets learn about data types and their operations + +//Declaring variables +var + int : integer; // a variable that contains an integer number data types + ch : char; // a variable that contains a character data types + str : string; // a variable that contains a string data types + r : real; // a variable that contains a real number data types + bool : boolean; //a variables that contains a Boolean(True/False) value data types +Begin + int := 1;// how to assign a value to a variable + r := 3.14; + ch := 'a'; + str := 'apple'; + bool := true; + //pascal is not a case-sensitive language + //arithmethic operation + int := 1 + 1; // int = 2 overwriting the previous assignment + int := int + 1; // int = 2 + 1 = 3; + int := 4 div 2; //int = 2 a division operation which the result will be floored + int := 3 div 2; //int = 1 + int := 1 div 2; //int = 0 + + bool := true or false; // bool = true + bool := false and true; // bool = false + bool := true xor true; // bool = false + + r := 3 / 2; // a division operator for real + r := int; // you can assign an integer to a real variable but not the otherwise + + c := str[1]; // assign the first letter of str to c + str := 'hello' + 'world'; //combining strings +End. +``` + +```pascal + +```
\ No newline at end of file diff --git a/perl6.html.markdown b/perl6.html.markdown index d0ccdc9a..04f9c6e3 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -13,8 +13,8 @@ least the next hundred years. The primary Perl 6 compiler is called [Rakudo](http://rakudo.org), which runs on the JVM and [the MoarVM](http://moarvm.com). -Meta-note : the triple pound signs are here to denote headlines, -double paragraphs, and single notes. +Meta-note : double pound signs (##) are used to indicate paragraphs, while +single pound signs (#) indicate notes. `#=>` represents the output of a command. @@ -30,9 +30,9 @@ double paragraphs, and single notes. ## Variables ```perl6 -# In Perl 6, you declare a lexical variable using `my` +## In Perl 6, you declare a lexical variable using `my` my $variable; -# Perl 6 has 4 kinds of variables: +## Perl 6 has 3 basic types of variables: scalars, arrays, and hashes. ``` ### Scalars @@ -44,9 +44,9 @@ my $str = 'String'; # double quotes allow for interpolation (which we'll see later): my $str2 = "String"; -# Variable names can contain but not end with simple quotes and dashes, -# and can contain (and end with) underscores : -# my $weird'variable-name_ = 5; # works ! +## Variable names can contain but not end with simple quotes and dashes, +## and can contain (and end with) underscores : +my $weird'variable-name_ = 5; # works ! my $bool = True; # `True` and `False` are Perl 6's boolean values. my $inverse = !$bool; # You can invert a bool with the prefix `!` operator @@ -54,15 +54,16 @@ my $forced-bool = so $str; # And you can use the prefix `so` operator # which turns its operand into a Bool ``` -### Lists +### Arrays and Lists ```perl6 -# Lists represent multiple values. Their name start with `@`. +## Arrays represent multiple values. Their name start with `@`. +## Lists are similar but are an immutable type. my @array = 'a', 'b', 'c'; # equivalent to : my @letters = <a b c>; # array of words, delimited by space. - # Similar to perl5's qw, or Ruby's %w. + # Similar to perl5's qw, or Ruby's %w. my @array = 1, 2, 3; say @array[2]; # Array indices start at 0 -- This is the third element @@ -81,24 +82,25 @@ say @array; #=> a 6 b ### Hashes, or key-value Pairs. ```perl6 -# Hashes are pairs of keys and values. -# You can construct a Pair object using the syntax `Key => Value`. -# Hash tables are very fast for lookup, and are stored unordered. -# Keep in mind that keys get "flattened" in hash context, and any duplicated -# keys are deduplicated. +## Hashes are pairs of keys and values. +## You can construct a Pair object using the syntax `Key => Value`. +## Hash tables are very fast for lookup, and are stored unordered. +## Keep in mind that keys get "flattened" in hash context, and any duplicated +## keys are deduplicated. my %hash = 1 => 2, 3 => 4; my %hash = foo => "bar", # keys get auto-quoted "some other" => "value", # trailing commas are okay ; -# Even though hashes are internally stored differently than arrays, -# Perl 6 allows you to easily create a hash from an even numbered array: + +## Even though hashes are internally stored differently than arrays, +## Perl 6 allows you to easily create a hash from an even numbered array: my %hash = <key1 value1 key2 value2>; my %hash = key1 => 'value1', key2 => 'value2'; # same result as above -# You can also use the "colon pair" syntax: -# (especially handy for named parameters that you'll see later) +## You can also use the "colon pair" syntax: +## (especially handy for named parameters that you'll see later) my %hash = :w(1), # equivalent to `w => 1` # this is useful for the `True` shortcut: :truey, # equivalent to `:truey(True)`, or `truey => True` @@ -114,18 +116,18 @@ say %hash<key2>; # If it's a string, you can actually use <> ## Subs ```perl6 -# subroutines or functions as most other languages call them are -# created with the `sub` keyword. +## Subroutines, or functions as most other languages call them, are +## created with the `sub` keyword. sub say-hello { say "Hello, world" } -# You can provide (typed) arguments. -# If specified, the type will be checked at compile-time if possible, -# otherwise at runtime. +## You can provide (typed) arguments. +## If specified, the type will be checked at compile-time if possible, +## otherwise at runtime. sub say-hello-to(Str $name) { say "Hello, $name !"; } -# A sub returns the last value of the block. +## A sub returns the last value of the block. sub return-value { 5; } @@ -134,7 +136,7 @@ sub return-empty { } say return-empty; # prints Nil -# Some control flow structures produce a value, like if: +## Some control flow structures produce a value, like if: sub return-if { if True { "Truthy"; @@ -142,13 +144,12 @@ sub return-if { } say return-if; # prints Truthy -# Some don't, like for: +## Some don't, like for: sub return-for { for 1, 2, 3 { } } say return-for; # prints Nil - ## A sub can have optional arguments: sub with-optional($arg?) { # the "?" marks the argument optional say "I might return `(Any)` (Perl's 'null'-like value) if I don't have @@ -169,20 +170,20 @@ hello-to('You'); #=> Hello, You ! ## You can also, by using a syntax akin to the one of hashes ## (yay unified syntax !), pass *named* arguments to a `sub`. -# They're optional, and will default to "Any". +## They're optional, and will default to "Any". sub with-named($normal-arg, :$named) { say $normal-arg + $named; } with-named(1, named => 6); #=> 7 -# There's one gotcha to be aware of, here: -# If you quote your key, Perl 6 won't be able to see it at compile time, -# and you'll have a single Pair object as a positional parameter, -# which means this fails: +## There's one gotcha to be aware of, here: +## If you quote your key, Perl 6 won't be able to see it at compile time, +## and you'll have a single Pair object as a positional parameter, +## which means this fails: with-named(1, 'named' => 6); with-named(2, :named(5)); #=> 7 -# To make a named argument mandatory, you can use `?`'s inverse, `!` +## To make a named argument mandatory, you can use `?`'s inverse, `!` sub with-mandatory-named(:$str!) { say "$str !"; } @@ -194,7 +195,7 @@ with-mandatory-named(3);# run time error:"Too many positional parameters passed" sub takes-a-bool($name, :$bool) { say "$name takes $bool"; } -# ... you can use the same "short boolean" hash syntax: +## ... you can use the same "short boolean" hash syntax: takes-a-bool('config', :bool); # config takes True takes-a-bool('config', :!bool); # config takes False @@ -205,15 +206,15 @@ sub named-def(:$def = 5) { named-def; #=> 5 named-def(def => 15); #=> 15 -# Since you can omit parenthesis to call a function with no arguments, -# you need "&" in the name to store `say-hello` in a variable. +## Since you can omit parenthesis to call a function with no arguments, +## you need "&" in the name to store `say-hello` in a variable. my &s = &say-hello; my &other-s = sub { say "Anonymous function !" } -# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" +## A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" sub as-many($head, *@rest) { #`*@` (slurpy) will "take everything else" -# Note: you can have parameters *before* a slurpy one (like here), -# but not *after*. +## Note: you can have parameters *before* a slurpy one (like here), +## but not *after*. say @rest.join(' / ') ~ " !"; } say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! @@ -221,8 +222,8 @@ say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! # consume the parameter before. ## You can call a function with an array using the -# "argument list flattening" operator `|` -# (it's not actually the only role of this operator, but it's one of them) +## "argument list flattening" operator `|` +## (it's not actually the only role of this operator, but it's one of them) sub concat3($a, $b, $c) { say "$a, $b, $c"; } @@ -233,12 +234,12 @@ concat3(|@array); #=> a, b, c ## Containers ```perl6 -# In Perl 6, values are actually stored in "containers". -# The assignment operator asks the container on the left to store the value on -# its right. When passed around, containers are marked as immutable. -# Which means that, in a function, you'll get an error if you try to -# mutate one of your arguments. -# If you really need to, you can ask for a mutable container using `is rw`: +## In Perl 6, values are actually stored in "containers". +## The assignment operator asks the container on the left to store the value on +## its right. When passed around, containers are marked as immutable. +## Which means that, in a function, you'll get an error if you try to +## mutate one of your arguments. +## If you really need to, you can ask for a mutable container using `is rw`: sub mutate($n is rw) { $n++; say "\$n is now $n !"; @@ -247,15 +248,15 @@ sub mutate($n is rw) { my $m = 42; mutate $m; # $n is now 43 ! -# This works because we are passing the container $m to mutate. If we try -# to just pass a number instead of passing a variable it won't work because -# there is no container being passed and integers are immutable by themselves: +## This works because we are passing the container $m to mutate. If we try +## to just pass a number instead of passing a variable it won't work because +## there is no container being passed and integers are immutable by themselves: mutate 42; # Parameter '$n' expected a writable container, but got Int value -# If what you want a copy instead, use `is copy`. +## If what you want a copy instead, use `is copy`. -# A sub itself returns a container, which means it can be marked as rw: +## A sub itself returns a container, which means it can be marked as rw: my $x = 42; sub x-store() is rw { $x } x-store() = 52; # in this case, the parentheses are mandatory @@ -267,12 +268,12 @@ say $x; #=> 52 ### Conditionals ```perl6 -# - `if` -# Before talking about `if`, we need to know which values are "Truthy" -# (represent True), and which are "Falsey" (or "Falsy") -- represent False. -# Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or `Int`) -# and of course False itself. -# Every other value is Truthy. +## - `if` +## Before talking about `if`, we need to know which values are "Truthy" +## (represent True), and which are "Falsey" (or "Falsy") -- represent False. +## Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or +## `Int`) and of course False itself. +## Every other value is Truthy. if True { say "It's true !"; } @@ -281,17 +282,17 @@ unless False { say "It's not false !"; } -# As you can see, you don't need parentheses around conditions. -# However, you do need the brackets around the "body" block: +## As you can see, you don't need parentheses around conditions. +## However, you do need the brackets around the "body" block: # if (true) say; # This doesn't work ! -# You can also use their postfix versions, with the keyword after: +## You can also use their postfix versions, with the keyword after: say "Quite truthy" if True; -# - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) -# returns $value-if-true if the condition is true and $value-if-false -# if it is false. -# my $result = $value condition ?? $value-if-true !! $value-if-false; +## - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) +## returns $value-if-true if the condition is true and $value-if-false +## if it is false. +## my $result = $value condition ?? $value-if-true !! $value-if-false; my $age = 30; say $age > 18 ?? "You are an adult" !! "You are under 18"; @@ -300,18 +301,18 @@ say $age > 18 ?? "You are an adult" !! "You are under 18"; ### given/when, or switch ```perl6 -# - `given`-`when` looks like other languages' `switch`, but is much more -# powerful thanks to smart matching and Perl 6's "topic variable", $_. -# -# This variable contains the default argument of a block, -# a loop's current iteration (unless explicitly named), etc. -# -# `given` simply puts its argument into `$_` (like a block would do), -# and `when` compares it using the "smart matching" (`~~`) operator. -# -# Since other Perl 6 constructs use this variable (as said before, like `for`, -# blocks, etc), this means the powerful `when` is not only applicable along with -# a `given`, but instead anywhere a `$_` exists. +## - `given`-`when` looks like other languages' `switch`, but is much more +## powerful thanks to smart matching and Perl 6's "topic variable", $_. +## +## This variable contains the default argument of a block, +## a loop's current iteration (unless explicitly named), etc. +## +## `given` simply puts its argument into `$_` (like a block would do), +## and `when` compares it using the "smart matching" (`~~`) operator. +## +## Since other Perl 6 constructs use this variable (as said before, like `for`, +## blocks, etc), this means the powerful `when` is not only applicable along +## with a `given`, but instead anywhere a `$_` exists. given "foo bar" { say $_; #=> foo bar @@ -337,8 +338,8 @@ given "foo bar" { ### Looping constructs ```perl6 -# - `loop` is an infinite loop if you don't pass it arguments, -# but can also be a C-style `for` loop: +## - `loop` is an infinite loop if you don't pass it arguments, +## but can also be a C-style `for` loop: loop { say "This is an infinite loop !"; last; # last breaks out of the loop, like the `break` keyword in other @@ -352,13 +353,13 @@ loop (my $i = 0; $i < 5; $i++) { say "This is a C-style for loop !"; } -# - `for` - Passes through an array +## - `for` - Passes through an array for @array -> $variable { say "I've got $variable !"; } -# As we saw with given, for's default "current iteration" variable is `$_`. -# That means you can use `when` in a `for` just like you were in a `given`. +## As we saw with given, for's default "current iteration" variable is `$_`. +## That means you can use `when` in a `for` just like you were in a `given`. for @array { say "I've got $_"; @@ -369,13 +370,13 @@ for @array { for @array { # You can... - next if $_ == 3; # Skip to the next iteration (`continue` in C-like languages). - redo if $_ == 4; # Re-do the iteration, keeping the same topic variable (`$_`). - last if $_ == 5; # Or break out of a loop (like `break` in C-like languages). + next if $_ == 3; # Skip to the next iteration (`continue` in C-like languages) + redo if $_ == 4; # Re-do the iteration, keeping the same topic variable (`$_`) + last if $_ == 5; # Or break out of a loop (like `break` in C-like languages) } -# The "pointy block" syntax isn't specific to for. -# It's just a way to express a block in Perl6. +## The "pointy block" syntax isn't specific to for. +## It's just a way to express a block in Perl6. if long-computation() -> $result { say "The result is $result"; } @@ -386,99 +387,100 @@ if long-computation() -> $result { ```perl6 ## Since Perl languages are very much operator-based languages, ## Perl 6 operators are actually just funny-looking subroutines, in syntactic -## categories, like infix:<+> (addition) or prefix:<!> (bool not). +## categories, like infix:<+> (addition) or prefix:<!> (bool not). ## The categories are: -# - "prefix": before (like `!` in `!True`). -# - "postfix": after (like `++` in `$a++`). -# - "infix": in between (like `*` in `4 * 3`). -# - "circumfix": around (like `[`-`]` in `[1, 2]`). -# - "post-circumfix": around, after another term (like `{`-`}` in `%hash{'key'}`) +## - "prefix": before (like `!` in `!True`). +## - "postfix": after (like `++` in `$a++`). +## - "infix": in between (like `*` in `4 * 3`). +## - "circumfix": around (like `[`-`]` in `[1, 2]`). +## - "post-circumfix": around, after another term (like `{`-`}` in +## `%hash{'key'}`) ## The associativity and precedence list are explained below. -# Alright, you're set to go ! +## Alright, you're set to go ! ## * Equality Checking -# - `==` is numeric comparison +## - `==` is numeric comparison 3 == 4; # False 3 != 4; # True -# - `eq` is string comparison +## - `eq` is string comparison 'a' eq 'b'; 'a' ne 'b'; # not equal 'a' !eq 'b'; # same as above -# - `eqv` is canonical equivalence (or "deep equality") +## - `eqv` is canonical equivalence (or "deep equality") (1, 2) eqv (1, 3); -# - Smart Match Operator: `~~` -# Aliases the left hand side to $_ and then evaluates the right hand side. -# Here are some common comparison semantics: +## - Smart Match Operator: `~~` +## Aliases the left hand side to $_ and then evaluates the right hand side. +## Here are some common comparison semantics: -# String or Numeric Equality +## String or Numeric Equality 'Foo' ~~ 'Foo'; # True if strings are equal. 12.5 ~~ 12.50; # True if numbers are equal. -# Regex - For matching a regular expression against the left side. -# Returns a (Match) object, which evaluates as True if regexp matches. +## Regex - For matching a regular expression against the left side. +## Returns a (Match) object, which evaluates as True if regexp matches. my $obj = 'abc' ~~ /a/; say $obj; # 「a」 say $obj.WHAT; # (Match) -# Hashes +## Hashes 'key' ~~ %hash; # True if key exists in hash -# Type - Checks if left side "has type" (can check superclasses and roles) +## Type - Checks if left side "has type" (can check superclasses and roles) 1 ~~ Int; # True -# Smart-matching against a boolean always returns that boolean (and will warn). +## Smart-matching against a boolean always returns that boolean (and will warn). 1 ~~ True; # True False ~~ True; # True -# # General syntax is $arg ~~ &bool-returning-function; -# For a complete list of combinations, use this table: -# http://perlcabal.org/syn/S03.html#Smart_matching +## General syntax is $arg ~~ &bool-returning-function; +## For a complete list of combinations, use this table: +## http://perlcabal.org/syn/S03.html#Smart_matching -# You also, of course, have `<`, `<=`, `>`, `>=`. -# Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. +## You also, of course, have `<`, `<=`, `>`, `>=`. +## Their string equivalent are also available : `lt`, `le`, `gt`, `ge`. 3 > 4; ## * Range constructors 3 .. 7; # 3 to 7, both included -# `^` on either side them exclusive on that side : +## `^` on either side them exclusive on that side : 3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) -# This also works as a shortcut for `0..^N`: +## This also works as a shortcut for `0..^N`: ^10; # means 0..^10 -# This also allows us to demonstrate that Perl 6 has lazy/infinite arrays, -# using the Whatever Star: +## This also allows us to demonstrate that Perl 6 has lazy/infinite arrays, +## using the Whatever Star: my @array = 1..*; # 1 to Infinite ! `1..Inf` is the same. say @array[^10]; # you can pass arrays as subscripts and it'll return # an array of results. This will print # "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) -# Note : when reading an infinite list, Perl 6 will "reify" the elements -# it needs, then keep them in memory. They won't be calculated more than once. -# It also will never calculate more elements that are needed. -# Trying +## Note : when reading an infinite list, Perl 6 will "reify" the elements +## it needs, then keep them in memory. They won't be calculated more than once. +## It also will never calculate more elements that are needed. +## Trying -# An array subscript can also be a closure. -# It'll be called with the length as the argument +## An array subscript can also be a closure. +## It'll be called with the length as the argument say join(' ', @array[15..*]); #=> 15 16 17 18 19 -# which is equivalent to: +## which is equivalent to: say join(' ', @array[-> $n { 15..$n }]); -# Note: if you try to do either of those with an infinite array, -# you'll trigger an infinite loop (your program won't finish) +## Note: if you try to do either of those with an infinite array, +## you'll trigger an infinite loop (your program won't finish) -# You can use that in most places you'd expect, even assigning to an array +## You can use that in most places you'd expect, even assigning to an array my @numbers = ^20; -# Here numbers increase by "6"; more on `...` operator later. +## Here numbers increase by "6"; more on `...` operator later. my @seq = 3, 9 ... * > 95; # 3 9 15 21 27 [...] 81 87 93 99; @numbers[5..*] = 3, 9 ... *; # even though the sequence is infinite, # only the 15 needed values will be calculated. @@ -495,11 +497,11 @@ say @numbers; #=> 0 1 2 3 4 3 9 15 21 [...] 81 87 my ( $a, $b, $c ) = 1, 0, 2; $a && $b && $c; # Returns 0, the first False value -# || Returns the first argument that evaluates to True +## || Returns the first argument that evaluates to True $b || $a; # 1 -# And because you're going to want them, -# you also have compound assignment operators: +## And because you're going to want them, +## you also have compound assignment operators: $a *= 2; # multiply and assignment. Equivalent to $a = $a * 2; $b %%= 5; # divisible by and assignment @array .= sort; # calls the `sort` method and assigns the result back @@ -508,15 +510,15 @@ $b %%= 5; # divisible by and assignment ## More on subs ! ```perl6 -# As we said before, Perl 6 has *really* powerful subs. We're going to see -# a few more key concepts that make them better than in any other language :-). +## As we said before, Perl 6 has *really* powerful subs. We're going to see +## a few more key concepts that make them better than in any other language :-). ``` ### Unpacking ! ```perl6 -# It's the ability to "extract" arrays and keys (AKA "destructuring"). -# It'll work in `my`s and in parameter lists. +## It's the ability to "extract" arrays and keys (AKA "destructuring"). +## It'll work in `my`s and in parameter lists. my ($f, $g) = 1, 2; say $f; #=> 1 my ($, $, $h) = 1, 2, 3; # keep the non-interesting anonymous @@ -532,50 +534,50 @@ sub unpack_array(@array [$fst, $snd]) { unpack_array(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 2 3 -# If you're not using the array itself, you can also keep it anonymous, -# much like a scalar: +## If you're not using the array itself, you can also keep it anonymous, +## much like a scalar: sub first-of-array(@ [$fst]) { $fst } first-of-array(@small); #=> 1 first-of-array(@tail); # Throws an error "Too many positional parameters passed" # (which means the array is too big). -# You can also use a slurp ... +## You can also use a slurp ... sub slurp-in-array(@ [$fst, *@rest]) { # You could keep `*@rest` anonymous say $fst + @rest.elems; # `.elems` returns a list's length. # Here, `@rest` is `(3,)`, since `$fst` holds the `2`. } slurp-in-array(@tail); #=> 3 -# You could even extract on a slurpy (but it's pretty useless ;-).) +## You could even extract on a slurpy (but it's pretty useless ;-).) sub fst(*@ [$fst]) { # or simply : `sub fst($fst) { ... }` say $fst; } fst(1); #=> 1 fst(1, 2); # errors with "Too many positional parameters passed" -# You can also destructure hashes (and classes, which you'll learn about later !) -# The syntax is basically `%hash-name (:key($variable-to-store-value-in))`. -# The hash can stay anonymous if you only need the values you extracted. +## You can also destructure hashes (and classes, which you'll learn about later) +## The syntax is basically `%hash-name (:key($variable-to-store-value-in))`. +## The hash can stay anonymous if you only need the values you extracted. sub key-of(% (:value($val), :qua($qua))) { say "Got val $val, $qua times."; } -# Then call it with a hash: (you need to keep the brackets for it to be a hash) +## Then call it with a hash: (you need to keep the brackets for it to be a hash) key-of({value => 'foo', qua => 1}); #key-of(%hash); # the same (for an equivalent `%hash`) ## The last expression of a sub is returned automatically -# (though you may use the `return` keyword, of course): +## (though you may use the `return` keyword, of course): sub next-index($n) { $n + 1; } my $new-n = next-index(3); # $new-n is now 4 -# This is true for everything, except for the looping constructs -# (due to performance reasons): there's reason to build a list -# if we're just going to discard all the results. -# If you still want to build one, you can use the `do` statement prefix: -# (or the `gather` prefix, which we'll see later) +## This is true for everything, except for the looping constructs +## (due to performance reasons): there's reason to build a list +## if we're just going to discard all the results. +## If you still want to build one, you can use the `do` statement prefix: +## (or the `gather` prefix, which we'll see later) sub list-of($n) { do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) $_ # current loop iteration @@ -589,16 +591,16 @@ my @list3 = list-of(3); #=> (0, 1, 2) ```perl6 ## You can create a lambda with `-> {}` ("pointy block") or `{}` ("block") my &lambda = -> $argument { "The argument passed to this lambda is $argument" } -# `-> {}` and `{}` are pretty much the same thing, except that the former can -# take arguments, and that the latter can be mistaken as a hash by the parser. +## `-> {}` and `{}` are pretty much the same thing, except that the former can +## take arguments, and that the latter can be mistaken as a hash by the parser. -# We can, for example, add 3 to each value of an array using map: +## We can, for example, add 3 to each value of an array using map: my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument -# A sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`): -# A block doesn't have a "function context" (though it can have arguments), -# which means that if you return from it, -# you're going to return from the parent function. Compare: +## A sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`): +## A block doesn't have a "function context" (though it can have arguments), +## which means that if you return from it, +## you're going to return from the parent function. Compare: sub is-in(@array, $elem) { # this will `return` out of the `is-in` sub # once the condition evaluated to True, the loop won't be run anymore @@ -611,51 +613,51 @@ sub truthy-array(@array) { # ^ the `return` only returns from the anonymous `sub` } -# You can also use the "whatever star" to create an anonymous function -# (it'll stop at the furthest operator in the current expression) +## You can also use the "whatever star" to create an anonymous function +## (it'll stop at the furthest operator in the current expression) my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` my @arrayplus3 = map(*+*+3, @array); # Same as `-> $a, $b { $a + $b + 3 }` # also `sub ($a, $b) { $a + $b + 3 }` say (*/2)(4); #=> 2 - # Immediatly execute the function Whatever created. + # Immediately execute the function Whatever created. say ((*+3)/5)(5); #=> 1.6 # works even in parens ! -# But if you need to have more than one argument (`$_`) -# in a block (without wanting to resort to `-> {}`), -# you can also use the implicit argument syntax, `$^` : +## But if you need to have more than one argument (`$_`) +## in a block (without wanting to resort to `-> {}`), +## you can also use the implicit argument syntax, `$^` : map({ $^a + $^b + 3 }, @array); # equivalent to following: map(sub ($a, $b) { $a + $b + 3 }, @array); # (here with `sub`) -# Note : those are sorted lexicographically. +## Note : those are sorted lexicographically. # `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` ``` ### About types... ```perl6 -# Perl6 is gradually typed. This means you can specify the type -# of your variables/arguments/return types, or you can omit them -# and they'll default to "Any". -# You obviously get access to a few base types, like Int and Str. -# The constructs for declaring types are "class", "role", -# which you'll see later. - -# For now, let us examine "subset": -# a "subset" is a "sub-type" with additional checks. -# For example: "a very big integer is an Int that's greater than 500" -# You can specify the type you're subtyping (by default, Any), -# and add additional checks with the "where" keyword: +## Perl6 is gradually typed. This means you can specify the type +## of your variables/arguments/return types, or you can omit them +## and they'll default to "Any". +## You obviously get access to a few base types, like Int and Str. +## The constructs for declaring types are "class", "role", +## which you'll see later. + +## For now, let us examine "subset": +## a "subset" is a "sub-type" with additional checks. +## For example: "a very big integer is an Int that's greater than 500" +## You can specify the type you're subtyping (by default, Any), +## and add additional checks with the "where" keyword: subset VeryBigInteger of Int where * > 500; ``` ### Multiple Dispatch ```perl6 -# Perl 6 can decide which variant of a `sub` to call based on the type of the -# arguments, or on arbitrary preconditions, like with a type or a `where`: +## Perl 6 can decide which variant of a `sub` to call based on the type of the +## arguments, or on arbitrary preconditions, like with a type or a `where`: -# with types +## with types multi sub sayit(Int $n) { # note the `multi` keyword here say "Number: $n"; } @@ -666,7 +668,7 @@ sayit("foo"); # prints "String: foo" sayit(True); # fails at *compile time* with # "calling 'sayit' will never work with arguments of types ..." -# with arbitrary precondition (remember subsets?): +## with arbitrary precondition (remember subsets?): multi is-big(Int $n where * > 50) { "Yes !" } # using a closure multi is-big(Int $ where 10..50) { "Quite." } # Using smart-matching # (could use a regexp, etc) @@ -678,7 +680,7 @@ multi odd-or-even(Even) { "Even" } # The main case using the type. # We don't name the argument. multi odd-or-even($) { "Odd" } # "else" -# You can even dispatch based on a positional's argument presence ! +## You can even dispatch based on a positional's argument presence ! multi with-or-without-you(:$with!) { # You need make it mandatory to # be able to dispatch against it. say "I can live ! Actually, I can't."; @@ -686,26 +688,26 @@ multi with-or-without-you(:$with!) { # You need make it mandatory to multi with-or-without-you { say "Definitely can't live."; } -# This is very, very useful for many purposes, like `MAIN` subs (covered later), -# and even the language itself is using it in several places. -# -# - `is`, for example, is actually a `multi sub` named `trait_mod:<is>`, -# and it works off that. -# - `is rw`, is simply a dispatch to a function with this signature: -# sub trait_mod:<is>(Routine $r, :$rw!) {} -# -# (commented because running this would be a terrible idea !) +## This is very, very useful for many purposes, like `MAIN` subs (covered +## later), and even the language itself is using it in several places. +## +## - `is`, for example, is actually a `multi sub` named `trait_mod:<is>`, +## and it works off that. +## - `is rw`, is simply a dispatch to a function with this signature: +## sub trait_mod:<is>(Routine $r, :$rw!) {} +## +## (commented because running this would be a terrible idea !) ``` ## Scoping ```perl6 -# In Perl 6, unlike many scripting languages, (such as Python, Ruby, PHP), -# you must declare your variables before using them. The `my` declarator -# you have learned uses "lexical scoping". There are a few other declarators, -# (`our`, `state`, ..., ) which we'll see later. -# This is called "lexical scoping", where in inner blocks, -# you can access variables from outer blocks. +## In Perl 6, unlike many scripting languages, (such as Python, Ruby, PHP), +## you must declare your variables before using them. The `my` declarator +## you have learned uses "lexical scoping". There are a few other declarators, +## (`our`, `state`, ..., ) which we'll see later. +## This is called "lexical scoping", where in inner blocks, +## you can access variables from outer blocks. my $file_scoped = 'Foo'; sub outer { my $outer_scoped = 'Bar'; @@ -716,27 +718,27 @@ sub outer { } outer()(); #=> 'Foo Bar' -# As you can see, `$file_scoped` and `$outer_scoped` were captured. -# But if we were to try and use `$bar` outside of `foo`, -# the variable would be undefined (and you'd get a compile time error). +## As you can see, `$file_scoped` and `$outer_scoped` were captured. +## But if we were to try and use `$bar` outside of `foo`, +## the variable would be undefined (and you'd get a compile time error). ``` ## Twigils ```perl6 -# There are many special `twigils` (composed sigil's) in Perl 6. -# Twigils define the variables' scope. -# The * and ? twigils work on standard variables: -# * Dynamic variable -# ? Compile-time variable -# The ! and the . twigils are used with Perl 6's objects: -# ! Attribute (class member) -# . Method (not really a variable) - -# `*` Twigil: Dynamic Scope -# These variables use the`*` twigil to mark dynamically-scoped variables. -# Dynamically-scoped variables are looked up through the caller, not through -# the outer scope +## There are many special `twigils` (composed sigil's) in Perl 6. +## Twigils define the variables' scope. +## The * and ? twigils work on standard variables: +## * Dynamic variable +## ? Compile-time variable +## The ! and the . twigils are used with Perl 6's objects: +## ! Attribute (class member) +## . Method (not really a variable) + +## `*` Twigil: Dynamic Scope +## These variables use the`*` twigil to mark dynamically-scoped variables. +## Dynamically-scoped variables are looked up through the caller, not through +## the outer scope my $*dyn_scoped_1 = 1; my $*dyn_scoped_2 = 10; @@ -749,8 +751,9 @@ sub call_say_dyn { my $*dyn_scoped_1 = 25; # Defines $*dyn_scoped_1 only for this sub. $*dyn_scoped_2 = 100; # Will change the value of the file scoped variable. say_dyn(); #=> 25 100 $*dyn_scoped 1 and 2 will be looked for in the call. - # It uses he value of $*dyn_scoped_1 from inside this sub's lexical - # scope even though the blocks aren't nested (they're call-nested). + # It uses the value of $*dyn_scoped_1 from inside this sub's + # lexical scope even though the blocks aren't nested (they're + # call-nested). } say_dyn(); #=> 1 10 call_say_dyn(); #=> 25 100 @@ -763,20 +766,20 @@ say_dyn(); #=> 1 100 We changed the value of $*dyn_scoped_2 in call_say_dyn ## Object Model ```perl6 -# To call a method on an object, add a dot followed by the method name: -# => $object.method -# Classes are declared with the `class` keyword. Attributes are declared -# with the `has` keyword, and methods declared with `method`. -# Every attribute that is private uses the ! twigil for example: `$!attr`. -# Immutable public attributes use the `.` twigil. -# (you can make them mutable with `is rw`) -# The easiest way to remember the `$.` twigil is comparing it to how methods -# are called. - -# Perl 6's object model ("SixModel") is very flexible, -# and allows you to dynamically add methods, change semantics, etc ... -# (these will not all be covered here, and you should refer to: -# https://docs.perl6.org/language/objects.html. +## To call a method on an object, add a dot followed by the method name: +## => $object.method +## Classes are declared with the `class` keyword. Attributes are declared +## with the `has` keyword, and methods declared with `method`. +## Every attribute that is private uses the ! twigil for example: `$!attr`. +## Immutable public attributes use the `.` twigil. +## (you can make them mutable with `is rw`) +## The easiest way to remember the `$.` twigil is comparing it to how methods +## are called. + +## Perl 6's object model ("SixModel") is very flexible, +## and allows you to dynamically add methods, change semantics, etc ... +## (these will not all be covered here, and you should refer to: +## https://docs.perl6.org/language/objects.html. class Attrib-Class { has $.attrib; # `$.attrib` is immutable. @@ -800,11 +803,11 @@ class Attrib-Class { } }; -# Create a new instance of Attrib-Class with $.attrib set to 5 : -# Note: you can't set private-attribute from here (more later on). +## Create a new instance of Attrib-Class with $.attrib set to 5 : +## Note: you can't set private-attribute from here (more later on). my $class-obj = Attrib-Class.new(attrib => 5); say $class-obj.get-value; #=> 15 -#$class-obj.attrib = 5; # This fails, because the `has $.attrib` is immutable +# $class-obj.attrib = 5; # This fails, because the `has $.attrib` is immutable $class-obj.other-attrib = 10; # This, however, works, because the public # attribute is mutable (`rw`). ``` @@ -812,11 +815,11 @@ $class-obj.other-attrib = 10; # This, however, works, because the public ### Object Inheritance ```perl6 -# Perl 6 also has inheritance (along with multiple inheritance) -# While `method`'s are inherited, `submethod`'s are not. -# Submethods are useful for object construction and destruction tasks, -# such as BUILD, or methods that must be overriden by subtypes. -# We will learn about BUILD later on. +## Perl 6 also has inheritance (along with multiple inheritance) +## While `method`'s are inherited, `submethod`'s are not. +## Submethods are useful for object construction and destruction tasks, +## such as BUILD, or methods that must be overridden by subtypes. +## We will learn about BUILD later on. class Parent { has $.age; @@ -836,24 +839,24 @@ class Child is Parent { my Parent $Richard .= new(age => 40, name => 'Richard'); $Richard.favorite-color; #=> "My favorite color is Blue" $Richard.talk; #=> "Hi, my name is Richard" -# # $Richard is able to access the submethod, he knows how to say his name. +## $Richard is able to access the submethod, he knows how to say his name. my Child $Madison .= new(age => 1, name => 'Madison'); -$Madison.talk; # prints "Goo goo ga ga" due to the overrided method. -# $Madison.favorite-color does not work since it is not inherited - -# When you use `my T $var`, `$var` starts off with `T` itself in it, -# so you can call `new` on it. -# (`.=` is just the dot-call and the assignment operator: -# `$a .= b` is the same as `$a = $a.b`) -# Also note that `BUILD` (the method called inside `new`) -# will set parent properties too, so you can pass `val => 5`. +$Madison.talk; # prints "Goo goo ga ga" due to the overridden method. +# $Madison.favorite-color # does not work since it is not inherited + +## When you use `my T $var`, `$var` starts off with `T` itself in it, +## so you can call `new` on it. +## (`.=` is just the dot-call and the assignment operator: +## `$a .= b` is the same as `$a = $a.b`) +## Also note that `BUILD` (the method called inside `new`) +## will set parent properties too, so you can pass `val => 5`. ``` ### Roles, or Mixins ```perl6 -# Roles are supported too (also called Mixins in other languages) +## Roles are supported too (also called Mixins in other languages) role PrintableVal { has $!counter = 0; method print { @@ -861,105 +864,108 @@ role PrintableVal { } } -# you "import" a mixin (a "role") with "does": +## you "import" a mixin (a "role") with "does": class Item does PrintableVal { has $.val; - # When `does`-ed, a `role` literally "mixes in" the class: - # the methods and attributes are put together, which means a class can access - # the private attributes/methods of its roles (but not the inverse !): + ## When `does`-ed, a `role` literally "mixes in" the class: + ## the methods and attributes are put together, which means a class can access + ## the private attributes/methods of its roles (but not the inverse !): method access { say $!counter++; } - # However, this: - # method print {} - # is ONLY valid when `print` isn't a `multi` with the same dispatch. - # (this means a parent class can shadow a child class's `multi print() {}`, - # but it's an error if a role does) + ## However, this: + ## method print {} + ## is ONLY valid when `print` isn't a `multi` with the same dispatch. + ## (this means a parent class can shadow a child class's `multi print() {}`, + ## but it's an error if a role does) - # NOTE: You can use a role as a class (with `is ROLE`). In this case, methods - # will be shadowed, since the compiler will consider `ROLE` to be a class. + ## NOTE: You can use a role as a class (with `is ROLE`). In this case, + ## methods will be shadowed, since the compiler will consider `ROLE` to + ## be a class. } ``` ## Exceptions ```perl6 -# Exceptions are built on top of classes, in the package `X` (like `X::IO`). -# In Perl6 exceptions are automatically 'thrown' +## Exceptions are built on top of classes, in the package `X` (like `X::IO`). +## In Perl6 exceptions are automatically 'thrown' open 'foo'; #> Failed to open file foo: no such file or directory -# It will also print out what line the error was thrown at and other error info +## It will also print out what line the error was thrown at and other error info -# You can throw an exception using `die`: +## You can throw an exception using `die`: die 'Error!'; #=> Error! -# Or more explicitly: + +## Or more explicitly: die X::AdHoc.new(payload => 'Error!'); -# In Perl 6, `orelse` is similar to the `or` operator, except it only matches -# undefined variables instead of anything evaluating as false. -# Undefined values include: `Nil`, `Mu` and `Failure` as well as `Int`, `Str` -# and other types that have not been initialized to any value yet. -# You can check if something is defined or not using the defined method: +## In Perl 6, `orelse` is similar to the `or` operator, except it only matches +## undefined variables instead of anything evaluating as false. +## Undefined values include: `Nil`, `Mu` and `Failure` as well as `Int`, `Str` +## and other types that have not been initialized to any value yet. +## You can check if something is defined or not using the defined method: my $uninitialized; say $uninitiazilzed.defined; #> False -# When using `orelse` it will disarm the exception and alias $_ to that failure -# This will avoid it being automatically handled and printing lots of scary -# error messages to the screen. -# We can use the exception method on $_ to access the exception +## When using `orelse` it will disarm the exception and alias $_ to that failure +## This will avoid it being automatically handled and printing lots of scary +## error messages to the screen. +## We can use the exception method on $_ to access the exception open 'foo' orelse say "Something happened {.exception}"; -# This also works: + +## This also works: open 'foo' orelse say "Something happened $_"; #> Something happened #> Failed to open file foo: no such file or directory -# Both of those above work but in case we get an object from the left side that -# is not a failure we will probably get a warning. We see below how we can use -# `try` and `CATCH` to be more specific with the exceptions we catch. +## Both of those above work but in case we get an object from the left side that +## is not a failure we will probably get a warning. We see below how we can use +## `try` and `CATCH` to be more specific with the exceptions we catch. ``` ### Using `try` and `CATCH` ```perl6 -# By using `try` and `CATCH` you can contain and handle exceptions without -# disrupting the rest of the program. `try` will set the last exception to -# the special variable `$!` Note: This has no relation to $!variables. +## By using `try` and `CATCH` you can contain and handle exceptions without +## disrupting the rest of the program. `try` will set the last exception to +## the special variable `$!` Note: This has no relation to $!variables. try open 'foo'; say "Well, I tried! $!" if defined $!; #> Well, I tried! Failed to open file #foo: no such file or directory -# Now, what if we want more control over handling the exception? -# Unlike many other languages, in Perl 6, you put the `CATCH` block *within* -# the block to `try`. Similar to how $_ was set when we 'disarmed' the -# exception with orelse, we also use $_ in the CATCH block. -# Note: ($! is only set *after* the `try` block) -# By default, a `try` has a `CATCH` block that catches -# any exception (`CATCH { default {} }`). +## Now, what if we want more control over handling the exception? +## Unlike many other languages, in Perl 6, you put the `CATCH` block *within* +## the block to `try`. Similar to how $_ was set when we 'disarmed' the +## exception with orelse, we also use $_ in the CATCH block. +## Note: ($! is only set *after* the `try` block) +## By default, a `try` has a `CATCH` block that catches +## any exception (`CATCH { default {} }`). try { my $a = (0 %% 0); CATCH { say "Something happened: $_" } } #=> Something happened: Attempt to divide by zero using infix:<%%> -# You can redefine it using `when`s (and `default`) -# to handle the exceptions you want: +## You can redefine it using `when`s (and `default`) +## to handle the exceptions you want: try { open 'foo'; CATCH { # In the `CATCH` block, the exception is set to $_ when X::AdHoc { say "Error: $_" } #=>Error: Failed to open file /dir/foo: no such file or directory - # Any other exception will be re-raised, since we don't have a `default` - # 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. + ## Any other exception will be re-raised, since we don't have a `default` + ## 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. } } -# There are also some subtleties to exceptions. Some Perl 6 subs return a -# `Failure`, which is a kind of "unthrown exception". They're not thrown until -# you tried to look at their content, unless you call `.Bool`/`.defined` on -# them - then they're handled. -# (the `.handled` method is `rw`, so you can mark it as `False` back yourself) -# -# You can throw a `Failure` using `fail`. Note that if the pragma `use fatal` -# is on, `fail` will throw an exception (like `die`). +## There are also some subtleties to exceptions. Some Perl 6 subs return a +## `Failure`, which is a kind of "unthrown exception". They're not thrown until +## you tried to look at their content, unless you call `.Bool`/`.defined` on +## them - then they're handled. +## (the `.handled` method is `rw`, so you can mark it as `False` back yourself) +## +## You can throw a `Failure` using `fail`. Note that if the pragma `use fatal` +## is on, `fail` will throw an exception (like `die`). fail "foo"; # We're not trying to access the value, so no problem. try { fail "foo"; @@ -968,28 +974,28 @@ try { } } -# There is also another kind of exception: Control exceptions. -# Those are "good" exceptions, which happen when you change your program's flow, -# using operators like `return`, `next` or `last`. -# You can "catch" those with `CONTROL` (not 100% working in Rakudo yet). +## There is also another kind of exception: Control exceptions. +## Those are "good" exceptions, which happen when you change your program's +## flow, using operators like `return`, `next` or `last`. +## You can "catch" those with `CONTROL` (not 100% working in Rakudo yet). ``` ## Packages ```perl6 -# Packages are a way to reuse code. Packages are like "namespaces", and any -# element of the six model (`module`, `role`, `class`, `grammar`, `subset` -# and `enum`) are actually packages. (Packages are the lowest common denominator) -# Packages are important - especially as Perl is well-known for CPAN, -# the Comprehensive Perl Archive Network. +## Packages are a way to reuse code. Packages are like "namespaces", and any +## element of the six model (`module`, `role`, `class`, `grammar`, `subset` and +## `enum`) are actually packages. (Packages are the lowest common denominator) +## Packages are important - especially as Perl is well-known for CPAN, +## the Comprehensive Perl Archive Network. -# You can use a module (bring its declarations into scope) with `use` +## You can use a module (bring its declarations into scope) with `use` use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module say from-json('[1]').perl; #=> [1] -# You should not declare packages using the `package` keyword (unlike Perl 5). -# Instead, use `class Package::Name::Here;` to declare a class, or if you only want to -# export variables/subs, you can use `module`. +## You should not declare packages using the `package` keyword (unlike Perl 5). +## Instead, use `class Package::Name::Here;` to declare a class, or if you only +## want to export variables/subs, you can use `module`. module Hello::World { # Bracketed form # If `Hello` doesn't exist yet, it'll just be a "stub", @@ -1001,22 +1007,22 @@ unit module Parse::Text; # file-scoped form grammar Parse::Text::Grammar { # A grammar is a package, which you could `use` } # You will learn more about grammars in the regex section -# As said before, any part of the six model is also a package. -# Since `JSON::Tiny` uses (its own) `JSON::Tiny::Actions` class, you can use it: +## As said before, any part of the six model is also a package. +## Since `JSON::Tiny` uses its own `JSON::Tiny::Actions` class, you can use it: my $actions = JSON::Tiny::Actions.new; -# We'll see how to export variables and subs in the next part: +## We'll see how to export variables and subs in the next part: ``` ## Declarators ```perl6 -# In Perl 6, you get different behaviors based on how you declare a variable. -# You've already seen `my` and `has`, we'll now explore the others. +## In Perl 6, you get different behaviors based on how you declare a variable. +## You've already seen `my` and `has`, we'll now explore the others. ## * `our` declarations happen at `INIT` time -- (see "Phasers" below) -# It's like `my`, but it also creates a package variable. -# (All packagish things (`class`, `role`, etc) are `our` by default) +## It's like `my`, but it also creates a package variable. +## (All packagish things (`class`, `role`, etc) are `our` by default) module Var::Increment { our $our-var = 1; # Note: you can't put a type constraint like Int on an my $my-var = 22; # `our` variable. @@ -1043,26 +1049,26 @@ Var::Increment::Inc; #=> 3 # Notice how the value of $our-var was Var::Increment::unavailable; #> Could not find symbol '&unavailable' ## * `constant` (happens at `BEGIN` time) -# You can use the `constant` keyword to declare a compile-time variable/symbol: +## You can use the `constant` keyword to declare a compile-time variable/symbol: constant Pi = 3.14; constant $var = 1; -# And if you're wondering, yes, it can also contain infinite lists. +## And if you're wondering, yes, it can also contain infinite lists. constant why-not = 5, 15 ... *; say why-not[^5]; #=> 5 15 25 35 45 ## * `state` (happens at run time, but only once) -# State variables are only initialized one time -# (they exist in other langages such as C as `static`) +## State variables are only initialized one time +## (they exist in other languages such as C as `static`) sub fixed-rand { state $val = rand; say $val; } fixed-rand for ^10; # will print the same number 10 times -# Note, however, that they exist separately in different enclosing contexts. -# If you declare a function with a `state` within a loop, it'll re-create the -# variable for each iteration of the loop. See: +## Note, however, that they exist separately in different enclosing contexts. +## If you declare a function with a `state` within a loop, it'll re-create the +## variable for each iteration of the loop. See: for ^5 -> $a { sub foo { state $val = rand; # This will be a different value for every value of `$a` @@ -1077,13 +1083,14 @@ for ^5 -> $a { ## Phasers ```perl6 -# Phasers in Perl 6 are blocks that happen at determined points of time in your -# program. They are called phasers because they mark a change in the phase -# of a program. For example, when the program is compiled, a for loop runs, -# you leave a block, or an exception gets thrown. (`CATCH` is actually a phaser !) -# Some of them can be used for their return values, some of them can't -# (those that can have a "[*]" in the beginning of their explanation text). -# Let's have a look ! +## Phasers in Perl 6 are blocks that happen at determined points of time in your +## program. They are called phasers because they mark a change in the phase +## of a program. For example, when the program is compiled, a for loop runs, +## you leave a block, or an exception gets thrown. +## (`CATCH` is actually a phaser!) +## Some of them can be used for their return values, some of them can't +## (those that can have a "[*]" in the beginning of their explanation text). +## Let's have a look ! ## * Compile-time phasers BEGIN { say "[*] Runs at compile time, as soon as possible, only once" } @@ -1104,7 +1111,8 @@ PRE { say "If this block doesn't return a truthy value, an exception of type X::Phaser::PrePost is thrown."; } -# exemple: + +## example: for 0..2 { PRE { $_ > 1 } # This is going to blow up with "Precondition failed" } @@ -1121,8 +1129,10 @@ for 0..2 { ## * Block/exceptions phasers sub { - KEEP { say "Runs when you exit a block successfully (without throwing an exception)" } - UNDO { say "Runs when you exit a block unsuccessfully (by throwing an exception)" } + KEEP { say "Runs when you exit a block successfully + (without throwing an exception)" } + UNDO { say "Runs when you exit a block unsuccessfully + (by throwing an exception)" } } ## * Loop phasers @@ -1135,10 +1145,10 @@ for ^5 { ## * Role/class phasers COMPOSE { "When a role is composed into a class. /!\ NOT YET IMPLEMENTED" } -# They allow for cute tricks or clever code ...: +## They allow for cute tricks or clever code ...: say "This code took " ~ (time - CHECK time) ~ "s to compile"; -# ... or clever organization: +## ... or clever organization: sub do-db-stuff { $db.start-transaction; # start a new transaction KEEP $db.commit; # commit the transaction if all went well @@ -1149,29 +1159,29 @@ sub do-db-stuff { ## Statement prefixes ```perl6 -# Those act a bit like phasers: they affect the behavior of the following code. -# Though, they run in-line with the executable code, so they're in lowercase. -# (`try` and `start` are theoretically in that list, but explained somewhere else) -# Note: all of these (except start) don't need explicit brackets `{` and `}`. - -# - `do` (that you already saw) - runs a block or a statement as a term -# You can't normally use a statement as a value (or "term"): -# -# my $value = if True { 1 } # `if` is a statement - parse error -# -# This works: +## Those act a bit like phasers: they affect the behavior of the following code. +## Though, they run in-line with the executable code, so they're in lowercase. +## (`try` and `start` are theoretically in that list, but explained elsewhere) +## Note: all of these (except start) don't need explicit brackets `{` and `}`. + +## - `do` (that you already saw) - runs a block or a statement as a term +## You can't normally use a statement as a value (or "term"): +## +## my $value = if True { 1 } # `if` is a statement - parse error +## +## This works: my $a = do if True { 5 } # with `do`, `if` is now a term. -# - `once` - Makes sure a piece of code only runs once +## - `once` - Makes sure a piece of code only runs once for ^5 { once say 1 }; #=> 1 # Only prints ... once. -# Like `state`, they're cloned per-scope +## Like `state`, they're cloned per-scope for ^5 { sub { once say 1 }() } #=> 1 1 1 1 1 # Prints once per lexical scope -# - `gather` - Co-routine thread -# Gather allows you to `take` several values in an array, -# much like `do`, but allows you to take any expression. +## - `gather` - Co-routine thread +## Gather allows you to `take` several values in an array, +## much like `do`, but allows you to take any expression. say gather for ^5 { take $_ * 3 - 1; take $_ * 3 + 1; @@ -1182,41 +1192,43 @@ say join ',', gather if False { take 3; } # Doesn't print anything. -# - `eager` - Evaluate statement eagerly (forces eager context) -# Don't try this at home: -# -# eager 1..*; # this will probably hang for a while (and might crash ...). -# -# But consider: +## - `eager` - Evaluate statement eagerly (forces eager context) +## Don't try this at home: +## +## eager 1..*; # this will probably hang for a while (and might crash ...). +## +## But consider: constant thrice = gather for ^3 { say take $_ }; # Doesn't print anything -# versus: + +## versus: constant thrice = eager gather for ^3 { say take $_ }; #=> 0 1 2 ``` ## Iterables ```perl6 -# Iterables are objects that can be iterated similar to the `for` construct -# `flat`, flattens iterables: +## Iterables are objects that can be iterated similar to the `for` construct +## `flat`, flattens iterables: say (1, 10, (20, 10) ); #> (1 10 (20 10)) Notice how grouping is maintained say (1, 10, (20, 10) ).flat; #> (1 10 20 10) Now the iterable is flat -# - `lazy` - Defer actual evaluation until value is fetched (forces lazy context) +## - `lazy` - Defer actual evaluation until value is fetched +## (forces lazy context) my @lazy-array = (1..100).lazy; -say @lazy-array.is-lazy; #> True # Check for lazyness with the `is-lazy` method. +say @lazy-array.is-lazy; #> True # Check for laziness with the `is-lazy` method. say @lazy-array; #> [...] List has not been iterated on! -my @lazy-array { .print }; # This works and will only do as much work as is -# needed. +my @lazy-array { .print }; # This works and will only do as much work as + # is needed. [//]: # ( TODO explain that gather/take and map are all lazy) -# - `sink` - An `eager` that discards the results (forces sink context) +## - `sink` - An `eager` that discards the results (forces sink context) constant nilthingie = sink for ^3 { .say } #=> 0 1 2 say nilthingie.perl; #=> Nil -# - `quietly` blocks will suppress warnings: +## - `quietly` blocks will suppress warnings: quietly { warn 'This is a warning!' }; #=> No output -# - `contend` - Attempts side effects under STM -# Not yet implemented ! +## - `contend` - Attempts side effects under STM +## Not yet implemented ! ``` ## More operators thingies ! @@ -1224,18 +1236,18 @@ quietly { warn 'This is a warning!' }; #=> No output ```perl6 ## Everybody loves operators ! Let's get more of them -# The precedence list can be found here: -# https://docs.perl6.org/language/operators#Operator_Precedence -# But first, we need a little explanation about associativity: +## The precedence list can be found here: +## https://docs.perl6.org/language/operators#Operator_Precedence +## But first, we need a little explanation about associativity: -# * Binary operators: +## * Binary operators: $a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` $a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)` $a ! $b ! $c; # with a non-associative `!`, this is illegal $a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)` $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` -# * Unary operators: +## * Unary operators: !$a! # with left-associative `!`, this is `(!$a)!` !$a! # with right-associative `!`, this is `!($a!)` !$a! # with non-associative `!`, this is illegal @@ -1244,12 +1256,12 @@ $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` ### Create your own operators ! ```perl6 -# Okay, you've been reading all of that, so I guess I should try -# to show you something exciting. -# I'll tell you a little secret (or not-so-secret): -# In Perl 6, all operators are actually just funny-looking subroutines. +## Okay, you've been reading all of that, so I guess I should try +## to show you something exciting. +## I'll tell you a little secret (or not-so-secret): +## In Perl 6, all operators are actually just funny-looking subroutines. -# You can declare an operator just like you declare a sub: +## You can declare an operator just like you declare a sub: sub prefix:<win>($winner) { # refer to the operator categories # (yes, it's the "words operator" `<>`) say "$winner Won !"; @@ -1257,7 +1269,7 @@ sub prefix:<win>($winner) { # refer to the operator categories win "The King"; #=> The King Won ! # (prefix is before) -# you can still call the sub with its "full name" +## you can still call the sub with its "full name": say prefix:<!>(True); #=> False sub postfix:<!>(Int $n) { @@ -1280,7 +1292,7 @@ sub infix:<times>(Int $n, Block $r) { # infix in the middle # You're very recommended to put spaces # around your infix operator calls. -# For circumfix and post-circumfix ones +## For circumfix and post-circumfix ones sub circumfix:<[ ]>(Int $n) { $n ** $n } @@ -1288,95 +1300,96 @@ say [5]; #=> 3125 # circumfix is around. Again, no whitespace. sub postcircumfix:<{ }>(Str $s, Int $idx) { - # post-circumfix is - # "after a term, around something" + ## post-circumfix is + ## "after a term, around something" $s.substr($idx, 1); } say "abc"{1}; #=> b # after the term `"abc"`, and around the index (1) -# This really means a lot -- because everything in Perl 6 uses this. -# For example, to delete a key from a hash, you use the `:delete` adverb -# (a simple named argument underneath): +## This really means a lot -- because everything in Perl 6 uses this. +## For example, to delete a key from a hash, you use the `:delete` adverb +## (a simple named argument underneath): %h{$key}:delete; -# equivalent to: +## equivalent to: postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that) -# It's *all* using the same building blocks! -# Syntactic categories (prefix infix ...), named arguments (adverbs), ..., -# - used to build the language - are available to you. -# (you are, obviously, recommended against making an operator out of -# *everything* -- with great power comes great responsibility) +## It's *all* using the same building blocks! +## Syntactic categories (prefix infix ...), named arguments (adverbs), ..., +## - used to build the language - are available to you. +## (you are, obviously, recommended against making an operator out of +## *everything* -- with great power comes great responsibility) ``` ### Meta operators ! ```perl6 -# Oh boy, get ready. Get ready, because we're delving deep -# into the rabbit's hole, and you probably won't want to go -# back to other languages after reading that. -# (I'm guessing you don't want to already at that point). -# Meta-operators, as their name suggests, are *composed* operators. -# Basically, they're operators that apply another operator. +## Oh boy, get ready. Get ready, because we're delving deep +## into the rabbit's hole, and you probably won't want to go +## back to other languages after reading that. +## (I'm guessing you don't want to already at that point). +## Meta-operators, as their name suggests, are *composed* operators. +## Basically, they're operators that apply another operator. ## * Reduce meta-operator -# It's a prefix meta-operator that takes a binary function and -# one or many lists. If it doesn't get passed any argument, -# it either returns a "default value" for this operator -# (a meaningless value) or `Any` if there's none (examples below). -# -# Otherwise, it pops an element from the list(s) one at a time, and applies -# the binary function to the last result (or the list's first element) -# and the popped element. -# -# To sum a list, you could use the reduce meta-operator with `+`, i.e.: +## It's a prefix meta-operator that takes a binary function and +## one or many lists. If it doesn't get passed any argument, +## it either returns a "default value" for this operator +## (a meaningless value) or `Any` if there's none (examples below). +## +## Otherwise, it pops an element from the list(s) one at a time, and applies +## the binary function to the last result (or the list's first element) +## and the popped element. +## +## To sum a list, you could use the reduce meta-operator with `+`, i.e.: say [+] 1, 2, 3; #=> 6 -# equivalent to `(1+2)+3` +## equivalent to `(1+2)+3` + say [*] 1..5; #=> 120 -# equivalent to `((((1*2)*3)*4)*5)`. +## equivalent to `((((1*2)*3)*4)*5)`. -# You can reduce with any operator, not just with mathematical ones. -# For example, you could reduce with `//` to get -# the first defined element of a list: +## You can reduce with any operator, not just with mathematical ones. +## For example, you could reduce with `//` to get +## the first defined element of a list: say [//] Nil, Any, False, 1, 5; #=> False # (Falsey, but still defined) - -# Default value examples: +## Default value examples: say [*] (); #=> 1 say [+] (); #=> 0 # meaningless values, since N*1=N and N+0=N. say [//]; #=> (Any) # There's no "default value" for `//`. -# You can also call it with a function you made up, using double brackets: +## You can also call it with a function you made up, using double brackets: sub add($a, $b) { $a + $b } say [[&add]] 1, 2, 3; #=> 6 ## * Zip meta-operator -# This one is an infix meta-operator than also can be used as a "normal" -# operator. It takes an optional binary function (by default, it just creates -# a pair), and will pop one value off of each array and call its binary function -# on these until it runs out of elements. It returns an array with all of these -# new elements. -(1, 2) Z (3, 4); # ((1, 3), (2, 4)), since by default, the function makes an array +## This one is an infix meta-operator that also can be used as a "normal" +## operator. It takes an optional binary function (by default, it just creates +## a pair), and will pop one value off of each array and call its binary +## function on these until it runs out of elements. It returns an array with +## all of these new elements. +(1, 2) Z (3, 4); # ((1, 3), (2, 4)), since by default, the function + # makes an array. 1..3 Z+ 4..6; # (5, 7, 9), using the custom infix:<+> function -# Since `Z` is list-associative (see the list above), -# you can use it on more than one list +## Since `Z` is list-associative (see the list above), +## you can use it on more than one list (True, False) Z|| (False, False) Z|| (False, False); # (True, False) -# And, as it turns out, you can also use the reduce meta-operator with it: +## And, as it turns out, you can also use the reduce meta-operator with it: [Z||] (True, False), (False, False), (False, False); # (True, False) ## And to end the operator list: ## * Sequence operator -# The sequence operator is one of Perl 6's most powerful features: -# it's composed of first, on the left, the list you want Perl 6 to deduce from -# (and might include a closure), and on the right, a value or the predicate -# that says when to stop (or Whatever for a lazy infinite list). +## The sequence operator is one of Perl 6's most powerful features: +## it's composed of first, on the left, the list you want Perl 6 to deduce from +## (and might include a closure), and on the right, a value or the predicate +## that says when to stop (or Whatever for a lazy infinite list). my @list = 1, 2, 3 ... 10; # basic deducing #my @list = 1, 3, 6 ... 10; # this dies because Perl 6 can't figure out the end my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element @@ -1389,175 +1402,189 @@ my @fib = 1, 1, *+* ... *; # lazy infinite list of fibonacci series, # computed using a closure! my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) my @fib = 1, 1, { $^a + $^b } ... *; #(... also equivalent to the above) -# $a and $b will always take the previous values, meaning here -# they'll start with $a = 1 and $b = 1 (values we set by hand). -# then $a = 1 and $b = 2 (result from previous $a+$b), and so on. +## $a and $b will always take the previous values, meaning here +## they'll start with $a = 1 and $b = 1 (values we set by hand). +## then $a = 1 and $b = 2 (result from previous $a+$b), and so on. say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55 # (using a range as the index) -# Note : as for ranges, once reified, elements aren't re-calculated. -# That's why `@primes[^100]` will take a long time the first time you print -# it, then be instant. +## Note : as for ranges, once reified, elements aren't re-calculated. +## That's why `@primes[^100]` will take a long time the first time you print +## it, then be instant. ``` ## Regular Expressions ```perl6 -# I'm sure a lot of you have been waiting for this one. -# Well, now that you know a good deal of Perl 6 already, we can get started. -# First off, you'll have to forget about "PCRE regexps" (perl-compatible regexps). -# -# IMPORTANT: Don't skip them because you know PCRE. They're different. -# Some things are the same (like `?`, `+`, and `*`), -# but sometimes the semantics change (`|`). -# Make sure you read carefully, because you might trip over a new behavior. -# -# Perl 6 has many features related to RegExps. After all, Rakudo parses itself. -# We're first going to look at the syntax itself, -# then talk about grammars (PEG-like), differences between -# `token`, `regex` and `rule` declarators, and some more. -# Side note: you still have access to PCRE regexps using the `:P5` modifier. -# (we won't be discussing this in this tutorial, however) -# -# In essence, Perl 6 natively implements PEG ("Parsing Expression Grammars"). -# The pecking order for ambiguous parses is determined by a multi-level -# tie-breaking test: -# - Longest token matching. `foo\s+` beats `foo` (by 2 or more positions) -# - Longest literal prefix. `food\w*` beats `foo\w*` (by 1) -# - Declaration from most-derived to less derived grammars -# (grammars are actually classes) -# - Earliest declaration wins +## I'm sure a lot of you have been waiting for this one. +## Well, now that you know a good deal of Perl 6 already, we can get started. +## First off, you'll have to forget about "PCRE regexps" (perl-compatible +## regexps). +## +## IMPORTANT: Don't skip them because you know PCRE. They're different. +## Some things are the same (like `?`, `+`, and `*`), +## but sometimes the semantics change (`|`). +## Make sure you read carefully, because you might trip over a new behavior. +## +## Perl 6 has many features related to RegExps. After all, Rakudo parses itself. +## We're first going to look at the syntax itself, +## then talk about grammars (PEG-like), differences between +## `token`, `regex` and `rule` declarators, and some more. +## Side note: you still have access to PCRE regexps using the `:P5` modifier. +## (we won't be discussing this in this tutorial, however) +## +## In essence, Perl 6 natively implements PEG ("Parsing Expression Grammars"). +## The pecking order for ambiguous parses is determined by a multi-level +## tie-breaking test: +## - Longest token matching. `foo\s+` beats `foo` (by 2 or more positions) +## - Longest literal prefix. `food\w*` beats `foo\w*` (by 1) +## - Declaration from most-derived to less derived grammars +## (grammars are actually classes) +## - Earliest declaration wins say so 'a' ~~ /a/; #=> True say so 'a' ~~ / a /; #=> True # More readable with some spaces! -# In all our examples, we're going to use the smart-matching operator against -# a regexp. We're converting the result using `so`, but in fact, it's -# returning a `Match` object. They know how to respond to list indexing, -# hash indexing, and return the matched string. -# The results of the match are available as `$/` (implicitly lexically-scoped). -# You can also use the capture variables which start at 0: -# `$0`, `$1', `$2`... -# -# You can also note that `~~` does not perform start/end checking -# (meaning the regexp can be matched with just one char of the string), -# we're going to explain later how you can do it. - -# In Perl 6, you can have any alphanumeric as a literal, -# everything else has to be escaped, using a backslash or quotes. -say so 'a|b' ~~ / a '|' b /; # `True`. Wouldn't mean the same if `|` wasn't escaped -say so 'a|b' ~~ / a \| b /; # `True`. Another way to escape it. - -# The whitespace in a regexp is actually not significant, -# unless you use the `:s` (`:sigspace`, significant space) adverb. +## In all our examples, we're going to use the smart-matching operator against +## a regexp. We're converting the result using `so`, but in fact, it's +## returning a `Match` object. They know how to respond to list indexing, +## hash indexing, and return the matched string. +## The results of the match are available as `$/` (implicitly lexically-scoped). +## You can also use the capture variables which start at 0: +## `$0`, `$1', `$2`... +## +## You can also note that `~~` does not perform start/end checking +## (meaning the regexp can be matched with just one char of the string), +## we're going to explain later how you can do it. + +## In Perl 6, you can have any alphanumeric as a literal, +## everything else has to be escaped, using a backslash or quotes. +say so 'a|b' ~~ / a '|' b /; # `True`. Wouldn't mean the same if `|` wasn't + # escaped +say so 'a|b' ~~ / a \| b /; # `True`. Another way to escape it. + +## The whitespace in a regexp is actually not significant, +## unless you use the `:s` (`:sigspace`, significant space) adverb. say so 'a b c' ~~ / a b c /; #> `False`. Space is not significant here say so 'a b c' ~~ /:s a b c /; #> `True`. We added the modifier `:s` here. -# If we use only one space between strings in a regex, Perl 6 will warn us: -say so 'a b c' ~~ / a b c /; #> 'False' #> Space is not significant here; please -# use quotes or :s (:sigspace) modifier (or, to suppress this warning, omit the -# space, or otherwise change the spacing) -# To fix this and make the spaces less ambiguous, either use at least two -# spaces between strings or use the `:s` adverb. - -# As we saw before, we can embed the `:s` inside the slash delimiters, but we can -# also put it outside of them if we specify `m` for 'match': +## If we use only one space between strings in a regex, Perl 6 will warn us: +say so 'a b c' ~~ / a b c /; #> 'False' #> Space is not significant here; +## please use quotes or :s (:sigspace) modifier (or, to suppress this warning, +## omit the space, or otherwise change the spacing) +## To fix this and make the spaces less ambiguous, either use at least two +## spaces between strings or use the `:s` adverb. + +## As we saw before, we can embed the `:s` inside the slash delimiters, but we +## can also put it outside of them if we specify `m` for 'match': say so 'a b c' ~~ m:s/a b c/; #> `True` -# By using `m` to specify 'match' we can also use delimiters other than slashes: +## By using `m` to specify 'match', we can also use delimiters other +## than slashes: say so 'abc' ~~ m{a b c}; #> `True` -# Use the :i adverb to specify case insensitivity: + +## Use the :i adverb to specify case insensitivity: say so 'ABC' ~~ m:i{a b c}; #> `True` -# It is, however, important as for how modifiers (that you're gonna see just below) -# are applied ... + +## It is, however, important as for how modifiers (that you're gonna see just +## below) are applied ... ## Quantifying - `?`, `+`, `*` and `**`. -# - `?` - 0 or 1 +## - `?` - 0 or 1 so 'ac' ~~ / a b c /; # `False` so 'ac' ~~ / a b? c /; # `True`, the "b" matched 0 times. so 'abc' ~~ / a b? c /; # `True`, the "b" matched 1 time. -# ... As you read just before, whitespace is important because it determines -# which part of the regexp is the target of the modifier: +## ... As you read just before, whitespace is important because it determines +## which part of the regexp is the target of the modifier: so 'def' ~~ / a b c? /; # `False`. Only the `c` is optional so 'def' ~~ / a b? c /; # `False`. Whitespace is not significant so 'def' ~~ / 'abc'? /; # `True`. The whole "abc" group is optional. -# Here (and below) the quantifier applies only to the `b` +## Here (and below) the quantifier applies only to the `b` -# - `+` - 1 or more +## - `+` - 1 or more so 'ac' ~~ / a b+ c /; # `False`; `+` wants at least one matching so 'abc' ~~ / a b+ c /; # `True`; one is enough so 'abbbbc' ~~ / a b+ c /; # `True`, matched 4 "b"s -# - `*` - 0 or more +## - `*` - 0 or more so 'ac' ~~ / a b* c /; # `True`, they're all optional. so 'abc' ~~ / a b* c /; # `True` so 'abbbbc' ~~ / a b* c /; # `True` so 'aec' ~~ / a b* c /; # `False`. "b"(s) are optional, not replaceable. -# - `**` - (Unbound) Quantifier -# If you squint hard enough, you might understand -# why exponentation is used for quantity. +## - `**` - (Unbound) Quantifier +## If you squint hard enough, you might understand +## why exponentation is used for quantity. so 'abc' ~~ / a b**1 c /; # `True` (exactly one time) so 'abc' ~~ / a b**1..3 c /; # `True` (one to three times) so 'abbbc' ~~ / a b**1..3 c /; # `True` so 'abbbbbbc' ~~ / a b**1..3 c /; # `False` (too much) so 'abbbbbbc' ~~ / a b**3..* c /; # `True` (infinite ranges are okay) -# - `<[]>` - Character classes -# Character classes are the equivalent of PCRE's `[]` classes, but -# they use a more perl6-ish syntax: +## - `<[]>` - Character classes +## Character classes are the equivalent of PCRE's `[]` classes, but +## they use a more perl6-ish syntax: say 'fooa' ~~ / f <[ o a ]>+ /; #=> 'fooa' -# You can use ranges: + +## You can use ranges: say 'aeiou' ~~ / a <[ e..w ]> /; #=> 'ae' -# Just like in normal regexes, if you want to use a special character, escape it -# (the last one is escaping a space) + +## Just like in normal regexes, if you want to use a special character, +## escape it (the last one is escaping a space) say 'he-he !' ~~ / 'he-' <[ a..z \! \ ]> + /; #=> 'he-he !' -# You'll get a warning if you put duplicate names -# (which has the nice effect of catching the wrote quoting:) -'he he' ~~ / <[ h e ' ' ]> /; # Warns "Repeated characters found in characters class" -# You can also negate them ... (equivalent to `[^]` in PCRE) +## You'll get a warning if you put duplicate names +## (which has the nice effect of catching the wrote quoting:) +'he he' ~~ / <[ h e ' ' ]> /; # Warns "Repeated characters found in characters + # class" + +## You can also negate them ... (equivalent to `[^]` in PCRE) so 'foo' ~~ / <-[ f o ]> + /; # False -# ... and compose them: : -so 'foo' ~~ / <[ a..z ] - [ f o ]> + /; # False (any letter except f and o) -so 'foo' ~~ / <-[ a..z ] + [ f o ]> + /; # True (no letter except f and o) -so 'foo!' ~~ / <-[ a..z ] + [ f o ]> + /; # True (the + doesn't replace the left part) +## ... and compose them: : +so 'foo' ~~ / <[ a..z ] - [ f o ]> + /; # False (any letter except f and o) +so 'foo' ~~ / <-[ a..z ] + [ f o ]> + /; # True (no letter except f and o) +so 'foo!' ~~ / <-[ a..z ] + [ f o ]> + /; # True (the + doesn't replace the + # left part) ``` ### Grouping and capturing ```perl6 -# Group: you can group parts of your regexp with `[]`. -# These groups are *not* captured (like PCRE's `(?:)`). +## Group: you can group parts of your regexp with `[]`. +## These groups are *not* captured (like PCRE's `(?:)`). so 'abc' ~~ / a [ b ] c /; # `True`. The grouping does pretty much nothing so 'foo012012bar' ~~ / foo [ '01' <[0..9]> ] + bar /; -# The previous line returns `True`. -# We match the "012" 1 or more time (the `+` was applied to the group). - -# But this does not go far enough, because we can't actually get back what -# we matched. -# Capture: We can actually *capture* the results of the regexp, using parentheses. -so 'fooABCABCbar' ~~ / foo ( 'A' <[A..Z]> 'C' ) + bar /; # `True`. (using `so` here, `$/` below) - -# So, starting with the grouping explanations. -# As we said before, our `Match` object is available as `$/`: -say $/; # Will print some weird stuff (we'll explain) (or "Nil" if nothing matched). - -# As we also said before, it has array indexing: +## The previous line returns `True`. +## We match the "012" 1 or more time (the `+` was applied to the group). + +## But this does not go far enough, because we can't actually get back what +## we matched. +## Capture: We can actually *capture* the results of the regexp, +## using parentheses. +so 'fooABCABCbar' ~~ / foo ( 'A' <[A..Z]> 'C' ) + bar /; # `True`. (using `so` + # here, `$/` below) + +## So, starting with the grouping explanations. +## As we said before, our `Match` object is available as `$/`: +say $/; # Will print some weird stuff (we'll explain) (or "Nil" if + # nothing matched). + +## As we also said before, it has array indexing: say $/[0]; #=> 「ABC」 「ABC」 # These weird brackets are `Match` objects. # Here, we have an array of these. say $0; # The same as above. -# Our capture is `$0` because it's the first and only one capture in the regexp. -# You might be wondering why it's an array, and the answer is simple: -# Some capture (indexed using `$0`, `$/[0]` or a named one) will be an array -# IFF it can have more than one element -# (so, with `*`, `+` and `**` (whatever the operands), but not with `?`). -# Let's use examples to see that: +## Our capture is `$0` because it's the first and only one capture in the +## regexp. You might be wondering why it's an array, and the answer is simple: +## Some capture (indexed using `$0`, `$/[0]` or a named one) will be an array +## IFF it can have more than one element +## (so, with `*`, `+` and `**` (whatever the operands), but not with `?`). +## Let's use examples to see that: -# Note: We quoted A B C to demonstrate that the whitespace between them isn't significant. -# If we want the whitespace to *be* significant there, we can use the :sigspace modifier. +## Note: We quoted A B C to demonstrate that the whitespace between them isn't +## significant. If we want the whitespace to *be* significant there, we +## can use the :sigspace modifier. so 'fooABCbar' ~~ / foo ( "A" "B" "C" )? bar /; # `True` say $/[0]; #=> 「ABC」 say $0.WHAT; #=> (Match) @@ -1570,16 +1597,16 @@ say $0.WHAT; #=> (Array) # A specific quantifier will always capture an Array, # may it be a range or a specific value (even 1). -# The captures are indexed per nesting. This means a group in a group will be nested -# under its parent group: `$/[0][0]`, for this code: +## The captures are indexed per nesting. This means a group in a group will be +## nested under its parent group: `$/[0][0]`, for this code: 'hello-~-world' ~~ / ( 'hello' ( <[ \- \~ ]> + ) ) 'world' /; say $/[0].Str; #=> hello~ say $/[0][0].Str; #=> ~ -# This stems from a very simple fact: `$/` does not contain strings, integers or arrays, -# it only contains match objects. These contain the `.list`, `.hash` and `.Str` methods. -# (but you can also just use `match<key>` for hash access -# and `match[idx]` for array access) +## This stems from a very simple fact: `$/` does not contain strings, integers +## or arrays, it only contains match objects. These contain the `.list`, `.hash` +## and `.Str` methods. (but you can also just use `match<key>` for hash access +## and `match[idx]` for array access) say $/[0].list.perl; #=> (Match.new(...),).list # We can see it's a list of Match objects. Those contain # a bunch of infos: where the match started/ended, @@ -1587,82 +1614,84 @@ say $/[0].list.perl; #=> (Match.new(...),).list # You'll see named capture below with grammars. ## Alternatives - the `or` of regexps -# WARNING: They are DIFFERENT from PCRE regexps. +## WARNING: They are DIFFERENT from PCRE regexps. so 'abc' ~~ / a [ b | y ] c /; # `True`. Either "b" or "y". so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ... -# The difference between this `|` and the one you're used to is LTM. -# LTM means "Longest Token Matching". This means that the engine will always -# try to match as much as possible in the strng +## The difference between this `|` and the one you're used to is LTM. +## LTM means "Longest Token Matching". This means that the engine will always +## try to match as much as possible in the strng 'foo' ~~ / fo | foo /; # `foo`, because it's longer. -# To decide which part is the "longest", it first splits the regex in two parts: -# The "declarative prefix" (the part that can be statically analyzed) -# and the procedural parts. -# Declarative prefixes include alternations (`|`), conjuctions (`&`), -# sub-rule calls (not yet introduced), literals, characters classes and quantifiers. -# The latter include everything else: back-references, code assertions, -# and other things that can't traditionnaly be represented by normal regexps. -# -# Then, all the alternatives are tried at once, and the longest wins. -# Examples: -# DECLARATIVE | PROCEDURAL +## To decide which part is the "longest", it first splits the regex in +## two parts: +## The "declarative prefix" (the part that can be statically analyzed) +## and the procedural parts. +## Declarative prefixes include alternations (`|`), conjunctions (`&`), +## sub-rule calls (not yet introduced), literals, characters classes and +## quantifiers. +## The latter include everything else: back-references, code assertions, +## and other things that can't traditionnaly be represented by normal regexps. +## +## Then, all the alternatives are tried at once, and the longest wins. +## Examples: +## DECLARATIVE | PROCEDURAL / 'foo' \d+ [ <subrule1> || <subrule2> ] /; -# DECLARATIVE (nested groups are not a problem) +## DECLARATIVE (nested groups are not a problem) / \s* [ \w & b ] [ c | d ] /; -# However, closures and recursion (of named regexps) are procedural. -# ... There are also more complicated rules, like specificity -# (literals win over character classes) +## However, closures and recursion (of named regexps) are procedural. +## ... There are also more complicated rules, like specificity +## (literals win over character classes) -# Note: the first-matching `or` still exists, but is now spelled `||` +## Note: the first-matching `or` still exists, but is now spelled `||` 'foo' ~~ / fo || foo /; # `fo` now. ``` ## Extra: the MAIN subroutine ```perl6 -# The `MAIN` subroutine is called when you run a Perl 6 file directly. -# It's very powerful, because Perl 6 actually parses the arguments -# and pass them as such to the sub. It also handles named argument (`--foo`) -# and will even go as far as to autogenerate a `--help` +## The `MAIN` subroutine is called when you run a Perl 6 file directly. +## It's very powerful, because Perl 6 actually parses the arguments +## and pass them as such to the sub. It also handles named argument (`--foo`) +## and will even go as far as to autogenerate a `--help` sub MAIN($name) { say "Hello, $name !" } -# This produces: -# $ perl6 cli.pl -# Usage: -# t.pl <name> - -# And since it's a regular Perl 6 sub, you can haz multi-dispatch: -# (using a "Bool" for the named argument so that we can do `--replace` -# instead of `--replace=1`) +## This produces: +## $ perl6 cli.pl +## Usage: +## t.pl <name> + +## And since it's a regular Perl 6 sub, you can have multi-dispatch: +## (using a "Bool" for the named argument so that we can do `--replace` +## instead of `--replace=1`) subset File of Str where *.IO.d; # convert to IO object to check the file exists multi MAIN('add', $key, $value, Bool :$replace) { ... } multi MAIN('remove', $key) { ... } multi MAIN('import', File, Str :$as) { ... } # omitting parameter name -# This produces: -# $ perl6 cli.pl -# Usage: -# t.pl [--replace] add <key> <value> -# t.pl remove <key> -# t.pl [--as=<Str>] import (File) -# As you can see, this is *very* powerful. -# It even went as far as to show inline the constants. -# (the type is only displayed if the argument is `$`/is named) +## This produces: +## $ perl6 cli.pl +## Usage: +## t.pl [--replace] add <key> <value> +## t.pl remove <key> +## t.pl [--as=<Str>] import (File) +## As you can see, this is *very* powerful. +## It even went as far as to show inline the constants. +## (the type is only displayed if the argument is `$`/is named) ``` ## APPENDIX A: ### List of things ```perl6 -# It's considered by now you know the Perl6 basics. -# This section is just here to list some common operations, -# but which are not in the "main part" of the tutorial to bloat it up +## It's considered by now you know the Perl6 basics. +## This section is just here to list some common operations, +## but which are not in the "main part" of the tutorial to bloat it up ## Operators ## * Sort comparison -# They return one value of the `Order` enum : `Less`, `Same` and `More` -# (which numerify to -1, 0 or +1). +## They return one value of the `Order` enum : `Less`, `Same` and `More` +## (which numerify to -1, 0 or +1). 1 <=> 4; # sort comparison for numerics 'a' leg 'b'; # sort comparison for string $obj eqv $obj2; # sort comparison using eqv semantics @@ -1672,20 +1701,20 @@ $obj eqv $obj2; # sort comparison using eqv semantics 'b' after 'a'; # True ## * Short-circuit default operator -# Like `or` and `||`, but instead returns the first *defined* value : +## Like `or` and `||`, but instead returns the first *defined* value : say Any // Nil // 0 // 5; #=> 0 ## * Short-circuit exclusive or (XOR) -# Returns `True` if one (and only one) of its arguments is true +## Returns `True` if one (and only one) of its arguments is true say True ^^ False; #=> True ## * Flip Flop -# The flip flop operators (`ff` and `fff`, equivalent to P5's `..`/`...`). -# are operators that take two predicates to test: -# They are `False` until their left side returns `True`, then are `True` until -# their right side returns `True`. -# Like for ranges, you can exclude the iteration when it became `True`/`False` -# by using `^` on either side. -# Let's start with an example : +## The flip flop operators (`ff` and `fff`, equivalent to P5's `..`/`...`). +## are operators that take two predicates to test: +## They are `False` until their left side returns `True`, then are `True` until +## their right side returns `True`. +## Like for ranges, you can exclude the iteration when it became `True`/`False` +## by using `^` on either side. +## Let's start with an example : for <well met young hero we shall meet later> { # by default, `ff`/`fff` smart-match (`~~`) against `$_`: if 'met' ^ff 'meet' { # Won't enter the if for "met" @@ -1697,35 +1726,36 @@ for <well met young hero we shall meet later> { say "This ... probably will never run ..."; } } -# This will print "young hero we shall meet" (excluding "met"): -# the flip-flop will start returning `True` when it first encounters "met" -# (but will still return `False` for "met" itself, due to the leading `^` -# on `ff`), until it sees "meet", which is when it'll start returning `False`. - -# The difference between `ff` (awk-style) and `fff` (sed-style) is that -# `ff` will test its right side right when its left side changes to `True`, -# and can get back to `False` right away -# (*except* it'll be `True` for the iteration that matched) - -# While `fff` will wait for the next iteration to -# try its right side, once its left side changed: +## This will print "young hero we shall meet" (excluding "met"): +## the flip-flop will start returning `True` when it first encounters "met" +## (but will still return `False` for "met" itself, due to the leading `^` +## on `ff`), until it sees "meet", which is when it'll start returning `False`. + +## The difference between `ff` (awk-style) and `fff` (sed-style) is that +## `ff` will test its right side right when its left side changes to `True`, +## and can get back to `False` right away +## (*except* it'll be `True` for the iteration that matched) - +## While `fff` will wait for the next iteration to +## try its right side, once its left side changed: .say if 'B' ff 'B' for <A B C B A>; #=> B B # because the right-hand-side was tested # directly (and returned `True`). - # "B"s are printed since it matched that time - # (it just went back to `False` right away). + # "B"s are printed since it matched that + # time (it just went back to `False` + # right away). .say if 'B' fff 'B' for <A B C B A>; #=> B C B # The right-hand-side wasn't tested until # `$_` became "C" # (and thus did not match instantly). -# A flip-flop can change state as many times as needed: +## A flip-flop can change state as many times as needed: for <test start print it stop not printing start print again stop not anymore> { .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", #=> "print it print again" } -# you might also use a Whatever Star, -# which is equivalent to `True` for the left side or `False` for the right: +## You might also use a Whatever Star, +## which is equivalent to `True` for the left side or `False` for the right: for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here # (sometimes called "superstitious parentheses") .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50, @@ -1733,8 +1763,8 @@ for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here #=> 60 3 40 60 } -# You can also use this property to create an `If` -# that'll not go through the first time : +## You can also use this property to create an `If` +## that'll not go through the first time : for <a b c> { .say if * ^ff *; # the flip-flop is `True` and never goes back to `False`, # but the `^` makes it *not run* on the first iteration @@ -1742,8 +1772,8 @@ for <a b c> { } -# - `===` is value identity and uses `.WHICH` on the objects to compare them -# - `=:=` is container identity and uses `VAR()` on the objects to compare them +## - `===` is value identity and uses `.WHICH` on the objects to compare them +## - `=:=` is container identity and uses `VAR()` on the objects to compare them ``` @@ -1754,13 +1784,16 @@ If you want to go further, you can: This will give you a dropdown menu of all the pages referencing your search term (Much better than using Google to find Perl 6 documents!) - Read the [Perl 6 Advent Calendar](http://perl6advent.wordpress.com/). This - is a great source of Perl 6 snippets and explainations. If the docs don't + is a great source of Perl 6 snippets and explanations. If the docs don't describe something well enough, you may find more detailed information here. This information may be a bit older but there are many great examples and - explainations. Posts stopped at the end of 2015 when the language was declared + explanations. Posts stopped at the end of 2015 when the language was declared stable and Perl 6.c was released. - - Come along on `#perl6` at `irc.freenode.net`. The folks here are always helpful. - - Check the [source of Perl 6's functions and classes](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo is mainly written in Perl 6 (with a lot of NQP, "Not Quite Perl", a Perl 6 subset easier to implement and optimize). - - Read [the language design documents](http://design.perl6.org). They explain P6 from an implementor point-of-view, but it's still very interesting. - - [//]: # ( vim: set filetype=perl softtabstop=2 shiftwidth=2 expandtab cc=80 : ) + - Come along on `#perl6` at `irc.freenode.net`. The folks here are + always helpful. + - Check the [source of Perl 6's functions and + classes](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo is + mainly written in Perl 6 (with a lot of NQP, "Not Quite Perl", a Perl 6 subset + easier to implement and optimize). + - Read [the language design documents](http://design.perl6.org). They explain + P6 from an implementor point-of-view, but it's still very interesting. diff --git a/php.html.markdown b/php.html.markdown index f247ba77..d4fbaa32 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -122,9 +122,9 @@ echo 'Multiple', 'Parameters', 'Valid'; // Returns 'MultipleParametersValid' // followed by any number of letters, numbers, or underscores. define("FOO", "something"); -// access to a constant is possible by calling the choosen name without a $ +// access to a constant is possible by calling the chosen name without a $ echo FOO; // Returns 'something' -echo 'This outputs ' . FOO; // Returns 'This ouputs something' +echo 'This outputs ' . FOO; // Returns 'This outputs something' @@ -132,9 +132,7 @@ echo 'This outputs ' . FOO; // Returns 'This ouputs something' * Arrays */ -// All arrays in PHP are associative arrays (hashmaps), - -// Associative arrays, known as hashmaps in some languages. +// All arrays in PHP are associative arrays (hashmaps in some languages) // Works with all PHP versions $associative = array('One' => 1, 'Two' => 2, 'Three' => 3); @@ -839,11 +837,11 @@ try { // Handle exception } -// When using try catch blocks in a namespaced enviroment use the following +// When using try catch blocks in a namespaced environment use the following try { // Do something -} catch (\Exception $e) { +} catch (Exception $e) { // Handle exception } @@ -856,7 +854,7 @@ try { $condition = true; if ($condition) { - throw new MyException('Something just happend'); + throw new MyException('Something just happened'); } } catch (MyException $e) { diff --git a/pl-pl/bf-pl.html.markdown b/pl-pl/bf-pl.html.markdown index 801f1a9a..88f928cf 100644 --- a/pl-pl/bf-pl.html.markdown +++ b/pl-pl/bf-pl.html.markdown @@ -1,11 +1,14 @@ --- +category: language language: bf +filename: learnbf-pl.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] translators: - ["Jakub Młokosiewicz", "https://github.com/hckr"] lang: pl-pl + --- Brainfuck (pisane małymi literami, za wyjątkiem początku zdania) jest bardzo diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index 3a51ade5..236b8958 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -1,8 +1,13 @@ --- +category: language +filename: haskell-pl.hs language: Haskell -lang: pl-pl contributors: + - ["Adit Bhargava", "http://adit.io"] +translators: - ["Remigiusz Suwalski", "https://github.com/remigiusz-suwalski"] +lang: pl-pl + --- Haskell został zaprojektowany jako praktyczny, czysto funkcyjny język diff --git a/pl-pl/json-pl.html.markdown b/pl-pl/json-pl.html.markdown index 872455de..edd059bf 100644 --- a/pl-pl/json-pl.html.markdown +++ b/pl-pl/json-pl.html.markdown @@ -1,6 +1,6 @@ --- +category: language language: json -filename: learnjson-pl.json contributors: - ["Anna Harren", "https://github.com/iirelu"] - ["Marco Scannadinari", "https://github.com/marcoms"] @@ -9,6 +9,7 @@ contributors: translators: - ["Michał Mitrosz", "https://github.com/Voltinus"] lang: pl-pl +filename: learnjson-pl.json --- JSON to bardzo prosty format wymiany danych. Jak jest napisane na [json.org](http://json.org), jest łatwy do pisania i czytania dla ludzi i do parsowania i generowania dla maszyn. diff --git a/pl-pl/perl-pl.html.markdown b/pl-pl/perl-pl.html.markdown index 029ca006..3e27cc4f 100644 --- a/pl-pl/perl-pl.html.markdown +++ b/pl-pl/perl-pl.html.markdown @@ -2,11 +2,14 @@ name: perl category: language language: perl -filename: learnperl.pl +filename: learnperl-pl.pm contributors: - ["Korjavin Ivan", "http://github.com/korjavin"] + - ["Dan Book", "http://github.com/Grinnz"] +translators: - ["Michał Kupczyński", "http://github.com/ukoms"] lang: pl-pl + --- Perl 5 jest wysoce użytecznym, bogatym w wiele opcji językiem programowania diff --git a/powershell.html.markdown b/powershell.html.markdown index bd46051c..5a5050b4 100644 --- a/powershell.html.markdown +++ b/powershell.html.markdown @@ -244,7 +244,7 @@ function New-Website() { 'string' | gm # Syntax for calling static .NET methods -[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') +\[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') # Note that .NET functions MUST be called with parentheses # while PS functions CANNOT be called with parentheses. diff --git a/prolog.html.markdown b/prolog.html.markdown new file mode 100644 index 00000000..f7b55ac6 --- /dev/null +++ b/prolog.html.markdown @@ -0,0 +1,339 @@ +--- +language: prolog +filename: learnprolog.pl +contributors: + - ["hyphz", "http://github.com/hyphz/"] +--- + +Prolog is a logic programming language first specified in 1972, and refined into multiple modern implementations. + +``` +% This is a comment. + +% Prolog treats code entered in interactive mode differently +% to code entered in a file and loaded ("consulted"). +% This code must be loaded from a file to work as intended. +% Lines that begin with ?- can be typed in interactive mode. +% A bunch of errors and warnings will trigger when you load this file +% due to the examples which are supposed to fail - they can be safely +% ignored. + +% Output is based on SWI-prolog 7.2.3. Different Prologs may behave +% differently. + +% Prolog is based on the ideal of logic programming. +% A subprogram (called a predicate) represents a state of the world. +% A command (called a goal) tells Prolog to make that state of the world +% come true, if possible. + +% As an example, here is a definition of the simplest kind of predicate: +% a fact. + +magicNumber(7). +magicNumber(9). +magicNumber(42). + +% This introduces magicNumber as a predicate and says that it is true +% with parameter 7, 9, or 42, but no other parameter. Note that +% predicate names must start with lower case letters. We can now use +% interactive mode to ask if it is true for different values: + +?- magicNumber(7). % True +?- magicNumber(8). % False +?- magicNumber(9). % True + +% Some older Prologs may display "Yes" and "No" instead of True and +% False. + +% What makes Prolog unusual is that we can also tell Prolog to _make_ +% magicNumber true, by passing it an undefined variable. Any name +% starting with a capital letter is a variable in Prolog. + +?- magicNumber(Presto). % Presto = 7 ; + % Presto = 9 ; + % Presto = 42. + +% Prolog makes magicNumber true by assigning one of the valid numbers to +% the undefined variable Presto. By default it assigns the first one, 7. +% By pressing ; in interactive mode you can reject that solution and +% force it to assign the next one, 9. Pressing ; again forces it to try +% the last one, 42, after which it no longer accepts input because this +% is the last solution. You can accept an earlier solution by pressing . +% instead of ;. + +% This is Prolog's central operation: unification. Unification is +% essentially a combination of assignment and equality! It works as +% follows: +% If both sides are bound (ie, defined), check equality. +% If one side is free (ie, undefined), assign to match the other side. +% If both sides are free, the assignment is remembered. With some luck, +% one of the two sides will eventually be bound, but this isn't +% necessary. +% +% The = sign in Prolog represents unification, so: + +?- 2 = 3. % False - equality test +?- X = 3. % X = 3 - assignment +?- X = 2, X = Y. % X = Y = 2 - two assignments + % Note Y is assigned to, even though it is + % on the right hand side, because it is free +?- X = 3, X = 2. % False + % First acts as assignment and binds X=3 + % Second acts as equality because X is bound + % Since 3 does not equal 2, gives False + % Thus in Prolog variables are immutable +?- X = 3+2. % X = 3+2 - unification can't do arithmetic +?- X is 3+2. % X = 5 - "is" does arithmetic. +?- 5 = X+2. % This is why = can't do arithmetic - + % because Prolog can't solve equations +?- 5 is X+2. % Error. Unlike =, the right hand side of IS + % must always be bound, thus guaranteeing + % no attempt to solve an equation. +?- X = Y, X = 2, Z is Y + 3. % X = Y, Y = 2, Z = 5. + % X = Y are both free, so Prolog remembers + % it. Therefore assigning X will also + % assign Y. + +% Any unification, and thus any predicate in Prolog, can either: +% Succeed (return True) without changing anything, +% because an equality-style unification was true +% Succeed (return True) and bind one or more variables in the process, +% because an assignment-style unification was made true +% or Fail (return False) +% because an equality-style unification was false +% (Failure can never bind variables) + +% The ideal of being able to give any predicate as a goal and have it +% made true is not always possible, but can be worked toward. For +% example, Prolog has a built in predicate plus which represents +% arithmetic addition but can reverse simple additions. + +?- plus(1, 2, 3). % True +?- plus(1, 2, X). % X = 3 because 1+2 = X. +?- plus(1, X, 3). % X = 2 because 1+X = 3. +?- plus(X, 2, 3). % X = 1 because X+2 = 3. +?- plus(X, 5, Y). % Error - although this could be solved, + % the number of solutions is infinite, + % which most predicates try to avoid. + +% When a predicate such as magicNumber can give several solutions, the +% overall compound goal including it may have several solutions too. + +?- magicNumber(X), plus(X,Y,100). % X = 7, Y = 93 ; + % X = 9, Y = 91 ; + % X = 42, Y = 58 . +% Note: on this occasion it works to pass two variables to plus because +% only Y is free (X is bound by magicNumber). + +% However, if one of the goals is fully bound and thus acts as a test, +% then solutions which fail the test are rejected. +?- magicNumber(X), X > 40. % X = 42 +?- magicNumber(X), X > 100. % False + +% To see how Prolog actually handles this, let's introduce the print +% predicate. Print always succeeds, never binds any variables, and +% prints out its parameter as a side effect. + +?- print("Hello"). % "Hello" true. +?- X = 2, print(X). % 2 true. +?- X = 2, print(X), X = 3. % 2 false - print happens immediately when + % it is encountered, even though the overall + % compound goal fails (because 2 != 3, + % see the example above). + +% By using Print we can see what actually happens when we give a +% compound goal including a test that sometimes fails. +?- magicNumber(X), print(X), X > 40. % 7 9 42 X = 42 . + +% MagicNumber(X) unifies X with its first possibility, 7. +% Print(X) prints out 7. +% X > 40 tests if 7 > 40. It is not, so it fails. +% However, Prolog remembers that magicNumber(X) offered multiple +% solutions. So it _backtracks_ to that point in the code to try +% the next solution, X = 9. +% Having backtracked it must work through the compound goal +% again from that point including the Print(X). So Print(X) prints out +% 9. +% X > 40 tests if 9 > 40 and fails again. +% Prolog remembers that magicNumber(X) still has solutions and +% backtracks. Now X = 42. +% It works through the Print(X) again and prints 42. +% X > 40 tests if 42 > 40 and succeeds so the result bound to X +% The same backtracking process is used when you reject a result at +% the interactive prompt by pressing ;, for example: + +?- magicNumber(X), print(X), X > 8. % 7 9 X = 9 ; + % 42 X = 42. + +% As you saw above we can define our own simple predicates as facts. +% More complex predicates are defined as rules, like this: + +nearby(X,Y) :- X = Y. +nearby(X,Y) :- Y is X+1. +nearby(X,Y) :- Y is X-1. + +% nearby(X,Y) is true if Y is X plus or minus 1. +% However this predicate could be improved. Here's why: + +?- nearby(2,3). % True ; False. +% Because we have three possible definitions, Prolog sees this as 3 +% possibilities. X = Y fails, so Y is X+1 is then tried and succeeds, +% giving the True answer. But Prolog still remembers there are more +% possibilities for nearby() (in Prolog terminology, "it has a +% choice point") even though "Y is X-1" is doomed to fail, and gives us +% the option of rejecting the True answer, which doesn't make a whole +% lot of sense. + +?- nearby(4, X). % X = 4 ; + % X = 5 ; + % X = 3. Great, this works +?- nearby(X, 4). % X = 4 ; + % error +% After rejecting X = 4 prolog backtracks and tries "Y is X+1" which is +% "4 is X+1" after substitution of parameters. But as we know from above +% "is" requires its argument to be fully instantiated and it is not, so +% an error occurs. + +% One way to solve the first problem is to use a construct called the +% cut, !, which does nothing but which cannot be backtracked past. + +nearbychk(X,Y) :- X = Y, !. +nearbychk(X,Y) :- Y is X+1, !. +nearbychk(X,Y) :- Y is X-1. + +% This solves the first problem: +?- nearbychk(2,3). % True. + +% But unfortunately it has consequences: +?- nearbychk(2,X). % X = 2. +% Because Prolog cannot backtrack past the cut after X = Y, it cannot +% try the possibilities "Y is X+1" and "Y is X-1", so it only generates +% one solution when there should be 3. +% However if our only interest is in checking if numbers are nearby, +% this may be all we need, thus the name nearbychk. +% This structure is used in Prolog itself from time to time (for example +% in list membership). + +% To solve the second problem we can use built-in predicates in Prolog +% to verify if a parameter is bound or free and adjust our calculations +% appropriately. +nearby2(X,Y) :- nonvar(X), X = Y. +nearby2(X,Y) :- nonvar(X), Y is X+1. +nearby2(X,Y) :- nonvar(X), Y is X-1. +nearby2(X,Y) :- var(X), nonvar(Y), nearby2(Y,X). + +% We can combine this with a cut in the case where both variables are +% bound, to solve both problems. +nearby3(X,Y) :- nonvar(X), nonvar(Y), nearby2(X,Y), !. +nearby3(X,Y) :- nearby2(X,Y). + +% However when writing a predicate it is not normally necessary to go to +% these lengths to perfectly support every possible parameter +% combination. It suffices to support parameter combinations we need to +% use in the program. It is a good idea to document which combinations +% are supported. In regular Prolog this is informally in structured +% comments, but in some Prolog variants like Visual Prolog and Mercury +% this is mandatory and checked by the compiler. + +% Here is the structured comment declaration for nearby3: + +%! nearby3(+X:Int, +Y:Int) is semideterministic. +%! nearby3(+X:Int, -Y:Int) is multi. +%! nearby3(-X:Int, +Y:Int) is multi. + +% For each variable we list a type. The + or - before the variable name +% indicates if the parameter is bound (+) or free (-). The word after +% "is" describes the behaviour of the predicate: +% semideterministic - can succeed once or fail +% ( Two specific numbers are either nearby or not ) +% multi - can succeed multiple times but cannot fail +% ( One number surely has at least 3 nearby numbers ) +% Other possibilities are: +% det - always succeeds exactly once (eg, print) +% nondet - can succeed multiple times or fail. +% In Prolog these are just structured comments and strictly informal but +% extremely useful. + +% An unusual feature of Prolog is its support for atoms. Atoms are +% essentially members of an enumerated type that are created on demand +% whenever an unquoted non variable value is used. For example: +character(batman). % Creates atom value batman +character(robin). % Creates atom value robin +character(joker). % Creates atom value joker +character(darthVader). % Creates atom value darthVader +?- batman = batman. % True - Once created value is reused +?- batman = batMan. % False - atoms are case sensitive +?- batman = darthVader. % False - atoms are distinct + +% Atoms are popular in examples but were created on the assumption that +% Prolog would be used interactively by end users - they are less +% useful for modern applications and some Prolog variants abolish them +% completely. However they can be very useful internally. + +% Loops in Prolog are classically written using recursion. +% Note that below, writeln is used instead of print because print is +% intended for debugging. + +%! countTo(+X:Int) is deterministic. +%! countUpTo(+Value:Int, +Limit:Int) is deterministic. +countTo(X) :- countUpTo(1,X). +countUpTo(Value, Limit) :- Value = Limit, writeln(Value), !. +countUpTo(Value, Limit) :- Value \= Limit, writeln(Value), + NextValue is Value+1, + countUpTo(NextValue, Limit). + +?- countTo(10). % Outputs 1 to 10 + +% Note the use of multiple declarations in countUpTo to create an +% IF test. If Value = Limit fails the second declaration is run. +% There is also a more elegant syntax. + +%! countUpTo2(+Value:Int, +Limit:Int) is deterministic. +countUpTo2(Value, Limit) :- writeln(Value), + Value = Limit -> true ; ( + NextValue is Value+1, + countUpTo2(NextValue, Limit)). + +?- countUpTo2(1,10). % Outputs 1 to 10 + +% If a predicate returns multiple times it is often useful to loop +% through all the values it returns. Older Prologs used a hideous syntax +% called a "failure-driven loop" to do this, but newer ones use a higher +% order function. + +%! countTo2(+X:Int) is deterministic. +countTo2(X) :- forall(between(1,X,Y),writeln(Y)). + +?- countTo2(10). % Outputs 1 to 10 + +% Lists are given in square brackets. Use memberchk to check membership. +% A group is safe if it doesn't include Joker or does include Batman. + +%! safe(Group:list(atom)) is deterministic. +safe(Group) :- memberchk(joker, Group) -> memberchk(batman, Group) ; true. + +?- safe([robin]). % True +?- safe([joker]). % False +?- safe([joker, batman]). % True + +% The member predicate works like memberchk if both arguments are bound, +% but can accept free variables and thus can be used to loop through +% lists. + +?- member(X, [1,2,3]). % X = 1 ; X = 2 ; X = 3 . +?- forall(member(X,[1,2,3]), + (Y is X+1, writeln(Y))). % 2 3 4 + +% The maplist function can be used to generate lists based on other +% lists. Note that the output list is a free variable, causing an +% undefined value to be passed to plus, which is then bound by +% unification. Also notice the use of currying on the plus predicate - +% it's a 3 argument predicate, but we specify only the first, because +% the second and third are filled in by maplist. + +?- maplist(plus(1), [2,3,4], Output). % Output = [3, 4, 5]. +``` + +##Ready For More? + +* [SWI-Prolog](http://www.swi-prolog.org/) diff --git a/pt-br/amd.html.markdown b/pt-br/amd-pt.html.markdown index 38c1f70f..40c7cd09 100644 --- a/pt-br/amd.html.markdown +++ b/pt-br/amd-pt.html.markdown @@ -141,7 +141,7 @@ require(['jquery', 'coolLibFromBower', 'modules/algunsHelpers'], function($, coo coolLib.facaAlgoDoidoCom(helpers.transform($('#foo'))); }); ``` -Apps baseados em `require.js` geralmente terão u´m único ponto de acesso (`main.js`) que é passado à tag script do `require.js` como um data-attribute. Ele vai ser automaticamente carregado e executado com o carregamento da página: +Apps baseados em `require.js` geralmente terão um único ponto de acesso (`main.js`) que é passado à tag script do `require.js` como um data-attribute. Ele vai ser automaticamente carregado e executado com o carregamento da página: ```html <!DOCTYPE html> diff --git a/pt-br/asciidoc-pt.html.markdown b/pt-br/asciidoc-pt.html.markdown index 75b3c268..b12c0693 100644 --- a/pt-br/asciidoc-pt.html.markdown +++ b/pt-br/asciidoc-pt.html.markdown @@ -87,10 +87,6 @@ Título de seções ===== Nível 4 <h5> -====== Nível 5 <h6> - -======= Nível 6 <h7> - ``` Listas @@ -103,7 +99,7 @@ Para criar uma lista com marcadores use asteriscos. * baz ``` -Para criar uma lista númerada use pontos. +Para criar uma lista numerada use pontos. ``` . item 1 diff --git a/pt-br/asymptotic-notation-pt.html.markdown b/pt-br/asymptotic-notation-pt.html.markdown index 2e299d09..aecc2194 100644 --- a/pt-br/asymptotic-notation-pt.html.markdown +++ b/pt-br/asymptotic-notation-pt.html.markdown @@ -38,7 +38,7 @@ Na primeira seção desse documento, descrevemos como Notação Assintótica ide *f*, *n* como o tamanho da entrada e *f(n)* sendo o tempo de execução. Então, para dado algoritmo *f*, com entrada de tamanho *n*, você terá tempo de execução *f(n)*. Isto resulta em um gráfico onde a coordernada Y é o tempo de execução -, a coordernada X representa o tamanho da entrada e os pontos representao o tempo +, a coordernada X representa o tamanho da entrada e os pontos representam o tempo de execução para dado tamanho de entrada. Você pode representar a função, ou o algoritmo, com Notação Assintótica de várias diff --git a/pt-br/asymptoticnotation-pt.html.markdown b/pt-br/asymptoticnotation-pt.html.markdown deleted file mode 100644 index c5299a11..00000000 --- a/pt-br/asymptoticnotation-pt.html.markdown +++ /dev/null @@ -1,161 +0,0 @@ ---- -category: Algorithms & Data Structures -name: Asymptotic Notation -contributors: - - ["Jake Prather", "http://github.com/JakeHP"] -translators: - - ["Carolina Knoll", "http://github.com/carolinaknoll"] -lang: pt-br ---- - -# Aprenda X em Y minutos -## Onde X=Notação Assintótica - -# Notações Assintóticas -## O que são? - -Notações assintóticas são notações matemáticas que nos permitem analisar tempo de execução -de um algoritmo, identificando o seu comportamento de acordo como o tamanho de entrada para -o algoritmo aumenta. Também é conhecido como taxa de "crescimento" de um algoritmo. O algoritmo -simplesmente se torna incrivelmente lento conforme o seu tamanho aumenta? Será que pode-se na -maior parte manter o seu tempo de execução rápido mesmo quando o tamanho de entrada aumenta? -A notação assintótica nos dá a capacidade de responder a essas perguntas. - -## Além desta, existem outras alternativas para responder a essas perguntas? - -Uma forma seria a de contar o número de operações primitivas em diferentes tamanhos de entrada. -Embora esta seja uma solução válida, a quantidade de trabalho necessário, mesmo para algoritmos -simples, não justifica a sua utilização. - -Outra maneira é a de medir fisicamente a quantidade de tempo que leva para se executar um algoritmo -de diferentes tamanhos. No entanto, a precisão e a relatividade (já que tempos obtidos só teriam -relação à máquina em que eles foram testados) deste método estão ligadas a variáveis ambientais, -tais como especificações de hardware, poder de processamento, etc. - -## Tipos de Notação Assintótica - -Na primeira seção deste documento nós descrevemos como uma notação assintótica identifica o comportamento -de um algoritmo como as alterações de tamanho de entrada (input). Imaginemos um algoritmo como uma função -f, n como o tamanho da entrada, e f (n) sendo o tempo de execução. Assim, para um determinado algoritmo f, -com tamanho de entrada n você obtenha algum tempo de execução resultante f (n). Isto resulta num gráfico, -em que o eixo Y representa o tempo de execução, o eixo X é o tamanho da entrada, e os pontos marcados são -os resultantes da quantidade de tempo para um dado tamanho de entrada. - -Pode-se rotular uma função ou algoritmo com uma notação assintótica de diversas maneiras diferentes. -Dentre seus exemplos, está descrever um algoritmo pelo seu melhor caso, pior caso, ou caso equivalente. -O mais comum é o de analisar um algoritmo pelo seu pior caso. Isso porque você normalmente não avaliaria -pelo melhor caso, já que essas condições não são as que você está planejando. Um bom exemplo disto é o de -algoritmos de ordenação; especificamente, a adição de elementos a uma estrutura de tipo árvore. O melhor -caso para a maioria dos algoritmos pode ser tão simples como uma única operação. No entanto, na maioria -dos casos, o elemento que você está adicionando terá de ser ordenado de forma adequada através da árvore, -o que poderia significar a análise de um ramo inteiro. Este é o pior caso, e é por ele que precisamos seguir. - -### Tipos de funções, limites, e simplificação - -``` -Função Logaritmica - log n -Função Linear - an + b -Função Quadrática - an^2 + bn + c -Função Polinomial - an^z + . . . + an^2 + a*n^1 + a*n^0, onde z é uma constante -Função Exponencial - a^n, onde a é uma constante -``` - -Estas são algumas classificações básicas de crescimento de função usados em várias notações. A lista -começa com a função crescimento mais lento (logarítmica, com tempo de execução mais rápido) e vai até -a mais rápida (exponencial, com tempo de execução mais lento). Observe que 'n', ou nossa entrada, -cresce em cada uma dessas funções, e o resultado claramente aumenta muito mais rapidamente em função -quadrática, polinomial e exponencial, em comparação com a logarítmica e a linear. - -Uma observação de boa importância é que, para as notações a serem discutidas, deve-se fazer o melhor -para utilizar termos mais simples. Isto significa desrespeitar constantes, e simplificar termos de -ordem, porque, como o tamanho da entrada (ou n no nosso f (n) exemplo) aumenta infinitamente (limites -matemáticos), os termos em ordens mais baixas e constantes são de pouca ou nenhuma importância. Dito -isto, se você possui constantes com valor 2^9001, ou alguma outra quantidade ridícula, inimaginável, -perceberá que a simplificação distorcerá a precisão de sua notação. - -Já que nós queremos a forma mais simples, vamos modificar nossas funções um pouco. - -``` -Logaritmica - log n -Linear - n -Quadrática - n^2 -Polinomial - n^z, onde z é uma constante -Exponencial - a^n, onde a é uma constante -``` - -### O Grande-O - -Grande-O, geralmente escrita como O, é uma Notação Assintótica para o pior caso para uma dada função. Digamos -que `f(n)` é o tempo de execução de seu algoritmo, e `g(n)` é uma complexidade de tempo arbitrário que você está -tentando se relacionar com o seu algoritmo. `f(n)` será O(g(n)), se, por qualquer constante real c (c > 0), -`f(n)` <= `c g(n)` para cada tamanho de entrada n (n > 0). - -*Exemplo 1* - -``` -f(n) = 3log n + 100 -g(n) = log n -``` - -É `f(n)` um O(g(n))? -É 3 `log n + 100` igual a O(log n)? -Vamos checar na definição de Grande-O. - -``` -3log n + 100 <= c * log n -``` - -Existe alguma constante c que satisfaça isso para todo n? - -``` -3log n + 100 <= 150 * log n, n > 2 (indefinido em n = 1) -``` - -Sim! A definição de Grande-O foi satisfeita. Sendo assim, `f(n)` é O(g(n)). - -*Exemplo 2* - -``` -f(n) = 3 * n^2 -g(n) = n -``` - -É `f(n)` um O(g(n))? -É `3 * n^2` um O(n)? -Vamos ver na definição de Grande-O. - -``` -3 * n^2 <= c * n -``` - -Existe alguma constante que satisfaça isso para todo n? -Não, não existe. `f(n)` NÃO É O(g(n)). - -### Grande-Omega - -Grande-Omega, comumente escrito como Ω, é uma Notação Assintótica para o melhor caso, ou -uma taxa de crescimento padrão para uma determinada função. - -`f(n)` é Ω(g(n)), se, por qualquer constante c real (c > 0), `f(n)` é >= `c g(n)` para cada -tamanho de entrada n (n > 0). - -Sinta-se livre para pesquisar recursos adicionais e obter mais exemplos sobre este assunto! -Grande-O é a notação primária utilizada para tempo de execução de algoritmos, de modo geral. - -### Notas de Finalização - -É complicado exibir este tipo de assunto de forma tão curta, então é definitivamente recomendado -pesquisar além dos livros e recursos on-line listados. Eles serão capazes de analisar o assunto com -uma profundidade muito maior, além de ter definições e exemplos. Mais sobre onde X="Algoritmos e -Estruturas de Dados" está a caminho: Haverá conteúdo focado na análise de exemplos de códigos reais -em breve. - -## Livros - -* [Algorithms] (http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X) -* [Algorithm Design] (http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651) - -## Recursos Online - -* [MIT] (http://web.mit.edu/16.070/www/lecture/big_o.pdf) -* [KhanAcademy] (https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation) diff --git a/pt-br/awk-pt.html.markdown b/pt-br/awk-pt.html.markdown new file mode 100644 index 00000000..761f5294 --- /dev/null +++ b/pt-br/awk-pt.html.markdown @@ -0,0 +1,376 @@ +--- +language: awk +filename: learnawk-pt.awk +contributors: + - ["Marshall Mason", "http://github.com/marshallmason"] +translators: + - ["Paulo Henrique Rodrigues Pinheiro", "https://github.com/paulohrpinheiro"] +lang: pt-br + +--- + +AWK é uma ferramenta padrão em todos os sistemas UNIX compatíveis com POSIX. É +como um Perl despojado, perfeito para tarefas de processamento de texto e +outras tarefas de script. Possui uma sintaxe C-like, mas sem ponto e vírgula, +gerenciamento manual de memória, ou tipagem estática. Destaca-se no +processamento de texto. Você pode chamá-lo a partir de um shell-script, ou você +pode usá-lo como uma linguagem de script autônomo. + +Por que usar AWK ao invés de Perl? Principalmente porque AWK faz parte do UNIX. +Você pode sempre contar com ele, enquanto o futuro do Perl é indefinido. AWK é +também mais fácil de ler que Perl. Para scripts simples de processamento de +texto, particularmente aqueles que leem arquivos linha por linha e fatiam texto +por delimitadores, AWK é provavelmente a ferramenta certa para a tarefa. + +```awk +#!/usr/bin/awk -f + +# Comentários são assim + +# Programas AWK consistem de uma coleção de padrões e ações. O mais +# importante padrão é chamado BEGIN. Ações estão dentro de blocos +# entre chaves. + +BEGIN { + + # O bloco BEGIN será executado no começo do programa. É onde você coloca + # todo código que prepara a execução, antes que você processe qualquer + # arquivo de texto. Se você não tem arquivos de texto, então pense no + # BEGIN como o ponto principal de entrada. + + # Variáveis são globais. Simplesmente atribua valores ou as use, sem + # necessidade de declarar. + + # Operadores são como em C e similares + a = count + 1 + b = count - 1 + c = count * 1 + d = count / 1 # divisão inteira + e = count % 1 # módulo + f = count ^ 1 # exponenciação + + a += 1 + b -= 1 + c *= 1 + d /= 1 + e %= 1 + f ^= 1 + + # Incrementando e decrementando por um + a++ + b-- + + # Como um operador pré-fixado, retorna o valor incrementado + ++a + --b + + # Perceba, não há pontuação, como ponto-e-vírgula, ao final das declarações + + # Declarações de controle + if (count == 0) + print "Começando com count em 0" + else + print "Como é que é?" + + # Ou você pode usar o operador ternário + print (count == 0) ? "Começando com count em 0" : "Como é que é?" + + # Blocos multilinhas devem usar chaves + while (a < 10) { + print "Concatenação de texto é feita" " com uma série" " de" + " textos separados por espaço" + print a + + a++ + } + + for (i = 0; i < 10; i++) + print "Uma boa opção para um loop de uma linha" + + # Quanto a comparações, eis os padrões: + a < b # Menor que + a <= b # Menor ou igual a + a != b # Não igual + a == b # Igual + a > b # Maior que + a >= b # Maior ou igual a + + # Bem como operadores lógicos + a && b # E + a || b # OU (inclusivo) + + # Em adição, há o utilíssimo operador para expressões regulares + if ("foo" ~ "^fo+$") + print "Fooey!" + if ("boo" !~ "^fo+$") + print "Boo!" + + # Matrizes + arr[0] = "foo" + arr[1] = "bar" + # Infelizmente, não há outra forma para inicializar uma matriz. Apenas + # coloque cada valor em uma linha, como mostrado acima. + + # Você também pode ter matrizes associativas + assoc["foo"] = "bar" + assoc["bar"] = "baz" + + # E matrizes multidimensionais, com algumas limitações que não mencionarei + multidim[0,0] = "foo" + multidim[0,1] = "bar" + multidim[1,0] = "baz" + multidim[1,1] = "boo" + + # Você pode testar a pertinência de um elemento em uma matriz + if ("foo" in assoc) + print "Fooey!" + + # Você pode também usar o operador 'in' para percorrer as chaves de uma + # matriz associativa + for (key in assoc) + print assoc[key] + + # Os argumentos da linha de comando estão em uma matriz especial ARGV + for (argnum in ARGV) + print ARGV[argnum] + + # Você pode remover elementos de uma matriz + # Isso é muito útil para prevenir que o AWK assuma que os argumentos são + # arquivo para ele processar + delete ARGV[1] + + # A quantidade de argumentos passados está na variável ARGC + print ARGC + + # O AWK tem várias funções nativas. Elas estão separadas em três categorias. + # Demonstrarei cada uma delas logo mais abaixo. + + return_value = arithmetic_functions(a, b, c) + string_functions() + io_functions() +} + +# Eis como você deve definir uma função +function arithmetic_functions(a, b, c, d) { + + # Provavelmente a parte mais irritante do AWK é ele não possuir variáveis + # locais. Tudo é global. Para pequenos scripts, isso não é problema, e + # pode até mesmo ser considerado útil, mas para grandes scripts, isso pode + # ser um problema. + + # Mas há como contornar isso (um hack). Os argumentos de função são locais + # para a função e o AWK permite que você defina mais argumentos de função + # do que ele precise. Então, coloque a variável local na declaração de + # função, como eu fiz acima. Como uma convenção, adicione alguns espaços + # extras para distinguir entre parâmetros de função reais e variáveis + # locais. Neste exemplo, a, b e c são parâmetros reais, enquanto d é + # meramente uma variável local. + + # Agora, serão demonstradas as funções aritméticas + + # Muitas implementações AWK possuem algumas funções trigonométricas padrão + localvar = sin(a) + localvar = cos(a) + localvar = atan2(b, a) # arco-tangente de b / a + + # E conteúdo logarítmico + localvar = exp(a) + localvar = log(a) + + # Raiz quadrada + localvar = sqrt(a) + + # Descartando a parte não inteira de um número em ponto flutuante. + localvar = int(5.34) # localvar => 5 + + # Números aleatórios + srand() # Forneça uma semente como argumento. Por padrão, ele usa a hora atual + localvar = rand() # Número aleatório entre 0 e 1. + + # Aqui mostramos como retornar um valor + return localvar +} + +function string_functions( localvar, arr) { + + # Sendo o AWK uma linguagem para processamento de texto, ele possui + # várias funções para manipulação de texto, muitas das quais + # fortemente dependentes de expressões regulares. + + # Procurar e substituir, primeira instância (sub), ou todas (gsub) + # Ambas retornam o número de instâncias substituídas + 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" + + # Localiza um texto que casa com uma expressão regular + # index() faz a mesma coisa, mas não permite uma expressão regular + match(localvar, "t") # => 4, pois 't' é o quarto carácter + + # Separa por delimitador + split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"] + + # Outras coisas úteis + 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) { + + # Você já viu como imprimir + print "Hello world" + + # Também há o printf + printf("%s %d %d %d\n", "Testing", 1, 2, 3) + + # O AWK não disponibiliza manipuladores de arquivo. Ele irá automaticamente + # manipular um arquivo quando você fizer algo que precise disso. O texto + # que você usou para isso pode ser usado como um manipulador de arquivo, + # para propósitos de E/S. Isso faz ele parecer com um shell script: + + print "foobar" >"/tmp/foobar.txt" + + # Agora a string "/tmp/foobar.txt" é um manipulador de arquivos. Você pode + # fechá-lo: + close("/tmp/foobar.txt") + + # Aqui está como você pode executar alguma coisa no shell + system("echo foobar") # => prints foobar + + # Lê uma linha da entrada padrão e armazena em localvar + getline localvar + + # Lê uma linha de um pipe + "echo foobar" | getline localvar # localvar => "foobar" + close("echo foobar") + + # Lê uma linha de um arquivo e armazena em localvar + getline localvar <"/tmp/foobar.txt" + close("/tmp/foobar.txt") +} + +# Como dito no início, os programas AWK consistem de uma coleção de padrões +# e ações. Você já viu o padrão BEGIN, o mais importante. Outros padrões são +# usados apenas se você estiver processando linhas de arquivos ou a entrada +# padrão. + +# Quando você passa argumentos para o AWK, eles são tratados como nomes de +# arquivos para processar. Todos serão processados, em ordem. Pense nisso como +# um implícito para loop, iterando sobre as linhas nesses arquivos. Esses +# padrões e ações são como instruções de mudança dentro do loop. + +/^fo+bar$/ { + + # Esta ação será executada para cada linha que corresponda à expressão + # regular, / ^ fo + bar $ /, e será ignorada para qualquer linha que não + # corresponda. Vamos apenas imprimir a linha: + + print + + # Opa, sem argumento! Isso ocorre pois print tem um argumento padrão: $0. + # $0 é o nome da linha atual que está sendo processada. Essa variável é + # criada automaticamente para você. + + # Você provavelmente pode adivinhar que existem outras variáveis $. Toda + # linha é implicitamente dividida antes de cada ação ser chamada, como + # o shell faz. E, como o shell, cada campo pode ser acessado com um sinal + # de cifrão + + # Isso irá imprimir o segundo e quarto campos da linha + print $2, $4 + + # O AWK automaticamente define muitas outras variáveis para ajudar você + # a inspecionar processar cada linha. A mais importante delas é NF. + + # Imprime o número de campos da linha atual + print NF + + # Imprime o último campo da linha atual + print $NF +} + +# Todo padrão é na verdade um teste verdadeiro/falso. A expressão regular no +# último padrão também é um teste verdadeiro/falso, mas parte dele estava +# escondido. Se você não informar um texto para testar, AWK assumirá $0, +# a linha que está atualmente sendo processada. Assim, a versão completa +# é a seguinte: + +$0 ~ /^fo+bar$/ { + print "Equivalente ao último padrão" +} + +a > 0 { + # Isso será executado uma vez para cada linha, quando a for positivo +} + +# Você entendeu. Processar arquivos de texto, ler uma linha de cada vez, e +# fazer algo com ela, particularmente dividir com base em um delimitador, é +# tão comum no UNIX que AWK é uma linguagem de script que faz tudo por você, +# sem você precisa perguntar. Tudo o que você precisa fazer é escrever os +# padrões e ações com base no que você espera da entrada, e o que você quer +# fazer com isso. + +# Aqui está um exemplo rápido de um script simples, o tipo de coisa que o AWK +# é perfeito para fazer. Ele irá ler um nome da entrada padrão e depois +imprimirá a média de idade de todos com esse primeiro nome. Digamos que você +forneça como argumento o nome de um arquivo com esses dados: + +# Bob Jones 32 +# Jane Doe 22 +# Steve Stevens 83 +# Bob Smith 29 +# Bob Barker 72 +# +# Eis o script: + +BEGIN { + + # Primeiro, pergunte o nome do usuário + print "Para qual nome você quer calcular a média de idade?" + + # Pega uma linha da entrada padrão, não dos arquivos indicados na + # linha de comando + getline name <"/dev/stdin" +} + +# Agora, processa cada linha em que o primeiro nome é o nome informado +$1 == name { + + # Dentro desse bloco, nós temos acesso a algumas variáveis uteis, que + # foram pré-carregadas para nós: + # $0 é a linha corrente completa + # $3 é o terceiro campo, que é o que nos interessa aqui + # NF é a quantidade de campos, que deve ser 3 + # NR é o número de registros (linhas) lidas até agora + # FILENAME é o nome do arquivo sendo processado + # FS é o delimitador em uso, que é " " aqui + # ...etc. Há muito mais, documentadas no manual. + + # Mantenha um registro do total e da quantidade de linhas encontradas + sum += $3 + nlines++ +} + +# Outro padrão especial é chamado END. Ele será executado após o processamento +# de todos os arquivos de texto. Ao contrário de BEGIN, ele só será executado +# se você tiver dado a ele dados para processar. Ele será executado depois de +# todos os arquivos terem sido lidos e processados de acordo com as regras e +# ações que você forneceu. O objetivo disso em geral é produzir algum tipo de +# relatório final, ou fazer algo com o agregado dos dados acumulados ao longo +# do script. + +END { + if (nlines) + print "A média da idade para " name " é " sum / nlines +} + +``` +Leituras adicionais (em inglês): + +* [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 é encontrado na maioria dos sistemas GNU/Linux. diff --git a/pt-br/bf.html.markdown b/pt-br/bf-pt.html.markdown index d6d7c6e9..53baa9a2 100644 --- a/pt-br/bf.html.markdown +++ b/pt-br/bf-pt.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: learnbf-pt.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] @@ -12,7 +13,9 @@ lang: pt-br Brainfuck (em letras minúsculas, exceto no início de frases) é uma linguagem de programação Turing-completa extremamente simples com apenas 8 comandos. -``` +Você pode experimentar brainfuck pelo seu browser com [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/). + +```bf Qualquer caractere exceto "><+-.,[]" (sem contar as aspas) é ignorado. Brainfuck é representado por um vetor com 30 000 células inicializadas em zero diff --git a/pt-br/c++-pt.html.markdown b/pt-br/c++-pt.html.markdown index fd392b9e..cd4adde7 100644 --- a/pt-br/c++-pt.html.markdown +++ b/pt-br/c++-pt.html.markdown @@ -1,6 +1,6 @@ --- language: c++ -filename: learncpp.cpp +filename: learncpp-pt.cpp contributors: - ["Steven Basart", "http://github.com/xksteven"] - ["Matt Kline", "https://github.com/mrkline"] @@ -18,9 +18,9 @@ foi concebida para - suportar programação orientada a objetos - suportar programação genérica -Embora sua sintaxe pode ser mais difícil ou complexa do que as linguagens mais -recentes, C++ é amplamente utilizado porque compila para instruções nativas que -podem ser executadas diretamente pelo processador e oferece um controlo rígido sobre hardware (como C), enquanto oferece recursos de alto nível, como os +Embora sua sintaxe possa ser mais difícil ou complexa do que as linguagens mais +recentes, C++ é amplamente utilizada porque compila para instruções nativas que +podem ser executadas diretamente pelo processador e oferece um controle rígido sobre o hardware (como C), enquanto oferece recursos de alto nível, como os genéricos, exceções e classes. Esta combinação de velocidade e funcionalidade faz C++ uma das linguagens de programação mais utilizadas. @@ -40,10 +40,10 @@ faz C++ uma das linguagens de programação mais utilizadas. int main(int argc, char** argv) { - // Argumentos de linha de comando são passados em pelo argc e argv da mesma + // Argumentos de linha de comando são passados para argc e argv da mesma // forma que eles estão em C. // argc indica o número de argumentos, - // e argv é um array de strings, feito C (char*) representado os argumentos + // e argv é um array de strings, feito C (char*) representando os argumentos // O primeiro argumento é o nome pelo qual o programa foi chamado. // argc e argv pode ser omitido se você não se importa com argumentos, // dando a assinatura da função de int main() @@ -274,7 +274,7 @@ public: void setWeight(int dogsWeight); - // Funções que não modificam o estado do objecto devem ser marcadas como + // Funções que não modificam o estado do objeto devem ser marcadas como // const. Isso permite que você chamá-los se for dada uma referência const // para o objeto. Além disso, observe as funções devem ser explicitamente // declarados como _virtual_, a fim de ser substituídas em classes diff --git a/pt-br/c-pt.html.markdown b/pt-br/c-pt.html.markdown index 0af553c8..d594b7b9 100644 --- a/pt-br/c-pt.html.markdown +++ b/pt-br/c-pt.html.markdown @@ -182,7 +182,7 @@ int main() { int a, b, c; a = b = c = 0; - // Aritimética é óbvia + // Aritmética é óbvia i1 + i2; // => 3 i2 - i1; // => 1 i2 * i1; // => 2 @@ -191,7 +191,7 @@ int main() { f1 / f2; // => 0.5, mais ou menos epsilon // Números e cálculos de ponto flutuante não são exatos - // Modulo também existe + // Módulo também existe 11 % 3; // => 2 // Operadores de comparação provavelmente são familiares, @@ -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 -[Linux kernel coding stlye](https://www.kernel.org/doc/Documentation/CodingStyle). +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 03a7c15c..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/csharp.html.markdown b/pt-br/csharp-pt.html.markdown index 547f4817..b6e95d36 100644 --- a/pt-br/csharp.html.markdown +++ b/pt-br/csharp-pt.html.markdown @@ -6,23 +6,23 @@ contributors: lang: pt-br --- -C# é uma linguagem elegante e altamente tipado orientada a objetos que permite aos desenvolvedores criarem uma variedade de aplicações seguras e robustas que são executadas no .NET Framework. +C# é uma linguagem elegante, altamente tipada e orientada a objetos que permite aos desenvolvedores criar uma variedade de aplicações seguras e robustas que são executadas no .NET Framework. -[Read more here.](http://msdn.microsoft.com/pt-br/library/vstudio/z1zx9t92.aspx) +[Leia mais aqui.](http://msdn.microsoft.com/pt-br/library/vstudio/z1zx9t92.aspx) ```c# -// Comentário de linha única começa com // +// Comentários de linha única começam com // /* -Múltipas linhas é desta forma +Comentários de múltiplas linhas são desta forma */ /// <summary> -/// Esta é uma documentação comentário XML que pode ser usado para gerar externo -/// documentação ou fornecer ajuda de contexto dentro de um IDE +/// Este é um comentário de documentação XML que pode ser usado para gerar documentação +/// externa ou para fornecer ajuda de contexto dentro de uma IDE /// </summary> //public void MethodOrClassOrOtherWithParsableHelp() {} -// Especificar qual namespace seu código irá usar -// Os namespaces a seguir são padrões do .NET Framework Class Library +// Especifica os namespaces que o código irá usar +// Os namespaces a seguir são padrões da biblioteca de classes do .NET Framework using System; using System.Collections.Generic; using System.Dynamic; @@ -33,11 +33,11 @@ using System.IO; // Mas este aqui não é : using System.Data.Entity; -// Para que consiga utiliza-lo, você precisa adicionar novas referências +// Para que consiga utilizá-lo, você precisa adicionar novas referências // Isso pode ser feito com o gerenciador de pacotes NuGet : `Install-Package EntityFramework` -// Namespaces são escopos definidos para organizar o códgo em "pacotes" or "módulos" -// Usando este código a partir de outra arquivo de origem: using Learning.CSharp; +// Namespaces são escopos definidos para organizar o código em "pacotes" ou "módulos" +// Usando este código a partir de outro arquivo de origem: using Learning.CSharp; namespace Learning.CSharp { // Cada .cs deve conter uma classe com o mesmo nome do arquivo @@ -762,7 +762,7 @@ on a new line! ""Wow!"", the masses cried"; } } - //Method to display the attribute values of this Object. + //Método para exibir os valores dos atributos deste objeto. public virtual string Info() { return "Gear: " + Gear + @@ -784,13 +784,13 @@ on a new line! ""Wow!"", the masses cried"; } // end class Bicycle - // PennyFarthing is a subclass of Bicycle + // PennyFarthing é uma subclasse de Bicycle class PennyFarthing : Bicycle { - // (Penny Farthings are those bicycles with the big front wheel. - // They have no gears.) + // (Penny Farthings são aquelas bicicletas com uma grande roda frontal. + // Elas não tem correias.) - // calling parent constructor + // chamando construtor pai public PennyFarthing(int startCadence, int startSpeed) : base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra) { @@ -823,10 +823,10 @@ on a new line! ""Wow!"", the masses cried"; } } - // Interfaces only contain signatures of the members, without the implementation. + // Interfaces contêm apenas as assinaturas dos membros, sem a implementação. interface IJumpable { - void Jump(int meters); // all interface members are implicitly public + void Jump(int meters); // todos os membros da interface são implicitamente públicos } interface IBreakable diff --git a/pt-br/css-pt.html.markdown b/pt-br/css-pt.html.markdown index b1fbd961..956b3614 100644 --- a/pt-br/css-pt.html.markdown +++ b/pt-br/css-pt.html.markdown @@ -25,7 +25,7 @@ O foco principal deste artigo é sobre a sintaxe e algumas dicas gerais. ```css /* Comentários aparecem dentro do slash-asterisk, tal como esta linha! - não há "comentários de uma linha"; este é o único estilo de comentário * / + Não há "comentários de uma linha"; este é o único estilo de comentário * / /* #################### ## SELETORES diff --git a/pt-br/dart-pt.html.markdown b/pt-br/dart-pt.html.markdown new file mode 100644 index 00000000..e9d72850 --- /dev/null +++ b/pt-br/dart-pt.html.markdown @@ -0,0 +1,509 @@ +--- +language: dart +filename: learndart-pt.dart +contributors: + - ["Joao Pedrosa", "https://github.com/jpedrosa/"] +translators: + - ["Junior Damacena", "https://github.com/jdamacena/"] +lang: pt-br +--- + +Dart é uma novata no reino das linguagens de programação. +Ela empresta muito de outras linguagens mais conhecidas, e tem a meta de não se diferenciar muito de seu irmão, JavaScript. Assim como JavaScript, Dart foi pensada para oferecer grande integração com o Browser. + +A característica mais controversa da Dart é a sua Tipagem Opcional, ou seja, não é obrigatório declarar tipos. + +```dart +import "dart:collection"; +import "dart:math" as DM; + +// Bem vindo ao Aprenda Dart em 15 minutos. http://www.dartlang.org/ +// Este é um tutorial executável. Você pode rodar esse tutorial com Dart ou no +// site Try Dart!, é só copiar e colar este código lá. http://try.dartlang.org/ + +// Declarações de funções e métodos são iguais. Declarações de funções +// podem ser aninhadas. A declaração é feita das seguintes formas +// nome() {} ou nome() => expressaoDeUmaLinhaSo; +// A declaração feita com a seta tem um return implícito para o resultado da +// expressão. +example1() { + example1nested1() { + example1nested2() => print("Example1 nested 1 nested 2"); + example1nested2(); + } + example1nested1(); +} + +// Funções anônimas são criadas sem um nome. +example2() { + example2nested1(fn) { + fn(); + } + example2nested1(() => print("Example2 nested 1")); +} + +// Quando uma função é declarada como parâmetro, a declaração pode incluir o número +// de parâmetros que a função recebe, isso é feito especificando o nome de cada um dos +// parâmetros que serão recebidos pela função. +example3() { + example3nested1(fn(informSomething)) { + fn("Example3 nested 1"); + } + example3planB(fn) { // Ou não declare o número de parâmetros. + fn("Example3 plan B"); + } + example3nested1((s) => print(s)); + example3planB((s) => print(s)); +} + +// Funções têm acesso à variáveis fora de seu escopo +var example4Something = "Example4 nested 1"; +example4() { + example4nested1(fn(informSomething)) { + fn(example4Something); + } + example4nested1((s) => print(s)); +} + +// Declaração de classe com um método chamado sayIt, que também tem acesso +// à variável externa, como se fosse uma função como se viu antes. +var example5method = "Example5 sayIt"; +class Example5Class { + sayIt() { + print(example5method); + } +} +example5() { + // Criar uma instância anônima de Example5Class e chamar o método sayIt + // nela. + new Example5Class().sayIt(); +} + +// A declaração de uma classe é feita da seguinte maneira: class name { [classBody] }. +// onde classBody pode incluir métodos e variáveis de instância, assim como +// métodos e variáveis de classe. +class Example6Class { + var example6InstanceVariable = "Example6 instance variable"; + sayIt() { + print(example6InstanceVariable); + } +} +example6() { + new Example6Class().sayIt(); +} + +// Métodos e variáveis de classe são declarados como "static". +class Example7Class { + static var example7ClassVariable = "Example7 class variable"; + static sayItFromClass() { + print(example7ClassVariable); + } + sayItFromInstance() { + print(example7ClassVariable); + } +} +example7() { + Example7Class.sayItFromClass(); + new Example7Class().sayItFromInstance(); +} + +// Literais são ótimos, mas há uma limitação para o que eles podem ser +// quando estão fora do corpo de uma função/método. Literais fora do escopo da classe +// ou fora da classe têm que ser constantes. Strings e números são constantes +// por padrão. Mas arrays e mapas não. Eles podem ser declarados como constantes +// usando o comando "const". +var example8A = const ["Example8 const array"], + example8M = const {"someKey": "Example8 const map"}; +example8() { + print(example8A[0]); + print(example8M["someKey"]); +} + +// Loops em Dart são criados com for () {} ou while () {}, +// um pouco mais moderno temos for (.. in ..) {}, ou funções de callbacks com muitas +// funcionalidades, começando com o forEach. +var example9A = const ["a", "b"]; +example9() { + for (var i = 0; i < example9A.length; i++) { + print("Example9 for loop '${example9A[i]}'"); + } + var i = 0; + while (i < example9A.length) { + print("Example9 while loop '${example9A[i]}'"); + i++; + } + for (var e in example9A) { + print("Example9 for-in loop '${e}'"); + } + example9A.forEach((e) => print("Example9 forEach loop '${e}'")); +} + +// Para percorrer os caracteres de uma string ou extrair uma substring. +var example10S = "ab"; +example10() { + for (var i = 0; i < example10S.length; i++) { + print("Example10 String character loop '${example10S[i]}'"); + } + for (var i = 0; i < example10S.length; i++) { + print("Example10 substring loop '${example10S.substring(i, i + 1)}'"); + } +} + +// Int e double são os dois formatos de número suportados. +example11() { + var i = 1 + 320, d = 3.2 + 0.01; + print("Example11 int ${i}"); + print("Example11 double ${d}"); +} + +// DateTime traz operações com data/hora. +example12() { + var now = new DateTime.now(); + print("Example12 now '${now}'"); + now = now.add(new Duration(days: 1)); + print("Example12 tomorrow '${now}'"); +} + +// Expressões regulares são suportadas. +example13() { + var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$"); + match(s) { + if (re.hasMatch(s)) { + print("Example13 regexp matches '${s}'"); + } else { + print("Example13 regexp doesn't match '${s}'"); + } + } + match(s1); + match(s2); +} + +// Expressões booleanas precisam retornar ou true ou false, já que +// Dart não faz a conversão implicitamente. +example14() { + var v = true; + if (v) { + print("Example14 value is true"); + } + v = null; + try { + if (v) { + // Nunca seria executada + } else { + // Nunca seria executada + } + } catch (e) { + print("Example14 null value causes an exception: '${e}'"); + } +} + +// try/catch/finally e throw são usados para tratamento de exceções. +// throw aceita qualquer objeto como parâmetro; +example15() { + try { + try { + throw "Some unexpected error."; + } catch (e) { + print("Example15 an exception: '${e}'"); + throw e; // Re-throw + } + } catch (e) { + print("Example15 catch exception being re-thrown: '${e}'"); + } finally { + print("Example15 Still run finally"); + } +} + +// Para mais eficiência ao criar strings longas dinamicamente, use o +// StringBuffer. Ou você pode também concatenar um array de strings. +example16() { + var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e; + for (e in a) { sb.write(e); } + print("Example16 dynamic string created with " + "StringBuffer '${sb.toString()}'"); + print("Example16 join string array '${a.join()}'"); +} + +// Strings podem ser concatenadas apenas colocando strings literais uma perto +// da outra, sem necessidade de nenhum outro operador. +example17() { + print("Example17 " + "concatenar " + "strings " + "é simples assim"); +} + +// Strings podem ser delimitadas por apóstrofos ou aspas e não há +// diferença entre os dois. Essa flexibilidade pode ser boa para +// evitar a necessidade de escapar conteúdos que contenham o delimitador da string. +// Por exemplo, aspas dos atributos HTMLse a string conter HTML. +example18() { + print('Example18 <a href="etc">' + "Don't can't I'm Etc" + '</a>'); +} + +// Strings com três apóstrofos ou aspas podem +// ter muitas linhas e incluem os delimitadores de linha (ou seja, os enter). +example19() { + print('''Example19 <a href="etc"> +Example19 Don't can't I'm Etc +Example19 </a>'''); +} + +// Strings têm a função de interpolação que é chamada com o caractere $. +// Com $ { [expression] }, o retorno da expressão é interpolado. +// $ seguido pelo nome de uma variável interpola o conteúdo dessa variável. +// $ pode ser escapedo assim \$. +example20() { + var s1 = "'\${s}'", s2 = "'\$s'"; + print("Example20 \$ interpolation ${s1} or $s2 works."); +} + +// A tipagem opcional permite que APIs usem anotações e também ajuda os +// IDEs na hora das refatorações, auto-complete e checagem de +// erros. Note que até agora não declaramos nenhum tipo e o programa está +// funcionando normalmente. De fato, os tipos são ignorados em tempo de execução. +// Os tipos podem até mesmo estarem errados e o programa ainda vai dar o +// benefício da dúvida e rodar, visto que os tipos não importam. +// Existe um parâmetro que checa erros de tipagem que é o +// checked mode, dizem que é útil enquanto se está desenvolvendo, +// mas também é mais lento devido às checagens extras e por isso +// é evitado em ambiente de produção. +class Example21 { + List<String> _names; + Example21() { + _names = ["a", "b"]; + } + List<String> get names => _names; + set names(List<String> list) { + _names = list; + } + int get length => _names.length; + void add(String name) { + _names.add(name); + } +} +void example21() { + Example21 o = new Example21(); + o.add("c"); + print("Example21 names '${o.names}' and length '${o.length}'"); + o.names = ["d", "e"]; + print("Example21 names '${o.names}' and length '${o.length}'"); +} + +// Herança em classes é feito assim: class name extends AnotherClassName {}. +class Example22A { + var _name = "Some Name!"; + get name => _name; +} +class Example22B extends Example22A {} +example22() { + var o = new Example22B(); + print("Example22 class inheritance '${o.name}'"); +} + +// Mistura de classes também é possível, e é feito assim: +// class name extends SomeClass with AnotherClassName {} +// É necessário extender uma classe para poder misturar com outra. +// No momento, classes misturadas não podem ter construtor. +// Mistura de classes é mais usado para compartilhar métodos com classes distantes, então +// a herança comum não fica no caminho do reuso de código. +// As misturas aparecem após o comando "with" na declaração da classe. +class Example23A {} +class Example23Utils { + addTwo(n1, n2) { + return n1 + n2; + } +} +class Example23B extends Example23A with Example23Utils { + addThree(n1, n2, n3) { + return addTwo(n1, n2) + n3; + } +} +example23() { + var o = new Example23B(), r1 = o.addThree(1, 2, 3), + r2 = o.addTwo(1, 2); + print("Example23 addThree(1, 2, 3) results in '${r1}'"); + print("Example23 addTwo(1, 2) results in '${r2}'"); +} + +// O método construtor da classe usa o mesmo nome da classe e +// é feito assim SomeClass() : super() {}, onde a parte ": super()" +// é opcional e é usada para passar parâmetros estáticos para o +// construtor da classe pai. +class Example24A { + var _value; + Example24A({value: "someValue"}) { + _value = value; + } + get value => _value; +} +class Example24B extends Example24A { + Example24B({value: "someOtherValue"}) : super(value: value); +} +example24() { + var o1 = new Example24B(), + o2 = new Example24B(value: "evenMore"); + print("Example24 calling super during constructor '${o1.value}'"); + print("Example24 calling super during constructor '${o2.value}'"); +} + +// Há um atalho para passar parâmetros para o construtor no caso de classes mais simples. +// Simplesmente use o prefixo this.nomeDoParametro e isso irá passar o parâmetro para uma +// instância de variável de mesmo nome. +class Example25 { + var value, anotherValue; + Example25({this.value, this.anotherValue}); +} +example25() { + var o = new Example25(value: "a", anotherValue: "b"); + print("Example25 shortcut for constructor '${o.value}' and " + "'${o.anotherValue}'"); +} + +// Parâmetros com nome estão disponíveis quando declarados entre {}. +// Quando os parâmetros têm nomes, eles podem ser passados em qualquer ordem. +// Parâmetros declarados entre [] são opcionais. +example26() { + var _name, _surname, _email; + setConfig1({name, surname}) { + _name = name; + _surname = surname; + } + setConfig2(name, [surname, email]) { + _name = name; + _surname = surname; + _email = email; + } + setConfig1(surname: "Doe", name: "John"); + print("Example26 name '${_name}', surname '${_surname}', " + "email '${_email}'"); + setConfig2("Mary", "Jane"); + print("Example26 name '${_name}', surname '${_surname}', " + "email '${_email}'"); +} + +// Variáveis declaradas com um final só podem receber valor uma vez. +// No caso de classes, variáveis final podem ter valor atribuido através +// de um parâmetro no construtor +class Example27 { + final color1, color2; + // Um pouco de flexibilidade ao criar variáveis final com a sintaxe + // que é a seguinte: + Example27({this.color1, color2}) : color2 = color2; +} +example27() { + final color = "orange", o = new Example27(color1: "lilac", color2: "white"); + print("Example27 color is '${color}'"); + print("Example27 color is '${o.color1}' and '${o.color2}'"); +} + +// para importar uma biblioteca, use import "libraryPath" ou se for uma biblioteca da linguagem, +// import "dart:libraryName". Também tem o gerenciador de pacotes "pub"que tem +// sua própria convenção de import "package:packageName". +// Veja o import "dart:collection"; no início do arquivo. Imports devem vir no início +// do arquivo. IterableBase vem de dart:collection. +class Example28 extends IterableBase { + var names; + Example28() { + names = ["a", "b"]; + } + get iterator => names.iterator; +} +example28() { + var o = new Example28(); + o.forEach((name) => print("Example28 '${name}'")); +} + +// Para controle de fluxo nós temos: +// * switch com comandos break obrigatórios +// * if-else if-else e se-ternário ..?..:.. +// * closures e funções anônimas +// * comandos break, continue e return +example29() { + var v = true ? 30 : 60; + switch (v) { + case 30: + print("Example29 switch statement"); + break; + } + if (v < 30) { + } else if (v > 30) { + } else { + print("Example29 if-else statement"); + } + callItForMe(fn()) { + return fn(); + } + rand() { + v = new DM.Random().nextInt(50); + return v; + } + while (true) { + print("Example29 callItForMe(rand) '${callItForMe(rand)}'"); + if (v != 30) { + break; + } else { + continue; + } + // Nunca chega aqui. + } +} + +// Você pode converter string para int, double para int, ou só pegar a parte inteira da divisão +// usando o comando ~/. Vamos jogar um jogo de adivinhação. +example30() { + var gn, tooHigh = false, + n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0; + top = top ~/ 6; + gn = new DM.Random().nextInt(top + 1); // +1 porque o máximo do nextInt conta o número passado - 1 + print("Example30 Diga um número entre 0 e ${top}"); + guessNumber(i) { + if (n == gn) { + print("Example30 Você acertou! O número é ${gn}"); + } else { + tooHigh = n > gn; + print("Example30 O número ${n} é muito " + "${tooHigh ? 'alto' : 'baixo'}. Tente de novo"); + } + return n == gn; + } + n = (top - bottom) ~/ 2; + while (!guessNumber(n)) { + if (tooHigh) { + top = n - 1; + } else { + bottom = n + 1; + } + n = bottom + ((top - bottom) ~/ 2); + } +} + +// Programas em Dart só têm um ponto de entrada, que é a função main. +// Nada será executado antes da funcão main de um programa. +// Isso ajuda a carregar o programa mais rapidamente, até mesmo quando o +// carregamento é "Lazy". +// O programa deve começar com: +main() { + print("Aprenda Dart em 15 minutos!"); + [example1, example2, example3, example4, example5, example6, example7, + example8, example9, example10, example11, example12, example13, example14, + example15, example16, example17, example18, example19, example20, + example21, example22, example23, example24, example25, example26, + example27, example28, example29, example30 + ].forEach((ef) => ef()); +} + +``` + +## Continue lendo + +Dart tem um site bastante fácil de entender. Ele tem os docs da API, tutoriais, artigos e muito mais, incluindo uma +opção muito útil de testar o Dart online. +* [https://www.dartlang.org](https://www.dartlang.org) +* [https://try.dartlang.org](https://try.dartlang.org) + + + + diff --git a/pt-br/dynamic-programming-pt.html.markdown b/pt-br/dynamic-programming-pt.html.markdown index 8de9bee6..84b055d9 100644 --- a/pt-br/dynamic-programming-pt.html.markdown +++ b/pt-br/dynamic-programming-pt.html.markdown @@ -22,16 +22,16 @@ Sempre se lembre!! ## Maneiras de Solucionar tais Problemas -1. Top-Down (De cima para baixo): Começe solucionando o problema quebrando-o em +1. Top-Down (De cima para baixo): Comece solucionando o problema quebrando-o em partes. Se você perceber que o problema já foi resolvido, então simplemente pegue a resposta salva. Se ainda não foi resolvido, solucione-o e salve a resposta. Isso é geralmente fácil de pensar e muito intuitivo. É geralmente referenciado como Memorização. 2. Bottom-Up (De baixo para cima): Analise o problema e veja a ordem em que os -subproblemas são resolvidos e começe a solucionar dos problemas mais triviais, +subproblemas são resolvidos e comece a solucionar dos problemas mais triviais, até o problema dado. Neste processo, é garantido que os subproblemas são -resolvidos antes de resoler o problema. Isto é referenciado como Programação Dinâmica. +resolvidos antes de resolver o problema. Isto é referenciado como Programação Dinâmica. ## Exemplo de Programação Dinâmica @@ -51,7 +51,7 @@ array antecedente e uma variável como maiorSequenciasAteAgora e seu índice ajudariam a poupar muito tempo. Um conceito similar poderia ser aplicado ao procurar o maior caminho em um grafo acíclico dirigido. ---------------------------------------------------------------------------- + ``` for i=0 to n-1 LS[i]=1 @@ -62,7 +62,7 @@ grafo acíclico dirigido. if (largest < LS[i]) ``` -### Alguns Problemas Famosos de Programação Dinâmica +## Alguns Problemas Famosos de Programação Dinâmica ``` Floyd Warshall Algorithm - 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 diff --git a/pt-br/elixir.html.markdown b/pt-br/elixir-pt.html.markdown index f8c56101..f8c56101 100644 --- a/pt-br/elixir.html.markdown +++ b/pt-br/elixir-pt.html.markdown diff --git a/pt-br/elm-pt.html.markdown b/pt-br/elm-pt.html.markdown index 78a4f1b7..d2469a93 100644 --- a/pt-br/elm-pt.html.markdown +++ b/pt-br/elm-pt.html.markdown @@ -76,8 +76,8 @@ List.head [] -- Nothing -- Acesse os elementos de um par com as funções first e second. -- (Este é um atalho; nós iremos para o "caminho real" em breve.) -fst ("elm", 42) -- "elm" -snd ("elm", 42) -- 42 +Tuple.first ("elm", 42) -- "elm" +Tuple.second ("elm", 42) -- 42 -- Uma tupla vazia ou "unidade" às vezes é utilizada como um placeholder. -- É o único valor de seu tipo, também chamado de "Unit". diff --git a/pt-br/groovy-pt.html.markdown b/pt-br/groovy-pt.html.markdown index 2ec7d967..aed23df1 100644 --- a/pt-br/groovy-pt.html.markdown +++ b/pt-br/groovy-pt.html.markdown @@ -1,7 +1,7 @@ --- language: Groovy category: language -filename: learngroovy.groovy +filename: learngroovy-pt.groovy contributors: - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"] translators: @@ -226,10 +226,12 @@ for (i in array) { //Itera sobre um mapa def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] -x = 0 +x = "" for ( e in map ) { x += e.value + x += " " } +assert x.equals("Roberto Grails Groovy ") /* Operadores diff --git a/pt-br/hy-pt.html.markdown b/pt-br/hy-pt.html.markdown index 5fa4df75..c69ac859 100644 --- a/pt-br/hy-pt.html.markdown +++ b/pt-br/hy-pt.html.markdown @@ -1,6 +1,6 @@ --- language: hy -filename: learnhy.hy +filename: learnhy-pt.hy contributors: - ["Abhishek L", "http://twitter.com/abhishekl"] translators: diff --git a/pt-br/java-pt.html.markdown b/pt-br/java-pt.html.markdown index 82989502..1b9d7fc6 100644 --- a/pt-br/java-pt.html.markdown +++ b/pt-br/java-pt.html.markdown @@ -42,7 +42,7 @@ public class LearnJava { " Double: " + 3.14 + " Boolean: " + true); - // Para imprimir sem inserir uma nova lina, use o System.out.print + // Para imprimir sem inserir uma nova linha, use o System.out.print System.out.print("Olá "); System.out.print("Mundo"); diff --git a/pt-br/javascript-pt.html.markdown b/pt-br/javascript-pt.html.markdown index e337f4bc..ed4a6ff3 100644 --- a/pt-br/javascript-pt.html.markdown +++ b/pt-br/javascript-pt.html.markdown @@ -1,5 +1,6 @@ --- language: javascript +filename: javascript-pt.js contributors: - ["Adam Brenecki", "http://adam.brenecki.id.au"] - ["Ariel Krakowski", "http://www.learneroo.com"] @@ -24,7 +25,7 @@ Feedback são muito apreciados! Você me encontrar em ```js // Comentários são como em C. Comentários de uma linha começam com duas barras, -/* e comentários de múltplas linhas começam com barra-asterisco +/* e comentários de múltiplas linhas começam com barra-asterisco e fecham com asterisco-barra */ // comandos podem ser terminados com ; diff --git a/pt-br/json-pt.html.markdown b/pt-br/json-pt.html.markdown index fd822c03..62d9ccad 100644 --- a/pt-br/json-pt.html.markdown +++ b/pt-br/json-pt.html.markdown @@ -16,7 +16,7 @@ Como JSON é um formato de intercâmbio de dados, este será, muito provavelment JSON na sua forma mais pura não tem comentários, mas a maioria dos analisadores aceitarão comentários no estilo C (//, /\* \*/). No entanto estes devem ser evitados para otimizar a compatibilidade. -Um valor JSON pode ser um numero, uma string, um array, um objeto, um booleano (true, false) ou null. +Um valor JSON pode ser um número, uma string, um array, um objeto, um booleano (true, false) ou null. Os browsers suportados são: Firefox 3.5+, Internet Explorer 8.0+, Chrome 1.0+, Opera 10.0+, e Safari 4.0+. diff --git a/pt-br/kotlin-pt.html.markdown b/pt-br/kotlin-pt.html.markdown new file mode 100644 index 00000000..7c3313fc --- /dev/null +++ b/pt-br/kotlin-pt.html.markdown @@ -0,0 +1,384 @@ +--- +language: kotlin +filename: LearnKotlin-pt.kt +contributors: + - ["S Webber", "https://github.com/s-webber"] +translators: + - ["Márcio Torres", "https://github.com/marciojrtorres"] +lang: pt-br +--- + +Kotlin é uma linguagem de programação estaticamente tipada para a JVM, Android e navegadores web. Ela é 100% interoperável com Java. +[Leia mais aqui.](https://kotlinlang.org/) + +```kotlin +// Comentários de uma linha iniciam com // +/* +Comentários multilinha se parecem com este. +*/ + +// A palavra-chave "package" funciona do mesmo modo que no Java. +package com.learnxinyminutes.kotlin + +/* +O ponto de entrada para um programa em Kotlin é uma função chamada "main" +Esta função recebe um vetor contendo quaisquer argumentos da linha de comando +*/ +fun main(args: Array<String>) { + /* + A declaração de valores pode ser feita tanto com "var" como "val" + Declarações com "val" não podem ser reatribuídas, enquanto com "var" podem. + */ + val umVal = 10 // não se poderá reatribuir qualquer coisa a umVal + var umVar = 10 + umVar = 20 // umVar pode ser reatribuída, mas respeitando o tipo + + /* + Na maioria dos casos Kotlin pode inferir o tipo, então não é preciso sempre + especificar o tipo explicitamente, mas quando o fazemos é assim: + */ + val umInteiro: Int = 7 + + /* + Strings podem ser representadas de forma semelhante a Java. + A contrabarra realiza o "escape", da mesma forma. + */ + val umaString = "Minha String está aqui!" + val outraString = "Imprimir na outra linha?\nSem problema!" + val maisString = "Você quer adicionar um tab?\tSem problema!" + println(umaString) + println(outraString) + println(maisString) + + /* + Uma string bruta é delimitada com três aspas ("""). + Strings brutas podem conter novas linhas e outros caracteres. + */ + val umaStringBruta = """ +fun olaMundo(val nome : String) { + println("Olá, mundo!") +} +""" + println(umaStringBruta) + + /* + As strings podem conter expressões modelo (template). + Uma expressão modelo começa com um cifrão ($). + É semelhante à interpolação de Strings em Ruby. + */ + val umaStringModelo = "$umaString tem ${umaString.length} caracteres" + println(umaStringModelo) + + /* + Para uma variável receber null deve-se explicitamente declara-la + como anulável. + A declaração de anulável é realizada incluindo uma "?" ao fim do tipo. + Pode-se acessar uma variável anulável usando o operador "?." + Usa-se o operador "?:" (também conhecido como operador Elvis) para + atribuir um valor alternativo para quando uma variável é nula. + */ + var umaVariavelAnulavel: String? = "abc" + println(umaVariavelAnulavel?.length) // => 3 + println(umaVariavelAnulavel?.length ?: -1) // => 3 + umaVariavelAnulavel = null + println(umaVariavelAnulavel?.length) // => null + println(umaVariavelAnulavel?.length ?: -1) // => -1 + + /* + Funções podem ser declaradas usando a palavra-chave "fun" + Os parâmetros da função são declarados entre parênteses logo + após o nome da função. + Os parâmetros da função podem ter opcionalmente um valor padrão. + O tipo de retorno da função, se necessário, é especificado após os argumentos. + */ + fun ola(nome: String = "mundo"): String { + return "Olá, $nome!" + } + println(ola("você")) // => Olá, você! + println(ola(nome = "tu")) // => Olá, tu! + println(ola()) // => Olá, mundo! + + /* + Um parâmetro pode ser declarado com a palavra-chave "vararg" para + permitir que seja passado um número variável de argumentos. + */ + fun exemploVarArg(vararg numeros: Int) { + println("Foram recebidos ${numeros.size} argumentos") + } + exemploVarArg() // => Passando nenhum argumento (0 argumentos) + exemploVarArg(1) // => Passando 1 argumento + exemploVarArg(1, 2, 3) // => Passando 3 argumentos + + /* + Quando uma função consiste numa única expressão as chaves + podem ser omitidas e o corpo declarado após o símbolo de "=" + */ + fun impar(x: Int): Boolean = x % 2 == 1 + println(impar(6)) // => false + println(impar(7)) // => true + + // O tipo de retorno não precisa ser declarado se pode ser inferido. + fun impar(x: Int) = x % 2 == 0 + println(impar(6)) // => true + println(impar(7)) // => false + + // Funções podem receber e retornar outras funções + fun nao(f: (Int) -> Boolean): (Int) -> Boolean { + return {n -> !f.invoke(n)} + } + // Funções nomeadas podem ser passadas como argumento usando o operador "::" + val naoImpar = nao(::impar) + val naoPar = nao(::par) + // Expressões Lambda podem ser usadas como argumentos + val naoZero = nao {n -> n == 0} + /* + Se uma lambda têm apenas um parâmetro sua declaração pode ser omitida, + incluindo o símbolo "->". + Neste caso o nome do único parâmetro deve ser "it". + */ + val naoPositivo = nao {it > 0} + for (i in 0..4) { + println("${naoImpar(i)} ${naoPar(i)} ${naoZero(i)} ${naoPositivo(i)}") + } + + // A palavra-chave "class" é usada para declarar classes + class ClasseExemplo(val x: Int) { + fun funcaoMembro(y: Int): Int { // ou "método" + return x + y + } + + infix fun funcaoMembroInfixa(y: Int): Int { + return x * y + } + } + /* + Para criar uma nova instância chama-se o construtor. + Note que Kotlin não tem a palavra-chave "new". + */ + val umaInstanciaDaClasseExemplo = ClasseExemplo(7) + // Funções membro (métodos) podem ser chamados usando a notação ponto "." + println(umaInstanciaDaClasseExemplo.funcaoMembro(4)) // => 11 + /* + Se uma função foi declarada com a palavra-chave "infix" então + ela pode ser invocada com a notação infixa. + */ + println(umaInstanciaDaClasseExemplo funcaoMembroInfixa 4) // => 28 + + /* + Classes de dados são um modo sucinto de criar classes que servem apenas + para guardas informações. + Os métodos "hashCode", "equals" e "toString" são gerados automaticamente. + */ + data class ExemploClasseDados (val x: Int, val y: Int, val z: Int) + val objetoDados = ExemploClasseDados(1, 2, 4) + println(objetoDados) // => ExemploClasseDados(x=1, y=2, z=4) + + // Classes de dados têm uma função "copy" + val dadosCopia = objetoDados.copy(y = 100) + println(dadosCopia) // => ExemploClasseDados(x=1, y=100, z=4) + + // Objetos podem ser desestruturados em múltiplas variáveis. + val (a, b, c) = dadosCopia + println("$a $b $c") // => 1 100 4 + + // desestruturando em um laço "for" + for ((a, b, c) in listOf(objetoDados)) { + println("$a $b $c") // => 1 100 4 + } + + val mapaDados = mapOf("a" to 1, "b" to 2) + // Map.Entry também é desestruturável + for ((chave, valor) in mapaDados) { + println("$chave -> $valor") + } + + // A função "with" é semelhante à declaração "with" do JavaScript + data class ExemploClasseDadosMutaveis (var x: Int, var y: Int, var z: Int) + val objDadosMutaveis = ExemploClasseDadosMutaveis(7, 4, 9) + with (objDadosMutaveis) { + x -= 2 + y += 2 + z-- + } + println(objDadosMutaveis) // => ExemploClasseDadosMutaveis(x=5, y=6, z=8) + + /* + Pode-se criar uma lista usando a função "listOf". + A lista é imutável, isto é, elementos não podem ser adicionados ou removidos. + */ + val umaLista = listOf("a", "b", "c") + println(umaLista.size) // => 3 + println(umaLista.first()) // => a + println(umaLista.last()) // => c + // Elementos de uma lista podem ser acessados pelo índice + println(umaLista[1]) // => b + + // Uma lista mutável pode ser criada com a função "mutableListOf". + val umaListaMutavel = mutableListOf("a", "b", "c") + umaListaMutavel.add("d") + println(umaListaMutavel.last()) // => d + println(umaListaMutavel.size) // => 4 + + // Similarmente, pode-se criar um conjunto com a função "setOf". + val umConjunto = setOf("a", "b", "c") + println(umConjunto.contains("a")) // => true + println(umConjunto.contains("z")) // => false + + // Da mesma forma que um mapa com a função "mapOf". + val umMapa = mapOf("a" to 8, "b" to 7, "c" to 9) + // Os valores contidos no mapa podem ser acessados pela sua chave. + println(umMapa["a"]) // => 8 + + /* + Sequências representam coleções avaliadas "preguiçosamente" (sob demanda). + Pode-se criar uma sequência usando a função "generateSequence". + */ + val umaSequencia = generateSequence(1, { it + 1 }) + val x = umaSequencia.take(10).toList() + println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + + // Um exemplo de uma sequência usada para gerar Números de Fibonacci: + fun sequenciaFibonacci(): Sequence<Long> { + var a = 0L + var b = 1L + + fun proximo(): Long { + val resultado = a + b + a = b + b = resultado + return a + } + + return generateSequence(::proximo) + } + val y = sequenciaFibonacci().take(10).toList() + println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] + + // Kotlin oferece funções de alta-ordem para trabalhar com coleções. + val z = (1..9).map {it * 3} + .filter {it < 20} + .groupBy {it % 2 == 0} + .mapKeys {if (it.key) "par" else "impar"} + println(z) // => {impar=[3, 9, 15], par=[6, 12, 18]} + + // Um "for" pode ser usado com qualquer coisa que ofereça um "iterator" + for (c in "salve") { + println(c) + } + + // O "while" funciona da mesma forma que em outras linguagens. + var contador = 0 + while (contador < 5) { + println(contador) + contador++ + } + do { + println(contador) + contador++ + } while (contador < 10) + + /* + "if" pode ser usado como uma expressão que retorna um valor. + Por este motivo o operador ternário "? :" não é necessário em Kotlin. + */ + val numero = 5 + val mensagem = if (numero % 2 == 0) "par" else "impar" + println("$numero é $mensagem") // => 5 é impar + + // "when" pode ser usado como alternativa às correntes de "if-else if". + val i = 10 + when { + i < 7 -> println("primeiro block") + umaString.startsWith("oi") -> println("segundo block") + else -> println("bloco else") + } + + // "when" pode ser usado com um argumento. + when (i) { + 0, 21 -> println("0 ou 21") + in 1..20 -> println("entre 1 e 20") + else -> println("nenhum dos anteriores") + } + + // "when" pode ser usada como uma função que retorna um valor. + var resultado = when (i) { + 0, 21 -> "0 ou 21" + in 1..20 -> "entre 1 e 20" + else -> "nenhum dos anteriores" + } + println(resultado) + + /* + Pode-se verificar se um objeto é de um certo tipo usando o operador "is". + Se o objeto passar pela verificação então ele pode ser usado como + este tipo, sem a necessidade de uma coerção (cast) explícita (SmartCast). + */ + fun exemploSmartCast(x: Any) : Boolean { + if (x is Boolean) { + // x é automaticamente coagido para Boolean + return x + } else if (x is Int) { + // x é automaticamente coagido para Int + return x > 0 + } else if (x is String) { + // x é automaticamente coagido para String + return x.isNotEmpty() + } else { + return false + } + } + println(exemploSmartCast("Olá, mundo!")) // => true + println(exemploSmartCast("")) // => false + println(exemploSmartCast(5)) // => true + println(exemploSmartCast(0)) // => false + println(exemploSmartCast(true)) // => true + + // O Smartcast também funciona com blocos "when" + fun exemploSmartCastComWhen(x: Any) = when (x) { + is Boolean -> x + is Int -> x > 0 + is String -> x.isNotEmpty() + else -> false + } + + /* + As extensões são uma maneira nova de adicionar funcionalidades a classes. + Elas são similares aos "extension methods" da linguagem C#. + */ + fun String.remove(c: Char): String { + return this.filter {it != c} + } + println("olá, mundo!".remove('o')) // => lá, mund! + + println(ExemploEnum.A) // => A + println(ExemploObjeto.ola()) // => olá +} + +// Classes Enum são similares aos "enum types" do Java. +enum class ExemploEnum { + A, B, C +} + +/* +A palavra-chave "object" pode ser usar para criar Singletons. +Eles não são instanciados, mas podem referenciar sua instância única pelo nome. +É semelhante aos "singleton objects" da linguagem Scala. +*/ +object ExemploObjeto { + fun ola(): String { + return "olá" + } +} + +fun usaObjeto() { + ExemploObjeto.ola() + val algumaReferencia: Any = ExemploObjeto // usa-se o nome diretamente +} + +``` + +### Leitura Adicional + +* [Tutoriais de Kotlin](https://kotlinlang.org/docs/tutorials/)(EN) +* [Experimente Kotlin no seu navegador](http://try.kotlinlang.org/)(EN) +* [Uma lista de material sobre Kotlin](http://kotlin.link/)(EN) diff --git a/pt-br/latex-pt.html.markdown b/pt-br/latex-pt.html.markdown new file mode 100644 index 00000000..a9ed566e --- /dev/null +++ b/pt-br/latex-pt.html.markdown @@ -0,0 +1,291 @@ +--- +language: latex +contributors: + - ["Chaitanya Krishna Ande", "http://icymist.github.io"] + - ["Colton Kohnke", "http://github.com/voltnor"] + - ["Sricharan Chiruvolu", "http://sricharan.xyz"] + - ["Ramanan Balakrishnan", "https://github.com/ramananbalakrishnan"] + - ["Svetlana Golubeva", "https://attillax.github.io/"] +translators: + - ["Paulo Henrique Rodrigues Pinheiro", "https://github.com/paulohrpinheiro"] +lang: pt-br +filename: learn-latex-pt.tex +--- + +```tex +% Todas as linhas de comentários começam com % +% Não existem comentários multilinhas + +$ LaTeX não é um programa processador de textos "Visual" como +% MS Word ou OpenOffice Writer + +$ Todo comando LaTeX começa com uma barra invertida (\) + +% Documentos LaTeX começam com a definição do tipo que será % compilado +% Os tipos de documento podem ser livro, relatório, apresentação, etc. +% As opções para um documento aparecem entre [] chaves. Nesse caso +% está especificado que queremos o tamanho da fonte em 12pt. +\documentclass[12pt]{article} + +% Em seguida definimos os pacotes que o documento usa. +% Se você quiser incluir gráficos, texto colorido, ou código fonte de outra +% linguagem em outro arquivo em seu documento, você precisa ampliar as +% capacidade do LaTeX. Isso é feito adicionando-se pacotes. +% Serão incluídos os pacotes float e caption para imagens e hyperref +% para links. +\usepackage{caption} +\usepackage{float} +\usepackage{hyperref} + +% Para poder usar caracteres acentuados, use o seguinte pacote: +\usepackage[utf8]{inputenc} + +% Podemos definir algumas outras propriedades do documento também! +\author{Chaitanya Krishna Ande, Colton Kohnke, Sricharan Chiruvolu \& \\ +Svetlana Golubeva} +\date{\today} +\title{Aprenda \LaTeX \hspace{1pt} em Y Minutos!} + +% Agora estamos pronto para começar o documento +% Tudo antes dessa linha é chamado "preâmbulo". +\begin{document} +% Se informarmos os campos author (autores), date (data), "title" (título), +% LaTeX poderá cria uma página inicial para nós. +\maketitle +% Se tivermos seções, poderemos criar uma tabela de conteúdo. Para isso, +% o documento deve ser compilado duas vezes, para que tudo apareça na ordem +% correta. +% É uma voa prática separar a tabela de conteúdo do corpo do documento. Para +% isso usa-se o comando \newpage +\newpage +\tableofcontents + +\newpage + +% Muitos artigos de pesquisa possuem um resumo, e pode-se isar comandos +% predefinidos para isso. +% Isso deve aparecer em sua ordem lógica, portanto, após o topo, +% mas antes das seções principais do corpo. +% Esse comando está disponível para os documentos do tipo artigo (article) +% e relatório (report). +\begin{abstract} + Documentação do \LaTeX \hspace{1pt} escrita em \LaTeX! Nada original! +\end{abstract} + +% Comandos para seções são intuitivos. +% Todos os títulos de seção são adicionados automaticamente à tabela de conteúdo. +\section{Introdução} +Olá, meu nome é Colton e juntos estamos explorando o mundo do \LaTeX! + +\section{Outra seção} +Esse é o texto para outra seção. Penso que precisamos de uma subseção. + +\subsection{Isso é uma subseção} % Subseções também são intuitivas. +Penso que precisamos de mais uma + +\subsubsection{Pythagoras} +Muito melhor agora. +\label{subsec:pythagoras} + +% Ao usar o asterisco nós impedimos a numeração automática. +% Isso funciona para outros comandos \LaTeX também. +\section*{Essa é uma seção não numerada} +Afinal nem todas as seções precisam ser numeradas! + +\section{Algumas notas sobre texto} +%\section{Espaçamento % É necessário mais informação sobre intervalos de espaço. +\LaTeX \hspace{1pt} geralmente é muito bom sobre colocar texto onde ele deve +ser posto. Se +uma linha \\ deve \\ ser \\ quebrada \\ adicione \textbackslash\textbackslash +\hspace{1pt} ao código de seu documento. \\ + +\section{Listas} +Listas são uma das coisas mais fáceis de criar no \LaTeX! Preciso fazer compras +amanhã, então façamos uma lista de compras. +\begin{enumerate} % Isso cria o bloco "enumerate". + % \item faz com que o enumerate incremente + \item Salada. + \item 27 melancias. + \item Uma lebre. + % pode-se também sobrescrever o número do item usando [] + \item[quantas?] Pistolas de água médias. + + Não é um item da lista, mas faz parte do bloco enumerate. + + \end{enumerate} % Todos os blocos devem ter um final (end{}). + +\section{Matemática} + +Um dos usos iniciais para \LaTeX \hspace{1pt} foi a produção de artigos +acadêmicos e técnicos. Usualmente nos campos da matemática e ciência. Assim, é +necessários que consigamos incluir alguns símbolos especiais em nosso texto! \\ + +A matemática tem muitos símbolos, além dos quais se pode encontrar no teclado; +símbolos para relações e conjuntos, setas, operadores, e letras gregas, apenas +para mencionar alguns.\\ + +Conjuntos e relações são essenciais em muitos textos de pesquisa em matemática. +Aqui está como você pode indicar como todo x que pertence +a X, $\forall$ x $\in$ X. \\ +% Perceba que é necessário adicionar os sinais $ antes e depois dos símbolos. +% Isso é porque quando escrevendo, estamos em modo texto. +% Mas os símbolos de matemática só existem no modo matemática. +% Podemos entrar no modo matemática a partir do modo texto com os símbolos $. +% O oposto também pode ocorrer. Variáveis podem ser renderizadas no modo +% matemática. +% Também podemos entrar no modo matemática com \[\] + +\[a^2 + b^2 = c^2 \] + +Minha letra grega favorita é $\xi$. Eu também gosto da $\beta$, $\gamma$ e $\sigma$. +Eu ainda não encontrei uma letra grega que o \LaTeX \hspace{1pt} não tenha!\\ + +Operadores são parte essencial de um documento sobre matemática: +funções trigonométricas ($\sin$, $\cos$, $\tan$), +logaritmo e exponencial ($\log$, $\exp$), +limites ($\lim$), etc. +possuem comandos pré-definidos em LaTex. +Vamos escrever uma equação para ver como se faz: +$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$ \\ + +Frações (numerador/denominador) podem ser escritas dessa forma: + +% 10 / 7 +$$ ^{10}/_{7} $$ + +% Frações relativamente complexas podem ser escritas como +% \frac{numerator}{denominator} +$$ \frac{n!}{k!(n - k)!} $$ \\ + +Também podemos escrever equações em um ``bloco de equação''. + +% Apresenta matemática com o 'bloco' equação +\begin{equation} % entra no modo matemática + c^2 = a^2 + b^2. + \label{eq:pythagoras} % para referência + \end{equation} % toda declaração \begin precisa de uma declaração end + +Podemos então referenciar nossa nova equação! +A equação~\ref{eq:pythagoras} é também conhecida como Teorema de Pitágoras que é +também assunto da Seção~\ref{subsec:pythagoras}. Muitas coisas podem ser +rotuladas: figuras, equações, seções, etc. + +Somatórios e Integrais são escritas com os comandos sum e int: + +% Alguns compiladores LaTeX irão reclamar se existirem linhas em branco +% em um bloco de equação. +\begin{equation} + \sum_{i=0}^{5} f_{i} +\end{equation} +\begin{equation} + \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x +\end{equation} + +\section{Figuras} + +Insiramos uma Figura. O local para colocar a figura pode ser difícil +de determinar. Eu tenho sempre que verificar as opções toda vez. + +\begin{figure}[H] % H aqui é uma opção para o local da figura. + \centering % centra a figura na página + % Inclui uma figura com escala de 0.8 do tamanho da página. + %\includegraphics[width=0.8\linewidth]{right-triangle.png} + % Comentado para propósitos de compilação. Por favor, use sua imaginação. + \caption{Triângulo retângulo com lados $a$, $b$, $c$} + \label{fig:right-triangle} +\end{figure} + +\subsection{Tabelas} +Também podemos incluir tabelas da mesma forma que figuras. + +\begin{table}[H] + \caption{Título para a Tabela.} + % os argumentos {} abaixo descrevem como cada linha da tabela é desenhada. + % Aqui também, Preciso ver isso. Toda. E. Cada. Vez. + \begin{tabular}{c|cc} + Número & Sobrenome & Primeiro Nome \\ % Colunas são separadas por & + \hline % uma linha horizontal + 1 & Biggus & Dickus \\ + 2 & Monty & Python + \end{tabular} +\end{table} + +\section{Fazendo o \LaTeX \hspace{1pt} não compilar algo (o código fonte)} +Digamos que precisamos incluir algum código dentro do nosso +documento \LaTeX \hspace{1pt}, para isso precisamos com o \LaTeX \hspace{1pt} +não tente interpretar esse texto e que apenas inclua ele no documento. Fazemos +isso com o bloco verbatim. + +% Existem outros pacotes (por exemplo, minty, lstlisting, etc.) +% mas verbatim é o básico +\begin{verbatim} + print("Hello World!") + a%b; % olha só! Podemos usar os sinais % no bloco verbatim. + random = 4; #decided by fair random dice roll +\end{verbatim} + +\section{Compilando} + +Imagino que agora você esteja pensando como compilar esse fantástico documento +e visualizar a gloriosa glória que é um pdf gerado por \LaTeX \hspace{1pt} pdf. +(sim, esse documento é compilável). \\ + +Finalizando o documento usando \LaTeX \hspace{1pt} consiste nos seguintes passos: + \begin{enumerate} + \item Escrever o documento em texto puro (o ``código fonte''). + \item Compilar o código fonte para gerar um pdf. + Os passos para compilar se parecem (em Linux) com: \\ + \begin{verbatim} + > pdflatex learn-latex.tex + \end{verbatim} + \end{enumerate} + +Existem editores de \LaTeX \hspace{1pt} que combinam os passos 1 e 2 no mesmo +sistema de software. Assim, você pode ver o passo 1, mas não o passo 2 por +completo. Passo 2 estará acontecendo escondido\footnote{Por exemplo, quando usar +referências (como Equação~\ref{eq:pythagoras}), pode ser necessário executar o +passo 2 várias vezes, para gerar arquivos *.aux intermediários.}. +% É assim que você adiciona notas de rodapé em seus documentos! + +Você escreve toda a informação de formatação em texto puro, no passo 1. O +momento da compilação no passo 2 é responsável por produzir o documento no +formato que você definiu no passo 1. + +\section{Links} +Nós podemos inserir links em nosso documento. Para isso nós necessitamos incluir +o pacote hyperref no preâmbulo com o comando: +\begin{verbatim} + \usepackage{hyperref} +\end{verbatim} + +Existem dois tipos principais de links: URL visíveis \\ +\url{https://learnxinyminutes.com/docs/latex/}, ou +\href{https://learnxinyminutes.com/docs/latex/}{um texto alternativo} +% Você não pode adicionar espaços extras ou símbolos especiais no texto +% alternativo, pois isso causará problemas na compilação. + +Esse pacote também produz uma lista de thumbnails no documento pdf gerado e +ativa os links na tabela de conteúdo. + +\section{End} + +Por enquanto é isso! + +% Frequentemente você precisa de uma seção de referências em seu documento. +% A forma mais fácil de configurá-la é usando uma seção de bibliografia +\begin{thebibliography}{1} + % como em outras listas, o comando \bibitem pode ser usado para itens da lista + % cada entrada pode ser citada diretamente no corpo do texto + \bibitem{latexwiki} The amazing \LaTeX \hspace{1pt} wikibook: {\em +https://en.wikibooks.org/wiki/LaTeX} + \bibitem{latextutorial} An actual tutorial: {\em http://www.latex-tutorial.com} +\end{thebibliography} + +% end the document +\end{document} +``` + +## Mais sobre LaTeX + +* The amazing LaTeX wikibook: [https://en.wikibooks.org/wiki/LaTeX](https://en.wikibooks.org/wiki/LaTeX) +* An actual tutorial: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/) 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/perl-pt.html.markdown b/pt-br/perl-pt.html.markdown index cc07a2ec..217861f9 100644 --- a/pt-br/perl-pt.html.markdown +++ b/pt-br/perl-pt.html.markdown @@ -21,7 +21,7 @@ Perl 5 roda em mais de 100 plataformas, de portáteis a mainframes e é adequada # Variáveis iniciam com um sigilo, que é um símbolo que mostra o tipo. # Um nome de variável válido começa com uma letra ou sublinhado, -# seguido por qualquer número de letras, números ou sublinhados. +# seguido por qualquer quantidade de letras, números ou sublinhados. ### Perl has three main variable types: $scalar, @array, e %hash. @@ -52,10 +52,10 @@ my %fruta_cor = ( banana => "amarelo", ); -# Scalars, arrays and hashes são documentados mais profundamentes em perldata. +# Scalars, arrays and hashes são documentados mais profundamente em perldata. # (perldoc perldata). -# Mais tipos de dados complexos podem ser construídas utilizando referências, +# Mais tipos de dados complexos podem ser construídos utilizando referências, # o que permite que você crie listas e hashes dentro de listas e hashes. #### Condicionais e construtores de iteração diff --git a/pt-br/pyqt-pt.html.markdown b/pt-br/pyqt-pt.html.markdown new file mode 100644 index 00000000..10d55784 --- /dev/null +++ b/pt-br/pyqt-pt.html.markdown @@ -0,0 +1,92 @@ +--- +category: tool +tool: PyQT +filename: learnpyqt-pt.py +contributors: + - ["Nathan Hughes", "https://github.com/sirsharpest"] +translators: + - ["Lucas Pugliesi", "https://github.com/fplucas"] +lang: pt-br +--- + +**Qt** é amplamente conhecido como um framework para desenvolvimento de +software multi-plataforma que pode rodar em vários outras plataformas de +softwares e hardwares com pouca ou nenhuma alteração no código, enquanto mantém +o poder e a velocidade de uma aplicação nativa. Embora o **Qt** tenha sido +originalmente escrito em *C++*. + + +Essa é uma adaptação de uma introdução ao QT em C++ por +[Aleksey Kholovchuk](https://github.com/vortexxx192), alguns dos exemplos de +código podem resultar na mesma funcionalidade que essa versão, apenas usando +o pyqt! + +```python +import sys +from PyQt4 import QtGui + +def window(): + # Cria um objeto para a aplicação + app = QtGui.QApplication(sys.argv) + # Cria um widget onde o nosso label será inserido + w = QtGui.QWidget() + # Adiciona um label ao widget + b = QtGui.QLabel(w) + # Informa algum texto ao label + b.setText("Hello World!") + # Define os tamanhos e posições dos objetos + w.setGeometry(100, 100, 200, 50) + b.move(50, 20) + # Define o título da janela + w.setWindowTitle("PyQt") + # Exibe a janela + w.show() + # Executa tudo o que foi pedido, apenas uma vez + sys.exit(app.exec_()) + +if __name__ == '__main__': + window() + +``` + +Para utilizar mais funcionalidades no **pyqt** veremos a construção de alguns +outros elementos. +Aqui mostraremos como criar uma janela popup, muito útil para perguntar ao +usuário qual decisão tomar ou exibir alguma informação. + +```Python +import sys +from PyQt4.QtGui import * +from PyQt4.QtCore import * + + +def window(): + app = QApplication(sys.argv) + w = QWidget() + # Cria um botão e o anexa ao widget w + b = QPushButton(w) + b.setText("Press me") + b.move(50, 50) + # Informa b a chamar essa função quando for clicado + # observe que a função chamada não necessita de "()" + b.clicked.connect(showdialog) + w.setWindowTitle("PyQt Dialog") + w.show() + sys.exit(app.exec_()) + +# Essa função deve criar uma janela de diálogo com um botão, +# aguarda ser clicado e encerra o programa +def showdialog(): + d = QDialog() + b1 = QPushButton("ok", d) + b1.move(50, 50) + d.setWindowTitle("Dialog") + # Essa modalidade define que o popup deve bloquear as outras janelas quando ativo + d.setWindowModality(Qt.ApplicationModal) + # Ao ser clicado deve encerrar o processo + b1.clicked.connect(sys.exit) + d.exec_() + +if __name__ == '__main__': + window() +``` diff --git a/pt-br/qt-pt.html.markdown b/pt-br/qt-pt.html.markdown new file mode 100644 index 00000000..99579c35 --- /dev/null +++ b/pt-br/qt-pt.html.markdown @@ -0,0 +1,174 @@ +--- +category: tool +tool: Qt Framework +language: c++ +filename: learnqt-pt.cpp +contributors: + - ["Aleksey Kholovchuk", "https://github.com/vortexxx192"] +translators: + - ["Lucas Pugliesi", "https://github.com/fplucas"] +lang: pt-br +--- + +**Qt** é amplamente conhecido como um framework para desenvolvimento de +software multi-plataforma que pode rodar em vários outras plataformas de +softwares e hardwares com pouca ou nenhuma alteração no código, enquanto mantém +o poder e a velocidade de uma aplicação nativa. Embora o **Qt** tenha sido +originalmente escrito em *C++*, é possível utilizá-lo em outras linguagens: +*[PyQt](https://learnxinyminutes.com/docs/pyqt/)*, *QtRuby*, *PHP-Qt*, etc. + +**Qt** é ótimo para criar aplicações com interface gráfica (GUI). Esse tutorial +será feito em *C++*. + +```c++ +/* + * Vamos começar + */ + +// Todos as dependências do framework Qt iniciam com a letra 'Q' maiúscula +#include <QApplication> +#include <QLineEdit> + +int main(int argc, char *argv[]) { + // Cria um objeto para utilizar todos os recursos da aplicação + QApplication app(argc, argv); + + // Cria um widget com linha editável e exibe na tela + QLineEdit lineEdit("Hello world!"); + lineEdit.show(); + + // Inicia a aplicação em um evento de loop + return app.exec(); +} +``` + +A parte gráfica do **Qt** é toda composta de *widgets* e *conexões* entre eles. + +[LEIA MAIS SOBRE WIDGETS](http://doc.qt.io/qt-5/qtwidgets-index.html) + +```c++ +/* + * Vamos criar um label e um botão. + * Um label irá aparecer quando o botão for clicado + * + * O próprio código do Qt é autoexplicativo. + */ + +#include <QApplication> +#include <QDialog> +#include <QVBoxLayout> +#include <QPushButton> +#include <QLabel> + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + + QDialog dialogWindow; + dialogWindow.show(); + + // Adiciona um layout vertical + QVBoxLayout layout; + dialogWindow.setLayout(&layout); + + QLabel textLabel("Thanks for pressing that button"); + layout.addWidget(&textLabel); + textLabel.hide(); + + QPushButton button("Press me"); + layout.addWidget(&button); + + // Exibe o label oculto quando o botão é clicado + QObject::connect(&button, &QPushButton::pressed, + &textLabel, &QLabel::show); + + return app.exec(); +} +``` + +Veja o *QObject::connect*. O método é usado para conectar o *SINAL* de um objeto +ao *ENCAIXE* outro. + +**Sinais** são emitidos quando algo ocorre com o objeto, como quando o sinal de +*clique* é acionado apertando o QPushButton. + +**Encaixes** são *ações* que são executadas em resposta aos sinais recebidos. + +[LEIA MAIS SOBRE SINAIS E ENCAIXES](http://doc.qt.io/qt-5/signalsandslots.html) + + +A seguir vamos aprender como usar não somente o comportamento padrão dos +widgets, mas também extender seus comportamentos usando herança. Vamos criar um +botão e contar quantas vezes é pressionado. Para esse propósito definiremos +nossa própria classe *CounterLabel*. Ela deve ser declarada em um arquivo +diferente devido a estrutura específica do Qt. + +```c++ +// counterlabel.hpp + +#ifndef COUNTERLABEL +#define COUNTERLABEL + +#include <QLabel> + +class CounterLabel : public QLabel { + Q_OBJECT // Define os macros presente em todo objeto Qt + +public: + CounterLabel() : counter(0) { + setText("Counter has not been increased yet"); // método do QLabel + } + +public slots: + // Ação que será chamada em resposta ao clique do botão + void increaseCounter() { + setText(QString("Counter value: %1").arg(QString::number(++counter))); + } + +private: + int counter; +}; + +#endif // COUNTERLABEL +``` + +```c++ +// main.cpp +// Quase igual ao exemplo anterior + +#include <QApplication> +#include <QDialog> +#include <QVBoxLayout> +#include <QPushButton> +#include <QString> +#include "counterlabel.hpp" + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + + QDialog dialogWindow; + dialogWindow.show(); + + QVBoxLayout layout; + dialogWindow.setLayout(&layout); + + CounterLabel counterLabel; + layout.addWidget(&counterLabel); + + QPushButton button("Push me once more"); + layout.addWidget(&button); + QObject::connect(&button, &QPushButton::pressed, + &counterLabel, &CounterLabel::increaseCounter); + + return app.exec(); +} +``` + +É isso! Claro, o framework Qt é muito maior do que exemplificamos no tutorial, +então esteja preparado para ler e praticar mais. + +## Leitura complementar + +- [Tutoriais Qt 4.8](http://doc.qt.io/qt-4.8/tutorials.html) +- [Tutoriais Qt 5](http://doc.qt.io/qt-5/qtexamplesandtutorials.html) + +Boa sorte e divirta-se! diff --git a/rust-pt.html.markdown b/pt-br/rust-pt.html.markdown index 79f69269..8134d3c5 100644 --- a/rust-pt.html.markdown +++ b/pt-br/rust-pt.html.markdown @@ -3,8 +3,8 @@ language: rust filename: rust-pt.rs contributors: - ["Paulo Henrique Rodrigues Pinheiro", "https://about.me/paulohrpinheiro"] -filename: learnrust.rs lang: pt-br + --- Rust é uma linguagem de programação desenvolvida pelo Mozilla Research. Rust diff --git a/pt-br/solidity-pt.html.markdown b/pt-br/solidity-pt.html.markdown new file mode 100644 index 00000000..d4555fa7 --- /dev/null +++ b/pt-br/solidity-pt.html.markdown @@ -0,0 +1,914 @@ +--- +language: Solidity +filename: learnSolidity-br.sol +contributors: + - ["Nemil Dalal", "https://www.nemil.com"] + - ["Joseph Chow", ""] +translators: + - ["João Farias", "http://thatsabug.com/"] +lang: pt-br +--- + +Solidity permite você programar para a [Ethereum] +(https://www.ethereum.org/), uma máquina virtual baseada na tecnologia blockhain +para criação e execução de contratos inteligentes, sem necessidade de partes +centralizadas ou de confiança. + +Solidity é uma linguagem de contratos estaticamente tipaada com similaridade com +Javascript e C. Como objetos em programação orientada a objetos, cada contrato +possue variáveis de estado, funções e tipos de dados comuns. Funcionalidades +particulares de contratados incluem cláusuras modificadoras (guarda), notifica +dores de eventos para listerners e variáveis globais customizadas. + + +Exemplos de contratos Ethereum incluem crowdfunding, votações e audições cegas. + +Erros em código Solidity causam grandes riscos e custos; portanto, você +deve ser muito cuidado com teste e divulgação. DEVIDO ÀS CONSTANTES MUDANÇAS +NO ETHEREUM, ESTE DOCUMENTOS PROVAVELMENTE NÃO ESTARÁ ATUALIZADO, VOCÊ DEVE +ACOMPANHAR A CHATROOM DO SOLIDITY E O BLOG DO ETHEREUM PARA NOTÍCIAS ATUALIZADAS. +TODO CÓDIGO AQUI É ENTREGUE COMO ESTÁ, COM SUBSTANCIAL RISCO DE ERRROS E PADRÕES +DE CÓDIGO DEPRECADOS. + +Diferentemente de outros tipo de código, você também deve adicionar padrões +como pausa, deprecação e retração para reduzir riscos. Este documento discute +sintaxe, então, muito padrões populares são excluídos. + +Como Solidity e Ethereum ainda estão sob desenvolvimento, funcionalidades beta +e experimentais são tipicamente marcadas e sujeitas à mudanças. Pull requests +são bem-vindos. + +```javascript +// Primeiramente, um contrato de um Banco simples +// Permite depósitos, retiradas e checagens de saldo + +// banco_simples.sol (note a extensão .sol) + +/* **** INCICIO DO EXEMPLO **** */ + +// Declare a versão do compilador. +pragma solidity ^0.4.2; + +// Inicie com comentários Natspec (as três barras) +// usados para documentação - e como dados descritivos para elementos/ação de UI + +/// @title BancoSimples +/// @author nemild + +/* 'contrato' tem similadirades com 'classes' em outras linguagens (variáveis de +class, herança, etc.) */ + +contract BancoSimples { // CamelCase + // Declare variáveis de estado fora da função, para persistí-la durante a + // duração do contrato + + // dicionário que mapeia endereços para saldos + // tenha cuidado sobre ataques de overflow com números + + mapping (address => uint) private saldos; + + // "private" significa que outros contratos não podem acessar saldos + // diretamente, mas o dado ainda é visível para outras partes da blockchain + + address public dono; + + // ´public´ é legível (mas sem acesso de escrita) por usuários e contratos + + // Eventos - ações públicas para ouvintes externo + event LogRealizacaoDeDeposito(address numeroDaConta, uint quantidade); + + // Construtor, pode receber uma ou várias variáveis; apenas uma opção é + // permitidas + + function BancoSimples() { + // msg dá detalhes sobre a mensagem mandada pelo contrato + // msg.sender é um chamador do contrato (endereço do criador do + // contrato) + + dono = msg.sender; + } + + /// @notice Deposita ether no banco + /// @return O saldo do usuário após o depósito + + function deposito() public returns (uint) { + saldos[msg.sender] += msg.value; + + // Sem necessidade de "this." ou "self." para variáveis de estado + // todos as variáveis são inciadas com seu valor default + + LogRealizacaoDeDeposito(msg.sender, msg.value); // dispara evento + + return saldos[msg.sender]; + } + + /// @notice Retira ether do banco + /// @dev Isto não retorna nenhum ether excendente + /// @param quantidadeDeRetirada quantidade que você quer retirar + /// @return O saldo restante do usuário + function retirada(uint quantidadeDeRetirada) public returns (uint saldoRestate) { + if(saldos[msg.sender] >= quantidadeDeRetirada) { + + // Observe como deduzimos o saldo imediatamente, antes de enviar - + // devido ao risco de uma chamada recursiva que permite o chamador + // pedir um valor maior que seu saldo + + saldos[msg.sender] -= quantidadeDeRetirada; + + if (!msg.sender.send(quantidadeDeRetirada)) { + // incremente de volta só se falhar, como pode estar enviando + // para o contrato que substituiu 'enviar' no final + // do recebimento + saldos[msg.sender] += quantidadeDeRetirada; + } + } + + return saldos[msg.sender]; + } + + /// @notice Retorna o saldo + /// @return O saldo do usuário + // 'constant' evita que a função edite variáveis de estado + // permite a função executar localmente/fora da blockchain + function saldo() constant returns (uint) { + return saldos[msg.sender]; + } + + // Função de fallback - Chamada se outras funções não forem chamadas ou + // se ether sem dados forem enviados + // Tipicamente chamada quando dados inválidos são enviados + // Adicionada para que ether enviado neste contrato seja revertido se o + // contrato falhar. Se não existisse, o dinheiro do enviante é transferido + // para o contrato + function () { + throw; // 'throw' reverte o estao para antes da chamada + } +} +// ** FIM DO EXEMPLO ** + +// Agora, o básico de Solidity + + +//1 TIPO DE DADOS E MÉTODOS ASSOCIADOS +// uint é usado para quantidade de moeda (não existem doubles ou floats) +// e para datas (no sistema de tempo Unix) + +uint x; + +// int de 256 bits, não pode ser mudado após sua instanciação +int constant a = 8; +int256 constant a = 8; // mesmo efeito, mas aqui os 256 bits são explícitos +uint constant VERSÃO_ID = 0x123A1; // uma constante hexadecimal + +// com 'constant', o compilador substitui cada ocorrência com o valor + +// Para int e uint, é possível determinar o espaço explicitamente, em intervalos +// de 8 a 256, e.g., int8, int16, int24 +uint8 b; +int64 c; +uint248 e; + +// Cuidado contra overflows, e proteja-se contra esse tipo de ataque + +// Não há funções randômicas padrão, use outros contratos para este objetivo + +// Casting de tipo +int x = int(b); + +bool b = true; // ou então 'var b = true;' para inferição de tipo + +// Endereços - comportam 20 bytes/160 bits endereços Ethereum +// Não permite operações aritiméticas +address public dono; + +// Tipos de contas: +// Conta de contrato: endereço criado ao criar (função do endereço do criador, +// número da transação) +// Conta externa: (pessoa/entidade externa): endereç criado a partir de chave +// pública + +// Adicione o campo 'public' para indicar visibilidade pública/externa +// um getter é automaticamente criado, mas NÃO um setter + +// Todos os endereços podem enviar ether +dono.send(ALGUM_SALDO); // returna falso caso falhe +if (dono.send) {} // LEMBRE-SE: encapsule num 'if', dado que endereços de +// contrato tem funções executadas no envio e estas podem falhar +//Também certifique-se que os saldos deduzidos ANTES de tentar enviar, dado que +// há um risco de chamada recursiva que pode drenar um contrato + +// pode sobrescrever seu próprio + +// Pode verificar o saldo +dona.balance; // o saldo do dono (usuário ou contrato) + +// Bytes permitidos de 1 a 32 +byte a; // byte é o mesmo que bytes1 +bytes2 b; +bytes32 c; + +// Bytes dinamicamente criados + +bytes m; // Um array especial, mesmo que byte[] (mas mais comprimido) + +// Mais custoso que byte1-byte32, então, prefira estes quando possível + +// mesmo que bytes, mas não permite verificar tamanho ou acesso por indíce (por +// enquanto) + +string n = "oi"; // guardado em UTF8, note as aspas duplas, não simples + +// funções de string serão adicionadas no futuro +// prefira bytes32/bytes, dado que UTF8 usa mais espaço + +// Inferência de tipo +// var não infere tipos baseados na primeira atribuição, +// não pode ser usado em paramêtros de funções + +var a = true; + +// use com cuidado, inferência pode resultar em tipos errados +// e.g., um int8, quando um contador precisa de int16 + +// var pode ser usado para assinalar uma função a uma variável +function a(uint x) returns (uint) { + return x * 2; +} +var f = a; +f(22); // chamada + +// por padrão, todos os valores são inicializados com 0 + +// Delete pode ser chamada na maioria dos tipos +// (NÃO destroi o valor, mas retorna para o valor 0, o incial) + +uint x = 5; + +// Desestruturação/Tuplas +(x, y) = (2, 7); // assinada/troca múltiplos valores + +// 2. ESTRUTURAS DE DADOS +// Arrays + +bytes32[5] apelidos; // array estático +bytes32[] nomes; // array dinâmico +uint novoTamanho = nomes.push("João"); // adicionando retorna o novo tamanho do + +// Tamanho +nomes.length; // pega o tamanho +nomes.length = 1; // tamanhos pode ser alterados (para arrays dinâmicos) + +// arrays multidimensionais +uint x[][5]; // array com 5 arrays dinâmicos como elementos (ordem da maioria +// das linguagens) + +// Dicionários (qualquer tipo para qualquer tipo) +mapping (string => uint) public saldos; +saldos["charles"] = 1; +console.log(saldos["ada"]); // é 0, toda chave não assinalada retorna zero +// 'public' permite o seguinte contrato +nomeDoContrato.saldos("charles"); // retorna 1 +// 'public' cria um getter (mas não um setter) como o seguinte +function saldos(string _conta) returns (uint saldo) { + return saldos[_conta]; +} + +// Mapeamentos aninhados +mapping (endereco => mapping (endereco => uint)) public guardioes; + +// Para deletar +delete saldos["John"]; +delete saldos; // assinala zero para todas as chaves + +// Diferentemente de outras linguages, NÃO É POSSÍVEL iterar sobre todos os +// elementos de um mapeamento, sem saber previamente as chaves - é possível +// construir estruturas de dados personalizadas para fazer isso + +// Structs e enums +struct Banco { + address dono; + uint saldo; +} +Banco b = Banco({ + dono: msg.sender, + saldo: 5 +}); +// ou +Banco c = Banco(msg.sender, 5); + +c.quantidade = 5; // cria novo valor +delete b; +// assinala todos os valores do enum para zero, exceto mapeamentos + +// Enums +enum Estado { Criado, Travado, Inativo }; // geralmente usado para máquina de +// estados +Estado public estado; // Declara variável para enum +estado = Estado.Criado; +// enums podem ser explicitamente convertidas em ints +uint estadoCriado = uint(Estado.Criado); // 0 + +// Localização de dados: Memória vs. disco vs. pilha - todos os tipos complexos +// (arrays, structs) tem uma localização de dados +// 'memória' não é persistida, 'disco' é +// Padrão é 'disco' para variáveis locais e de estado; 'memória' para paramêtros +// de função. Pilha guarda pequena variáveis locais + +// a maioria dos tipos podem ter sua localização de dados explicitamente assinalos + +// 3. Operações simples +// Comparações, operadores binários e aritimétricos são providos +// exponenciação: ** +// ou exclusivo: ^ +// negação binária: ~ + +// 4. Variáveis Globais de nota +// ** this ** +this; // endereço do contrato +// geralmente usado no final do contrato para enviar o saldo restante às partes +this.balance; +this.algumFuncao(); // chamada de função externa via call, não via jump interno + +// ** msg - Mensagem corrente recebida pelo contrato ** ** +msg.sender; // endereço do enviador +msg.value; // quantidade de ether provida para este contrato em wei +msg.data; // bytes, todos os dados da chamada +msg.gas; // gas restante + +// ** tx - Esta transação ** +tx.origin; // endereço do enviador da transação +tx.gasprice; // valor do gas da transação + +// ** block - Informação do bloco corrente ** +now; // tempo corrente (aproxiamdo), substituto para block.timestamp +//(usa tempo do Unix) +block.number; // número do bloco corrente +block.difficulty; // dificuldade do bloco corrente +block.blockhash(1); // retorna bytes32, só funciona para os 256 blocos mais +//recentes +block.gasLimit(); + +// ** storage - Hash de disco persistente ** +storage['abc'] = 'def'; // mapea palavras de 256 bits em palavras de 256 bits + + +// 4. FUNÇÕES E MAIS +// A. Funções +// Funções simples +function incremento(uint x) returns (uint) { + x += 1; + return x; +} + +// Funções podem retornar muito argumentos, e podem especificar argumentos +// retornados sem necessidade de explicitamente usar return +function incremento(uint x, uint y) returns (uint x, uint y) { + x += 1; + y += 1; +} +// Chamando a função anterior +uint (a,b) = incremento(1,1); + +// 'constant' indica que uam função não altera variáveis persistidas +// Funções constantes são executadas localmente, não na blockchain +uint y; + +function incremento(uint x) constant returns (uint x) { + x += 1; + y += 1; // Esta linha deve falhar + // y é uma variável de estado e não pode ser alterada por uma função local +} + +// 'Especificadores de visibilidade de funções' +// Estes podem substituitir 'constante', incluíndo: +// public - visbilidade externa e interna (padrão) +// private - apenas visível no contrato corrente +// internal - apenas visível no contrato corrente e seus derivados + +// Functions hosteada - e pode ser assinalada para variável +function a() { + var z = b; + b(); +} + +function b() { + +} + +// Prefira loops sobre recursões (pilha de chamada é no máximo 1024) + +// B. Eventos +// Eventos podem notificar partes externas; facilmente buscáveis e acessáveis +// de fora da blockchain (com clientes leves) +// tipicamente declarados após os parâmetros do contrato + +// Tipicamente, com letra maiúscula - e adicione Log na frente para +// ser explicito e previnir confusão na chamada da função + +// Declaração +event LogEnvio(address indexed de, address indexed para, uint quantidade); +// Observe a letra maíscula no início do nome + +// Chamada +Envio(de, para, quantidade); + +// Para partes externas (um contrato ou entidade externo), observe: +Coin.Envio().watch({}, '', function(erro, resultado) { + if (!erro) { + console.log("Moeda transferida: " + resultado.args.quantidade + + " moedas enviadas de " + resultado.args.de + + " para " + resultado.args.para + "."); + console.log("Saldo atual:\n" + + "Enviador: " + Coin.balances.call(resultado.args.de) + + "Recebedor: " + Coin.balances.call(resultado.args.para)); + } +} +// Paradigma comum para um contrato depender de outro (e.g., um contrato que +// depende da taxa de troca provida por outro) + +// C. ModifiCadores +// MOdificadores validam entradas de funções, como saldo mínimo e autorização +// do usuário; semelhantes a guardas em outras linguagens + +// '_' (subtraço) geralmente incluído como última linha do corpo, indica que a +// função sendo chamada deve ser colocada ali +modifier apenasDepois(uint _tempo) { if (agora <= _tempo) throw; _ } +modifier apenasDono { if (msg.sender == dono) _ } +// geralmente usado para máquina de estado +modifier apenasSeEmEstado (Estado estadoCorrente) +{ if (estadoCorrente != Estado.A) _ } + +// Concatenado logo após a chamada da função +function mudeDona(novoDono) +apenasDepois(algumTempo) +apenasDono() +apenasSeEmEstado(Estado.A) +{ + dono = novoDono; +} + +// subtração pode ser incluído antes do final do corpo, mas retorno explícitos +// pode ser ignorado, então, tome cuidado +modifier chequeValor(uint quantidade) { + _ + if (msg.value > quantidade) { + uint quantidadeASerDevolvida = quantidade - msg.value; + if (!msg.sender.send(quantidadeASerDevolvida)) { + throw; + } + } +} + +// 6. BRANCHES E LOOPS + +// Todas as lógicas básicas de bloco funcionam - incluindo if/else, +// while, break, continue, return - mas não há switch + +// A sintaxe é semelhante a Javascript, mas sem conversão de tipos +// de não-booleanos para booleanos (operadores de comparação precisam +// utilizar valores booleanos) + +// Loops que dependem o comportamento do usuário exigem cuidado - dado +// que contratos tem uma quantidade máxima de gas para cada bloco de +// de código - falhas acontecerão caso ele seja excedido +// Por exemplo: +for(uint x = 0; x < listaDeEnderecoDeRefundo.length; x++) { + if (!listaDeEnderecoDeRefundo[x].send(ALGUMA_QUANTIDADE)) { + throw; + } +} + +// Dois erros acima: +// 1. Uma falha no enviar para o loop completamente, travando dinheiro +// 2. Este loop pode ser abitrariamente longo (basado na quando que +// o usuário precisa de refundo), portanto, pode falhar quando exceder +// a quantidade máxima de gas do bloco +// Ao invés disso, você deve deixar as pessoas retirarem +// individualmente de suas subcontas e marcarem a retirada + + +// 7. OBJETOS/CONTRATOS + +// A. Chamando um contrato externo +contract FonteDeInformacoes { + function info() returns (uint ret) { return 42; } +} + +contract Consumidor { + FonteDeInformacoes fonte; // aponta para um contrato na blockchain + + // Assinala variável para uma instância do contrato + function setFonte(address endereco) { + // Cast automático, cuidado; construtor não é chamado + fonte = FonteDeInformacoes(endereco); + } + + // Assinala variável para uma nova instância do contrato + function createNewFeed() { + fonte = new FonteDeInformacoes(); // nova instância criada + // construtor é chamado + } + + function chameFonte() { + // último parenteses chama o contrato, podendo adicionar + // opcionalmente valores ou gas + fonte.info.value(10).gas(800)(); + } +} + +// B. Herança + +// Ordem importa, último contrato herdado (i.e., 'def') pode +// sobrescrever partes de contratos previamente herdados +contract MeuContratdo is abc, def("um argumento personalizado def") { + +// sobrescrevendo função + function z() { + if (msg.sender == dono) { + def.z(); // chama função sobrescrita de def + super.z(); // chama função do pai imeadiato + } + } +} + +// função abstrata +function umaFuncaoAbstrata(uint x); +// não pode ser compilada, usada em contratos base/abstratos que +// então, a implementam + +// C. Import + +import "filename"; +import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol"; + +// 'Import' está sobre desenvolvimento +// Atualmente não pode ser usada na linha de comando + + +// 8.OUTRAS PALAVRAS-CHAVE + +// A. Throwing +// Throwing +throw; // reverte estado e dinheiro NÃO-USADO é devolvido ao usuários +// Atualmente não pode ser capturado + +// Um padrão de design comum é: +if (!endereco.send(123)) { + throw; +} + +// B. Selfdestruct +// auto-destroe o contrato corrente, enviando fundos para o endereço +// (geralmente o criador) +selfdestruct(ALGUM_ENDERECO); + +// remove disco/código dos blocos corrente e futuros +// ajuda clientes leves, mas dados persistidos continuam no blockchain + +// Padrão comum, permite ao dono finalizar o contrato e receber fundos +// restantes +function remover() { + if(msg.sender == criador) { // Apenas o criador do contrato pode + // fazer isso + selfdestruct(criador); // Inativa o contrato e retorna os fundos + } +} + +// Talvez queria desativar o contrato manualmente, ao invés de usar +// selfdestruct (ether enviado para contratos selfdestructed é perdido) + + +// 9. NOTAS SOBRE DESIGN DE CONTRATOS + +// A. Obfuscação +// Todas as variáveis são publicamente visíveis na blockchain, então +// qualquer coisa privada precisa ser obfuscada (e.g., hash com segredo) + +// Passo-a-pass: 1. Comprometa-se com algo, 2. Revele compromisso +sha3("quantidade_de_lance", "algum segredo"); // compromisso + +// chame a função reveal (revelar) do contrato no futuros +// mostrando o lance mais o segredo para foi hasheado com SHA3 +reveal(100, "meuSegredo"); + +// B. Otimização de disco +// Escrever na blockchain pode ser caro, dado que os dados são guardados +// para sempre. É encorajado que contratos inteligentes usem memória ( +// enventualmente, compilação será melhor, mas por enquanto é benéfico +// usar estruturas de dados simples - armazenando minimamente na +// blockchain) + +// Custo pode ser alto para item como arrays multidimensionais +// (custo para guardar os dados - não declarar variáveis) + +// C. Acesso de dados da blockchain + +// Não pode restringir humanos ou computadores de ler os conteúdos +// de transações ou estado de transações + +// Enquanto 'private' previne outros *contratos* de ler dados ] +// diretamente - qualquer outra parte ainda pode ler dados da blockchain + +// Todos os dados são armazedos na blockchain, para que qualquer um +// possa observar dados antigos e mudanças + +// D. Jobs Cron +// Contratos deve ser manualmente chamados para lidar com agendamentos +// baseados em tempo; podendo criar código externo para pingar +// regularmente ou prover incentivos (ether) para outros fazê-lo + +// E. Padrão Observador +// O Padrão Observador permite que você registre um inscritor e +// registre uma função a ser chamada pelo Oráculo (nota, Oráculos pagam +// pela ação executada). Similarmente à subscrição em Pub/sub + +// Este é um contrato abstrato, tanto as classes cliente como a +// servidor importam o cliente que deve ser implementado +contract AlgumOraculoCallback { + function OraculoCallback(int _valor, uint _tempo, bytes32 info) external; +} + +contract AlgumOráculo { + AlgumOraculoCallback[] callbacks; // array com todos os inscritos + + // Registra inscrito + function addInscrito(AlgumOraculoCallback a) { + callbacks.push(a); + } + + function notificar(valor, tempo, info) private { + for(uint i = 0;i < callbacks.length; i++) { + // todos os inscritos precisam implementar AlgumOraculoCallback + callbacks[i].OraculoCallback(valor, tempo, info); + } + } + + function facaAlgo() public { + // Código para fazer algo + + // Notifica todos os inscrito + notificar(_valor, _tempo, _info); + } +} + +// Agora, seu contrato cliente pode addInscrito importando +// AlgumOraculoCallback e registrando algum Oráculo + +// F. Máquinas de estado +// veja o exemplo abaixo para o enum Estado e o modificador noEstado + +// *** EXEMPLO: Um exemplo de crowdfunding example (similar ao +// Kickstarter) *** +// ** INCIO DO EXEMPLO ** + +// FundadorDoCrowdFunding.sol + +/// @title FundadorDoCrowdFunding +/// @author nemild +contract FundadorDoCrowdFunding { + // Variáveis assinaladas na crição pelo criador + address public criador; + address public recipiente; // criador pode ser diferente do Recipiente + uint public minALevantar; // requisito para pagar, pelo contrário + // os doadores recebem o dinheiro de volta + string urlDaCampanha; + byte constant versao = 1; + + // Estruturas de dados + enum Estado { + LevantandoFundos, + RefundoExpirado, + Sucesso + } + struct Contribuicao { + uint quantidade; + address contribuidor; + } + + // Variáveis de Estado + State public estado = Estado.LevantandoFundos; // incializado na criação + uint public totalLevantado; + uint public levantadoPor; + uint public completadoEm; + Contribution[] contribuidores; + + event LogRecebimentoDeFundos(address endereco, + uint quantidade, + uint totalAtual); + event LogFundosPagos(address enderecoDoRecebedor); + + modifier noEstado(Estado _estado) { + if (estado != _estado) throw; + _ + } + + modifier eOCriador() { + if (msg.sender != criador) throw; + _ + } + + // Aguarda 6 meses após o final do contrato para destruí-lo + modifier noFimDoContrato() { + if(!((estado == Estado.RefundoExpirado || estado == Estado.Sucesso) && + completadoEm + 6 months < now)) { + throw; + } + _ + } + + function FundadorDoCrowdFunding( + uint tempoEmHorasParaFundraising, + string _urlDaCampanha, + address _recipiente, + uint _minALevantar) + { + criador = msg.sender; + recipiente = _recipiente; + urlDaCampanha = _urlDaCampanha; + minALevantar = _minALevantar; + levantadoPor = now + (tempoEmHorasParaFundraising * 1 hours); + } + + function contribuir() + public + noEstado(Estado.LevantandoFundos) + { + contribuidores.push( + Contribuicao({ + quantidade: msg.value, + contribuidor: msg.sender + }) // use array, para podermos iterar + ); + totalLevantado += msg.value; + + LogRecebimentoDeFundos(msg.sender, msg.value, totalRaised); + + verifiqueSeLevantamentoFoiCompletadoOuExpirado(); + return contribuicoes.length - 1; // retorna id + } + + function verifiqueSeLevantamentoFoiCompletadoOuExpirado() { + if (totalLevantado > minALevantar) { + estado = Estado.Sucesso; + pagar(); + + // pode incentivar enviador que iniciou a mudanção de estado + } else if ( now > levantadoPor ) { + estado = Estado.RefundoExpirado; // backers podem coletar + // o fundo chamando receberRefundo(id) + } + completadoEm = now; + } + + function pagar() + public + emEstado(Estado.Sucesso) + { + if(!recipiente.send(this.balance)) { + throw; + } + + + LogFundosPagos(fundRecipient); + } + + function receberRefundo(id) + public + emEstado(Estado.RefundoExpirado) + { + if (contribuicoes.length <= id || id < 0 || contribuicoes[id].amount == 0 ) { + throw; + } + + uint quantidadeDeRefundo = contribuicoes[id].amount; + contribuicoes[id].amount = 0; + + if(!contribuicoes[id].contribuidor.send(quantidadeParaEnviar)) { + contribuicoes[id].amount = quantidadeParaEnviar; + return false; + } + + return true; + } + + function removerContrato() + public + eOCriador() + noFimDoContrato() + { + selfdestruct(msg.sender); + // criador recebe todo o dinheiro restante{ + + } + + function () { throw; } +} +// ** FIM DO EXEMPLO ** + +// 10. OUTRAS FUNÇÕES NATIVAS + +// Unidades monetárias +// Moeda é definida usando wei, menor quantidade de ether +uint quantidadeMin = 1 wei; +uint a = 1 finney; // 1 ether == 1000 finney +// Para outras unidades, veja: http://ether.fund/tool/converter + +// Unidades temporais +1 == 1 second // segundos +1 minutes == 60 seconds // Minutos + +// Pode multiplicar uma variável de tempo, dado que unidades não são guardadas +// na variável +uint x = 5; +(x * 1 days); // 5 dias + +// Cuidado com o salto de segundos / anos com declarações de igualdade para o tempo +// (em vez disso, prefira maior que / menor que) + +// Criptografia +// Todas as string passadas são concatenadas antes de realizar hashing +sha3("ab", "cd"); +ripemd160("abc"); +sha256("def"); + +// 11. Segurança + +// Bugs são desastrosos para contratos Ethereum - até padrões Solidity populares +// podem ser considerados anti-padrões + +// Veja links para segurança no final deste documento + +// 12. FUNÇÕES DE BAIXO NÍVELS +// call - baixo nível, geralmente não usada, não tem segurança de tipos +booleanSucesso = algumEnderecoDeContrato.call('nome_da_funcao', 'arg1', 'arg2'); + +// callcode - Código no endereço alvo executado no *contexto* do contrato +// de chamada. Fornece funcionalidade de biblioteca +algumEnderecoDeContrato.callcode('nome_da_funcao'); + + +// 13. NOTAS DE ESTILO +// Baseado no guia de estilo PEP8 do Python + +// Resumo rápido: +// 4 espaços para identação +// Duas linhas separam declaração de contratos (e outras declarações de alto nível) +// Evite espaços estranhos entre parênteses +// Pode omitir chaves curly para uma declaração de linha(if, for, etc) +// else deve ser colocado na própria linha + + +// 14. COMENTÁRIOS NATSPEC +// usado para documentação, comentários e UIs externos + +// Contrato natspec - sempre acima da definição do contrato +/// @title Título do Contrato +/// @author Nome do autor + +// Função natspec +/// @notice informações sobre o que funciona; mostrado quando a função é executada +/// @dev Documentação de função para desenvolvedor + +// Parâmetro de função / valor de retorno natspec +/// @param algumParametro Alguma descrição do que o parametro faz +/// @return Descrição do valor de retorno +``` + +## Recursos adicionais +- [Documetanção Solidity](https://solidity.readthedocs.org/en/latest/) +- [Guia de Estilo do Solidity](https://ethereum.github.io/solidity//docs/style-guide/): + O guia de estilo Ethereum é derivado do guia de estilo do Python [pep8](https://www.python.org/dev/peps/pep-0008/). +- [Editor de Browser Solidity](http://chriseth.github.io/browser-solidity/) +- [Gitter Solidity Chat room](https://gitter.im/ethereum/solidity) +- [Estratégias de projeto modular para contratos Ethereum](https://docs.erisindustries.com/tutorials/solidity/) + +## Contratos de Exemplo +- [Dapp Bin](https://github.com/ethereum/dapp-bin) +- [Solidity Baby Step Contracts](https://github.com/fivedogit/solidity-baby-steps/tree/master/contracts) +- [ConsenSys Contracts](https://github.com/ConsenSys/dapp-store-contracts) +- [State of Dapps](http://dapps.ethercasts.com/) + +## Segurança +- [Thinking About Smart Contract Security](https://blog.ethereum.org/2016/06/19/thinking-smart-contract-security/) +- [Smart Contract Security](https://blog.ethereum.org/2016/06/10/smart-contract-security/) +- [Hacking Distributed Blog](http://hackingdistributed.com/) + +## Informação excluída intencionalmente +- Libraries + +## Estilo +- [PEP8](https://www.python.org/dev/peps/pep-0008/) é usado como guia de estilo, + incluindo sua filosofia geral + +## Editores +- [Vim Solidity](https://github.com/tomlion/vim-solidity) +- Snippets de Editores ([Ultisnips format](https://gist.github.com/nemild/98343ce6b16b747788bc)) + +## Trabalhos Futuros +- Novas palavras-chave: protected, inheritable +- Lista de padrões de design comuns (throttling, RNG, atualização de versão) +- Padrões anti-segurança comuns + + +Sinta-se a vontade para enviar um pull request com quaisquer edições - ou email +para nemild - / at- / gmail diff --git a/pt-br/swift-pt.html.markdown b/pt-br/swift-pt.html.markdown index 4ad67a06..bf410352 100644 --- a/pt-br/swift-pt.html.markdown +++ b/pt-br/swift-pt.html.markdown @@ -1,12 +1,13 @@ --- language: swift +filename: learnswift-pt.swift contributors: - ["Grant Timmerman", "http://github.com/grant"] - ["Christopher Bess", "http://github.com/cbess"] translators: - ["Mariane Siqueira Machado", "https://twitter.com/mariane_sm"] lang: pt-br -filename: learnswift.swift + --- Swift é uma linguagem de programação para desenvolvimento de aplicações no iOS e OS X criada pela Apple. Criada para @@ -388,13 +389,13 @@ if mySquare === mySquare { // Podem conter métodos do mesmo jeito que classes. enum Suit { - case Spades, Hearts, Diamonds, Clubs + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } diff --git a/pt-br/visualbasic-pt.html.markdown b/pt-br/visualbasic-pt.html.markdown index 76cca567..2a7205cd 100644 --- a/pt-br/visualbasic-pt.html.markdown +++ b/pt-br/visualbasic-pt.html.markdown @@ -8,16 +8,16 @@ lang: pt-br filename: learnvisualbasic-pt.vb --- -```vb +``` Module Module1 module Module1 Sub Main () ' Uma visão geral de console de aplicativos do Visual Basic antes de - ' mergulharmos mais profundamente na linguagem + ' mergulharmos mais profundamente na linguagem. ' Aspas simples começam comentários. - ' Para Navegar este tutorial dentro do compilador do Visual Basic, + ' Para navegar neste tutorial dentro do compilador do Visual Basic, ' eu criei um sistema de navegação. ' Este sistema de navegação vai ser explicado conforme avançarmos no ' tutorial, e você vai entender o que isso significa. @@ -93,16 +93,16 @@ module Module1 Private Sub HelloWorldInput () Console.Title = " Olá Mundo YourName | Saiba X em Y Minutes" ' Variáveis - 'Os dados inseridos por um usuário precisa ser armazenada . + 'Os dados inseridos por um usuário precisam ser armazenados. ' As variáveis também começar com um Dim e terminar com um Como VariableType . - ' Neste tutorial, nós queremos saber o que o seu nome, e faça o programa + ' Neste tutorial, nós queremos saber qual é o seu nome, e faça o programa ' Responder ao que é dito. Nome de usuário Dim As String " Nós usamos string como string é uma variável de texto baseado . Console.WriteLine (" Olá, Qual é o seu nome? ") ' Peça ao usuário seu nome. - username = Console.ReadLine () ' armazena o nome usuários. - Console.WriteLine (" Olá " + nome do usuário) " A saída é Olá ' Seu nome ' + username = Console.ReadLine () ' armazena o nome do usuário. + Console.WriteLine (" Olá " + username) ' A saída é "Olá < seu nome >". Console.ReadLine () ' Outsputs acima. ' O código acima irá lhe fazer uma pergunta seguiu imprimindo sua resposta. " Outras variáveis incluem Integer e usamos inteiro para números inteiros. diff --git a/pt-br/whip-pt.html.markdown b/pt-br/whip-pt.html.markdown new file mode 100644 index 00000000..989bae05 --- /dev/null +++ b/pt-br/whip-pt.html.markdown @@ -0,0 +1,247 @@ +--- +language: whip +contributors: + - ["Tenor Biel", "http://github.com/L8D"] + - ["Saurabh Sandav", "http://github.com/SaurabhSandav"] +author: Tenor Biel +author_url: http://github.com/L8D +translators: + - ["Paulo Henrique Rodrigues Pinheiro", "https://github.com/paulohrpinheiro"] +lang: pt-br +filename: whip-pt.lisp +--- + +Whip é um dialeto de Lisp feito para construir scripts e trabalhar com +conceitos mais simples. +Ele também copia muitas funções e sintaxe de Haskell (uma linguagem não correlata) + +Esse documento foi escrito pelo próprio autor da linguagem. Então é isso. + +```scheme +; Comentário são como em Lisp. Pontos-e-vírgulas... + +; A maioria das declarações de primeiro nível estão dentro de "listas" +; que nada mais são que coisas entre parêntesis separadas por espaços em branco +nao_é_uma_lista +(uma lista) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 1. Números, texto e operadores + +; Whip tem um tipo numérico (que é um double de 64 bits IEE 754, do JavaScript) +3 ; => 3 +1.5 ; => 1.5 + +; Funções são chamadas se elas são o primeiro elemento em uma lista +(funcao_chamada argumentos) + +; A maioria das operações são feitas com funções +; Todas as funções aritméticas básicas são bem diretas +(+ 1 1) ; => 2 +(- 2 1) ; => 1 +(* 1 2) ; => 2 +(/ 2 1) ; => 2 +; até mesmo o módulo +(% 9 4) ; => 1 +; Divisão não inteira ao estilo JavaScript. +(/ 5 2) ; => 2.5 + +; Aninhamento de listas funciona como esperado. +(* 2 (+ 1 3)) ; => 8 + +; Há um tipo boleano. +true +false + +; Textos são criados com ". +"Hello, world" + +; Caracteres são criados com '. +'a' + +; Para negação usa-se a função 'not'. +(not true) ; => false +(not false) ; => true + +; Mas a maioria das funções não-haskell tem atalhos +; o não atalho é um '!'. +(! (! true)) ; => true + +; Igualdade é `equal` ou `=`. +(= 1 1) ; => true +(equal 2 1) ; => false + +; Por exemplo, inigualdade pode ser verificada combinando as funções +;`not` e `equal`. +(! (= 2 1)) ; => true + +; Mais comparações +(< 1 10) ; => true +(> 1 10) ; => false +; e suas contra partes para texto. +(lesser 1 10) ; => true +(greater 1 10) ; => false + +; Texto pode ser concatenado com +. +(+ "Hello " "world!") ; => "Hello world!" + +; Você pode usar as características comparativas do JavaScript. +(< 'a' 'b') ; => true +; ... e coerção de tipos +(= '5' 5) + +; As funções `at` ou `@` acessarão caracteres de um texto, começando em 0. +(at 0 'a') ; => 'a' +(@ 3 "foobar") ; => 'b' + +; Também existem as variáveis `null` e `undefined`. +null ; usada para indicar a ausência de algum valor +undefined ; usada para indicar que um valor não foi informado + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 2. Variáveis, matrizes e dicionários + +; Variáveis são declaradas com as funções `def` ou `let`. +; Variáveis que não tiveram valor atribuído serão `undefined`. +(def some_var 5) +; `def` deixará a variável no contexto global. +; `let` deixará a variável no contexto local, e tem uma sintaxe estranha. +(let ((a_var 5)) (+ a_var 5)) ; => 10 +(+ a_var 5) ; = undefined + 5 => undefined + +; Matrizes são listas de valores de qualquer tipo. +; Elas basicamente são listas sem funções no início +(1 2 3) ; => [1, 2, 3] (sintaxe JavaScript) + +; Dicionários em Whip são o equivalente a 'object' em JavaScript ou +; 'dict' em python ou 'hash' em Ruby: eles s]ão uma coleção desordenada +de pares chave-valor. +{"key1" "value1" "key2" 2 3 3} + +; Chaves podem ser apenas identificadores, números ou texto. +(def my_dict {my_key "my_value" "my other key" 4}) +; Mas em Whip, dicionários são parceados como: valor, espaço, valor; +; com mais espaço entre cada. Então isso significa que +{"key" "value" +"another key" +1234 +} +é avaliado da mesma forma que +{"key" "value" "another key" 1234} + +; Dicionários podem ser acessados usando a função `at` +; (como em texto e listas) +(@ "my other key" my_dict) ; => 4 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 3. Lógica e controle de fluxo + +; A função `if` é muito simples, ainda que muito diferente do que em muitas +linguagens imperativas. +(if true "returned if first arg is true" "returned if first arg is false") +; => "returned if first arg is true" + +; E por conta do legado operador ternário +; `?` é o atalho não utilizado para `if`. +(? false true false) ; => false + +; `both` é uma declaração lógica `and`, e `either` é o `or` lógico. +(both true true) ; => true +(both true false) ; => false +(either true false) ; => true +(either false false) ; => false +; E seus atalhos são +; & => both +; ^ => either +(& true true) ; => true +(^ false true) ; => true + +;;;;;;;;; +; Lambdas + +; Lambdas em Whip são declaradas com as funções `lambda` ou `->`. +; E funções são na verdade lambdas com nomes. +(def my_function (-> (x y) (+ (+ x y) 10))) +; | | | | +; | | | valor retornado (com escopo contento argumentos) +; | | argumentos +; | declaração de funções lambda +; | +; nome do lambda a ser declarado + +(my_function 10 10) ; = (+ (+ 10 10) 10) => 30 + +; Obviamente, todos os lambdas por definição são anônimos e +; tecnicamente sempre usados anonimamente. Redundância. +((lambda (x) x) 10) ; => 10 + +;;;;;;;;;;;;;;;; +; Comprehensions + +; `range` or `..` geram uma lista dos números para +; cada número entre seus dois argumentos. +(range 1 5) ; => (1 2 3 4 5) +(.. 0 2) ; => (0 1 2) + +; `map` aplica seu primeiro argumento (que deve ser um lambda/função) +; a cada item dos argumentos seguintes (que precisa ser uma lista) +(map (-> (x) (+ x 1)) (1 2 3)) ; => (2 3 4) + +; Reduce +(reduce + (.. 1 5)) +; equivalente a +((+ (+ (+ 1 2) 3) 4) 5) + +; Nota: map e reduce não possuem atalhos + +; `slice` ou `\` é similar ao .slice() do JavaScript +; mas veja que ele pega uma lista como primeiro argumento, não o último. +(slice (.. 1 5) 2) ; => (3 4 5) +(\ (.. 0 100) -5) ; => (96 97 98 99 100) + +; `append` ou `<<` são auto explicativos +(append 4 (1 2 3)) ; => (1 2 3 4) +(<< "bar" ("foo")) ; => ("foo" "bar") + +; Length é auto explicativo. +(length (1 2 3)) ; => 3 +(_ "foobar") ; => 6 + +;;;;;;;;;;;;;;; +; Delicadezas Haskell + +; Primeiro item de uma lista +(head (1 2 3)) ; => 1 +; Pega do segundo ao último elemento de uma lista +(tail (1 2 3)) ; => (2 3) +; Último item de uma lista +(last (1 2 3)) ; => 3 +; Contrário de `tail` +(init (1 2 3)) ; => (1 2) +; Pega do primeiro até o elemento especificado da lista +(take 1 (1 2 3 4)) ; (1 2) +; Contrário de `take` +(drop 1 (1 2 3 4)) ; (3 4) +; Menos valor em uma lista +(min (1 2 3 4)) ; 1 +; Maior valor em uma lista +(max (1 2 3 4)) ; 4 +; Verifica se o valor está em uma lista ou objeto +(elem 1 (1 2 3)) ; true +(elem "foo" {"foo" "bar"}) ; true +(elem "bar" {"foo" "bar"}) ; false +; Inverte a ordem de uma lista +(reverse (1 2 3 4)) ; => (4 3 2 1) +; Verifica se o valor é par ou ímpar +(even 1) ; => false +(odd 1) ; => true +; Separa um texto cortando por espaço em branco +(words "foobar nachos cheese") ; => ("foobar" "nachos" "cheese") +; Junta lista de textos +(unwords ("foo" "bar")) ; => "foobar" +; Sucessor e predecessor +(pred 21) ; => 20 +(succ 20) ; => 21 +``` + +Para mais informação, verifique o [repositório](http://github.com/L8D/whip) diff --git a/pt-pt/bf.html.markdown b/pt-pt/bf-pt.html.markdown index da4c787f..13c22387 100644 --- a/pt-pt/bf.html.markdown +++ b/pt-pt/bf-pt.html.markdown @@ -1,5 +1,6 @@ --- language: brainfuck +filename: brainfuck-pt.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/pt-pt/swift.html.markdown b/pt-pt/swift-pt.html.markdown index 9462ee1c..6b263942 100644 --- a/pt-pt/swift.html.markdown +++ b/pt-pt/swift-pt.html.markdown @@ -445,49 +445,49 @@ if let circle = myEmptyCircle { // Enums pode opcionalmente ser um tipo especifico ou não. // Enums podem conter métodos tal como as classes. -enum Suit { - case Spades, Hearts, Diamonds, Clubs +enum suit { + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } // Os valores de Enum permitem syntax reduzida, não é preciso escrever o tipo do enum // quando a variável é explicitamente definida. -var suitValue: Suit = .Hearts +var suitValue: Suit = .hearts // Enums que não sejam inteiros obrigam a atribuições valor bruto (raw value) diretas enum BookName: String { - case John = "John" - case Luke = "Luke" + case john = "John" + case luke = "Luke" } -print("Name: \(BookName.John.rawValue)") +print("Name: \(BookName.john.rawValue)") // Enum com valores associados enum Furniture { // Associar com um inteiro (Int) - case Desk(height: Int) + case desk(height: Int) // Associar com uma String e um Int - case Chair(String, Int) + case chair(String, Int) func description() -> String { switch self { - case .Desk(let height): + case .desk(let height): return "Desk with \(height) cm" - case .Chair(let brand, let height): + case .chair(let brand, let height): return "Chair of \(brand) with \(height) cm" } } } -var desk: Furniture = .Desk(height: 80) +var desk: Furniture = .desk(height: 80) print(desk.description()) // "Desk with 80 cm" -var chair = Furniture.Chair("Foo", 40) +var chair = Furniture.chair("Foo", 40) print(chair.description()) // "Chair of Foo with 40 cm" diff --git a/purescript.html.markdown b/purescript.html.markdown index 9ea84d5b..df0cb66e 100644 --- a/purescript.html.markdown +++ b/purescript.html.markdown @@ -1,5 +1,6 @@ --- language: purescript +filename: purescript.purs contributors: - ["Fredrik Dyrkell", "http://www.lexicallyscoped.com"] - ["Thimoteus", "https://github.com/Thimoteus"] diff --git a/python.html.markdown b/python.html.markdown index 0de51285..df1ca6f2 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -7,6 +7,7 @@ contributors: - ["evuez", "http://github.com/evuez"] - ["asyne", "https://github.com/justblah"] - ["habi", "http://github.com/habi"] + - ["Rommel Martinez", "https://ebzzry.io"] filename: learnpython.py --- @@ -302,7 +303,7 @@ filled_dict.values() # => [3, 2, 1] # Note - Same as above regarding key ordering. # Get all key-value pairs as a list of tuples with "items()" -filled_dicts.items() # => [("one", 1), ("two", 2), ("three", 3)] +filled_dict.items() # => [("one", 1), ("two", 2), ("three", 3)] # Check for existence of keys in a dictionary with "in" "one" in filled_dict # => True @@ -363,6 +364,12 @@ filled_set | other_set # => {1, 2, 3, 4, 5, 6} # Check for existence in a set with in 2 in filled_set # => True 10 in filled_set # => False +10 not in filled_set # => True + +# Check data type of variable +type(li) # => list +type(filled_dict) # => dict +type(5) # => int #################################################### @@ -502,9 +509,9 @@ all_the_args(1, 2, a=3, b=4) prints: # Use * to expand positional args and use ** to expand keyword args. args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} -all_the_args(*args) # equivalent to foo(1, 2, 3, 4) -all_the_args(**kwargs) # equivalent to foo(a=3, b=4) -all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) +all_the_args(*args) # equivalent to all_the_args(1, 2, 3, 4) +all_the_args(**kwargs) # equivalent to all_the_args(a=3, b=4) +all_the_args(*args, **kwargs) # equivalent to all_the_args(1, 2, 3, 4, a=3, b=4) # you can pass args and kwargs along to other functions that take args/kwargs diff --git a/python3.html.markdown b/python3.html.markdown index f25d9228..b378a8c6 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -6,6 +6,8 @@ contributors: - ["Andre Polykanine", "https://github.com/Oire"] - ["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 --- @@ -137,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 @@ -209,9 +215,9 @@ li[4] # Raises an IndexError # The start index is included, the end index is not # (It's a closed/open range for you mathy types.) li[1:3] # => [2, 4] -# Omit the end +# Omit the beginning and return the list li[2:] # => [4, 3] -# Omit the beginning +# Omit the end and return the list li[:3] # => [1, 2, 4] # Select every second entry li[::2] # =>[1, 4] @@ -345,7 +351,10 @@ invalid_set = {[1], 1} # => Raises a TypeError: unhashable type: 'list' 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} @@ -545,9 +554,9 @@ all_the_args(1, 2, a=3, b=4) prints: # Use * to expand tuples and use ** to expand kwargs. args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} -all_the_args(*args) # equivalent to foo(1, 2, 3, 4) -all_the_args(**kwargs) # equivalent to foo(a=3, b=4) -all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) +all_the_args(*args) # equivalent to all_the_args(1, 2, 3, 4) +all_the_args(**kwargs) # equivalent to all_the_args(a=3, b=4) +all_the_args(*args, **kwargs) # equivalent to all_the_args(1, 2, 3, 4, a=3, b=4) # Returning multiple values (with tuple assignments) def swap(x, y): @@ -668,7 +677,7 @@ class Human: # An instance method. All methods take "self" as the first argument def say(self, msg): - print ("{name}: {message}".format(name=self.name, message=msg)) + print("{name}: {message}".format(name=self.name, message=msg)) # Another instance method def sing(self): @@ -739,10 +748,105 @@ if __name__ == '__main__': #################################################### -## 6.1 Multiple Inheritance +## 6.1 Inheritance +#################################################### + +# Inheritance allows new child classes to be defined that inherit methods and +# variables from their parent class. + +# Using the Human class defined above as the base or parent class, we can +# define a child class, Superhero, which inherits the class variables like +# "species", "name", and "age", as well as methods, like "sing" and "grunt" +# from the Human class, but can also have its own unique properties. + +# To take advantage of modularization by file you could place the classes above in their own files, +# say, human.py + +# To import functions from other files use the following format +# from "filename-without-extension" import "function-or-class" + +from human import Human + + +# Specify the parent class(es) as parameters to the class definition +class Superhero(Human): + + # If the child class should inherit all of the parent's definitions without + # any modifications, you can just use the "pass" keyword (and nothing else) + # but in this case it is commented out to allow for a unique child class: + # pass + + # Child classes can override their parents' attributes + species = 'Superhuman' + + # Children automatically inherit their parent class's constructor including + # its arguments, but can also define additional arguments or definitions + # and override its methods such as the class constructor. + # This constructor inherits the "name" argument from the "Human" class and + # adds the "superpower" and "movie" arguments: + def __init__(self, name, movie=False, + superpowers=["super strength", "bulletproofing"]): + + # add additional class attributes: + self.fictional = True + self.movie = movie + self.superpowers = superpowers + + # The "super" function lets you access the parent class's methods + # that are overridden by the child, in this case, the __init__ method. + # This calls the parent class constructor: + super().__init__(name) + + # override the sing method + def sing(self): + return 'Dun, dun, DUN!' + + # add an additional instance method + 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") + + # Instance type checks + if isinstance(sup, Human): + print('I am human') + if type(sup) is Superhero: + print('I am a superhero') + + # Get the Method Resolution search Order used by both getattr() and super() + # This attribute is dynamic and can be updated + print(Superhero.__mro__) # => (<class '__main__.Superhero'>, + # => <class 'human.Human'>, <class 'object'>) + + # Calls parent method but uses its own class attribute + print(sup.get_species()) # => Superhuman + + # Calls overridden method + print(sup.sing()) # => Dun, dun, DUN! + + # Calls method from Human + sup.say('Spoon') # => Tick: Spoon + + # Call method that exists only in Superhero + sup.boast() # => I wield the power of super strength! + # => I wield the power of bulletproofing! + + # Inherited class attribute + sup.age = 31 + print(sup.age) # => 31 + + # Attribute that only exists within Superhero + print('Am I Oscar eligible? ' + str(sup.movie)) + +#################################################### +## 6.2 Multiple Inheritance #################################################### # Another class definition +# bat.py class Bat: species = 'Baty' @@ -764,31 +868,25 @@ if __name__ == '__main__': print(b.say('hello')) print(b.fly) -# To take advantage of modularization by file you could place the classes above in their own files, -# say, human.py and bat.py - -# To import functions from other files use the following format -# from "filename-without-extension" import "function-or-class" +# And yet another class definition that inherits from Superhero and Bat # superhero.py -from human import Human +from superhero import Superhero from bat import Bat -# Batman inherits from both Human and Bat -class Batman(Human, Bat): - - # Batman has its own value for the species class attribute - species = 'Superhero' +# Define Batman as a child that inherits from both Superhero and Bat +class Batman(Superhero, Bat): def __init__(self, *args, **kwargs): # Typically to inherit attributes you have to call super: - #super(Batman, self).__init__(*args, **kwargs) + # super(Batman, self).__init__(*args, **kwargs) # However we are dealing with multiple inheritance here, and super() # only works with the next base class in the MRO list. # So instead we explicitly call __init__ for all ancestors. # The use of *args and **kwargs allows for a clean way to pass arguments, # with each parent "peeling a layer of the onion". - Human.__init__(self, 'anonymous', *args, **kwargs) + Superhero.__init__(self, 'anonymous', movie=True, + superpowers=['Wealthy'], *args, **kwargs) Bat.__init__(self, *args, can_fly=False, **kwargs) # override the value for the name attribute self.name = 'Sad Affleck' @@ -800,22 +898,17 @@ class Batman(Human, Bat): if __name__ == '__main__': sup = Batman() - # Instance type checks - if isinstance(sup, Human): - print('I am human') - if isinstance(sup, Bat): - print('I am bat') - if type(sup) is Batman: - print('I am Batman') - # Get the Method Resolution search Order used by both getattr() and super(). # This attribute is dynamic and can be updated - print(Batman.__mro__) # => (<class '__main__.Batman'>, <class 'human.Human'>, <class 'bat.Bat'>, <class 'object'>) + print(Batman.__mro__) # => (<class '__main__.Batman'>, + # => <class 'superhero.Superhero'>, + # => <class 'human.Human'>, + # => <class 'bat.Bat'>, <class 'object'>) # Calls parent method but uses its own class attribute - print(sup.get_species()) # => Superhero + print(sup.get_species()) # => Superhuman - # Calls overloaded method + # Calls overridden method print(sup.sing()) # => nan nan nan nan nan batman! # Calls method from Human, because inheritance order matters @@ -826,10 +919,10 @@ if __name__ == '__main__': # Inherited class attribute sup.age = 100 - print(sup.age) + print(sup.age) # => 100 # Inherited attribute from 2nd ancestor whose default value was overridden. - print('Can I fly? ' + str(sup.fly)) + print('Can I fly? ' + str(sup.fly)) # => Can I fly? False diff --git a/pythonstatcomp.html.markdown b/pythonstatcomp.html.markdown index 8ee3aa64..2440d859 100644 --- a/pythonstatcomp.html.markdown +++ b/pythonstatcomp.html.markdown @@ -13,10 +13,11 @@ This is a tutorial on how to do some typical statistical programming tasks using # 0. Getting set up ==== -""" Get set up with IPython and pip install the following: numpy, scipy, pandas, +""" To get started, pip install the following: jupyter, numpy, scipy, pandas, matplotlib, seaborn, requests. - Make sure to do this tutorial in the IPython notebook so that you get - the inline plots and easy documentation lookup. + Make sure to do this tutorial in a Jupyter notebook so that you get + the inline plots and easy documentation lookup. The shell command to open + one is simply `jupyter notebook`, then click New -> Python. """ # 1. Data acquisition ==== @@ -37,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/ @@ -70,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 @@ -104,7 +103,7 @@ import matplotlib as mpl import matplotlib.pyplot as plt %matplotlib inline -# To do data vizualization in Python, use matplotlib +# To do data visualization in Python, use matplotlib plt.hist(pets.age); @@ -204,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 @@ -221,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/qt.html.markdown b/qt.html.markdown index a9c855a6..0db75726 100644 --- a/qt.html.markdown +++ b/qt.html.markdown @@ -5,7 +5,7 @@ language: c++ filename: learnqt.cpp contributors: - ["Aleksey Kholovchuk", "https://github.com/vortexxx192"] -lang: en + --- **Qt** is a widely-known framework for developing cross-platform software that can be run on various software and hardware platforms with little or no change in the code, while having the power and speed of native applications. Though **Qt** was originally written in *C++*, there are its ports to other languages: *[PyQt](https://learnxinyminutes.com/docs/pyqt/)*, *QtRuby*, *PHP-Qt*, etc. @@ -14,7 +14,7 @@ lang: en ```c++ /* - * Let's start clasically + * Let's start classically */ // all headers from Qt framework start with capital letter 'Q' @@ -79,7 +79,7 @@ int main(int argc, char *argv[]) { Notice that *QObject::connect* part. This method is used to connect *SIGNALS* of one objects to *SLOTS* of another. -**Signals** are being emited when certain things happen with objects, like *pressed* signal is emited when user presses on QPushButton object. +**Signals** are being emitted when certain things happen with objects, like *pressed* signal is emitted when user presses on QPushButton object. **Slots** are *actions* that might be performed in response to received signals. diff --git a/r.html.markdown b/r.html.markdown index 8539b10e..e7486e60 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -192,7 +192,7 @@ class(-Inf) # "numeric" 2.0 * 2L # 4 # numeric times integer gives numeric 3L / 4 # 0.75 # integer over numeric gives numeric 3 %% 2 # 1 # the remainder of two numerics is another numeric -# Illegal arithmetic yeilds you a "not-a-number": +# Illegal arithmetic yields you a "not-a-number": 0 / 0 # NaN class(NaN) # "numeric" # You can do arithmetic on two vectors with length greater than 1, @@ -662,8 +662,9 @@ require(plyr) ######################### # "pets.csv" is a file on the internet -# (but it could just as easily be be a file on your own computer) -pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv") +# (but it could just as easily be a file on your own computer) +require(RCurl) +pets <- read.csv(textConnection(getURL("http://learnxinyminutes.com/docs/pets.csv"))) pets head(pets, 2) # first two rows tail(pets, 1) # last row diff --git a/red.html.markdown b/red.html.markdown index 0c979ce1..3575032f 100644 --- a/red.html.markdown +++ b/red.html.markdown @@ -51,7 +51,7 @@ comment { ; no need to restrict this to a 'main' function. ; Valid variable names start with a letter and can contain numbers, -; variables containing only capital A thru F and numbers and ending with 'h' +; variables containing only capital A through F and numbers and ending with 'h' ; are forbidden, because that is how hexadecimal numbers are expressed in Red ; and Red/System. @@ -136,14 +136,14 @@ if a < 0 [print "a is negative"] ; Evaluate a block of code if a given condition is true, else evaluate an ; alternative block of code. If the last expressions in both blocks have the ; same type, EITHER can be used inside an expression. -either a < 0 [ +either a > 0 [ + msg: "positive" +][ either a = 0 [ msg: "zero" ][ msg: "negative" ] -][ - msg: "positive" ] print ["a is " msg lf] @@ -151,14 +151,14 @@ print ["a is " msg lf] ; There is an alternative way to write this ; (Which is allowed because all code paths return a value of the same type): -msg: either a < 0 [ +msg: either a > 0 [ + "positive" +][ either a = 0 [ "zero" ][ "negative" ] - ][ - "positive" ] print ["a is " msg lf] 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 1fbff015..65f848ed 100644 --- a/rst.html.markdown +++ b/rst.html.markdown @@ -32,7 +32,7 @@ $ pip install docutils A simple example of the file syntax: -```rst +``` .. Lines starting with two dots are special commands. But if no command can be found, the line is considered as a comment ========================================================= diff --git a/ru-ru/.directory b/ru-ru/.directory deleted file mode 100644 index 4d20336b..00000000 --- a/ru-ru/.directory +++ /dev/null @@ -1,4 +0,0 @@ -[Dolphin] -SortRole=size -Timestamp=2015,10,31,18,6,13 -Version=3 diff --git a/ru-ru/bf.html.markdown b/ru-ru/bf.html.markdown index 20f0fa56..d2e74e8f 100644 --- a/ru-ru/bf.html.markdown +++ b/ru-ru/bf.html.markdown @@ -1,5 +1,6 @@ --- language: bf +filename: learnbf-ru.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/ru-ru/c++-ru.html.markdown b/ru-ru/c++-ru.html.markdown index cef5ab7e..b9704fc3 100644 --- a/ru-ru/c++-ru.html.markdown +++ b/ru-ru/c++-ru.html.markdown @@ -853,7 +853,7 @@ pt2 = nullptr; // Устанавливает pt2 в null. // '=' != '=' != '='! // Вызывает Foo::Foo(const Foo&) или некий вариант (смотрите "move semantics") -// копирования конструктора. +// конструктора копирования. Foo f2; Foo f1 = f2; diff --git a/ru-ru/c-ru.html.markdown b/ru-ru/c-ru.html.markdown index 71e41ee3..44e7ad3b 100644 --- a/ru-ru/c-ru.html.markdown +++ b/ru-ru/c-ru.html.markdown @@ -477,7 +477,7 @@ void str_reverse_through_pointer(char *str_in) { Очень важно использовать правильные отступы и ставить пробелы в нужных местах. Читаемый код лучше чем красивый или быстрый код. -Чтобы научиться писать хороший код, почитайте [Linux kernel coding stlye](https://www.kernel.org/doc/Documentation/CodingStyle). +Чтобы научиться писать хороший код, почитайте [Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle). Также не забывайте, что [Google](http://google.com) и [Яндекс](http://yandex.ru) – ваши хорошие друзья. diff --git a/ru-ru/elixir-ru.html.markdown b/ru-ru/elixir-ru.html.markdown new file mode 100644 index 00000000..c8c2c060 --- /dev/null +++ b/ru-ru/elixir-ru.html.markdown @@ -0,0 +1,467 @@ +--- +language: elixir +contributors: + - ["Joao Marques", "http://github.com/mrshankly"] + - ["Dzianis Dashkevich", "https://github.com/dskecse"] + - ["Ryan Plant", "https://github.com/ryanplant-au"] +translator: + - ["Ev Bogdanov", "https://github.com/evbogdanov"] +filename: learnelixir-ru.ex +lang: ru-ru +--- + +Elixir — современный функциональный язык программирования, который работает на +виртуальной машине Erlang. Elixir полностью совместим с Erlang, но обладает +дружелюбным синтаксисом и предлагает больше возможностей. + +```elixir + +# Однострочные комментарии начинаются с символа решётки. + +# Для многострочных комментариев отдельного синтаксиса нет, +# поэтому просто используйте несколько однострочных комментариев. + +# Запустить интерактивную Elixir-консоль (аналог `irb` в Ruby) можно +# при помощи команды `iex`. +# Чтобы скомпилировать модуль, воспользуйтесь командой `elixirc`. + +# Обе команды будут работать из терминала, если вы правильно установили Elixir. + +## --------------------------- +## -- Базовые типы +## --------------------------- + +# Числа +3 # целое число +0x1F # целое число +3.0 # число с плавающей запятой + +# Атомы, которые являются нечисловыми константами. Они начинаются с символа `:`. +:hello # атом + +# Кортежи, которые хранятся в памяти последовательно. +{1,2,3} # кортеж + +# Получить доступ к элементу кортежа мы можем с помощью функции `elem`: +elem({1, 2, 3}, 0) #=> 1 + +# Списки, которые реализованы как связные списки. +[1,2,3] # список + +# У каждого непустого списка есть голова (первый элемент списка) +# и хвост (все остальные элементы списка): +[head | tail] = [1,2,3] +head #=> 1 +tail #=> [2,3] + +# В Elixir, как и в Erlang, знак `=` служит для сопоставления с образцом, +# а не для операции присваивания. +# +# Это означает, что выражение слева от знака `=` (образец) сопоставляется с +# выражением справа. +# +# Сопоставление с образцом позволило нам получить голову и хвост списка +# в примере выше. + +# Если выражения слева и справа от знака `=` не удаётся сопоставить, будет +# брошена ошибка. Например, если кортежи разных размеров. +{a, b, c} = {1, 2} #=> ** (MatchError) + +# Бинарные данные +<<1,2,3>> + +# Вы столкнётесь с двумя видами строк: +"hello" # Elixir-строка (заключена в двойные кавычки) +'hello' # Erlang-строка (заключена в одинарные кавычки) + +# Все строки представлены в кодировке UTF-8: +"привет" #=> "привет" + +# Многострочный текст +""" +Я текст на несколько +строк. +""" +#=> "Я текст на несколько\nстрок.\n" + +# Чем Elixir-строки отличаются от Erlang-строк? Elixir-строки являются бинарными +# данными. +<<?a, ?b, ?c>> #=> "abc" +# Erlang-строка — это на самом деле список. +[?a, ?b, ?c] #=> 'abc' + +# Оператор `?` возвращает целое число, соответствующее данному символу. +?a #=> 97 + +# Для объединения бинарных данных (и Elixir-строк) используйте `<>` +<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>> +"hello " <> "world" #=> "hello world" + +# Для объединения списков (и Erlang-строк) используйте `++` +[1,2,3] ++ [4,5] #=> [1,2,3,4,5] +'hello ' ++ 'world' #=> 'hello world' + +# Диапазоны записываются как `начало..конец` (оба включительно) +1..10 #=> 1..10 + +# Сопоставление с образцом применимо и для диапазонов: +lower..upper = 1..10 +[lower, upper] #=> [1, 10] + +# Карты (известны вам по другим языкам как ассоциативные массивы, словари, хэши) +genders = %{"david" => "male", "gillian" => "female"} +genders["david"] #=> "male" + +# Для карт, где ключами выступают атомы, доступен специальный синтаксис +genders = %{david: "male", gillian: "female"} +genders.gillian #=> "female" + +## --------------------------- +## -- Операторы +## --------------------------- + +# Математические операции +1 + 1 #=> 2 +10 - 5 #=> 5 +5 * 2 #=> 10 +10 / 2 #=> 5.0 + +# В Elixir оператор `/` всегда возвращает число с плавающей запятой. + +# Для целочисленного деления применяйте `div` +div(10, 2) #=> 5 + +# Для получения остатка от деления к вашим услугам `rem` +rem(10, 3) #=> 1 + +# Булевые операторы: `or`, `and`, `not`. +# В качестве первого аргумента эти операторы ожидают булевое значение. +true and true #=> true +false or true #=> true +1 and true #=> ** (BadBooleanError) + +# Elixir также предоставляет `||`, `&&` и `!`, которые принимают аргументы +# любого типа. Всё, кроме `false` и `nil`, считается `true`. +1 || true #=> 1 +false && 1 #=> false +nil && 20 #=> nil +!true #=> false + +# Операторы сравнения: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<`, `>` +1 == 1 #=> true +1 != 1 #=> false +1 < 2 #=> true + +# Операторы `===` и `!==` более строгие. Разница заметна, когда мы сравниваем +# числа целые и с плавающей запятой: +1 == 1.0 #=> true +1 === 1.0 #=> false + +# Elixir позволяет сравнивать значения разных типов: +1 < :hello #=> true + +# При сравнении разных типов руководствуйтесь следующим правилом: +# число < атом < ссылка < функция < порт < процесс < кортеж < список < строка + +## --------------------------- +## -- Порядок выполнения +## --------------------------- + +# Условный оператор `if` +if false do + "Вы этого никогда не увидите" +else + "Вы увидите это" +end + +# Противоположный ему условный оператор `unless` +unless true do + "Вы этого никогда не увидите" +else + "Вы увидите это" +end + +# Помните сопоставление с образцом? +# Многие конструкции в Elixir построены вокруг него. + +# `case` позволяет сравнить выражение с несколькими образцами: +case {:one, :two} do + {:four, :five} -> + "Этот образец не совпадёт" + {:one, x} -> + "Этот образец совпадёт и присвоит переменной `x` значение `:two`" + _ -> + "Этот образец совпадёт с чем угодно" +end + +# Символ `_` называется анонимной переменной. Используйте `_` для значений, +# которые в текущем выражении вас не интересуют. Например, вам интересна лишь +# голова списка, а хвост вы желаете проигнорировать: +[head | _] = [1,2,3] +head #=> 1 + +# Для лучшей читаемости вы можете написать: +[head | _tail] = [:a, :b, :c] +head #=> :a + +# `cond` позволяет проверить сразу несколько условий за раз. +# Используйте `cond` вместо множественных операторов `if`. +cond do + 1 + 1 == 3 -> + "Вы меня никогда не увидите" + 2 * 5 == 12 -> + "И меня" + 1 + 2 == 3 -> + "Вы увидите меня" +end + +# Обычно последним условием идёт `true`, которое выполнится, если все предыдущие +# условия оказались ложны. +cond do + 1 + 1 == 3 -> + "Вы меня никогда не увидите" + 2 * 5 == 12 -> + "И меня" + true -> + "Вы увидите меня (по сути, это `else`)" +end + +# Обработка ошибок происходит в блоках `try/catch`. +# Elixir также поддерживает блок `after`, который выполнится в любом случае. +try do + throw(:hello) +catch + message -> "Поймана ошибка с сообщением #{message}." +after + IO.puts("Я выполнюсь всегда") +end +#=> Я выполнюсь всегда +# "Поймана ошибка с сообщением hello." + +## --------------------------- +## -- Модули и функции +## --------------------------- + +# Анонимные функции (обратите внимание на точку при вызове функции) +square = fn(x) -> x * x end +square.(5) #=> 25 + +# Анонимные функции принимают клозы и гарды. +# +# Клозы (от англ. clause) — варианты исполнения функции. +# +# Гарды (от англ. guard) — охранные выражения, уточняющие сопоставление с +# образцом в функциях. Гарды следуют после ключевого слова `when`. +f = fn + x, y when x > 0 -> x + y + x, y -> x * y +end + +f.(1, 3) #=> 4 +f.(-1, 3) #=> -3 + +# В Elixir много встроенных функций. +# Они доступны в текущей области видимости. +is_number(10) #=> true +is_list("hello") #=> false +elem({1,2,3}, 0) #=> 1 + +# Вы можете объединить несколько функций в модуль. Внутри модуля используйте `def`, +# чтобы определить свои функции. +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 + +# Чтобы скомпилировать модуль Math, сохраните его в файле `math.ex` +# и наберите в терминале: `elixirc math.ex` + +defmodule PrivateMath do + # Публичные функции начинаются с `def` и доступны из других модулей. + def sum(a, b) do + do_sum(a, b) + end + + # Приватные функции начинаются с `defp` и доступны только внутри своего модуля. + defp do_sum(a, b) do + a + b + end +end + +PrivateMath.sum(1, 2) #=> 3 +PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError) + +# Функции внутри модуля тоже принимают клозы и гарды +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) + +# Из-за неизменяемых переменных в Elixir важную роль играет рекурсия +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 + +# Модули в Elixir поддерживают атрибуты. +# Атрибуты бывают как встроенные, так и ваши собственные. +defmodule MyMod do + @moduledoc """ + Это встроенный атрибут + """ + + @my_data 100 # А это ваш атрибут + IO.inspect(@my_data) #=> 100 +end + +# Одна из фишек языка — оператор `|>` +# Он передаёт выражение слева в качестве первого аргумента функции справа: +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] + +## --------------------------- +## -- Структуры и исключения +## --------------------------- + +# Структуры — это расширения поверх карт, привносящие в Elixir значения по +# умолчанию, проверки на этапе компиляции и полиморфизм. +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"} + +# Доступ к полю структуры +joe_info.name #=> "Joe" + +# Обновление поля структуры +older_joe_info = %{ joe_info | age: 31 } +#=> %Person{age: 31, height: 180, name: "Joe"} + +# Блок `try` с ключевым словом `rescue` используется для обработки исключений +try do + raise "какая-то ошибка" +rescue + RuntimeError -> "перехвачена ошибка рантайма" + _error -> "перехват любой другой ошибки" +end +#=> "перехвачена ошибка рантайма" + +# У каждого исключения есть сообщение +try do + raise "какая-то ошибка" +rescue + x in [RuntimeError] -> + x.message +end +#=> "какая-то ошибка" + +## --------------------------- +## -- Параллелизм +## --------------------------- + +# Параллелизм в Elixir построен на модели акторов. Для написания +# параллельной программы нам понадобятся три вещи: +# 1. Создание процессов +# 2. Отправка сообщений +# 3. Приём сообщений + +# Новый процесс создаётся функцией `spawn`, которая принимает функцию +# в качестве аргумента. +f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245> +spawn(f) #=> #PID<0.40.0> + +# `spawn` возвращает идентификатор процесса (англ. process identifier, PID). +# Вы можете использовать PID для отправки сообщений этому процессу. Сообщения +# отправляются через оператор `send`. А для приёма сообщений используется +# механизм `receive`: + +# Блок `receive do` ждёт сообщений и обработает их, как только получит. Блок +# `receive do` обработает лишь одно полученное сообщение. Чтобы обработать +# несколько сообщений, функция, содержащая блок `receive do`, должна рекурсивно +# вызывать себя. + +defmodule Geometry do + def area_loop do + receive do + {:rectangle, w, h} -> + IO.puts("Площадь = #{w * h}") + area_loop() + {:circle, r} -> + IO.puts("Площадь = #{3.14 * r * r}") + area_loop() + end + end +end + +# Скомпилируйте модуль и создайте процесс +pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0> +# Альтернативно +pid = spawn(Geometry, :area_loop, []) + +# Отправьте сообщение процессу +send pid, {:rectangle, 2, 3} +#=> Площадь = 6 +# {:rectangle,2,3} + +send pid, {:circle, 2} +#=> Площадь = 12.56 +# {:circle,2} + +# Кстати, интерактивная консоль — это тоже процесс. +# Чтобы узнать текущий PID, воспользуйтесь встроенной функцией `self` +self() #=> #PID<0.27.0> + +## --------------------------- +## -- Агенты +## --------------------------- + +# Агент — это процесс, который следит за некоторым изменяющимся значением. + +# Создайте агента через `Agent.start_link`, передав ему функцию. +# Начальным состоянием агента будет значение, которое эта функция возвращает. +{ok, my_agent} = Agent.start_link(fn -> ["красный", "зелёный"] end) + +# `Agent.get` принимает имя агента и анонимную функцию `fn`, которой будет +# передано текущее состояние агента. В результате вы получите то, что вернёт +# анонимная функция. +Agent.get(my_agent, fn colors -> colors end) #=> ["красный", "зелёный"] + +# Похожим образом вы можете обновить состояние агента +Agent.update(my_agent, fn colors -> ["синий" | colors] end) +``` + +## Ссылки + +* [Официальный сайт](http://elixir-lang.org) +* [Шпаргалка по языку](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf) +* [Книга "Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) +* [Книга "Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) +* [Книга "Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) diff --git a/ru-ru/haml-ru.html.markdown b/ru-ru/haml-ru.html.markdown new file mode 100644 index 00000000..c2f8852e --- /dev/null +++ b/ru-ru/haml-ru.html.markdown @@ -0,0 +1,233 @@ +--- +language: haml +filename: learnhaml-ru.haml +contributors: + - ["Simon Neveu", "https://github.com/sneveu"] + - ["Vasiliy Petrov", "https://github.com/Saugardas"] +translators: + - ["Vasiliy Petrov", "https://github.com/Saugardas"] +lang: ru-ru +--- + +Haml - язык разметки (в основном используемый с Ruby), с помощью которого могут быть легко описаны HTML-документы. +Он является популярной альтернативой используемому в Rails шаблонизатору (.erb), и позволяет вставлять Ruby-код в вашу разметку. + +Haml убирает избыточность закрывающих тегов благодаря отступам. +В результате получается меньшая по размерам, хорошо структурированная, логичная и читаемая разметка. + +Вы можете использовать Haml и вне Ruby-проекта. Установите гем Haml и используйте командную строку для конвертирования html-файлов: + +```shell +$ haml input_file.haml output_file.html +``` + + +```haml +/ ------------------------------------------- +/ Отступы +/ ------------------------------------------- + +/ + Отступы являются важным элементом синтаксиса, поэтому они должны быть + одинаковыми во всём документе. Обычно используют два пробела, + но это не является обязательным правилом - можно использовать любое + количество пробелов для отступов. Главное, чтобы это количество было + одинаковым во всём документе. + + +/ ------------------------------------------- +/ Комментарии +/ ------------------------------------------- + +/ Комментари начинается с символа косой черты. + +/ + Для написания многострочного комментария расположите ваш комментарий + на следующем уровне вложенности от символа косой черты + +-# "Скрытый" комментарий. Этот комментарий не попадёт в результирующий документ + + +/ ------------------------------------------- +/ Элементы HTML +/ ------------------------------------------- + +/ Чтобы написать тег, используйте символ процента (%) и название тега +%body + %header + %nav + +/ Обратите внимание на отсутствие закрывающих тегов. Код выше выведет: + <body> + <header> + <nav></nav> + </header> + </body> + +/ + Так как тег div используется очень часто, его можно опустить. + Можно указать только имя класса или идентификатора (. или #) + Например код: + +%div.my_class + %div#my_id + +/ Можно записать: +.my_class + #my_id + +/ Для добавления контента в тег, просто добавьте текст после объявления тега +%h1 Заголовок + +/ Для многострочного содержания используйте отступы +%p + Многострочное содержание + в две строки. + +/ + Амперсанд - равно (&=) обрабатывают Ruby код также, как и без амперсанда, + но HTML-символы в результате будут экранированы. Например: + +%p + &= "Да & да" + +/ выведет 'Да & да' + +/ + Чтобы выполнять Ruby-код без экранрования, можно использовать + "восклицательный знак" и "равно" (!=) + +%p + != "Тег абзаца <p></p>" + +/ выведет 'Тег абзаца <p></p>' + +/ CSS - классы могут быть добавлены через точку от определения тега +%div.foo.bar + +/ Или с помощью хеша атрибутов +%div{ :class => 'foo bar' } + +/ Хеш атрибутов может быть добавлен для любого тега +%a{ :href => '#', :class => 'bar', :title => 'Bar' } + +/ Для булевых атрибутов просто присвойте значение 'true' +%input{ :selected => true } + +/ Для data - атрибутов присвойте ключу :data хеш с данными +%div{ :data => { :attribute => 'foo' } } + +/ Для Ruby версии 1.9 или выше, можно использовать новый синтаксис хешей +%div{ data: { attribute: 'foo' } } + +/ Также можно использовать HTML-синтаксис атрибутов +%a(href='#' title='bar') + +/ Можно использовать оба варианта одновременно +%a(href='#'){ title: @my_class.title } + + +/ ------------------------------------------- +/ Включение Ruby +/ ------------------------------------------- + +/ Для включения Ruby кода используйте знак "равно" + +%h1= book.name + +%p + = book.author + = book.publisher + + +/ Для выполнения Ruby кода без вывода в HTML, используйте знак дефиса +- books = ['book 1', 'book 2', 'book 3'] + +/ + Можно выполнять любой Ruby код, например с блоками. + Закрывающий "end" не нужен, так как они будут закрыты автоматически, + основываясь на вложенности. + +- books.shuffle.each_with_index do |book, index| + %h1= book + + - if book do + %p This is a book + +/ Добавление списка +%ul + %li + =item1 + =item2 + +/ ------------------------------------------- +/ Пример таблицы с классами Bootstrap'a +/ ------------------------------------------- + +%table.table.table-hover + %thead + %tr + %th Header 1 + %th Header 2 + + %tr + %td Value1 + %td value2 + + %tfoot + %tr + %td + Foot value + + +/ ------------------------------------------- +/ Интерполяция Ruby кода +/ ------------------------------------------- + +/ Ruby код может быть интерполирован в текст с помощью #{} +%p Ваша самая любимая игра - #{best_game} + +/ Тоже самое, что и: +%p= "Ваша самая любимая игра - #{best_game}" + + +/ ------------------------------------------- +/ Фильтры +/ ------------------------------------------- + +/ + Фильтры передают связанный блок текста в соотвествующую + фильтрующую программу и возвращают результат в Haml + Фильтр обозначается двоеточием и названием фильтра: + +/ Markdown filter +:markdown + # Заголовк + + Текст **внутри** *блока* + +/ Код выше будет скомпилирован в +<h1>Заголовок</h1> + +<p>Текст <strong>внутри</strong> <em>блока</em></p> + +/ Javascript - фильтр +:javascript + console.log('This is inline <script>'); + +/ скомпилируется в: +<script> + console.log('This is inline <script>'); +</script> + +/ + Существует множество типов фильров (:markdown, :javascript, :coffee, + :css, :ruby и так далее). Вы можете определить собственный фильтр c + помощью Haml::Filters. + +``` + +## Дополнительные ресурсы + +- [О Haml](https://haml.ru) - Хорошее введение, описывает преимущества Haml. +- [Документация](https://haml.ru/documentation/) - Документация Haml на русском языке. diff --git a/ru-ru/haskell-ru.html.markdown b/ru-ru/haskell-ru.html.markdown index fecee51b..b1b8eb79 100644 --- a/ru-ru/haskell-ru.html.markdown +++ b/ru-ru/haskell-ru.html.markdown @@ -1,5 +1,6 @@ --- language: Haskell +filename: haskell-ru.hs contributors: - ["Adit Bhargava", "http://adit.io"] translators: diff --git a/ru-ru/html-ru.html.markdown b/ru-ru/html-ru.html.markdown index 5069d77f..120981b9 100644 --- a/ru-ru/html-ru.html.markdown +++ b/ru-ru/html-ru.html.markdown @@ -1,6 +1,6 @@ --- language: html -filename: learnhtml.html +filename: learnhtml-ru.html contributors: - ["Christophe THOMAS", "https://github.com/WinChris"] translators: diff --git a/ru-ru/java-ru.html.markdown b/ru-ru/java-ru.html.markdown index a1a5cdfc..1aff801c 100644 --- a/ru-ru/java-ru.html.markdown +++ b/ru-ru/java-ru.html.markdown @@ -1,12 +1,13 @@ --- language: java +filename: LearnJava-ru.java contributors: - ["Jake Prather", "http://github.com/JakeHP"] - ["Madison Dickson", "http://github.com/mix3d"] translators: - ["Sergey Gaykov", "https://github.com/gaykov"] -filename: LearnJavaRu.java lang: ru-ru + --- Java - это объектно-ориентированный язык программирования общего назначения, diff --git a/ru-ru/kotlin-ru.html.markdown b/ru-ru/kotlin-ru.html.markdown index 21940e41..58dab4cd 100644 --- a/ru-ru/kotlin-ru.html.markdown +++ b/ru-ru/kotlin-ru.html.markdown @@ -8,7 +8,7 @@ translators: - ["Vadim Toptunov", "https://github.com/VadimToptunov"] --- -Kotlin - статистически типизированный язык для JVM, Android и браузера. Язык полностью cjdvtcnbv c Java. +Kotlin - статистически типизированный язык для JVM, Android и браузера. Язык полностью совместим c Java. [Более детальная информация здесь.](https://kotlinlang.org/) ```kotlin 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/nim-ru.html.markdown b/ru-ru/nim-ru.html.markdown new file mode 100644 index 00000000..d05583d7 --- /dev/null +++ b/ru-ru/nim-ru.html.markdown @@ -0,0 +1,279 @@ +--- +language: Nim +filename: learnNim-ru.nim +contributors: + - ["Jason J. Ayala P.", "http://JasonAyala.com"] + - ["Dennis Felsing", "http://felsin9.de/nnis/"] +translators: + - ["Nomadic", "https://github.com/n0madic"] +lang: ru-ru +--- + +Nim (ранее известный, как Nimrod) — язык программирования со статической +типизацией, поддерживающий процедурный, объектно-ориентированный, +функциональный и обобщённый стили программирования. + +Nim эффективный, выразительный и элегантный. + +```nim +var # Объявление (и присваивание) переменных, + letter: char = 'n' # с указанием типа или без + lang = "N" & "im" + nLength : int = len(lang) + boat: float + truth: bool = false + +let # Используйте let *сразу* для объявления и связывания переменных. + legs = 400 # legs неизменяемый. + arms = 2_000 # Символ _ игнорируется и удобен для длинных чисел. + aboutPi = 3.15 + +const # Константы вычисляются во время компиляции. Это обеспечивает + debug = true # производительность и полезно в выражениях этапа компиляции. + compileBadCode = false + +when compileBadCode: # `when` это `if` этапа компиляции. + legs = legs + 1 # Эта ошибка никогда не будет скомпилирована. + const input = readline(stdin) # Значения констант должны быть известны во + # время компиляции. + +discard 1 > 2 # Примечание. Компилятор будет жаловаться, если результат + # выражения не используется. `discard` обходит это. + +discard """ +Это может использоваться как многострочный комментарий. +Или для не поддающегося синтаксическому анализу, сломанного кода +""" + +# +# Структуры данных +# + +# Кортежи + +var + child: tuple[name: string, age: int] # Кортежи определяют *как* имя поля + today: tuple[sun: string, temp: float] # так *и* порядок полей. + +child = (name: "Rudiger", age: 2) # Присвоить все сразу литералом () +today.sun = "Overcast" # или отдельно по полям. +today.temp = 70.1 + +# Последовательности + +var + drinks: seq[string] + +drinks = @["Water", "Juice", "Chocolate"] # @[V1,..,Vn] является литералом + # последовательности + +drinks.add("Milk") + +if "Milk" in drinks: + echo "We have Milk and ", drinks.len - 1, " other drinks" + +let myDrink = drinks[2] + +# +# Определение типов +# + +# Определение собственных типов позволяет компилятору работать на вас. +# Это то, что делает статическую типизацию мощной и полезной. + +type + Name = string # Псевдоним типа дает вам новый тип, который равнозначен + Age = int # старому типу, но более нагляден. + Person = tuple[name: Name, age: Age] # Определение структур данных. + AnotherSyntax = tuple + fieldOne: string + secondField: int + +var + john: Person = (name: "John B.", age: 17) + newage: int = 18 # Было бы лучше использовать Age, чем int + +john.age = newage # Но это все же работает, потому что int и Age синонимы. + +type + Cash = distinct int # `distinct` делает новый тип несовместимым с его + Desc = distinct string # базовым типом. + +var + money: Cash = 100.Cash # `.Cash` преобразует int в наш тип + description: Desc = "Interesting".Desc + +when compileBadCode: + john.age = money # Error! age is of type int and money is Cash + john.name = description # Компилятор говорит: "Нельзя!" + +# +# Дополнительные типы и структуры данных +# + +# Перечисления позволяют типу иметь одно из ограниченного числа значений + +type + Color = enum cRed, cBlue, cGreen + Direction = enum # Альтернативный формат + dNorth + dWest + dEast + dSouth +var + orient = dNorth # `orient` имеет тип Direction, со значением `dNorth` + pixel = cGreen # `pixel` имеет тип Color, со значением `cGreen` + +discard dNorth > dEast # Перечисления обычно являются "порядковыми" типами + +# Поддиапазоны определяют ограниченный допустимый диапазон + +type + DieFaces = range[1..20] # Допустимым значением являются только int от 1 до 20 +var + my_roll: DieFaces = 13 + +when compileBadCode: + my_roll = 23 # Error! + +# Arrays + +type + RollCounter = array[DieFaces, int] # Массивы фиксированной длины и + DirNames = array[Direction, string] # индексируются любым порядковым типом. + Truths = array[42..44, bool] +var + counter: RollCounter + directions: DirNames + possible: Truths + +possible = [false, false, false] # Массивы создаются литералом [V1,..,Vn] +possible[42] = true + +directions[dNorth] = "Ahh. The Great White North!" +directions[dWest] = "No, don't go there." + +my_roll = 13 +counter[my_roll] += 1 +counter[my_roll] += 1 + +var anotherArray = ["Default index", "starts at", "0"] + +# Доступны другие структуры данных, в том числе таблицы, множества, +# списки, очереди и crit-bit деревья. +# http://nim-lang.org/docs/lib.html#collections-and-algorithms (EN) + +# +# IO и поток управления выполнением +# + +# `case`, `readLine()` + +echo "Read any good books lately?" +case readLine(stdin) +of "no", "No": + echo "Go to your local library." +of "yes", "Yes": + echo "Carry on, then." +else: + echo "That's great; I assume." + +# `while`, `if`, `continue`, `break` + +import strutils as str # http://nim-lang.org/docs/strutils.html (EN) +echo "I'm thinking of a number between 41 and 43. Guess which!" +let number: int = 42 +var + raw_guess: string + guess: int +while guess != number: + raw_guess = readLine(stdin) + if raw_guess == "": continue # Пропустить эту итерацию + guess = str.parseInt(raw_guess) + if guess == 1001: + echo("AAAAAAGGG!") + break + elif guess > number: + echo("Nope. Too high.") + elif guess < number: + echo(guess, " is too low") + else: + echo("Yeeeeeehaw!") + +# +# Итерации (циклы) +# + +for i, elem in ["Yes", "No", "Maybe so"]: # Или просто `for elem in` + echo(elem, " is at index: ", i) + +for k, v in items(@[(person: "You", power: 100), (person: "Me", power: 9000)]): + echo v + +let myString = """ +an <example> +`string` to +play with +""" # Многострочная "сырая" строка + +for line in splitLines(myString): + echo(line) + +for i, c in myString: # Индекс и символ. Или `for j in` только для символов + if i mod 2 == 0: continue # Компактная форма `if` + elif c == 'X': break + else: echo(c) + +# +# Процедуры +# + +type Answer = enum aYes, aNo + +proc ask(question: string): Answer = + echo(question, " (y/n)") + while true: + case readLine(stdin) + of "y", "Y", "yes", "Yes": + return Answer.aYes # Перечисления могут быть квалифицированы + of "n", "N", "no", "No": + return Answer.aNo + else: echo("Please be clear: yes or no") + +proc addSugar(amount: int = 2) = # Значение поумолчанию 2, ничего не возвращает + assert(amount > 0 and amount < 9000, "Crazy Sugar") + for a in 1..amount: + echo(a, " sugar...") + +case ask("Would you like sugar in your tea?") +of aYes: + addSugar(3) +of aNo: + echo "Oh do take a little!" + addSugar() +# Здесь нет необходимости в `else`. Возможны только `yes` и `no`. + +# +# FFI (интерфейс внешних функций) +# + +# Так как Nim компилируется в C, то FFI делается очень просто: + +proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.} + +let cmp = strcmp("C?", "Easy!") +``` + +Кроме того, Nim выделяется среди себе подобных метапрограммированием, +производительностью, функциями этапа компиляции. + +## Дальнейшее чтение (EN) + +* [Домашняя страница](http://nim-lang.org) +* [Скачать](http://nim-lang.org/download.html) +* [Сообщество](http://nim-lang.org/community.html) +* [FAQ](http://nim-lang.org/question.html) +* [Документация](http://nim-lang.org/documentation.html) +* [Руководство](http://nim-lang.org/docs/manual.html) +* [Стандартная библиотека](http://nim-lang.org/docs/lib.html) +* [Rosetta Code](http://rosettacode.org/wiki/Category:Nim) diff --git a/ru-ru/objective-c-ru.html.markdown b/ru-ru/objective-c-ru.html.markdown index d60db1d8..3baa15f8 100644 --- a/ru-ru/objective-c-ru.html.markdown +++ b/ru-ru/objective-c-ru.html.markdown @@ -781,7 +781,7 @@ MyClass *newVar = [classVar retain]; // Если classVar освободится // автоматический подсчет ссылок (ARC). // ARC - это особенность компилятора, который помещает "retain", "release" // и "autorelease" автоматически за вас тогда, когда используется ARC, -// вам не нужно больше обращаться к "retain", "relase" или "autorelease" +// вам не нужно больше обращаться к "retain", "release" или "autorelease" MyClass *arcMyClass = [[MyClass alloc] init]; // ... код, использующий объект arcMyClass // Без ARC, вам нужно было бы вызвать: [arcMyClass release] после того, как вы diff --git a/ru-ru/php-ru.html.markdown b/ru-ru/php-ru.html.markdown index 181368de..af77a9ca 100644 --- a/ru-ru/php-ru.html.markdown +++ b/ru-ru/php-ru.html.markdown @@ -61,6 +61,8 @@ $int4 = 0x0F; // => 15 (ведущие символы 0x означают шес // Двоичная запись integer доступна начиная с PHP 5.4.0. $int5 = 0b11111111; // 255 (0b в начале означает двоичное число) +// Удаление переменной +unset($int1); // Дробные числа $float = 1.234; @@ -128,7 +130,7 @@ define("FOO", "something"); // Доступ к константе возможен через прямое указание её имени без знака $ echo FOO; // печатает 'something' -echo 'This outputs ' . FOO; // печатает 'This ouputs something' +echo 'This outputs ' . FOO; // печатает 'This outputs something' /******************************** * Массивы @@ -687,45 +689,6 @@ use My\Namespace as SomeOtherNamespace; $cls = new SomeOtherNamespace\MyClass(); -*//********************** -* Позднее статическое связывание. -* -*/ - -class ParentClass -{ - public static function who() - { - echo "I'm a " . __CLASS__ . "\n"; - } - - public static function test() - { - // self ссылается на класс в котором определен метод. - self::who(); - // static ссылается на класс в котором метод вызван. - static::who(); - } -} - -ParentClass::test(); -/* -I'm a ParentClass -I'm a ParentClass -*/ - -class ChildClass extends ParentClass -{ - public static function who() - { - echo "But I'm " . __CLASS__ . "\n"; - } -} - -ChildClass::test(); -/* -I'm a ParentClass -But I'm ChildClass /********************** * Позднее статическое связывание. diff --git a/ru-ru/python3-ru.html.markdown b/ru-ru/python3-ru.html.markdown index 2b6b59a7..bf80fed2 100644 --- a/ru-ru/python3-ru.html.markdown +++ b/ru-ru/python3-ru.html.markdown @@ -106,6 +106,9 @@ False or True #=> True # И строки тоже могут складываться! Хотя лучше не злоупотребляйте этим. "Привет " + "мир!" #=> "Привет мир!" +# Строки можно умножать. +"aa" * 4 #=> "aaaaaaaa" + # Со строкой можно работать, как со списком символов "Это строка"[0] #=> 'Э' diff --git a/ru-ru/ruby-ru.html.markdown b/ru-ru/ruby-ru.html.markdown index 69b5fb46..e69c6d94 100644 --- a/ru-ru/ruby-ru.html.markdown +++ b/ru-ru/ruby-ru.html.markdown @@ -10,6 +10,7 @@ contributors: - ["Nick LaMuro", "https://github.com/NickLaMuro"] translators: - ["Alexey Makarov", "https://github.com/Anakros"] + - ["Vasiliy Petrov", "https://github.com/Saugardas"] --- ```ruby @@ -35,6 +36,13 @@ translators: 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 +2**5 #=> 32 +5 % 3 #=> 2 + +# Побитовые операторы +3 & 5 #=> 1 +3 | 5 #=> 7 +3 ^ 5 #=> 6 # Арифметика -- это синтаксический сахар # над вызовом метода для объекта @@ -57,8 +65,6 @@ false.class #=> FalseClass # Операция неравенства 1 != 1 #=> false 2 != 1 #=> true -!true #=> false -!false #=> true # nil -- имеет такое же логическое значение, как и false @@ -72,6 +78,26 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true +# Оператор сравнения <=> +1 <=> 10 #=> -1 +10 <=> 1 #=> 1 +1 <=> 1 #=> 0 + +# Булевы операторы +true && false #=> false +true || false #=> true +!true #=> false + +# Существуют альтернативные версии логических операторов с гораздо меньшим +# приоритетом. Они используются для связывания операций, пока одна из них +# не вернёт false или true + +# `do_something_else` будет вызван если `do_something` вернёт истинное значение +do_something() and do_something_else() +# `log_error` будет вызван если `do_something` вернёт (nil/false) +do_something() or log_error() + + # Строки -- это объекты 'Я строка'.class #=> String @@ -82,6 +108,16 @@ placeholder = "использовать интерполяцию строк" #=> "Я могу использовать интерполяцию строк, # когда создаю строку с двойными кавычками" +# Конкатенация строк +'hello ' + 'world' #=> "hello world" +'hello ' + 3 #=> TypeError: can't convert Fixnum into String +'hello ' + 3.to_s #=> "hello 3" + +# Умножение строк +'hello ' * 3 #=> "hello hello hello " + +# Добавление к строке +'hello' << ' world' #=> "hello world" # печатать в стандартный вывод puts "Я печатаюсь!" @@ -134,6 +170,7 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Значение в массиве можно получить по индексу с левой границы array[0] #=> 1 +array.first #=> 1 array[12] #=> nil # Как и арифметика, доступ к значению в массиве @@ -143,15 +180,26 @@ array.[] 12 #=> nil # Также, можно получить по индексу с правой границы array[-1] #=> 5 +array.last #=> 5 -# С заданными левой и правой границами индексов -array[2, 4] #=> [3, 4, 5] +# Задавая индекс и количество элементов +array[0,2] #=> [1, 2] +array[0,999] #=> [1, 2, 3, 4, 5] # Или с использованием диапазона значений array[1..3] #=> [2, 3, 4] +# Перестановка элементов в обратном порядке +a = [1, 2, 3] +a.reverse #=> [3, 2, 1] + # Вот так можно добавить значение в массив array << 6 #=> [1, 2, 3, 4, 5, 6] +# Или так +array.push(6) #=> [1, 2, 3, 4, 5, 6] + +# Проверка включения элемента в массив +array.include?(1) #=> true # Хэши -- это массив пар "ключ => значение". # Хэши объявляются с использованием фигурных скобок: @@ -174,17 +222,21 @@ new_hash = { defcon: 3, action: true} new_hash.keys #=> [:defcon, :action] +# Проверка существования ключа и значения в хеше +new_hash.key?(:defcon) #=> true +new_hash.value?(3) #=> true + # Массивы и Хэши -- перечисляемые типы данных # У них есть много полезных методов, например: each, map, count и другие # Управление ходом выполнения (Управляющие структуры) if true - "Если истина" + 'Если истина' elsif false - "Иначе, если ложь (опционально)" + 'Иначе, если ложь (опционально)' else - "Во всех других случаях" + 'Во всех других случаях (тоже опционально)' end for counter in 1..5 @@ -220,7 +272,7 @@ end #=> итерация 5 # Вы также можете ограничивать блоки фигурными скобками: -(1..5).each {|counter| puts "итерация #{counter}"} +(1..5).each { |counter| puts "итерация #{counter}" } # Содержимое структурных данных также можно перебирать используя "each": array.each do |element| @@ -230,6 +282,21 @@ hash.each do |key, value| puts "#{key} -- это #{value}" end +# Если вам нужен индекс вы можете использовать "each_with_index" +# В этом случае индекс будет начинаться с 0 +array.each_with_index do |element, index| + puts "#{element} is number #{index} in the array" +end + +# Если индекс должен начинаться с произвольного значения, +# используйте "each.with_index" +[:q, :w, :e].each.with_index(100) do |element, index| + puts "#{element} -> #{index}" +end +#=> :q -> 100 +#=> :w -> 101 +#=> :e -> 102 + counter = 1 while counter <= 5 do puts "итерация #{counter}" @@ -241,22 +308,65 @@ end #=> итерация 4 #=> итерация 5 +# Существует большое количество других полезных функций, +# например "map", "reduce", "inject", и так далее. Например, "map" +# выполняет связанный с ним блок для каждого элемента перечисляемого объекта, +# возвращая массив результатов. +array = [1, 2, 3, 4, 5] +doubled = array.map do |element| + element * 2 +end +puts doubled +#=> [2, 4, 6, 8, 10] +puts array +#=> [1, 2, 3, 4, 5] + grade = 'B' case grade when 'A' - puts "Так держать, детка!" + puts 'Так держать, детка!' when 'B' - puts "Тебе повезёт в следующий раз" + puts 'Тебе повезёт в следующий раз' when 'C' - puts "Ты можешь сделать лучше" + puts 'Ты можешь сделать лучше' when 'D' - puts "Выскоблил последнее" + puts 'Выскоблил последнее' when 'F' - puts "Ты провалился!" + puts 'Ты провалился!' +else + puts 'Альтернативная система оценок, да?' +end +#=> 'Тебе повезёт в следующий раз' + +# в when также можно использовать диапазоны +grade = 82 +case grade +when 90..100 + puts 'Ура!' +when 80...90 + puts 'Хорошая работа!' +else + puts 'Вы не справились!' +end +#=> 'Хорошая работа!' + +# Обработка исключений +begin + # здесь код, который может вызвать исключение + raise NoMemoryError, 'У вас закончилась память.' +rescue NoMemoryError => exception_variable + puts 'Был вызван NoMemoryError', exception_variable +rescue RuntimeError => other_exception_variable + puts 'Был вызван RuntimeError' else - puts "Альтернативная система оценок, да?" + puts 'Этот код будет выполнятся, если исключения не были вызваны' +ensure + puts 'Этот код выполняется всегда' end +#=> Был вызван NoMemoryError +#=> У вас закончилась память. +#=> Этот код выполняется всегда # Функции @@ -298,6 +408,43 @@ surround { puts 'hello world' } # } +# Вы можете передать блок методу +# "&" отмечает ссылку на переданный блок +def guests(&block) + block.call 'some_argument' +end + +# Чтобы метод принимал произвольное количество аргументов, спереди +# одного из параметров ставится префикс "*" +def method(first, *rest) + p rest +end +method(1, 2, 3, 4) #=> [2, 3, 4] + +# Если метод возвращает массив. можно использовать множественное присваивание +def foods + ['pancake', 'sandwich', 'quesadilla'] +end +breakfast, lunch, dinner = foods +breakfast #=> 'pancake' +dinner #=> 'quesadilla' + +# По соглашению, все методы, возвращающие булево значение +# оканчиваются символом "?" +5.even? #=> false +5.odd? #=> true + +# Если метод заканчивается восклицательным знаком, значит он делает что-то +# опасное или необратимое, например изменяет внутреннее состояние объекта. +# Многие из таких методов-мутаторов часто имеют "безопасную" версию без "!" +# которая возвращает новое значение +company_name = "Dunder Mifflin" +company_name.upcase #=> "DUNDER MIFFLIN" +company_name #=> "Dunder Mifflin" +company_name.upcase! # Изменяем зачение company_name! +company_name #=> "DUNDER MIFFLIN" + + # Определение класса с помощью ключевого слова "class" class Human @@ -323,6 +470,13 @@ class Human @name end + # Тоже самое можно определить с помощью att_accessor + attr_accessor :name + + # Также можно создать методы только для записи или чтения + attr_reader :name + attr_writer :name + # Метод класса определяется с ключевым словом "self", # чтобы можно было отличить его от метода экземпляра класса. # Он может быть вызван только на уровне класса, но не экземпляра. diff --git a/ru-ru/swift-ru.html.markdown b/ru-ru/swift-ru.html.markdown index 7ff660e1..f2b1fd36 100644 --- a/ru-ru/swift-ru.html.markdown +++ b/ru-ru/swift-ru.html.markdown @@ -376,14 +376,14 @@ print("Имя :\(name)") // Имя: Яков // Протокол `Error` используется для перехвата выбрасываемых ошибок enum MyError: Error { - case BadValue(msg: String) - case ReallyBadValue(msg: String) + case badValue(msg: String) + case reallyBadValue(msg: String) } // фунции помеченные словом `throws` должны вызываться с помощью `try` func fakeFetch(value: Int) throws -> String { guard 7 == value else { - throw MyError.ReallyBadValue(msg: "Действительно плохое значение") + throw MyError.reallyBadValue(msg: "Действительно плохое значение") } return "тест" @@ -401,7 +401,7 @@ func testTryStuff() { do { // обычно try оператор, позволяющий обработать ошибку в `catch` блоке try fakeFetch(value: 1) - } catch MyError.BadValue(let msg) { + } catch MyError.badValue(let msg) { print("Ошибка: \(msg)") } catch { // все остальное @@ -535,49 +535,49 @@ if let circle = myEmptyCircle { // Они могут содержать методы подобно классам. enum Suit { - case Spades, Hearts, Diamonds, Clubs + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } // Значения перечислений допускают сокращенный синтаксис, нет необходимости // указывать тип перечисления, когда переменная объявляется явно -var suitValue: Suit = .Hearts +var suitValue: Suit = .hearts // Значения нецелочисленных перечислений должны быть указаны явно // или могут выводится с помощью функции `rawValue` из имени enum BookName: String { - case John - case Luke = "Лука" + case john + case luke = "Лука" } -print("Имя: \(BookName.John.rawValue)") +print("Имя: \(BookName.john.rawValue)") // Перечисление (enum) со связанными значениями enum Furniture { // Связать с типом Int - case Desk(height: Int) + case desk(height: Int) // Связать с типами String и Int - case Chair(String, Int) + case chair(String, Int) func description() -> String { switch self { - case .Desk(let height): + case .desk(let height): return "Письменный стол высотой \(height) см." - case .Chair(let brand, let height): + case .chair(let brand, let height): return "Стул марки \(brand) высотой \(height) см." } } } -var desk: Furniture = .Desk(height: 80) +var desk: Furniture = .desk(height: 80) print(desk.description()) // "Письменный стол высотой 80 см." -var chair = Furniture.Chair("Foo", 40) +var chair = Furniture.chair("Foo", 40) print(chair.description()) // "Стул марки Foo высотой 40 см." diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown index 1fbcc752..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 @@ -88,7 +76,7 @@ implementation. to have stopped since Microsoft pulled their support. Ruby implementations may have their own release version numbers, but they always -target a specific version of MRI for compatability. Many implementations have +target a specific version of MRI for compatibility. Many implementations have the ability to enter different modes (for example, 1.8 or 1.9 mode) to specify which MRI version to target. diff --git a/ruby.html.markdown b/ruby.html.markdown index a1532855..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 @@ -359,16 +348,16 @@ ensure puts 'This code always runs no matter what' end -# Functions +# Methods def double(x) x * 2 end -# Functions (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 function -# "&" 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/scala.html.markdown b/scala.html.markdown index 192af953..7429ac9a 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -280,6 +280,8 @@ r foreach println // NB: Scala is quite lenient when it comes to dots and brackets - study the // rules separately. This helps write DSLs and APIs that read like English +// Why doesn't `println` need any parameters here? +// Stay tuned for first-class functions in the Functional Programming section below! (5 to 1 by -1) foreach (println) // A while loop @@ -303,7 +305,7 @@ do { // Recursion is the idiomatic way of repeating an action in Scala (as in most // other functional languages). // Recursive functions need an explicit return type, the compiler can't infer it. -// Here it's Unit. +// Here it's Unit, which is analagous to a `void` return type in Java def showNumbersInRange(a: Int, b: Int): Unit = { print(a) if (a < b) @@ -416,8 +418,8 @@ class Dog(br: String) { private def sleep(hours: Int) = println(s"I'm sleeping for $hours hours") - // Abstract methods are simply methods with no body. If we uncomment the next - // line, class Dog would need to be declared abstract + // Abstract methods are simply methods with no body. If we uncomment the + // def line below, class Dog would need to be declared abstract like so: // abstract class Dog(...) { ... } // def chaseAfter(what: String): String } @@ -459,13 +461,57 @@ george.phoneNumber // => "1234" Person("George", "1234") == Person("Kate", "1236") // => false // Easy way to copy -// otherGeorge == Person("george", "9876") +// otherGeorge == Person("George", "9876") val otherGeorge = george.copy(phoneNumber = "9876") // And many others. Case classes also get pattern matching for free, see below. +// Traits +// Similar to Java interfaces, traits define an object type and method +// signatures. Scala allows partial implementation of those methods. +// Constructor parameters are not allowed. Traits can inherit from other +// traits or classes without parameters. + +trait Dog { + def breed: String + def color: String + def bark: Boolean = true + def bite: Boolean +} +class SaintBernard extends Dog { + val breed = "Saint Bernard" + val color = "brown" + def bite = false +} + +scala> b +res0: SaintBernard = SaintBernard@3e57cd70 +scala> b.breed +res1: String = Saint Bernard +scala> b.bark +res2: Boolean = true +scala> b.bite +res3: Boolean = false + +// A trait can also be used as Mixin. The class "extends" the first trait, +// but the keyword "with" can add additional traits. + +trait Bark { + def bark: String = "Woof" +} +trait Dog { + def breed: String + def color: String +} +class SaintBernard extends Dog with Bark { + val breed = "Saint Bernard" + val color = "brown" +} -// Traits coming soon! +scala> val b = new SaintBernard +b: SaintBernard = SaintBernard@7b69c6ba +scala> b.bark +res0: String = Woof ///////////////////////////////////////////////// @@ -483,7 +529,9 @@ def matchPerson(person: Person): String = person match { case Person(name, number) => "We matched someone : " + name + ", phone : " + number } -val email = "(.*)@(.*)".r // Define a regex for the next example. +// Regular expressions are also built in. +// Create a regex with the `r` method on a string: +val email = "(.*)@(.*)".r // Pattern matching might look familiar to the switch statements in the C family // of languages, but this is much more powerful. In Scala, you can match much @@ -549,6 +597,8 @@ List("Dom", "Bob", "Natalia") foreach println // Combinators +// Using `s` from above: +// val s = Set(1, 3, 7) s.map(sq) @@ -568,8 +618,8 @@ List( ).filter(_.age > 25) // List(Person("Bob", 30)) -// Scala a foreach method defined on certain collections that takes a type -// returning Unit (a void method) +// Certain collections (such as List) in Scala have a `foreach` method, +// which takes as an argument a type returning Unit - that is, a void method val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100) aListOfNumbers foreach (x => println(x)) aListOfNumbers foreach println @@ -670,7 +720,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/shutit.html.markdown b/shutit.html.markdown index d16290b3..67d7a4b5 100644 --- a/shutit.html.markdown +++ b/shutit.html.markdown @@ -10,7 +10,7 @@ filename: learnshutit.html ShutIt is an shell automation framework designed to be easy to use. -It is a wrapper around a python-based expect clone (pexpect). +It is a wrapper around a Python-based expect clone (pexpect). You can look at it as 'expect without the pain'. @@ -167,8 +167,8 @@ session2.logout() Here you use the 'send\_and\_get\_output' method to retrieve the output of the capacity command (df). -There are much more elegant ways to do the above (eg have a dictionary of the -servers to iterate over), but it's up to you how clever you need the python to +There are much more elegant ways to do the above (e.g. have a dictionary of the +servers to iterate over), but it's up to you how clever you need the Python to be. @@ -300,7 +300,7 @@ over a minute to complete (using the 'wait' method). Again, this is trivial, but imagine you have hundreds of servers to manage like this and you can see the power it can bring in a few lines of code and one -python import. +Python import. ## Learn More diff --git a/sk-sk/bash.html.markdown b/sk-sk/bash-sk.html.markdown index e9d1490c..e9d1490c 100644 --- a/sk-sk/bash.html.markdown +++ b/sk-sk/bash-sk.html.markdown diff --git a/sk-sk/coffeescript.html.markdown b/sk-sk/coffeescript-sk.html.markdown index 30bbceec..30bbceec 100644 --- a/sk-sk/coffeescript.html.markdown +++ b/sk-sk/coffeescript-sk.html.markdown diff --git a/sk-sk/elixir.html.markdown b/sk-sk/elixir-sk.html.markdown index 2401f92e..2401f92e 100644 --- a/sk-sk/elixir.html.markdown +++ b/sk-sk/elixir-sk.html.markdown diff --git a/sk-sk/git.html.markdown b/sk-sk/git-sk.html.markdown index 21741406..21741406 100644 --- a/sk-sk/git.html.markdown +++ b/sk-sk/git-sk.html.markdown diff --git a/sk-sk/json.html.markdown b/sk-sk/json-sk.html.markdown index 2b1fbb58..2b1fbb58 100644 --- a/sk-sk/json.html.markdown +++ b/sk-sk/json-sk.html.markdown diff --git a/sk-sk/latex.html.markdown.tex b/sk-sk/latex-sk.html.markdown.tex index 5e2f9c7f..5e2f9c7f 100644 --- a/sk-sk/latex.html.markdown.tex +++ b/sk-sk/latex-sk.html.markdown.tex diff --git a/sk-sk/ruby.html.markdown b/sk-sk/ruby-sk.html.markdown index 799865b0..799865b0 100644 --- a/sk-sk/ruby.html.markdown +++ b/sk-sk/ruby-sk.html.markdown diff --git a/smalltalk.html.markdown b/smalltalk.html.markdown index cc7ab84c..a253df55 100644 --- a/smalltalk.html.markdown +++ b/smalltalk.html.markdown @@ -1,5 +1,6 @@ --- language: smalltalk +filename: smalltalk.st contributors: - ["Jigyasa Grover", "https://github.com/jig08"] --- @@ -185,7 +186,7 @@ x := Float pi. "pi" x := Float e. "exp constant" x := Float infinity. "infinity" x := Float nan. "not-a-number" -x := Random new next; yourself. x next. "random number stream (0.0 to 1.0) +x := Random new next; yourself. x next. "random number stream (0.0 to 1.0)" x := 100 atRandom. "quick random number" ``` @@ -903,11 +904,11 @@ b := String isVariable. "true if has indexed b := String isPointers. "true if index instance vars contain objects" b := String isBits. "true if index instance vars contain bytes/words" b := String isBytes. "true if index instance vars contain bytes" -b := String isWords. true if index instance vars contain words" +b := String isWords. "true if index instance vars contain words" Object withAllSubclasses size. "get total number of class entries" ``` -## Debuging: +## Debugging: ``` | a b x | x yourself. "returns receiver" diff --git a/solidity.html.markdown b/solidity.html.markdown index 602d74f0..004c225e 100644 --- a/solidity.html.markdown +++ b/solidity.html.markdown @@ -37,6 +37,9 @@ features are typically marked, and subject to change. Pull requests welcome. // simple_bank.sol (note .sol extension) /* **** START EXAMPLE **** */ +// Declare the source file compiler version +pragma solidity ^0.4.19; + // Start with Natspec comment (the three slashes) // used for documentation - and as descriptive data for UI elements/actions @@ -62,7 +65,7 @@ contract SimpleBank { // CapWords event LogDepositMade(address accountAddress, uint amount); // Constructor, can receive one or many variables here; only one allowed - function SimpleBank() { + function SimpleBank() public { // msg provides details about the message that's sent to the contract // msg.sender is contract caller (address of contract creator) owner = msg.sender; @@ -70,7 +73,11 @@ contract SimpleBank { // CapWords /// @notice Deposit ether into bank /// @return The balance of the user after the deposit is made - function deposit() public returns (uint) { + function deposit() public payable returns (uint) { + // Use 'require' to test user inputs, 'assert' for internal invariants + // Here we are making sure that there isn't an overflow issue + require((balances[msg.sender] + msg.value) >= balances[msg.sender]); + balances[msg.sender] += msg.value; // no "this." or "self." required with state variable // all values set to data type's initial value by default @@ -85,18 +92,17 @@ contract SimpleBank { // CapWords /// @param withdrawAmount amount you want to withdraw /// @return The balance remaining for the user function withdraw(uint withdrawAmount) public returns (uint remainingBal) { - if(balances[msg.sender] >= withdrawAmount) { - // Note the way we deduct the balance right away, before sending - due to - // the risk of a recursive call that allows the caller to request an amount greater - // than their balance - balances[msg.sender] -= withdrawAmount; - - if (!msg.sender.send(withdrawAmount)) { - // increment back only on fail, as may be sending to contract that - // has overridden 'send' on the receipt end - balances[msg.sender] += withdrawAmount; - } - } + require(withdrawAmount <= balances[msg.sender]); + + // Note the way we deduct the balance right away, before sending + // Every .transfer/.send from this contract can call an external function + // This may allow the caller to request an amount greater + // than their balance using a recursive call + // Aim to commit state before calling external functions, including .transfer/.send + balances[msg.sender] -= withdrawAmount; + + // this automatically throws on a failure, which means the updated balance is reverted + msg.sender.transfer(withdrawAmount); return balances[msg.sender]; } @@ -105,18 +111,9 @@ contract SimpleBank { // CapWords /// @return The balance of the user // 'constant' prevents function from editing state variables; // allows function to run locally/off blockchain - function balance() constant returns (uint) { + function balance() constant public returns (uint) { return balances[msg.sender]; } - - // Fallback function - Called if other functions don't match call or - // sent ether without data - // Typically, called when invalid data is sent - // Added so ether sent to this contract is reverted if the contract fails - // otherwise, the sender's money is transferred to contract - function () { - throw; // throw reverts state to before call - } } // ** END EXAMPLE ** @@ -134,6 +131,11 @@ int256 constant a = 8; // same effect as line above, here the 256 is explicit uint constant VERSION_ID = 0x123A1; // A hex constant // with 'constant', compiler replaces each occurrence with actual value +// 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 +int256 public a = 8; // For int and uint, can explicitly set space in steps of 8 up to 256 // e.g., int8, int16, int24 @@ -142,6 +144,12 @@ int64 c; uint248 e; // Be careful that you don't overflow, and protect against attacks that do +// For example, for an addition, you'd do: +uint256 c = a + b; +assert(c >= a); // assert tests for internal invariants; require is used for user inputs +// For more examples of common arithmetic issues, see Zeppelin's SafeMath library +// https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/math/SafeMath.sol + // No random functions built in, use other contracts for randomness @@ -162,14 +170,14 @@ address public owner; // a getter is automatically created, but NOT a setter // All addresses can be sent ether -owner.send(SOME_BALANCE); // returns false on failure -if (owner.send) {} // REMEMBER: wrap in 'if', as contract addresses have +owner.transfer(SOME_BALANCE); // fails and reverts on failure + +// Can also do a lower level .send call, which returns a false if it failed +if (owner.send) {} // REMEMBER: wrap send in 'if', as contract addresses have // functions executed on send and these can fail // Also, make sure to deduct balances BEFORE attempting a send, as there is a risk of a recursive // call that can drain the contract -// can override send by defining your own - // Can check balance owner.balance; // the balance of the owner (user or contract) @@ -188,7 +196,7 @@ string n = "hello"; // stored in UTF8, note double quotes, not single // string utility functions to be added in future // prefer bytes32/bytes, as UTF8 uses more storage -// Type inferrence +// Type inference // var does inferred typing based on first assignment, // can't be used in functions parameters var a = true; @@ -210,7 +218,7 @@ uint x = 5; // Destructuring/Tuples -(x, y) = (2, 7); // assign/swap multiple value +(x, y) = (2, 7); // assign/swap multiple values // 2. DATA STRUCTURES @@ -247,7 +255,7 @@ delete balances; // sets all elements to 0 // mapping, without knowing source keys - can build data structure // on top to do this -// Structs and enums +// Structs struct Bank { address owner; uint balance; @@ -259,7 +267,7 @@ Bank b = Bank({ // or Bank c = Bank(msg.sender, 5); -c.amount = 5; // set to new value +c.balance = 5; // set to new value delete b; // sets to initial value, set all variables in struct to 0, except mappings @@ -270,7 +278,7 @@ state = State.Created; // enums can be explicitly converted to ints uint createdState = uint(State.Created); // 0 -// Data locations: Memory vs. storage vs. stack - all complex types (arrays, +// Data locations: Memory vs. storage vs. calldata - all complex types (arrays, // structs) have a data location // 'memory' does not persist, 'storage' does // Default is 'storage' for local and state variables; 'memory' for func params @@ -289,13 +297,13 @@ uint createdState = uint(State.Created); // 0 // 4. Global Variables of note // ** this ** this; // address of contract -// often used at end of contract life to send remaining balance to party +// often used at end of contract life to transfer remaining balance to party this.balance; this.someFunction(); // calls func externally via call, not via internal jump // ** msg - Current message received by the contract ** ** msg.sender; // address of sender -msg.value; // amount of ether provided to this contract in wei +msg.value; // amount of ether provided to this contract in wei, the function should be marked "payable" msg.data; // bytes, complete call data msg.gas; // remaining gas @@ -305,6 +313,8 @@ tx.gasprice; // gas price of the transaction // ** block - Information about current block ** now; // current time (approximately), alias for block.timestamp (uses Unix time) +// Note that this can be manipulated by miners, so use carefully + block.number; // current block number block.difficulty; // current block difficulty block.blockhash(1); // returns bytes32, only works for most recent 256 blocks @@ -331,9 +341,10 @@ function increment(uint x, uint y) returns (uint x, uint y) { // Call previous functon uint (a,b) = increment(1,1); -// 'constant' indicates that function does not/cannot change persistent vars +// 'constant' (alias for 'view') +// indicates that function does not/cannot change persistent vars // Constant function execute locally, not on blockchain -uint y; +uint y = 1; function increment(uint x) constant returns (uint x) { x += 1; @@ -341,13 +352,21 @@ 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 +// even allow reading of state vars +// The exact rules are more complicated, so see more about +// constant/pure: +// http://solidity.readthedocs.io/en/develop/contracts.html#view-functions + // 'Function Visibility specifiers' // These can be placed where 'constant' is, including: -// public - visible externally and internally (default) -// external +// public - visible externally and internally (default for function) +// external - only visible externally (including a call made with this.) // private - only visible in the current contract // internal - only visible in current contract, and those deriving from it +// Generally, a good idea to mark each function explicitly + // Functions hoisted - and can assign a function to a variable function a() { var z = b; @@ -358,8 +377,15 @@ function b() { } +// All functions that receive ether must be marked 'payable' +function depositEther() public payable { + balances[msg.sender] += msg.value; +} + // Prefer loops to recursion (max call stack depth is 1024) +// Also, don't setup loops that you haven't bounded, +// as this can hit the gas limit // B. Events // Events are notify external parties; easy to search and @@ -373,10 +399,11 @@ function b() { 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: -Coin.Sent().watch({}, '', function(error, result) { +// For an external party (a contract or external entity), to watch using +// the Web3 Javascript library: +Coin.LogSent().watch({}, '', function(error, result) { if (!error) { console.log("Coin transfer: " + result.args.amount + " coins were sent from " + result.args.from + @@ -395,10 +422,10 @@ Coin.Sent().watch({}, '', function(error, result) { // '_' (underscore) often included as last line in body, and indicates // function being called should be placed there -modifier onlyAfter(uint _time) { if (now <= _time) throw; _ } -modifier onlyOwner { if (msg.sender == owner) _ } +modifier onlyAfter(uint _time) { require (now >= _time); _; } +modifier onlyOwner { require(msg.sender == owner) _; } // commonly used with state machines -modifier onlyIfState (State currState) { if (currState != State.A) _ } +modifier onlyIfStateA (State currState) { require(currState == State.A) _; } // Append right after function declaration function changeOwner(newOwner) @@ -412,12 +439,10 @@ onlyIfState(State.A) // underscore can be included before end of body, // but explicitly returning will skip, so use carefully modifier checkValue(uint amount) { - _ + _; if (msg.value > amount) { uint amountToRefund = amount - msg.value; - if (!msg.sender.send(amountToRefund)) { - throw; - } + msg.sender.transfer(amountToRefund); } } @@ -434,22 +459,21 @@ modifier checkValue(uint amount) { // amount of gas for a block of code - and will fail if that is exceeded // For example: for(uint x = 0; x < refundAddressList.length; x++) { - if (!refundAddressList[x].send(SOME_AMOUNT)) { - throw; - } + refundAddressList[x].transfer(SOME_AMOUNT); } // Two errors above: -// 1. A failure on send stops the loop from completing, tying up money +// 1. A failure on transfer stops the loop from completing, tying up money // 2. This loop could be arbitrarily long (based on the amount of users who need refunds), and // therefore may always fail as it exceeds the max gas for a block // Instead, you should let people withdraw individually from their subaccount, and mark withdrawn +// e.g., favor pull payments over push payments // 7. OBJECTS/CONTRACTS // A. Calling external contract -contract infoFeed { +contract InfoFeed { function info() returns (uint ret) { return 42; } } @@ -484,7 +508,7 @@ contract MyContract is abc, def("a custom argument to def") { function z() { if (msg.sender == owner) { def.z(); // call overridden function from def - super.z(); // call immediate parent overriden function + super.z(); // call immediate parent overridden function } } } @@ -499,23 +523,10 @@ function someAbstractFunction(uint x); import "filename"; import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol"; -// Importing under active development -// Cannot currently be done at command line - // 8. OTHER KEYWORDS -// A. Throwing -// Throwing -throw; // reverts unused money to sender, state is reverted -// Can't currently catch - -// Common design pattern is: -if (!addr.send(123)) { - throw; -} - -// B. Selfdestruct +// A. Selfdestruct // selfdestruct current contract, sending funds to address (often creator) selfdestruct(SOME_ADDRESS); @@ -540,7 +551,7 @@ function remove() { // that is private needs to be obfuscated (e.g., hashed w/secret) // Steps: 1. Commit to something, 2. Reveal commitment -sha3("some_bid_amount", "some secret"); // commit +keccak256("some_bid_amount", "some secret"); // commit // call contract's reveal function in the future // showing bid plus secret that hashes to SHA3 @@ -614,6 +625,7 @@ contract SomeOracle { // ** START EXAMPLE ** // CrowdFunder.sol +pragma solidity ^0.4.19; /// @title CrowdFunder /// @author nemild @@ -647,22 +659,20 @@ contract CrowdFunder { event LogWinnerPaid(address winnerAddress); modifier inState(State _state) { - if (state != _state) throw; - _ + require(state == _state); + _; } modifier isCreator() { - if (msg.sender != creator) throw; - _ + require(msg.sender == creator); + _; } - // Wait 6 months after final contract state before allowing contract destruction + // Wait 24 weeks after final contract state before allowing contract destruction modifier atEndOfLifecycle() { - if(!((state == State.ExpiredRefund || state == State.Successful) && - completeAt + 6 months < now)) { - throw; - } - _ + require(((state == State.ExpiredRefund || state == State.Successful) && + completeAt + 24 weeks < now)); + _; } function CrowdFunder( @@ -670,6 +680,7 @@ contract CrowdFunder { string _campaignUrl, address _fundRecipient, uint _minimumToRaise) + public { creator = msg.sender; fundRecipient = _fundRecipient; @@ -680,7 +691,9 @@ contract CrowdFunder { function contribute() public + payable inState(State.Fundraising) + returns(uint256 id) { contributions.push( Contribution({ @@ -696,7 +709,9 @@ contract CrowdFunder { return contributions.length - 1; // return id } - function checkIfFundingCompleteOrExpired() { + function checkIfFundingCompleteOrExpired() + public + { if (totalRaised > minimumToRaise) { state = State.Successful; payOut(); @@ -712,31 +727,23 @@ contract CrowdFunder { public inState(State.Successful) { - if(!fundRecipient.send(this.balance)) { - throw; - } - - + fundRecipient.transfer(this.balance); LogWinnerPaid(fundRecipient); } - function getRefund(id) - public + function getRefund(uint256 id) inState(State.ExpiredRefund) + public + returns(bool) { - if (contributions.length <= id || id < 0 || contributions[id].amount == 0 ) { - throw; - } + require(contributions.length > id && id >= 0 && contributions[id].amount != 0 ); - uint amountToRefund = contributions[id].amount; + uint256 amountToRefund = contributions[id].amount; contributions[id].amount = 0; - if(!contributions[id].contributor.send(amountToSend)) { - contributions[id].amount = amountToSend; - return false; - } + contributions[id].contributor.transfer(amountToRefund); - return true; + return true; } function removeContract() @@ -747,8 +754,6 @@ contract CrowdFunder { selfdestruct(msg.sender); // creator gets all money that hasn't be claimed } - - function () { throw; } } // ** END EXAMPLE ** @@ -795,6 +800,7 @@ someContractAddress.callcode('function_name'); // 13. STYLE NOTES // Based on Python's PEP8 style guide +// Full Style guide: http://solidity.readthedocs.io/en/develop/style-guide.html // Quick summary: // 4 spaces for indentation @@ -804,7 +810,7 @@ someContractAddress.callcode('function_name'); // else should be placed on own line -// 14. NATSPEC COMENTS +// 14. NATSPEC COMMENTS // used for documentation, commenting, and external UIs // Contract natspec - always above contract definition @@ -822,11 +828,15 @@ someContractAddress.callcode('function_name'); ## Additional resources - [Solidity Docs](https://solidity.readthedocs.org/en/latest/) -- [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. -- [Browser-based Solidity Editor](http://chriseth.github.io/browser-solidity/) +- [Smart Contract Best Practices](https://github.com/ConsenSys/smart-contract-best-practices) +- [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) - [Modular design strategies for Ethereum Contracts](https://docs.erisindustries.com/tutorials/solidity/) +## Important libraries +- [Zeppelin](https://github.com/OpenZeppelin/zeppelin-solidity/): Libraries that provide common contract patterns (crowdfuding, safemath, etc) + ## Sample contracts - [Dapp Bin](https://github.com/ethereum/dapp-bin) - [Solidity Baby Step Contracts](https://github.com/fivedogit/solidity-baby-steps/tree/master/contracts) @@ -838,13 +848,11 @@ someContractAddress.callcode('function_name'); - [Smart Contract Security](https://blog.ethereum.org/2016/06/10/smart-contract-security/) - [Hacking Distributed Blog](http://hackingdistributed.com/) -## Information purposefully excluded -- Libraries - ## 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/standard-ml.html.markdown b/standard-ml.html.markdown index c9eb2a2e..b34f1c08 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -1,10 +1,12 @@ --- language: "Standard ML" +filename: standardml.sml contributors: - ["Simon Shine", "http://shine.eu.org/"] - ["David Pedersen", "http://lonelyproton.com/"] - ["James Baker", "http://www.jbaker.io/"] - ["Leo Zovic", "http://langnostic.inaimathi.ca/"] + - ["Chris Wilson", "http://sencjw.com/"] --- Standard ML is a functional programming language with type inference and some @@ -265,6 +267,16 @@ fun second_elem (x::y::xs) = y fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs | evenly_positioned_elems [odd] = [] (* Base case: throw away *) | evenly_positioned_elems [] = [] (* Base case *) + +(* The case expression can also be used to pattern match and return a value *) +datatype temp = + C of real + | F of real + +fun temp_to_f t = + case t of + C x => x * (9.0 / 5.0) + 32.0 + | F x => x (* When matching on records, you must use their slot names, and you must bind every slot in a record. The order of the slots doesn't matter though. *) @@ -351,7 +363,10 @@ val _ = print (say(Red) ^ "\n") fun say Red = "You are red!" | say Green = "You are green!" | say Blue = "You are blue!" - | say _ = raise Fail "Unknown color" + +(* We did not include the match arm `say _ = raise Fail "Unknown color"` +because after specifying all three colors, the pattern is exhaustive +and redundancy is not permitted in pattern matching *) (* Here is a binary tree datatype *) @@ -385,7 +400,7 @@ fun calculate_interest(n) = if n < 0.0 (* Exceptions can be caught using "handle" *) val balance = calculate_interest ~180.0 - handle Domain => ~180.0 (* x now has the value ~180.0 *) + handle Domain => ~180.0 (* balance now has the value ~180.0 *) (* Some exceptions carry extra information with them *) (* Here are some examples of built-in exceptions *) @@ -395,7 +410,7 @@ fun failing_function [] = raise Empty (* used for empty lists *) | failing_function xs = raise Fail "This list is too long!" (* We can pattern match in 'handle' to make sure - a specfic exception was raised, or grab the message *) + a specific exception was raised, or grab the message *) val err_msg = failing_function [1,2] handle Fail _ => "Fail was raised" | Domain => "Domain was raised" | Empty => "Empty was raised" @@ -459,5 +474,5 @@ fun decrement_ret x y = (x := !x - 1; y) [Moscow ML](http://mosml.org), [SML/NJ](http://smlnj.org/). * Follow the Coursera course [Programming Languages](https://www.coursera.org/course/proglang). -* Get the book *ML for the Working Programmer* by Larry C. Paulson. +* Read *[ML for the Working Programmer](https://www.cl.cam.ac.uk/~lp15/MLbook/pub-details.html)* by Larry C. Paulson. * Use [StackOverflow's sml tag](http://stackoverflow.com/questions/tagged/sml). diff --git a/sv-se/brainfuck-sv.html.markdown b/sv-se/brainfuck-sv.html.markdown index e9fbc436..57520955 100644 --- a/sv-se/brainfuck-sv.html.markdown +++ b/sv-se/brainfuck-sv.html.markdown @@ -1,5 +1,6 @@ --- language: brainfuck +filename: brainfuck-sv.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] diff --git a/sv-se/haskell-sv.html.markdown b/sv-se/haskell-sv.html.markdown new file mode 100644 index 00000000..da2d6ab0 --- /dev/null +++ b/sv-se/haskell-sv.html.markdown @@ -0,0 +1,461 @@ +--- +language: Haskell +filename: learnhaskell-sv.hs +contributors: + - ["Adit Bhargava", "http://adit.io"] +translators: + - ["Edward Tjörnhammar", "http://edwtjo.me"] +lang: sv-se +--- + +Haskell skapades för att vara ett praktiskt, rent, funktionellt +programmeringsspråk. Det är känt för sin använding av monader och dess +härledande typsystem men anledningen till att jag ständigt återbesöker språket +är på grund av dess elegans. Haskell gör programmering till ett rent nöje. + +```haskell +-- Radkommenterar börjar med två bindestreck. +{- Flerradskommentarer innesluts av vänster/höger måsvinge bindestreck +block på detta vis. +-} + +---------------------------------------------------- +-- 1. Fördefinierade datatyper och operatorer +---------------------------------------------------- + +-- Du har siffror +3 -- 3 + +-- Matte fungerar som förväntat +1 + 1 -- 2 +8 - 1 -- 7 +10 * 2 -- 20 +35 / 5 -- 7.0 + +-- Division är normalt inte heltalsdivision +35 / 4 -- 8.75 + +-- Heltalsdivision, här infix div +35 `div` 4 -- 8 + +-- Boolar (Sant och Falskt) är fördefinierade +True +False + +-- Samt dess operationer +not True -- False +not False -- True +1 == 1 -- True +1 /= 1 -- False +1 < 10 -- True + +-- I ovanstående exempel är `not` en funktion vilken bara tar ett argument. +-- Haskell behöver inte paranteser för sina funktionsanrop... alla argument +-- ges mellanslagsseparerade direkt efter funktionen. Det övergripande mönstret +-- är: +-- func arg1 arg2 arg3... +-- Se sektionen om funktioner för information om hur du skriver dina egna. + +-- Strängar och bokstäver +"Detta är en sträng" +'a' -- bokstav +'Du kan inte använda enkelfnutt för strängar.' -- fel! + +-- Strängar kan konkateneras +"Hej " ++ "världen!" -- "Hej världen!" + +-- En sträng är en lista av bokstäver +['H', 'e', 'j', 's', 'a', 'n'] -- "Hejsan" +"Detta är en sträng" !! 0 -- 'D' + + +---------------------------------------------------- +-- 2. Listor och Tupler +---------------------------------------------------- + +-- Varje element i en lista måste ha samma typ. +-- Dessa listor är ekvivalenta: +[1, 2, 3, 4, 5] +[1..5] + +-- Intervall är mångsidiga. +['A'..'F'] -- "ABCDEF" + +-- Man kan stega intervall. +[0,2..10] -- [0, 2, 4, 6, 8, 10] +[5..1] -- [] (Haskell förutsätter normalt inkrement) +[5,4..1] -- [5, 4, 3, 2, 1] + +-- Indexering in i en lista +[1..10] !! 3 -- 4 (nollindexerat) + +-- Man kan ha oändliga listor i Haskell! +[1..] -- listan över alla naturliga tal + +-- Oändliga listor fungerar enbart för att Haskell har "lat evaluering". +-- Det betyder att Haskell bara evaluerar de uttryck den måste. Du kan alltså +-- fråga efter det 1000:e elementet i en oändlig lista och Haskell kommer då ge +-- dig det: + +[1..] !! 999 -- 1000 + +-- Nu har Haskell evaluerat element 1 till 1000 i denna lista... men resten +-- av medlemmarna i denna oändliga lista existerar inte ännu! Haskell kommer +-- faktiskt inte utvärdera element den inte måste. + +-- Sammanslagning av två listor +[1..5] ++ [6..10] + +-- Lägg till 0 vid listhuvudet +0:[1..5] -- [0, 1, 2, 3, 4, 5] + +-- fler listoperationer som huvud, svans, initiella samt sista +head [1..5] -- 1 +tail [1..5] -- [2, 3, 4, 5] +init [1..5] -- [1, 2, 3, 4] +last [1..5] -- 5 + +-- listomfattningar +[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10] + +-- med bivilkor +[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10] + +-- Varje element i en tupel kan ha olika typ men en tupel kan bara ha en +-- fixerad, eller statisk, längd. +-- En tupel: +("haskell", 1) + +-- För att komma åt element i ett par, alltså en 2-tupel, finns +-- de fördefinierade funktionerna: +fst ("haskell", 1) -- "haskell" +snd ("haskell", 1) -- 1 + +---------------------------------------------------- +-- 3. Funktioner +---------------------------------------------------- +-- En enkel funktion med två parametrar +add a b = a + b + +-- Notera även att om du använder ghci (Haskellinterpretatorn) kommer du behöva +-- använda `let` namnbindning för att synliggöra din funktionsdeklaration, +-- alltså +let add a b = a + b + +-- För att använda funktionen +add 1 2 -- 3 + +-- Man kan även göra funktionsanropet infix, alltså mellan parametersättningen, +-- med hjälp av bakåtfnuttar: +1 `add` 2 -- 3 + +-- Du kan även definiera funktioner vars funktionsnamn avsaknar bokstäver! +-- Med hjälp av parenteser kan du därmed definiera operatorer (normalt infix)! +-- Följande är en operator för heltalsdivision, vilken förlitar sig på div: +(//) a b = a `div` b +35 // 4 -- 8 + +-- Funktionsvakter: ett enkelt sätt att grena ut dina funktioner +fib x + | x < 2 = 1 + | otherwise = fib (x - 1) + fib (x - 2) + +-- Mönstermatchning fungerar på liknande vis. Här ger vi tre olika +-- parametermatchningar för vårat fib-resulat. Haskell kommer automatiskt följa +-- första bästa träff, uppifrån ned, vars vänstra sida om likhetstecknet matchar +-- anroparens parametervärde. +fib 1 = 1 +fib 2 = 2 +fib x = fib (x - 1) + fib (x - 2) + +-- Mönstermatchning på tupler: +foo (x, y) = (x + 1, y + 2) + +-- Mönstermatchning på listor. Här är `x` det första elementet i listan och `xs` +-- är resten av listan. Nu kan vi skriva våran egen map-funktion +minMap func [] = [] +minMap func (x:xs) = func x:(minMap func xs) + +-- Anonyma funktioner, eller lambdauttryck, skapas med hjälp av omvänt +-- snedstreck, följt av parametrarna +minMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] + +-- Användning av fold (även kallad `inject`, `reduce`, osv.) tillsammans med en +-- anonym funktion. `fold1` är en vänstervikande funktion och använder första +-- värdet i listan som det initiella värdet för ackumulatorn. +foldl1 (\acc x -> acc + x) [1..5] -- 15 + +---------------------------------------------------- +-- 4. Mer funktioner +---------------------------------------------------- + +-- Partiell applikation: +-- Om du inte anropar funktionen med alla sina argument +-- blir den partiellt applicerad. Det betyder att du erhåller en funktion där en +-- delmängd av parametrarna blivit värdesatta men några är fortfarande fria. +add a b = a + b +foo = add 10 -- foo är nu en funktion som tar ett nummer och lägger till 10 till + -- det +foo 5 -- 15 + +-- Ett annat sätt att skriva samma sak +foo = (10+) +foo 5 -- 15 + +-- Funktionskomposition: +-- Operatorn `.` kedjar ihop funktioner +-- Till exempel, nedan är `foo` en funktion som tar ett värde, den adderar 10 +-- till det, multiplicerar det resultatet med 4 och sen ersätts med det värdet. +foo = (4*) . (10+) + +-- 4*(10+5) = 60 +foo 5 -- 60 + +-- Precedensordning: +-- Haskell har en operator `$`. Denna operator applicerar en funktion till en +-- given parameter med dess precedens. I kontrast mot vanlig +-- funktionsapplikation, vilket har den högsta utvärderingsprioriteten 10 och +-- associerar till vänster, har denna prioritetsordning 0 och är +-- högerassociativ. Denna låga prioritet medför att parameteruttrycket till +-- höger om operatorn får det reducerat innan det appliceras till sin vänster. + +-- före +even (fib 7) -- falskt + +-- ekvivalent +even $ fib 7 -- falskt + +-- med funktionskomposition +even . fib $ 7 -- falskt + + +---------------------------------------------------- +-- 5. Typsignaturer +---------------------------------------------------- + +-- Haskell har ett väldigt starkt typsystem, alla giltiga uttryck har en typ. + +-- Några grundläggande typer: +5 :: Integer +"hello" :: String +True :: Bool + +-- Funktioner har också typer, +-- `not` tar en bool och returnerar en bool: +-- not :: Bool -> Bool + +-- Här är ett exempel på en funktionssignatur vilken beskriver en funktion som +-- reducerar två heltal till ett: +-- add :: Integer -> Integer -> Integer + +-- Trots att Haskell härleder typen på icke typsatta uttryck är det bra form att +-- explicit ange dessa för ens deklarerade funktioner: +double :: Integer -> Integer +double x = x * 2 + +---------------------------------------------------- +-- 6. Kontrollflöde och Ifsatser +---------------------------------------------------- + +-- if-sats +haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome" + +-- if-statser kan spridas över rader men indentering har betydelse +haskell = if 1 == 1 + then "awesome" + else "awful" + +-- case uttryck: följande är ett exempel på kommandoradsparsning +case args of + "help" -> printHelp + "start" -> startProgram + _ -> putStrLn "bad args" + +-- Haskell har inte loopar istället används recursion. +-- map applicerar en funktion över varje element i en lista + +map (*2) [1..5] -- [2, 4, 6, 8, 10] + +-- man kan deklarera en for funktion genom att använda map +for array func = map func array + +-- och därefter använda den tillsammans med en anonym funktion för att +-- efterlikna en loop +for [0..5] $ \i -> show i + +-- men vi kunde även ha skrivit på följande vis: +for [0..5] show + +-- Du kan använda foldl eller foldr för att reducera en lista +-- foldl <fn> <initial value> <list> +foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43 + +-- Vilket är samma sak som +(2 * (2 * (2 * 4 + 1) + 2) + 3) + +-- foldl viker från vänster, foldr från höger +foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16 + +-- Vilket alltså är samma sak som +(2 * 1 + (2 * 2 + (2 * 3 + 4))) + +---------------------------------------------------- +-- 7. Datatyper +---------------------------------------------------- + +-- Såhär definierar du din egen datatyp i Haskell +data Color = Red | Blue | Green + +-- När du gjort det kan du använda den i funktionssignaturer och uttryck +say :: Color -> String +say Red = "Du är Rö!" +say Blue = "Du är Blå!" +say Green = "Du är Grön!" + +-- Dina datatyper kan även ta parametrar +data Maybe a = Nothing | Just a + +-- Följande uttryck är alla specialiseringar av typen Maybe +Just "hello" -- har typen `Maybe String` +Just 1 -- har typen `Maybe Int` +Nothing -- har typen `Maybe a` för alla `a` + +---------------------------------------------------- +-- 8. Haskell IO +---------------------------------------------------- + +-- Även om IO inte kan förstås fullt ut utan att först förklara monader är det +-- inte svårt att lära sig tillräckligt för att komma igång + +-- När ett Haskellprogram körs är det topnivåns main som körs. Main måste +-- returnerna ett värde av typen `IO a`, för någon typ `a`. Till exempel: + +main :: IO () +main = putStrLn $ "Hej, himmelen! " ++ (say Blue) +-- putStrLn har typen type String -> IO () + +-- Det är enkelt att göra IO om du kan implementera ditt program som en funktion +-- från String till String. Funktionen +-- interact :: (String -> String) -> IO () +-- tar denna funktion och matar den med strängdata från stdin och skriver ut +-- resultatet som en sträng på stdout + +countLines :: String -> String +countLines = show . length . lines + +main' = interact countLines + +-- Du kan tänka på värden av typen `IO ()` som att representera +-- händelsesekvenser du vill att din dator skall utföra, likt imperativa språk. +-- För att kedja ihop händelsesekvenser använder man ett syntaktiskt socker +-- kallat do-notation. Som exempel: + +sägHej :: IO () +sägHej = do + putStrLn "Vad heter du?" + namn <- getLine -- denna raden läser en rad från stdin och vi binder den till + -- funktionsnamnet `namn` + putStrLn $ "Hejsan, " ++ namn + +-- Övning: Skriv din egen version av interageringsfunktionen `interact` som bara +-- läser en rad från stdin, vanliga `interact` läser till EOF. + +-- Koden i sägHej kommer dock aldrig exekveras. Den enda handlingen som blir det +-- är som bekant utvärderingen av `main`. +-- För att köra `sägHej` kommentera ut definition av `main` ovan och +-- avkommentera nedanstående version: +-- main = sayHello + +-- Låt oss bättre förstå hur funktionen `getLine` vi just använde fungerar. Dess +-- typsignatur är: +-- getLine :: IO String +-- Du kan tänka på typen `IO a` som att representeras av ett datorprogram vilken +-- kommer generera ett värde av typen `a` när det exekveras (utöver allt annat +-- det kan tänkas göra). Vi kan därtill binda detta värde till ett namn för +-- återanvändning genom att använda `<-`. Vi kan även skapa våran egen handling +-- av typen `IO String`: + +handling :: IO String +handling = do + putStrLn "Detta är en rad, tihi" + input1 <- getLine + input2 <- getLine + -- Typen av hela `do` blocket är vad som står på sista raden. Här är även + -- `return` inte ett nyckelord i språket utan en funktion med en typsignatur + return (input1 ++ "\n" ++ input2) -- return :: String -> IO String + +-- Vi kan använda `return` på samma sätt som vi använde `getLine`: + +main'' = do + putStrLn "Jag kommer eka två rader!" + result <- handling + putStrLn result + putStrLn "Tack och hej leverpastej!" + +-- Typen `IO` är ett exempel på en monad. Sättet Haskell utnyttjar monader på är +-- anledningen till hur språket kan bibehålla sin renhet. En funktion vilken +-- interagerar med omvärlden (alltså gör IO) blir markerad med `IO` i sin +-- typsignatur. Detta låter oss enkelt upptäcka vilka funktioner som är "rena" +-- (inte interagerar med omvärlden eller är tillståndsoberoende) and vilka +-- funktioner som inte är det. + +-- Detta är ett mäktigt särdrag eftersom det är enkelt att köra rena funktioner +-- sammanlöpande; Samtidig programmering är enkel att göra i Haskell. + +---------------------------------------------------- +-- 9. Haskell REPL (kodtolk) +---------------------------------------------------- + +-- Efter installation av GHC kan vi starta tolken genom att skriva `ghci`. +-- Nu kan du mata in Haskellkod direkt i den. Nya värden måste introduceras med +-- `let` bindning: + +let foo = 5 + +-- Du kan även se typen av namnbindningen med `:t` + +> :t foo +foo :: Integer + +-- Operatorer, som `+`, `:` och `$` är funktioner. Deras typ kan inspekteras +-- genom att skriva operatorn mellan parenteser: + +> :t (:) +(:) :: a -> [a] -> [a] + +-- Du kan få ytterliggare information om något namn genom att använda `:i` + +> :i (+) +class Num a where + (+) :: a -> a -> a + ... + -- Defined in ‘GHC.Num’ +infixl 6 + + +-- Du kan även köra alla handlingar av typen `IO ()` direkt i tolken + +> sägHej +Vad är ditt namn? +Kompis! +Hello, Kompis! + +``` + +Det finns mycket mer att upptäcka med Haskell, inklusive typklasser och monader. +Vilka är de stora idéerna som gör Haskell till det roliga programmeringsspråket +det är. Jag lämar dig med ett sista exempel; En implementation av quicksort: + +```haskell +qsort [] = [] +qsort (p:xs) = qsort mindre ++ [p] ++ qsort större + where mindre = filter (< p) xs + större = filter (>= p) xs +``` + +Det finns två populära sätt att installera Haskell på: Den traditionella [Cabal sättet](http://www.haskell.org/platform/), eller det nyare [Stack sättet](https://www.stackage.org/install). + +Du kan finna vänligare och/eller djupare introduktioner till Haskell på engelska +från: +[Learn you a Haskell](http://learnyouahaskell.com/), +[Happy Learn Haskell Tutorial](http://www.happylearnhaskelltutorial.com/) eller +[Real World Haskell](http://book.realworldhaskell.org/). diff --git a/sv-se/nix-sv.html.markdown b/sv-se/nix-sv.html.markdown new file mode 100644 index 00000000..15d9456b --- /dev/null +++ b/sv-se/nix-sv.html.markdown @@ -0,0 +1,368 @@ +--- +language: nix +filename: learn-sv.nix +contributors: + - ["Chris Martin", "http://chris-martin.org/"] +translators: + - ["Edward Tjörnhammar", "http://edwtjo.me"] +lang: sv-se +--- + +Nix är ett enkelt funktionelt språk utvecklat för +[Nix pakethanteraren](https://nixos.org/nix/) och +[NixOS](https://nixos.org/) linuxdistributionen. + +Du kan utvärdera Nix uttryck genom att använda +[nix-instantiate](https://nixos.org/nix/manual/#sec-nix-instantiate) +eller [`nix-repl`](https://github.com/edolstra/nix-repl). + +``` +with builtins; [ + + # Kommentarer + #========================================= + + # Inlinekommentarer ser ut såhär. + + /* Flerradskommentarer ser ut + såhär. */ + + + # Booleaner + #========================================= + + (true && false) # Och + #=> false + + (true || false) # Eller + #=> true + + (if 3 < 4 then "a" else "b") # Villkorlig + #=> "a" + + + # Heltal + #========================================= + + # Heltal är den enda numeriska typen. + + 1 0 42 (-3) # Några heltal + + (4 + 6 + 12 - 2) # Addition + #=> 20 + + (7 / 2) # Division + #=> 3 + + + # Strängar + #========================================= + + "Stränglitteraler omgärdas av raka citationstecken." + + " + Stränglitteraler kan sträcka sig + över flera rader. + " + + '' + Detta kallas för en indenterad strängliteral, omgärdad av dubbla apostrofer + Den plockar intelligent bort ledande blanktecken. + '' + + '' + a + b + '' + #=> "a\n b" + + ("ab" + "cd") # Strängkonkatenering + #=> "abcd" + + # Antikvotering låter dig bädda in språkvärden i strängar. + ("Din hemkatalog är ${getEnv "HOME"}") + #=> "Din hemkatalog är /home/alice" + + + # Sökvägar + #========================================= + + # Nix har en primitiv, inbyggd, typ för sökvägar. + /tmp/tutorials/learn.nix + + # Relativa sökvägar förenas med sökvägen till dess definerande fils sökväg + # vid tolkningstillfället för att skapa dess absoluta sökväg. + + tutorials/learn.nix + #=> /the-base-path/tutorials/learn.nix + + # En sökväg måste innehålla åtminstonde ett snedstreck, så en relativ sökväg + # till en fil i samma katalog måste ges ett "./" prefix + + ./learn.nix + #=> /the-base-path/learn.nix + + # Divisionsoperatorn / måste omges av blanksteg om man vill att det skall + # tolkas som heltalsdivision + + 7/2 # Detta är en sökväg + (7 / 2) # Detta är heltalsdivision + + + # Importer + #========================================= + + # En nix fil innehåller ett enstaka topnivåuttryck utan fria variabler. + # Ett importuttryck evalueras till värdet på filen som den importerar. + (import /tmp/foo.nix) + + # Importer kan också specificeras med hjälp av strängar. + (import "/tmp/foo.nix") + + # Importsökvägar måste vara absoluta. Sökvägslitteraler härleds vid + # tolkningstillfället så följande är ok. + (import ./foo.nix) + + # Men detta är inte något som sker med strängar. + (import "./foo.nix") + #=> error: string ‘foo.nix’ doesn't represent an absolute path + + + # Let + #========================================= + + # `let` block tillåter oss att binda värden till namn. + (let x = "a"; in + x + x + x) + #=> "aaa" + + # Bindingar kan referera till varandra och deras ordning sinsemellan spelar + # ingen roll. + (let y = x + "b"; + x = "a"; in + y + "c") + #=> "abc" + + # Innre bindningar skuggar utanpåliggande bindingar. + (let a = 1; in + let a = 2; in + a) + #=> 2 + + + # Funktioner + #========================================= + + (n: n + 1) # En lambdafunktion som lägger till 1 + + ((n: n + 1) 5) # Samma funktion applicerad på 5 + #=> 6 + + # Det finns ingen syntax för direkt namngivna funktioner, istället binder man + # dessa med `let` block som andra värden. + (let succ = (n: n + 1); in succ 5) + #=> 6 + + # En funktion är en lambda med en parameter. Flera parameterar kan ges med + # hjälp av currying. + ((x: y: x + "-" + y) "a" "b") + #=> "a-b" + + # Vi kan också ha namngivna funktionsparametrar, vilket vi kommer komma till + # senare, efter att vi introducerat attributset. + + # Listor + #========================================= + + # Listor noteras med hakparenteser. + + (length [1 2 3 "x"]) + #=> 4 + + ([1 2 3] ++ [4 5]) + #=> [1 2 3 4 5] + + (concatLists [[1 2] [3 4] [5]]) + #=> [1 2 3 4 5] + + (head [1 2 3]) + #=> 1 + (tail [1 2 3]) + #=> [2 3] + + (elemAt ["a" "b" "c" "d"] 2) + #=> "c" + + (elem 2 [1 2 3]) + #=> true + (elem 5 [1 2 3]) + #=> false + + (filter (n: n < 3) [1 2 3 4]) + #=> [ 1 2 ] + + + # Mängder + #========================================= + + # Ett attributset är en oordnad mappning av strängnycklar och värden. + { foo = [1 2]; bar = "x"; } + + # Punktoperatorn . väljer ett värde från attributset:et + { a = 1; b = 2; }.a + #=> 1 + + # Frågeoperatorn ? testar om en nyckel är närvarande i ett attributset + ({ a = 1; b = 2; } ? a) + #=> true + ({ a = 1; b = 2; } ? c) + #=> false + + # Snedstrecksoperatorn // slår ihop två attributset:ar. + ({ a = 1; } // { b = 2; }) + #=> { a = 1; b = 2; } + + # Värden på höger skriver över värden till vänster. + ({ a = 1; b = 2; } // { a = 3; c = 4; }) + #=> { a = 3; b = 2; c = 4; } + + # Recursionsnyckelordet rec noterar ett rekursivt attributset (en fixpunkt) + # i vilket attributen kan referera till varandra. + (let a = 1; in { a = 2; b = a; }.b) + #=> 1 + (let a = 1; in rec { a = 2; b = a; }.b) + #=> 2 + + # Nästlade attributset:ar kan definieras bit för bit. + { + a.b = 1; + a.c.d = 2; + a.c.e = 3; + }.a.c + #=> { d = 2; e = 3; } + + # Ett attributsets barn kan inte tilldelas på detta vis om attributsetet + # självt blivit direkt tilldelat. + { + a = { b = 1; }; + a.c = 2; + } + #=> error: attribute ‘a’ already defined + + + # Bindningsintroduktion, `with` + #========================================= + + # Det attributset vilket återfinns i ett `with` uttryck kommer få sina + # värdebindningar introducerade i efterkommande uttryck. + (with { a = 1; b = 2; }; + a + b) + # => 3 + + # Innre bindningar skuggar yttre bindningar. + (with { a = 1; b = 2; }; + (with { a = 5; }; + a + b)) + #=> 7 + + # Första raden av detta exempel börjar med "with builtins;" eftersom builtins + # är ett attributset innehållande alla inbyggda hjälpfunktioner såsom + # (length, head, tail, filter, etc.). Detta sparar oss från att hela tiden + # referera in i det attributset:et , alltså du kan använda bara "length" + # istället för "builtins.length". + + + # Attributsetmönster + #========================================= + + # Attributset är användbara när vi skall skicka med flera värden till en + # funktion. + (args: args.x + "-" + args.y) { x = "a"; y = "b"; } + #=> "a-b" + + # Man kan använda attributsetmönster för ökad tydlighet. + ({x, y}: x + "-" + y) { x = "a"; y = "b"; } + #=> "a-b" + + # Attributmönster misslyckas dock om det medskickade attributmönstret + # innehåller extra nycklar. + ({x, y}: x + "-" + y) { x = "a"; y = "b"; z = "c"; } + #=> error: anonymous function called with unexpected argument ‘z’ + + # Genom att lägga till ", ..." kan vi ignorera ytterliggare nycklar. + ({x, y, ...}: x + "-" + y) { x = "a"; y = "b"; z = "c"; } + #=> "a-b" + + + # Felmeddelanden + #========================================= + + # `throw` gör att programtolken gör abort med dess tillhörande felmeddelande + causes evaluation to abort with an error message. + (2 + (throw "foo")) + #=> error: foo + + # `tryEval` fångar kastade fel `throw`. + (tryEval 42) + #=> { success = true; value = 42; } + (tryEval (2 + (throw "foo"))) + #=> { success = false; value = false; } + + # `abort` fungerar som `throw`, men är kritiskt och kan inte fångas. + (tryEval (abort "foo")) + #=> error: evaluation aborted with the following error message: ‘foo’ + + # `assert` utvärderas till det givna värdet om dess predikat är sant. + # annars skickar den ett fångbart fel. + (assert 1 < 2; 42) + #=> 42 + (assert 1 > 2; 42) + #=> error: assertion failed at (string):1:1 + (tryEval (assert 1 > 2; 42)) + #=> { success = false; value = false; } + + + # Orenhet + #========================================= + + # Eftersom repeterbarhet för byggen är en kritisk egenskap för + # Nix-pakethanteraren betonas funktionell renhet i Nix-programmeringsspråket. + # Men med det sagt existerar det källor till orenhet + + # Man kan referera till miljövariabler. + (getEnv "HOME") + #=> "/home/alice" + + # `trace` funktionen används för att debugga. Den skriver ut första argumentet + # till stderr och reduceras samtidigt till det andra argumentet. + (trace 1 2) + #=> trace: 1 + #=> 2 + + # Man kan skriva filer till Nix-store, lagringsplatsen för alla Nix-uttryck. + # Även om detta är orent beteende är det hyfsat säkert eftersom filens + # lagringsplats är härledd från dess innehåll och beroenden. Man kan läsa + # filer från precis överallt. I nedanstående exempel skriver vi en fil till + # Nix-store och sedan läser tillbaka den. + + (let filename = toFile "foo.txt" "hello!"; in + [filename (builtins.readFile filename)]) + #=> [ "/nix/store/ayh05aay2anx135prqp0cy34h891247x-foo.txt" "hello!" ] + + # Vi kan också ladda ned filer till Nix-store. + (fetchurl "https://example.com/package-1.2.3.tgz") + #=> "/nix/store/2drvlh8r57f19s9il42zg89rdr33m2rm-package-1.2.3.tgz" + +] +``` + +### Vidare Läsning (eng) + +* [Nix Manual - Nix expression language] + (https://nixos.org/nix/manual/#ch-expression-language) + +* [James Fisher - Nix by example - Part 1: The Nix expression language] + (https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55) + +* [Susan Potter - Nix Cookbook - Nix By Example] + (http://funops.co/nix-cookbook/nix-by-example/) diff --git a/swift.html.markdown b/swift.html.markdown index c17510b6..516debed 100644 --- a/swift.html.markdown +++ b/swift.html.markdown @@ -281,7 +281,7 @@ testGuard() // Variadic Args func setup(numbers: Int...) { - // its an array + // it's an array let _ = numbers[0] let _ = numbers.count } @@ -361,14 +361,14 @@ print("Name is \(name)") // Name is Them // The `Error` protocol is used when throwing errors to catch enum MyError: Error { - case BadValue(msg: String) - case ReallyBadValue(msg: String) + case badValue(msg: String) + case reallyBadValue(msg: String) } // functions marked with `throws` must be called using `try` func fakeFetch(value: Int) throws -> String { guard 7 == value else { - throw MyError.ReallyBadValue(msg: "Some really bad value") + throw MyError.reallyBadValue(msg: "Some really bad value") } return "test" @@ -385,7 +385,7 @@ func testTryStuff() { do { // normal try operation that provides error handling via `catch` block try fakeFetch(value: 1) - } catch MyError.BadValue(let msg) { + } catch MyError.badValue(let msg) { print("Error message: \(msg)") } catch { // must be exhaustive @@ -518,49 +518,49 @@ if let circle = myEmptyCircle { // They can contain methods like classes. enum Suit { - case Spades, Hearts, Diamonds, Clubs + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } // Enum values allow short hand syntax, no need to type the enum type // when the variable is explicitly declared -var suitValue: Suit = .Hearts +var suitValue: Suit = .hearts // String enums can have direct raw value assignments // or their raw values will be derived from the Enum field enum BookName: String { - case John - case Luke = "Luke" + case john + case luke = "Luke" } -print("Name: \(BookName.John.rawValue)") +print("Name: \(BookName.john.rawValue)") // Enum with associated Values enum Furniture { // Associate with Int - case Desk(height: Int) + case desk(height: Int) // Associate with String and Int - case Chair(String, Int) + case chair(String, Int) func description() -> String { switch self { - case .Desk(let height): + case .desk(let height): return "Desk with \(height) cm" - case .Chair(let brand, let height): + case .chair(let brand, let height): return "Chair of \(brand) with \(height) cm" } } } -var desk: Furniture = .Desk(height: 80) +var desk: Furniture = .desk(height: 80) print(desk.description()) // "Desk with 80 cm" -var chair = Furniture.Chair("Foo", 40) +var chair = Furniture.chair("Foo", 40) print(chair.description()) // "Chair of Foo with 40 cm" diff --git a/ta_in/json-ta.html.markdown b/ta_in/json-ta.html.markdown index d85e0d82..7f6559fa 100644 --- a/ta_in/json-ta.html.markdown +++ b/ta_in/json-ta.html.markdown @@ -1,6 +1,6 @@ --- language: json -filename: learnjson.json +filename: learnjson-ta.json contributors: - ["Anna Harren", "https://github.com/iirelu"] - ["Marco Scannadinari", "https://github.com/marcoms"] diff --git a/tcl.html.markdown b/tcl.html.markdown index 1f4ca63b..f48d5271 100644 --- a/tcl.html.markdown +++ b/tcl.html.markdown @@ -49,7 +49,7 @@ discipline of exposing all programmatic functionality as routines, including things like looping and mathematical operations that are usually baked into the syntax of other languages, allows it to fade into the background of whatever domain-specific functionality a project needs. Its syntax, which is even -lighter that that of Lisp, just gets out of the way. +lighter than that of Lisp, just gets out of the way. @@ -75,7 +75,7 @@ lighter that that of Lisp, just gets out of the way. ## 2. Syntax ############################################################################### -# A script is made up of commands delimited by newlines or semiclons. Each +# A script is made up of commands delimited by newlines or semicolons. Each # command is a call to a routine. The first word is the name of a routine to # call, and subsequent words are arguments to the routine. Words are delimited # by whitespace. Since each argument is a word in the command it is already a @@ -99,13 +99,13 @@ set greeting $part1$part2[set part3] # An embedded script may be composed of multiple commands, the last of which provides -# the result for the substtution: +# the result for the substitution: set greeting $greeting[ incr i incr i incr i ] -puts $greeting ;# The output is "Salutations3" +puts $greeting ;# The output is "Salutations3" # Every word in a command is a string, including the name of the routine, so # substitutions can be used on it as well. Given this variable @@ -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!\" +# " @@ -377,7 +378,7 @@ set amount [lindex $amounts 1] set inventory {"item 1" item\ 2 {item 3}} -# It's generally a better idea to use list routines when modifing lists: +# It's generally a better idea to use list routines when modifying lists: lappend inventory {item 1} {item 2} {item 3} @@ -422,8 +423,7 @@ eval {set name Neo} eval [list set greeting "Hello, $name"] -# Therefore, when using "eval", , use "list" to build -# up the desired command: +# Therefore, when using "eval", use "list" to build up the desired command: set command {set name} lappend command {Archibald Sorbisol} eval $command @@ -517,7 +517,7 @@ proc while {condition script} { # and then calls that routine. "yield" suspends evaluation in that stack and # returns control to the calling stack: proc countdown count { - # send something back to the creater of the coroutine, effectively pausing + # send something back to the creator of the coroutine, effectively pausing # this call stack for the time being. yield [info coroutine] diff --git a/tcsh.html.markdown b/tcsh.html.markdown new file mode 100644 index 00000000..80411ea6 --- /dev/null +++ b/tcsh.html.markdown @@ -0,0 +1,790 @@ +--- +language: tcsh +filename: LearnTCSH.csh +contributors: + - ["Nicholas Christopoulos", "https://github.com/nereusx"] + +--- +tcsh ("tee-see-shell") is a Unix shell based on and compatible with the C shell (csh). +It is essentially the C shell with programmable command-line completion, command-line editing, +and a few other features. +It is the native root shell for BSD-based systems such as FreeBSD. + +Almost all Linux distros and BSD today use tcsh instead of the original csh. In +most cases csh is a symbolic link that points to tcsh. +This is because tcsh is backward compatible with csh, and the last +is not maintained anymore. + +- [TCSH Home](http://www.tcsh.org/) +- [TCSH Wikipedia](https://en.wikipedia.org/wiki/Tcsh) +- [TCSH manual page](http://www.tcsh.org/tcsh.html/top.html) +- [“An Introduction to the C shell”, William Joy](https://docs.freebsd.org/44doc/usd/04.csh/paper.html) +- [TCSH Bug reports and/or features requests](https://bugs.gw.com/) + +Some more files: +[tcsh help command (for 132x35 terminal size)](https://github.com/nereusx/dotfiles/blob/master/csh-help), +[my ~/.tcshrc](https://github.com/nereusx/dotfiles/blob/master/.tcshrc) + +```tcsh +#!/bin/tcsh +# First line of the script is shebang which tells the system how to execute the +# script: http://en.wikipedia.org/wiki/Shebang_(Unix) +# TCSH emulates the shebang on systems which don't understand it. + +# In most cases you'll use `#!/bin/tcsh -f', because `-f' option does not load +# any resource or start-up files, or perform any command hashing, and thus +# starts faster. + +# --- the echo command -------------------------------------------------------- +# The `echo' writes each word to the shell's standard output, separated by +# spaces and terminated with a newline. The echo_style shell variable may be +# set to emulate (or not) the flags and escape sequences. + +# Display the value of echo_style +echo $echo_style + +# Enable `echo' to support backslashed characters and `-n' option (no new line) +# This is the default for tcsh, but your distro may change it. Slackware has +# done so. +set echo_style = both + +# Prints "Hello world" +echo Hello world +echo "Hello world" +echo 'Hello world' +echo `echo Hello world` + +# This prints "twonlines" in one line +echo two\nlines + +# Prints the two lines +echo "two\nlines" +echo 'two\nlines' + +# --- Basic Syntax ------------------------------------------------------------ + +# A special character (including a blank or tab) may be prevented from having +# its special meaning by preceding it with a backslash `\'. +# this will display the last history commands +echo !! +# this will not +echo \!\! + +# Single quotes prevents expanding special characters too, but some +# characters like `!' and backslash have higher priority +# `$' (variable value) will not expands +echo '$1 tip' +# `!' (history) will expands +echo '!!' + +# Strings enclosed by back-quotes will be executed and replaced by the result. +echo `ls` + +# Semi-colon separate commands +echo 'first line'; echo 'second line' + +# There is also conditional execution +echo "Always executed" || echo "Only executed if first command fails" +echo "Always executed" && echo "Only executed if first command does NOT fail" + +# Parenthesised commands are always executed in a subshell, + +# example: create a project and then informs you that it finished while +# it does the installation. +make && ( espeak "BOSS, compilation finished"; make install ) + +# prints the home directory but leaving you where you were +(cd; pwd); pwd + +# Read tcsh man-page documentation +man tcsh + +# --- Variables --------------------------------------------------------------- +# The shell maintains a list of variables, each of which has as value a list of +# zero or more words. The values of shell variables can be displayed and +# changed with the `set' and `unset' commands. +# The system maintains its own list of ``environment'' variables. +# These can be displayed and changed with `printenv', `setenv' and `unsetenv'. +# The syntax of setenv is similar to POSIX sh. + +# Assign a value or nothing will create a variable +# Assign nothing +set var +# Assign a numeric value +# the '@' denotes the expression is arithmetic; it works similar to 'set' but +# the right value can be a numeric expression. +@ var = 1 + 2 +# Assign a string value +set var = "Hello, I am the contents of 'var' variable" +# Assign the output of a program +set var = `ls` + +# Remove a variable +unset var +# Prints 1 (true) if the variable `var' exists otherwise prints 0 (false) +echo $?var +# Print all variables and their values +set + +# Prints the contents of 'var' +echo $var; +echo "$var"; +# Prints the string `$var' +echo \$var +echo '$var' +# braces can be used to separate variable from the rest when its needed +set num = 12; echo "There ${num}th element" + +# Prints the number of characters of the value: 6 +set var = '123456'; echo $%var + +### LISTs +# Assign a list of values +set var = ( one two three four five ) +# Print all the elements: one two three four five +echo $var +echo $var[*] +# Print the count of elements: 5 +echo $#var +# Print indexed element; prints the second element: two +echo $var[2] +# Print range of elements; prints 2nd up to 3rd: two, three +echo $var[2-3] +# Prints all elements starting from the 3rd: three four five +echo $var[3-] +# Prints print all up to 3rd element: one two three +echo $var[-3] + +### Special Variables +# $argv list of command-line arguments +# $argv[0] this file-name (the file of the script file) +# $# $0, $n, $* are the same as $#argv, $argv[0], $argv[n], $argv[*] +# $status, $? the exit code of the last command that executed +# $_ the previous command line +# $! the PID of the last background process started by this shell +# $$ script's PID + +# $path, $PATH the list of directories that will search for executable to run +# $home, $HOME user's home directory, also the `~' can be used instead +# $uid user's login ID +# $user user's login name +# $gid the user's group ID +# $group the user's group-name +# $cwd, $PWD the Current/Print Working Directory +# $owd the previous working directory +# $tcsh tcsh version +# $tty the current tty; ttyN for linux console, pts/N for terminal +# emulators under X +# $term the terminal type +# $verbose if set, causes the words of each command to be printed. +# can be set by the `-v' command line option too. +# $loginsh if set, it is a login shell + +# TIP: $?0 is always false in interactive shells +# TIP: $?prompt is always false in non-interactive shells +# TIP: if `$?tcsh' is unset; you run the original `csh' or something else; +# try `echo $shell' +# TIP: $verbose this is useful to debugging scripts +# NOTE: $PWD and $PATH are synchronised with $cwd and $pwd automatically. + +# --- Variable modifiers ------------------------------------------------------ +# Syntax: ${var}:m[:mN] +# Where <m> is: +# h : the directory t : the filenane r : remove extension e : the extension +# u : uppercase the first lowercase letter +# l : lowercase the first uppercase letter +# p : print but do not execute it (hist) +# q : quote the substituted words, preventing further substitutions +# x : like q, but break into words at white spaces +# g : apply the following modifier once to each word +# a : apply the following modifier as many times as possible to single word +# s/l/r/ : search for `l' and replace with `r', not regex; the `&' in the r is +# replaced by l +# & : Repeat the previous substitution + +# start with this file +set f = ~/Documents/Alpha/beta.txt +# prints ~/Documents/Alpha/beta +echo $f:r +# prints ~/Documents/Alpha +echo $f:h +# prints beta.txt +echo $f:t +# prints txt +echo $f:e +# prints beta +echo $f:t:r +# prints Beta +echo $f:t:r:u +# prints Biota +echo $f:t:r:u:s/eta/iota/ + +# --- Redirection ------------------------------------------------------------- + +# Create file.txt and write the standard output to it +echo 'this string' > file.txt +# Create file.txt and write the standard output and standard error to it +echo 'this string' >& file.txt +# Append the standard output to file.txt +echo 'this string' >> file.txt +# Append the standard output and standard error to file.txt +echo 'this string' >>& file.txt +# Redirect the standard input from file.txt +cat < file.txt +# Input from keyboard; this stores the input line to variable `x' +set x = $< +# Document here; +cat << LABEL +...text here... +LABEL + +# TIP: this is how to get standard error separated: +(grep 'AGP' /usr/src/linux/Documentation/* > output-file.txt) >& error-file.txt + +# example: read a name from standard input and display a greetings message +echo -n "Enter your name? " +set name = $< +echo "Greetings $name" + +# --- Expressions ------------------------------------------------------------ + +# Operators: +# == equal != not equal ! not +# > greater than < less than >= greater or equal <= less or equal +# && logical AND || logical OR + +if ( $name != $user ) then + echo "Your name isn't your username" +else + echo "Your name is your username" +endif + +# single-line form +if ( $name != $user ) echo "Your name isn't your username" + +# NOTE: if $name is empty, tcsh sees the above condition as: +# if ( != $user ) ... +# which is invalid syntax +# so the "safe" way to use potentially empty variables in tcsh is: +# if ( "$name" != $user ) ... +# which, when $name is empty, is seen by tcsh as: +# if ( "" != $user ) ... +# which works as expected + +# There is also conditional execution +echo "Always executed" || echo "Only executed if first command fails" +echo "Always executed" && echo "Only executed if first command does NOT fail" + +# To use && and || with if statements, you don't need multiple pairs of +# square brackets: +if ( "$name" == "Steve" && "$age" == 15 ) then + echo "This will run if $name is Steve AND $age is 15." +endif + +if ( "$name" == "Daniya" || "$name" == "Zach" ) then + echo "This will run if $name is Daniya OR Zach." +endif + +# String matching operators ( `=~' and `!~' ) +# The ‘==’ ‘!=’ ‘=~’ and ‘!~’ operators compare their arguments as strings; +# all others operate on numbers. The operators ‘=~’ and ‘!~’ are like ‘!=’ +# and ‘==’ except that the right hand side is a glob-pattern against which +# the left hand operand is matched. + +if ( $user =~ ni[ck]* ) echo "Greetings Mr. Nicholas." +if ( $user !~ ni[ck]* ) echo "Hey, get out of Nicholas PC." + +# Arithmetic expressions are denoted with the following format: +@ result = 10 + 5 +echo $result + +# Arithmetic Operators +# +, -, *, /, % +# +# Arithmetic Operators which must be parenthesised +# !, ~, |, &, ^, ~, <<, >>, +# Compare and logical operators +# +# All operators are same as in C. + +# It is non so well documented that numeric expressions require spaces +# in-between; Also, `@' has its own parser, it seems that work well when the +# expression is parenthesised otherwise the primary parser seems it is active. +# Parenthesis require spaces around, this is documented. + +# wrong +@ x = $y+1 +@ x = 0644 & 022; echo $x +@ x = (0644 & 022) +1; echo $x +@ x = (0644 & 022)+ 1; echo $x +@ x = ( ~077 ); echo $x + +# correct +@ x = $y + 1 +@ x = ( 0644 & 022 ) + 1; echo $x +@ x = ( ~ 077 ); echo $x +@ x = ( ~ 077 | 022 ); echo $x +@ x = ( ! 0 ); echo $x + +# C's operators ++ and -- are supported if there is not assignment +@ result ++ + +# None shell created to do mathematics; +# Except for the basic operations, use an external command with backslashes. +# +# I suggest the calc as the best option. +# (http://www.isthe.com/chongo/tech/comp/calc/) +# +# The standard Unix's bc as second option +# (https://www.gnu.org/software/bc/manual/html_mono/bc.html) +# +# The standard Unix's AWK as third option +# (https://www.gnu.org/software/gawk/manual/gawk.html) + +# You can also use `perl', `php' or even several BASICs, but prefer the +# above utilities for faster load-and-run results. + +# real example: (that I answer in StackExchange) +# REQ: x := 1001b OR 0110b + +# in `tcsh' expression (by using octal) +@ x = ( 011 | 06 ); echo $x + +# the same by using `calc' (and using binary as the original req) +set x = `calc '0b1001 | 0b110'`; echo $x + +# --- File Inquiry Operators -------------------------------------------------- +# NOTE: The builtin `filetest' command do the same thing. + +#### Boolean operators +# -r read access -w write access -x execute access -e existence +# -f plain file -d directory -l symbolic link -p named pipe +# -S socket file +# -o ownership -z zero size -s non-zero size +# -u SUID is set -g SGID is set -k sticky is set +# -b block device -c char device +# -t file (digit) is an open file descriptor for a terminal device + +# if the file `README' exists, displays a message +if ( -e README ) echo "I have already README file" + +# if the `less' program is installed, use this instead of `more' +if ( -e `where less` ) then + alias more 'less' +endif + +#### Non-boolean operators +# -Z returns the file size in bytes +# -M returns the modification time (mtime) -M: returns mtime string +# -A returns the lass access time (atime) -A: returns atime string +# -U returns the owners user ID -U: returns the owners user-name +# -G returns the group ID -G: returns the group-name +# -P returns the permissions as octal number -Pmode returns perm. AND mode + +# this will display the date as Unix-time integer: 1498511486 +filetest -M README.md + +# This will display "Tue Jun 27 00:11:26 2017" +filetest -M: README.md + +# --- Basic Commands ---------------------------------------------------------- + +# Navigate though file system with `chdir' (cd) +cd path # change working directory +cd # change to home directory +cd - # change to previous directory +cd .. # go up one directory + +# Examples: +cd ~/Downloads # go to my `Downloads' directory + +# Use `mkdir` to create new directories. +mkdir newdir +# The `-p` flag causes new intermediate directories to be created as necessary. +mkdir -p ~/.backup/saves + +# which & where +# find if csh points to tcsh +ls -lha `which csh` +# find if csh is installed on more than one directory +where csh + +# --- Pipe-lines -------------------------------------------------------------- +# A pipeline is a sequence of processes chained together by their standard +# streams, so that the output of each process (stdout) feeds directly as input +# (stdin) to the next one. This `pipes' are created with the `|' special +# character and it is one of the most powerful characteristics of Unix. + +# example: +ls -l | grep key | less +# "ls -l" produces a process, the output (stdout) of which is piped to the +# input (stdin) of the process for "grep key"; and likewise for the process +# for "less". + +# the `ls', the `grep' and the `less' are programs of Unix and they have their +# own man-page. The `pipe' mechanism is part of the kernel but the syntax +# and the control is job of the shell, the tcsh in our case. + +# NOTE: `pipe' mechanism has Windows too, but it is buggy and I sign it for all +# versions until Windows XP SP3 API32 which was the last one that I worked on. +# Microsoft still denied it but is well known bug since it is a common method +# for inter-process communication. For small I/O it will work well. +# tcsh, along with grep, gcc and perl is one of the first Unix programs that +# ported to DOS (with EMX DOS extender) and later to Windows (1998). + +# example: this will convert tcsh to PostScript and will show it with okular +zcat /usr/man/man1/tcsh.1.gz | groff -Tps -man | okular - + +# a better version +zcat `locate -b -n 1 '\tcsh.1.gz'` | groff -Tps -man | okular - + +# even better +set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz"); + zcat `eval $loc` | groff -Tps -man | okular - + +# the same, modified to create man page pdf +set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz"); + zcat `eval $loc` | groff -Tps -man | ps2pdf - ${page}.pdf + +# the same, but now shows the ${page}.pdf too +set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz"); + zcat `eval $loc` | groff -Tps -man | ps2pdf - ${page}.pdf && okular tcsh.pdf + +# NOTE: `okular' is the default application of KDE environment and it shows +# postcript and pdf files. You can replace it with your lovely pdf viewer. +# zcat, locate, groff, are common programs in all Unices. `ps2pdf' program +# is part of `ghostscript' package that is widely used. + +# --- Control Flow ------------------------------------------------------------ + +#### IF-THEN-ELSE-ENDIF +# Syntax: +# if ( expr ) then +# ... +# [else if ( expr2 ) then +# ...] +# [else +# ...] +# endif +# +# If the specified expr is true then the commands to the first else are +# executed; otherwise if expr2 is true then the commands to the second else +# are executed, etc. +# Any number of else-if pairs are possible; only one endif is needed. +# +# Single-line form: +# +# if ( expr ) command +# +# If `expr' evaluates true, then command is executed. +# `command' must be a simple command, not an alias, a pipeline, a command list +# or a parenthesized command list. With few words, avoid to use it. +# +# BUG: Input/output redirection occurs even if expr is false and command is +# thus not executed. +# + +# check if we are in non-interactive shell and quit if true +if ( $?USER == 0 || $?prompt == 0 ) exit + +# check if we are a login shell +if ( $?loginsh ) then + # check if you are on linux console (not X's terminal) + if ( $tty =~ tty* ) then + # enable keypad application keys (man console_codes) + echo '\033=' + endif +endif + +#### SWITCH-ENDSW +# Syntax: +# switch ( expr ) +# case pattern: +# ... +# [breaksw] +# [default: +# ...] +# endsw +# +# tcsh uses a case statement that works similarly to switch in C. +# Each case label is successively matched, against the specified string which +# is first command and filename expanded. The file metacharacters `*', `?' +# and `[...]' may be used in the case labels. If none of the labels match the +# execution begins after the default label if its defined. +# The command `breaksw' causes execution to continue after the endsw. Otherwise +# control may fall through case labels and default labels as in C. + +switch ( $var ) +case *.[1-9]: +case *.[1-9].gz: + echo "$var is a man-page." + breaksw +case *gz: + echo "$var is gzipped" + breaksw +default: + file $var +endsw + +#### FOREACH-END +# Syntax: +# foreach name ( wordlist ) +# ... +# [break | continue] +# end +# +# Successively sets the variable `name' to each member of `wordlist' and +# executes the sequence of commands between this command and the matching +# `end' keyword. The `continue' keyword jump to the next element back to +# top; and the `break' keyword terminates the loop. +# +# BUG: `foreach' doesn't ignore here documents when looking for its end. + +# example: counting 1 to 10 +foreach i ( `seq 1 10` ) + echo $i +end + +# example: type all files in the list +foreach f ( a.txt b.txt c.txt ) + cat $f +end + +# example: convert wma to ogg +foreach f ( *.wma ) + ffmpeg -i "$f" "$f:r".ogg +end + +#### WHILE-END +# while ( expr ) +# ... +# [break | continue] +# end +# +# Executes the commands between the `while' and the matching `end' while `expr' +# evaluates non-zero. `break' and `continue' may be used to terminate or +# continue the loop prematurely. + +# count from 1 to 10 +set num = 1 +while ( $num <= 10 ) + echo $num + @ num ++ +end + +# print all directories of CWD +set lst = ( * ) +while ( $#lst ) + if ( -d $lst[1] ) echo $lst[1] is directory + shift lst +end + +# separate command-line arguments to options or parameters +set options +set params +set lst = ( $* ) +while ( $#lst ) + if ( "$lst[1]" =~ '-*' ) then + set options = ( $options $lst[1] ) + else + set params = ( $params $lst[1] ) + endif + shift lst +end +echo 'options =' $options +echo 'parameters =' $params + +#### REPEAT +# Syntax: repeat count command +# +# The specified command, which is subject to the same restrictions as the +# command in the one line if statement above, is executed count times. +# I/O redirections occur exactly once, even if count is 0. +# +# TIP: in most cases prefer `while' + +repeat 3 echo "ding dong" + +# --- Functions --------------------------------------------------------------- +# tcsh has no functions but its expression syntax is advanced enough to use +# `alias' as functions. Another method is recursion + +# Alias argument selectors; the ability to define an alias to take arguments +# supplied to it and apply them to the commands that it refers to. +# Tcsh is the only shell that provides this feature. +# +# \!# argument selector for all arguments, including the alias/command +# itself; arguments need not be supplied. +# \!* argument selector for all arguments, excluding the alias/command; +# arguments need not be supplied. +# \!$ argument selector for the last argument; argument need not be supplied, +# but if none is supplied, the alias name is considered to be the +# last argument. +# \!^ argument selector for first argument; argument MUST be supplied. +# \!:n argument selector for the nth argument; argument MUST be supplied; +# n=0 refers to the alias/command name. +# \!:m-n argument selector for the arguments from the mth to the nth; +# arguments MUST be supplied. +# \!:n-$ argument selector for the arguments from the nth to the last; +# at least argument n MUST be supplied. + +# Alias the cd command so that when you change directories, the contents +# are immediately displayed. +alias cd 'cd \!* && ls' + +# --- Recursion method --- begin --- +#!/bin/tcsh -f +set todo = option1 +if ( $#argv > 0 ) then + set todo = $argv[1] +endif + +switch ( $todo ) +case option1: +# ... + $0 results + breaksw +case option2: +# ... + $0 results + breaksw +case results: + echo "print the results here" +# ... + breaksw +default: + echo "Unknown option: $todo" +# exit 0 +endsw +# --- Recursion method --- end --- + +# --- examples ---------------------------------------------------------------- + +# this script prints available power-states if no argument is set; +# otherwise it set the state of the $argv[1] +# --- power-state script --- begin -------------------------------------------- +#!/bin/tcsh -f +# get parameter ("help" for none) +set todo = help +if ( $#argv > 0 ) then + set todo = $argv[1] +endif +# available options +set opts = `cat /sys/power/state` +# is known? +foreach o ( $opts ) + if ( $todo == $o ) then + # found; execute it + echo -n $todo > /sys/power/state + break + endif +end +# print help and exit +echo "usage: $0 [option]" +echo "available options on kernel: $opts" +# --- power-state script --- end ---------------------------------------------- + +# Guess the secret number game +# --- secretnum.csh --- begin ------------------------------------------------- +#!/bin/tcsh -f +set secret=`shuf -i1-100 -n1` +echo "I have a secret number from 1 up to 100" +while ( 1 ) + echo -n "Guess: " + set guess = $< + if ( $secret == $guess ) then + echo "You found it" + exit 1 + else + if ( $secret > $guess ) then + echo "its greater" + else if ( $secret < $guess ) then + echo "its lesser" + endif + endif + endif +end +# --- secretnum.csh --- end --------------------------------------------------- + +# ----------------------------------------------------------------------------- +# Appendices + +#### About [T]CSH: +# * CSH is notorious about its bugs; +# * It was also famous about its advanced interactive mode. +# * TCSH is famous that have the most advanced completition subsystem. +# * TCSH is famous that have the most advanced aliases subsystem; aliases +# can take parameters and often used as functions! +# * TCSH is well known that preferred by people (me too) because of better +# syntax. All shells are using Thomson's syntax with exception of [t]csh, +# fish and plan9's shells (rc, ex). +# * It is smaller and consume far less memory than bash, zsh even mksh! +# (memusage reports) +# * TCSH still has bugs; less but have; if you write readable clean code you'll +# find none; well almost none... This has to do with the implementation of +# csh; that no means the other shells has good implementation. +# * no one well known shell is capable for regular programming; if your script +# getting big, use a programming language, or at least PHP or Perl (good +# script languages). +# +# Advises: +# 1. Do not use redirection in single-line if (it is well documented bug) +# In most cases avoid to use single-line IFs. +# 2. Do not mess up with other shells code, c-shell is not compatible with +# other shells and has different abilities and priorities. +# 3. Use spaces as you'll use them to write readable code in any language. +# A bug of csh was `set x=1' worked, `set x = 1' worked, `set x =1' did not! +# 4. It is well documented that numeric expressions require spaces in-between; +# also parenthesise all bit-wise and unary operators. +# 5. Do not write a huge weird expression with several quotes, backslashes etc +# It is bad practice for generic programming, it is dangerous in any shell. +# 6. Help tcsh, report the bug here <https://bugs.gw.com/> +# 7. Read the man page, `tcsh' has huge number of options, and variables. +# +# I suggest the following options enabled by default +# -------------------------------------------------- +# Even in non-interactive shells +# set echo_style=both +# set backslash_quote +# set parseoctal +# unset noclobber +# +# Whatever... +# set inputmode=insert +# set autolist +# set listjobs +# set padhour +# set color +# set colorcat +# set nobeep +# set cdtohome +# +# set histdup +# set histlit +# set nohistclop +# +# unset compat_expr +# unset noglob +# unset autologout +# unset time +# unset tperiod +# +# NOTE: If the `backslash_quote' is set, it may create compatibility issues +# with other tcsh scripts which was written without it. +# +# NOTE: The same for `parseoctal', but it is better to fix the problematic +# scripts. +# +# NOTE: **for beginners only** +# This enable automatically rescan `path' directories if need to. (like bash) +# set autorehash + +#### common aliases +# alias hist 'history 20' +# alias ll 'ls --color -lha' +# alias today "date '+%d%h%y' +# alias ff 'find . -name ' + +#### a nice prompt +# set prompt = "%B%{\033[35m%}%t %{\033[32m%}%n@%m%b %C4 %# " +``` 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/c-tr.html.markdown b/tr-tr/c-tr.html.markdown index 33544765..6042a609 100644 --- a/tr-tr/c-tr.html.markdown +++ b/tr-tr/c-tr.html.markdown @@ -481,7 +481,7 @@ Diğer bir iyi kaynak ise [Learn C the hard way](http://c.learncodethehardway.or It's very important to use proper spacing, indentation and to be consistent with your coding style in general. Readable code is better than clever code and fast code. For a good, sane coding style to adopt, see the -[Linux kernel coding stlye](https://www.kernel.org/doc/Documentation/CodingStyle). +[Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle). Diğer taraftan google sizin için bir arkadaş olabilir. diff --git a/tr-tr/css-tr.html.markdown b/tr-tr/css-tr.html.markdown new file mode 100644 index 00000000..d13dadcc --- /dev/null +++ b/tr-tr/css-tr.html.markdown @@ -0,0 +1,304 @@ +--- +language: css +contributors: + - ["Mohammad Valipour", "https://github.com/mvalipour"] + - ["Marco Scannadinari", "https://github.com/marcoms"] + - ["Geoffrey Liu", "https://github.com/g-liu"] + - ["Connor Shea", "https://github.com/connorshea"] + - ["Deepanshu Utkarsh", "https://github.com/duci9y"] + - ["Brett Taylor", "https://github.com/glutnix"] + - ["Tyler Mumford", "https://tylermumford.com"] +filename: learncss-tr.css +translators: + - ["Fatih Turan", "http://fatihturan.com"] +lang: tr-tr +--- + +Web sayfaları bir sayfanın içeriğini belirleyen HTML ile inşa edilirler. CSS (Basamaklı Biçim Sayfaları) ise bir sayfanın **görünümünü** belirleyen ayrı bir dildir. + +CSS kodu statik *kurallardan* oluşur. Her kural bir ya da daha fazla *seçici* alır ve görsel *özelliklere* belirli *değerleri* verir. Sonrasında bu özellikler seçiciler tarafından belirlenen sayfa unsurlarına uygulanır. + +Bu rehber, CSS 3'ün yeni özellikleri ile genişletilen CSS 2 ile dikkate alınarak yazılmıştır. + +**NOT:** CSS görsel sonuçlar ürettiğinden dolayı, öğrenmek için herşeyi bir CSS oyun alanı içinde ([dabblet](http://dabblet.com) gibi) denemeniz gerekmektedir. Bu makale sözdizimi kuralları ve genel ipuçları üzerine odaklanmaktadır. + +## Sözdizimi + +```css +/* yorumlar bu satırdaki gibi taksim-yıldız içinde görünür +CSS'te "tek satırlık yorumlar" bulunmamaktadır; bu sadece tek bir yorum yazma stilidir */ + +/* #################### + ## SEÇİCİLER + #################### */ + +/* seçici bir sayfadaki unsuru hedeflemek için kullanılır. */ +seçici { özellik: değer; /* daha fazla özellikler...*/ } + +/* +İşte bir örnek: + +<div class='class1 class2' id='anID' attr='value' otherAttr='en-us foo bar' /> +*/ + +/* */ + +/* CSS sınıflarının birini kullanarak hedefleyebilirsiniz */ +.class1 { } + +/* veya her iki sınıfı birden!*/ +.class1.class2 { } + +/* veya sadece ögenin adını yazarak */ +div { } + +/* veya onun ID adını */ +#anID { } + +/* veya onun aldığı bir özelliği kullanarak! */ +[attr] { font-size:smaller; } + +/* veya onun aldığı özelliğin belirli bir değeri varsa */ +[attr='value'] { font-size:smaller; } + +/* bir değer ile başlıyorsa (CSS 3) */ +[attr^='val'] { font-size:smaller; } + +/* veya bir değer ile bitiyorsa (CSS 3)*/ +[attr$='ue'] { font-size:smaller; } + +/* veya boşlukla ayrılmış liste içinde bir değer içeriyorsa */ +[otherAttr~='foo'] { } +[otherAttr~='bar'] { } + +/* veya tire ile ayrılmış bir liste içinde bir değer içeriyorsa, örneğin: "-" (U+002D) */ +[otherAttr|='en'] { font-size:smaller; } + +/* Farklı seçicileri birleştirerek daha fazla odaklanmış bir seçici oluşturabilirsiniz. Seçiciler arasında boşluk bırakmayın. */ +div.some-class[attr$='ue'] { } + +/* Başka bir ögenin alt ögesi olan bir ögeyi seçebilirsiniz. */ +div.some-parent > .class-name { } + +/* veya bir başka ögeden türeyeni seçebilirsiniz. Alt ögeler onların ebeveynlerinin direkt türünden gelir, sadece ağacın bir alt ögeleridirler. Soyundan gelenler ağacın herhangi bir alt seviyesinde olabilir. */ + +div.some-parent .class-name { } + +/* Uyarı: Seçiciler arasında bir boşluk bırakmazsanız aynı seçicinin başka bir anlamı olur. +Ne olduğunu tahmin edebilir misiniz? */ + +div.some-parent.class-name { } + +/* Ayrıca bir ögenin bitişik kardeşini temel alarak bir ögeyi seçebilirsiniz. */ +.i-am-just-before + .this-element { } + +/* veya kendisinden önce gelen herhangi bir kardeş ögeyi */ +.i-am-any-element-before ~ .this-element { } + +/* Yalnızca belli bir durumda bir öge seçmek için kullanılan sahte sınıflar adı verilen bazı seçiciler vardır. */ + +/* Örneğin, imleç bir ögenin üzerine geldiğinde */ +selector:hover { } + +/* veya bir bağlantı ziyaret edildiğinde */ +selector:visited { } + +/* veya ziyaret edilmediğinde */ +selected:link { } + +/* veya bir ögeye odaklanıldığında */ +selected:focus { } + +/* Ebeveyninin ilk alt ögesi olan herhangi bir öge */ +selector:first-child {} + +/* Ebeveyninin son alt ögesi olan herhangi bir öge */ +selector:last-child {} + +/* Sahte sınıflar gibi sahte elementler de bir dokümanın belirli bir parçasına stil vermenize izin verir. */ + +/* Seçilen ögenin sanal ilk alt ögesiyle eşleşir. */ +selector::before {} + +/* Seçilen ögenin sanal son alt ögesiyle eşleşir. */ +selector::after {} + +/* Uygun yerlerde yıldız karakteri ile bütün ögeleri seçmek için joker olarak kullanılabilir. */ + +* { } /* Bütün ögeler */ +.parent * { } /* Tüm alt ögeler */ +.parent > * { } /* Tüm çocuk ögeler */ + +/* #################### + ## ÖZELLİKLER + #################### */ + +selector { + + /* Ölçü birimleri kesin veya göreceli olabilir.*/ + + /* Göreceli birimler */ + width: 50%; /* Ebeveyn elementin yüzdesel olarak genişliği */ + font-size: 2em; /* Öğenin özgün yazı tipi boyutunda katları */ + font-size: 2rem; /* veya kök ögenin yazı tipi boyutu */ + font-size: 2vw; /* Görüntüleme çerçevesinin genişliğinin %1 olarak katları (CSS 3) */ + font-size: 2vh; /* veya onun yüksekliğinin */ + font-size: 2vmin; /* Bir vh veya vw'nin hangisi küçükse */ + font-size: 2vmax; /* veya daha büyük... */ + + /* Kesin birimler */ + width: 200px; /* Piksel */ + font-size: 20pt; /* Nokta */ + width: 5cm; /* Santimetre */ + min-width: 50mm; /* Milimetre */ + max-width: 5in; /* İnç */ + + /* Renkler */ + color: #F6E; /* Kısa onaltılık (HEX) biçimi */ + color: #FF66EE; /* Uzun onaltılık (HEX) biçimi */ + color: tomato; /* Bir isim verilen renk */ + color: rgb(255, 255, 255); /* RGB değerleri verilen türde */ + color: rgb(10%, 20%, 50%); /* RGB yüzdeleri verilen türde */ + color: rgba(255, 0, 0, 0.3); /* RGBA değerleri verilen türde (CSS 3) Not: 0 <= a <= 1 */ + color: transparent; /* Şeffaflık değerinin sıfır olması ile eşdeğer */ + color: hsl(0, 100%, 50%); /* HSL yüzdeleri verilen türde (CSS 3) */ + color: hsla(0, 100%, 50%, 0.3); /* HSL ile beraber şeffaflık değeri verilen türde */ + + /* Kenarlıklar */ + border-width:5px; + border-style:solid; + border-color:red; /* background-color'ın ayarlanışına benzer şekilde */ + border: 5px solid red; /* Bu aynı şeyin kısayol ile yazılışıdır */ + border-radius:20px; /* Bu bir CSS3 özelliğidir */ + + /* Görseller ve Ögelerin Arkaplanları */ + background-image: url(/img-path/img.jpg); /* url() içindeki tırnak işaretleri isteğe bağlı */ + + /* Yazı tipleri */ + font-family: Arial; + /* Eğer yazı tipi ailesi isminde bir boşluk var ise tırnak işareti içine alınmalıdır. */ + font-family: "Courier New"; + /* Eğer ilk sıradaki bulunamazsa, tarayıcı bir sonrakini kullanır */ + font-family: "Courier New", Trebuchet, Arial, sans-serif; +} +``` + +## Kullanım + +CSS dosyasınızı `.css`uzantısı ile kaydedin. + +```html +<!-- CSS dosyanızı sayfanın içindeki <head> alanına dahil etmeniz gerekiyor. Bu önerilen yöntemdir. Bakın: http://stackoverflow.com/questions/8284365 --> +<link rel='stylesheet' type='text/css' href='path/to/style.css'> + +<!-- Bazı CSS kodları satır içi olarak yazabilirsiniz. --> +<style> + a { color: purple; } +</style> + +<!-- Veya ögenin üzerinde CSS özelliklerini direkt ayarlayabilirsiniz. --> +<div style="border: 1px solid red;"> +</div> +``` + +## Öncelik veya Basamak + +Bir öge birden çok seçici tarafından hedef alınabilir ve bir özellik kümesine birden fazla kez sahip olabilir. Bunun gibi durumlarda, kurallardan biri diğerlerine göre önceliklidir. Daha spesifik bir seçiciye sahip kurallar, daha az spesifik bir seçicinin önceliğini alır ve kural daha sonra stil sayfasında bir önceki kuralın üzerine yazar. + +Bu işleme geçiş denir ve olayısıyla Geçişli/Basamaklı Stil Sayfaları adı da buradan gelmiştir. + +Aşağıdaki CSS göz önüne alındığında: + +```css +/* A */ +p.class1[attr='değer'] + +/* B */ +p.class1 { } + +/* C */ +p.class2 { } + +/* D */ +p { } + +/* E */ +p { özellik: değer !important; } +``` + +ve aşağıdaki biçimlendirmeyi: + +```html +<p style='/*F*/ özellik:değer;' class='class1 class2' attr='değer'> +``` + +Stilin önceliği ise aşağıdaki gibidir. Unutmayın, öncelik **her bir özellik için ayrı ayrı geçerlidir**, tüm blok için geçerli değildir. + +* `E` `!important` kelimesi yüzünden en yüksek önceliğe sahiptir. Kullanımından kaçınmanız önerilir. +* `F` satıriçi stil olduğu için bir sonraki önceliğe sahiptir. +* `A` bir sonraki önceliğe sahiptir. Çünkü her şeyden daha "özgüdür". 3 belirteci vardır: `p` ögesinin adı, sınıf` class1`, bir öznitelik `attr = 'değer'. +* `C`, `B` ile aynı özdeşliğe sahip olsa da, bundan sonra geldiğinden dolayı öncelik hakkına sahiptir. +* `B` bir sonraki önceliğe sahiptir. +* Sonuncu önceliğe sahip olan`D`'dir. + +## Medya Sorguları + +CSS Medya Sorguları, CSS 3'te belirli CSS kurallarının ne zaman uygulanması gerektiğini (örneğin basılan zaman veya belirli boyutlar veya piksel yoğunluğu olan bir ekranda olduğunda) belirlemenize izin veren bir özelliktir. Medya Sorguları, seçicilere önceliğk eklemez. + +```css +/* Tüm cihazlarda kullanılacak olan bir kural */ +h1 { + font-size: 2em; + color: white; + background-color: black; +} + +/* h1 ögesini değiştirip bir yazıcıda daha az mürekkep kullanın*/ +@media print { + h1 { + color: black; + background-color: white; + } +} + +/* En az 480 piksel genişliğinde bir ekran gösterildiğinde font yüksekliğini daha büyük yap */ +@media screen and (min-width: 480px) { + h1 { + font-size: 3em; + font-weight: normal; + } +} +``` + +Medya sorguları aşağıdaki bu özellikleri içerebilir: +`width`, `height`, `device-width`, `device-height`, `orientation`, `aspect-ratio`, `device-aspect-ratio`, `color`, `color-index`, `monochrome`, `resolution`, `scan`, `grid`. Bu özelliklerin birçoğunu `min-` veya `max-` öneki ile kullanabilirsiniz. + +`resolution` özelliği eski cihazlarda desteklenmediğinden ötürü `device-pixel-ratio` kullanın. + +Eğer `viewport` meta etiketi sağlanmadıkça birçok akıllı telefon ve tabletler, sayfayı masaüstü bilgisayardaymış gibi göstermeye çalışacaktır. + +```html +<head> + <meta name="viewport" content="width=device-width; initial-scale=1.0"> +</head> +``` + +## Uyumluluk + +CSS 2'deki çoğu özellik (ve CSS 3'deki birçoğu) bütün tarayıcılar ve cihazlar için bulunmaktadır. Ancak yeni bir özelliği kullanmadan önce kontrol etmek her zaman iyi bir uygulamadır. + +## Kaynaklar + +* [CanIUse](http://caniuse.com) (Detaylı uyumluluk bilgileri) +* [Dabblet](http://dabblet.com/) (CSS oyun alanı) +* [Mozilla Geliştirici Ağının CSS belgelendirmesi](https://developer.mozilla.org/en-US/docs/Web/CSS) (Eğitseller ve referanslar) +* [Codrops' CSS Referansı](http://tympanus.net/codrops/css_reference/) (Referans) + +## Daha Fazla Okuma + +* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/) +* [Selecting elements using attributes](https://css-tricks.com/almanac/selectors/a/attribute/) +* [QuirksMode CSS](http://www.quirksmode.org/css/) +* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context) +* [SASS](http://sass-lang.com/) and [LESS](http://lesscss.org/) for CSS pre-processing +* [CSS-Tricks](https://css-tricks.com) diff --git a/tr-tr/dynamic-programming-tr.html.markdown b/tr-tr/dynamic-programming-tr.html.markdown index 1900ee93..606ecf04 100644 --- a/tr-tr/dynamic-programming-tr.html.markdown +++ b/tr-tr/dynamic-programming-tr.html.markdown @@ -1,5 +1,6 @@ --- language: Dynamic Programming +filename: dynamic-tr.txt contributors: - ["Akashdeep Goel", "https://github.com/akashdeepgoel"] translators: @@ -27,6 +28,8 @@ En Uzun Artan Subsequence problemi belirli bir dizinin en uzun artan alt dizini En uzun artan alt dizinin uzunluğunu bulmak için sözde kod: Bu algoritmaların karmaşıklığı dizi yerine daha iyi veri yapısı kullanılarak azaltılabilir. Büyük dizin ve dizin gibi selefi dizi ve değişkeni saklama çok zaman kazandıracaktır. Yönlendirilmiş asiklik grafiğinde en uzun yolu bulmak için benzer bir kavram uygulanabilir. + +```python for i=0 to n-1 LS[i]=1 for j=0 to i-1 @@ -35,6 +38,8 @@ for i=0 to n-1 for i=0 to n-1 if (largest < LS[i]) +``` + Bazı Ünlü Dinamik Programlama Problemleri -Floyd Warshall Algorithm - 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 -Integer Knapsack Problem - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming—the-integer-knapsack-problem 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/kotlin-tr.html.markdown b/tr-tr/kotlin-tr.html.markdown new file mode 100644 index 00000000..4d2dd291 --- /dev/null +++ b/tr-tr/kotlin-tr.html.markdown @@ -0,0 +1,474 @@ +--- +language: kotlin +filename: kotlin-tr.kt +contributors: + - ["Baha Can Aydın", "https://github.com/bahacan19"] +lang: tr-tr +--- +Kotlin, JVM, Android ve tarayıcı için statik olarak yazılmış bir programlama dilidir. +Java %100 birlikte çalışabilir. +[Daha:](https://kotlinlang.org/) + +```kotlin + +// Tek satır yoruma almak için : // +/* + Birkaç satırı yoruma almak için +*/ + +// "package" anahtar kelimesi tıpkı Java'da olduğu gibidir. +package com.learnxinyminutes.kotlin + +/* +Bir Kotlin programının başlama noktası (Java'da olduğu gibi) "com.learnxinyminutes.kotlin.main" metodudur. +Bu metoda komut satırından bir 'Array' gönderilebilir. +*/ +fun main(args: Array<String>) { + /* + Bir değer tanımlamak için "var" ya da "val" anahtar kelimeleri kullanılıyor. + "val" tanımlananlar tekrar atanamazken "var" tanımlananlar atanabilir. + */ + val fooVal = 10 // fooVal değerini daha sonra tekrar atayamıyoruz + var fooVar = 10 + fooVar = 20 // fooVar tekrar atanabilir. + + /* + Çoğu zaman, Kotlin bir değişkenin tipini anlayabilir, + bu yüzden her zaman belirtmeye gerek yoktur. + Bir değişkenin tipini şöyle belirtebiliriz: + */ + val foo: Int = 7 + + /* + String değerler Java'da olduğu gibi tanımlanır. + */ + val fooString = "İşte String bu!" + val barString = "Yeni satıra geçiyorum...?\nGeçtim!" + val bazString = "Tab mı istedin?\tAl bakalım!" + println(fooString) + println(barString) + println(bazString) + + /* + Raw string, üçlü çift-tırnak sınırlandırılan String bloklarıdır. + Tıpkı bir text editör gibi String tanımlamaya izin verir. + */ + val fooRawString = """ +fun helloWorld(val name : String) { + println("Merhaba, dünya!") +} +""" + println(fooRawString) + + /* + String değerler, ($) işareti ile birtakım deyimler ve değerler içererbilir + */ + val fooTemplateString = "$fooString değerinin ${fooString.length} adet karakteri vardır." + println(fooTemplateString) + + /* + Null atanabilen bir değişken nullable olarak tanımlanmalıdır. + Bu, deişken tipinin sonuna ? eklenerek yapılabilir. + Erişim ise '?.' operatörü ile yapılır. + Bir değişken null ise, yerine kullaılacak alternatif bir değer belirtmek için + '?:' operatörünü kullanırız. + */ + var fooNullable: String? = "abc" + println(fooNullable?.length) // => 3 + println(fooNullable?.length ?: -1) // => 3 + fooNullable = null + println(fooNullable?.length) // => null + println(fooNullable?.length ?: -1) // => -1 + + /* + Metodlar "fun" anahtar kelimesi ile tanımlanır. + Metod argümanları, Metod adından sonra parantez içinde belirtilir. + Metod argümanlarının opsiyonel olarak default (varsayılan) değerleri olabilir. + Metodun dönüş tipi, gerekirse, metod parentezinden sonra ':' operatörü ile belirtilir. + */ + fun hello(name: String = "dünya"): String { + return "Merhaba, $name!" + } + println(hello("foo")) // => Merhaba, foo! + println(hello(name = "bar")) // => Merhaba, bar! + println(hello()) // => Merhaba, dünya! + + /* + Bir metoda çokca argüman göndermek için 'vararg' anahtar kelimesi + kullanılır. + */ + fun varargExample(vararg names: Int) { + println("${names.size} adet arguman paslanmıştır") + } + varargExample() // => 0 adet arguman paslanmıştır + varargExample(1) // => 1 adet arguman paslanmıştır + varargExample(1, 2, 3) // => 3 adet arguman paslanmıştır + + /* + Bir metod tek bir ifadeden oluşuyorsa + süslü parantezler yerine '=' kullanılabilir. + */ + fun odd(x: Int): Boolean = x % 2 == 1 + println(odd(6)) // => false + println(odd(7)) // => true + + // Eğer dönüş tipi anlaşılabiliyorsa ayrıca belirtmemize gerek yoktur. + fun even(x: Int) = x % 2 == 0 + println(even(6)) // => true + println(even(7)) // => false + + // Metodlar, metodları arguman ve dönüş tipi olarak alabilir + fun not(f: (Int) -> Boolean): (Int) -> Boolean { + return {n -> !f.invoke(n)} // bu satırdaki !f.invoke(n) metodu !f(n) şeklinde sadeleştirilebilir. + } + + + // Bir metodu sadece '::' ön eki ile de arguman olarak çağırabiliriz + println(not(::odd)(4)) // ==> true + + // Metodlar değişken gibi atanabilir. + val notOdd = not(::odd) + val notEven = not(::even) + + // Lambda ifadeleri arguman olarak paslanabilir. + val notZero = not {n -> n == 0} + /* + Eğer bir lambda fonksiyonu sadece bir arguman alıyorsa, + '->' ifadesi atlanabilir, 'it' ifadesi ile belirtilebilir. + */ + val notPositive = not { it > 0} // not(n -> n > 0) ifadesi ile aynı + + for (i in 0..4) { + println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}") + } + + /* + * Diğer for döngüleri + * */ + val myInt = 3 + for (i in 1..100) { } // kapalı aralık. 100 dahil. + for (i in 1 until 100) { } // 100 dahil değil + for (x in 2..10 step 2) { } // ikişer adımlı + for (x in 10 downTo 1) { } // Ondan geriye doğru. 1 dahil. + if (myInt in 1..10) { } + + + + /* + Bir sınıf tanımlamak için 'class' anahtar kelimesi kullanılır. + Kotlin'de bütün sınıflar varsayılan olarak 'final' tanımlanırlar. + * */ + class ExampleClass(val x: Int) { + + fun memberFunction(y: Int): Int { + return x + y + } + + infix fun yTimes(y: Int): Int { + return x * y + } + } + /* + * Bir sınıfı türetilebilir yapmak için 'open' anahtar kelimesi kullanılır. + * */ + open class A + + class B : A() + + + /* + Yeni bir instance oluşturmak için doğrudan constructor çağırılır. + Kotlinde 'new' anahtar kelimesi yoktur. + */ + val fooExampleClass = ExampleClass(7) + // Bir sınıfa üye metodları . (nokta) ile çağırabiliriz. + println(fooExampleClass.memberFunction(4)) // => 11 + /* + 'infix' ön eki ile tanımlanan metodlar + alışılan metod çağrısını daha kolay bir söz dizimine dönüştürür. + */ + println(fooExampleClass yTimes 4) // => 28 + + /* + Data class lar sadece veri tutan sınıflar için uygun bir çözümdür. + Bu şekilde tanımlanan sınıfların "hashCode"/"equals" ve "toString" metodları + otomatik olarak oluşur. + */ + data class DataClassExample (val x: Int, val y: Int, val z: Int) + val fooData = DataClassExample(1, 2, 4) + println(fooData) // => DataClassExample(x=1, y=2, z=4) + + // Data class ların copy metodları olur. + val fooCopy = fooData.copy(y = 100) + println(fooCopy) // => DataClassExample(x=1, y=100, z=4) + + // Destructuring Declarations, bir objeyi çoklu değişkenler ile ifade etme yöntemidir. + val (a, b, c) = fooCopy + println("$a $b $c") // => 1 100 4 + + // bir 'for' döngüsü içinde 'Destructuring' : + for ((a, b, c) in listOf(fooData)) { + println("$a $b $c") // => 1 100 4 + } + + val mapData = mapOf("a" to 1, "b" to 2) + // Map.Entry de destructurable gösterilebilir. + for ((key, value) in mapData) { + println("$key -> $value") + } + + // 'with' metodu ile bir objeye bir lamda metodu uygulayabiliriz. + data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) + val fooMutableData = MutableDataClassExample(7, 4, 9) + with (fooMutableData) { + x -= 2 + y += 2 + z-- + } + + println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8) + + /* + 'listOf' metodu ile bir liste oluşturulabilir. + Oluşan liste immutable olacaktır, yani elaman eklenemez ve çıkarılamaz. + */ + val fooList = listOf("a", "b", "c") + println(fooList.size) // => 3 + println(fooList.first()) // => a + println(fooList.last()) // => c + // Elemanlara indexleri ile erişilebilir. + println(fooList[1]) // => b + + // Mutable bir liste ise 'mutableListOf' metodu ile oluşturabilir. + val fooMutableList = mutableListOf("a", "b", "c") + fooMutableList.add("d") + println(fooMutableList.last()) // => d + println(fooMutableList.size) // => 4 + + // Bir 'set' oluşturmak için 'setOf' metodunu kullanabiliriz. + val fooSet = setOf("a", "b", "c") + println(fooSet.contains("a")) // => true + println(fooSet.contains("z")) // => false + + // 'mapOf' metodu ile 'map' oluşturabiliriz. + val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9) + // Map değerlerine ulaşmak için : + println(fooMap["a"]) // => 8 + + /* + Sequence, Kotlin dilinde lazy-hesaplanan collection ları temsil eder. + Bunun için 'generateSequence' metodunu kullanabiliriz. Bu metod bir önceki değerden + bir sonraki değeri hesaplamak için gerekli bir lamda metodunu arguman olarak alır. + */ + val fooSequence = generateSequence(1, { it + 1 }) + + val x = fooSequence.take(10).toList() + println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + + // Örneğin fibonacci serisi oluşturabilen bir 'Sequence' oluşturmak için: + fun fibonacciSequence(): Sequence<Long> { + var a = 0L + var b = 1L + + fun next(): Long { + val result = a + b + a = b + b = result + return a + } + + return generateSequence(::next) + } + val y = fibonacciSequence().take(10).toList() + println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] + + + // Kotlin Collection lar ile çalışmak için higher-order metodlar sağlar. + val z = (1..9) + .map {it * 3} // her bir elamanı 3 ile çarp + .filter {it < 20} // 20 den küçük değerleri ele + .groupBy {it % 2 == 0} // ikiye tam bölünen ve bölünmeyen şeklinde grupla (Map) + .mapKeys {if (it.key) "even" else "odd"} // oluşan map in boolen 'key' lerini String bir değere dönüştür. + println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]} + + // Bir 'for' döngüsü 'itearator' sağlayan her objeye uygulanabilir. + for (c in "merhaba") { + println(c) + } + + // 'while' döngüsü diğer dillere benzer şekilde çalışır. + var ctr = 0 + while (ctr < 5) { + println(ctr) + ctr++ + } + do { + println(ctr) + ctr++ + } while (ctr < 10) + + /* + 'if' bir dönüş değeri olan deyim gibi de kullanılabilir. + Bu sebepten Kotlin, Java'da bulunan '?:' ifadesi içermez. + */ + val num = 5 + val message = if (num % 2 == 0) "even" else "odd" + println("$num is $message") // => 5 is odd + + // 'if-else if' yapıları için 'when' kullanılabilir. + val i = 10 + when { + i < 7 -> println("first block") + fooString.startsWith("hello") -> println("second block") + else -> println("else block") + } + + // 'when' bir parametre ile de kullanılabilir. + when (i) { + 0, 21 -> println("0 or 21") + in 1..20 -> println("in the range 1 to 20") + else -> println("none of the above") + } + + // 'when' dönüş değeri olan bir metod gibi de davranabilir. + var result = when (i) { + 0, 21 -> "0 or 21" + in 1..20 -> "in the range 1 to 20" + else -> "none of the above" + } + println(result) + + + /* + Bir objenin tipini 'is' operatörü ile tayin edebiliriz. + Eğer obje tip kontrolünü geçerse, cast etmeden doğrudan + o tipteymiş gibi kullanılabilir. + */ + fun smartCastExample(x: Any) : Boolean { + if (x is Boolean) { + // x otomatik olarak Boolean'a cast edilir. + return x + } else if (x is Int) { + // x otomatik olarak Int tipine cast edilir. + return x > 0 + } else if (x is String) { + // x otomatik olarak String tipine cast edilir. + return x.isNotEmpty() + } else { + return false + } + } + println(smartCastExample("Merhaba, dünya!")) // => true + println(smartCastExample("")) // => false + println(smartCastExample(5)) // => true + println(smartCastExample(0)) // => false + println(smartCastExample(true)) // => true + + // Smartcast 'when' bloğu ile de çalışır. + fun smartCastWhenExample(x: Any) = when (x) { + is Boolean -> x + is Int -> x > 0 + is String -> x.isNotEmpty() + else -> false + } + + /* + Extension lar, bir sınıfa fonksinolalite eklemenin bir yoludur. + */ + fun String.remove(c: Char): String { + return this.filter {it != c} + } + println("Merhaba, dünya!".remove('a')) // => Merhb, düny! + + + + //Biraz detaylı Kotlin + + + /* + * Delegated Properties, bir değişken tanımlarken kullanılan birkaç standart yöntemler içerir. + * https://kotlinlang.org/docs/reference/delegated-properties.html + * En bilinen delegate property metodları: lazy(), observable() + * */ + + /* + * Lazy, bir değişkeni ilk erişimde çalıştırılacak olan bir lambda ile tanımlama metodudur. + * Sonraki erişimlerde değişkene atanan değer hatırlanır. + * Lazy, synchronized bir delegation yöntemidir; değer sadece bir thread içinde hesaplanır, + * tüm thread ler aynı değere erişir. Eğer senkronizasyon gerekli değilse, lazy metodu içine + * LazyThreadSafetyMode.PUBLICATION paslanabilir. + * */ + + val lazyValue: String by lazy( { + println("bi sn... hesaplıyorum....") + "Selam!" + }) + + println(lazyValue)// bi sn... hesaplıyorum.... Selam! + println(lazyValue) // Selam! + /* + * Observable, bir değişkende olabilecek yeniden atama değişikliklerini dinleme yöntemidir. + * İki arguman alır; değişkenin ilk değeri, değiştiğinde çağrılan bir handler metodu. Handler + * metodu değişken her değiştiğinde çağırılır. + * */ + var myObservableName: String by Delegates.observable("<isim yok>") { + prop, old, new -> + println("$old -> $new") + } + myObservableName = "Baha" //<isim yok> -> Baha + myObservableName = "Can" //Baha -> Can + + + /* + * Eğer değişkenin yeniden atanmasını denetlemek isterek vetoable() + * metodunu kullanabiliriz. + * */ + + var myVetoableName : String by Delegates.vetoable("<isim yok>"){ + property, oldValue, newValue -> + if (newValue.length < 2) { + println("Tek harfli isim kabul etmiyoruz!") + false + } else { + println("$oldValue -> $newValue") + true + } + } + + myVetoableName = "Baha" //<isim yok> -> Baha + myVetoableName = "C" //Tek harfli isim kabul etmiyoruz! + println(myVetoableName) //Baha + + + //singleton değişkene ulaşmak: + println(ObjectExample.hello()) // => Merhaba +} + +// Enum class lar Java'daki enum lara benzerdir. +enum class EnumExample { + A, B, C +} + +/* +'object' anahtar kelimesi ile singleton nesneler oluşturulabilir. +Bu şekilde tanımlanan sınıflardan yeni nesneler oluşturulamaz, sadece adı ile refere edilebilir. +*/ +object ObjectExample { + fun hello(): String { + return "Merhaba" + } +} + +fun useObject() { + ObjectExample.hello() + val someRef: Any = ObjectExample +} + +``` + +### İlerisi için: + +* [Kotlin tutorials](https://kotlinlang.org/docs/tutorials/) +* [Try Kotlin in your browser](http://try.kotlinlang.org/) +* [A list of Kotlin resources](http://kotlin.link/) +* [Kotlin Koans in your IDE](https://kotlinlang.org/docs/tutorials/koans.html/) 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/tr-tr/swift-tr.html.markdown b/tr-tr/swift-tr.html.markdown index e694d95d..4c2cf59b 100644 --- a/tr-tr/swift-tr.html.markdown +++ b/tr-tr/swift-tr.html.markdown @@ -443,47 +443,47 @@ if let daire = benimBosDairem { // Sınıflar gibi metotlar içerebilirler. enum Kart { - case Kupa, Maca, Sinek, Karo + case kupa, maca, sinek, karo func getIcon() -> String { switch self { - case .Maca: return "♤" - case .Kupa: return "♡" - case .Karo: return "♢" - case .Sinek: return "♧" + case .maca: return "♤" + case .kupa: return "♡" + case .karo: return "♢" + case .sinek: return "♧" } } } // Enum değerleri kısayol syntaxa izin verir. Eğer değişken tipi açık olarak belirtildiyse enum tipini yazmaya gerek kalmaz. -var kartTipi: Kart = .Kupa +var kartTipi: Kart = .kupa // Integer olmayan enumlar direk değer (rawValue) atama gerektirir. enum KitapAdi: String { - case John = "John" - case Luke = "Luke" + case john = "John" + case luke = "Luke" } -print("Name: \(KitapAdi.John.rawValue)") +print("Name: \(KitapAdi.john.rawValue)") // Değerlerle ilişkilendirilmiş Enum enum Mobilya { // Int ile ilişkilendirilmiş - case Masa(yukseklik: Int) + case masa(yukseklik: Int) // String ve Int ile ilişkilendirilmiş - case Sandalye(String, Int) - + case sandalye(String, Int) + func aciklama() -> String { switch self { - case .Masa(let yukseklik): + case .masa(let yukseklik): return "Masa boyu \(yukseklik) cm" - case .Sandalye(let marka, let yukseklik): + case .sandalye(let marka, let yukseklik): return "\(brand) marka sandalyenin boyu \(yukseklik) cm" } } } -var masa: Mobilya = .Masa(yukseklik: 80) +var masa: Mobilya = .masa(yukseklik: 80) print(masa.aciklama()) // "Masa boyu 80 cm" -var sandalye = Mobilya.Sandalye("Foo", 40) +var sandalye = Mobilya.sandalye("Foo", 40) print(sandalye.aciklama()) // "Foo marka sandalyenin boyu 40 cm" diff --git a/typescript.html.markdown b/typescript.html.markdown index ef37182d..acc258b4 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -9,7 +9,7 @@ TypeScript is a language that aims at easing development of large scale applicat TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript. It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript. -This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](javascript.html.markdown). +This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](/docs/javascript). To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. @@ -29,7 +29,7 @@ let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean -// Use const keyword for constant variables +// Use const keyword for constants const numLivesForCat = 9; numLivesForCat = 1; // Error @@ -50,7 +50,7 @@ function bigHorribleAlert(): void { // Functions are first class citizens, support the lambda "fat arrow" syntax and // use type inference -// The following are equivalent, the same signature will be infered by the +// The following are equivalent, the same signature will be inferred by the // compiler, and same JavaScript will be emitted let f1 = function (i: number): number { return i * i; } // Return type inferred diff --git a/uk-ua/java-ua.html.markdown b/uk-ua/java-ua.html.markdown index 1ea30f3d..df642f73 100644 --- a/uk-ua/java-ua.html.markdown +++ b/uk-ua/java-ua.html.markdown @@ -1,5 +1,6 @@ --- language: java +filename: LearnJava-ua.java contributors: - ["Jake Prather", "http://github.com/JakeHP"] - ["Jakukyo Friel", "http://weakish.github.io"] @@ -11,8 +12,8 @@ contributors: translators: - ["Oleksandr Tatarchuk", "https://github.com/tatarchuk"] - ["Andre Polykanine", "https://github.com/Oire"] -filename: LearnJavaUa.java lang: uk-ua + --- Java є об’єктно-орієнтованою мовою програмування загального призначення з підтримкою паралельного програмування, яка базується на класах. @@ -29,7 +30,7 @@ JavaDoc-коментар виглядає так. Використовуєтьс // Імпорт класу ArrayList з пакета java.util import java.util.ArrayList; -// Імпорт усіх класів з пакета java.security +// Імпорт усіх класів з пакета java.security import java.security.*; // Кожний .java файл містить один зовнішній публічний клас, ім’я якого співпадає @@ -98,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; @@ -129,13 +130,13 @@ public class LearnJava { // байтів, операції над ними виконуються функціями, які мають клас BigInteger // // BigInteger можна ініціалізувати, використовуючи масив байтів чи рядок. - + BigInteger fooBigInteger = new BigInteger(fooByteArray); // BigDecimal — Незмінні знакові дробові числа довільної точності // - // BigDecimal складається з двох частин: цілого числа довільної точності + // BigDecimal складається з двох частин: цілого числа довільної точності // з немасштабованим значенням та 32-бітного масштабованого цілого числа // // BigDecimal дозволяє розробникам контролювати десяткове округлення. @@ -146,10 +147,10 @@ public class LearnJava { // чи немасштабованим значенням (BigInteger) і масштабованим значенням (int). BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt); - + // Для дотримання заданої точності рекомендується використовувати - // конструктор, який приймає String - + // конструктор, який приймає String + BigDecimal tenCents = new BigDecimal("0.1"); @@ -294,7 +295,7 @@ public class LearnJava { // Виконається 10 разів, fooFor 0->9 } System.out.println("Значення fooFor: " + fooFor); - + // Вихід із вкладеного циклу через мітку outer: for (int i = 0; i < 10; i++) { @@ -305,7 +306,7 @@ public class LearnJava { } } } - + // Цикл For Each // Призначений для перебору масивів та колекцій int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9}; @@ -317,7 +318,7 @@ public class LearnJava { // Оператор вибору Switch Case // Оператор вибору працює з типами даних byte, short, char, int. - // Також працює з переліками Enum, + // Також працює з переліками Enum, // класом String та класами-обгортками примітивних типів: // Character, Byte, Short та Integer. int month = 3; @@ -333,7 +334,7 @@ public class LearnJava { break; } System.out.println("Результат Switch Case: " + monthString); - + // Починаючи з Java 7 і далі, вибір рядкових змінних здійснюється так: String myAnswer = "можливо"; switch(myAnswer) { @@ -397,7 +398,7 @@ public class LearnJava { // toString повертає рядкове представлення об’єкту. System.out.println("Інформація про об’єкт trek: " + trek.toString()); - + // У Java немає синтаксису для явного створення статичних колекцій. // Це можна зробити так: @@ -553,7 +554,7 @@ public interface Digestible { // Можна створити клас, що реалізує обидва інтерфейси. public class Fruit implements Edible, Digestible { - + @Override public void eat() { // ... @@ -693,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(); // => Понеділки важкі. @@ -736,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 ac6a2bde..6a64a623 100644 --- a/uk-ua/javascript-ua.html.markdown +++ b/uk-ua/javascript-ua.html.markdown @@ -17,7 +17,7 @@ JavaScript було створено в 1995 році Бренданом Айк вбудована підтримка браузерами призвела до того, що JavaScript став популярніший за власне Java. -Зараз JavaScript не обмежується тільки веб-браузеорм. Наприклад, Node.js, +Зараз JavaScript не обмежується тільки веб-браузером. Наприклад, Node.js, програмна платформа, що дозволяє виконувати JavaScript код з використанням рушія V8 від браузера Google Chrome, стає все більш і більш популярною. @@ -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/vala.html.markdown b/vala.html.markdown new file mode 100755 index 00000000..393578b0 --- /dev/null +++ b/vala.html.markdown @@ -0,0 +1,503 @@ +--- +language: vala +contributors: + - ["Milo Gilad", "https://github.com/Myl0g"] +filename: LearnVala.vala +--- + +In GNOME's own words, "Vala is a programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C." + +Vala has aspects of Java and C#, so it'll be natural to those who know either. + +[Read more here.](https://wiki.gnome.org/Projects/Vala) + +```vala + +// Single line comment + +/* Multiline +Comment */ + +/** +* Documentation comment +*/ + +/* Data Types */ + +char character = 'a' +unichar unicode_character = 'u' // 32-bit unicode character + +int i = 2; // ints can also have guaranteed sizes (e.g. int64, uint64) +uint j = -6; // Won't compile; unsigned ints can only be positive + +long k; + +short l; +ushort m; + +string text = "Hello,"; // Note that the == operator will check string content + +string verbatim = """This is a verbatim (a.k.a. raw) string. Special characters +(e.g. \n and "") are not interpreted. They may also be multiple lines long."""; + +// String Templates allow for easy string formatting +string string_template = @"$text world"; // "$text" evaluates to "Hello," + +int test = 5; +int test2 = 10; +string template2 = @"$(test * test2) is a number."; // Expression evaluation + +string template_slice = string_template[7:12]; // => "world" + +// Most data types have methods for parsing. + +bool parse_bool = bool.parse("false"); // => false +int parse_int = int.parse("-52"); // => -52 +string parse_string = parse_int.to_string(); // => "-52" + +/* Basic I/O */ + +stdout.printf(parse_string); // Prints to console +string input = stdin.read_line(); // Gets input from console + +stderr.printf("Error message"); // Error printing + +/* Arrays */ + +int[] int_array = new int[10]; // Array of ints with 10 slots +int better_int_array[10]; // Above expression, shortened +int_array.length; // => 10; + +int[] int_array2 = {5, 10, 15, 20}; // Can be created on-the-fly + +int[] array_slice = int_array2[1:3]; // Slice (copy of data) +unowned int[] array_slice_ref = int_array2[1:3]; // Reference to data + +// Multi-dimensional Arrays (defined with a number of commas in the brackets) + +int[,] multi_array = new int[6,4]; // 6 is the number of arrays, 4 is their size +int[,] multi_array2 = {{7, 4, 6, 4}, + {3, 2, 4, 6}, + {5, 9, 5, 1}}; // new int[3,4] +multi_array2[2,3] = 12; // 2 is the array, 3 is the index in the array +int first_d = multi_array2.length[0] // => 3 +int second_d = multi_array2.length[1] // => 4 + +// Stacked arrays (e.g. int[][]) where array lengths vary are not supported. + +// Multi-dimensional arrays cannot be sliced, nor can they be converted to one- +// dimensional. + +int[] add_to_array = {}; +add_to_array += 12; // Arrays can be dynamically added to + +add_to_array.resize(20); // Array now has 20 slots + +uint8[] chars = "test message".data; +chars.move(5, 0, 7); +stdout.printf((string) chars); // Casts the array to a string and prints it + +/* Control Flow */ + +int a = 1; +int b = 2; +int[] foreach_demo = {2, 4, 6, 8}; + +while (b > a) { // While loop; checks if expression is true before executing + b--; +} + +do { + b--; +} +while (b > a); // Do While loop; executes the code in "do" before while (b > a) + +for (a = 0; a < 10; a++) { stdout.printf("%d\n", a); } // for loop + +foreach (int foreach_demo_var in foreach_demo) { + stdout.printf("%d\n", foreach_demo_var); +} // foreach works on any iterable collection + +if (a == 0) { + stdout.printf("%d\n", a); +} else if (a > 1) { + stdout.printf("%d\n", a); +} else { + stdout.printf("A is less than 0"); +} // if-then-else + +switch (a) { + case 1: + stdout.printf("A is 1\n"); + break; + case 5: + case 10: + stdout.printf("A is 5 or 10\n"); + break; + default: + stdout.printf("???\n") + break; +} // switch statement + +/* Type Casting and Inference */ + +int cast_to_float = 10; +float casted_float = (float) cast_to_float; // static casting; no runtime checks + +// For runtime checks, use dynamic casting. +// Dynamically casted objects must be the following: +// - Object's class is the same class as the desired type +// - Object's class is a subclass of the desired type +// - Desired class is an interface implemented by the object's class + +float dyna_casted_float = cast_to_float as float // Won't compile + +var inferred_string = "hello"; // Type inference + +/* Methods (a.k.a. functions) */ + +int method_demo(string arg1, Object arg2) { // Returns int and takes args + return 1; +} + +// Vala methods cannot be overloaded. + +void some_method(string text) { } +void some_method(int number) { } // Won't compile + +// To achieve similar functionality, use default argument values. + +void some_better_method(string text, int number = 0) { } + +some_better_method("text"); +some_better_method("text", 12); + +// varargs (variable-length argument lists) are also supported. + +void method_with_varargs(int arg1, ...) { + var varargs_list = va_list(); // gets the varargs list + + string arg_string = varargs_list.arg(); // gets arguments, one after another + int int_vararg = varargs_list.arg(); + + stdout.printf("%s, %d\n", arg_string, int_vararg) +} + +string? ok_to_be_null(int? test_int) { } // "?" denotes possible null value + +// Delegates + +delegate void DelegateDemo(char char_a); + +void delegate_match(char char_a) { // Matches DelegateDemo's signature + stdout.printf("%d\n"); +} + +void call_delegate(DelegateDemo d, char char_b) { // Takes a delegate arg + d(char_b) // calls delegate +} + +void final_delegate_demo() { + call_delegate(delegate_match); // Passes matching method as argument +} + +// Lambdas (a.k.a. Anonymous Methods) are defined with "=>" + +(a) => { stdout.printf("%d\n", a); } // Prints "a" + +/* Namespaces */ + +namespace NamespaceDemo { + // Allows you to organize variable names + int namespace_int = 12; +} +namespace_int += 5; // Won't compile + +using NamespaceDemo; +namespace_int += 5; // Valid + +/* Structs and Enums */ + +struct Closet { + public uint shirts; // Default access modifier is private + public uint jackets; +} + +Closet struct_init_1 = Closet(); // or Closet struct_init_1 = {}; +Closet struct_init_2 = {15, 3}; +var struct_init_3 = Closet() { // Type inference also works + shirts = 15; + jackets = 3; +} + +enum HouseSize { // An example of an enum + SMALL, + MODERATE, + BIG +} + +/* Classes and Object-Oriented Programming */ + +class Message : GLib.Object { // Class Message extends GLib's Object + private string sender; // a private field + public string text {get; set;} // a public property (more on that later) + protected bool is_digital = true; // protected (this class and subclasses) + internal bool sent = false; // internal (classes in same package) + + public void send(string sender) { // public method + this.sender = sender; + sent = true; + } + + public Message() { // Constructor + // ... + } + +} + +// Since method overloading isn't possible, you can't overload constructors. +// However, you can use named constructors to achieve the same functionality. + +public class Calculator : GLib.Object { + + public Calculator() { + } + + public Calculator.with_name(string name) { + } + + public Calculator.model(string model_id, string name = "") { + this.with_name(@"$model_id $name"); // Chained constructors with "this" + } + ~Calculator() { } // Only needed if you're using manual memory management +} + +var calc1 = new Calculator.with_name("Temp"); +var calc2 = new Calculator.model("TI-84"); + +// Signals (a.k.a. events or event listeners) are a way to execute multiple +// methods with the same signature at the same time. + +public class SignalDemo : GLib.Object { + public signal void sig_demo(int sig_demo_int); // Must be public + + public static int main(string[] args) { + // main method; program does not compile without it + + var sig_demo_class = new SignalDemo(); // New instance of class + + sig_demo_class.sig_demo.connect((ob, sig_int) => { // Lambda used as handler + stdout.printf("%d\n", sig_int); // "ob" is object on which it is emitted + }); + + sig_demo_class.sig_demo(27); // Signal is emitted + + return 0; + } +} + +// You may use the connect() method and attach as many handlers as you'd like. +// They'll all run at around the same time when the signal is emitted. + +// Properties (getters and setters) + +class Animal : GLib.Object { + private int _legs; // prefixed with underscore to prevent name clashes + + public int legs { + get { return _legs; } + set { _legs = value; } + } + + public int eyes { get; set; default = 5; } // Shorter way + public int kingdom { get; private set; default = "Animalia"} // Read-only + + public static void main(string args[]) { + rabbit = new Animal(); + + // All GLib.Objects have a signal "notify" emitted when a property changes. + + // If you specify a specific property, replace all underscores with dashes + // to conform to the GObject naming convention. + + rabbit.notify["eyes"].connect((s, p) => { // Remove the ["eyes"] for all + stdout.printf("Property '%s' has changed!\n", p.name); + }); + + rabbit.legs = 2; + rabbit.legs += 2; + rabbit.eyes = 2; + + } +} + +// Inheritance: Vala classes may inherit 1 class. Inheritance is not implicit. + +class SuperDemo : GLib.Object { + public int data1; + protected int data2; + internal int data3; + private int data4; + + public static void test_method { } // Statics can be called w/out an object +} +class SubDemo : SuperDemo { + public static void main(string args[]) { + stdout.printf((string) data1); // Will compile + stdout.printf((string) data2); // Protected can be accessed by subclasses + stdout.printf((string) data3); // Internal is accessible to package + stdout.printf((string) data4); // Won't compile + } +} + +// Abstract Classes and Methods + +public abstract class OperatingSystem : GLib.Object { + public void turn_on() { + stdout.printf("Booted successfully.\n"); + } + public abstract void use_computer(); +} + +public class Linux : OperatingSystem { + public override void use_computer() { // Abstract methods must be overridden + stdout.printf("Beep boop\n"); + } +} + +// Add default behavior to an abstract method by making it "virtual". + +public abstract class HardDrive : GLib.Object { + public virtual void die() { + stdout.printf("CLICK-CLICK-CLICK\n"); + } +} +public class MyHD : HardDrive { + public override void die() { + return; + } +} + +// Interfaces: classes can implement any number of these. + +interface Laptop { // May only contain abstracts or virtuals + public abstract void turn_on(); + public abstract void turn_off(); + + public abstract int cores; // Won't compile; fields cannot be abstract + public abstract int cores {get; set;} // Will compile + + public virtual void keyboard() { // Virtuals are allowed (unlike Java/C#) + stdout.printf("Clickity-clack\n"); + } +} + +// The ability to use virtuals in Vala means that multiple inheritance is +// possible (albeit somewhat confined) + +// Interfaces cannot implement interfaces, but they may specify that certain +// interfaces or classes must be also implemented (pre-requisites). + +public interface CellPhone : Collection, GLib.Object {} + +// You can get the type info of a class at runtime dynamically. + +bool type_info = object is TypeName; // uses "is" to get a bool + +Type type_info2 = object.get_type(); +var type_name = type_info2.name(); + +Type type_info3 = typeof(Linux); +Linux type_demo = (Linux) Object.new(type_info3); + +// Generics + +class Computer<OperatingSystem> : GLib.Object { + private OperatingSystem os; + + public void install_os(OperatingSystem os) { + this.os = os; + } + public OperatingSystem retrieve_os() { + return this.os; + } +} + +var new_computer = new Computer<Linux>(); + +/* Other Features */ + +// Assertions: crash if a statement is not true (at runtime) + +bool is_true = true; +assert(is_true); + +// Contract Programming + +int contract_demo(int arg1, int arg2) { + requires(arg1 > 0 && arg1 < 10) // Notice the lack of semicolon + requires(arg2 >= 12) + ensures(result >= 0) +} + +// Error Handling + +void error_demo(int int_ex) throws GError { + if (int_ex != 1) { + throw new GError("TEST MESSAGE"); + } +} +void error_demo2() { + try { + error_demo(0); + } catch (GError ge) { + stdout.printf("%s\n", ge.message); + } +} + +// Main Loop + +void main() { + + var main_loop = new MainLoop(); + var time = new TimeoutSource(2000); + + time.set_callback(() => { // Executes the following lambda after 2000ms + stdout.printf("2000ms have passed\n"); + main_loop.quit(); + return false; + }); + + time.attach(main_loop.get_context()); + + loop.run(); +} + +// Pointers (manual memory management) + +Object* pointer_obj = new Object(); // Creates Object instance and gives pointer + +pointer_obj->some_method(); // Executes some_method +pointer_obj->some_data; // Returns some_data + +delete pointer_obj; + +int more = 57; +int* more_pointer = &i; // & = address-of +int indirection_demo = more_pointer*; // indirection + +// Profiles: affect which Vala features are avaliable and which libraries the +// C-code will use. +// - gobject (default) +// posix +// dova +// Use "--profile=whatever" when compiling. + +``` +* More Vala documentation can be found [here](https://valadoc.org/). +* [Alternate construction syntax](https://wiki.gnome.org/Projects/Vala/Tutorial#GObject-Style_Construction) similar to GObject +* More on contract programming [here](http://en.wikipedia.org/wiki/Contract_programming) +* Collections library can be found [here](https://wiki.gnome.org/Projects/Vala/Tutorial#Collections) +* [Multithreading](https://wiki.gnome.org/Projects/Vala/Tutorial#Multi-Threading) +* Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). +* D-Bus [integration](https://wiki.gnome.org/Projects/Vala/Tutorial#D-Bus_Integration) diff --git a/vi-vn/html-vi.html.markdown b/vi-vn/html-vi.html.markdown index 4897649f..ca591174 100644 --- a/vi-vn/html-vi.html.markdown +++ b/vi-vn/html-vi.html.markdown @@ -1,6 +1,6 @@ --- language: html -filename: learnhtml.html +filename: learnhtml-vi.html contributors: - ["Christophe THOMAS", "https://github.com/WinChris"] translators: diff --git a/vi-vn/less-vi.html.markdown b/vi-vn/less-vi.html.markdown new file mode 100644 index 00000000..594ccc31 --- /dev/null +++ b/vi-vn/less-vi.html.markdown @@ -0,0 +1,395 @@ +--- +language: less +contributors: + - ["Saravanan Ganesh", "http://srrvnn.me"] +translators: + - ["Thanh Duy Phan", "https://github.com/thanhpd"] +filename: learnless-vi.less +lang: vi-vn +--- + +Less là một CSS pre-processor (bộ tiền xử lí CSS), nó thêm các tính năng như biến (variable), lồng (nesting), mixin và nhiều thứ khác. Less cùng với các CSS pre-processor khác như [Sass](http://sass-lang.com/) giúp lập trình viên viết được các đoạn CSS bảo trì được và không bị lặp lại (DRY - Don't Repeat Yourself). + +```css + + +// Comment (chú thích) một dòng sẽ bị xóa khi Less được biên dịch thành CSS + +/* Comment trên nhiều dòng sẽ được giữ lại */ + + + +/* Biến +==============================*/ + + +/* Ta có thể lưu giá trị CSS (ví dụ như color) vào một biến. + Sử dụng ký hiệu '@' để khai báo một biến. */ + +@primary-color: #a3a4ff; +@secondary-color: #51527f; +@body-font: 'Roboto', sans-serif; + +/* Sau khi khai báo biến, ta có thể sử dụng nó ở trong tệp stylesheet. + Nhờ sử dụng biến ta chỉ cần thay đổi một lần + tại 1 nơi để thay đổi tất cả những đoạn sử dụng biến */ + +body { + background-color: @primary-color; + color: @secondary-color; + font-family: @body-font; +} + +/* Đoạn code trên sẽ được biên dịch thành: */ + +body { + background-color: #a3a4ff; + color: #51527F; + font-family: 'Roboto', sans-serif; +} + + +/* Cách sử dụng này giúp ta dễ dàng bảo trì hơn + việc phải đổi giá trị mỗi lần nó xuất hiện + trong tệp stylesheet. */ + + + +/* Mixins +==============================*/ + + +/* Nếu đang viết một đoạn code cho nhiều hơn một + element, ta có thể sử dụng lại nó dễ dàng. */ + +.center { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} + +/* Ta có thể dùng mixin chỉ bằng việc thêm selector + vào trong nội dung style của element khác */ + +div { + .center; + background-color: @primary-color; +} + +/* Đoạn code trên sẽ được biên dịch thành: */ + +.center { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} +div { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; + background-color: #a3a4ff; +} + +/* Ta có thể ngăn không cho code mixin được biên dịch + bằng cách thêm cặp ngoặc tròn đằng sau selector */ + +.center() { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} + +div { + .center; + background-color: @primary-color; +} + +/* Đoạn code trên sẽ được biên dịch thành: */ +div { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; + background-color: #a3a4ff; +} + + + +/* Nesting - Lồng +==============================*/ + + +/* Less cho phép ta có thể lồng selector bên trong selector */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: #f00; + } +} + +/* Selector bắt đầu bằng ký tự '&' sẽ thay thế ký tự '&' + với selector cha. */ +/* Ta cũng có thể lồng các pseudo-class với nhau */ +/* Nên lưu ý không nên lồng quá nhiều lần sẽ làm code kém tính bảo trì. + Kinh nghiệm cho thấy không nên lồng quá 3 lần. + Ví dụ: */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: red; + + &:hover { + background-color: blue; + } + + a { + color: white; + } + } +} + +/* Biên dịch thành: */ + +ul { + list-style-type: none; + margin-top: 2em; +} + +ul li { + background-color: red; +} + +ul li:hover { + background-color: blue; +} + +ul li a { + color: white; +} + + + +/* Function +==============================*/ + + +/* Less cung cấp các function có thể được dùng để hoàn thành + các công việc khác nhau. */ + +/* Function được gọi sử dụng tên của nó và truyền vào + các tham số được yêu cầu. */ + +body { + width: round(10.25px); +} + +.header { + background-color: lighten(#000, 0.5); +} + +.footer { + background-color: fadeout(#000, 0.25) +} + +/* Biên dịch thành: */ + +body { + width: 10px; +} + +.header { + background-color: #010101; +} + +.footer { + background-color: rgba(0, 0, 0, 0.75); +} + +/* Ta có thể định nghĩa function mới. + Function khá tương tự với mixin bởi chúng đều có thể được tái + sử dụng. Khi lựa chọn giữa việc sử dụng function hay mixin, + hãy nhớ mixin được tối ưu cho việc tạo ra CSS trong khi + function sẽ được sử dụng tốt hơn cho logic sẽ được sử dụng + xuyên suốt Less code. Các ví dụ trong phần 'Toán tử' là ứng cử viên + sáng giá cho việc dùng function có thể tái sử dụng được. +*/ + +/* Function này tính giá trị trung bình của hai số: */ +.average(@x, @y) { + @average-result: ((@x + @y) / 2); +} + +div { + .average(16px, 50px); // gọi mixin + padding: @average-result; // sử dụng giá trị trả về của mixin +} + +/* Biên dịch thành: */ + +div { + padding: 33px; +} + + + +/* Mở rộng (Thừa kế) +==============================*/ + + +/* Mở rộng là cách để chia sẻ thuộc tính của một selector cho selector khác */ + +.display { + height: 50px; +} + +.display-success { + &:extend(.display); + border-color: #22df56; +} + +/* Biên dịch thành: */ +.display, +.display-success { + height: 50px; +} +.display-success { + border-color: #22df56; +} + +/* Nên mở rộng một khai báo CSS có trước thay vì tạo một mixin mới + bởi cách nó nhóm các lớp có chung một style gốc. + Nếu thực hiện với mixin, các thuộc tính sẽ bị trùng lặp + cho mỗi khai báo có sử dụng mixin. Mặc dù không ảnh hưởng đến luồng công việc nhưng nó + tạo ra các đoạn code CSS thừa sau khi được biên dịch. +*/ + + +/* Partials and Imports - Chia nhỏ và nhập vào +==============================*/ + + +/* Less cho phép ta tạo các partial file (tệp con). + Sử dụng nó giúp ta có thể tổ chức code Less theo mô-đun có hệ thống. + Các tệp con thường bắt đầu với ký tự gạch dưới '_', vd: _reset.less + và được nhập vào file Less chính để được biên dịch thành CSS */ + +/* Quan sát ví dụ sau, ta sẽ đặt đoạn code dưới đây vào tệp tên là _reset.less */ + +html, +body, +ul, +ol { + margin: 0; + padding: 0; +} + +/* Less cung cấp cú pháp @import cho phép nhập các partial vào một file. + Cú pháp này trong Less sẽ nhập các file và kết hợp chúng lại với + code CSS được sinh ra. Nó khác với cú pháp @import của CSS, + bản chất là tạo một HTTP request mới để tải về tệp tin được yêu cầu. */ + +@import 'reset'; + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + +/* Biên dịch thành: */ + +html, body, ul, ol { + margin: 0; + padding: 0; +} + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + + + +/* Toán học +==============================*/ + + +/* Less cung cấp các toán tử sau: +, -, *, / và %. + Điều này rất có ích cho việc tính toán giá trị trực tiếp + trong tệp Less thay vì phải tính toán thủ công. + Dưới đây là ví dụ về việc tạo một khung thiết kế đơn giản có hai cột. */ + +@content-area: 960px; +@main-content: 600px; +@sidebar-content: 300px; + +@main-size: @main-content / @content-area * 100%; +@sidebar-size: @sidebar-content / @content-area * 100%; +@gutter: 100% - (@main-size + @sidebar-size); + +body { + width: 100%; +} + +.main-content { + width: @main-size; +} + +.sidebar { + width: @sidebar-size; +} + +.gutter { + width: @gutter; +} + +/* Biên dịch thành: */ + +body { + width: 100%; +} + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + +.gutter { + width: 6.25%; +} + + +``` + +## Tập sử dụng Less + +Nếu bạn cần xài thử Less trên trình duyệt, hãy ghé qua: +* [Codepen](http://codepen.io/) +* [LESS2CSS](http://lesscss.org/less-preview/) + +## Tính tương thích + +Less có thể được dùng trong bất kì dự án nào miễn là ta có chương trình để biên dịch nó thành CSS. Ta cần chắc chắn rằng đoạn CSS đang dùng tương thích với các phiên bản trình duyệt mong muốn. + +[QuirksMode CSS](http://www.quirksmode.org/css/) và [CanIUse](http://caniuse.com) là nguồn thông tin tin cậy để kiểm tra tính tương thích của mã CSS. + +## Tìm hiểu thêm +* [Tài liệu chính thức](http://lesscss.org/features/) +* [Less CSS - Hướng dẫn cho người mới bắt đầu](http://www.hongkiat.com/blog/less-basic/)
\ No newline at end of file diff --git a/vi-vn/markdown-vi.html.markdown b/vi-vn/markdown-vi.html.markdown new file mode 100644 index 00000000..89b59253 --- /dev/null +++ b/vi-vn/markdown-vi.html.markdown @@ -0,0 +1,325 @@ +--- +language: markdown +contributors: + - ["Dan Turkel", "http://danturkel.com/"] + - ["Jacob Ward", "http://github.com/JacobCWard/"] +translators: + - ["Thanh Duy Phan", "https://github.com/thanhpd"] +filename: markdown-vi.md +lang: vi-vn +--- + + +Ngôn ngữ Markdown được sáng lập bởi John Gruber vào năm 2004. Nó được tạo ra với mục đích dễ đọc với cú pháp có thể được dễ dàng chuyển đổi qua HTML và các ngôn ngữ khác + +Markdown có sự khác biệt trong cách cài đặt giữa các trình phân tích cú pháp. Hướng dẫn này sẽ đề cập, giải thích tới nếu tính năng có thể được sử dụng chung hay nó chỉ áp dụng cho một trình phân tích riêng biệt. + +- [Phần tử HTML](#html-elements) +- [Đầu mục](#headings) +- [Định dạng văn bản](#simple-text-styles) +- [Đoạn văn](#paragraphs) +- [Danh sách](#lists) +- [Khối code](#code-blocks) +- [Đường kẻ ngang](#horizontal-rule) +- [Liên kết](#links) +- [Ảnh](#images) +- [Khác](#miscellany) + +## 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. + +```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, +ta không thể sử dụng cú pháp Markdown cho nội dung bên trong phần tử đó. --> +``` + +## Đầu mục + +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. + +```md +# Đây là đầu mục <h1> +## Đây là đầu mục <h2> +### Đây là đầu mục <h3> +#### Đây là đầu mục <h4> +##### Đây là đầu mục <h5> +###### Đây là đầu mục <h6> +``` +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. + +```md +Đây là đầu mục h1 +============= + +Đây là đầu mục h2 +------------- +``` + +## Định dạng văn bản + +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. + +```md +*Đoạn văn bản này được in nghiêng.* +_Và đoạn này cũng như vậy._ + +**Đoạn văn bản này được in đậm.** +__Và đoạn này cũng vậy.__ + +***Đoạn văn bản này được in nghiêng và đậm.*** +**_Cách này cũng tương tự_** +*__Và cách này nữa__* +``` + +Trong cài đặt Markdown để hiển thị file của GitHub,ta còn có gạch ngang: + +```md +~~Đoạn văn bản này được gạch ngang.~~ +``` +## Đoạn văn + +Đ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. + +```md +Đây là đoạn văn thứ nhất. + +Đây là đoạn văn thứ hai. +Dòng này vẫn thuộc đoạn văn thứ hai, do không có cách dòng. + + +Đây là đoạn văn thứ ba. +``` + +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. + +```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. +``` + +Khối trích dẫn được sử dụng với kí tự > + +```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ự `>` + +> Ta còn có thể dùng nhiều mức +>> của khối trích dẫn. +> Như vậy có tốt không? + +``` + +## Danh sách + +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. + +```md +* Một mục +* Một mục +* Một mục nữa + +hoặc + ++ Một mục ++ Một mục ++ Một mục khác + +hay + +- Một mục +- Một mục +- Một mục sau +``` + +Danh sách có thứ tự được tạo bởi một số theo sau bằng một dấu chấm. + +```md +1. Mục thứ nhất +2. Mục thứ hai +3. Mục thứ ba +``` + +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! + +```md +1. Mục thứ nhất +1. Mục thứ hai +1. Mục thứ ba +``` +(Sẽ hiển thị như ví dụ trước đó) + +Ta còn có thể sử dụng danh sách con + +```md +1. Mục thứ nhất +2. Mục thứ hai +3. Mục thứ ba + * Mục nhỏ + * Mục nhỏ +4. Mục thứ tư +``` + +Markdown còn cung cấp danh mục (checklist). Nó sẽ hiển thị ra hộp đánh dấu dạng HTML. + +```md +Boxes below without the 'x' are unchecked HTML checkboxes. +- [ ] First task to complete. +- [ ] Second task that needs done +This checkbox below will be a checked HTML checkbox. +- [x] This task has been completed +``` + +## Khối code + +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) + +```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 + +```md + my_array.each do |item| + puts item + end +``` + +Code hiển thị cùng dòng có thể được đánh dấu sử dụng cặp ``. + +```md +John didn't even know what the `go_to()` function did! +``` + +Trong Markdown của GitHub, ta còn có thêm cách để hiển thị code: + +<pre> +<code class="highlight">```ruby +def foobar + puts "Hello world!" +end +```</code></pre> + +The above text doesn't require indenting, plus GitHub will use syntax +highlighting of the language you specify after the \`\`\` +Đoạn trên không cần sử dụng thụt đầu dòng, và GitHub sẽ tô sáng cú pháp sử dụng ngôn ngữ mà ta cung cấp sau đoạn kí tự \`\`\` + +## Kẻ ngang + +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. + + +```md +*** +--- +- - - +**************** +``` + +## Liên kết + +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 (). + +```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 + +```md +[Click me!](http://test.com/ "Link to Test.com") +``` +Đường dẫn tương đối cũng hoạt động. + +```md +[Go to music](/music/). +``` + +Markdown còn hỗ trợ liên kết kiểu tham chiếu. + +<pre><code class="highlight">[<span class="nv">Nhấn vào đây</span>][<span class="ss">link1</span>] để xem thêm! +[<span class="nv">Ngoài ra nhấn vào đây</span>][<span class="ss">foobar</span>] nếu bạn muốn xem qua. + +[<span class="nv">link1</span>]: <span class="sx">http://test.com/</span> <span class="nn">"Tuyệt!"</span> +[<span class="nv">foobar</span>]: <span class="sx">http://foobar.biz/</span> <span class="nn">"Tốt!"</span></code></pre> + +Tiêu đề có thể được đóng trong dấu nháy hay ngoặc đơn, hoặc có thể được bỏ qua. Tham chiếu có thể được đặt bất kì đâu trong văn bản và ID của tham chiếu có thể là bất kì gì miễn là nó độc nhất. + +Ngoài ra còn có kiểu đặt tên ngầm cho phép ta sử dụng đường dẫn làm ID. + +<pre><code class="highlight">[<span class="nv">This</span>][] is a link. + +[<span class="nv">this</span>]: <span class="sx">http://thisisalink.com/</span></code></pre> + +Nhưng nó không được sử dụng rộng rãi. + +## Ảnh + +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 + +```md +![Thuộc tính alt cho ảnh](http://imgur.com/myimage.jpg "Tiêu đề tùy chọn") +``` + +Và kiểu tham chiếu cũng hoạt động như vậy. + +<pre><code class="highlight">![<span class="nv">Đây là thuộc tính alt.</span>][<span class="ss">myimage</span>] + +[<span class="nv">myimage</span>]: <span class="sx">relative/urls/cool/image.jpg</span> <span class="nn">"Đây là tiêu đề"</span></code></pre> + +## Khác + +### Tự động đặt liên kết + +```md +<http://testwebsite.com/> tương đương với +[http://testwebsite.com/](http://testwebsite.com/) +``` + +### Tự động đặt liên kết cho email + +```md +<foo@bar.com> +``` + +### Hiển thị Kí tự đặc biệt + +```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\*. +``` + +### Phím bàn phím + +Trong Markdown của Github, ta có thể sử dụng thẻ `<kbd>` để thay cho phím trên bàn phím. + +```md +Máy treo? Thử bấm tổ hợp +<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd> +``` +### Bảng biểu + +Bảng biểu được hỗ trợ trên Markdown của GitHub, Jira, Trello, v.v và khá khó viết: + +```md +| Cột 1 | Cột2 | Cột 3 | +| :----------- | :------: | ------------: | +| Căn trái | Căn giữa | Căn phải | +| blah | blah | blah | +``` +Hoặc có thể sử dụng kết quả dưới đây + +```md +Cột 1 | Cột 2 | Cột 3 +:-- | :-: | --: +blah | blah | blah +``` + +--- +Để biết thêm thông tin, hãy ghé qua hướng dẫn chính thức về cú pháp của John Gruber [tại đây](http://daringfireball.net/projects/markdown/syntax) và cheatsheet của Adam Pritchard [tại đây](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet). diff --git a/vi-vn/python3-vi.html.markdown b/vi-vn/python3-vi.html.markdown index 16238b35..f6cce1a3 100644 --- a/vi-vn/python3-vi.html.markdown +++ b/vi-vn/python3-vi.html.markdown @@ -1,5 +1,6 @@ --- language: python3 +filename: learnpython3-vi.py contributors: - ["Louie Dinh", "http://pythonpracticeprojects.com"] - ["Steven Basart", "http://github.com/xksteven"] @@ -8,8 +9,8 @@ contributors: - ["evuez", "http://github.com/evuez"] translators: - ["Xuan (Sean) Luong, https://github.com/xuanluong"] -filename: learnpython3.py lang: vi-vn + --- Python được tạo ra bởi Guido van Rossum vào đầu những năm 90s. Ngày nay nó là một trong những ngôn ngữ phổ biến diff --git a/vi-vn/ruby-vi.html.markdown b/vi-vn/ruby-vi.html.markdown index 73382100..1e198205 100644 --- a/vi-vn/ruby-vi.html.markdown +++ b/vi-vn/ruby-vi.html.markdown @@ -1,6 +1,6 @@ --- language: ruby -filename: learnruby.rb +filename: learnruby-vi.rb contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] diff --git a/vi-vn/sass-vi.html.markdown b/vi-vn/sass-vi.html.markdown new file mode 100644 index 00000000..313890d4 --- /dev/null +++ b/vi-vn/sass-vi.html.markdown @@ -0,0 +1,590 @@ +--- +language: sass +filename: learnsass-vi.scss +contributors: + - ["Laura Kyle", "https://github.com/LauraNK"] + - ["Sean Corrales", "https://github.com/droidenator"] + - ["Kyle Mendes", "https://github.com/pink401k"] + - ["Keith Miyake", "https://github.com/kaymmm"] +translators: + - ["Thanh Duy Phan", "https://github.com/thanhpd"] +lang: vi-vn +--- + +Less là một ngôn ngữ mở rộng CSS/ CSS pre-processor, thêm các tính năng như biến (variable), lồng (nesting), mixin và nhiều thứ khác. Sass cùng với các CSS pre-processor khác như [Less](http://lesscss.org/) giúp lập trình viên viết được các đoạn CSS bảo trì được và không bị lặp lại (DRY - Don't Repeat Yourself). + +Sass có hai lựa chọn sử dụng cú pháp khác nhau. Một là SCSS, sử dụng cú pháp giống như CSS nhưng bổ sung thêm các tính năng của Sass. Hai là Sass (cú pháp nguyên bản), sử dụng thụt đầu dòng - indention thay vì ngoặc nhọn và dấu chấm phẩy. +Bài hướng dẫn này sử dụng SCSS. + +Nếu bạn đọc đã quen thuộc với CSS3 thì sẽ tương đối nhanh chóng để nắm được Sass. Nó không cung cấp thuộc tính để style CSS mới nhưng đưa ra những công cụ để có thể viết CSS hiệu quả hơn và có thể bảo trì dễ dàng hơn. + +```sass + + +// Comment (chú thích) một dòng sẽ bị xóa khi Less được biên dịch thành CSS + +/* Comment trên nhiều dòng sẽ được giữ lại */ + + + +/* Variable - Biến +============================== */ + + + +/* Ta có thể lưu giá trị CSS (ví dụ như color) vào một biến. + Sử dụng ký hiệu '$' để khai báo một biến. */ + +$primary-color: #A3A4FF; +$secondary-color: #51527F; +$body-font: 'Roboto', sans-serif; + +/* Sau khi khai báo biến, ta có thể sử dụng nó ở trong tệp stylesheet. + Nhờ sử dụng biến ta chỉ cần thay đổi một lần + tại 1 nơi để thay đổi tất cả những đoạn sử dụng biến */ + +body { + background-color: $primary-color; + color: $secondary-color; + font-family: $body-font; +} + +/* Đoạn code trên sẽ được biên dịch thành: */ +body { + background-color: #A3A4FF; + color: #51527F; + font-family: 'Roboto', sans-serif; +} + +/* Cách sử dụng này giúp ta dễ dàng bảo trì hơn + việc phải đổi giá trị mỗi lần nó xuất hiện + trong tệp stylesheet. */ + + + +/* Control Directive - Chỉ thị +============================== */ + + +/* Sass cho phép sử dụng @if, @else, @for, @while và @each để quản lý luồng code sinh ra CSS */ + +/* Khối điều kiện @if/@else hoạt động như các ngôn ngữ khác */ + +$debug: true !default; + +@mixin debugmode { + @if $debug { + @debug "Debug mode enabled"; + + display: inline-block; + } + @else { + display: none; + } +} + +.info { + @include debugmode; +} + +/* Trong đoạn code trên, nếu $debug được đặt là true thì class .info sẽ được sinh ra và ngược lại. + Lưu ý: @debug sẽ sinh ra thông tin debug trên dòng lệnh (command line). + Chế độ này rất có ích khi thực hiện debug trên file SCSS. */ + +.info { + display: inline-block; +} + +/* @for là khối vòng lặp trên một khoảng các giá trị. + Nó rất có ích cho việc đặt style của một tập hợp các phần tử. + Có hai cách để lặp, "through" sẽ lặp tới kể cả giá trị cuối cùng, "to" sẽ lặp tới và dừng khi đến giá trị cuối cùng. */ + +// Lặp 3 lần (không kể 4) +@for $c from 1 to 4 { + div:nth-of-type(#{$c}) { + left: ($c - 1) * 900 / 3; + } +} + +// Lặp 3 lần (kể cả 3) +@for $c from 1 through 3 { + .myclass-#{$c} { + color: rgb($c * 255 / 3, $c * 255 / 3, $c * 255 / 3); + } +} + +/* Biên dịch thành */ + +div:nth-of-type(1) { + left: 0; +} + +div:nth-of-type(2) { + left: 300; +} + +div:nth-of-type(3) { + left: 600; +} + +.myclass-1 { + color: #555555; +} + +.myclass-2 { + color: #aaaaaa; +} + +.myclass-3 { + color: white; +// SASS tự động chuyển mã #FFFFFF thành white (trắng) +} + +/* Khối lặp @while rất cơ bản: */ + +$columns: 4; +$column-width: 80px; + +@while $columns > 0 { + .col-#{$columns} { + width: $column-width; + left: $column-width * ($columns - 1); + } + + $columns: $columns - 1; +} + +/* Sẽ được biên dịch thành: */ + +.col-4 { + width: 80px; + left: 240px; +} + +.col-3 { + width: 80px; + left: 160px; +} + +.col-2 { + width: 80px; + left: 80px; +} + +.col-1 { + width: 80px; + left: 0px; +} + +/* @each hoạt động giống như @for, nhưng sử dụng một danh sách (list) thay vì thứ tự số đếm. + List được khai báo như những biến khác, sử dụng dấu cách để làm dấu phân cách. */ + +$social-links: facebook twitter linkedin reddit; + +.social-links { + @each $sm in $social-links { + .icon-#{$sm} { + background-image: url("images/#{$sm}.png"); + } + } +} + +/* Sẽ sinh ra: */ + +.social-links .icon-facebook { + background-image: url("images/facebook.png"); +} + +.social-links .icon-twitter { + background-image: url("images/twitter.png"); +} + +.social-links .icon-linkedin { + background-image: url("images/linkedin.png"); +} + +.social-links .icon-reddit { + background-image: url("images/reddit.png"); +} + + +/* Mixin +==============================*/ + +/* Nếu đang viết một đoạn code cho nhiều hơn một + element, ta có thể sử dụng lại nó dễ dàng. + Sử dụng cú pháp '@mixin' kèm theo tên để tạo một mixin. */ + +@mixin center { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; +} + +/* Ta có thể dùng mixin bằng cú pháp '@include' kèm theo tên của mixin. */ + +div { + @include center; + background-color: $primary-color; +} + +/* Được biên dịch thành: */ +div { + display: block; + margin-left: auto; + margin-right: auto; + left: 0; + right: 0; + background-color: #A3A4FF; +} + +/* Ta có thể dùng mixin để tạo nhanh các thuộc tính. */ + +@mixin size($width, $height) { + width: $width; + height: $height; +} + +/* Trong ví dụ này ta có thể tạo nhanh 2 thuộc tính width và height + bằng cách sử dụng mixin size và truyền vào tham số cho width và height. */ + +.rectangle { + @include size(100px, 60px); +} + +.square { + @include size(40px, 40px); +} + +/* Biên dịch thành: */ +.rectangle { + width: 100px; + height: 60px; +} + +.square { + width: 40px; + height: 40px; +} + + + +/* Function - Hàm +============================== */ + + + +/* Less cung cấp các hàm có thể được dùng để hoàn thành + các công việc khác nhau. */ + +/* Hàm được gọi sử dụng tên của nó và truyền vào + các tham số được yêu cầu. */ +body { + width: round(10.25px); +} + +.footer { + background-color: fade_out(#000000, 0.25); +} + +/* Biên dịch thành: */ + +body { + width: 10px; +} + +.footer { + background-color: rgba(0, 0, 0, 0.75); +} + +/* Ta có thể định nghĩa hàm mới. + hàm khá tương tự với mixin bởi chúng đều có thể được tái + sử dụng. Khi lựa chọn giữa việc sử dụng hàm hay mixin, + hãy nhớ mixin được tối ưu cho việc tạo ra CSS trong khi + hàm sẽ được sử dụng tốt hơn cho logic sẽ được sử dụng + xuyên suốt Less code. Các ví dụ trong phần 'Toán tử toán học' là ứng cử viên + sáng giá cho việc dùng hàm có thể tái sử dụng được. +*/ + +/* Hàm này sẽ tính độ tương đối giữa hai giá trị kích thước. */ + +@function calculate-percentage($target-size, $parent-size) { + @return $target-size / $parent-size * 100%; +} + +$main-content: calculate-percentage(600px, 960px); + +.main-content { + width: $main-content; +} + +.sidebar { + width: calculate-percentage(300px, 960px); +} + +/* Biên dịch thành: */ + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + + + +/* Mở rộng (Thừa kế) +============================== */ + + + +/* Mở rộng là cách để chia sẻ thuộc tính của một selector cho selector khác */ + +.display { + @include size(5em, 5em); + border: 5px solid $secondary-color; +} + +.display-success { + @extend .display; + border-color: #22df56; +} + +/* Biên dịch thành: */ +.display, .display-success { + width: 5em; + height: 5em; + border: 5px solid #51527F; +} + +.display-success { + border-color: #22df56; +} + +/* Nên mở rộng một khai báo CSS có trước thay vì tạo một mixin mới + bởi cách nó nhóm các lớp có chung một style gốc. + Nếu thực hiện với mixin, các thuộc tính sẽ bị trùng lặp + cho mỗi khai báo có sử dụng mixin. Mặc dù không ảnh hưởng đến luồng công việc nhưng nó + tạo ra các đoạn code CSS thừa sau khi được biên dịch. +*/ + + + +/* Nesting - Lồng +============================== */ + + + +/* Sass cho phép ta có thể lồng selector bên trong selector */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: #FF0000; + } +} + +/* Selector bắt đầu bằng ký tự '&' sẽ thay thế ký tự '&' + với selector cha. */ +/* Ta cũng có thể lồng các pseudo-class với nhau */ +/* Nên lưu ý không nên lồng quá nhiều lần sẽ làm code kém tính bảo trì. + Kinh nghiệm cho thấy không nên lồng quá 3 lần. + Ví dụ: */ + +ul { + list-style-type: none; + margin-top: 2em; + + li { + background-color: red; + + &:hover { + background-color: blue; + } + + a { + color: white; + } + } +} + +/* Biên dịch thành: */ + +ul { + list-style-type: none; + margin-top: 2em; +} + +ul li { + background-color: red; +} + +ul li:hover { + background-color: blue; +} + +ul li a { + color: white; +} + + + +/* Partials and Imports - Chia nhỏ thành tệp con và nhập vào +============================== */ + + +/* Less cho phép ta tạo các partial file (tệp con). + Sử dụng nó giúp ta có thể tổ chức code Less theo mô-đun có hệ thống. + Các tệp con thường bắt đầu với ký tự gạch dưới '_', vd: _reset.less + và được nhập vào file Less chính để được biên dịch thành CSS. + File con không được biên dịch thành file CSS riêng. */ + +/* Quan sát ví dụ sau, ta sẽ đặt đoạn code dưới đây vào tệp tên là _reset.less */ + +html, +body, +ul, +ol { + margin: 0; + padding: 0; +} + +/* Sass cung cấp cú pháp @import cho phép nhập các partial vào một file. + Cú pháp này trong Sass sẽ nhập các file và kết hợp chúng lại với + code CSS được sinh ra. Nó khác với cú pháp @import của CSS, + bản chất là tạo một HTTP request mới để tải về tệp tin được yêu cầu. */ + +@import 'reset'; + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + +/* Biên dịch thành: */ + +html, body, ul, ol { + margin: 0; + padding: 0; +} + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + + + +/* Placeholder Selectors - Selector trống +============================== */ + + + +/* Khai báo trống rất hữu dụng khi ta cần tạo một khai báo CSS cần được mở rộng. + Nếu bạn cần tạo một khai báo CSS gốc cho các lần mở rộng sau ta có thể + sử dụng một khai báo trống. Khai báo trống bắt đầu với kí tự '$' thay vì + sử dụng '.' hay '#'. Khai báo trống sẽ không xuất hiện trong code CSS được biên dịch. */ + +%content-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + @extend %content-window; + background-color: #0000ff; +} + +/* Biên dịch thành: */ + +.message-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + background-color: #0000ff; +} + + + +/* Toán tử toán học +============================== */ + + + +/* Sass cung cấp các toán tử sau: +, -, *, / và %. + Điều này rất có ích cho việc tính toán giá trị trực tiếp + trong tệp Sass thay vì phải tính toán thủ công. + Dưới đây là ví dụ về việc tạo một khung thiết kế đơn giản có hai cột. */ + +$content-area: 960px; +$main-content: 600px; +$sidebar-content: 300px; + +$main-size: $main-content / $content-area * 100%; +$sidebar-size: $sidebar-content / $content-area * 100%; +$gutter: 100% - ($main-size + $sidebar-size); + +body { + width: 100%; +} + +.main-content { + width: $main-size; +} + +.sidebar { + width: $sidebar-size; +} + +.gutter { + width: $gutter; +} + +/* Biên dịch thành: */ + +body { + width: 100%; +} + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + +.gutter { + width: 6.25%; +} + +``` + +## SASS hay Sass? +Bạn đã bao giờ thắc mắc liệu Sass có phải là từ viết tắt hay không? Nhiều nguwòi lầm tưởng nó là từ viết tắt nhưng thực chất tên của ngôn ngữ này lại là một từ - Sass. +Do sự lầm tưởng như vậy và mọi người thường xuyên viết nó là "SASS", người sáng lập ra ngôn ngữ này đã đặt một cái tên hài hước cho nó là "Syntactically Awesome StyleSheets" (Thiết lập style có cú pháp một cách tuyệt vời đáng kinh ngạc). + + +## Tập sử dụng Sass +Nếu bạn muốn thử dùng Sass trên trình duyệt, hãy ghé qua [SassMeister](http://sassmeister.com/). Bạn có thể dùng cả hai cú pháp, hoặc mở cài đặt và chọn Sass hoặc SCSS. + +## Tính tương thích +Sass có thể được dùng trong bất kì dự án nào miễn là ta có chương trình để biên dịch nó thành CSS. Ta cần chắc chắn rằng đoạn CSS đang dùng tương thích với các phiên bản trình duyệt mong muốn. + +[QuirksMode CSS](http://www.quirksmode.org/css/) và [CanIUse](http://caniuse.com) là nguồn thông tin tin cậy để kiểm tra tính tương thích của mã CSS. + + +## Tìm hiểu thêm +* [Tài liệu chính thức](http://sass-lang.com/documentation/file.SASS_REFERENCE.html) +* [The Sass Way](http://thesassway.com/) cung cấp các hướng dẫn từ cơ bản đến nâng cao cùng với các tin tức. diff --git a/vi-vn/typescript-vi.html.markdown b/vi-vn/typescript-vi.html.markdown new file mode 100644 index 00000000..ba459c11 --- /dev/null +++ b/vi-vn/typescript-vi.html.markdown @@ -0,0 +1,193 @@ +--- +language: TypeScript +contributors: + - ["Philippe Vlérick", "https://github.com/pvlerick"] +translators: + - ["Thanh Duy Phan", "https://github.com/thanhpd"] +filename: learntypescript-vi.ts +lang: vi-vn +--- + +TypeScript là ngôn ngữ được viết nhằm tinh giản quá trình phát triển ứng dụng quy mô lớn được viết bằng JavaScript. +TypeScript bổ sung thêm các khái niệm phổ biến như Class, Module, Interface, Generic và Static typing (tùy chọn) vào JavaScript. +Ngôn ngữ này là tập lớn hơn của JavaScript: tất cả code JavaScript đều là code TypeScript đúng nên nó có thể được thêm vào các dự án một cách nhanh chóng. Trình biên dịch TypeScript sẽ sinh ra JavaScript. + +Bài viết này sẽ chỉ tập trung tới các cú pháp bổ sung mà TypeScript thêm vào thay vì nói đến cả các cú pháp [JavaScript](javascript-vi.html.markdown). + +Để thử dùng TypeScript với trình biên dịch, đi đến [Sân chơi TypeScript](http://www.typescriptlang.org/play) nơi mà bạn có thể nhập code, sử dụng chức năng hỗ trợ tự hoàn thành code - autocompletion và trực tiếp quan sát mã JavaScript được sinh ra. + +```ts +// Đây là 3 khai báo kiểu biến cơ bản trong TypeScript +// (JavaScript chỉ có kiểu của giá trị, không có kiểu của biến) +let isDone: boolean = false; +let lines: number = 42; +let name: string = "Anders"; + +// Bạn có thể bỏ khai báo kiểu của biến nếu như nó đã được suy ra từ kiểu giá trị cơ bản +let isDone = false; +let lines = 42; +let name = "Anders"; + +// Có kiểu biến "any" tương thích với mọi kiểu của biến, +// được dùng khi ta không chắc chắn về kiểu của biến khi được khai báo +let notSure: any = 4; +notSure = "có thể là một biến kiểu string"; +notSure = false; // cũng có thể là biến kiểu boolean + +// Dùng từ khóa const cho khái báo biến không thay đổi (constant variable) +const numLivesForCat = 9; +numLivesForCat = 1; // Có lỗi! + +// Khi khai báo tập hợp ta có thể dùng mảng có kiểu được khai báo trước - typed array +let list: number[] = [1, 2, 3]; +// Ta cũng có thể sử dụng mảng kiểu chung - generic array +let list: Array<number> = [1, 2, 3]; + +// Để dùng enumeration - danh sách của một tập hợp: +enum Color { Red, Green, Blue }; +let c: Color = Color.Green; + +// Nếu function không trả về kết quả, sử dụng "void" cho kết quả trả về +function bigHorribleAlert(): void { + alert("I'm a little annoying box!"); +} + +// Function trong TypeScript là first-class citizen (tạm dịch: phần tử hạng nhất), hỗ trợ thao tác tới các thực thể khác +// (vd: truyền vào như tham số, được trả về từ function, chỉnh sửa, gán vào một biến) +// TypeScript hỗ trợ sử dụng function với cú pháp lambda (mũi tên) và suy luận kiểu trả về + +// Các cú pháp dưới đây tương đương với nhau, +// trình biên dịch sẽ tự nhận biết và sinh ra mã JavaScript giống nhau +let f1 = function (i: number): number { return i * i; } +// Kiểu trả về nếu không khai báo được tự suy diễn +let f2 = function (i: number) { return i * i; } +// Cú pháp mũi tên (arrow syntax) +let f3 = (i: number): number => { return i * i; } +// Cú pháp mũi tên với kiểu trả về được suy diễn +let f4 = (i: number) => { return i * i; } +// Cú pháp mũi tên với kiểu trả về được suy diễn +// khi không sử dụng dấu ngoặc nhọn {} thì không cần sử dụng return +let f5 = (i: number) => i * i; + +// Interface mang tính cấu trúc, mọi thứ có các đặc điểm (property) đều tương thích +interface IPerson { + name: string; + // Đặc điểm có thể tùy chọn bằng sử dụng dấu "?" + age?: number; + // Có thể sử dụng function + move(): void; +} + +// Object sử dụng interface IPerson nói trên +// có thể được coi là 1 thực thể Person vì nó có đặc điểm name và chức năng move +let p: Person = { name: "Bobby", move: () => { } }; +// Object sử dụng property tùy chọn +let validPerson: Person = { name: "Bobby", age: 42, move: () => { } }; +// Khai báo dưới đây gây lỗi vì giá trị đặc điểm age không mang kiểu number +let invalidPerson: Person = { name: "Bobby", age: true }; + +// Interface cũng có thể mô tả đặc tả của function +interface SearchFunc { + (source: string, subString: string): boolean; +} +// Chỉ có kiểu của tham số là quan trọng còn tên không quan trọng +let mySearch: SearchFunc; +mySearch = function (src: string, sub: string) { + return src.search(sub) != -1; +} + +// Class - các khai báo mặc định là public +class Point { + // Property + x: number; + + // Constructor - sử dụng tham số với từ khóa public/private + // sẽ tạo ra property tương ứng (ví dụ với property y) + // Có thể khai báo giá trị mặc định + + constructor(x: number, public y: number = 0) { + this.x = x; + } + + // Function + dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + + // Biến Static + static origin = new Point(0, 0); +} + +let p1 = new Point(10, 20); +let p2 = new Point(25); // y sử dụng giá trị mặc định là 0 + +// Thừa kế - Inheritance +class Point3D extends Point { + constructor(x: number, y: number, public z: number = 0) { + super(x, y); // Bắt buộc phải gọi constructor của class cha + } + + // Overwrite/Polymorphism - Ghi đè/Đa hình + dist() { + let d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } +} + +// module, "." có thể được dùng như những module con +module Geometry { + export class Square { + constructor(public sideLength: number = 0) { + } + area() { + return Math.pow(this.sideLength, 2); + } + } +} + +let s1 = new Geometry.Square(5); + +// Bí danh (alias) có thể được sử dụng để tham vấn module khác +import G = Geometry; + +let s2 = new G.Square(10); + +// Generic +// Class +class Tuple<T1, T2> { + constructor(public item1: T1, public item2: T2) { + } +} + +// Interface +interface Pair<T> { + item1: T; + item2: T; +} + +// Function +let pairToTuple = function <T>(p: Pair<T>) { + return new Tuple(p.item1, p.item2); +}; + +let tuple = pairToTuple({ item1: "hello", item2: "world" }); + +// Các thư viện viết bằng JavaScript thường đi kèm file định nghĩa kiểu để có thể sử dụng cho TypeScript +// Thêm vào tham vấn tới file định nghĩa: +/// <reference path="jquery.d.ts" /> + +// Template Strings - Chuỗi dạng mẫu (string sử dụng dấu `) +// String Interpolation - Nội suy chuỗi with với template string +let name = 'Tyrone'; +let greeting = `Chào ${name}, bạn khỏe không?` +// Chuỗi nhiều dòng với template string +let multiline = `Đây là ví dụ +cho chuỗi nhiều dòng`; + +``` + +## Tìm hiểu thêm + +* [Website TypeScript chính thức](http://www.typescriptlang.org/) +* [Đặc tả ngôn ngữ TypeScript] (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md) +* [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript) +* [Mã nguồn trên GitHub] (https://github.com/Microsoft/TypeScript) +* [Definitely Typed - repository for type definitions] (http://definitelytyped.org/) diff --git a/vim.html.markdown b/vim.html.markdown index 70f43be1..d5c4e865 100644 --- a/vim.html.markdown +++ b/vim.html.markdown @@ -8,18 +8,20 @@ filename: LearnVim.txt [Vim](http://www.vim.org) -(Vi IMproved) is a clone of the popular vi editor for Unix. It is a text -editor designed for speed and increased productivity, and is ubiquitous in most -unix-based systems. It has numerous keybindings for speedy navigation to +(Vi IMproved) is a clone of the popular vi editor for Unix. It is a text +editor designed for speed and increased productivity, and is ubiquitous in most +unix-based systems. It has numerous keybindings for speedy navigation to specific points in the file, and for fast editing. ## Basics of navigating Vim ``` vim <filename> # Open <filename> in vim + :help <topic> # Open up built-in help docs about <topic> if any exists :q # Quit vim :w # Save current file :wq # Save file and quit vim + ZZ # Save file and quit vim :q! # Quit vim without saving file # ! *forces* :q to execute, hence quiting vim without saving :x # Save file and quit vim, shorter version of :wq @@ -40,23 +42,24 @@ specific points in the file, and for fast editing. # Searching in the text - /word # Highlights all occurences of word after cursor - ?word # Highlights all occurences of word before cursor - n # Moves cursor to next occurence of word after search + /word # Highlights all occurrences of word after cursor + ?word # Highlights all occurrences of word before cursor + n # Moves cursor to next occurrence of word after search N # Moves cursor to previous occerence of word :%s/foo/bar/g # Change 'foo' to 'bar' on every line in the file :s/foo/bar/g # Change 'foo' to 'bar' on the current line + :%s/\n/\r/g # Replace new line characters with new line characters # Jumping to characters f<character> # Jump forward and land on <character> - t<character> # Jump forward and land right before <character> + t<character> # Jump forward and land right before <character> - # For example, + # For example, f< # Jump forward and land on < t< # Jump forward and land right before < - + # Moving by word w # Move forward by one word @@ -73,19 +76,28 @@ specific points in the file, and for fast editing. L # Move to the bottom of the screen ``` +## Help docs: + +Vim has built in help documentation that can accessed with `:help <topic>`. +For example `:help navigation` will pull up documentation about how to navigate +your workspace! + +`:help` can also be used without an option. This will bring up a default help dialog +that aims to make getting started with vim more approachable! + ## Modes: Vim is based on the concept on **modes**. -Command Mode - vim starts up in this mode, used to navigate and write commands -Insert Mode - used to make changes in your file -Visual Mode - used to highlight text and do operations to them +Command Mode - vim starts up in this mode, used to navigate and write commands +Insert Mode - used to make changes in your file +Visual Mode - used to highlight text and do operations to them Ex Mode - used to drop down to the bottom with the ':' prompt to enter commands ``` i # Puts vim into insert mode, before the cursor position a # Puts vim into insert mode, after the cursor position - v # Puts vim into visual mode + v # Puts vim into visual mode : # Puts vim into ex mode <esc> # 'Escapes' from whichever mode you're in, into Command mode @@ -102,18 +114,18 @@ Ex Mode - used to drop down to the bottom with the ':' prompt to enter comm ## The 'Grammar' of vim -Vim can be thought of as a set of commands in a +Vim can be thought of as a set of commands in a 'Verb-Modifier-Noun' format, where: -Verb - your action -Modifier - how you're doing your action +Verb - your action +Modifier - how you're doing your action Noun - the object on which your action acts on A few important examples of 'Verbs', 'Modifiers', and 'Nouns': ``` # 'Verbs' - + d # Delete c # Change y # Yank (copy) @@ -135,7 +147,7 @@ A few important examples of 'Verbs', 'Modifiers', and 'Nouns': s # Sentence p # Paragraph b # Block - + # Sample 'sentences' or commands d2w # Delete 2 words @@ -157,6 +169,20 @@ A few important examples of 'Verbs', 'Modifiers', and 'Nouns': ddp # Swap position of consecutive lines, dd then p . # Repeat previous action :w !sudo tee % # Save the current file as root + :set syntax=c # Set syntax highlighting to 'c' + :sort # Sort all lines + :sort! # Sort all lines in reverse + :sort u # Sort all lines and remove duplicates + ~ # Toggle letter case of selected text + u # Selected text to lower case + U # Selected text to upper case + + # Fold text + zf # Create fold from selected text + zo # Open current fold + zc # Close current fold + zR # Open all folds + zM # Close all folds ``` ## Macros @@ -180,7 +206,7 @@ Here's a sample ~/.vimrc file: ``` " Example ~/.vimrc -" 2015.10 +" 2015.10 " Required for vim to be iMproved set nocompatible diff --git a/visualbasic.html.markdown b/visualbasic.html.markdown index f081b907..63f224b7 100644 --- a/visualbasic.html.markdown +++ b/visualbasic.html.markdown @@ -5,14 +5,14 @@ contributors: filename: learnvisualbasic.vb --- -```vb +``` Module Module1 Sub Main() 'A Quick Overview of Visual Basic Console Applications before we dive 'in to the deep end. 'Apostrophe starts comments. - 'To Navigate this tutorial within the Visual Basic Complier, I've put + 'To Navigate this tutorial within the Visual Basic Compiler, I've put 'together a navigation system. 'This navigation system is explained however as we go deeper into this 'tutorial, you'll understand what it all means. diff --git a/whip.html.markdown b/whip.html.markdown index e7e5e427..c692714a 100644 --- a/whip.html.markdown +++ b/whip.html.markdown @@ -3,6 +3,7 @@ language: whip contributors: - ["Tenor Biel", "http://github.com/L8D"] - ["Saurabh Sandav", "http://github.com/SaurabhSandav"] + - ["Paulo Henrique Rodrigues Pinheiro", "https://github.com/paulohrpinheiro"] author: Tenor Biel author_url: http://github.com/L8D filename: whip.lisp @@ -232,6 +233,7 @@ undefined ; user to indicate a value that hasn't been set (words "foobar nachos cheese") ; => ("foobar" "nachos" "cheese") ; Join list of strings together. (unwords ("foo" "bar")) ; => "foobar" +; Successor and Predecessor (pred 21) ; => 20 (succ 20) ; => 21 ``` diff --git a/yaml.html.markdown b/yaml.html.markdown index 95adbd83..52658453 100644 --- a/yaml.html.markdown +++ b/yaml.html.markdown @@ -2,8 +2,8 @@ language: yaml filename: learnyaml.yaml contributors: - - ["Adam Brenecki", "https://github.com/adambrenecki"] - - ["Suhas SG", "https://github.com/jargnar"] +- [Adam Brenecki, 'https://github.com/adambrenecki'] +- [Suhas SG, 'https://github.com/jargnar'] --- YAML is a data serialisation language designed to be directly writable and @@ -11,7 +11,7 @@ readable by humans. It's a strict superset of JSON, with the addition of syntactically significant newlines and indentation, like Python. Unlike Python, however, -YAML doesn't allow literal tab characters at all. +YAML doesn't allow literal tab characters for indentation. ```yaml # Comments in YAML look like this. @@ -27,13 +27,15 @@ another_key: Another value goes here. a_number_value: 100 scientific_notation: 1e+12 # The number 1 will be interpreted as a number, not a boolean. if you want -# it to be intepreted as a boolean, use true +# it to be interpreted as a boolean, use true boolean: true null_value: null key with spaces: value # Notice that strings don't need to be quoted. However, they can be. -however: "A string, enclosed in quotes." -"Keys can be quoted too.": "Useful if you want to put a ':' in your key." +however: 'A string, enclosed in quotes.' +'Keys can be quoted too.': "Useful if you want to put a ':' in your key." +single quotes: 'have ''one'' escape pattern' +double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more." # Multiple-line strings can be written either as a 'literal block' (using |), # or a 'folded block' (using '>'). @@ -59,12 +61,12 @@ folded_style: > # COLLECTION TYPES # #################### -# Nesting is achieved by indentation. +# Nesting uses indentation. 2 space indent is preferred (but not required). a_nested_map: - key: value - another_key: Another Value - another_nested_map: - hello: hello + key: value + another_key: Another Value + another_nested_map: + hello: hello # Maps don't have to have string keys. 0.25: a float key @@ -72,8 +74,8 @@ a_nested_map: # Keys can also be complex, like multi-line objects # We use ? followed by a space to indicate the start of a complex key. ? | - This is a key - that has multiple lines + This is a key + that has multiple lines : and this is its value # YAML also allows mapping between sequences with the complex key syntax @@ -83,22 +85,26 @@ a_nested_map: - Real Madrid : [ 2001-01-01, 2002-02-02 ] -# Sequences (equivalent to lists or arrays) look like this: +# Sequences (equivalent to lists or arrays) look like this +# (note that the '-' counts as indentation): a_sequence: - - Item 1 - - Item 2 - - 0.5 # sequences can contain disparate types. - - Item 4 - - key: value - another_key: another_value - - - - This is a sequence - - inside another sequence +- Item 1 +- Item 2 +- 0.5 # sequences can contain disparate types. +- Item 4 +- key: value + another_key: another_value +- + - This is a sequence + - inside another sequence +- - - Nested sequence indicators + - can be collapsed # Since YAML is a superset of JSON, you can also write JSON-style maps and # sequences: json_map: {"key": "value"} json_seq: [3, 2, 1, "takeoff"] +and quotes are optional: {key: [3, 2, 1, takeoff]} ####################### # EXTRA YAML FEATURES # @@ -111,15 +117,15 @@ other_anchor: *anchor_name # Anchors can be used to duplicate/inherit properties base: &base - name: Everyone has same name + name: Everyone has same name foo: &foo - <<: *base - age: 10 + <<: *base + age: 10 bar: &bar - <<: *base - age: 20 + <<: *base + age: 20 # foo and bar would also have name: Everyone has same name @@ -132,7 +138,7 @@ python_complex_number: !!python/complex 1+2j # We can also use yaml complex keys with language specific tags ? !!python/tuple [5, 7] : Fifty Seven -# Would be {(5, 7): 'Fifty Seven'} in python +# Would be {(5, 7): 'Fifty Seven'} in Python #################### # EXTRA YAML TYPES # @@ -147,22 +153,23 @@ date: 2002-12-14 # The !!binary tag indicates that a string is actually a base64-encoded # representation of a binary blob. gif_file: !!binary | - R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 - OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ - +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC - AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= + R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 + OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ + +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC + AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= # YAML also has a set type, which looks like this: set: - ? item1 - ? item2 - ? item3 + ? item1 + ? item2 + ? item3 +or: {item1, item2, item3} # Like Python, sets are just maps with null values; the above is equivalent to: set2: - item1: null - item2: null - item3: null + item1: null + item2: null + item3: null ``` ### More Resources diff --git a/zfs.html.markdown b/zfs.html.markdown index 3d033da9..fad6b62d 100644 --- a/zfs.html.markdown +++ b/zfs.html.markdown @@ -149,6 +149,7 @@ $ zpool destroy test ### Datasets Actions: + * Create * List * Rename @@ -269,6 +270,7 @@ ZFS snapshots are one of the things about zfs that are a really big deal * They are easy to automate. Actions: + * Create * Delete * Rename diff --git a/zh-cn/bf-cn.html.markdown b/zh-cn/bf-cn.html.markdown index 6cea3012..2d2a114a 100644 --- a/zh-cn/bf-cn.html.markdown +++ b/zh-cn/bf-cn.html.markdown @@ -1,11 +1,13 @@ --- language: bf -lang: zh-cn +filename: brainfuck-cn.bf contributors: - ["Prajit Ramachandran", "http://prajitr.github.io/"] - ["Mathias Bynens", "http://mathiasbynens.be/"] translators: - ["lyuehh", "https://github.com/lyuehh"] +lang: zh-cn + --- Brainfuck 是一个极小的只有8个指令的图灵完全的编程语言。 diff --git a/zh-cn/c-cn.html.markdown b/zh-cn/c-cn.html.markdown index 1e10416e..02ec7f7b 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/crystal-cn.html.markdown b/zh-cn/crystal-cn.html.markdown new file mode 100644 index 00000000..14805114 --- /dev/null +++ b/zh-cn/crystal-cn.html.markdown @@ -0,0 +1,567 @@ +--- +language: crystal +filename: learncrystal-cn.cr +contributors: + - ["Vitalii Elenhaupt", "http://veelenga.com"] + - ["Arnaud Fernandés", "https://github.com/TechMagister/"] +translators: + - ["Xuty", "https://github.com/xtyxtyx"] +lang: zh-cn +--- + +```crystal + +# 这是一行注释 + +# 一切都是对象(object) +nil.class #=> Nil +100.class #=> Int32 +true.class #=> Bool + +# nil, false 以及空指针是假值(falsey values) +!nil #=> true : Bool +!false #=> true : Bool +!0 #=> false : Bool + +# 整数类型 + +1.class #=> Int32 + +# 四种有符号整数 +1_i8.class #=> Int8 +1_i16.class #=> Int16 +1_i32.class #=> Int32 +1_i64.class #=> Int64 + +# 四种无符号整数 +1_u8.class #=> UInt8 +1_u16.class #=> UInt16 +1_u32.class #=> UInt32 +1_u64.class #=> UInt64 + +2147483648.class #=> Int64 +9223372036854775808.class #=> UInt64 + +# 二进制数 +0b1101 #=> 13 : Int32 + +# 八进制数 +0o123 #=> 83 : Int32 + +# 十六进制数 +0xFE012D #=> 16646445 : Int32 +0xfe012d #=> 16646445 : Int32 + +# 浮点数类型 + +1.0.class #=> Float64 + +# Crystal中有两种浮点数 +1.0_f32.class #=> Float32 +1_f32.class #=> Float32 + +1e10.class #=> Float64 +1.5e10.class #=> Float64 +1.5e-7.class #=> Float64 + +# 字符类型 + +'a'.class #=> Char + +# 八进制字符 +'\101' #=> 'A' : Char + +# Unicode字符 +'\u0041' #=> 'A' : Char + +# 字符串 + +"s".class #=> String + +# 字符串不可变(immutable) +s = "hello, " #=> "hello, " : String +s.object_id #=> 134667712 : UInt64 +s += "Crystal" #=> "hello, Crystal" : String +s.object_id #=> 142528472 : UInt64 + +# 支持字符串插值(interpolation) +"sum = #{1 + 2}" #=> "sum = 3" : String + +# 多行字符串 +"这是一个 + 多行字符串" + +# 书写带有引号的字符串 +%(hello "world") #=> "hello \"world\"" + +# 符号类型 +# 符号是不可变的常量,本质上是Int32类型 +# 符号通常被用来代替字符串,来高效地传递特定的值 + +:symbol.class #=> Symbol + +sentence = :question? # :"question?" : Symbol + +sentence == :question? #=> true : Bool +sentence == :exclamation! #=> false : Bool +sentence == "question?" #=> false : Bool + +# 数组类型(Array) + +[1, 2, 3].class #=> Array(Int32) +[1, "hello", 'x'].class #=> Array(Int32 | String | Char) + +# 必须为空数组指定类型 +[] # Syntax error: for empty arrays use '[] of ElementType' +[] of Int32 #=> [] : Array(Int32) +Array(Int32).new #=> [] : Array(Int32) + +# 数组可以通过下标访问 +array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] : Array(Int32) +array[0] #=> 1 : Int32 +array[10] # raises IndexError +array[-6] # raises IndexError +array[10]? #=> nil : (Int32 | Nil) +array[-6]? #=> nil : (Int32 | Nil) + +# 使用负位置编号,从后往前访问数组 +array[-1] #=> 5 + +# With a start index and size +# 使用起始位置编号+大小 +array[2, 3] #=> [3, 4, 5] + +# 使用范围(range)访问数组 +array[1..3] #=> [2, 3, 4] + +# 向尾部添加元素 +array << 6 #=> [1, 2, 3, 4, 5, 6] + +# 删除尾部元素 +array.pop #=> 6 +array #=> [1, 2, 3, 4, 5] + +# 删除首部元素 +array.shift #=> 1 +array #=> [2, 3, 4, 5] + +# 检查元素是否存在与数组之中 +array.includes? 3 #=> true + +# 一种特殊语法,用来创建字符串数组或符号数组 +%w(one two three) #=> ["one", "two", "three"] : Array(String) +%i(one two three) #=> [:one, :two, :three] : Array(Symbol) + +# 对于定义了`new`和`#<<`方法的类,可以用以下语法创建新对象 +set = Set{1, 2, 3} #=> [1, 2, 3] +set.class #=> Set(Int32) + +# 以下代码与上方等同 +set = Set(typeof(1, 2, 3)).new +set << 1 +set << 2 +set << 3 + +# 哈希表类型(Hash) + +{1 => 2, 3 => 4}.class #=> Hash(Int32, Int32) +{1 => 2, 'a' => 3}.class #=> Hash(Int32 | Char, Int32) + +# 必须为空哈希表指定类型 +{} # Syntax error +{} of Int32 => Int32 # {} +Hash(Int32, Int32).new # {} + +# 可以使用键(key)快速查询哈希表 +hash = {"color" => "green", "number" => 5} +hash["color"] #=> "green" +hash["no_such_key"] #=> Missing hash key: "no_such_key" (KeyError) +hash["no_such_key"]? #=> nil + +# 检查某一键哈希表中是否存在 +hash.has_key? "color" #=> true + +# 对于定义了`#[]=`方法的类,可以使用以下语法创建对象 +class MyType + def []=(key, value) + puts "do stuff" + end +end + +MyType{"foo" => "bar"} + +# 以上与下列代码等同 +tmp = MyType.new +tmp["foo"] = "bar" +tmp + +# 范围类型(Range) + +1..10 #=> Range(Int32, Int32) +Range.new(1, 10).class #=> Range(Int32, Int32) + +# 包含或不包含端点 +(3..5).to_a #=> [3, 4, 5] +(3...5).to_a #=> [3, 4] + +# 检查某一值是否在范围内 +(1..8).includes? 2 #=> true + +# 元组类型(Tuple) + +# 元组类型尺寸固定,不可变,储存在栈中 +# 元组可以有不同类型的对象组成 +{1, "hello", 'x'}.class #=> Tuple(Int32, String, Char) + +# 使用下标访问元组 +tuple = {:key1, :key2} +tuple[1] #=> :key2 +tuple[2] #=> syntax error : Index out of bound + +# 将元组中的元素赋值给变量 +a, b, c = {:a, 'b', "c"} +a #=> :a +b #=> 'b' +c #=> "c" + +# 命名元组类型(NamedTuple) + +tuple = {name: "Crystal", year: 2011} # NamedTuple(name: String, year: Int32) +tuple[:name] # => "Crystal" (String) +tuple[:year] # => 2011 (Int32) + +# 命名元组的键可以是字符串常量 +{"this is a key": 1} # => NamedTuple("this is a key": Int32) + +# 过程类型(Proc) +# 过程代表一个函数指针,以及可选的上下文(闭包) +# 过程通常使用字面值创建 +proc = ->(x : Int32) { x.to_s } +proc.class # Proc(Int32, String) + +# 或者使用`new`方法创建 +Proc(Int32, String).new { |x| x.to_s } + +# 使用`call`方法调用过程 +proc.call 10 #=> "10" + +# 控制语句(Control statements) + +if true + "if 语句" +elsif false + "else-if, 可选" +else + "else, 同样可选" +end + +puts "可以将if后置" if true + +# 将if作为表达式 +a = if 2 > 1 + 3 + else + 4 + end + +a #=> 3 + +# 条件表达式 +a = 1 > 2 ? 3 : 4 #=> 4 + +# `case`语句 +cmd = "move" + +action = case cmd + when "create" + "Creating..." + when "copy" + "Copying..." + when "move" + "Moving..." + when "delete" + "Deleting..." +end + +action #=> "Moving..." + +# 循环 +index = 0 +while index <= 3 + puts "Index: #{index}" + index += 1 +end +# Index: 0 +# Index: 1 +# Index: 2 +# Index: 3 + +index = 0 +until index > 3 + puts "Index: #{index}" + index += 1 +end +# Index: 0 +# Index: 1 +# Index: 2 +# Index: 3 + +# 更好的做法是使用`each` +(0..3).each do |index| + puts "Index: #{index}" +end +# Index: 0 +# Index: 1 +# Index: 2 +# Index: 3 + +# 变量的类型取决于控制语句中表达式的类型 +if a < 3 + a = "hello" +else + a = true +end +typeof a #=> (Bool | String) + +if a && b + # 此处`a`与`b`均为Nil +end + +if a.is_a? String + a.class #=> String +end + +# 函数(Functions) + +def double(x) + x * 2 +end + +# 函数(以及所有代码块)均将最末尾表达式的值作为返回值 +double(2) #=> 4 + +# 在没有歧义的情况下,括号可以省略 +double 3 #=> 6 + +double double 3 #=> 12 + +def sum(x, y) + x + y +end + +# 使用逗号分隔参数 +sum 3, 4 #=> 7 + +sum sum(3, 4), 5 #=> 12 + +# yield +# 所有函数都有一个默认生成、可选的代码块(block)参数 +# 在函数中可以使用yield调用此代码块 + +def surround + puts '{' + yield + puts '}' +end + +surround { puts "hello world" } + +# { +# hello world +# } + + +# 可将代码块作为参数传给函数 +# "&" 表示对代码块参数的引用 +def guests(&block) + block.call "some_argument" +end + +# 使用星号"*"将参数转换成元组 +def guests(*array) + array.each { |guest| puts guest } +end + +# 如果函数返回数组,可以将其解构 +def foods + ["pancake", "sandwich", "quesadilla"] +end +breakfast, lunch, dinner = foods +breakfast #=> "pancake" +dinner #=> "quesadilla" + +# 按照约定,所有返回布尔值的方法都以问号结尾 +5.even? # false +5.odd? # true + +# 以感叹号结尾的方法,都有一些破坏性(destructive)行为,比如改变调用接收者(receiver) +# 对于某些方法,带有感叹号的版本将改变调用接收者,而不带有感叹号的版本返回新值 +company_name = "Dunder Mifflin" +company_name.gsub "Dunder", "Donald" #=> "Donald Mifflin" +company_name #=> "Dunder Mifflin" +company_name.gsub! "Dunder", "Donald" +company_name #=> "Donald Mifflin" + + +# 使用`class`关键字来定义类(class) +class Human + + # 类变量,由类的所有实例所共享 + @@species = "H. sapiens" + + # `name`的类型为`String` + @name : String + + # 构造器方法(initializer) + # 其中@name、@age为简写,相当于 + # + # def initialize(name, age = 0) + # @name = name + # @age = age + # end + # + # `age`为可选参数,如果未指定,则使用默认值0 + def initialize(@name, @age = 0) + end + + # @name的setter方法 + def name=(name) + @name = name + end + + # @name的getter方法 + def name + @name + end + + # 上述getter与setter的定义可以用property宏简化 + property :name + + # 也可用getter与setter宏独立创建getter与setter + getter :name + setter :name + + # 此处的`self.`使`say`成为类方法 + def self.say(msg) + puts msg + end + + def species + @@species + end +end + + +# 将类实例化 +jim = Human.new("Jim Halpert") + +dwight = Human.new("Dwight K. Schrute") + +# 调用一些实例方法 +jim.species #=> "H. sapiens" +jim.name #=> "Jim Halpert" +jim.name = "Jim Halpert II" #=> "Jim Halpert II" +jim.name #=> "Jim Halpert II" +dwight.species #=> "H. sapiens" +dwight.name #=> "Dwight K. Schrute" + +# 调用类方法 +Human.say("Hi") #=> 输出 Hi ,返回 nil + +# 带有`@`前缀的变量为实例变量 +class TestClass + @var = "I'm an instance var" +end + +# 带有`@@`前缀的变量为类变量 +class TestClass + @@var = "I'm a class var" +end +# 首字母大写的变量为常量 +Var = "这是一个常量" +Var = "无法再次被赋值" # 常量`Var`已经被初始化 + +# 在crystal中类也是对象(object),因此类也有实例变量(instance variable) +# 类变量的定义由类以及类的派生类所共有,但类变量的值是独立的 + +# 基类 +class Human + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + +# 派生类 +class Worker < Human +end + +Human.foo #=> 0 +Worker.foo #=> 0 + +Human.foo = 2 #=> 2 +Worker.foo #=> 0 + +Worker.foo = 3 #=> 3 +Human.foo #=> 2 +Worker.foo #=> 3 + +module ModuleExample + def foo + "foo" + end +end + +# include <Module> 将模块(module)中的方法添加为实例方法 +# extend <Module> 将模块中的方法添加为类方法 + +class Person + include ModuleExample +end + +class Book + extend ModuleExample +end + +Person.foo # => undefined method 'foo' for Person:Class +Person.new.foo # => 'foo' +Book.foo # => 'foo' +Book.new.foo # => undefined method 'foo' for Book + + +# 异常处理 + +# 定义新的异常类(exception) +class MyException < Exception +end + +# 再定义一个异常类 +class MyAnotherException < Exception; end + +ex = begin + raise MyException.new +rescue ex1 : IndexError + "ex1" +rescue ex2 : MyException | MyAnotherException + "ex2" +rescue ex3 : Exception + "ex3" +rescue ex4 # 捕捉任何类型的异常 + "ex4" +end + +ex #=> "ex2" + +``` + +## 参考资料 + +- [官方网站](https://crystal-lang.org/) +- [官方文档](https://crystal-lang.org/docs/overview/) +- [在线运行代码](https://play.crystal-lang.org/#/cr) +- [Github仓库](https://github.com/crystal-lang/crystal) diff --git a/zh-cn/dart-cn.html.markdown b/zh-cn/dart-cn.html.markdown index 6a6562bc..b0287f0c 100644 --- a/zh-cn/dart-cn.html.markdown +++ b/zh-cn/dart-cn.html.markdown @@ -492,8 +492,8 @@ main() { Dart 有一个综合性网站。它涵盖了 API 参考、入门向导、文章以及更多, 还包括一个有用的在线试用 Dart 页面。 -http://www.dartlang.org/ -http://try.dartlang.org/ +* [https://www.dartlang.org](https://www.dartlang.org) +* [https://try.dartlang.org](https://try.dartlang.org) diff --git a/zh-cn/elisp-cn.html.markdown b/zh-cn/elisp-cn.html.markdown index 06f38d77..3f6ccbcf 100644 --- a/zh-cn/elisp-cn.html.markdown +++ b/zh-cn/elisp-cn.html.markdown @@ -293,7 +293,7 @@ lang: zh-cn (hello-to-bonjour)
-;; 给这些名字上个色:
+;; 给这些名字加粗:
(defun boldify-names ()
(switch-to-buffer-other-window "*test*")
@@ -340,5 +340,6 @@ lang: zh-cn ;; - Kevin Montuori
;; - Arne Babenhauserheide
;; - Alan Schmitt
+;; - spacegoing
```
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/go-cn.html.markdown b/zh-cn/go-cn.html.markdown index 75498367..37b4b137 100644 --- a/zh-cn/go-cn.html.markdown +++ b/zh-cn/go-cn.html.markdown @@ -142,6 +142,7 @@ func learnTypes() { func learnNamedReturns(x, y int) (z int) { z = x * y return // z is implicit here, because we named it earlier. +} // Go全面支持垃圾回收。Go有指针,但是不支持指针运算。 // 你会因为空指针而犯错,但是不会因为增加指针而犯错。 diff --git a/zh-cn/groovy-cn.html.markdown b/zh-cn/groovy-cn.html.markdown index 562a0284..0e7a020c 100644 --- a/zh-cn/groovy-cn.html.markdown +++ b/zh-cn/groovy-cn.html.markdown @@ -219,10 +219,12 @@ for (i in array) { //遍历映射 def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] -x = 0 +x = "" for ( e in map ) { x += e.value + x += " " } +assert x.equals("Roberto Grails Groovy ") /* 运算符 diff --git a/zh-cn/java-cn.html.markdown b/zh-cn/java-cn.html.markdown index 1e9c38f6..27003f3e 100644 --- a/zh-cn/java-cn.html.markdown +++ b/zh-cn/java-cn.html.markdown @@ -108,7 +108,7 @@ public class LearnJava { boolean [] booleanArray = new boolean[100]; // 声明并初始化数组也可以这样: - int [] y = {9000, 1000, 1337}; + int [] intArray = {9000, 1000, 1337}; // 随机访问数组中的元素 System.out.println("intArray @ 0: " + intArray[0]); @@ -309,7 +309,7 @@ class Bicycle { name = "Bontrager"; } - // 一下是一个含有参数的构造函数 + // 以下是一个含有参数的构造函数 public Bicycle(int startCadence, int startSpeed, int startGear, String name) { this.gear = startGear; this.cadence = startCadence; diff --git a/zh-cn/javascript-cn.html.markdown b/zh-cn/javascript-cn.html.markdown index bdef0099..360f7c65 100644 --- a/zh-cn/javascript-cn.html.markdown +++ b/zh-cn/javascript-cn.html.markdown @@ -12,12 +12,9 @@ translators: lang: zh-cn --- -Javascript于1995年由网景公司的Brendan Eich发明。 -最初发明的目的是作为一个简单的网站脚本语言,来作为 -复杂网站应用java的补充。但由于它与网页结合度很高并且由浏览器内置支持, -所以javascript变得比java在前端更为流行了。 +Javascript 于 1995 年由网景公司的 Brendan Eich 发明。最初它作为一种简单的,用于开发网站的脚本语言而被发明出来,是用于开发复杂网站的 Java 的补充。但由于它与网页结合度很高并且在浏览器中得到内置的支持,所以在网页前端领域 Javascript 变得比 Java 更流行了。 -不过 JavaScript 可不仅仅只用于浏览器: Node.js,一个基于Google Chrome V8引擎的独立运行时环境,也越来越流行。 +不过,Javascript 不仅用于网页浏览器,一个名为 Node.js 的项目提供了面向 Google Chrome V8 引擎的独立运行时环境,它正在变得越来越流行。 很欢迎来自您的反馈,您可以通过下列方式联系到我: [@adambrenecki](https://twitter.com/adambrenecki), 或者 diff --git a/zh-cn/kotlin-cn.html.markdown b/zh-cn/kotlin-cn.html.markdown index 5d655029..f6dcd847 100644 --- a/zh-cn/kotlin-cn.html.markdown +++ b/zh-cn/kotlin-cn.html.markdown @@ -22,7 +22,7 @@ package com.learnxinyminutes.kotlin /* Kotlin程序的入口点是一个"main"函数 -该函数传递一个包含任何命令行参数的数组。 +该函数传递一个包含所有命令行参数的数组。 */ fun main(args: Array<String>) { /* @@ -67,10 +67,10 @@ fun helloWorld(val name : String) { 模板表达式从一个美元符号($)开始。 */ val fooTemplateString = "$fooString has ${fooString.length} characters" - println(fooTemplateString) + println(fooTemplateString) // => 输出 My String Is Here! has 18 characters /* - 当某个变量的值可以为 null 的时候,我们必须被明确指定它是可为空的。 + 当某个变量的值可以为 null 的时候,我们必须明确指定它是可为空的。 在变量声明处的类型后面加上?来标识它是可为空的。 我们可以用?.操作符来访问可为空的变量。 我们可以用?:操作符来指定一个在变量为空时使用的替代值。 @@ -96,24 +96,24 @@ fun helloWorld(val name : String) { println(hello()) // => Hello, world! /* - 用"vararg"关键字来修饰一个函数的参数来允许可变参数传递给该函数 + 函数的可变参数可使用 "vararg" 关键字来修饰 */ fun varargExample(vararg names: Int) { println("Argument has ${names.size} elements") } - varargExample() // => Argument has 0 elements - varargExample(1) // => Argument has 1 elements - varargExample(1, 2, 3) // => Argument has 3 elements + varargExample() // => 传入 0 个参数 + varargExample(1) // => 传入 1 个参数 + varargExample(1, 2, 3) // => 传入 3 个参数 /* - 当函数只包含一个单独的表达式时,大括号可以被省略。 - 函数体可以被指定在一个=符号后面。 + 当函数只包含一个单独的表达式时,大括号可以省略。 + 函数体可以写在一个=符号后面。 */ fun odd(x: Int): Boolean = x % 2 == 1 println(odd(6)) // => false println(odd(7)) // => true - // 如果返回值类型可以被推断,那么我们不需要指定它。 + // 如果返回值类型可以推断,那么我们不需要指定它。 fun even(x: Int) = x % 2 == 0 println(even(6)) // => true println(even(7)) // => false @@ -122,15 +122,14 @@ fun helloWorld(val name : String) { fun not(f: (Int) -> Boolean) : (Int) -> Boolean { return {n -> !f.invoke(n)} } - // 命名函数可以用::运算符被指定为参数。 + // 普通函数可以用::运算符传入引用作为函数参数。 val notOdd = not(::odd) val notEven = not(::even) - // 匿名函数可以被指定为参数。 + // lambda 表达式可以直接作为参数传递。 val notZero = not {n -> n == 0} /* - 如果一个匿名函数只有一个参数 - 那么它的声明可以被省略(连同->)。 - 这个参数的名字是"it"。 + 如果一个 lambda 表达式只有一个参数 + 那么它的声明可以省略(连同->),内部以 "it" 引用。 */ val notPositive = not {it > 0} for (i in 0..4) { @@ -152,7 +151,7 @@ fun helloWorld(val name : String) { 注意,Kotlin没有"new"关键字。 */ val fooExampleClass = ExampleClass(7) - // 可以使用一个点号来调用成员函数。 + // 可以使用一个点号来调用成员方法。 println(fooExampleClass.memberFunction(4)) // => 11 /* 如果使用"infix"关键字来标记一个函数 @@ -162,7 +161,7 @@ fun helloWorld(val name : String) { /* 数据类是创建只包含数据的类的一个简洁的方法。 - "hashCode"、"equals"和"toString"方法将被自动生成。 + "hashCode"、"equals"和"toString"方法将自动生成。 */ data class DataClassExample (val x: Int, val y: Int, val z: Int) val fooData = DataClassExample(1, 2, 4) diff --git a/zh-cn/livescript-cn.html.markdown b/zh-cn/livescript-cn.html.markdown index fea00bc1..cc7daab1 100644 --- a/zh-cn/livescript-cn.html.markdown +++ b/zh-cn/livescript-cn.html.markdown @@ -1,6 +1,6 @@ --- language: LiveScript -filename: learnLivescript.ls +filename: learnLivescript-cn.ls contributors: - ["Christina Whyte", "http://github.com/kurisuwhyte/"] translators: diff --git a/zh-cn/make-cn.html.markdown b/zh-cn/make-cn.html.markdown new file mode 100644 index 00000000..4cdf1e63 --- /dev/null +++ b/zh-cn/make-cn.html.markdown @@ -0,0 +1,262 @@ +--- +language: make +contributors: +- ["Robert Steed", "https://github.com/robochat"] +- ["Jichao Ouyang", "https://github.com/jcouyang"] +translators: +- ["Jichao Ouyang", "https://github.com/jcouyang"] +filename: Makefile-cn +lang: zh-cn +--- + +Makefile 用于定义如何创建目标文件, 比如如何从源码到可执行文件. 创建这一工具的目标是 +减少不必要的编译或者任务.是传说中的 Stuart Feldman 在 1976 年花了一个周末写出来的, +而今仍然使用广泛, 特别是在 Unix 和 Linux 系统上. + +虽然每个语言可能都有相应的或多或少提供 make 的功能, 比如 ruby 的 rake, node 的 gulp, broccoli +, scala 的 sbt 等等. 但是 make 的简洁与高效, 和只做一件事并做到极致的风格, 使其至今仍是无可替代的, +甚至与其他构建工具一起使用也并无冲突. + +尽管有许多的分支和变体, 这篇文章针对是标准的 GNU make. + +```make +# 这行表示注释 + +# 文件名一定要交 Makefile, 大小写区分, 使用 `make <target>` 生成 target +# 如果想要取别的名字, 可以用 `make -f "filename" <target>`. + +# 重要的事情 - 只认识 TAB, 空格是不认的, 但是在 GNU Make 3.82 之后, 可以通过 +# 设置参数 .RECIPEPREFIX 进行修改 + +#----------------------------------------------------------------------- +# 初级 +#----------------------------------------------------------------------- + +# 创建一个 target 的规则非常简单 +# targets : prerequisites +# recipe +# … +# prerequisites(依赖) 是可选的, recipe(做法) 也可以多个或者不给. + +# 下面这个任务没有给 prerequisites, 只会在目标文件 file0.txt 文件不存在是跑 +file0.txt: + echo "foo" > file0.txt + # 试试 `make file0.txt` + # 或者直接 `make`, 因为第一个任务是默认任务. + # 注意: 即使是这些注释, 如果前面有 TAB, 也会发送给 shell, 注意看 `make file0.txt` 输出 + +# 如果提供 prerequisites, 则只有 prerequisites 比 target 新时会执行 +# 比如下面这个任务只有当 file1.txt 比 file0.txt 新时才会执行. +file1.txt: file0.txt + cat file0.txt > file1.txt + # 这里跟shell里的命令式一毛一样的. + @cat file0.txt >> file1.txt + # @ 不会把命令往 stdout 打印. + -@echo 'hello' + # - 意思是发生错误了也没关系. + # 试试 `make file1.txt` 吧. + +# targets 和 prerequisites 都可以是多个, 以空格分割 +file2.txt file3.txt: file0.txt file1.txt + touch file2.txt + touch file3.txt + +# 如果声明重复的 target, make 会给一个 warning, 后面会覆盖前面的 +# 比如重复定义 file2.txt 会得到这样的 warning +# Makefile:46: warning: overriding commands for target `file2.txt' +# Makefile:40: warning: ignoring old commands for target `file2.txt' +file2.txt: file0.txt + touch file2.txt + +# 但是如果不定义任何 recipe, 就不会冲突, 只是多了依赖关系 +file2.txt: file0.txt file3.txt + +#----------------------------------------------------------------------- +# Phony(假的) Targets +#----------------------------------------------------------------------- + +# phony targets 意思是 tagets 并不是文件, 可以想象成一个任务的名字而已. +# 因为不是文件, 无法比对是否有更新, 所以每次make都会执行. +all: maker process + +# 依赖于 phony target 的 target 也会每次 make 都执行, 即使 target 是文件 +ex0.txt ex1.txt: maker + +# target 的声明顺序并不重要, 比如上面的 all 的依赖 maker 现在才声明 +maker: + touch ex0.txt ex1.txt + +# 如果定义的 phony target 与文件名重名, 可以用 .PHONY 显示的指明哪些 targets 是 phony +.PHONY: all maker process +# This is a special target. There are several others. + +# 常用的 phony target 有: all clean install ... + +#----------------------------------------------------------------------- +# 变量与通配符 +#----------------------------------------------------------------------- + +process: file*.txt | dir/a.foo.b # 可以用通配符匹配多个文件作为prerequisites + @echo $^ # $^ 是 prerequisites + @echo $@ # $@ 代表 target, 如果 target 为多个, $@ 代表当前执行的那个 + @echo $< # $< prerequisite 中的第一个 + @echo $? # $? 需要更新的 prerequisite 文件列表 + @echo $+ # $+ 所有依赖, 包括重复的 + @echo $| # $| 竖线后面的 order-only prerequisites + +a.%.b: + @echo $* # $* match 的target % 那部分, 包括路径, 比如 `make dir/a.foo.b` 会打出 `dir/foo` + +# 即便分开定义依赖, $^ 依然能拿到 +process: ex1.txt file0.txt +# 非常智能的, ex1.txt 会被找到, file0.txt 会被去重. + +#----------------------------------------------------------------------- +# 模式匹配 +#----------------------------------------------------------------------- + +# 可以让 make 知道如何转换某些文件到别格式 +# 比如 从 svg 到 png +%.png: %.svg + inkscape --export-png $^ + +# 一旦有需要 foo.png 这个任务就会运行 + +# 路径会被忽略, 所以上面的 target 能匹配所有 png +# 但是如果加了路径, make 会找到最接近的匹配, 如果 +# make small/foo.png (在这之前要先有 small/foo.svg 这个文件) +# 则会匹配下面这个规则 +small/%.png: %.svg + inkscape --export-png --export-dpi 30 $^ + +%.png: %.svg + @echo 重复定义会覆盖前面的, 现在 inkscape 没用了 + +# make 已经有一些内置的规则, 比如从 *.c 到 *.o + +#----------------------------------------------------------------------- +# 变量 +#----------------------------------------------------------------------- +# 其实是宏 macro + +# 变量都是字符串类型, 下面这俩是一样一样的 + +name = Ted +name2="Sarah" + +echo: + @echo $(name) + @echo ${name2} + @echo $name # 这个会被蠢蠢的解析成 $(n)ame. + @echo \"$(name3)\" # 为声明的变量或扩展成空字符串. + @echo $(name4) + @echo $(name5) +# 你可以通过4种方式设置变量. +# 按以下顺序由高到低: +# 1: 命令行参数. 比如试试 `make echo name3=JICHAO` +# 2: Makefile 里面的 +# 3: shell 中的环境变量 +# 4: make 预设的一些变量 + +name4 ?= Jean +# 问号意思是如果 name4 被设置过了, 就不设置了. + +override name5 = David +# 用 override 可以防止命令行参数设置的覆盖 + +name4 +=grey +# 用加号可以连接 (中间用空格分割). + +# 在依赖的地方设置变量 +echo: name2 = Sara2 + +# 还有一些内置的变量 +echo_inbuilt: + echo $(CC) + echo ${CXX)} + echo $(FC) + echo ${CFLAGS)} + echo $(CPPFLAGS) + echo ${CXXFLAGS} + echo $(LDFLAGS) + echo ${LDLIBS} + +#----------------------------------------------------------------------- +# 变量 2 +#----------------------------------------------------------------------- + +# 加个冒号可以声明 Simply expanded variables 即时扩展变量, 即只在声明时扩展一次 +# 之前的等号声明时 recursively expanded 递归扩展 + +var := hello +var2 := $(var) hello + +# 这些变量会在其引用的顺序求值 +# 比如 var3 声明时找不到 var4, var3 会扩展成 `and good luck` +var3 := $(var4) and good luck +# 但是一般的变量会在调用时递归扩展, 先扩展 var5, 再扩展 var4, 所以是正常的 +var5 = $(var4) and good luck +var4 := good night + +echoSEV: + @echo $(var) + @echo $(var2) + @echo $(var3) + @echo $(var4) + @echo $(var5) + +#----------------------------------------------------------------------- +# 函数 +#----------------------------------------------------------------------- + +# make 自带了一些函数. +# wildcard 会将后面的通配符变成一串文件路径 +all_markdown: + @echo $(wildcard *.markdown) +# patsubst 可以做替换, 比如下面会把所有 markdown +# 后缀的文件重命名为 md 后缀 +substitue: * + @echo $(patsubst %.markdown,%.md,$* $^) + +# 函数调用格式是 $(func arg0,arg1,arg2...) + +# 试试 +ls: * + @echo $(filter %.txt, $^) + @echo $(notdir $^) + @echo $(join $(dir $^),$(notdir $^)) + +#----------------------------------------------------------------------- +# Directives +#----------------------------------------------------------------------- + +# 可以用 include 引入别的 Makefile 文件 +# include foo.mk + +sport = tennis +# 一些逻辑语句 if else 什么的, 顶个写 +report: +ifeq ($(sport),tennis) + @echo 'game, set, match' +else + @echo "They think it's all over; it is now" +endif + +# 还有 ifneq, ifdef, ifndef + +foo = true + +# 不只是 recipe, 还可以写在外面哟 +ifdef $(foo) +bar = 'bar' +endif + +hellobar: + @echo bar +``` + +### 资源 + ++ GNU Make 官方文档 [HTML](https://www.gnu.org/software/make/manual/) [PDF](https://www.gnu.org/software/make/manual/make.pdf) ++ [software carpentry tutorial](http://swcarpentry.github.io/make-novice/) ++ learn C the hard way [ex2](http://c.learncodethehardway.org/book/ex2.html) [ex28](http://c.learncodethehardway.org/book/ex28.html) diff --git a/zh-cn/markdown-cn.html.markdown b/zh-cn/markdown-cn.html.markdown index 87ed46ad..e9a8aeb2 100644 --- a/zh-cn/markdown-cn.html.markdown +++ b/zh-cn/markdown-cn.html.markdown @@ -13,7 +13,7 @@ Markdown 由 John Gruber 于 2004年创立. 它旨在成为一门容易读写的 欢迎您多多反馈以及分支和请求合并。 -```markdown +```md <!-- Markdown 是 HTML 的父集,所以任何 HTML 文件都是有效的 Markdown。 这意味着我们可以在 Markdown 里使用任何 HTML 元素,比如注释元素, 且不会被 Markdown 解析器所影响。不过如果你在 Markdown 文件内创建了 HTML 元素, diff --git a/zh-cn/matlab-cn.html.markdown b/zh-cn/matlab-cn.html.markdown index 77ba765a..2fbccfc4 100644 --- a/zh-cn/matlab-cn.html.markdown +++ b/zh-cn/matlab-cn.html.markdown @@ -1,11 +1,13 @@ ---
language: Matlab
+filename: matlab-cn.m
contributors:
- ["mendozao", "http://github.com/mendozao"]
- ["jamesscottbrown", "http://jamesscottbrown.com"]
translators:
- ["sunxb10", "https://github.com/sunxb10"]
lang: zh-cn
+
---
MATLAB 是 MATrix LABoratory (矩阵实验室)的缩写,它是一种功能强大的数值计算语言,在工程和数学领域中应用广泛。
diff --git a/zh-cn/python3-cn.html.markdown b/zh-cn/python3-cn.html.markdown index 76455a46..211ce0c5 100644 --- a/zh-cn/python3-cn.html.markdown +++ b/zh-cn/python3-cn.html.markdown @@ -568,13 +568,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/red-cn.html.markdown b/zh-cn/red-cn.html.markdown new file mode 100644 index 00000000..85812990 --- /dev/null +++ b/zh-cn/red-cn.html.markdown @@ -0,0 +1,208 @@ +--- +name: Red +category: language +language: Red +filename: LearnRed-zh.red +contributors: + - ["Arnold van Hofwegen", "https://github.com/iArnold"] +translators: + - ["Limo Saplf", "https://github.com/saplf"] +lang: zh-cn +--- + +Red 的编写是出于工作需要,该语言的作者想要使用 REBOL,但它有许多缺陷。 +当时 REBOL 还没有开源,由于它是一门解释型语言,这就意味着它比编译型语言效率低。 + +Red 使用 C 语言级别的 Red/System,是一门涉及所有编程领域的语言。 +Red 基于 REBOL 编写,它继承了 REBOL 的灵活性,同时也包含了许多 C 语言能做的底层实现。 + +Red 将会成为世界上第一门全栈式编程语言,这意味着它可以完成几乎所有的编程任务,从底层到抽象,无需其他工具的参与。 +而且,Red 支持交叉编译,任意两个平台之间,不需要任何 GCC 之类的工具链的支持。 +所有的工作,仅仅需要一个不到 1 MB 的二进制可执行文件。 + +准备好你的 Red 第一课了吗? + +```red +所有 header 之前的文字都是注释,只要你不使用 "red" 关键字,其中的 "r" 大写。 +这是词法分析器的一个缺陷,所以大多数时候,你都应该直接以 header 开始程序或者脚本的编写。 + +red 脚本的 header 由关键字,首字母大写的 "red" 开始,后跟一个空格,再跟一对方括号 []。 +方括号里可以写上一些关于这段脚本或者程序的相关信息: +作者,文件名,版本号,license,程序功能的简介,它依赖的其他文件。 +red/System 的 header 和 red header 类似,仅仅是说明 "red/System" 而非 "red"。 + + +Red [] + +; 这是一条行注释 + +print "Hello Red World" ; 另一条注释 + +comment { + 这是多行注释。 + 你刚刚看到的就是 Red 版的 Hello World。 +} + +; 程序的入口就是第一句可执行的代码 +; 不需要把它放在 'main' 函数里 + +; 变量名以一个字母开始,可以包含数字, +; 只包含 A ~ F 字符和数字的变量名不能以 'h' 结尾, +; 因为这是 Red 和 Red/System 中十六进制数字的表达方式。 + +; 给变量赋值使用冒号 ":" +my-name: "Red" +reason-for-using-the-colon: {使用冒号作为赋值符号 + 是为了能够让 "=" 能够用来作为比较符号,这本来就是 "=" + 存在的意义!还记得上学时学的,y = x + 1 、 x = 1, + 以及推导出的 y = 2 吗? +} +is-this-name-valid?: true + +; 用 print 打印输出,prin 打印不带换行的输出 + +prin "我的名字是 " print my-name +; 我的名字是 Red + +print ["我的名字是 " my-name lf] +; 我的名字是 Red + +; 注意到了吗:语句没有以分号结尾 ;-) + +; +; 数据类型 +; +; 如果你了解 Rebol,你可能就会注意到它有许多数据类型。 +; Red 并没有囊括它所有的类型,但由于 Red 想要尽可能的 +; 接近 Rebol,所以它也会有很多数据类型。 +; 类型以叹号结尾,但要注意,变量名也是可以以叹号结尾的。 +; 一些内置类型有 integer! string! block! + +; 使用变量前需要声明吗? +; Red 能够分辨什么时候使用什么变量,变量声明并非必要的。 +; 一般认为,声明变量是较好的编码实践,但 Red 并不会强制这点。 +; 你可以声明一个变量然后指定它的类型,而一个变量的类型就 +; 指定了它的字节大小。 + +; integer! 类型的变量通常是 4 字节,32位 +my-integer: 0 +; Red 的整型包含符号,暂时不支持无符号类型,但以后会支持的。 + +; 怎样判断一个变量的类型? +type? my-integer +; integer! + +; 一个变量的初始化可以使用另一个同样刚刚初始化的变量: +i2: 1 + i1: 1 + +; 算数运算符 +i1 + i2 ; 3 +i2 - i1 ; 1 +i2 * i1 ; 2 +i1 / i2 ; 0 (0.5,但截取为 0) + +; 比较运算符都差不多,但和其他语言不一样的是相等的比较, +; Red 使用单个的 '='。 +; Red 有一个类似 boolean 的类型,它的值是 true 和 false, +; 但也可以使用 on/off 或者 yes/on + +3 = 2 ; false +3 != 2 ; true +3 > 2 ; true +3 < 2 ; false +2 <= 2 ; true +2 >= 2 ; true + +; +; 控制流 +; +; if +; 如果给定的条件为 true 则执行一段代码块。 +; if 没有返回值,所以不能用作表达式。 +if a < 0 [print "a 是负值"] + +; either +; 如果给定的条件为 true 则执行一段代码块,否则就 +; 执行另一段可选的代码块。如果两个代码块中最后一个表达式 +; 的类型相同, either 就可以用作表达式。 +either a > 0 [ + msg: "正值" +][ + either a = 0 [ + msg: "零" + ][ + msg: "负值" + ] +] +print ["a 是 " msg lf] + +; 还可以有另一种写法 +; (因为两条路径的返回值相同,所以可以这么写): + +msg: either a > 0 [ + "正值" +][ + either a = 0 [ + "零" + ][ + "负值" + ] +] +print ["a 是 " msg lf] + +; util +; 循环执行一段代码块,直到满足给定的条件为止。 +; util 没有返回值,所以它不能用在表示式中。 +c: 5 +util [ + prin "o" + c: c - 1 + c = 0 ; 终止循环的条件 +] +; 输出:ooooo +; 需要注意的是,即使条件从一开始就不满足, +; 这个循环也至少会执行一次。 + +; while +; 当满足给定的条件,就执行一段代码。 +; while 没有返回值,不能用在表达式中。 +c: 5 +while [c > 0][ + prin "o" + c: c - 1 +] +; 输出:ooooo + +; +; 函数 +; +; 函数示例 +twice: function [a [integer!] /one return: [integer!]][ + c: 2 + a: a * c + either one [a + 1][a] +] +b: 3 +print twice b ; 输出 6 + +; 使用 #include 和 %文件名 来导入外部文件 +#include %includefile.red +; 现在就可以使用 includefile.red 中的函数了。 + +``` + +## 更进一步 + +Red 相关的源码信息在 [Red 语言主页](http://www.red-lang.org)。 + +源代码的 [github 库](https://github.com/red/red)。 + +Red/System 特性在 [这里](http://static.red-lang.org/red-system-specs-light.html)。 + +想要了解更多关于 Rebol 和 Red 的信息,加入 [Gitter 聊天室](https://gitter.im/red/red)。如果你无法加入,也可以给我们发[邮件](mailto:red-langNO_SPAM@googlegroups.com)。 + +也可以在 [Stack Overflow](stackoverflow.com/questions/tagged/red) 上查阅、提交问题。 + +也许你现在就要试一试 Red ?可以在线尝试 [try Rebol and Red site](http://tryrebol.esperconsultancy.nl)。 + +你也可以通过学习一些 [Rebol](http://www.rebol.com/docs.html) 来学习 Red。 diff --git a/zh-cn/swift-cn.html.markdown b/zh-cn/swift-cn.html.markdown index cba9252d..c25b2918 100644 --- a/zh-cn/swift-cn.html.markdown +++ b/zh-cn/swift-cn.html.markdown @@ -445,47 +445,47 @@ if let circle = myEmptyCircle { // 枚举可以像类一样,拥有方法 enum Suit { - case Spades, Hearts, Diamonds, Clubs + case spades, hearts, diamonds, clubs func getIcon() -> String { switch self { - case .Spades: return "♤" - case .Hearts: return "♡" - case .Diamonds: return "♢" - case .Clubs: return "♧" + case .spades: return "♤" + case .hearts: return "♡" + case .diamonds: return "♢" + case .clubs: return "♧" } } } // 当变量类型明确指定为某个枚举类型时,赋值时可以省略枚举类型 -var suitValue: Suit = .Hearts +var suitValue: Suit = .hearts // 非整型的枚举类型需要在定义时赋值 enum BookName: String { - case John = "John" - case Luke = "Luke" + case john = "John" + case luke = "Luke" } -print("Name: \(BookName.John.rawValue)") +print("Name: \(BookName.john.rawValue)") // 与特定数据类型关联的枚举 enum Furniture { // 和 Int 型数据关联的枚举记录 - case Desk(height: Int) + case desk(height: Int) // 和 String, Int 关联的枚举记录 - case Chair(brand: String, height: Int) + case chair(brand: String, height: Int) func description() -> String { switch self { - case .Desk(let height): + case .desk(let height): return "Desk with \(height) cm" - case .Chair(let brand, let height): + case .chair(let brand, let height): return "Chair of \(brand) with \(height) cm" } } } -var desk: Furniture = .Desk(height: 80) +var desk: Furniture = .desk(height: 80) print(desk.description()) // "Desk with 80 cm" -var chair = Furniture.Chair(brand: "Foo", height: 40) +var chair = Furniture.chair(brand: "Foo", height: 40) print(chair.description()) // "Chair of Foo with 40 cm" diff --git a/zh-cn/typescript-cn.html.markdown b/zh-cn/typescript-cn.html.markdown index 2651b1cb..032f89e4 100644 --- a/zh-cn/typescript-cn.html.markdown +++ b/zh-cn/typescript-cn.html.markdown @@ -153,7 +153,7 @@ var pairToTuple = function<T>(p: Pair<T>) { var tuple = pairToTuple({ item1:"hello", item2:"world"}); // 引用定义文件 -// <reference path="jquery.d.ts" /> +/// <reference path="jquery.d.ts" /> // 模板字符串(使用反引号的字符串) // 嵌入变量的模板字符串 diff --git a/zh-cn/visualbasic-cn.html.markdown b/zh-cn/visualbasic-cn.html.markdown index 59f18fe2..e30041b3 100644 --- a/zh-cn/visualbasic-cn.html.markdown +++ b/zh-cn/visualbasic-cn.html.markdown @@ -5,10 +5,10 @@ contributors: translators: - ["Abner Chou", "http://cn.abnerchou.me"] lang: zh-cn -filename: learnvisualbasic.vb-cn +filename: learnvisualbasic-cn.vb --- -```vb +``` Module Module1 Sub Main() @@ -77,7 +77,7 @@ Module Module1 ' 使用 private subs 声明函数。 Private Sub HelloWorldOutput() ' 程序名 - Console.Title = "Hello World Ouput | Learn X in Y Minutes" + Console.Title = "Hello World Output | Learn X in Y Minutes" ' 使用 Console.Write("") 或者 Console.WriteLine("") 来输出文本到屏幕上 ' 对应的 Console.Read() 或 Console.Readline() 用来读取键盘输入 Console.WriteLine("Hello World") diff --git a/zh-tw/pcre-tw.html.markdown b/zh-tw/pcre-tw.html.markdown new file mode 100644 index 00000000..c9cdc537 --- /dev/null +++ b/zh-tw/pcre-tw.html.markdown @@ -0,0 +1,83 @@ +--- +language: PCRE +filename: pcre-tw.txt +contributors: + - ["Sachin Divekar", "http://github.com/ssd532"] +translators: + - ["Michael Yeh", "https://hinet60613.github.io/"] +lang: zh-tw +--- + +正規表達式(regular expression,或縮寫為regex, regexp)是一種用來表示搜尋模式的特殊字串。例如,你可以用`/^[a-z]+:/`來從網址`http://github.com`中擷取出`http:`這段http協定名稱。 + +相容Perl正規表達式(Perl Compatible Regular Expressions, PCRE)是一個實作正規表達式的C語言函式庫。此函式庫在1997年被開發出來,在當時面對複雜字串處理時大多會選擇使用Perl。也因為如此,PCRE大多的正規表達式語法都很酷似Perl。PCRE語法被廣泛運用在許多大專案中,包括PHP、Apache、R等。 + +PCRE中的超字元(metacharacter)主要可以分為以下兩類: +* 在中括號外會被辨識的字元 +``` + \ 通用跳脫字元 + ^ 字串開頭 或 行首 + $ 字串結尾 或 行尾 + . 除了換行符號外的任何字元 + [ 字元集合定義開始 + | 支流開始 + ( 子串模式定義開始 + ) 子串模式定義結束 + ? extends the meaning of ( + 同時為數量0或1配對 + also quantifier minimizer + * 量詞 至少0個 至多無限個 + + 量詞 至少1個 至多無限個 + 同時為佔有型量詞 + { 最大/最小量詞開始 +``` + +* 在中括號內會被辨識的超字元,在中括號外會被視為字元集合使用 + +``` + + \ 通用跳脫字元 + ^ 非字元集合的字,但只會抓到第一個符合的字元 + - 字元範圍 + [ POSIX字元集合(若後面接POSIX格式) + ] 字元集合定義結束 + +``` + +PCRE提供了一些通用的字元類型,可被當作字元集合使用 +``` + \d 任何數字字元 + \D 任何非數字字元 + \h 任何水平空白字元 + \H 任何非水平空白字元 + \s 任何空白字元 + \S 任何非空白至元 + \v 任何垂直空白字元 + \V 任何非垂直空白字元 + \w 任何英文字 + \W 任何非英文字 +``` + +## 範例 + +我們以字串 `66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"` 作為範例,這是一個標準的Apache存取記錄。 + +| 正規表達式 | 結果 | 說明 | +| :---- | :-------------- | :------ | +| GET | GET | GET 抓取 GET 字串 (會分別大小寫) | +| \d+.\d+.\d+.\d+ | 66.249.64.13 | `\d+` 抓取數字字元,數量由 `+` 定義為至少一個至多無限個。 `\.` 抓取 `.` 字元 | +| (\d+\.){3}\d+ | 66.249.64.13 | `(\d+\.){3}` 會試著抓取剛好三次的 (`\d+\.`) | +| \[.+\] | [18/Sep/2004:11:07:48 +1000] | `.+` 抓取除了換行符號以外的任何字元, `.` 表示任意字元 | +| ^\S+ | 66.249.64.13 | `^` 為行首, `\S+` 抓取至少一個非空白字元 | +| \+[0-9]+ | +1000 | `\+` 抓取 `+` 字元。 `[0-9]` 字元集表示剛好一個數字字元。 可以用 `\+\d+` 達到相同效果。 | + +以上範例皆可在 https://regex101.com/ 測試,步驟如下: + +1. 複製範例字串到 `TEST STRING` 區域 +2. 複製正規表達式字串到 `Regular Expression` 區域 +3. 網頁會顯示自動表達式抓取結果 + + +## 更多資料 + + |