diff options
| -rw-r--r-- | bash.html.markdown | 20 | ||||
| -rw-r--r-- | javascript.html.markdown | 31 | ||||
| -rw-r--r-- | julia.html.markdown | 6 | ||||
| -rw-r--r-- | objective-c.html.markdown | 104 | ||||
| -rw-r--r-- | scala.html.markdown | 2 | 
5 files changed, 134 insertions, 29 deletions
| diff --git a/bash.html.markdown b/bash.html.markdown index 815290dd..a6bd2b7c 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -21,7 +21,7 @@ Nearly all examples below can be a part of a shell script or executed directly i  # As you already figured, comments start with #. Shebang is also a comment.  # Simple hello world example: -echo Hello, world! +echo Hello world!  # Each command starts on a new line, or after semicolon:  echo 'This is the first line'; echo 'This is the second line' @@ -56,24 +56,24 @@ echo "Last program return value: $?"  echo "Script's PID: $$"  echo "Number of arguments: $#"  echo "Scripts arguments: $@" -echo "Scripts arguments separeted in different variables: $1 $2..." +echo "Scripts arguments seperated in different variables: $1 $2..."  # Reading a value from input:  echo "What's your name?" -read NAME # Note that we didn't need to declare new variable +read NAME # Note that we didn't need to declare a new variable  echo Hello, $NAME!  # We have the usual if structure:  # use 'man test' for more info about conditionals  if [ $NAME -ne $USER ]  then -    echo "Your name is you username" +    echo "Your name is your username"  else -    echo "Your name isn't you username" +    echo "Your name isn't your username"  fi  # There is also conditional execution -echo "Always executed" || echo "Only executed if first command fail" +echo "Always executed" || echo "Only executed if first command fails"  echo "Always executed" && echo "Only executed if first command does NOT fail"  # Expressions are denoted with the following format: @@ -81,7 +81,7 @@ echo $(( 10 + 5 ))  # Unlike other programming languages, bash is a shell — so it works in a context  # of current directory. You can list files and directories in the current -# directories with ls command: +# directory with the ls command:  ls  # These commands have options that control their execution: @@ -89,10 +89,10 @@ ls -l # Lists every file and directory on a separate line  # Results of the previous command can be passed to the next command as input.  # grep command filters the input with provided patterns. That's how we can list -# txt files in the current directory: +# .txt files in the current directory:  ls -l | grep "\.txt" -# You can also redirect a command output, input and error output. +# You can also redirect a command, input and error output.  python2 hello.py < "input.in"  python2 hello.py > "output.out"  python2 hello.py 2> "error.err" @@ -116,7 +116,7 @@ case "$VARIABLE" in      *) echo "It is not null.";;  esac -# For loops iterate for as many arguments given: +# for loops iterate for as many arguments given:  # The contents of var $VARIABLE is printed three times.  for VARIABLE in {1..3}  do diff --git a/javascript.html.markdown b/javascript.html.markdown index 4584a28c..7fb7ba55 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -319,6 +319,37 @@ var myOtherFunc = function(){  myObj.myOtherFunc = myOtherFunc;  myObj.myOtherFunc(); // = "HELLO WORLD!" +// We can also specify a context for a function to execute in when we invoke it +// using 'call' or 'apply'. + +var anotherFunc = function(s){ +    return this.myString + s; +} +anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!" + +// The 'apply' function is nearly identical, but takes an array for an argument list.  + +anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!" + +// This is useful when working with a function that accepts a sequence of arguments +// and you want to pass an array. + +Math.min(42, 6, 27); // = 6 +Math.min([42, 6, 27]); // = NaN (uh-oh!) +Math.min.apply(Math, [42, 6, 27]); // = 6 + +// But, 'call' and 'apply' are only temporary. When we want it to stick, we can use +// bind. + +var boundFunc = anotherFunc.bind(myObj); +boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!" + +// Bind can also be used to partially apply (curry) a function. + +var product = function(a, b){ return a * b; } +var doubler = product.bind(this, 2); +doubler(8); // = 16 +  // When you call a function with the new keyword, a new object is created, and  // made available to the function via the this keyword. Functions designed to be  // called like that are called constructors. diff --git a/julia.html.markdown b/julia.html.markdown index ce55f956..4b946d46 100644 --- a/julia.html.markdown +++ b/julia.html.markdown @@ -105,7 +105,7 @@ some_var #=> 5  # Accessing a previously unassigned variable is an error  try -  some_other_var #=> ERROR: some_other_var not defined +    some_other_var #=> ERROR: some_other_var not defined  catch e      println(e)  end @@ -417,7 +417,7 @@ try      defaults('h') #=> ERROR: no method defaults(Char,)      defaults() #=> ERROR: no methods defaults()  catch e -println(e) +    println(e)  end  # You can define functions that take keyword arguments @@ -509,7 +509,7 @@ type Tiger  end  # The default constructor's arguments are the properties -# of the tyep, in order the order they are listed in the definition +# of the type, in the order they are listed in the definition  tigger = Tiger(3.5,"orange") #=> Tiger(3.5,"orange")  # The type doubles as the constructor function for values of that type diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 1ed0ed58..419c0475 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -50,52 +50,101 @@ int main (int argc, const char * argv[])      // String      NSString *worldString = @"World"; -    NSLog(@"Hello %@!", worldString); // Print "Hello World!" +    NSLog(@"Hello %@!", worldString); // prints => "Hello World!"  +    // NSMutableString is a mutable version of the NSString object. +    NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello"]; +    [mutableString appendString:@" World!"]; +    NSLog(@"%@", mutableString); // prints => "Hello World!"      // Character literals      NSNumber *theLetterZNumber = @'Z'; -    char theLetterZ            = [theLetterZNumber charValue]; +    char theLetterZ            = [theLetterZNumber charValue]; // or 'Z'      NSLog(@"%c", theLetterZ);      // Integral literals      NSNumber *fortyTwoNumber = @42; -    int fortyTwo             = [fortyTwoNumber intValue]; +    int fortyTwo             = [fortyTwoNumber intValue]; // or 42      NSLog(@"%i", fortyTwo);      NSNumber *fortyTwoUnsignedNumber = @42U; -    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue]; +    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue]; // or 42      NSLog(@"%u", fortyTwoUnsigned);      NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42]; -    short fortyTwoShort           = [fortyTwoShortNumber shortValue]; +    short fortyTwoShort           = [fortyTwoShortNumber shortValue]; // or 42      NSLog(@"%hi", fortyTwoShort); + +    NSNumber *fortyTwoShortNumber   = [NSNumber numberWithShort:41]; +    unsigned short fortyTwoUnsigned = [fortyTwoShortNumber unsignedShortValue]; // or 41 +    NSLog(@"%hu", fortyTwoUnsigned);      NSNumber *fortyTwoLongNumber = @42L; -    long fortyTwoLong            = [fortyTwoLongNumber longValue]; +    long fortyTwoLong            = [fortyTwoLongNumber longValue]; // or 42      NSLog(@"%li", fortyTwoLong); +    NSNumber *fortyTwoLongNumber     = @53L; +    unsigned long fiftyThreeUnsigned = [fortyTwoLongNumber unsignedLongValue]; // or 53 +    NSLog(@"%lu", fiftyThreeUnsigned); +      // Floating point literals      NSNumber *piFloatNumber = @3.141592654F; -    float piFloat           = [piFloatNumber floatValue]; -    NSLog(@"%f", piFloat); +    float piFloat           = [piFloatNumber floatValue]; // or 3.141592654f +    NSLog(@"%f", piFloat); // prints => 3.141592654 +    NSLog(@"%5.2f", piFloat); // prints => " 3.14"      NSNumber *piDoubleNumber = @3.1415926535; -    double piDouble                 = [piDoubleNumber doubleValue]; +    double piDouble          = [piDoubleNumber doubleValue]; // or 3.1415926535      NSLog(@"%f", piDouble); +    NSLog(@"%4.2f", piDouble); // prints => "3.14" + +    // NSDecimalNumber is a fixed-point class that's more precise then float or double +    NSDecimalNumber *oneDecNum = [NSDecimalNumber decimalNumberWithString:@"10.99"]; +    NSDecimalNumber *twoDecNum = [NSDecimalNumber decimalNumberWithString:@"5.002"]; +    // NSDecimalNumber isn't able to use standard +, -, *, / operators so it provides its own: +    [oneDecNum decimalNumberByAdding:twoDecNum];  +    [oneDecNum decimalNumberBySubtracting:twoDecNum]; +    [oneDecNum decimalNumberByMultiplyingBy:twoDecNum]; +    [oneDecNum decimalNumberByDividingBy:twoDecNum]; +    NSLog(@"%@", oneDecNum); // prints => 10.99 as NSDecimalNumber is immutable.       // BOOL literals      NSNumber *yesNumber = @YES;      NSNumber *noNumber  = @NO; +    // or +    BOOL yesBool = YES; +    BOOL noBool  = NO; +    NSLog(@"%i", yesBool); // prints => 1      // Array object      NSArray *anArray      = @[@1, @2, @3, @4];      NSNumber *thirdNumber = anArray[2];      NSLog(@"Third number = %@", thirdNumber); // Print "Third number = 3" +    // NSMutableArray is mutable version of NSArray allowing to change items in array +    // and extend or shrink array object. Convenient, but not as efficient as NSArray. +    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2]; +    [mutableArray addObject:@"Hello"]; +    [mutableArray addObject:@"World"]; +    [mutableArray removeObjectAtIndex:0]; +    NSLog(@"%@", [mutableArray objectAtIndex:0]); // prints => "World"      // Dictionary object      NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };      NSObject *valueObject     = aDictionary[@"A Key"];      NSLog(@"Object = %@", valueObject); // Print "Object = (null)" +    // NSMutableDictionary also available as a mutable dictionary object. +    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2]; +    [mutableDictionary setObject:@"value1" forKey:@"key1"]; +    [mutableDictionary setObject:@"value2" forKey:@"key2"]; +    [mutableDictionary removeObjectForKey:@"key1"]; + +    // Set object +    NSSet *set = [NSSet setWithObjects:@"Hello", @"Hello", @"World", nil]; +    NSLog(@"%@", set); // prints => {(Hello, World)} (may be in different order) +    // NSMutableSet also available as a mutable set object.  +    NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:2]; +    [mutableSet addObject:@"Hello"]; +    [mutableSet addObject:@"Hello"]; +    NSLog(@"%@", mutableSet); // prints => {(Hello)}      ///////////////////////////////////////      // Operators @@ -176,6 +225,14 @@ int main (int argc, const char * argv[])        //           "2,"        //           "3," +    // Object for loop statement. Can be used with any Objective-C object type. +    for (id item in values) {  +        NSLog(@"%@,", item);  +    } // => prints "0,"  +      //           "1," +      //           "2," +      //           "3," +      // Try-Catch-Finally statements      @try      { @@ -225,14 +282,30 @@ int main (int argc, const char * argv[])  // @end  @interface MyClass : NSObject <MyProtocol>  { -    int count; -    id data; -    NSString *name; +    // Instance variable declarations (can exist in either interface or implementation file) +    int count; // Protected access by default.  +    @private id data; // Private access. (More convenient to declare in implementation file) +    NSString *name;   } -// Convenience notation to auto generate public getter and setter +// Convenient notation to auto generate public access getter and setter  @property int count;  @property (copy) NSString *name; // Copy the object during assignment.  @property (readonly) id data;    // Declare only a getter method. +// To access public variable in implementation file, use '_' followed by variable name: +_count = 5; +NSLog(@"%d", _count); // prints => 5 +// To access public variable outside implementation file, @property generates setter method +// automatically. Method name is 'set' followed by @property variable name: +MyClass *myClass = [[MyClass alloc] init]; // create MyClass object instance. +[myClass setCount:10];  +NSLog(@"%@", [myClass count]); // prints => 10 +// You can customize the getter and setter names instead of using default 'set' name: +@property (getter=countGet, setter=countSet:) int count;  +[myClass countSet:32]; +NSLog(@"%i", [myClass countGet]); // prints => 32 +// For convenience, you may use dot notation to set object instance variables: +myClass.count = 45; +NSLog(@"%i", myClass.count); // prints => 45  // Methods  +/- (return type)methodSignature:(Parameter Type *)parameterName; @@ -247,8 +320,9 @@ int main (int argc, const char * argv[])  @end  // Implement the methods in an implementation (MyClass.m) file: - -@implementation MyClass +@implementation MyClass { +    long count; // Private access instance variable. +}  // Call when the object is releasing  - (void)dealloc diff --git a/scala.html.markdown b/scala.html.markdown index 03c1ea76..5dfaefe0 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -396,7 +396,7 @@ object Application {  // To read a file line by line  import scala.io.Source -for(line <- Source.fromPath("myfile.txt").getLines()) +for(line <- Source.fromFile("myfile.txt").getLines())    println(line)  // To write a file use Java's PrintWriter | 
