diff options
| -rw-r--r-- | bash.html.markdown | 11 | ||||
| -rw-r--r-- | c.html.markdown | 883 | ||||
| -rw-r--r-- | de-de/haskell-de.html.markdown | 425 | ||||
| -rw-r--r-- | es-es/css-es.html.markdown | 244 | ||||
| -rw-r--r-- | es-es/json-es.html.markdown | 59 | ||||
| -rw-r--r-- | es-es/livescript-es.html.markdown | 339 | ||||
| -rw-r--r-- | es-es/markdown-es.html.markdown | 252 | ||||
| -rw-r--r-- | fr-fr/csharp-fr.html.markdown | 2 | ||||
| -rw-r--r-- | go.html.markdown | 439 | ||||
| -rw-r--r-- | ja-jp/bash-jp.html.markdown | 172 | ||||
| -rw-r--r-- | ja-jp/r-jp.html.markdown | 775 | ||||
| -rw-r--r-- | matlab.html.markdown | 3 | ||||
| -rw-r--r-- | perl6.html.markdown | 1007 | ||||
| -rw-r--r-- | python.html.markdown | 23 | ||||
| -rw-r--r-- | python3.html.markdown | 34 | ||||
| -rw-r--r-- | swift.html.markdown | 11 | ||||
| -rw-r--r-- | zh-cn/julia-cn.html.markdown | 729 | 
17 files changed, 4739 insertions, 669 deletions
| diff --git a/bash.html.markdown b/bash.html.markdown index d5d08e9d..15d1c068 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -6,6 +6,7 @@ contributors:      - ["Darren Lin", "https://github.com/CogBear"]      - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]      - ["Denis Arh", "https://github.com/darh"] +    - ["akirahirose", "https://twitter.com/akirahirose"]  filename: LearnBash.sh  --- @@ -155,14 +156,14 @@ bar ()  foo "My name is" $NAME  # There are a lot of useful commands you should learn: -tail -n 10 file.txt  # prints last 10 lines of file.txt -head -n 10 file.txt +tail -n 10 file.txt  # prints first 10 lines of file.txt -sort file.txt +head -n 10 file.txt  # sort file.txt's lines -uniq -d file.txt +sort file.txt  # report or omit repeated lines, with -d it reports them -cut -d ',' -f 1 file.txt +uniq -d file.txt  # prints only the first column before the ',' character +cut -d ',' -f 1 file.txt  ``` diff --git a/c.html.markdown b/c.html.markdown index 22f251f2..8e170300 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -16,16 +16,16 @@ memory management and C will take you as far as you need to go.  ```c  // Single-line comments start with // - only available in C99 and later. -/* +  /*  Multi-line comments look like this. They work in C89 as well. -*/ +  */ -// Constants: #define <keyword> +  // Constants: #define <keyword>  #define DAYS_IN_YEAR 365 -// Enumeration constants are also ways to declare constants.  -enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; -// MON gets 2 automatically, TUE gets 3, etc.  +  // Enumeration constants are also ways to declare constants. +  enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; +// MON gets 2 automatically, TUE gets 3, etc.  // Import headers with #include  #include <stdlib.h> @@ -34,388 +34,386 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};  // (File names between <angle brackets> are headers from the C standard library.)  // For your own headers, use double quotes instead of angle brackets: -#include "my_header.h" +//#include "my_header.h"  // Declare function signatures in advance in a .h file, or at the top of  // your .c file. -void function_1(char c); +void function_1();  int function_2(void);  // Must declare a 'function prototype' before main() when functions occur after  // your main() function. -int add_two_ints(int x1, int x2); // function prototype  +int add_two_ints(int x1, int x2); // function prototype  // Your program's entry point is a function called  // main with an integer return type.  int main() { -    // print output using printf, for "print formatted" -    // %d is an integer, \n is a newline -    printf("%d\n", 0); // => Prints 0 -    // All statements must end with a semicolon - -    /////////////////////////////////////// -    // Types -    /////////////////////////////////////// - -    // ints are usually 4 bytes  -    int x_int = 0; - -    // shorts are usually 2 bytes -    short x_short = 0; - -    // chars are guaranteed to be 1 byte -    char x_char = 0; -    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 -    long x_long = 0; -    long long x_long_long = 0;  - -    // floats are usually 32-bit floating point numbers -    float x_float = 0.0; - -    // doubles are usually 64-bit floating-point numbers -    double x_double = 0.0; - -    // Integral types may be unsigned. -    unsigned short ux_short; -    unsigned int ux_int; -    unsigned long long ux_long_long; - -    // chars inside single quotes are integers in machine's character set.  -    '0' // => 48 in the ASCII character set.  -    'A' // => 65 in the ASCII character set.  - -    // sizeof(T) gives you the size of a variable with type T in bytes -    // sizeof(obj) yields the size of the expression (variable, literal, etc.). -    printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words) - - -    // If the argument of the `sizeof` operator is an expression, then its argument -    // is not evaluated (except VLAs (see below)). -    // The value it yields in this case is a compile-time constant. -    int a = 1; -    // size_t is an unsigned integer type of at least 2 bytes used to represent  -    // the size of an object. -    size_t size = sizeof(a++); // a++ is not evaluated -    printf("sizeof(a++) = %zu where a = %d\n", size, a); -    // prints "sizeof(a++) = 4 where a = 1" (on a 32-bit architecture) - -    // Arrays must be initialized with a concrete size. -    char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes -    int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes -                          // (assuming 4-byte words) - - -    // You can initialize an array to 0 thusly: -    char my_array[20] = {0}; - -    // Indexing an array is like other languages -- or, -    // rather, other languages are like C -    my_array[0]; // => 0 - -    // Arrays are mutable; it's just memory! -    my_array[1] = 2; -    printf("%d\n", my_array[1]); // => 2 - -    // In C99 (and as an optional feature in C11), variable-length arrays (VLAs) -    // can be declared as well. The size of such an array need not be a compile -    // time constant: -    printf("Enter the array size: "); // ask the user for an array size -    char buf[0x100]; -    fgets(buf, sizeof buf, stdin); - -    // strtoul parses a string to an unsigned integer -    size_t size = strtoul(buf, NULL, 10); -    int var_length_array[size]; // declare the VLA -    printf("sizeof array = %zu\n", sizeof var_length_array); - -    // A possible outcome of this program may be: -    // > Enter the array size: 10 -    // > sizeof array = 40 - -    // Strings are just arrays of chars terminated by a NULL (0x00) byte, -    // represented in strings as the special character '\0'. -    // (We don't have to include the NULL byte in string literals; the compiler -    //  inserts it at the end of the array for us.) -    char a_string[20] = "This is a string"; -    printf("%s\n", a_string); // %s formats a string - -    printf("%d\n", a_string[16]); // => 0 -    // i.e., byte #17 is 0 (as are 18, 19, and 20) - -    // If we have characters between single quotes, that's a character literal. -    // It's of type `int`, and *not* `char` (for historical reasons). -    int cha = 'a'; // fine -    char chb = 'a'; // fine too (implicit conversion from int to char) - -    //Multi-dimensional arrays: -    int multi_array[2][5] = { -        {1, 2, 3, 4, 5}, -        {6, 7, 8, 9, 0} -    }; -    //access elements: -    int array_int = multi_array[0][2]; // => 3 - -    /////////////////////////////////////// -    // Operators -    /////////////////////////////////////// - -    // Shorthands for multiple declarations: -    int i1 = 1, i2 = 2;  -    float f1 = 1.0, f2 = 2.0; - -    int a, b, c; -    a = b = c = 0; - -    // Arithmetic is straightforward -    i1 + i2; // => 3 -    i2 - i1; // => 1 -    i2 * i1; // => 2 -    i1 / i2; // => 0 (0.5, but truncated towards 0) - -    f1 / f2; // => 0.5, plus or minus epsilon -    // Floating-point numbers and calculations are not exact - -    // Modulo is there as well -    11 % 3; // => 2 - -    // Comparison operators are probably familiar, but -    // there is no Boolean type in c. We use ints instead. -    // (Or _Bool or bool in C99.) -    // 0 is false, anything else is true. (The comparison  -    // operators always yield 0 or 1.) -    3 == 2; // => 0 (false) -    3 != 2; // => 1 (true) -    3 > 2; // => 1 -    3 < 2; // => 0 -    2 <= 2; // => 1 -    2 >= 2; // => 1 - -    // C is not Python - comparisons don't chain. -    int a = 1; -    // WRONG: -    int between_0_and_2 = 0 < a < 2; -    // Correct: -    int between_0_and_2 = 0 < a && a < 2; - -    // Logic works on ints -    !3; // => 0 (Logical not) -    !0; // => 1 -    1 && 1; // => 1 (Logical and) -    0 && 1; // => 0 -    0 || 1; // => 1 (Logical or) -    0 || 0; // => 0 - -    //Conditional expression ( ? : ) -    int a = 5; -    int b = 10; -    int z; -    z = (a > b) ? a : b; // => 10 "if a > b return a, else return b."  - -    //Increment and decrement operators: -    char *s = "iLoveC"; -    int j = 0; -    s[j++]; // => "i". Returns the j-th item of s THEN increments value of j. -    j = 0;  -    s[++j]; // => "L". Increments value of j THEN returns j-th value of s.  -    // same with j-- and --j - -    // Bitwise operators! -    ~0x0F; // => 0xF0 (bitwise negation, "1's complement") -    0x0F & 0xF0; // => 0x00 (bitwise AND) -    0x0F | 0xF0; // => 0xFF (bitwise OR) -    0x04 ^ 0x0F; // => 0x0B (bitwise XOR) -    0x01 << 1; // => 0x02 (bitwise left shift (by 1)) -    0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) - -    // Be careful when shifting signed integers - the following are undefined: -    // - shifting into the sign bit of a signed integer (int a = 1 << 32) -    // - left-shifting a negative number (int a = -1 << 2) -    // - shifting by an offset which is >= the width of the type of the LHS: -    //   int a = 1 << 32; // UB if int is 32 bits wide - -    /////////////////////////////////////// -    // Control Structures -    /////////////////////////////////////// - -    if (0) { -      printf("I am never run\n"); -    } else if (0) { -      printf("I am also never run\n"); -    } else { -      printf("I print\n"); -    } - -    // While loops exist -    int ii = 0; -    while (ii < 10) { //ANY value not zero is true.  -        printf("%d, ", ii++); // ii++ increments ii AFTER using its current value. -    } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " - -    printf("\n"); - -    int kk = 0; -    do { -        printf("%d, ", kk); -    } while (++kk < 10); // ++kk increments kk BEFORE using its current value. -    // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " - -    printf("\n"); - -    // For loops too -    int jj; -    for (jj=0; jj < 10; jj++) { -        printf("%d, ", jj); -    } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " - -    printf("\n"); - -    // *****NOTES*****: -    // Loops and Functions MUST have a body. If no body is needed: -    int i; -    for (i = 0; i <= 5; i++) { -        ; // use semicolon to act as the body (null statement) -    } - -    // branching with multiple choices: switch() -    switch (some_integral_expression) { -    case 0: // labels need to be integral *constant* expressions -        do_stuff(); -        break; // if you don't break, control flow falls over labels -    case 1: -        do_something_else(); -        break; -    default: -        // if `some_integral_expression` didn't match any of the labels -        fputs("error!\n", stderr); -        exit(-1); -        break; -    } -     - -    /////////////////////////////////////// -    // Typecasting -    /////////////////////////////////////// - -    // Every value in C has a type, but you can cast one value into another type -    // if you want (with some constraints). - -    int x_hex = 0x01; // You can assign vars with hex literals - -    // Casting between types will attempt to preserve their numeric values -    printf("%d\n", x_hex); // => Prints 1 -    printf("%d\n", (short) x_hex); // => Prints 1 -    printf("%d\n", (char) x_hex); // => Prints 1 - -    // Types will overflow without warning -    printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 if char is 8 bits long) - -    // For determining the max value of a `char`, a `signed char` and an `unsigned char`, -    // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from <limits.h> - -    // Integral types can be cast to floating-point types, and vice-versa. -    printf("%f\n", (float)100); // %f formats a float -    printf("%lf\n", (double)100); // %lf formats a double -    printf("%d\n", (char)100.0); - -    /////////////////////////////////////// -    // Pointers -    /////////////////////////////////////// - -    // A pointer is a variable declared to store a memory address. Its declaration will -    // also tell you the type of data it points to. You can retrieve the memory address  -    // of your variables, then mess with them. - -    int x = 0; -    printf("%p\n", (void *)&x); // Use & to retrieve the address of a variable -    // (%p formats an object pointer of type void *) -    // => Prints some address in memory; - - -    // Pointers start with * in their declaration -    int *px, not_a_pointer; // px is a pointer to an int -    px = &x; // Stores the address of x in px -    printf("%p\n", (void *)px); // => Prints some address in memory -    printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer)); -    // => Prints "8, 4" on a typical 64-bit system - -    // To retrieve the value at the address a pointer is pointing to, -    // put * in front to dereference it. -    // Note: yes, it may be confusing that '*' is used for _both_ declaring a -    // pointer and dereferencing it. -    printf("%d\n", *px); // => Prints 0, the value of x - -    // You can also change the value the pointer is pointing to. -    // We'll have to wrap the dereference in parenthesis because -    // ++ has a higher precedence than *. -    (*px)++; // Increment the value px is pointing to by 1 -    printf("%d\n", *px); // => Prints 1 -    printf("%d\n", x); // => Prints 1 - -    // Arrays are a good way to allocate a contiguous block of memory -    int x_array[20]; //declares array of size 20 (cannot change size) -    int xx; -    for (xx = 0; xx < 20; xx++) { -        x_array[xx] = 20 - xx; -    } // Initialize x_array to 20, 19, 18,... 2, 1 +  // print output using printf, for "print formatted" +  // %d is an integer, \n is a newline +  printf("%d\n", 0); // => Prints 0 +  // All statements must end with a semicolon + +  /////////////////////////////////////// +  // Types +  /////////////////////////////////////// + +  // ints are usually 4 bytes +  int x_int = 0; + +  // shorts are usually 2 bytes +  short x_short = 0; + +  // chars are guaranteed to be 1 byte +  char x_char = 0; +  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 +  long x_long = 0; +  long long x_long_long = 0; + +  // floats are usually 32-bit floating point numbers +  float x_float = 0.0; + +  // doubles are usually 64-bit floating-point numbers +  double x_double = 0.0; + +  // Integral types may be unsigned. +  unsigned short ux_short; +  unsigned int ux_int; +  unsigned long long ux_long_long; + +  // chars inside single quotes are integers in machine's character set. +  '0'; // => 48 in the ASCII character set. +  'A'; // => 65 in the ASCII character set. + +  // sizeof(T) gives you the size of a variable with type T in bytes +  // sizeof(obj) yields the size of the expression (variable, literal, etc.). +  printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words) + + +  // If the argument of the `sizeof` operator is an expression, then its argument +  // is not evaluated (except VLAs (see below)). +  // The value it yields in this case is a compile-time constant. +  int a = 1; +  // size_t is an unsigned integer type of at least 2 bytes used to represent +  // the size of an object. +  size_t size = sizeof(a++); // a++ is not evaluated +  printf("sizeof(a++) = %zu where a = %d\n", size, a); +  // prints "sizeof(a++) = 4 where a = 1" (on a 32-bit architecture) + +  // Arrays must be initialized with a concrete size. +  char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes +  int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes +  // (assuming 4-byte words) + + +  // You can initialize an array to 0 thusly: +  char my_array[20] = {0}; + +  // Indexing an array is like other languages -- or, +  // rather, other languages are like C +  my_array[0]; // => 0 + +  // Arrays are mutable; it's just memory! +  my_array[1] = 2; +  printf("%d\n", my_array[1]); // => 2 + +  // In C99 (and as an optional feature in C11), variable-length arrays (VLAs) +  // can be declared as well. The size of such an array need not be a compile +  // time constant: +  printf("Enter the array size: "); // ask the user for an array size +  char buf[0x100]; +  fgets(buf, sizeof buf, stdin); + +  // strtoul parses a string to an unsigned integer +  size_t size2 = strtoul(buf, NULL, 10); +  int var_length_array[size2]; // declare the VLA +  printf("sizeof array = %zu\n", sizeof var_length_array); + +  // A possible outcome of this program may be: +  // > Enter the array size: 10 +  // > sizeof array = 40 + +  // Strings are just arrays of chars terminated by a NULL (0x00) byte, +  // represented in strings as the special character '\0'. +  // (We don't have to include the NULL byte in string literals; the compiler +  //  inserts it at the end of the array for us.) +  char a_string[20] = "This is a string"; +  printf("%s\n", a_string); // %s formats a string + +  printf("%d\n", a_string[16]); // => 0 +  // i.e., byte #17 is 0 (as are 18, 19, and 20) + +  // If we have characters between single quotes, that's a character literal. +  // It's of type `int`, and *not* `char` (for historical reasons). +  int cha = 'a'; // fine +  char chb = 'a'; // fine too (implicit conversion from int to char) + +  //Multi-dimensional arrays: +  int multi_array[2][5] = { +    {1, 2, 3, 4, 5}, +    {6, 7, 8, 9, 0} +  }; +  //access elements: +  int array_int = multi_array[0][2]; // => 3 + +  /////////////////////////////////////// +  // Operators +  /////////////////////////////////////// + +  // Shorthands for multiple declarations: +  int i1 = 1, i2 = 2; +  float f1 = 1.0, f2 = 2.0; + +  int b, c; +  b = c = 0; + +  // Arithmetic is straightforward +  i1 + i2; // => 3 +  i2 - i1; // => 1 +  i2 * i1; // => 2 +  i1 / i2; // => 0 (0.5, but truncated towards 0) + +  f1 / f2; // => 0.5, plus or minus epsilon +  // Floating-point numbers and calculations are not exact + +  // Modulo is there as well +  11 % 3; // => 2 + +  // Comparison operators are probably familiar, but +  // there is no Boolean type in c. We use ints instead. +  // (Or _Bool or bool in C99.) +  // 0 is false, anything else is true. (The comparison +  // operators always yield 0 or 1.) +  3 == 2; // => 0 (false) +  3 != 2; // => 1 (true) +  3 > 2; // => 1 +  3 < 2; // => 0 +  2 <= 2; // => 1 +  2 >= 2; // => 1 + +  // C is not Python - comparisons don't chain. +  // WRONG: +  //int between_0_and_2 = 0 < a < 2; +  // Correct: +  int between_0_and_2 = 0 < a && a < 2; + +  // Logic works on ints +  !3; // => 0 (Logical not) +  !0; // => 1 +  1 && 1; // => 1 (Logical and) +  0 && 1; // => 0 +  0 || 1; // => 1 (Logical or) +  0 || 0; // => 0 + +  //Conditional expression ( ? : ) +  int e = 5; +  int f = 10; +  int z; +  z = (a > b) ? a : b; // => 10 "if a > b return a, else return b." + +  //Increment and decrement operators: +  char *s = "iLoveC"; +  int j = 0; +  s[j++]; // => "i". Returns the j-th item of s THEN increments value of j. +  j = 0; +  s[++j]; // => "L". Increments value of j THEN returns j-th value of s. +  // same with j-- and --j + +  // Bitwise operators! +  ~0x0F; // => 0xF0 (bitwise negation, "1's complement") +  0x0F & 0xF0; // => 0x00 (bitwise AND) +  0x0F | 0xF0; // => 0xFF (bitwise OR) +  0x04 ^ 0x0F; // => 0x0B (bitwise XOR) +  0x01 << 1; // => 0x02 (bitwise left shift (by 1)) +  0x02 >> 1; // => 0x01 (bitwise right shift (by 1)) + +  // Be careful when shifting signed integers - the following are undefined: +  // - shifting into the sign bit of a signed integer (int a = 1 << 32) +  // - left-shifting a negative number (int a = -1 << 2) +  // - shifting by an offset which is >= the width of the type of the LHS: +  //   int a = 1 << 32; // UB if int is 32 bits wide + +  /////////////////////////////////////// +  // Control Structures +  /////////////////////////////////////// + +  if (0) { +    printf("I am never run\n"); +  } else if (0) { +    printf("I am also never run\n"); +  } else { +    printf("I print\n"); +  } + +  // While loops exist +  int ii = 0; +  while (ii < 10) { //ANY value not zero is true. +    printf("%d, ", ii++); // ii++ increments ii AFTER using its current value. +  } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +  printf("\n"); + +  int kk = 0; +  do { +    printf("%d, ", kk); +  } while (++kk < 10); // ++kk increments kk BEFORE using its current value. +  // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +  printf("\n"); + +  // For loops too +  int jj; +  for (jj=0; jj < 10; jj++) { +    printf("%d, ", jj); +  } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " + +  printf("\n"); + +  // *****NOTES*****: +  // Loops and Functions MUST have a body. If no body is needed: +  int i; +  for (i = 0; i <= 5; i++) { +    ; // use semicolon to act as the body (null statement) +  } + +  // branching with multiple choices: switch() +  switch (a) { +  case 0: // labels need to be integral *constant* expressions +    printf("Hey, 'a' equals 0!\n"); +    break; // if you don't break, control flow falls over labels +  case 1: +    printf("Huh, 'a' equals 1!\n"); +    break; +  default: +    // if `some_integral_expression` didn't match any of the labels +    fputs("error!\n", stderr); +    exit(-1); +    break; +  } + +  /////////////////////////////////////// +  // Typecasting +  /////////////////////////////////////// + +  // Every value in C has a type, but you can cast one value into another type +  // if you want (with some constraints). + +  int x_hex = 0x01; // You can assign vars with hex literals + +  // Casting between types will attempt to preserve their numeric values +  printf("%d\n", x_hex); // => Prints 1 +  printf("%d\n", (short) x_hex); // => Prints 1 +  printf("%d\n", (char) x_hex); // => Prints 1 + +  // Types will overflow without warning +  printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 if char is 8 bits long) + +  // For determining the max value of a `char`, a `signed char` and an `unsigned char`, +  // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from <limits.h> + +  // Integral types can be cast to floating-point types, and vice-versa. +  printf("%f\n", (float)100); // %f formats a float +  printf("%lf\n", (double)100); // %lf formats a double +  printf("%d\n", (char)100.0); + +  /////////////////////////////////////// +  // Pointers +  /////////////////////////////////////// + +  // A pointer is a variable declared to store a memory address. Its declaration will +  // also tell you the type of data it points to. You can retrieve the memory address +  // of your variables, then mess with them. + +  int x = 0; +  printf("%p\n", (void *)&x); // Use & to retrieve the address of a variable +  // (%p formats an object pointer of type void *) +  // => Prints some address in memory; + + +  // Pointers start with * in their declaration +  int *px, not_a_pointer; // px is a pointer to an int +  px = &x; // Stores the address of x in px +  printf("%p\n", (void *)px); // => Prints some address in memory +  printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer)); +  // => Prints "8, 4" on a typical 64-bit system + +  // To retrieve the value at the address a pointer is pointing to, +  // put * in front to dereference it. +  // Note: yes, it may be confusing that '*' is used for _both_ declaring a +  // pointer and dereferencing it. +  printf("%d\n", *px); // => Prints 0, the value of x + +  // You can also change the value the pointer is pointing to. +  // We'll have to wrap the dereference in parenthesis because +  // ++ has a higher precedence than *. +  (*px)++; // Increment the value px is pointing to by 1 +  printf("%d\n", *px); // => Prints 1 +  printf("%d\n", x); // => Prints 1 + +  // Arrays are a good way to allocate a contiguous block of memory +  int x_array[20]; //declares array of size 20 (cannot change size) +  int xx; +  for (xx = 0; xx < 20; xx++) { +    x_array[xx] = 20 - xx; +  } // Initialize x_array to 20, 19, 18,... 2, 1      // Declare a pointer of type int and initialize it to point to x_array -    int* x_ptr = x_array; -    // x_ptr now points to the first element in the array (the integer 20).  -    // This works because arrays often decay into pointers to their first element. -    // For example, when an array is passed to a function or is assigned to a pointer, -    // it decays into (implicitly converted to) a pointer. -    // Exceptions: when the array is the argument of the `&` (address-of) operator: -    int arr[10]; -    int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! -                                  // It's of type "pointer to array" (of ten `int`s). -    // or when the array is a string literal used for initializing a char array: -    char arr[] = "foobarbazquirk"; -    // or when it's the argument of the `sizeof` or `alignof` operator: -    int arr[10]; -    int *ptr = arr; // equivalent with int *ptr = &arr[0]; -    printf("%zu, %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8" - - -    // Pointers are incremented and decremented based on their type -    // (this is called pointer arithmetic) -    printf("%d\n", *(x_ptr + 1)); // => Prints 19 -    printf("%d\n", x_array[1]); // => Prints 19 - -    // You can also dynamically allocate contiguous blocks of memory with the -    // standard library function malloc, which takes one argument of type size_t -    // representing the number of bytes to allocate (usually from the heap, although this -    // may not be true on e.g. embedded systems - the C standard says nothing about it). -    int *my_ptr = malloc(sizeof(*my_ptr) * 20); -    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) +  int* x_ptr = x_array; +  // x_ptr now points to the first element in the array (the integer 20). +  // This works because arrays often decay into pointers to their first element. +  // For example, when an array is passed to a function or is assigned to a pointer, +  // it decays into (implicitly converted to) a pointer. +  // Exceptions: when the array is the argument of the `&` (address-of) operator: +  int arr[10]; +  int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! +  // It's of type "pointer to array" (of ten `int`s). +  // or when the array is a string literal used for initializing a char array: +  char otherarr[] = "foobarbazquirk"; +  // or when it's the argument of the `sizeof` or `alignof` operator: +  int arraythethird[10]; +  int *ptr = arraythethird; // equivalent with int *ptr = &arr[0]; +  printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr); // probably prints "40, 4" or "40, 8" + + +  // Pointers are incremented and decremented based on their type +  // (this is called pointer arithmetic) +  printf("%d\n", *(x_ptr + 1)); // => Prints 19 +  printf("%d\n", x_array[1]); // => Prints 19 + +  // You can also dynamically allocate contiguous blocks of memory with the +  // standard library function malloc, which takes one argument of type size_t +  // representing the number of bytes to allocate (usually from the heap, although this +  // may not be true on e.g. embedded systems - the C standard says nothing about it). +  int *my_ptr = malloc(sizeof(*my_ptr) * 20); +  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)      // Dereferencing memory that you haven't allocated gives      // "unpredictable results" - the program is said to invoke "undefined behavior" -    printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash. - -    // When you're done with a malloc'd block of memory, you need to free it,  -    // or else no one else can use it until your program terminates -    // (this is called a "memory leak"): -    free(my_ptr); - -    // Strings are arrays of char, but they are usually represented as a -    // pointer-to-char (which is a pointer to the first element of the array). -    // It's good practice to use `const char *' when referring to a string literal, -    // since string literals shall not be modified (i.e. "foo"[0] = 'a' is ILLEGAL.) -    const char *my_str = "This is my very own string literal"; -    printf("%c\n", *my_str); // => 'T' - -    // This is not the case if the string is an array -    // (potentially initialized with a string literal) -    // that resides in writable memory, as in: -    char foo[] = "foo"; -    foo[0] = 'a'; // this is legal, foo now contains "aoo" - -    function_1(); +  printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash. + +  // When you're done with a malloc'd block of memory, you need to free it, +  // or else no one else can use it until your program terminates +  // (this is called a "memory leak"): +  free(my_ptr); + +  // Strings are arrays of char, but they are usually represented as a +  // pointer-to-char (which is a pointer to the first element of the array). +  // It's good practice to use `const char *' when referring to a string literal, +  // since string literals shall not be modified (i.e. "foo"[0] = 'a' is ILLEGAL.) +  const char *my_str = "This is my very own string literal"; +  printf("%c\n", *my_str); // => 'T' + +  // This is not the case if the string is an array +  // (potentially initialized with a string literal) +  // that resides in writable memory, as in: +  char foo[] = "foo"; +  foo[0] = 'a'; // this is legal, foo now contains "aoo" + +  function_1();  } // end main function  /////////////////////////////////////// @@ -427,16 +425,16 @@ int main() {  int add_two_ints(int x1, int x2)  { -    return x1 + x2; // Use return to return a value +  return x1 + x2; // Use return to return a value  }  /* -Functions are call by value. When a function is called, the arguments passed to  -the function are copies of the original arguments (except arrays). Anything you   -do to the arguments in the function do not change the value of the original  -argument where the function was called.  +Functions are call by value. When a function is called, the arguments passed to +≈the function are copies of the original arguments (except arrays). Anything you +do to the arguments in the function do not change the value of the original +argument where the function was called. -Use pointers if you need to edit the original argument values.  +Use pointers if you need to edit the original argument values.  Example: in-place string reversal  */ @@ -444,14 +442,14 @@ Example: in-place string reversal  // A void function returns no value  void str_reverse(char *str_in)  { -    char tmp; -    int ii = 0; -    size_t len = strlen(str_in); // `strlen()` is part of the c standard library -    for (ii = 0; ii < len / 2; ii++) { -        tmp = str_in[ii]; -        str_in[ii] = str_in[len - ii - 1]; // ii-th char from end -        str_in[len - ii - 1] = tmp; -    } +  char tmp; +  int ii = 0; +  size_t len = strlen(str_in); // `strlen()` is part of the c standard library +  for (ii = 0; ii < len / 2; ii++) { +    tmp = str_in[ii]; +    str_in[ii] = str_in[len - ii - 1]; // ii-th char from end +    str_in[len - ii - 1] = tmp; +  }  }  /* @@ -463,13 +461,13 @@ printf("%s\n", c); // => ".tset a si sihT"  //if referring to external variables outside function, must use extern keyword.  int i = 0;  void testFunc() { -    extern int i; //i here is now using external variable i +  extern int i; //i here is now using external variable i  }  //make external variables private to source file with static: -static int i = 0; //other files using testFunc() cannot access variable i -void testFunc() { -    extern int i; +static int j = 0; //other files using testFunc() cannot access variable i +void testFunc2() { +  extern int j;  }  //**You may also declare functions as static to make them private** @@ -486,8 +484,8 @@ my_type my_type_var = 0;  // Structs are just collections of data, the members are allocated sequentially,  // in the order they are written:  struct rectangle { -    int width; -    int height; +  int width; +  int height;  };  // It's not generally true that @@ -497,20 +495,20 @@ struct rectangle {  void function_1()  { -    struct rectangle my_rec; +  struct rectangle my_rec; -    // Access struct members with . -    my_rec.width = 10; -    my_rec.height = 20; +  // Access struct members with . +  my_rec.width = 10; +  my_rec.height = 20; -    // You can declare pointers to structs -    struct rectangle *my_rec_ptr = &my_rec; +  // You can declare pointers to structs +  struct rectangle *my_rec_ptr = &my_rec; -    // Use dereferencing to set struct pointer members... -    (*my_rec_ptr).width = 30; +  // Use dereferencing to set struct pointer members... +  (*my_rec_ptr).width = 30; -    // ... or even better: prefer the -> shorthand for the sake of readability -    my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10; +  // ... or even better: prefer the -> shorthand for the sake of readability +  my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10;  }  // You can apply a typedef to a struct for convenience @@ -518,34 +516,34 @@ typedef struct rectangle rect;  int area(rect r)  { -    return r.width * r.height; +  return r.width * r.height;  }  // if you have large structs, you can pass them "by pointer" to avoid copying  // the whole struct: -int area(const rect *r) +int areaptr(const rect *r)  { -    return r->width * r->height; +  return r->width * r->height;  }  /////////////////////////////////////// -// Function pointers  +// Function pointers  ///////////////////////////////////////  /*  At run time, functions are located at known memory addresses. Function pointers are -much like any other pointer (they just store a memory address), but can be used  +much like any other pointer (they just store a memory address), but can be used  to invoke functions directly, and to pass handlers (or callback functions) around.  However, definition syntax may be initially confusing.  Example: use str_reverse from a pointer  */  void str_reverse_through_pointer(char *str_in) { -    // Define a function pointer variable, named f.  -    void (*f)(char *); // Signature should exactly match the target function. -    f = &str_reverse; // Assign the address for the actual function (determined at run time) -    // f = str_reverse; would work as well - functions decay into pointers, similar to arrays -    (*f)(str_in); // Just calling the function through the pointer -    // f(str_in); // That's an alternative but equally valid syntax for calling it. +  // Define a function pointer variable, named f. +  void (*f)(char *); // Signature should exactly match the target function. +  f = &str_reverse; // Assign the address for the actual function (determined at run time) +  // f = str_reverse; would work as well - functions decay into pointers, similar to arrays +  (*f)(str_in); // Just calling the function through the pointer +  // f(str_in); // That's an alternative but equally valid syntax for calling it.  }  /* @@ -557,39 +555,40 @@ typedef void (*my_fnp_type)(char *);  // Then used when declaring the actual pointer variable:  // ... -// my_fnp_type f;  +// my_fnp_type f;  //Special characters: -'\a' // alert (bell) character -'\n' // newline character -'\t' // tab character (left justifies text) -'\v' // vertical tab -'\f' // new page (form feed) -'\r' // carriage return -'\b' // backspace character -'\0' // NULL character. Usually put at end of strings in C.  -     //   hello\n\0. \0 used by convention to mark end of string.  -'\\' // backslash -'\?' // question mark -'\'' // single quote -'\"' // double quote -'\xhh' // hexadecimal number. Example: '\xb' = vertical tab character -'\ooo' // octal number. Example: '\013' = vertical tab character +/* +'\a'; // alert (bell) character +'\n'; // newline character +'\t'; // tab character (left justifies text) +'\v'; // vertical tab +'\f'; // new page (form feed) +'\r'; // carriage return +'\b'; // backspace character +'\0'; // NULL character. Usually put at end of strings in C. +//   hello\n\0. \0 used by convention to mark end of string. +'\\'; // backslash +'\?'; // question mark +'\''; // single quote +'\"'; // double quote +'\xhh'; // hexadecimal number. Example: '\xb' = vertical tab character +'\ooo'; // octal number. Example: '\013' = vertical tab character  //print formatting: -"%d"    // integer -"%3d"   // integer with minimum of length 3 digits (right justifies text) -"%s"    // string -"%f"    // float -"%ld"   // long -"%3.2f" // minimum 3 digits left and 2 digits right decimal float  -"%7.4s" // (can do with strings too) -"%c"    // char -"%p"    // pointer -"%x"    // hexadecimal -"%o"    // octal -"%%"    // prints %  - +"%d";    // integer +"%3d";   // integer with minimum of length 3 digits (right justifies text) +"%s";    // string +"%f";    // float +"%ld";   // long +"%3.2f"; // minimum 3 digits left and 2 digits right decimal float +"%7.4s"; // (can do with strings too) +"%c";    // char +"%p";    // pointer +"%x";    // hexadecimal +"%o";    // octal +"%%";    // prints % +*/  ///////////////////////////////////////  // Order of Evaluation  /////////////////////////////////////// diff --git a/de-de/haskell-de.html.markdown b/de-de/haskell-de.html.markdown new file mode 100644 index 00000000..df6267f9 --- /dev/null +++ b/de-de/haskell-de.html.markdown @@ -0,0 +1,425 @@ +--- +language: haskell +lang: de-de +contributors: +    - ["Adit Bhargava", "http://adit.io"] +translators: +    - ["Henrik Jürges", "https://github.com/santifa"] +filename: haskell-de.hs + +--- + +Haskell wurde als praktische und funktionale Sprache entworfen. +Es ist berühmt für das Schema der Monaden und des Typsystems, aber +es sticht vor allem die Einfachheit und Eleganz hervor. + +```haskell +-- Einfache Kommentare beginnen mit 2 Bindestriche. +{- So wird ein Kommentar +über mehrere Zeilen angelegt. +-} + +---------------------------------------------------- +-- 1. Primitive Datentypen und Operatoren +---------------------------------------------------- + +-- Normale Zahlen. +3 -- 3 + +-- Einfache Rechenoperationen. +1 + 1 -- 2 +8 - 1 -- 7 +10 * 2 -- 20 +35 / 5 -- 7.0 + +-- Die Division ist per se auf Fließkommazahlen. +35 / 4 -- 8.75 + +-- Ganzzahlige Division +35 `div` 4 -- 8 + +-- Boolesche Werte sind Primitiven. +True +False + +-- Logik Operationen +not True -- False +not False -- True +1 == 1 -- True +1 /= 1 -- False +1 < 10 -- True + +-- `not` ist eine Funktion die ein Argument entgegenimmt. +-- Haskell benötigt keine Klammern um Argumente. +-- Sie werden einfach aufgelistet: func arg1 arg2 arg3... +-- Wie man Funktionen definiert kommt weiter unten. + + +-- Strings und Zeichen +"Das ist ein String." +'a' -- Zeichen +'Einfache Anfuehrungszeichen gehen nicht.' -- error! + +-- Strings können konkateniert werden. +"Hello " ++ "world!" -- "Hello world!" + +-- Ein String ist eine Liste von Zeichen. +"Das ist eine String" !! 0 -- 'D' + + +---------------------------------------------------- +-- Listen und Tupel +---------------------------------------------------- + +-- Jedes Element einer Liste muss vom gleichen Typ sein. +-- Zwei gleiche Listen +[1, 2, 3, 4, 5] +[1..5] + +-- Haskell unterstuetzt unendliche Listen! +[1..] -- Die Liste aller natuerlichen Zahlen + +-- Unendliche Listen funktionieren in Haskell, da es "lazy evaluation" +-- unterstuetzt. Haskell evaluiert erst etwas, wenn es benötigt wird. +-- Somit kannst du nach dem 1000. Element fragen und Haskell gibt es dir: + +[1..] !! 999 -- 1000 + +-- Haskell evaluiert nun die ersten 1 - 1000 Elemente, aber der Rest der Liste +-- bleibt unangetastet. Haskell wird sie solange nicht weiterevalieren +-- bis es muss. + +-- Zwei Listen konkatenieren +[1..5] ++ [6..10] + +-- Ein Element als Head hinzufuegen +0:[1..5] -- [0, 1, 2, 3, 4, 5] + +-- Gibt den 5. Index zurueck +[0..] !! 5 -- 5 + +-- Weitere Listenoperationen +head [1..5] -- 1 +tail [1..5] -- [2, 3, 4, 5] +init [1..5] -- [1, 2, 3, 4] +last [1..5] -- 5 + +-- list comprehensions | Listen erschaffen +[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10] + +-- Mit Bedingungen +[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10] + +-- Tupel haben eine feste Länge, jedes Element darf aber ein anderen Typ haben. +-- Ein Tupel: +("haskell", 1) + +-- Auf Elemente eines Tupels zugreifen: +fst ("haskell", 1) -- "haskell" +snd ("haskell", 1) -- 1 + +---------------------------------------------------- +-- 3. Funktionen +---------------------------------------------------- +-- Eine einfache Funktion die zwei Argumente hat. +add a b = a + b + +-- Wenn man ghci (den Haskell Interpreter) benutzt, muss ein `let` davor. +-- let add a b = a + b + +-- Eine Funktion aufrufen +add 1 2 -- 3 + +-- Man kann eine Funktion auch Infix verwenden, +-- wenn man sie mit backticks umgibt +1 `add` 2 -- 3 + +-- So sieht die Definition eines eigenen Operators aus. +-- Also einer Funktion deren Name aus Symbolen besteht. +-- Die Integer Division: +(//) a b = a `div` b +35 // 4 -- 8 + +-- Guards sind eine einfache Möglichkeit fuer Fallunterscheidungen. +fib x +  | x < 2 = x +  | otherwise = fib (x - 1) + fib (x - 2) + +-- Pattern Matching funktioniert ähnlich. +-- Hier sind drei Definitionen von fib. Haskell wird automatisch +-- die erste Funktionen nehmen die dem Pattern der Eingabe entspricht. +fib 1 = 1 +fib 2 = 2 +fib x = fib (x - 1) + fib (x - 2) + +-- Pattern matching auf Tupeln: +foo (x, y) = (x + 1, y + 2) + +-- Pattern matching auf Listen. +-- `x` ist das erste Element der Liste und `xs` der Rest der Liste. +-- Damit können wir unsere eigene map Funktion bauen: +myMap func [] = [] +myMap func (x:xs) = func x:(myMap func xs) + +-- Anonyme Funktionen (Lambda-Funktionen) werden mit einem +-- Backslash eingeleitet, gefolgt von allen Argumenten. +myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] + +-- Fold (`inject` in einigen Sprachen) +-- Foldl1 bedeutet: fold von links nach rechts und nehme den ersten +-- Wert der Liste als Basiswert f[r den Akkumulator. +foldl1 (\acc x -> acc + x) [1..5] -- 15 + +---------------------------------------------------- +-- 4. Mehr Funktionen +---------------------------------------------------- + +-- currying: Wenn man nicht alle Argumente an eine Funktion uebergibt, +-- so wird sie eine neue Funktion gebildet ("curried"). +-- Es findet eine partielle Applikation statt und die neue Funktion +-- nimmt die fehlenden Argumente auf. + +add a b = a + b +foo = add 10 -- foo ist nun Funktion die ein Argument nimmt und 10 addiert +foo 5 -- 15 + +-- Ein alternativer Weg +foo = (+10) +foo 5 -- 15 + +-- Funktionskomposition +-- Die (.) Funktion verkettet Funktionen. +-- Zum Beispiel, die Funktion Foo nimmt ein Argument addiert 10 dazu und +-- multipliziert dieses Ergebnis mit 5. +foo = (*5) . (+10) + +-- (5 + 10) * 5 = 75 +foo 5 -- 75 + + +-- Haskell hat eine Funktion `$`. Diese ändert den Vorrang, +-- so dass alles links von ihr zuerst berechnet wird und +-- und dann an die rechte Seite weitergegeben wird. +-- Mit `.` und `$` kann man sich viele Klammern ersparen. + +-- Vorher +(even (fib 7)) -- true + +-- Danach +even . fib $ 7 -- true + +---------------------------------------------------- +-- 5. Typensystem +---------------------------------------------------- + +-- Haskell hat ein sehr starkes Typsystem. +-- Alles hat einen Typ und eine Typsignatur. + +-- Einige grundlegende Typen: +5 :: Integer +"hello" :: String +True :: Bool + +-- Funktionen haben genauso Typen. +-- `not` ist Funktion die ein Bool annimmt und ein Bool zurueckgibt: +-- not :: Bool -> Bool + +-- Eine Funktion die zwei Integer Argumente annimmt: +-- add :: Integer -> Integer -> Integer + +-- Es ist guter Stil zu jeder Funktionsdefinition eine +-- Typdefinition darueber zu schreiben: +double :: Integer -> Integer +double x = x * 2 + +---------------------------------------------------- +-- 6. If-Anweisung und Kontrollstrukturen +---------------------------------------------------- + +-- If-Anweisung: +haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome" + +-- If-Anweisungen können auch ueber mehrere Zeilen verteilt sein. +-- Das Einruecken ist dabei äußerst wichtig. +haskell = if 1 == 1 +            then "awesome" +            else "awful" + +-- Case-Anweisung: Zum Beispiel "commandline" Argumente parsen. +case args of +  "help" -> printHelp +  "start" -> startProgram +  _ -> putStrLn "bad args" + +-- Haskell nutzt Rekursion anstatt Schleifen. +-- map wendet eine Funktion auf jedes Element einer Liste an. + +map (*2) [1..5] -- [2, 4, 6, 8, 10] + +-- So kann man auch eine for-Funktion kreieren. +for array func = map func array + +-- und so benutzt man sie: +for [0..5] $ \i -> show i + +-- wir hätten sie auch so benutzen können: +for [0..5] show + +-- foldl oder foldr reduziren Listen auf einen Wert. +-- foldl <fn> <initial value> <list> +foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43 + +-- die Abarbeitung sieht so aus: +(2 * (2 * (2 * 4 + 1) + 2) + 3) + +-- foldl ist linksseitig und foldr rechtsseitig. +foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16 + +-- die Abarbeitung sieht so aus: +(2 * 3 + (2 * 2 + (2 * 1 + 4))) + +---------------------------------------------------- +-- 7. Datentypen +---------------------------------------------------- + +-- So kann man seine eigenen Datentypen in Haskell anlegen: + +data Color = Red | Blue | Green + +-- Nun können wir sie in einer Funktion benutzen. + +say :: Color -> String +say Red = "You are Red!" +say Blue = "You are Blue!" +say Green =  "You are Green!" + +-- Datentypen können auch Parameter aufnehmen: + +data Maybe a = Nothing | Just a + +-- Diese sind alle vom Typ Maybe: +Just "hello"    -- vom Typ `Maybe String` +Just 1          -- vom Typ `Maybe Int` +Nothing         -- vom Typ `Maybe a` fuer jedes `a` + +---------------------------------------------------- +-- 8. Haskell IO +---------------------------------------------------- + +-- IO kann nicht völlig erklärt werden ohne Monaden zu erklären, +-- aber man kann die grundlegenden Dinge erklären. + +-- Wenn eine Haskell Programm ausgefuehrt wird, so wird `main` aufgerufen. +-- Diese muss etwas vom Typ `IO ()` zurueckgeben. Zum Beispiel: + +main :: IO () +main = putStrLn $ "Hello, sky! " ++ (say Blue) +-- putStrLn hat den Typ String -> IO () + +-- Es ist am einfachsten, wenn man sein Programm als Funktion von +-- String nach String implementiert. +-- Zum Beispiel die Funktion interact :: (String -> String) -> IO () +-- nimmt einen Text, tut etwas damit und gibt diesen wieder aus. + +countLines :: String -> String +countLines = show . length . lines + +main' = interact countLines + +-- Man kann den Typ `IO ()` als Repräsentation einer Sequenz von +-- Aktionen sehen, die der Computer abarbeiten muss. +-- Wie bei einem Programm das in einer Imperativen Sprache geschreiben wurde. +-- Mit der `do` Notation können Aktionen verbunden werden. + +sayHello :: IO () +sayHello = do +   putStrLn "What is your name?" +   name <- getLine -- eine Zeile wird geholt und +                   -- an die Variable "name" gebunden +   putStrLn $ "Hello, " ++ name + +-- Uebung: Schreibe deine eigene Version von `interact`, +-- die nur eine Zeile einliest. + +-- `sayHello` wird niemals ausgefuehrt, nur `main` wird ausgefuehrt. +-- Um `sayHello` laufen zulassen kommentiere die Definition von `main` +-- aus und ersetze sie mit: +--     main = sayHello + +-- Lass uns untersuchen wie `getLine` arbeitet. +-- Der Typ ist: getLine :: IO String +-- Man kann sich vorstellen das der Wert vom Typ `IO a` ein +-- Programm repräsentiert das etwas vom Typ `a` generiert. +-- Der Wert wird mit `<-` gespeichert und kann wieder benutzt werden. +-- Wir könne auch eigene Funktionen vom Typ `IO String` definieren: + +action :: IO String +action = do +   putStrLn "This is a line. Duh" +   input1 <- getLine +   input2 <- getLine +   -- Der Typ von `do` ergibt sich aus der letzten Zeile. +   -- `return` ist eine Funktion und keine Schluesselwort +   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String + +-- Nun können wir `action` wie `getLine` benutzen: + +main'' = do +    putStrLn "I will echo two lines!" +    result <- action +    putStrLn result +    putStrLn "This was all, folks!" + +-- Der Typ `IO` ist ein Beispiel fuer eine Monade. +-- Haskell benutzt Monaden Seiteneffekte zu kapseln und somit +-- eine rein funktional Sprache zu sein. +-- Jede Funktion die mit der Außenwelt interagiert (z.B. IO) +-- hat den Typ `IO` in seiner Signatur. +-- Damit kann man zwischen "reinen" Funktionen (interagieren nicht +-- mit der Außenwelt oder ändern ihren Zustand) und Anderen unterscheiden. + +-- Nebenläufigkeit ist in Haskell sehr einfach, da reine Funktionen +-- leicht nebenläufig arbeiten können. + +---------------------------------------------------- +-- 9. Die Haskell REPL +---------------------------------------------------- + +-- Starte die REPL mit dem Befehl `ghci` +-- Nun kann man Haskell Code eingeben. +-- Alle neuen Werte muessen mit `let` gebunden werden: + +let foo = 5 + +-- `:t` zeigt den Typen von jedem Wert an: + +>:t foo +foo :: Integer + +-- Auch jede `IO ()` Funktion kann ausgefuehrt werden. + +> sayHello +What is your name? +Friend! +Hello, Friend! + +``` + +Es gibt noch viel mehr in Haskell, wie zum Beispiel Typklassen und Monaden. +Dies sind die Ideen durch die Haskell Programmierung zum Spaß wird. +Mit dem folgenden kleinen Beispiel werde ich euch verlassen: +Quicksort in Haskell: + +```haskell +qsort [] = [] +qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater +    where lesser  = filter (< p) xs +          greater = filter (>= p) xs +``` + +Haskell ist sehr einfach zu installieren. +Hohl es dir von [hier](http://www.haskell.org/platform/). + +Eine sehr viele langsamere Einfuehrung findest du unter: +[Learn you a Haskell](http://learnyouahaskell.com/) oder +[Real World Haskell](http://book.realworldhaskell.org/). diff --git a/es-es/css-es.html.markdown b/es-es/css-es.html.markdown new file mode 100644 index 00000000..31000785 --- /dev/null +++ b/es-es/css-es.html.markdown @@ -0,0 +1,244 @@ +--- +language: css +filename: learncss-es.css +contributors: +    - ["Mohammad Valipour", "https://github.com/mvalipour"] +    - ["Marco Scannadinari", "https://github.com/marcoms"] +translators: +    - ["Daniel Zendejas","https://github.com/DanielZendejas"] +lang: es-es +--- + +Tutorial de CSS en español + +En los primeros días de la web no había elementos visuales, todo +era texto plano. Pero después, con el desarrollo de los navegadores, +las páginas con contenido visual empezaron a ser más comunes. +CSS es el lenguaje estándar que existe para separar el contenido +(HTML) y el aspecto visual de las páginas web. + +Lo que CSS hace es proveer con una sintaxis que te permite apuntar a distintos  +elementos HTML y asignarles diferentes propiedades visuales. + +CSS, como cualquier otro lenguaje, tiene múltiples versiones. Aquí nos enfocamos +en CSS 2.0. No es la versión más reciente pero sí la más soportada y compatible. + +**NOTA:** Como los resultados de CSS son efectos visuales, para aprenderlo,  +necesitarás probar todo tipo de cosas en ambientes como  +[dabblet](http://dabblet.com/). Este artículo se enfoca, principalmente, en +la sintaxis y consejos generales. + +```css +/* ¡Los comentarios aparecen dentro de diagonal-asterisco, justo como esta línea! */ + +/* #################### +   ## SELECTORES +   ####################*/ + +/* Generalmente, la sentencia principal en CSS es muy simple. */ +selector { propiedad: valor; /* más propiedades separados por punto y coma...*/ } + +/* El selector es usado para apuntar a (seleccionar) un elemento en la página. + +¡Puedes apuntar a todos los elementos en la página con el asterisco! */ +* { color:red; } + +/* +Dado un elemento como este en la página: + +<div class='una-clase clase2' id='unaId' attr='valor' /> +*/ + +/* puedes seleccionar el <div> por el nombre de su clase */ +.una-clase { } + +/*¡O por sus dos clases! */ +.una-clase.clase2 { } + +/* O por el nombre de su elemento */ +div { } + +/* O por su Id */ +#unaId { } + +/* ¡O por el hecho de que tiene un atributo! */ +[attr] { font-size:smaller; } + +/* O por el hecho de que el atributo tiene un valor determinado */ +[attr='valor'] { font-size:smaller; } + +/* Empieza con un valor ('val' en este caso)*/ +[attr^='val'] { font-size:smaller; } + +/* O termina con un valor ('or' en este caso) */ +[attr$='or'] { font-size:smaller; } + +/* O incluso contiene un valor ('lo' en este caso) */ +[attr~='lo'] { font-size:smaller; } + +/*Más importante, puedes combinar estos criterios de búsqueda entre sí. +No debe existir ningún espacio entre estas partes porque hace que el  +significado cambie.*/ +div.una-clase[attr$='or'] { } + +/* También puedes seleccionar un elemento HTML basándote en sus padres*/ + +/* Un elemento que es hijo directo de otro elemento (Seleccionado de la forma que +vimos anteriormente) */ + +div.un-padre > .nombre-clase {} + +/* O cualquiera de sus ancestros en la jerarquía*/ +/* La siguiente sentencia selecciona a cualquier elemento que tenga una clase +"nombre-clase" y sea hijo de un div con clase "un-padre" EN CUALQUIER PROFUNDIDAD*/ +div.un-padre .nombre-clase {} + +/* advertencia: el mismo selector sin espacio tiene otro significado. ¿Puedes +identificar la diferencia?*/ + +/* También puedes seleccionar un elemento basado en su hermano inmediato previo*/ +.yo-estoy-antes + .este-elemento { } + +/*o cualquier hermano previo */ +.yo-soy-cualquiera-antes ~ .estes-elemento {} + +/* Existen algunas pseudo-clases que permiten seleccionar un elemento +basado en el comportamiendo de la página (a diferencia de la estructura de +la página) */ + +/* Por ejemplo, para cuando pasas el mouse por encima de un elemento */ +:hover {} + +/* o una liga visitada*/ +:visited {} + +/* o una liga no visitada aún*/ +:link {} + +/* o un elemento de un formulario que esté seleccionado */ +:focus {} + + +/* #################### +   ## PROPIEDADES +   ####################*/ + +selector { +     +    /* Unidades */ +    width: 50%; /* en porcentaje */ +    font-size: 2em; /* dos veces el tamaño de la fuente actual */ +    width: 200px; /* en pixeles */ +    font-size: 20pt; /* en puntos */ +    width: 5cm; /* en centimetros */ +    width: 50mm; /* en milimetros */ +    width: 5in; /* en pulgadas */ +     +    /* Colores */ +    background-color: #F6E;  /* en hexadecimal corto */ +    background-color: #F262E2; /* en hexadecimal largo */ +    background-color: tomato; /* puede ser un color con nombre */ +    background-color: rgb(255, 255, 255); /* en rgb */ +    background-color: rgb(10%, 20%, 50%); /* en rgb percent */ +    background-color: rgba(255, 0, 0, 0.3); /* en rgb semi-transparente (con valor alfa)*/ +     +    /* Imagenes */ +    background-image: url(/ruta-a-la-imagen/imagen.jpg); +     +    /* Fuentes */ +    font-family: Arial; +    font-family: "Courier New"; /* si el nombre contiene espacios, debe ir entre comillas */ +    font-family: "Courier New", Trebuchet, Arial; /* si la primera fuente no se encontró  +    entonces se busca la seguna, o la tercera, así recursivamente*/ +} + +``` + +## Uso + +Guarda cualquier CSS que quieras en un archivo con extensión `.css`. + +```xml +<!-- Necesitas incluir tu archivo CSS en el elemento <head> de tu HTML: --> +<link rel='stylesheet' type='text/css' href='ruta/archivoDeEstilos.css' /> + +<!-- +también puedes incluir CSS dentro del archivo HTML. Esta no es una buena práctica +y debe ser evitada. +--> +<style> +   selector { propiedad:valor; } +</style> + +<!-- +También se pueden aplicar propiedades al elemento directamente. +Esta práctica también debe ser evitada a toda costa +--> +<div style='propiedad:valor;'> +</div> + +``` + +## Preferencia y orden + +Como te habrás dado cuenta un elemento puede ser seleccionado por más +de un selector. En este caso alguna de las reglas cobra preferencia +sobre las otras: + +Dado el siguiente CSS: + +```css +/*A*/ +p.clase1[attr='valor'] + +/*B*/ +p.clase1 {} + +/*C*/ +p.clase2 {} + +/*D*/ +p {} + +/*E*/ +p { propiedad: valor !important; } + +``` + +Y el siguiente HTML: + +```xml +<p style='/*F*/ propiedad:valor;' class='clase1 clase2' attr='valor'> +</p> +``` + +El orden respetado es el siguiente:   +Recuerda, la preferencia es por cada **property**, no para el bloque completo. + +* `E` tiene la preferencia más elevada gracias a la palabra `!important`.   +	Es recomendado evitar esto a menos que sea estrictamente necesario incluirlo. +* `F` le sigue, porque es estilo incrustado directamente en el HTML. +* `A` le sigue, porque es más específico que cualquier otra opción.   +	más específico = más especificadores. Aquí hay tres especificadores: elemento `p` +    +	nombre de la clase `clase1` + un atributo `attr='valor'` +* `C` le sigue. Aunque tiene el mismo número de especificadores como `B`   +	pero aparece después. +* Luego va `B` +* y al final  `D`. + +## Compatibilidad + +La mayoría de las funcionalidades de CSS2 (y gradualmente de CSS3) son compatibles  +en todos los navegadores y dispositivos. Pero siempre es vital tener en mente la +compatibilidad y disponibilidad del CSS que uses con respecto a los navegadores +y dispositivos para los que desarrolles. + + +[QuirksMode CSS](http://www.quirksmode.org/css/) es una excelente referencia para esto. + +## Referencias + +* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/) +* [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) + diff --git a/es-es/json-es.html.markdown b/es-es/json-es.html.markdown new file mode 100644 index 00000000..fff678eb --- /dev/null +++ b/es-es/json-es.html.markdown @@ -0,0 +1,59 @@ +--- +language: json +filename: learnjson-es.json +contributors: +  - ["Anna Harren", "https://github.com/iirelu"] +  - ["Marco Scannadinari", "https://github.com/marcoms"] +translators: +  - ["Daniel Zendejas","https://github.com/DanielZendejas"] +lang: es-es +--- + +Siendo JSON un formato de intercambio de infomación tan sencillo, probablemente este será el Learn X in Y más sencillo jamás. + +JSON en su forma más pura no tiene comentarios, pero la mayoría de los parseadores aceptarán comentarios de C (//, /\* \*/). De todas formas, para el propóstio de esto todo será JSON 100% válido. Por suerte, habla por sí mismo. + +```json + +{ +  "llave": "valor", +   +  "llaves": "siempre debe estar entre comillas (ya sean dobles o simples)", +  "numeros": 0, +  "strings": "Høla, múndo. Todo el unicode está permitido, así como \"escapar\".", +  "soporta booleanos?": true, +  "vacios": null, + +  "numero grande": 1.2e+100, + +  "objetos": { +    "comentario": "La mayoria de tu estructura vendra de objetos.", + +    "arreglo": [0, 1, 2, 3, "Los arreglos pueden contener cualquier cosa.", 5], + +    "otro objeto": { +      "comentario": "Estas cosas pueden estar anidadas, muy util." +    } +  }, + +  "tonteria": [ +    { +      "fuentes de potasio": ["bananas"] +    }, +    [ +      [1, 0, 0, 0], +      [0, 1, 0, 0], +      [0, 0, 1, "neo"], +      [0, 0, 0, 1] +    ] +  ], +   +  "estilo alternativo": { +    "comentario": "Mira esto!" +  , "posicion de la coma": "no importa - mientras este antes del valor, entonces sera valido" +  , "otro comentario": "que lindo" +  }, + +  "eso fue rapido": "Y, estas listo. Ahora sabes todo lo que JSON tiene para ofrecer." +} +``` diff --git a/es-es/livescript-es.html.markdown b/es-es/livescript-es.html.markdown new file mode 100644 index 00000000..103a3142 --- /dev/null +++ b/es-es/livescript-es.html.markdown @@ -0,0 +1,339 @@ +--- +language: LiveScript +filename: learnLivescript-es.ls +contributors: +    - ["Christina Whyte", "http://github.com/kurisuwhyte/"] +translators: +    - ["Daniel Zendejas", "http://github.com/DanielZendejas/"] +lang: es-es +--- + +LiveScript es un lenguaje funcional compilado sobre Javascript. Comparte +la mayoría de la semántica con este mismo lenguaje. Composición de funciones, +comparación de patrones y muchas otras cosas son las adiciones que hace +LiveScript. Está inspirado en lenguajes como Haskell, F# y Scala. + +Livescript es un bifurcación de [Coco][], que en sí mismo es una bifurcación +de [CoffeeScript][]. El lenguaje es estable, y una nueva versión está en  +desarrollo activo para traer aún más funciones. + +[Coco]: http://satyr.github.io/coco/ +[CoffeeScript]: http://coffeescript.org/ + +La retroalimentación siempre es bienvenida, así que sientete libre de +contactarme en [@kurisuwhyte](https://twitter.com/kurisuwhyte) :) + +```coffeescript +# Justo como su primo CoffeeScript, LiveScript usa símbolos de gato para +# comentarios de una sola línea + +/* + Comentarios multi-línea son escritos con estilo de C. Usa este estilo si quieres + que los comentarios se preserven en el output de Javascript + */ +``` +```coffeescript +# En lo que a la sintaxis se refiere, LiveScript usa indentación para delimitar +# bloques en lugar de llaves {} y espacios para aplicar funciones, en lugar de +# paréntesis. + +######################################################################## +## 1. Valores básicos +######################################################################## + +# La carencia de valor se define con la palabra `void` en lugar de `undefined` +void            # igual que `undefined` pero más seguro (no puede ser sobre escrito) + +# Ningún valor válido se representa con Null. +null + +# El valor básico más pequeño es de tipo lógico:  +true +false + +# Y tiene múltiples alias que significan lo mismo: +on; off +yes; no + +# Luego vienen los números. Estos número con punto flotante tienen la misma  +# precisión que los de JS: +10 +0.4     # Note que el `0` al inicio es requerido + +# Para fines de fácil lectura, puedes usar guiones bajos y sufijos en un +# número, y estos serán ignorados por el compilador. +12_344km + +# Los Strings son secuencias de caracteres inmutables, como en JS: +"Christina"             # ¡Los apóstrofes están bien! +"""Strings +   de muchas +   líneas +   están +   bien +   también.""" + +# A veces quieres codificar un palabra clave, la diagonal invertida sirve para esto: +\keyword                # => 'keyword' + + +# Los arreglos son colecciones ordenadas de datos: +frutas = +  * \manzana +  * \naranja +  * \pera + +# Una forma más concisa de representarlos son con corchetes:  +frutas = [ \manzana, \naranja, \pera ] + +# Esta es una conveniente de crear listas de Strings, usando +# espacio en blanco para delimitar los items: +frutas = <[ manzana naranja pera ]> + +# Puedes recuperar un item usando su índice (empezando en 0): +frutas[0]       # => "manzana" + +# Los objetos son colecciones de pares llave/valor sin ordenar, entre otras cosas, +# (detallaremos más al respecto en un momento): +persona = +  nombre: "Christina" +  gusta: +    * "gatitos" +    * "otras cosas" + +# Otra vez, puedes expresar el objeto con más consistencia con llaves {}: +persona = {nombre: "Christina", gusta: ["gatitos", "otras cosas"]} + +# Puedes conseguir un valor por medio de su llave: +persona.nombre    # => "Christina" +persona["nombre"]  # => "Christina" + + +# Las expresiones regulares tienen la misma sintaxis que en JavaScript: +expresion-regular = /\s$/ + +# A excepción de que puedes hacer expresiones de múltiples líneas +# (los comentarios y espacios se ignoran): +expresion-regular = // +        function\s+(.+)         # nombre +        \s* \((.*)\) \s*        # argumentos +        { (.*) }                # cuerpo +        // + + +######################################################################## +## 2. Operaciones básicas +######################################################################## + +# Los operadores aritméticos son los mismos que en JavaScript: +1 + 2   # => 3 +2 - 1   # => 1 +2 * 3   # => 6 +4 / 2   # => 2 +3 % 2   # => 1 + + +# Las comparaciones son casi las mismas, excepto `==` que es igual +# a `===` en. El operador `==` de JS en LiveScript es `~=`, y `===` +# permite comparaciones entre objetos y arreglos, y también  +# comparasiones más estrictas: +2 == 2          # => true +2 == "2"        # => false +2 ~= "2"        # => true +2 === "2"       # => false + +[1,2,3] == [1,2,3]        # => false +[1,2,3] === [1,2,3]       # => true + ++0 == -0     # => true ++0 === -0    # => false + +# Otros operadores relacionales incluyen <, <=, > and >= + +# Los valores lógicos pueden ser combinados mediante los operadores  +# lógicos `or`, `and` and `not`:  +true and false  # => false +false or true   # => true +not false       # => true + +# Las colecciones también tienen algunos operadores adicionales:  +[1, 2] ++ [3, 4]                # => [1, 2, 3, 4] +'a' in <[ a b c ]>              # => true +'nombre' of { nombre: 'Chris' }     # => true + + +######################################################################## +## 3. Funciones +########################################################################         + +# Como LiveScript es funcional, uno esperaría que las funciones recibirían +# un buen tratamiento. En LiveScript es más que aparente que las funciones +# son de primera clase:  +suma = (primerElemento, segundoElemento) -> primerElemento + segundoElemento +add 1, 2        # => 3 + +# Las funciones que no reciben argumentos son llamadas rápidamente +dos = -> 2 +dos! + +# LiveScript, al igual que JS, aplica ámbitos (alcance) a sus variables y  +# tiene cierres (closures) también. A diferencia de JavaScript, el operador +# `=` sirve como declaración y siempre declarará la variable en lado izquierdo. + +# El operador `:=` está disponible para *reusar* un nombre del ámbito del padre. + +# Puedes acceder a los argumentos de una función para conseguir +# los datos internos de una estructura de datos rápidamente:  +cola = ([cabeza, ...resto]) -> resto +cola [1, 2, 3]  # => [2, 3] + +# También puedes transformar argumentos usando operadores binarios o unarios. +# Argumentos por defecto también son posibles +foo = (a = 1, b = 2) -> a + b +foo!    # => 3 + +# También puedes usarlo para clonar un argumento en particular para evitar efectos +# secundarios, por ejemplo:  +copiar = (^^objetivo, fuente) -> +  for k,v of fuente => objetivo[k] = v +  objetivo +a = { a: 1 } +copiar a, { b: 2 }        # => { a: 1, b: 2 } +a                       # => { a: 1 } + +# Una función puede ser abreviada usando una flecha larga en lugar de una corta: +suma = (primerElemento, segundoElemento) --> primerElemento + segundoElemento +sumaAbreviada = suma 1 +sumaAbreviada 2          # => 3 + +# Las funciones obtienen un argumento `it` implícito, incluso si no declaras +# ningún argument +identidad = -> it +identidad 1      # => 1 + +# Los operadores no son funciones en LiveScript. ¡Pero se pueden convertir fácilmente +# en una! Presentamos el seccionamiento de operadores:  +dividir-entre-2 = (/ 2) +[2, 4, 8, 16].map(dividir-entre-2) .reduce (+) + +# LiveScript vive de otras cosas aparte de las funciones. Como en cualquier lenguaje  +# funcional obtienes medios para componer (juntar) funciones:  +doble-menos-uno = (- 1) . (* 2) + +# A parte de la clásica fórmula matemática `f . g`, también cuentas co los operadores +# `>>` y `<<`, que describen el flujo de los valores dentro de las funciones: +double-minus-one = (* 2) >> (- 1) +double-minus-one = (- 1) << (* 2) + +# Hablando del flujo de los valores, LiveScript también tiene los operadores `|>` y `<|` +# que aplican un valor a una función: +map = (f, xs) --> xs.map f +[1 2 3] |> map (* 2)            # => [2 4 6] + +# También puedes escoger dónde quieres que el valor se posicione, sólo márcalo con un +# guíon bajo: +reducir = (f, xs, initial) --> xs.reducir f, initial +[1 2 3] |> reducir (+), _, 0     # => 6 + +# El guíon bajo también se usa para apartar lugares para tus argumentos, por ejemplo:  +division = (dividendo,divisor) -> dividendo / divisor +dividir-entre-2 = division _, 2 +dividir-entre-2 4      # => 2 + +# Por último, LiveScript tiene back-calls (útiles mecanismos para hacer +# callbacks.). A pesar de esto deberías intentar formas más funcionales de hacerlo, +# como Promises: +leerArchivo = (nombre, f) -> f name +a <- leerArchivo 'foo' +b <- leerArchivo 'bar' +console.log a + b + +# Igual a: +leerArchivo 'foo', (a) -> leerArchivo 'bar', (b) -> console.log a + b + + +######################################################################## +## 4. Patrones, guardias y control de flujo +######################################################################## + +# Puedes bifurcar cálculos con la expresión `if...else`: +x = if n > 0 then \positive else \negative + +# En lugar de `then`, puedes usar `=>` +x = if n > 0 => \positivo +    else        \negativo +     +# A pesar de esto, a las condiciones complejas es mejor expresarlas con el `switch`: +y = {} +x = switch +  | (typeof y) is \number => \numero +  | (typeof y) is \string => \string +  | 'length' of y         => \arreglo +  | otherwise             => \objeto      # `otherwise` y `_` son lo mismo. + +# Los cuerpos de las funciones, declaraciones y asignaciones tienen un `switch` por defecto, +# así que no necesitas escribirlo nuevamente:  + +take = (n, [x, ...xs]) --> +                        | n == 0 => [] +                        | _      => [x] ++ take (n - 1), xs + + +######################################################################## +## 5. Comprehensions (Auxiliares) +######################################################################## + +# Mientras que los auxiliares funcionales (para lidiar con listas y objetos) +# están en la librería estándar de JavaScript (y complementada con prelude-ls, +# que es una "librería estándar" de LiveScipt) los "comprehensions" (Auxiliares) +# usualemente te permiten hacer lo mismo pero más rápido y con una sintaxis más +# comprehensible (de ahí su nombre en inglés): +unoAVeinte = [1 to 20] +pares       = [x for x in oneToTwenty when x % 2 == 0] + +# `when` y `unless` pueden ser usados como filtros en el auxiliar. + +# Los auxiliares para objetos funcionan de la misma forma, excepto que regresa un  +# objeto en lugar de un arreglo: +copiar = { [k, v] for k, v of source } + + +######################################################################## +## 4. PROGRAMACIÓN ORIENTADA A OBJETOS +######################################################################## + +# Mientras que LiveScript es un lenguaje funcional en la mayoría de los  +# aspectos, también brinda ayudas para la programación orientada a objetos. +# Algunas de estas ayudas son la sintaxis para las clases y un poco de "azucar" +# para las clases heredada de CoffeeScript: +class Animal +  (@nombre, tipo) -> +    @tipo = tipo +  action: (accion) -> "*#{@nombre} (un #{@tipo}) #{accion}*" + +class Gato extends Animal +  (@nombre) -> super @nombre, 'gato' +  ronronear: -> @action 'ronronea' + +gatito = new Gato 'Mei' +gatito.purr!      # => "*Mei (un gato) ronronea*" + +# A parte del clásico patrón de herencia simple, también puedes proveer +# cuantas mezclas quieras para una clase. Las mezclas sólo son objetos: +Abrazable = +  abrazar: -> @action 'es abrazado' + +class GatoAbrazable extends Gato implements Abrazable + +gatito = new GatoAbrazable 'Ronroneo' +gatito.abrazar!     # => "*Mei (un gato) es abrazado*" +``` + +## Más recursos + +Existe mucho más sobre LiveScript, pero esto debe bastar para que empieces. +El [sitio oficial](http://livescript.net/) tiene mucha información sobre el  +lenguaje, y un compilador en linea para que pruebes cosas inmediatamente. + +También querras probar un poco de [prelude.ls](http://gkz.github.io/prelude-ls/),  +y probar el canal `#livescript` en la red Freenode. diff --git a/es-es/markdown-es.html.markdown b/es-es/markdown-es.html.markdown new file mode 100644 index 00000000..3865126c --- /dev/null +++ b/es-es/markdown-es.html.markdown @@ -0,0 +1,252 @@ +--- +language: markdown +filename: markdown-es.md +contributors: +    - ["Dan Turkel", "http://danturkel.com/"] +translators: +    - ["Daniel Zendejas", "https://github.com/DanielZendejas"] +lang: es-es +--- + +Markdown fue creado por John Gruber en 2004. Su propósito es ser una sintaxis fácil de leer y escribir que se convierta +fácilmente a HTML (y, actualmente, otros formatos también). + +¡Denme todo la retroalimentación que quieran! / ¡Sientanse en la libertad de hacer forks o pull requests! + + +``` +<!-- 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  +así si creas un elemento HTML en tu archivo Markdown no podrás usar sintaxis +Markdown dentro de él. --> + +<!-- La implementación de Markdown cambia de acuerdo al parseador. Esta +guía servirá para clarificar cuales características son universales y +cuales son específicas de cada parseador--> + +<!-- Headers --> +<!-- Puedes crear headers HTML fácilmente precediendo al texto con una serie +de símbolos de números (#)--> + +# Esto es un <h1> +## Esto es un <h2> +### Esto es un <h3> +#### Esto es un <h4> +##### Esto es un <h5> +###### Esto es un <h6> + +<!-- Markdown también nos proveé con dos alternativas para indicar h1 y h2 --> +Esto es un h1 +============= + +Esto es un h2 +------------- + +<!-- Estilos para texto plano --> +<!-- El texto puede ser fácilmente estilizaedo con italicas, negritas o tachado  +usando markdown --> + +*Este texto está en itálicas.* +_Al igual que este texto._ + +**Este texto está en negritas.** +__Al igual que este texto.__ + +***Este texto tiene ambos estilos.*** +**_Al igual que este!_** +*__¡Y este!__* + +<!-- En Github Flavored Markdown, el cual es usado para mostrar archivos  +Markdown en Github, también tenemos: --> + +~~Este texto está tachado.~~ + +<!-- Los párrafos son una o múltuples líneas de texto adyacentes separadas por  +una o múltiples líneas en blanco--> + +Este es un párrafo. Estoy escribiendo un párrafo, ¿No es divertido? + +Ahora estoy en el párrafo dos. +¡Sigo en el párrafo dos! + +¡Estoy en el párrafo tres! + +<!-- Si en algún momento quieres insertar un break HTML <br />, puedes terminar +un párrafo con dos o más espacios y luego empieza un párrafo nuevo--> + +Termino con dos espacios (selecciona esta línea completa para que los veas).   + +¡Hay un <br /> arriba de mí! + +<!-- Las citas de bloque son fáciles y se pueden hacer con el caracter >. --> + +> Esta es una cita de bloque. Puedes +> envolver tus líneas manualmente y poner un `>` antes de cada línea o puedes dejar que tus líneas sean muy largas y que se envuelvan solas. +> No hay diferencia, siempre y cuando empiecen con `>`. + +> ¿También puedes usar más de un nivel +>> de indentación? +> Esto es muy útil ¿No? + +<!-- Listas --> +<!-- Las listas desordenadas se hacen usando asteriscos, símbolos de más, + o guiones --> + +* Item +* Item +* Otro item + +o + ++ Item ++ Item ++ Un item más + +o + +- Item +- Item +- El último item + +<!-- Las listas ordenadas se logran con un número seguido de un punto --> + +1. Item uno +2. Item dos +3. Item tres + +<!-- Aunque Markdown mostrará los items correctamente en orden, esto no +es una buena idea --> + +1. Item uno +1. Item dos +1. Item tres +<!-- (Esto muestra lo mismo que el ejemplo de arriba) --> + +<!-- También puedes usar sub-listas --> + +1. Item uno +2. Item dos +3. Item tres +    * Sub-item +    * Sub-item +4. Item cuatro + +<!-- Bloques de código --> +<!-- Puedes indicar un bloque de código (usan los elementos <code>) indentando  +una línea con cuatro espacios o un tab--> + +    Esto es código +    Esto también + +<!-- También puedes insertar dos tabs (o cuatro espacios adicionales) +para indentar dentro del código --> + +    my_array.each do |item| +        puts item +    end + +<!-- Código dentro de la línea puede ser escrito usando la comilla ` --> + +¡John no sabía lo que la función `go_to()` hacía! + +<!-- Con Github Flavored Markdown, puedes usar una sintaxis especial para código --> + +\`\`\`ruby <!-- quita esas comillas cuando lo hagas, deja sólo ```ruby ! --> +def foobar +    puts "Hello world!" +end +\`\`\` <!-- aquí también, sin comillas, sólo ``` --> + +<!-- El texto de arriba no necesita indentación, aparte Github usará +resaltará la sintaxis del lenguaje que especifiques después de ``` --> + +<!-- Regla horizontal (<hr />) --> +<!-- Las reglas horizontales se agregan fácilmente con tres o más asteriscos o guiones, +con o sin espacios. --> + +*** +--- +- - -  +**************** + +<!-- Ligas --> +<!-- Una de las mejores cosas de Markdown es la facilidad para hacer ligas. Pon +el texto a mostrar en corchetes [] seguidos por la URL en paréntesis () --> + +[¡Haz click!](http://test.com/) + +<!-- También puedes agregar el titulo de la liga usando comillas dentro de los paréntesis --> + +[¡Haz click!](http://test.com/ "Liga al test.com") + +<!-- También funcionan las rutas relativas. --> + +[Ir a la música](/music/). + +<!-- Markdown también soporta ligas con estilo de referencia --> + +¡[Has click a esta liga][liga1] para más información! +[También mira esta liag][foobar] si quieres. + + + + +<!-- El título también puede estar en comillas simples o dentro de paréntesis, +también se pueden omitir completamente. Las referencias pueden estar en cualquier +lugar en tu documento y los IDs de referencia pueden ser lo que sea mientras sean únicos. --> + +<!-- También hay "nombramiento implicito" el cual te permite usar el texto de la liga como id --> + +[Esta][] es una liga. + + + +<!-- Pero no se usa comúnmente. --> + +<!-- Imagenes --> +<!-- Las imagenes se hacen de la misma forma que las ligas pero con un símbolo de exclamaciónal frente! --> + + + +<!-- Y el estilo de referencia funciona como se espera --> + +![Esta es una etiqueta.][myimage] + + + +<!-- Misceláneos --> +<!-- Auto-ligas --> + +<http://testwebsite.com/> equivale a +[http://testwebsite.com/](http://testwebsite.com/) + +<!-- Auto-ligas para correos electrónicos --> + +<foo@bar.com> + +<!-- Escapando caracteres --> + +Quiero escribir *este texto rodeado por asteriscos* pero no quiero que esté en itálicas, +así que hago esto: \*Este texto está rodeado de asteriscos\*. + +<!-- Tablas --> +<!-- Las tablas sólo están disponibles en Github Flavored Markdown y son un poco pesadas, +pero si de verdad las quieres: --> + +| Col1         | Col2     | Col3          | +| :----------- | :------: | ------------: | +| Izquierda | Centrado | Derecha | +| blah         | blah     | blah          | + +<!-- o, para los mismos resultados --> + +Col 1 | Col2 | Col3 +:-- | :-: | --: +Ugh esto es feo | has que | pare. + +<!-- ¡El fin! --> + +``` + +Para más información, mira el post oficial de John Gruber's [aquí](http://daringfireball.net/projects/markdown/syntax) y la gran referencia de Adam Pritchard's [aquí](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet). diff --git a/fr-fr/csharp-fr.html.markdown b/fr-fr/csharp-fr.html.markdown index c1641716..e51eacc8 100644 --- a/fr-fr/csharp-fr.html.markdown +++ b/fr-fr/csharp-fr.html.markdown @@ -7,7 +7,7 @@ contributors:      - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]  translators:      - ["Olivier Hoarau", "https://github.com/Olwaro"] -filename: LearnCSharp.cs +filename: LearnCSharp-fr.cs  lang: fr-fr  --- diff --git a/go.html.markdown b/go.html.markdown index 4ea6861b..a1be08af 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -9,6 +9,7 @@ contributors:      - ["Jesse Johnson", "https://github.com/holocronweaver"]      - ["Quint Guvernator", "https://github.com/qguv"]      - ["Jose Donizetti", "https://github.com/josedonizetti"] +    - ["Alexej Friesen", "https://github.com/heyalexej"]  ---  Go was created out of the need to get work done.  It's not the latest trend @@ -33,87 +34,88 @@ package main  // Import declaration declares library packages referenced in this file.  import ( -    "fmt"      // A package in the Go standard library. -    "net/http" // Yes, a web server! -    "strconv"  // String conversions. -    m "math"   // Math library with local alias m. +	"fmt"       // A package in the Go standard library. +	"io/ioutil" // Implements some I/O utility functions. +	m "math"    // Math library with local alias m. +	"net/http"  // Yes, a web server! +	"strconv"   // String conversions.  )  // A function definition.  Main is special.  It is the entry point for the  // 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. -    fmt.Println("Hello world!") +	// Println outputs a line to stdout. +	// Qualify it with the package name, fmt. +	fmt.Println("Hello world!") -    // Call another function within this package. -    beyondHello() +	// Call another function within this package. +	beyondHello()  }  // Functions have parameters in parentheses.  // If there are no parameters, empty parentheses are still required.  func beyondHello() { -    var x int // Variable declaration.  Variables must be declared before use. -    x = 3     // Variable assignment. -    // "Short" declarations use := to infer the type, declare, and assign. -    y := 4 -    sum, prod := learnMultiple(x, y)        // Function returns two values. -    fmt.Println("sum:", sum, "prod:", prod) // Simple output. -    learnTypes()                            // < y minutes, learn more! +	var x int // Variable declaration. Variables must be declared before use. +	x = 3     // Variable assignment. +	// "Short" declarations use := to infer the type, declare, and assign. +	y := 4 +	sum, prod := learnMultiple(x, y)        // Function returns two values. +	fmt.Println("sum:", sum, "prod:", prod) // Simple output. +	learnTypes()                            // < y minutes, learn more!  }  // Functions can have parameters and (multiple!) return values.  func learnMultiple(x, y int) (sum, prod int) { -    return x + y, x * y // Return two values. +	return x + y, x * y // Return two values.  }  // Some built-in types and literals.  func learnTypes() { -    // Short declaration usually gives you what you want. -    s := "Learn Go!" // string type. +	// Short declaration usually gives you what you want. +	s := "Learn Go!" // string type. -    s2 := `A "raw" string literal +	s2 := `A "raw" string literal  can include line breaks.` // Same string type. -    // Non-ASCII literal.  Go source is UTF-8. -    g := 'Σ' // rune type, an alias for uint32, holds a unicode code point. +	// Non-ASCII literal.  Go source is UTF-8. +	g := 'Σ' // rune type, an alias for uint32, holds a unicode code point. -    f := 3.14195 // float64, an IEEE-754 64-bit floating point number. -    c := 3 + 4i  // complex128, represented internally with two float64's. +	f := 3.14195 // float64, an IEEE-754 64-bit floating point number. +	c := 3 + 4i  // complex128, represented internally with two float64's. -    // Var syntax with an initializers. -    var u uint = 7 // Unsigned, but implementation dependent size as with int. -    var pi float32 = 22. / 7 +	// Var syntax with an initializers. +	var u uint = 7 // Unsigned, but implementation dependent size as with int. +	var pi float32 = 22. / 7 -    // Conversion syntax with a short declaration. -    n := byte('\n') // byte is an alias for uint8. +	// Conversion syntax with a short declaration. +	n := byte('\n') // byte is an alias for uint8. -    // 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 of 3 ints, initialized as shown. +	// 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 of 3 ints, initialized as shown. -    // 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. -    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. +	// 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. +	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. -    p, q := learnMemory() // Declares p, q to be type pointer to int. -    fmt.Println(*p, *q)   // * follows a pointer.  This prints two ints. +	p, q := learnMemory() // Declares p, q to be type pointer to int. +	fmt.Println(*p, *q)   // * follows a pointer.  This prints two ints. -    // Maps are a dynamically growable associative array type, like the -    // hash or dictionary types of some other languages. -    m := map[string]int{"three": 3, "four": 4} -    m["one"] = 1 +	// Maps are a dynamically growable associative array type, like the +	// hash or dictionary types of some other languages. +	m := map[string]int{"three": 3, "four": 4} +	m["one"] = 1 -    // Unused variables are an error in Go. -    // The underbar lets you "use" a variable but discard its value. -    _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs -    // Output of course counts as using a variable. -    fmt.Println(s, c, a4, s3, d2, m) +	// Unused variables are an error in Go. +	// The underbar lets you "use" a variable but discard its value. +	_, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs +	// Output of course counts as using a variable. +	fmt.Println(s, c, a4, s3, d2, m) -    learnFlowControl() // Back in the flow. +	learnFlowControl() // Back in the flow.  }  // It is possible, unlike in many other languages for functions in go @@ -122,240 +124,249 @@ can include line breaks.` // Same string type.  // allows us to easily return from multiple points in a function as well as to  // only use the return keyword, without anything further.  func learnNamedReturns(x, y int) (z int) { -    z = x * y -    return              // z is implicit here, because we named it earlier. +	z = x * y +	return // z is implicit here, because we named it earlier.  }  // Go is fully garbage collected.  It has pointers but no pointer arithmetic.  // You can make a mistake with a nil pointer, but not by incrementing a pointer.  func learnMemory() (p, q *int) { -    // Named return values p and q have type pointer to int. -    p = new(int) // Built-in function new allocates memory. -    // The allocated int is initialized to 0, p is no longer nil. -    s := make([]int, 20) // Allocate 20 ints as a single block of memory. -    s[3] = 7             // Assign one of them. -    r := -2              // Declare another local variable. -    return &s[3], &r     // & takes the address of an object. +	// Named return values p and q have type pointer to int. +	p = new(int) // Built-in function new allocates memory. +	// The allocated int is initialized to 0, p is no longer nil. +	s := make([]int, 20) // Allocate 20 ints as a single block of memory. +	s[3] = 7             // Assign one of them. +	r := -2              // Declare another local variable. +	return &s[3], &r     // & takes the address of an object.  }  func expensiveComputation() float64 { -    return m.Exp(10) +	return m.Exp(10)  }  func learnFlowControl() { -    // If statements require brace brackets, and do not require parens. -    if true { -        fmt.Println("told ya") -    } -    // Formatting is standardized by the command line command "go fmt." -    if false { -        // Pout. -    } else { -        // Gloat. -    } -    // Use switch in preference to chained if statements. -    x := 42.0 -    switch x { -    case 0: -    case 1: -    case 42: -        // Cases don't "fall through". -    case 43: -        // Unreached. -    } -    // Like if, for doesn't use parens either. -    // Variables declared in for and if are local to their scope. -    for x := 0; x < 3; x++ { // ++ is a statement. -        fmt.Println("iteration", x) -    } -    // x == 42 here. - -    // For is the only loop statement in Go, but it has alternate forms. -    for { // Infinite loop. -        break    // Just kidding. -        continue // Unreached. -    } -    // As with for, := in an if statement means to declare and assign y first, -    // then test y > x. -    if y := expensiveComputation(); y > x { -        x = y -    } -    // Function literals are closures. -    xBig := func() bool { -        return x > 10000 // References x declared above switch statement. -    } -    fmt.Println("xBig:", xBig()) // true (we last assigned e^10 to x). -    x = 1.3e3                    // This makes x == 1300 -    fmt.Println("xBig:", xBig()) // false now. - -    // When you need it, you'll love it. -    goto love +	// If statements require brace brackets, and do not require parens. +	if true { +		fmt.Println("told ya") +	} +	// Formatting is standardized by the command line command "go fmt." +	if false { +		// Pout. +	} else { +		// Gloat. +	} +	// Use switch in preference to chained if statements. +	x := 42.0 +	switch x { +	case 0: +	case 1: +	case 42: +		// Cases don't "fall through". +	case 43: +		// Unreached. +	} +	// Like if, for doesn't use parens either. +	// Variables declared in for and if are local to their scope. +	for x := 0; x < 3; x++ { // ++ is a statement. +		fmt.Println("iteration", x) +	} +	// x == 42 here. + +	// For is the only loop statement in Go, but it has alternate forms. +	for { // Infinite loop. +		break    // Just kidding. +		continue // Unreached. +	} +	// As with for, := in an if statement means to declare and assign +	// y first, then test y > x. +	if y := expensiveComputation(); y > x { +		x = y +	} +	// Function literals are closures. +	xBig := func() bool { +		return x > 10000 // References x declared above switch statement. +	} +	fmt.Println("xBig:", xBig()) // true (we last assigned e^10 to x). +	x = 1.3e3                    // This makes x == 1300 +	fmt.Println("xBig:", xBig()) // false now. + +	// What's more is function literals may be defined and called inline, +	// acting as an argument to function, as long as: +	// a) function literal is called immediately (), +	// b) result type matches expected type of argument. +	fmt.Println("Add + double two numbers: ", +		func(a, b int) int { +			return (a + b) * 2 +		}(10, 2)) // Called with args 10 and 2 +	// => Add + double two numbers:  24 + +	// When you need it, you'll love it. +	goto love  love: -    learnFunctionFactory() // func returning func is fun(3)(3) -    learnDefer() // A quick detour to an important keyword. -    learnInterfaces() // Good stuff coming up! +	learnFunctionFactory() // func returning func is fun(3)(3) +	learnDefer()      // A quick detour to an important keyword. +	learnInterfaces() // Good stuff coming up!  }  func learnFunctionFactory() { -    // Next two are equivalent, with second being more practical -    fmt.Println(sentenceFactory("summer")("A beautiful", "day!")) +	// Next two are equivalent, with second being more practical +	fmt.Println(sentenceFactory("summer")("A beautiful", "day!")) -    d := sentenceFactory("summer") -    fmt.Println(d("A beautiful", "day!")) -    fmt.Println(d("A lazy", "afternoon!")) +	d := sentenceFactory("summer") +	fmt.Println(d("A beautiful", "day!")) +	fmt.Println(d("A lazy", "afternoon!"))  }  // Decorators are common in other languages. Same can be done in Go  // with function literals that accept arguments.  func sentenceFactory(mystring string) func(before, after string) string { -    return func(before, after string) string { -        return fmt.Sprintf("%s %s %s", before, mystring, after) // new string -    } +	return func(before, after string) string { +		return fmt.Sprintf("%s %s %s", before, mystring, after) // new string +	}  }  func learnDefer() (ok bool) { -    // Deferred statements are executed just before the function returns. -    defer fmt.Println("deferred statements execute in reverse (LIFO) order.") -    defer fmt.Println("\nThis line is being printed first because") -    // Defer is commonly used to close a file, so the function closing the file -    // stays close to the function opening the file -    return true +	// Deferred statements are executed just before the function returns. +	defer fmt.Println("deferred statements execute in reverse (LIFO) order.") +	defer fmt.Println("\nThis line is being printed first because") +	// Defer is commonly used to close a file, so the function closing the +	// file stays close to the function opening the file. +	return true  }  // Define Stringer as an interface type with one method, String.  type Stringer interface { -    String() string +	String() string  }  // Define pair as a struct with two fields, ints named x and y.  type pair struct { -    x, y int +	x, y int  }  // Define a method on type pair.  Pair now implements Stringer.  func (p pair) String() string { // p is called the "receiver" -    // Sprintf is another public function in package fmt. -    // Dot syntax references fields of p. -    return fmt.Sprintf("(%d, %d)", p.x, p.y) +	// Sprintf is another public function in package fmt. +	// Dot syntax references fields of p. +	return fmt.Sprintf("(%d, %d)", p.x, p.y)  }  func learnInterfaces() { -    // Brace syntax is a "struct literal."  It evaluates to an initialized -    // struct.  The := syntax declares and initializes p to this struct. -    p := pair{3, 4} -    fmt.Println(p.String()) // Call String method of p, of type pair. -    var i Stringer          // Declare i of interface type Stringer. -    i = p                   // Valid because pair implements Stringer -    // Call String method of i, of type Stringer.  Output same as above. -    fmt.Println(i.String()) - -    // Functions in the fmt package call the String method to ask an object -    // for a printable representation of itself. -    fmt.Println(p) // Output same as above. Println calls String method. -    fmt.Println(i) // Output same as above. - -    learnVariadicParams("great", "learning", "here!") +	// Brace syntax is a "struct literal."  It evaluates to an initialized +	// struct.  The := syntax declares and initializes p to this struct. +	p := pair{3, 4} +	fmt.Println(p.String()) // Call String method of p, of type pair. +	var i Stringer          // Declare i of interface type Stringer. +	i = p                   // Valid because pair implements Stringer +	// Call String method of i, of type Stringer.  Output same as above. +	fmt.Println(i.String()) + +	// Functions in the fmt package call the String method to ask an object +	// for a printable representation of itself. +	fmt.Println(p) // Output same as above. Println calls String method. +	fmt.Println(i) // Output same as above. + +	learnVariadicParams("great", "learning", "here!")  }  // Functions can have variadic parameters.  func learnVariadicParams(myStrings ...interface{}) { -    // Iterate each value of the variadic. -    // The underbar here is ignoring the index argument of the array. -    for _, param := range myStrings { -        fmt.Println("param:", param) -    } +	// Iterate each value of the variadic. +	// The underbar here is ignoring the index argument of the array. +	for _, param := range myStrings { +		fmt.Println("param:", param) +	} -    // Pass variadic value as a variadic parameter. -    fmt.Println("params:", fmt.Sprintln(myStrings...)) +	// Pass variadic value as a variadic parameter. +	fmt.Println("params:", fmt.Sprintln(myStrings...)) -    learnErrorHandling() +	learnErrorHandling()  }  func learnErrorHandling() { -    // ", ok" idiom used to tell if something worked or not. -    m := map[int]string{3: "three", 4: "four"} -    if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map. -        fmt.Println("no one there") -    } else { -        fmt.Print(x) // x would be the value, if it were in the map. -    } -    // An error value communicates not just "ok" but more about the problem. -    if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value -        // prints 'strconv.ParseInt: parsing "non-int": invalid syntax' -        fmt.Println(err) -    } -    // We'll revisit interfaces a little later.  Meanwhile, -    learnConcurrency() +	// ", ok" idiom used to tell if something worked or not. +	m := map[int]string{3: "three", 4: "four"} +	if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map. +		fmt.Println("no one there") +	} else { +		fmt.Print(x) // x would be the value, if it were in the map. +	} +	// An error value communicates not just "ok" but more about the problem. +	if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value +		// prints 'strconv.ParseInt: parsing "non-int": invalid syntax' +		fmt.Println(err) +	} +	// We'll revisit interfaces a little later.  Meanwhile, +	learnConcurrency()  }  // c is a channel, a concurrency-safe communication object.  func inc(i int, c chan int) { -    c <- i + 1 // <- is the "send" operator when a channel appears on the left. +	c <- i + 1 // <- is the "send" operator when a channel appears on the left.  }  // We'll use inc to increment some numbers concurrently.  func learnConcurrency() { -    // Same make function used earlier to make a slice.  Make allocates and -    // initializes slices, maps, and channels. -    c := make(chan int) -    // Start three concurrent goroutines.  Numbers will be incremented -    // concurrently, perhaps in parallel if the machine is capable and -    // properly configured.  All three send to the same channel. -    go inc(0, c) // go is a statement that starts a new goroutine. -    go inc(10, c) -    go inc(-805, c) -    // Read three results from the channel and print them out. -    // There is no telling in what order the results will arrive! -    fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator. - -    cs := make(chan string)       // Another channel, this one handles strings. -    ccs := make(chan chan string) // A channel of string channels. -    go func() { c <- 84 }()       // Start a new goroutine just to send a value. -    go func() { cs <- "wordy" }() // Again, for cs this time. -    // Select has syntax like a switch statement but each case involves -    // a channel operation.  It selects a case at random out of the cases -    // that are ready to communicate. -    select { -    case i := <-c: // The value received can be assigned to a variable, -        fmt.Printf("it's a %T", i) -    case <-cs:     // or the value received can be discarded. -        fmt.Println("it's a string") -    case <-ccs:    // Empty channel, not ready for communication. -        fmt.Println("didn't happen.") -    } -    // At this point a value was taken from either c or cs.  One of the two -    // goroutines started above has completed, the other will remain blocked. - -    learnWebProgramming() // Go does it. You want to do it too. +	// Same make function used earlier to make a slice.  Make allocates and +	// initializes slices, maps, and channels. +	c := make(chan int) +	// Start three concurrent goroutines.  Numbers will be incremented +	// concurrently, perhaps in parallel if the machine is capable and +	// properly configured.  All three send to the same channel. +	go inc(0, c) // go is a statement that starts a new goroutine. +	go inc(10, c) +	go inc(-805, c) +	// Read three results from the channel and print them out. +	// There is no telling in what order the results will arrive! +	fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator. + +	cs := make(chan string)       // Another channel, this one handles strings. +	ccs := make(chan chan string) // A channel of string channels. +	go func() { c <- 84 }()       // Start a new goroutine just to send a value. +	go func() { cs <- "wordy" }() // Again, for cs this time. +	// Select has syntax like a switch statement but each case involves +	// a channel operation.  It selects a case at random out of the cases +	// that are ready to communicate. +	select { +	case i := <-c: // The value received can be assigned to a variable, +		fmt.Printf("it's a %T", i) +	case <-cs: // or the value received can be discarded. +		fmt.Println("it's a string") +	case <-ccs: // Empty channel, not ready for communication. +		fmt.Println("didn't happen.") +	} +	// At this point a value was taken from either c or cs.  One of the two +	// goroutines started above has completed, the other will remain blocked. + +	learnWebProgramming() // Go does it. You want to do it too.  }  // A single function from package http starts a web server.  func learnWebProgramming() { -    // First parameter of ListenAndServe is TCP address to listen to. -    // Second parameter is an interface, specifically http.Handler. -    go func() { -        err := http.ListenAndServe(":8080", pair{}) -        fmt.Println(err) // don't ignore errors -    }() +	// First parameter of ListenAndServe is TCP address to listen to. +	// Second parameter is an interface, specifically http.Handler. +	go func() { +		err := http.ListenAndServe(":8080", pair{}) +		fmt.Println(err) // don't ignore errors +	}() -    requestServer(); +	requestServer()  } -  // Make pair an http.Handler by implementing its only method, ServeHTTP.  func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) { -    // Serve data with a method of http.ResponseWriter. -    w.Write([]byte("You learned Go in Y minutes!")) +	// Serve data with a method of http.ResponseWriter. +	w.Write([]byte("You learned Go in Y minutes!"))  }  func requestServer() { -    resp, err := http.Get("http://localhost:8080") -    fmt.Println(err) -    defer resp.Body.Close() -    body, err := ioutil.ReadAll(resp.Body) -    fmt.Printf("\nWebserver said: `%s`", string(body)) +	resp, err := http.Get("http://localhost:8080") +	fmt.Println(err) +	defer resp.Body.Close() +	body, err := ioutil.ReadAll(resp.Body) +	fmt.Printf("\nWebserver said: `%s`", string(body))  }  ``` diff --git a/ja-jp/bash-jp.html.markdown b/ja-jp/bash-jp.html.markdown new file mode 100644 index 00000000..88e5ff1c --- /dev/null +++ b/ja-jp/bash-jp.html.markdown @@ -0,0 +1,172 @@ +--- +category: tool +tool: bash +contributors: +    - ["Max Yankov", "https://github.com/golergka"] +    - ["Darren Lin", "https://github.com/CogBear"] +    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] +    - ["Denis Arh", "https://github.com/darh"] +translators: +    - ["akirahirose", "https://twitter.com/akirahirose"] +filename: LearnBash-jp.sh +lang: ja-jp +--- + +Bash はunixシェルの1つです。GNUオペレーションシステムのシェルとして配布されています。 +LinuxやMac OS Xの、デフォルトシェルにもなっています。 +以下にある例は、ほぼ全部シェルスクリプトの一部として使えます。また、一部はそのままシェルから実行できます。 + +[ちゃんとした説明は、こちらをどうぞ](http://www.gnu.org/software/bash/manual/bashref.html) + +```bash +#!/bin/bash +# 最初の行はShebang(シェバング、シバン)というもので、システムに対して何を使って実行するのかを教えるためのものです +# ちゃんとした説明、こちらをどうぞ: http://en.wikipedia.org/wiki/Shebang_(Unix) +# 既にお気づきのように、コメント行は#で始まります. Shebangもコメントです + +# まずは Hello world です +echo Hello world! + +# コマンド毎に改行を入れるか、セミコロンで区切ります +echo 'This is the first line'; echo 'This is the second line' + +# 変数の宣言はこのようにやります +VARIABLE="Some string" + +# が、以下のようにやってはダメです +VARIABLE = "Some string" +# このように(空白を入れて)書くと、Bash はVARIABLEを実行するべきコマンドとみなし、実行します。 +# そして、VARIABLEというコマンドはない(はずな)ので、エラーになります + + +# 変数の使い方 +echo $VARIABLE +echo "$VARIABLE" +echo '$VARIABLE' +# 変数の値(中身)を使わず、変数名だけを使うとき(代入するときや渡すときなど)は、$なしで変数名を書いてください +# 変数の値(中身)を使うときは、$をつけます +# 上記例の最後にある、' (シングルクォート) で囲んだ場合は、変数の値は表示されません! + +# 変数値の文字列置換 +echo ${VARIABLE/Some/A} +# 最初に出てくる "Some" を "A" で置換します + +# 変数値の一部を取り出します +echo ${VARIABLE:0:7} +# 最初の7文字だけを取り出します + +# デフォルトの変数値設定(訳注:シェル実行時に引数で変数値を設定できるが、設定しなかった場合の値を指定できる) +echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"} +# 上記は、FOO番目の引数がnullだったとき(FOO番目=)や、空文字が指定されたとき(FOO番目="")に、変数に0を入れます +# ( 当然ですが、引数に0を指定したとき(FOO番目=0) も、0は入ります) + +# 組込み変数 +# 以下のような便利な組込み変数があります +echo "Last program return value: $?" +echo "Script's PID: $$" +echo "Number of arguments: $#" +echo "Scripts arguments: $@" +echo "Scripts arguments seperated in different variables: $1 $2..." + +# 入力値の読み込み +echo "What's your name?" +read NAME # 新しく変数を宣言する必要はないことに注意 +echo Hello, $NAME! + +# 普通のif文も使えます +# 利用できる判定条件については、'man test' で参照してください +if [ $NAME -ne $USER ] +then +    echo "Your name is your username" +else +    echo "Your name isn't your username" +fi + +# 他にも、条件判定ができます +echo "Always executed" || echo "Only executed if first command fails" +echo "Always executed" && echo "Only executed if first command does NOT fail" + +# 数式は以下のように書きます +echo $(( 10 + 5 )) + +# 他のプログラム言語とは違ってbashはシェルなので、現在いるディレクトリ位置が異なると、異なる結果になります +# lsコマンドで、現在いるディレクトリにあるファイルと、ディレクトリのリストが取得できます +ls + +# これらのコマンドには、実行オプションがいろいろあります +ls -l # ファイルとディレクトリのリストを行に分けて表示します + +# あるコマンド実行による返値を、次のコマンドの入力値としてつかえます +# 例えばですが、lsコマンドの返値を、grepコマンドによって指定したルールに基づいてフィルタできます。 +# 以下は、現在いるディレクトリにある、.txtファイルのリストを表示する例です +ls -l | grep "\.txt" + +# コマンドに対する入力元や出力先、またエラー出力先などを変更できます +python2 hello.py < "input.in" +python2 hello.py > "output.out" +python2 hello.py 2> "error.err" +# 出力先として指定したファイルが既に存在する場合は、上書きされます +# もしもファイルに追記したい場合は、代わりに">>" を使ってください + +# コマンド文中で、$()内に別コマンドを入れると、その別コマンドの返値をコマンド文の一部として使う事ができます +# 次のコマンドは、現在いるディレクトリにあるファイルの数を表示します +echo "There are $(ls | wc -l) items here." + +# バッククォート(backticks) `` でも同じことができますが、入れ子にはできません +# そのため、$()がお勧めです +echo "There are `ls | wc -l` items here." + +# BashはJavaやC++のように、case文による分岐ができます +case "$VARIABLE" in  +    #分岐条件として使いたいパターンを並べてください +    0) echo "There is a zero.";; +    1) echo "There is a one.";; +    *) echo "It is not null.";; +esac + +# 指定した回数、処理を繰り返し +# 変数の値 $VARIABLE が3回表示されます +for VARIABLE in {1..3} +do +    echo "$VARIABLE" +done + +# while ループです +while [true] +do +    echo "loop body here..." +    break +done + +# 関数の定義もできます +function foo () +{ +    echo "Arguments work just like script arguments: $@" +    echo "And: $1 $2..." +    echo "This is a function" +    return 0 +} + +# 以下のように、もっと簡単な書き方もあります +bar () +{ +    echo "Another way to declare functions!" +    return 0 +} + +# 自作関数を呼びます +foo "My name is" $NAME + +# 他にもいろいろと、知っておくと便利なコマンドがあります +# file.txtの最後10行を表示します +tail -n 10 file.txt +# file.txtの最初10行を表示します +head -n 10 file.txt +# file.txt's の行を並び替えます +sort file.txt +# 重複している行を表示するか、削除できます。-dオプションをつけると、表示します +uniq -d file.txt +# 1行ごとに、','が最初に出てくる前の部分を表示します +cut -d ',' -f 1 file.txt + +``` diff --git a/ja-jp/r-jp.html.markdown b/ja-jp/r-jp.html.markdown new file mode 100644 index 00000000..3e6621f5 --- /dev/null +++ b/ja-jp/r-jp.html.markdown @@ -0,0 +1,775 @@ +--- +language: R +contributors: +    - ["e99n09", "http://github.com/e99n09"] +    - ["isomorphismes", "http://twitter.com/isomorphisms"] +translators: +    - ["akirahirose", "https://twitter.com/akirahirose"] +filename: learnr-jp.r +lang: ja-jp +--- + + +R は統計計算用の言語です。 +データの取得やクリーニング、統計処理やグラフ作成をするために便利な、たくさんのライブラリがあります。また、LaTeX文書からRコマンドを呼び出すこともできます + + +```python +# コメント行は、#で開始します + + +# 複数行をまとめてコメントにすることはできないので、 +# コメントを複数の行に分けたい場合、このように、単に毎行をコメントにしてください + + +# WindowsやMacでは、 COMMAND-ENTERで、コマンドを1行実行できます + + + + + + +############################################################################# +# プログラミングがわからなくとも使えるコマンド類 +############################################################################# + + +# この節では、プログラミングがわからなくとも使える便利なRコマンドを紹介します +# 全てを理解できなくとも、まずはやってみましょう! + + +data()                # 既にロードされているデータを閲覧します +data(rivers)        # "北米にある大きな川の長さ"データを取得します +ls()                # "rivers" がワークスペースに表示されました +head(rivers)        # データの先頭部分です +# 735 320 325 392 524 450 + + +length(rivers)        # 何本の川がデータにある? +# 141 +summary(rivers) # 統計的に要約するとどうなる? +#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.  +#  135.0   310.0   425.0   591.2   680.0  3710.0  + + +# 茎葉図(ヒストグラムに似た図)を描く +stem(rivers) + + +#  The decimal point is 2 digit(s) to the right of the | +# +#   0 | 4 +#   2 | 011223334555566667778888899900001111223333344455555666688888999 +#   4 | 111222333445566779001233344567 +#   6 | 000112233578012234468 +#   8 | 045790018 +#  10 | 04507 +#  12 | 1471 +#  14 | 56 +#  16 | 7 +#  18 | 9 +#  20 |  +#  22 | 25 +#  24 | 3 +#  26 |  +#  28 |  +#  30 |  +#  32 |  +#  34 |  +#  36 | 1 + + +stem(log(rivers)) # このデータは、正規分布でも対数正規分布でもないので、注意! +# 特に正規分布原理主義のみなさん + + +#  The decimal point is 1 digit(s) to the left of the | +# +#  48 | 1 +#  50 |  +#  52 | 15578 +#  54 | 44571222466689 +#  56 | 023334677000124455789 +#  58 | 00122366666999933445777 +#  60 | 122445567800133459 +#  62 | 112666799035 +#  64 | 00011334581257889 +#  66 | 003683579 +#  68 | 0019156 +#  70 | 079357 +#  72 | 89 +#  74 | 84 +#  76 | 56 +#  78 | 4 +#  80 |  +#  82 | 2 + + +# ヒストグラム作成 +hist(rivers, col="#333333", border="white", breaks=25) # これらのパラメータをつかいます +hist(log(rivers), col="#333333", border="white", breaks=25) # いろいろな使い方ができます + + +# 別のロード済データでやってみましょう。Rには、いろいろなデータがロードされています。 +data(discoveries) +plot(discoveries, col="#333333", lwd=3, xlab="Year", +     main="Number of important discoveries per year") +plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year", +     main="Number of important discoveries per year") + + +# 年次のソートだけではなく、 +# 標準的な並べ替えもできます +sort(discoveries) +#  [1]  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2 +# [26]  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3 +# [51]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4 +# [76]  4  4  4  4  5  5  5  5  5  5  5  6  6  6  6  6  6  7  7  7  7  8  9 10 12 + + +stem(discoveries, scale=2) +#  +#  The decimal point is at the | +# +#   0 | 000000000 +#   1 | 000000000000 +#   2 | 00000000000000000000000000 +#   3 | 00000000000000000000 +#   4 | 000000000000 +#   5 | 0000000 +#   6 | 000000 +#   7 | 0000 +#   8 | 0 +#   9 | 0 +#  10 | 0 +#  11 |  +#  12 | 0 + + +max(discoveries) +# 12 +summary(discoveries) +#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.  +#    0.0     2.0     3.0     3.1     4.0    12.0  + + +# サイコロを振ります +round(runif(7, min=.5, max=6.5)) +# 1 4 6 1 4 6 4 +# 私と同じrandom.seed(31337)を使わない限りは、別の値になります + + +# ガウス分布を9回生成します +rnorm(9) +# [1]  0.07528471  1.03499859  1.34809556 -0.82356087  0.61638975 -1.88757271 +# [7] -0.59975593  0.57629164  1.08455362 + + + + + + +################################################## +# データ型と基本計算 +################################################## + + +# ここからは、プログラミングをつかうチュートリアルです +# この節ではRで重要なデータ型(データクラス)の、整数型、数字型、文字型、論理型と因子(ファクター)型をつかいます +# 他にもいろいろありますが、これらの必要最小限なものから始めましょう + + +# 整数型 +# 整数型はLで指定します +5L # 5 +class(5L) # "integer" +# (?class を実行すると、class()関数について、さらなる情報が得られます) +# Rでは、この5Lのような1つの値は、長さ1のベクトルとして扱われます +length(5L) # 1 +# 整数型のベクトルはこのようにつくります +c(4L, 5L, 8L, 3L) # 4 5 8 3 +length(c(4L, 5L, 8L, 3L)) # 4 +class(c(4L, 5L, 8L, 3L)) # "integer" + + +# 数字型 +# 倍精度浮動小数点数です +5 # 5 +class(5) # "numeric" +# しつこいですが、すべてはベクトルです +# 1つ以上の要素がある数字のベクトルも、作ることができます +c(3,3,3,2,2,1) # 3 3 3 2 2 1 +# 指数表記もできます +5e4 # 50000 +6.02e23 # アボガドロ数 +1.6e-35 # プランク長 +# 無限大、無限小もつかえます +class(Inf)        # "numeric" +class(-Inf)        # "numeric" +# 例のように、"Inf"を使ってください。integrate( dnorm(x), 3, Inf); +# Z-スコア表が必要なくなります + + +# 基本的な計算 +# 数を計算できます +# 整数と整数以外の数字を両方使った計算をすると、結果は整数以外の数字になります +10L + 66L # 76      # 整数足す整数は整数 +53.2 - 4  # 49.2    # 整数引く数字は数字 +2.0 * 2L  # 4       # 数字かける整数は数字 +3L / 4    # 0.75    # 整数割る数字は数字 +3 %% 2          # 1       # 二つの数字を割った余りは数字 +# 不正な計算は "not-a-number"になります +0 / 0 # NaN +class(NaN) # "numeric" +# 長さが1より大きなベクター同士の計算もできます +# どちらかが長い場合、短い方は何度も繰り返して使われます +c(1,2,3) + c(1,2,3) # 2 4 6 + +# 文字 +# Rでは、文字列と文字に区別がありません +"Horatio" # "Horatio" +class("Horatio") # "character" +class('H') # "character" +# 上記は両方とも、長さ1のベクターです +# 以下は、より長い場合です +c('alef', 'bet', 'gimmel', 'dalet', 'he') +# => +# "alef"   "bet"    "gimmel" "dalet"  "he" +length(c("Call","me","Ishmael")) # 3 +# 正規表現処理を文字ベクターに適用できます +substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis " +gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis." +# Rはいくつかの文字ベクターを組み込みで持っています +letters +# => +#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" +# [20] "t" "u" "v" "w" "x" "y" "z" +month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" + + +# 論理 +# Rでは、Booleanは論理(logical)型です +class(TRUE)        # "logical" +class(FALSE)        # "logical" +# 以下は比較演算子の例です +TRUE == TRUE        # TRUE +TRUE == FALSE        # FALSE +FALSE != FALSE        # FALSE +FALSE != TRUE        # TRUE +# 無いデータ (NA) も論理型です +class(NA)        # "logical" +# 以下のようにすると、複数の要素を持つ、論理型ベクターが返ります +c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE +c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE + + +# 因子(ファクター) +# 因子型は、カテゴリカルデータ用の型です +# 因子には、子供の学年のように順序がつけられるものか、性別のように順序がないものがあります +factor(c("female", "female", "male", "NA", "female")) +#  female female male   NA     female +# Levels: female male NA +# "levels" は、カテゴリカルデータがとりうる値を返します +levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male"   "NA"  +# 因子ベクターの長さが1ならば、そのlevelも1です +length(factor("male")) # 1 +length(levels(factor("male"))) # 1 +# 因子型は、この後で紹介するデータフレーム(というデータ型)内で、よくみられます +data(infert) # "Infertility after Spontaneous and Induced Abortion" +levels(infert$education) # "0-5yrs"  "6-11yrs" "12+ yrs" + + +# NULL +# "NULL" は特殊な型なのですが、ベクターを空にするときに使います +class(NULL)        # NULL +parakeet +# => +# [1] "beak"     "feathers" "wings"    "eyes"     +parakeet <- NULL +parakeet +# => +# NULL + + +# 型の強制 +# 型の強制とは、ある値を、強制的に別の型として利用する事です +as.character(c(6, 8)) # "6" "8" +as.logical(c(1,0,1,1)) # TRUE FALSE  TRUE  TRUE +# さまざまな要素が入っているベクターに対して型の強制を行うと、おかしなことになります +c(TRUE, 4) # 1 4 +c("dog", TRUE, 4) # "dog"  "TRUE" "4" +as.numeric("Bilbo") +# => +# [1] NA +# Warning message: +# NAs introduced by coercion  + + +# 追記: ここで紹介したのは、基本的な型だけです +# 実際には、日付(dates)や時系列(time series)など、いろいろな型があります + + + + + + +################################################## +# 変数、ループ、もし/ほかに(if/else) +################################################## + + +# 変数は、ある値を後で使うために入れておく、箱のようなものです +# 箱に入れることを、変数に値を代入する、といいます +# 変数を使うと、ループや関数、if/else 分岐を利用できます + + +# 変数 +# 代入する方法はいろいろあります +x = 5 # これはできます +y <- "1" # これがおすすめです +TRUE -> z # これも使えますが、ちょっとわかりにくいですね + + +# ループ +# forでループできます +for (i in 1:4) { +  print(i) +} +# whileでループできます +a <- 10 +while (a > 4) { +        cat(a, "...", sep = "") +        a <- a - 1 +} +# Rでは、forやwhileは遅いことを覚えておいてください +# ベクターを丸ごと処理する(つまり、行全体や、列全体を指定して処理する)か、 +# 後述する、apply()系の関数を使うのが、速度的にはお勧めです + + +# IF/ELSE +# ごく普通のif文です +if (4 > 3) { +        print("4 is greater than 3") +} else { +        print("4 is not greater than 3") +} +# => +# [1] "4 is greater than 3" + + +# 関数 +# 以下のように定義します +jiggle <- function(x) { +        x = x + rnorm(1, sd=.1)        #すこしだけ(制御された)ノイズを入れます +        return(x) +} +# 他の関数と同じように、呼びます +jiggle(5)        # 5±ε.  set.seed(2716057)をすると、jiggle(5)==5.005043 + + + + + + +########################################################################### +# データ構造: ベクター、行列、データフレーム、配列 +########################################################################### + + +# 1次元 + + +# まずは基本からです。ご存じベクターからです +vec <- c(8, 9, 10, 11) +vec        #  8  9 10 11 +# 特定の要素を、[角括弧]による指定で取り出せます +# (Rでは、最初の要素は1番目と数えます) +vec[1]                # 8 +letters[18]        # "r" +LETTERS[13]        # "M" +month.name[9]        # "September" +c(6, 8, 7, 5, 3, 0, 9)[3]        # 7 +# 特定のルールに当てはまる要素を見つけることもできます +which(vec %% 2 == 0)        # 1 3 +# 最初か最後の数個を取り出すこともできます +head(vec, 1)        # 8 +tail(vec, 2)        # 10 11 +# ある値がベクターにあるかどうかをみることができます +any(vec == 10) # TRUE +# ベクターの数より大きなインデックスを指定すると、NAが返ります +vec[6]        # NA +# ベクターの長さは、length()で取得できます +length(vec)        # 4 +# ベクター全体、または1部に対して、操作ができます +vec * 4        # 16 20 24 28 +vec[2:3] * 5        # 25 30 +any(vec[2:3] == 8) # FALSE +# R には、ベクターにある値を要約するための様々な関数があります +mean(vec)        # 9.5 +var(vec)        # 1.666667 +sd(vec)                # 1.290994 +max(vec)        # 11 +min(vec)        # 8 +sum(vec)        # 38 +# 他にも、ベクター関連ではいろいろな関数があります。以下はベクターをつくるための方法です +5:15        # 5  6  7  8  9 10 11 12 13 14 15 +seq(from=0, to=31337, by=1337) +# => +#  [1]     0  1337  2674  4011  5348  6685  8022  9359 10696 12033 13370 14707 +# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751 + + +# 2次元配列 (すべての値が同じ型の場合) + + +# 同じ型の値が含まれる2次元配列は、このように作れます +mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6)) +mat +# => +#      [,1] [,2] +# [1,]    1    4 +# [2,]    2    5 +# [3,]    3    6 +# ベクターとは違い、2次元配列の型名は"matrix"です。 +class(mat) # => "matrix" +# 最初の行 +mat[1,]        # 1 4 +# 最初の列に対する操作 +3 * mat[,1]        # 3 6 9 +# 特定のセルを取り出し +mat[3,2]        # 6 + + +# 2次元配列全体を転置します +t(mat) +# => +#      [,1] [,2] [,3] +# [1,]    1    2    3 +# [2,]    4    5    6 + + +# 2次元配列の積 +mat %*% t(mat) +# => +#      [,1] [,2] [,3] +# [1,]   17   22   27 +# [2,]   22   29   36 +# [3,]   27   36   45 + + +# cbind() は、複数のベクターを、別々の列に並べて2次元配列を作ります +mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog")) +mat2 +# => +#      [,1] [,2]    +# [1,] "1"  "dog"   +# [2,] "2"  "cat"   +# [3,] "3"  "bird"  +# [4,] "4"  "dog" +class(mat2)        # matrix +# ここでいま1度、2次元配列内の型について注意してください! +# 2次元配列にある値は、すべて同じ型にする必要があります。そのため、すべて文字型に変換されています +c(class(mat2[,1]), class(mat2[,2])) + + +# rbind() は、複数のベクターを、別々の行に並べて2次元配列を作ります +mat3 <- rbind(c(1,2,4,5), c(6,7,0,4)) +mat3 +# => +#      [,1] [,2] [,3] [,4] +# [1,]    1    2    4    5 +# [2,]    6    7    0    4 +# 全ての値は同じ型になります。上記例は幸い、強制変換がされないものでした + + +# 2次元配列 (いろいろな型を含む場合) + + +# 異なる型の値を含む配列をつくりたい場合、データフレームを使ってください +# データフレームは、統計処理を行うプログラムをする際にとても便利です +# Pythonでも、 "pandas"というパッケージにて、似たものが利用可能です + + +students <- data.frame(c("Cedric","Fred","George","Cho","Draco","Ginny"), +                       c(3,2,2,1,0,-1), +                       c("H", "G", "G", "R", "S", "G")) +names(students) <- c("name", "year", "house") #カラム名 +class(students)        # "data.frame" +students +# => +#     name year house +# 1 Cedric    3     H +# 2   Fred    2     G +# 3 George    2     G +# 4    Cho    1     R +# 5  Draco    0     S +# 6  Ginny   -1     G +class(students$year)        # "numeric" +class(students[,3])        # "factor" +# 行と列の数をみます +nrow(students)        # 6 +ncol(students)        # 3 +dim(students)        # 6 3 +# このdata.frame() 関数は、デフォルトでは文字列ベクターを因子ベクターに変換します +# stringsAsFactors = FALSE に設定してからデータフレームを作成すると、変換されません +?data.frame + + +# データフレームの1部を取り出すには、いろいろな(変な)、似たような方法があります +students$year        # 3  2  2  1  0 -1 +students[,2]        # 3  2  2  1  0 -1 +students[,"year"]        # 3  2  2  1  0 -1 + + +# データフレームの拡張版が、データテーブルです。 +# 大きなデータやパネルデータ、データセットの結合が必要な場合には、データテーブルを使うべきです。 +# 以下に駆け足で説明します +install.packages("data.table") # CRANからパッケージをダウンロードします +require(data.table) # ロードします +students <- as.data.table(students) +students # 若干異なる出力がされることに注意 +# => +#      name year house +# 1: Cedric    3     H +# 2:   Fred    2     G +# 3: George    2     G +# 4:    Cho    1     R +# 5:  Draco    0     S +# 6:  Ginny   -1     G +students[name=="Ginny"] # name == "Ginny"の行を取り出します +# => +#     name year house +# 1: Ginny   -1     G +students[year==2] # year == 2の行を取り出します +# => +#      name year house +# 1:   Fred    2     G +# 2: George    2     G +# データテーブルは、二つのデータセットを結合するのにも便利です +# 結合用に、生徒データが入った別のデータテーブルをつくります +founders <- data.table(house=c("G","H","R","S"), +                       founder=c("Godric","Helga","Rowena","Salazar")) +founders +# => +#    house founder +# 1:     G  Godric +# 2:     H   Helga +# 3:     R  Rowena +# 4:     S Salazar +setkey(students, house) +setkey(founders, house) +students <- founders[students] # 二つのデータテーブルを、"house"をキーとして結合します +setnames(students, c("house","houseFounderName","studentName","year")) +students[,order(c("name","year","house","houseFounderName")), with=F] +# => +#    studentName year house houseFounderName +# 1:        Fred    2     G           Godric +# 2:      George    2     G           Godric +# 3:       Ginny   -1     G           Godric +# 4:      Cedric    3     H            Helga +# 5:         Cho    1     R           Rowena +# 6:       Draco    0     S          Salazar + + +# データテーブルは、要約を作るのも簡単です +students[,sum(year),by=house] +# => +#    house V1 +# 1:     G  3 +# 2:     H  3 +# 3:     R  1 +# 4:     S  0 + + +# データフレームやデータテーブルから列を消したい場合は、NULL値を代入します +students$houseFounderName <- NULL +students +# => +#    studentName year house +# 1:        Fred    2     G +# 2:      George    2     G +# 3:       Ginny   -1     G +# 4:      Cedric    3     H +# 5:         Cho    1     R +# 6:       Draco    0     S + + +# データテーブルから行を消す場合は、以下のように除く行を指定すればできます +students[studentName != "Draco"] +# => +#    house studentName year +# 1:     G        Fred    2 +# 2:     G      George    2 +# 3:     G       Ginny   -1 +# 4:     H      Cedric    3 +# 5:     R         Cho    1 +# データフレームの場合も同様です +students <- as.data.frame(students) +students[students$house != "G",] +# => +#   house houseFounderName studentName year +# 4     H            Helga      Cedric    3 +# 5     R           Rowena         Cho    1 +# 6     S          Salazar       Draco    0 + + +# 多次元 (すべての値が同じ型の場合) + + +# 配列を並べて、N次元の表を作ります +# 配列なので、すべての値は同じ型にする必要があります +# ちなみに、以下のようにすれば2次元配列・2次元表も作成可能です +array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4)) +# => +#      [,1] [,2] [,3] [,4] +# [1,]    1    4    8    3 +# [2,]    2    5    9    6 +# 2次元配列を並べて、3次元配列を作ることもできます +array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2)) +# => +# , , 1 +# +#      [,1] [,2] +# [1,]    2    8 +# [2,]  300    9 +# [3,]    4    0 +# +# , , 2 +# +#      [,1] [,2] +# [1,]    5   66 +# [2,]   60    7 +# [3,]    0  847 + + +# リスト(多次元、不完全または複数の型が使われているもの) + + +# ついにRのリストです +list1 <- list(time = 1:40) +list1$price = c(rnorm(40,.5*list1$time,4)) # random +list1 +# リストの要素は以下のようにして取得できます +list1$time # ある方法 +list1[["time"]] # 別の方法 +list1[[1]] # また別の方法 +# => +#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 +# [34] 34 35 36 37 38 39 40 +# 他のベクターと同じく、1部を取り出すことができます +list1$price[4] + + +# リストは、Rで1番効率的なデータ型ではありません +# 特別な理由がない限りは、リストの代わりにデータフレームを使うべきです +# リストは、線形回帰関数の返値として、しばしば使われています + + +################################################## +# apply() 系の関数 +################################################## + + +# matは覚えていますよね? +mat +# => +#      [,1] [,2] +# [1,]    1    4 +# [2,]    2    5 +# [3,]    3    6 +# apply(X, MARGIN, FUN) は、行列Xの行(MARGIN=1で指定)または列(MARGIN=2で指定)に対して、関数FUNを実行します +# Rで、このように指定してXの全行または全列に関数を実行するのは、forやwhileループを使うよりも、遥かに速いです +apply(mat, MAR = 2, jiggle) +# => +#      [,1] [,2] +# [1,]    3   15 +# [2,]    7   19 +# [3,]   11   23 +# 他にも便利な関数があります。?lapply, ?sapply で確認してみてください + + +# apply()系関数の使い方は、ちょっとややこしいです(みんなそう思ってます)。なので、あまり怖がりすぎないでください + + +# plyr パッケージは、*apply() 系の関数を置き換えて(さらに改善して)いこうとしています +install.packages("plyr") +require(plyr) +?plyr + + + + + + +######################### +# データロード +######################### + + +# "pets.csv"は、インターネット上に置いてあるファイルです +# (しかし、自分のPCにあるのと同じぐらい簡単に扱う事ができます) +pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv") +pets +head(pets, 2) # 最初の2行 +tail(pets, 1) # 最後の行 + + +# データフレームか行列をcsvファイルとして保存します +write.csv(pets, "pets2.csv") # 新しくcsvファイルを作ります +# ワーキングディレクトリを、setwd()で設定します。 ワーキングディレクトリは getwd()で確認可能です + + +# ?read.csv や ?write.csv を入力すると、よりたくさんの情報を確認できます + + + + + + +######################### +# プロット +######################### + + +# Rに組込まれているプロット関数をつかいます +# 散布図! +plot(list1$time, list1$price, main = "fake data") +# 回帰図! +linearModel <- lm(price  ~ time, data = list1) +linearModel # outputs result of regression +# 回帰直線を既存の図上に引きます +abline(linearModel, col = "red") +# いろいろな散布図をつくって、確認できます +plot(linearModel) +# ヒストグラム! +hist(rpois(n = 10000, lambda = 5), col = "thistle") +# 棒グラフ! +barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow")) + + +# GGPLOT2 +# 上記の組込み関数を使うよりも、もっときれいな図を描くこともできます +# ggplot2 パッケージを使って、より多くのよい図を描いてみましょう +install.packages("ggplot2") +require(ggplot2) +?ggplot2 +pp <- ggplot(students, aes(x=house)) +pp + geom_histogram() +ll <- as.data.table(list1) +pp <- ggplot(ll, aes(x=time,price)) +pp + geom_point() +# ggplot2 には、素晴らしい関連ドキュメントがそろっています (http://docs.ggplot2.org/current/) + + + + + + +``` + + +## Rの入手方法 + + +* RとR GUIはこちら [http://www.r-project.org/](http://www.r-project.org/) +* [RStudio](http://www.rstudio.com/ide/) 別のGUI
\ No newline at end of file diff --git a/matlab.html.markdown b/matlab.html.markdown index 9baefe68..d9a82890 100644 --- a/matlab.html.markdown +++ b/matlab.html.markdown @@ -423,7 +423,8 @@ tril(x) % Returns the lower triangular part of x  cross(A,B) %  Returns the cross product of the vectors A and B  dot(A,B) % Returns scalar product of two vectors (must have the same length)  transpose(A) % Returns the transpose of A -flipl(A) % Flip matrix left to right +fliplr(A) % Flip matrix left to right +flipud(A) % Flip matrix up to down  % Matrix Factorisations  [L, U, P] = lu(A) % LU decomposition: PA = LU,L is lower triangular, U is upper triangular, P is permutation matrix diff --git a/perl6.html.markdown b/perl6.html.markdown new file mode 100644 index 00000000..b21cb1fa --- /dev/null +++ b/perl6.html.markdown @@ -0,0 +1,1007 @@ +--- +name: perl6 +category: language +language: perl6 +filename: learnperl6.pl +contributors: +    - ["Nami-Doc", "http://github.com/Nami-Doc"] +--- + +Perl 6 is a highly capable, feature-rich programming language made for the upcoming hundred years. + +Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](http://moarvm.com). + +Meta-note : the triple pound signs are here to denote headlines, double paragraphs, single notes. +`#=>` represents the output of a command. + +```perl +# Single line comment start with a pound + +#`( +  Multiline comments use #` and a quoting construct. (), [], {}, 「」, etc, will work. +) + +### Variables + +# In Perl 6, you declare a lexical variable using `my` +a +# Perl 6 has 4 variable types : + +## * Scalars. They represent a single value. They start with a `$` + +my $str = 'String'; +my $str2 = "String"; # double quotes allow for interpolation + +# 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 +my $inverse = !$bool; # You can invert a bool with the prefix `!` operator +my $forced-bool = so $str; # And you can use the prefix `so` operator which turns its operand into a Bool + +## * Arrays. They represent multiple values. Their name start with `@`. + +my @array = 1, 2, 3; +my @array = 'a', 'b', 'c'; +# equivalent to : +my @array = <a b c>; # array of words, delimited by space. similar to perl5's qw, or Ruby's %w + +say @array[2]; # Array indices start at 0 -- This is the third element + +say "Interpolate an array using [] : @array[]"; #=> Interpolate an array using [] : a b c + +## * Hashes. Key-Value Pairs. +# Hashes are actually arrays of Pairs (`Key => Value`), +#  except they get "flattened", removing duplicated keys. +my %hash = 1 => 2, +           3 => 4; +my %hash = autoquoted => "key", # keys *can* get auto-quoted +            "some other" => "value", # trailing commas are okay +            ; +my %hash = <key1 value1 key2 value2>; # you can also create a hash from an even-numbered array +my %hash = key1 => 'value1', key2 => 'value2'; # same as this + +# 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` +           # and for the `False` one: +           :!falsey, # equivalent to `:falsey(False)`, or `falsey => False` +           ; + +say %hash{'key1'}; # You can use {} to get the value from a key +say %hash<key2>; # if it's a string, you can actually use <> + +## * Subs (subroutines, or functions in most other languages). Stored in variable, they use `&` +sub say-hello { say "Hello, world" } + +sub say-hello-to(Str $name) { # you can provide the type of an argument +                              # and it'll be checked at compile-time + +    say "Hello, $name !"; +} + +# since you can omit parenthesis to call a function with no arguments, +#  you need "&" in the name to capture `say-hello` +my &s = &say-hello; +my &other-s = sub { say "anonymous function !" } + +# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" +sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything else". +                             # Note: you can have parameters *before* (like here) a slurpy one, +                             # but not *after*. +  say @rest.join(' / ') ~ " !"; +} +say as-many('Happy', 'Happy', 'Birthday'); #=> Happy Birthday ! +                                           # Note that the splat did not consume the parameter before. + +## You can call a function with an array using the "argument list flattening" operator `|` +#  (it's not actually the only feature of the operator, but it's one of them) +sub concat3($a, $b, $c) { +  say "$a, $b, $c"; +} +concat3(|@array); #=> a, b, c +                  # `@array` got "flattened" as a part of the argument list + +## It can also have optional arguments: +sub with-optional($arg?) { # the "?" marks the argument optional +  say "I might return `(Any)` if I don't have an argument passed, or I'll return my argument"; +  $arg; +} +with-optional; # returns Any +with-optional(); # returns Any +with-optional(1); # returns 1 + +## You can also give them a default value when they're not passed: +sub hello-to($name = "World") { +  say "Hello, $name !"; +} +hello-to; #=> Hello, World ! +hello-to(); #=> Hello, World ! +hello-to('You'); #=> Hello, You ! + +## You can also, by using a syntax akin to the one of hashes (yay unification !), +##  pass *named* arguments to a `sub`. +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 as compile time, +#  and you'll have a single Pair object as a positional paramater. + +with-named(2, :named(5)); #=> 7 +with-named(3, :4named); #=> 7 +                        # (special colon pair syntax for numbers, mainly useful for `:2nd` etc) + +with-named(3); # warns, because we tried to use the undefined $named in a `+`: +               # by default, named arguments are *optional* + +# To make a named argument mandatory, you can use `?`'s inverse, `!` +sub with-mandatory-named(:$str!)  { +  say "$named !"; +} +with-mandatory-named(str => "My String"); #=> My String ! +with-mandatory-named; # run time error: "Required named parameter not passed"  +with-mandatory-named(3); # run time error: "Too many positional parameters passed" + +## If a sub takes a named boolean argument ... +sub takes-a-bool($name, :$bool) { +  say "$name takes $bool"; +} +# ... 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 +# or you can use the "adverb" form: +takes-a-bool('config'):bool; #=> config takes True +takes-a-bool('config'):!bool; #=> config takes False +# You'll learn to love (or maybe hate, eh) that syntax later. + + +## You can also provide your named arguments with defaults: +sub named-def(:$def = 5) { +  say $def; +} +named-def; #=> 5 +named-def(:10def); #=> 10 +named-def(def => 15); #=> 15 + +# -- Note: we're going to learn *more* on subs really soon, +#  but we need to grasp a few more things to understand their real power. Ready? + +### Containers +# 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 argument. +# 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 !"; +} + +# If what you want is a copy instead, use `is copy`. + +# A sub itself returns a container, which means it can be marked as rw : +my $x = 42; +sub mod() is rw { $x } +mod() = 52; # in this case, the parentheses are mandatory (else Perl 6 thinks it's a "term") +say $x; #=> 52 + + +### Control Flow Structures + +# You don't need to put parenthesis around the condition, +# but that also means you always have to use brackets (`{ }`) for their body : + +## Conditionals + +# - `if` +# Before talking about `if`, we need to know which values are "Truthy" (represent True), +#  and which are "Falsey" (or "Falsy") -- meaning they represent False. +# Only these values are Falsey: (), 0, "0", Nil, A type, and of course False itself. +# Every other value is Truthy. +if True { +  say "It's true !"; +} + +unless False { +  say "It's not false !"; +} + +# You can also use their postfix versions, with the keyword after: +say "Quite truthy" if True; + +# if (true) say; # This doesn't work ! + +# - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) +my $a = $condition ?? $value-if-true !! $value-if-false; + +# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching, +# and thanks to 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` uses 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" { +  when /foo/ { # you'll read about the smart-matching operator below -- just know `when` uses it +               # this is equivalent to `if $_ ~~ /foo/` +    say "Yay !"; +  } +  when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True, +                       # so you can also put "normal" conditionals. +    say "Quite a long string !"; +  } +  default { # same as `when *` (using the Whatever Star) +    say "Something else" +  } +} + +## Looping constructs + +# - `loop` is an infinite loop if you don't pass it arguments, but can also be a c-style `for` : +loop { +  say "This is an infinite loop !"; +  last; # last breaks out of the loop, like the `break` keyword in other languages +} + +loop (my $i = 0; $i < 5; $i++) { +  next if $i == 3; # `next` skips to the next iteration, like `continue` in other languages. +                   # Notice that you can also use postfix conditionals, loops, etc. +  say "This is a C-style for loop !"; +} + +# - `for` - Passes through an array +for @array -> $variable { +  say "I've found $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 when. +for @array { +  say "I've got $_"; +   +  .say; # This is also allowed. +        # A dot call with no "topic" (receiver) is sent to `$_` by default +  $_.say; # the above and this are equivalent. +} + +for @array { +  next if $_ == 3; # you can skip to the next iteration (like `continue` in C-like languages) +  redo if $_ == 4; # you can re-do the iteration, keeping the same topic variable (`$_`) +  last if $_ == 5; # you can also break out of a loop (like `break` in C-like languages) +} + +# Note - the "lambda" `->` syntax isn't reserved to `for` : +if long-computation() -> $result { +  say "The result is $result"; +} + +### Operators + +## 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) + +## 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'}`) + +## The associativity and precedence list are explained below. + +# Alright, you're set to go ! + +## * Equality Checking + +# - `==` is numeric comparison +3 == 4; # False +3 != 4; # True + +# - `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") +(1, 2) eqv (1, 3); + +# - `~~` is smart matching +# for a complete combinations list, use this table : http://perlcabal.org/syn/S03.html#Smart_matching +'a' ~~ /a/; # true if matches regexp +'key' ~~ %hash; # true if key exists in hash +$arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an argument, returns True +1 ~~ Int; # "is of type" +1 ~~ True; # smart-matching against a boolean always returns that boolean (and will warn). + +# - `===` is value identity and uses `.WHICH` on the objects to compare them +# - `=:=` is container identity and uses `VAR()` on the objects to compare them + +# You also, of course, have `<`, `<=`, `>`, `>=`. +# Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. +3 > 4; + +## * Range constructors +3 .. 7; # 3 to 7, both included +# `^` 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` +^10; # means 0..^10 + +# This also allows us to demonstrate that Perl 6 has lazy arrays, using the Whatever Star : +my @array = 1..*; # 1 to Infinite ! +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. +                  +# Warning, though: if you try this example in the REPL and juste put `1..*`, +# Perl 6 will be forced to try and evaluate the whole array (to print it), +# so you'll end with an infinite loop. + +## * And, Or +3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`. +0 || False; # False. Calls `.Bool` on `0` + +## * Short-circuit (and tight) versions of the above +$a && $b && $c; # returns the first argument that evaluates to False, or the last argument +$a || $b; + +# And because you're going to want them, you also have composed assignment operators: +$a *= 2; # multiply and assignment +$b %%= 5; # divisible by and assignment +$c .= say; # method call and assignment + +### More on subs ! +# 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 ! It's the ability to "extract" arrays and keys. It'll work in `my`s and parameters. +my ($a, $b) = 1, 2; +say $a; #=> 1 +my ($, $, $c) = 1, 2, 3; # keep the non-interesting anonymous +say $c; #=> 3 + +my ($head, *@tail) = 1, 2, 3; # Yes, it's the same as with "slurpy subs" +my (*@small) = 1; + +sub foo(@array [$fst, $snd]) { +  say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; +  # (^ remember the `[]` to interpolate the array) +} +foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 + + +# 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); # errors with "Too many positional parameters passed" (the array is too big) + +# You can also use a slurp ... +sub slurp-in-array(@ [$fst, *@rest]) { # you could decide to keep `*@rest` anonymous +  say $fst + @rest.elems; +} +slurp-in-array(@tail); #=> 3 + +# 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. +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) +key-of({value => 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): +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 no purpose in building a list if we're just going to discard all the results. +# If you still want to build one, you can use the `do` 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 +  } +} +my @list3 = list-of(3); #=> (0, 1, 2) + +## 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. + +# 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: +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 +  map({ return True if $_ == $elem }, @array); +} +sub truthy-array(@array) { +  # this will produce an array of `True` and `False`: +  # (you can also say `anon sub` for "anonymous subroutine") +  map(sub { if $_ { return True } else { return False } }, @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) +my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` +my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` +say (*/2)(4); #=> 2 +              # Immediatly 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, `$^` : +map({ $^a + $^b + 3 }, @array); # same as the above + +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` + +## Multiple Dispatch +# 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 +multi sub sayit(Int $n) { # note the `multi` keyword here +  say "Number: $n"; +} +multi sayit(Str $s) } # the `sub` is the default +  say "String: $s"; +} +sayit("foo"); # prints "String: foo" +sayit(True); # fails at *compile time* with "calling 'sayit' will never work with arguments of types ..." + +# with arbitrary precondition: +multi is-big(Int $n where * > 50) { "Yes !" } # using a closure +multi is-big(Int $ where 10..50) { "Quite." } # this uses smart-matching (could use a regexp, etc) +multi is-big(Int $) { "No" } + +# you can also name these checks, by creating "subsets": +subset Even of Int where * %% 2; + +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 ! +multi with-or-without-you(:$with!) { # make it mandatory to be able to dispatch against it +  say "I can live ! Actually, I can't."; +} +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`, for example, is a dispatch to a function with this signature: +# sub trait_mod:<is>(Routine $r, :$rw!) {} +# (commented because running this would probably lead to some very surprising side-effects !) + + +### Scoping +# In Perl 6, contrarily to many scripting languages (Python, Ruby, PHP, for example), +#  you are to declare your variables before using them. You already saw it, with `my`. +# (there are other declarator keywords, like `our`, `has` and `state`, but we'll talk about them later) +# This is called "lexical scoping", where in inner blocks, you can access variables from outer blocks. +my $foo = 'Foo'; +sub foo { +  my $bar = 'Bar'; +  sub bar { +    say "$foo $bar"; +  } +  &bar; # return the function +} +foo()(); #=> 'Foo Bar' + +# As you can see, `$foo` and `$bar` 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) + +# Perl 6 has another kind of scope : dynamic scope. +# They use the twigil (composed sigil) `*` to mark dynamically-scoped variables: +my $*a = 1; +# Dyamically-scoped variables depend on the current call stack, instead of the current block stack. +sub foo { +  my $*foo = 1; +  bar(); # call `bar` in-place +} +sub bar { +  say $*foo; # Perl 6 will look into the call stack instead, and find `foo`'s `$*a`, +             # even though the blocks aren't nested (they're call-nested). +             #=> 1 +} + +### Object Model + +## Perl 6 has a quite comprehensive object model +## You declare a class with the keyword `class`, fields with `has`, methods with `method`. +## In Perl 6, every field is private, and named `$!attr`, but if you declare it with `$.`, +##  you get a public (immutable) accessor along with it. + +# (Perl 6's object model ("SixModel") is very flexible, and allows you to dynamically add methods, +#  change semantics, etc -- This will not be covered here, and you should refer to the Synopsis) + +class A { +  has $.field; # `$.field` is immutable. Use `$!field` from inside the class to modify it. +  has $.other-field is rw; # You can, however, mark a public field as being read/write. +  has Int $!private-field = 10; + +  method get-value { +    $.field + $!private-field + $n; +  } +   +  method set-value($n) { +    # $.field = $n; # As stated before, you can't use the `$.` immutable version. +    $!field = $n;   # This works, because `$!` is always mutable. +     +    $.other-field = 5; # This works, because `$.other-field` was declared `rw` (mutable). +  } +   +  method !private-method { +    say "This method is private to the class !"; +  } +}; + +# Create a new instance of A with $.field set to 5 : +# note : you can't set private-field from here (more later on) +my $a = A.new(field => 5); +$a.get-value; #=> 18 +#$a.field = 5; # This fails, because the `has $.field` is immutable +$a.other-field = 10; # This, however, works, because the public field is mutable (`rw`). + +## Perl 6 also has inheritance (along with multiple inheritance ... Considered a misfeature by many) + +class A { +  has $.val; +   +  submethod not-inherited { +    say "This method won't be available on B."; +    say "This is most useful for BUILD, which we'll see later"; +  } +   +  method bar { $.val * 5 } +} +class B is A { # inheritance uses `is` +  method foo { +    say $.val; +  } +   +  method bar { $.val * 10 } # this shadows A's `bar` +} + +my B $b .= new(val => 5); # When you use `my T $var`, `$var` starts off with `T` itself in it, +                # so you can call `new` on it. +                # (`.=` is just the compound operator composed of the dot-call and of 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`  +# $b.not-inherited; # This won't work, for reasons explained above +$b.foo; # prints 5 +$b.bar; #=> 50, since it calls B's `bar` + +## Roles are supported too (also called Mixins in other languages) +role PrintableVal { +  has $!counter = 0; +  method print { +    say $.val; +  } +} + +# you "use" a mixin with "does" : +class Item does PrintableVal { +  has $.val; +   +  # When `does`-ed, a `role` literally "mixes in" the class : +  # the methods and fields are put together, which means a class can access +  # the private fields/methods of its roles (but not the inverse !) : +  method access { +    say $!counter++; +  } +   +  # However, this : +  # method print {} +  # is an error, since the compiler wouldn't know which `print` to use : +  # contrarily to inheritance, methods mixed in can't be shadowed - they're put at the same "level" +   +  # 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 +# Exceptions are built on top of classes, usually in the package `X` (like `X::IO`). +# Unlike many other languages, in Perl 6, you put the `CATCH` block *within* the block to `try`. +# By default, a `try` has a `CATCH` block that catches any exception (`CATCH { default {} }`). +# You can redefine it using `when`s (and `default`) to handle the exceptions you want: +try { +  open 'foo'; +  CATCH { +    when X::AdHoc { say "unable to open file !" } +    # any other exception will be re-raised, since we don't have a `default` +  } +} + +# You can throw an exception using `die`: +die X::AdHoc.new(payload => 'Error !'); +# TODO warn +# TODO fail +# TODO CONTROL + +### Packages +# 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. +#  (you can say that packages are the lowest common denomitor between them) +# Packages play a big part in a language, as Perl is well-known for CPAN, +#  the Comprehensive Perl Archive Network. +# You usually don't use packages directly : you use `class Package::Name::Here;`, 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 created as an "empty package stub" +                      # that can be redeclared as something else later. +  # declarations here +} +module Parse::Text; # file-scoped form +grammar Parse::Text::Grammar { # A grammar is a fine package, which you could `use` +} + +# NOTE for Perl 5 users: even though the `package` keyword exists, +#  the braceless form is invalid (to catch a "perl5ism"). This will error out: +# package Foo; # because Perl 6 will think the entire file is Perl 5 +# Just use `module` or the brace version of `package`. + +# 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] + +# 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: + +### Declarators +# 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` (happens at `INIT` time -- see "Phasers" below) +# Along with `my`, there are several others declarators you can use. +# The first one you'll want for the previous part is `our`. +# (All packagish things (`class`, `role`, etc) are `our` by default) +# it's like `my`, but it also creates a package variable: +module Foo::Bar { +  our $n = 1; # note: you can't put a type constraint on an `our` variable +  our sub inc { +    our sub available { # if you try to make scoped `sub`s `our` ... Better know what you're doing (Don't !). +      say "Don't do that. Seriously. You'd get burned."; +    } +    my sub unavailable { # `my sub` is the default +      say "Can't access me from outside, I'm my !"; +    } +  } +   +  say ++$n; # lexically-scoped variables are still available +} +say $Foo::Bar::n; #=> 1 +Foo::Bar::inc; #=> 2 +Foo::Bar::inc; #=> 3 + +## * `constant` (happens at `BEGIN` time) +# You can use the `constant` keyword to declare a compile-time variable/symbol: +constant Pi = 3.14; +constant $var = 1; + +## * `state` (happens at run time, but only once) +# State variables are only executed one time +# (they exist in other langages such as C as `static`) +sub fixed-rand { +  state $val = rand; +  say $rand; +} +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 loop. See: +for ^5 -> $a { +  sub foo { +    state $val = rand; # This will be a different value for every value of `$a` +  } +  for ^5 -> $b { +    say foo; # This will print the same value 5 times, but only 5. Next iteration will re-run `rand` +  } +} + + + +### Phasers +# Phasers in Perl 6 are blocks that happen at determined points of time in your program +# When the program is compiled, when a for loop runs, when you leave a block, when +#  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" } +CHECK { say "[*] Runs at compile time, instead as late as possible, only once" } + +## * Run-time phasers +INIT { say "[*] Runs at run time, as soon as possible, only once" } +END { say "Runs at run time, as late as possible, only once" } + +## * Block phasers +ENTER { say "[*] Runs everytime you enter a block, repeats on loop blocks" } +LEAVE { say "Runs everytime you leave a block, even when an exception happened. Repeats on loop blocks." } + +PRE { say "Asserts a precondition at every block entry, before ENTER (especially useful for loops)" } +POST { say "Asserts a postcondition at every block exit, after LEAVE (especially useful for loops)" } + +## * 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)" } +} + +## * Loop phasers +for ^5 { +  FIRST { say "[*] The first time the loop is run, before ENTER" } +  NEXT { say "At loop continuation time, before LEAVE" } +  LAST { say "At loop termination time, after LEAVE" } +} + +## * Role/class phasers +COMPOSE { "When a role is composed into a class. /!\ NOT YET IMPLEMENTED" } + +# They allow for cute trick or clever code ...: +say "This code took " ~ (time - CHECK time) ~ "s to run"; + +# ... or clever organization: +sub do-db-stuff { +  ENTER $db.start-transaction; # create a new transaction everytime we enter the sub +  KEEP $db.commit; # commit the transaction if all went well +  UNDO $db.rollback; # or rollback if all hell broke loose +} + + +### More operators thingies ! + +## Everybody loves operators ! Let's get more of them + +## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence +## But first, we need a little explanation about associativity : + +# * 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: +!$a! # with left-associative `!`, this is `(!$a)!` +!$a! # with right-associative `!`, this is `!($a!)` +!$a! # with non-associative `!`, this is illegal + +## Create your own operators ! +# 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 (actually not): +# In Perl 6, all operators are actually just funny-looking subroutines. + +# 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 !"; +} +win "The King"; #=> The King Won ! +                # (prefix is before) + +# you can still call the sub with its "full name" +say prefix:<!>(True); #=> False + +sub postfix:<!>(Int $n) { +  [*] 2..$n; # using the reduce meta-operator ... See below ;-) ! +} +say 5!; #=> 120 +        # Postfix operators (after) have to come *directly* after the term. +        # No whitespace. You can use parentheses to disambiguate, i.e. `(5!)!` + + +sub infix:<times>(Int $n, Block $r) { # infix in the middle +  for ^$n { +    $r(); # You need the explicit parentheses to call the function in `$r`, +          #  else you'd be referring at the variable itself, kind of like with `&r`. +  } +} +3 times -> { say "hello" }; #=> hello +                            #=> hello +                            #=> hello +                            # You're very recommended to put spaces +                            # around your infix operator calls. + +# For circumfix and post-circumfix ones +sub circumfix:<[ ]>(Int $n) { +  $n ** $n +} +say [5]; #=> 3125 +         # circumfix is around. Again, not whitespace. + +sub postcircumfix:<{ }>(Str $s, Int $idx) { # 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 (named argument) +%h{$key}:delete; +# equivalent to: +postcircumfix:<{ }>(%h, $key, :delete); +# 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 ! +# Oh boy, get ready. Get ready, because we're dwelving 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 functions and one or many lists. +# If it doesn't get passed any argument, it either return a "default value" for this operator +#  (a value that'd be non-meaningful if contained in a list) or `Any` if there's none. +# 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` +say [*] 1..5; #=> 120 +# 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: +say [//] Nil, Any, False, 1, 5; #=> False +                                # (Falsey, but still defined) + + +# Default value examples: +say [*] (); #=> 1 +say [+] (); #=> 0 +            # In both cases, they're results that, if they were contained in the lists, +            # wouldn't have any impact on the final value (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: +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 runs the an array with all 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 +(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: +[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 for when to stop, or even Whatever for a lazy infinite list. +my @list = 1, 2, 3 ... 10; # basic deducing +#my @list = 1, 3, 6 ... 10; # this throws you into an infinite loop, because Perl 6 can't figure out the end +my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element (when the predicate matches) +my @list = 1, 3, 9 ... * > 30; # you can use a predicate (with the Whatever Star, here) +my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) +my @fib = 1, 1, *+* ... *; # lazy infinite list of prime numbers, computed using a closure ! +my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) +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 + + +## * Sort comparison +# 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 + +## * Generic ordering +3 before 4; # True +'b' after 'a'; # True + +## * Short-circuit default operator +# Like `or` and `||`, but instead returns the first *defined* value : +say Any // Nil // 0 // 5; #=> 5 + +## * Short-circuit exclusive or (XOR) +# 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 Perl 5/Ruby's `..` and `...`). +#  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" (explained in details below). +    .say +  } +   +  if rand == 0 ff rand == 1 { # compare variables other than `$_` +    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 just as 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 still 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 +                                    # because the right-hand-side wasn't tested until `$_` became "C" +                                    # (and thus did not match directly). + +# A flip-flop can change state as many times as needed: +for <test start print this stop you stopped printing start printing again stop not anymore> { +  .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", +                                           #=> "print this printing again" +} + +# 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" + .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50, it'll never go back to `False` +                       #=> 60 3 40 60 +} + +# You can also use this property to create an `If` that'll not execute 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 +                   #=> b c +} +``` diff --git a/python.html.markdown b/python.html.markdown index 210c9619..aa077e57 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -45,6 +45,13 @@ to Python 2.x. Look for another tour of Python 3 soon!  2.0     # This is a float  11.0 / 4.0  # => 2.75 ahhh...much better +# Truncation or Integer division  +5 // 3     # => 1 +5.0 // 3.0 # => 1.0 # works on floats too + +# Modulo operation +7 % 3 # => 1 +  # Enforce precedence with parentheses  (1 + 3) * 2  # => 8 @@ -380,6 +387,22 @@ 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) +# Function Scope                                                                 +x = 5 + +def setX(num): +    # Local var x not the same as global variable x +    x = num # => 43 +    print (x) # => 43 +     +def setGlobalX(num): +    global x +    print (x) # => 5 +    x = num # global var x is now set to 6 +    print (x) # => 6 + +setX(43) +setGlobalX(6)  # Python has first class functions  def create_adder(x): diff --git a/python3.html.markdown b/python3.html.markdown index 778076f8..bc0c05bd 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -2,6 +2,7 @@  language: python3  contributors:      - ["Louie Dinh", "http://pythonpracticeprojects.com"] +    - ["Steven Basart", "http://github.com/xksteven"]  filename: learnpython3.py  --- @@ -37,9 +38,16 @@ Note: This article applies to Python 3 specifically. Check out the other tutoria  # Except division which returns floats by default  35 / 5  # => 7.0 +# Truncation or Integer division  +5 // 3     # => 1 +5.0 // 3.0 # => 1.0 +  # When you use a float, results are floats  3 * 2.0 # => 6.0 +# Modulo operation +7 % 3 # => 1 +  # Enforce precedence with parentheses  (1 + 3) * 2  # => 8 @@ -90,6 +98,10 @@ not False  # => True  # You can use keywords if you don't want to count.  "{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna" +# If your Python 3 code also needs to run on Python 2.5 and below, you can also +# still use the old style of formatting: +"%s can be %s the %s way" % ("strings", "interpolated", "old") +  # None is an object  None  # => None @@ -284,7 +296,7 @@ prints:      mouse is a mammal  """  for animal in ["dog", "cat", "mouse"]: -    # You can use % to interpolate formatted strings +    # You can use format() to interpolate formatted strings      print("{} is a mammal".format(animal))  """ @@ -406,6 +418,24 @@ 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) +# Function Scope                                                                 +x = 5 + +def setX(num): +    # Local var x not the same as global variable x +    x = num # => 43 +    print (x) # => 43 +     +def setGlobalX(num): +    global x +    print (x) # => 5 +    x = num # global var x is now set to 6 +    print (x) # => 6 + +setX(43) +setGlobalX(6) + +  # Python has first class functions  def create_adder(x):      def adder(y): @@ -445,7 +475,7 @@ class Human(object):      # An instance method. All methods take "self" as the first argument      def say(self, msg): -        return "{name}: {message}" % (name=self.name, message=msg) +        return "{name}: {message}".format(name=self.name, message=msg)      # A class method is shared among all instances      # They are called with the calling class as the first argument diff --git a/swift.html.markdown b/swift.html.markdown index f24b1592..a47b085a 100644 --- a/swift.html.markdown +++ b/swift.html.markdown @@ -31,7 +31,7 @@ optionalString = nil  // Array  var shoppingList = ["catfish", "water", "lemons"]  shoppingList[1] = "bottle of water" -let emptyArray = String[]() +let emptyArray = [String]()  // Dictionary  var occupations = [ @@ -65,7 +65,7 @@ for (key, value) in dict {  for i in -1...1 { // [-1, 0, 1]    println(i)  } -// use .. to exclude the last number +// use ..< to exclude the last number  // while loop  var i = 1 @@ -127,6 +127,7 @@ increment(7)  //  // Closures  // +var numbers = [1, 2, 6]  // Functions are special case closures ({}) @@ -140,8 +141,10 @@ numbers.map({    })  // When the type is known, like above, we can do this -var numbers = [1, 2, 6]  numbers = numbers.map({ number in 3 * number }) +//Or even this +//numbers = numbers.map({ $0 * 3 }) +  print(numbers) // [3, 6, 18] @@ -221,4 +224,4 @@ enum Suit {  // Generics: Similar to Java. Use the `where` keyword to specify the  //   requirements of the generics. -```
\ No newline at end of file +``` diff --git a/zh-cn/julia-cn.html.markdown b/zh-cn/julia-cn.html.markdown new file mode 100644 index 00000000..7afc9043 --- /dev/null +++ b/zh-cn/julia-cn.html.markdown @@ -0,0 +1,729 @@ +--- +language: julia +filename: learn-julia-zh.jl +contributors: +    - ["Jichao Ouyang", "http://oyanglul.us"] +translators: +    - ["Jichao Ouyang", "http://oyanglul.us"] +lang: zh-cn +--- + +```ruby +# 单行注释只需要一个井号 +#= 多行注释 +   只需要以 '#=' 开始 '=#' 结束 +   还可以嵌套. +=# + +#################################################### +## 1. 原始类型与操作符 +#################################################### + +# Julia 中一切皆是表达式。 + +# 这是一些基本数字类型. +3 # => 3 (Int64) +3.2 # => 3.2 (Float64) +2 + 1im # => 2 + 1im (Complex{Int64}) +2//3 # => 2//3 (Rational{Int64}) + +# 支持所有的普通中缀操作符。 +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7.0 +5 / 2 # => 2.5 # 用 Int 除 Int 永远返回 Float +div(5, 2) # => 2 # 使用 div 截断小数点 +5 \ 35 # => 7.0 +2 ^ 2 # => 4 # 次方, 不是二进制 xor +12 % 10 # => 2 + +# 用括号提高优先级 +(1 + 3) * 2 # => 8 + +# 二进制操作符 +~2 # => -3   # 非 +3 & 5 # => 1 # 与 +2 | 4 # => 6 # 或 +2 $ 4 # => 6 # 异或 +2 >>> 1 # => 1 # 逻辑右移 +2 >> 1  # => 1 # 算术右移 +2 << 1  # => 4 # 逻辑/算术 右移 + +# 可以用函数 bits 查看二进制数。 +bits(12345) +# => "0000000000000000000000000000000000000000000000000011000000111001" +bits(12345.0) +# => "0100000011001000000111001000000000000000000000000000000000000000" + +# 布尔值是原始类型 +true +false + +# 布尔操作符 +!true # => false +!false # => true +1 == 1 # => true +2 == 1 # => false +1 != 1 # => false +2 != 1 # => true +1 < 10 # => true +1 > 10 # => false +2 <= 2 # => true +2 >= 2 # => true +# 比较可以串联 +1 < 2 < 3 # => true +2 < 3 < 2 # => false + +# 字符串可以由 " 创建 +"This is a string." + +# 字符字面量可用 ' 创建 +'a' + +# 可以像取数组取值一样用 index 取出对应字符 +"This is a string"[1] # => 'T' # Julia 的 index 从 1 开始 :( +# 但是对 UTF-8 无效, +# 因此建议使用遍历器 (map, for loops, 等). + +# $ 可用于字符插值: +"2 + 2 = $(2 + 2)" # => "2 + 2 = 4" +# 可以将任何 Julia 表达式放入括号。 + +# 另一种格式化字符串的方式是 printf 宏. +@printf "%d is less than %f" 4.5 5.3 # 5 is less than 5.300000 + +# 打印字符串很容易 +println("I'm Julia. Nice to meet you!") + +#################################################### +## 2. 变量与集合 +#################################################### + +# 给变量赋值就是声明变量 +some_var = 5 # => 5 +some_var # => 5 + +# 访问未声明变量会抛出异常 +try +    some_other_var # => ERROR: some_other_var not defined +catch e +    println(e) +end + +# 变量名需要以字母开头. +# 之后任何字母,数字,下划线,叹号都是合法的。 +SomeOtherVar123! = 6 # => 6 + +# 甚至可以用 unicode 字符 +☃ = 8 # => 8 +# 用数学符号非常方便 +2 * π # => 6.283185307179586 + +# 注意 Julia 的命名规约: +# +# * 变量名为小写,单词之间以下划线连接('\_')。 +# +# * 类型名以大写字母开头,单词以 CamelCase 方式连接。 +# +# * 函数与宏的名字小写,无下划线。 +# +# * 会改变输入的函数名末位为 !。 +#   这类函数有时被称为 mutating functions 或 in-place functions. + +# 数组存储一列值,index 从 1 开始。 +a = Int64[] # => 0-element Int64 Array + +# 一维数组可以以逗号分隔值的方式声明。 +b = [4, 5, 6] # => 包含 3 个 Int64 类型元素的数组: [4, 5, 6] +b[1] # => 4 +b[end] # => 6 + +# 二维数组以分号分隔维度。 +matrix = [1 2; 3 4] # => 2x2 Int64 数组: [1 2; 3 4] + +# 使用 push! 和 append! 往数组末尾添加元素 +push!(a,1)     # => [1] +push!(a,2)     # => [1,2] +push!(a,4)     # => [1,2,4] +push!(a,3)     # => [1,2,4,3] +append!(a,b) # => [1,2,4,3,4,5,6] + +# 用 pop 弹出末尾元素 +pop!(b)        # => 6 and b is now [4,5] + +# 可以再放回去 +push!(b,6)   # b 又变成了 [4,5,6]. + +a[1] # => 1 #  永远记住 Julia 的 index 从 1 开始! + +# 用 end 可以直接取到最后索引. 可用作任何索引表达式 +a[end] # => 6 + +# 还支持 shift 和 unshift +shift!(a) # => 返回 1,而 a 现在时 [2,4,3,4,5,6] +unshift!(a,7) # => [7,2,4,3,4,5,6] + +# 以叹号结尾的函数名表示它会改变参数的值 +arr = [5,4,6] # => 包含三个 Int64 元素的数组: [5,4,6] +sort(arr) # => [4,5,6]; arr 还是 [5,4,6] +sort!(arr) # => [4,5,6]; arr 现在是 [4,5,6] + +# 越界会抛出 BoundsError 异常 +try +    a[0] # => ERROR: BoundsError() in getindex at array.jl:270 +    a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270 +catch e +    println(e) +end + +# 错误会指出发生的行号,包括标准库 +# 如果你有 Julia 源代码,你可以找到这些地方 + +# 可以用 range 初始化数组 +a = [1:5] # => 5-element Int64 Array: [1,2,3,4,5] + +# 可以切割数组 +a[1:3] # => [1, 2, 3] +a[2:end] # => [2, 3, 4, 5] + +# 用 splice! 切割原数组 +arr = [3,4,5] +splice!(arr,2) # => 4 ; arr 变成了 [3,5] + +# 用 append! 连接数组 +b = [1,2,3] +append!(a,b) # a 变成了 [1, 2, 3, 4, 5, 1, 2, 3] + +# 检查元素是否在数组中 +in(1, a) # => true + +# 用 length 获得数组长度 +length(a) # => 8 + +# Tuples 是 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) +catch e +    println(e) +end + +# 大多数组的函数同样支持 tuples +length(tup) # => 3 +tup[1:2] # => (1,2) +in(2, tup) # => true + +# 可以将 tuples 元素分别赋给变量 +a, b, c = (1, 2, 3) # => (1,2,3)  # a is now 1, b is now 2 and c is now 3 + +# 不用括号也可以 +d, e, f = 4, 5, 6 # => (4,5,6) + +# 单元素 tuple 不等于其元素值 +(1,) == 1 # => false +(1) == 1 # => true + +# 交换值 +e, d = d, e  # => (5,4) # d is now 5 and e is now 4 + + +# 字典Dictionaries store mappings +empty_dict = Dict() # => Dict{Any,Any}() + +# 也可以用字面量创建字典 +filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3] +# => Dict{ASCIIString,Int64} + +# 用 [] 获得键值 +filled_dict["one"] # => 1 + +# 获得所有键 +keys(filled_dict) +# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) +# 注意,键的顺序不是插入时的顺序 + +# 获得所有值 +values(filled_dict) +# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2]) +# 注意,值的顺序也一样 + +# 用 in 检查键值是否已存在,用 haskey 检查键是否存在 +in(("one", 1), filled_dict) # => true +in(("two", 3), filled_dict) # => false +haskey(filled_dict, "one") # => true +haskey(filled_dict, 1) # => false + +# 获取不存在的键的值会抛出异常 +try +    filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489 +catch e +    println(e) +end + +# 使用 get 可以提供默认值来避免异常 +# get(dictionary,key,default_value) +get(filled_dict,"one",4) # => 1 +get(filled_dict,"four",4) # => 4 + +# 用 Sets 表示无序不可重复的值的集合 +empty_set = Set() # => Set{Any}() +# 初始化一个 Set 并定义其值 +filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4) + +# 添加值 +push!(filled_set,5) # => Set{Int64}(5,4,2,3,1) + +# 检查是否存在某值 +in(2, filled_set) # => true +in(10, filled_set) # => false + +# 交集,并集,差集 +other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3) +intersect(filled_set, other_set) # => Set{Int64}(3,4,5) +union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6) +setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4) + + +#################################################### +## 3. 控制流 +#################################################### + +# 声明一个变量 +some_var = 5 + +# 这是一个 if 语句,缩进不是必要的 +if some_var > 10 +    println("some_var is totally bigger than 10.") +elseif some_var < 10    # elseif 是可选的. +    println("some_var is smaller than 10.") +else                    # else 也是可选的. +    println("some_var is indeed 10.") +end +# => prints "some var is smaller than 10" + + +# For 循环遍历 +# Iterable 类型包括 Range, Array, Set, Dict, 以及 String. +for animal=["dog", "cat", "mouse"] +    println("$animal is a mammal") +    # 可用 $ 将 variables 或 expression 转换为字符串into strings +end +# prints: +#    dog is a mammal +#    cat is a mammal +#    mouse is a mammal + +# You can use 'in' instead of '='. +for animal in ["dog", "cat", "mouse"] +    println("$animal is a mammal") +end +# prints: +#    dog is a mammal +#    cat is a mammal +#    mouse is a mammal + +for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"] +    println("$(a[1]) is a $(a[2])") +end +# prints: +#    dog is a mammal +#    cat is a mammal +#    mouse is a mammal + +for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"] +    println("$k is a $v") +end +# prints: +#    dog is a mammal +#    cat is a mammal +#    mouse is a mammal + +# While 循环 +x = 0 +while x < 4 +    println(x) +    x += 1  # x = x + 1 +end +# prints: +#   0 +#   1 +#   2 +#   3 + +# 用 try/catch 处理异常 +try +   error("help") +catch e +   println("caught it $e") +end +# => caught it ErrorException("help") + + +#################################################### +## 4. 函数 +#################################################### + +# 用关键字 'function' 可创建一个新函数 +#function name(arglist) +#  body... +#end +function add(x, y) +    println("x is $x and y is $y") + +    # 最后一行语句的值为返回 +    x + y +end + +add(5, 6) # => 在 "x is 5 and y is 6" 后会打印 11 + +# 还可以定义接收可变长参数的函数 +function varargs(args...) +    return args +    # 关键字 return 可在函数内部任何地方返回 +end +# => varargs (generic function with 1 method) + +varargs(1,2,3) # => (1,2,3) + +# 省略号 ... 被称为 splat. +# 刚刚用在了函数定义中 +# 还可以用在函数的调用 +# Array 或者 Tuple 的内容会变成参数列表 +Set([1,2,3])    # => Set{Array{Int64,1}}([1,2,3]) # 获得一个 Array 的 Set +Set([1,2,3]...) # => Set{Int64}(1,2,3) # 相当于 Set(1,2,3) + +x = (1,2,3)     # => (1,2,3) +Set(x)          # => Set{(Int64,Int64,Int64)}((1,2,3)) # 一个 Tuple 的 Set +Set(x...)       # => Set{Int64}(2,3,1) + + +# 可定义可选参数的函数 +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" +try +    defaults('h') # => ERROR: no method defaults(Char,) +    defaults() # => ERROR: no methods defaults() +catch e +    println(e) +end + +# 还可以定义键值对的参数 +function keyword_args(;k1=4,name2="hello") # note the ; +    return ["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] + +# 可以组合各种类型的参数在同一个函数的参数列表中 +function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo") +    println("normal arg: $normal_arg") +    println("optional arg: $optional_positional_arg") +    println("keyword arg: $keyword_arg") +end + +all_the_args(1, 3, keyword_arg=4) +# prints: +#   normal arg: 1 +#   optional arg: 3 +#   keyword arg: 4 + +# Julia 有一等函数 +function create_adder(x) +    adder = function (y) +        return x + y +    end +    return adder +end + +# 这是用 "stabby lambda syntax" 创建的匿名函数 +(x -> x > 2)(3) # => true + +# 这个函数和上面的 create_adder 一模一样 +function create_adder(x) +    y -> x + y +end + +# 你也可以给内部函数起个名字 +function create_adder(x) +    function adder(y) +        x + y +    end +    adder +end + +add_10 = create_adder(10) +add_10(3) # => 13 + + +# 内置的高阶函数有 +map(add_10, [1,2,3]) # => [11, 12, 13] +filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7] + +# 还可以使用 list comprehensions 替代 map +[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13] +[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] + +#################################################### +## 5.  类型 +#################################################### + +# Julia 有类型系统 +# 所有的值都有类型;但变量本身没有类型 +# 你可以用 `typeof` 函数获得值的类型 +typeof(5) # => Int64 + +# 类型是一等值 +typeof(Int64) # => DataType +typeof(DataType) # => DataType +# DataType 是代表类型的类型,也代表他自己的类型 + +# 类型可用作文档化,优化,以及调度 +# 并不是静态检查类型 + +# 用户还可以自定义类型 +# 跟其他语言的 records 或 structs 一样 +# 用 `type` 关键字定义新的类型 + +# type Name +#   field::OptionalType +#   ... +# end +type Tiger +  taillength::Float64 +  coatcolor # 不附带类型标注的相当于 `::Any` +end + +# 构造函数参数是类型的属性 +tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange") + +# 用新类型作为构造函数还会创建一个类型 +sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire") + +# struct 类似的类型被称为具体类型 +# 他们可被实例化但不能有子类型 +# 另一种类型是抽象类型 + +# abstract Name +abstract Cat # just a name and point in the type hierarchy + +# 抽象类型不能被实例化,但是可以有子类型 +# 例如,Number 就是抽象类型 +subtypes(Number) # => 6-element Array{Any,1}: +                 #     Complex{Float16} +                 #     Complex{Float32} +                 #     Complex{Float64} +                 #     Complex{T<:Real} +                 #     ImaginaryUnit +                 #     Real +subtypes(Cat) # => 0-element Array{Any,1} + +# 所有的类型都有父类型; 可以用函数 `super` 得到父类型. +typeof(5) # => Int64 +super(Int64) # => Signed +super(Signed) # => Real +super(Real) # => Number +super(Number) # => Any +super(super(Signed)) # => Number +super(Any) # => Any +# 所有这些类型,除了 Int64, 都是抽象类型. + +# <: 是类型集成操作符 +type Lion <: Cat # Lion 是 Cat 的子类型 +  mane_color +  roar::String +end + +# 可以继续为你的类型定义构造函数 +# 只需要定义一个同名的函数 +# 并调用已有的构造函数设置一个固定参数 +Lion(roar::String) = Lion("green",roar) +# 这是一个外部构造函数,因为他再类型定义之外 + +type Panther <: Cat # Panther 也是 Cat 的子类型 +  eye_color +  Panther() = new("green") +  # Panthers 只有这个构造函数,没有默认构造函数 +end +# 使用内置构造函数,如 Panther,可以让你控制 +# 如何构造类型的值 +# 应该尽可能使用外部构造函数而不是内部构造函数 + +#################################################### +## 6. 多分派 +#################################################### + +# 在Julia中, 所有的具名函数都是类属函数 +# 这意味着他们都是有很大小方法组成的 +# 每个 Lion 的构造函数都是类属函数 Lion 的方法 + +# 我们来看一个非构造函数的例子 + +# Lion, Panther, Tiger 的 meow 定义为 +function meow(animal::Lion) +  animal.roar # 使用点符号访问属性 +end + +function meow(animal::Panther) +  "grrr" +end + +function meow(animal::Tiger) +  "rawwwr" +end + +# 试试 meow 函数 +meow(tigger) # => "rawwr" +meow(Lion("brown","ROAAR")) # => "ROAAR" +meow(Panther()) # => "grrr" + +# 再看看层次结构 +issubtype(Tiger,Cat) # => false +issubtype(Lion,Cat) # => true +issubtype(Panther,Cat) # => true + +# 定义一个接收 Cats 的函数 +function pet_cat(cat::Cat) +  println("The cat says $(meow(cat))") +end + +pet_cat(Lion("42")) # => prints "The cat says 42" +try +    pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,) +catch e +    println(e) +end + +# 在面向对象语言中,通常都是单分派 +# 这意味着分派方法是通过第一个参数的类型决定的 +# 在Julia中, 所有参数类型都会被考虑到 + +# 让我们定义有多个参数的函数,好看看区别 +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! + +# 让我们修改一下传入具体为 Lion 类型时的行为 +fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!") +# => fight (generic function with 2 methods) + +fight(tigger,Panther()) # => prints The orange tiger wins! +fight(tigger,Lion("ROAR")) # => prints The green-maned lion wins! + +# 把 Tiger 去掉 +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 +try +  fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion) +catch +end + +# 在试试让 Cat 在前面 +fight(c::Cat,l::Lion) = println("The cat beats the Lion") +# => Warning: New definition +#    fight(Cat,Lion) at none:1 +# is ambiguous with +#    fight(Lion,Cat) at none:2. +# Make sure +#    fight(Lion,Lion) +# is defined first. +#fight (generic function with 4 methods) + +# 警告说明了无法判断使用哪个 fight 方法 +fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The victorious cat says rarrr +# 结果在老版本 Julia 中可能会不一样 + +fight(l::Lion,l2::Lion) = println("The lions come to a tie") +fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The lions come to a tie + + +# Under the hood +# 你还可以看看 llvm 以及生成的汇编代码 + +square_area(l) = l * l      # square_area (generic function with 1 method) + +square_area(5) #25 + +# 给 square_area 一个整形时发生什么 +code_native(square_area, (Int32,)) +    #       .section    __TEXT,__text,regular,pure_instructions +    #   Filename: none +    #   Source line: 1              # Prologue +    #       push    RBP +    #       mov RBP, RSP +    #   Source line: 1 +    #       movsxd  RAX, EDI        # Fetch l from memory? +    #       imul    RAX, RAX        # Square l and store the result in RAX +    #       pop RBP                 # Restore old base pointer +    #       ret                     # Result will still be in RAX + +code_native(square_area, (Float32,)) +    #       .section    __TEXT,__text,regular,pure_instructions +    #   Filename: none +    #   Source line: 1 +    #       push    RBP +    #       mov RBP, RSP +    #   Source line: 1 +    #       vmulss  XMM0, XMM0, XMM0  # Scalar single precision multiply (AVX) +    #       pop RBP +    #       ret + +code_native(square_area, (Float64,)) +    #       .section    __TEXT,__text,regular,pure_instructions +    #   Filename: none +    #   Source line: 1 +    #       push    RBP +    #       mov RBP, RSP +    #   Source line: 1 +    #       vmulsd  XMM0, XMM0, XMM0 # Scalar double precision multiply (AVX) +    #       pop RBP +    #       ret +    # +# 注意 只要参数中又浮点类型,Julia 就使用浮点指令 +# 让我们计算一下圆的面积 +circle_area(r) = pi * r * r     # circle_area (generic function with 1 method) +circle_area(5)                  # 78.53981633974483 + +code_native(circle_area, (Int32,)) +    #       .section    __TEXT,__text,regular,pure_instructions +    #   Filename: none +    #   Source line: 1 +    #       push    RBP +    #       mov RBP, RSP +    #   Source line: 1 +    #       vcvtsi2sd   XMM0, XMM0, EDI          # Load integer (r) from memory +    #       movabs  RAX, 4593140240              # Load pi +    #       vmulsd  XMM1, XMM0, QWORD PTR [RAX]  # pi * r +    #       vmulsd  XMM0, XMM0, XMM1             # (pi * r) * r +    #       pop RBP +    #       ret +    # + +code_native(circle_area, (Float64,)) +    #       .section    __TEXT,__text,regular,pure_instructions +    #   Filename: none +    #   Source line: 1 +    #       push    RBP +    #       mov RBP, RSP +    #       movabs  RAX, 4593140496 +    #   Source line: 1 +    #       vmulsd  XMM1, XMM0, QWORD PTR [RAX] +    #       vmulsd  XMM0, XMM1, XMM0 +    #       pop RBP +    #       ret +    # +``` | 
