Skip to content

Instantly share code, notes, and snippets.

@fmaylinch
Last active September 15, 2025 00:43
Show Gist options
  • Save fmaylinch/4a708abcdb42bf2330e5 to your computer and use it in GitHub Desktop.
Save fmaylinch/4a708abcdb42bf2330e5 to your computer and use it in GitHub Desktop.

Revisions

  1. fmaylinch revised this gist Nov 16, 2019. 1 changed file with 53 additions and 45 deletions.
    98 changes: 53 additions & 45 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -154,6 +154,7 @@ const name = "John Smith"; // Constant. Strings also with ''.
    let height = 1.75;
    let hasChildren = false;
    let none = null;
    let none2 = undefined; // not the same as null
    ```

    **Kotlin**
    @@ -572,9 +573,58 @@ let area3 = triangleAreaImplicit(10, 15)

    [Back to top](#code-cheat-sheet)

    ## Classes and objects
    ## Arrays and Dictionaries

    TODO
    **Java**

    ```java
    import java.util.*; // For collections, except arrays

    // arrays are created with a specific size, which can't change
    int n = 10;
    int[] array = new int[n]; // array with n slots
    array[0] = 5;
    array[1] = 10;
    int length = array.length; // 10
    int a0 = array[0] // 5
    int a2 = array[2] // 0 (default value for `int`)

    // In type parameters we can't use primitive types like `int`
    List<Integer> list = new ArrayList<>(); // empty list
    list.add(5);
    list.add(10);
    num size = list.size(); // 2
    int b0 = list.get(0); // 5
    int b3 = list.get(3); // IndexOutOfBoundsException
    ```

    **JavaScript**

    ```javascript
    let array = [5, 10]; // can be initially empty or not
    array.push(15); // add
    array.push("text"); // mix types
    array.push(null);
    let a0 = array[0]; // 5
    let a3 = array[3]; // "text"
    let a4 = array[4]; // null
    let a5 = array[5]; // undefined
    int length = array.length; // 5
    ```

    **Python**

    ```python
    array = [5, 10] # can be initially empty or not
    array.append(15) # add
    array.append("text") # mix types
    array.append(None)
    a0 = array[0] # 5
    a3 = array[3] # "text"
    a4 = array[4] # None
    a5 = array[5] # IndexError: list index out of range
    length = len(array) # 5
    ```

    ## Snippets

    @@ -617,55 +667,13 @@ extension String {

    ```

    ## More
    ## More to do

    **To do**:
    * conditions and flow control (if/else, while, for)
    * Classes and objects
    * Arrays/lists
    * Maps/dictionaries
    * Dates
    * Pass functions as arguments
    * primitives/classes and wrapping

    **Java**

    ```java
    ```

    **JavaScript**

    ```javascript
    ```

    **Kotlin**

    ```kotlin
    ```

    **Objective-C**

    ```objectivec
    ```

    **Python**

    ```python
    ```

    **Ruby**

    ```ruby
    ```

    **Scala**

    ```scala
    ```

    **Swift**

    ```swift
    ```

    [Back to top](#code-cheat-sheet)
  2. fmaylinch revised this gist Nov 16, 2019. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Code cheat sheet

    Common lines of code in different languages for quick reference and comparison: *Java, JavaScript, Kotlin, Objective-C, Python, Ruby, Scala, Swift*.
    Common code statements in different languages for quick reference and comparison: *Java, JavaScript, Kotlin, Objective-C, Python, Ruby, Scala, Swift*.

    ### Contents

    @@ -20,7 +20,7 @@ This document shows common code constructions in some popular languages. You can

    If you see something that could be improved, added or fixed please [let me know](mailto:[email protected]). I will gladly consider your suggestions.

    This document was edited using the wonderful [Classeur](http://classeur.io/).
    This document was edited using the wonderful [StackEdit](https://stackedit.io/).

    [Back to top](#code-cheat-sheet)

    @@ -32,7 +32,7 @@ To edit files you may use editors like [Brackets](http://brackets.io/), [Sublime

    **Java**

    * Install [Java JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html?ssSourceSiteId=otnes) (latest version: 8).
    * Install [Java JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html?ssSourceSiteId=otnes).
    * Create `HelloWorld.java` file with the content shown below.
    * From the command line, compile the file with `javac HelloWorld.java` (a file called `HelloWorld.class` will be created.
    * Execute the compiled file with `java HelloWorld` (note you don't add the file extension there, just the *class* name).
    @@ -85,7 +85,7 @@ Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS a

    **Python**

    * Download and install [Python](https://www.python.org) (version 2 is recommended).
    * Download and install [Python](https://www.python.org).
    * Create a file `hello_world.py` with content below.
    * Execute the script with `python hello_world.py`.

    @@ -143,6 +143,7 @@ int age = 18;
    final String name = "John Smith"; // Constant (final)
    double height = 1.75;
    boolean hasChildren = false;
    String none = null;
    ```

    **JavaScript**
    @@ -152,6 +153,7 @@ let age = 18; // `let` from ES6, previously `var`
    const name = "John Smith"; // Constant. Strings also with ''.
    let height = 1.75;
    let hasChildren = false;
    let none = null;
    ```

    **Kotlin**
    @@ -161,6 +163,7 @@ var age = 18
    val name = "John Smith" // Constant (val)
    var height : Double = 1.75 // Explicit type is optional
    var hasChildren = false
    var none = null
    ```

    **Objective-C**
    @@ -170,6 +173,7 @@ int age = 18;
    NSString* name = @"John Smith";
    double height = 1.75;
    BOOL hasChildren = NO; // also `false`
    NSString* none = nil
    ```

    **Python**
    @@ -179,6 +183,7 @@ age = 18
    name = "John Smith" # Also with ''
    height = 1.75
    has_children = False
    none = None
    ```

    **Ruby**
    @@ -188,6 +193,7 @@ age = 18
    name = 'John Smith' # With "" you can interpolate
    height = 1.75
    has_children = false
    none = nil
    ```

    **Scala**
    @@ -197,6 +203,7 @@ var age = 18
    val name = "John Smith" // Constant (val)
    var height : Double = 1.75 // Explicit type is optional
    var hasChildren = false
    var none = null
    ```

    **Swift**
    @@ -206,6 +213,7 @@ var age = 18
    let name = "John Smith" // Constant (let)
    var height : Double = 1.75 // Explicit type is optional
    var hasChildren = false
    var none = nil
    ```

    [Back to top](#code-cheat-sheet)
    @@ -578,6 +586,7 @@ Here you will find useful snippets that are referred in some code examples.
    /**
    * Swift string manipulation is somewhat complex.
    * This extension methods simplify things.
    * NOTE: This might be obsolete due to Swift updates.
    */
    extension String {

    @@ -610,7 +619,7 @@ extension String {

    ## More

    To do:
    **To do**:
    * conditions and flow control (if/else, while, for)
    * Classes and objects
    * Arrays/lists
  3. fmaylinch revised this gist Dec 9, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -20,15 +20,15 @@ This document shows common code constructions in some popular languages. You can

    If you see something that could be improved, added or fixed please [let me know](mailto:[email protected]). I will gladly consider your suggestions.

    This document was edited using the wonderful [Classeur](app.classeur.io).
    This document was edited using the wonderful [Classeur](http://classeur.io/).

    [Back to top](#code-cheat-sheet)

    ## Getting started

    Instructions to install language interpreter and write a minimal runnable program.

    To edit files you may use editors like [Brackets](http://brackets.io/), [Sublime Text](https://www.sublimetext.com/) or [Atom](https://atom.io/).
    To edit files you may use editors like [Brackets](http://brackets.io/), [Sublime Text](https://www.sublimetext.com/3) or [Atom](https://atom.io/).

    **Java**

  4. fmaylinch revised this gist Dec 9, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -570,10 +570,15 @@ TODO

    ## Snippets

    Here you will find useful snippets that are referred in some code examples.

    **Swift**

    ```swift
    /** Extension methods with simpler or shorter sintax */
    /**
    * Swift string manipulation is somewhat complex.
    * This extension methods simplify things.
    */
    extension String {

    // http://stackoverflow.com/questions/24044851 - substring and ranges
  5. fmaylinch revised this gist Dec 9, 2016. 1 changed file with 65 additions and 33 deletions.
    98 changes: 65 additions & 33 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@ Common lines of code in different languages for quick reference and comparison:
    * [Strings](#strings)
    * [Functions](#functions)
    * [Classes and objects](#classes-and-objects)
    * [Snippets](#snippets)
    * [More](#more)

    ## Intro
    @@ -147,10 +148,10 @@ boolean hasChildren = false;
    **JavaScript**

    ```javascript
    var age = 18;
    var name = "John Smith"; // Also with ''
    var height = 1.75;
    var hasChildren = false;
    let age = 18; // `let` from ES6, previously `var`
    const name = "John Smith"; // Constant. Strings also with ''.
    let height = 1.75;
    let hasChildren = false;
    ```

    **Kotlin**
    @@ -223,7 +224,7 @@ System.out.println("Hello " + user.getName());
    **JavaScript**

    ```javascript
    var user = database.find(email, pwd);
    let user = database.find(email, pwd);
    console.log("Hello " + user.name);
    ```

    @@ -291,12 +292,12 @@ String[] parts = text.split(" "); // Split
    **JavaScript**

    ```javascript
    var length = name.length(); // Length
    var text = name + " is " + age; // Concatenate
    var b = name.indexOf("Smith") >= 0; // Find/Index
    var first = name.substring(0, 4); // Substring
    var c = name[4]; // Char at
    var parts = text.split(" "); // Split
    let length = name.length(); // Length
    let text = name + " is " + age; // Concatenate
    let b = name.indexOf("Smith") >= 0; // Find/Index
    let first = name.substring(0, 4); // Substring
    let c = name[4]; // Char at
    let parts = text.split(" "); // Split

    ```

    @@ -371,17 +372,19 @@ text = parts.mkString(" ") // Join

    **Swift**

    In the [snippets](#snippets) section there's a `String` extension that is used in this code.

    ```swift
    let length = name.characters.count // Length
    let count = name.characters.count // Length
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, String(age)) // Interpolate
    text += ", height: " + String(height) // Concatenate
    let range = string.rangeOfString("Smith") // Index
    let found = range != nil // Find
    // About substrings
    // http://stackoverflow.com/questions/24029163
    // http://stackoverflow.com/a/24046551/1121497
    let parts = text.componentsSeparatedByString(" ") // Split
    // These lines are using the extension mentioned above
    let length = name.length() // Length
    let firstName = name.substring(0, 4) // Substring
    let parts = text.split(" ") // Split
    ```

    [Back to top](#code-cheat-sheet)
    @@ -392,24 +395,18 @@ How to declare and call functions.

    **Java**

    We use the `static` modifier here (it's for class functions). See [Classes and objects](#classes-and-objects) for more info.

    ```java
    public class Example {

    static double triangleArea(double base, double height) {
    return base * height / 2;
    }

    static void sayHello() {
    System.out.println("Hello!");
    }
    double triangleArea(double base, double height) {
    return base * height / 2;
    }

    public static void main(String[] args) {
    double area = triangleArea(10, 15);
    sayHello();
    }
    void sayHello() {
    System.out.println("Hello!");
    }

    // This code would be placed into some method
    double area = triangleArea(10, 15);
    sayHello();
    ```

    **JavaScript**
    @@ -423,7 +420,7 @@ function sayHello() {
    console.log("Hello!");
    }

    var area = triangleArea(10, 15);
    let area = triangleArea(10, 15);
    sayHello();
    ```

    @@ -469,7 +466,7 @@ In Objective-C you call functions using the identifiers to the left of the colon
    NSLog(@"Hello!");
    }

    // Put this code inside some function
    // This code would be placed into some method
    double area = [self triangleAreaWithBase:10 height:15];
    [self sayHello];
    ```
    @@ -571,6 +568,41 @@ let area3 = triangleAreaImplicit(10, 15)

    TODO

    ## Snippets

    **Swift**

    ```swift
    /** Extension methods with simpler or shorter sintax */
    extension String {

    // http://stackoverflow.com/questions/24044851 - substring and ranges

    func substring(range: NSRange) -> String {
    return substring(range.location, range.location + range.length)
    }

    func substring(start:Int, _ end:Int) -> String {
    let from = index(start)
    let to = index(end)
    return self[from..<to]
    }

    func index(pos: Int) -> Index {
    return pos >= 0 ? startIndex.advancedBy(pos) : endIndex.advancedBy(pos)
    }

    func length() -> Int {
    return characters.count
    }

    func split(separator: String) -> [String] {
    return componentsSeparatedByString(separator)
    }
    }

    ```

    ## More

    To do:
  6. fmaylinch revised this gist Apr 29, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -458,7 +458,7 @@ fun sayHello() {

    **Objective-C**

    In Objective-C you call functions using the texts to the left of the colons (i.e. `triangleAreaWithBase` and `height`), and inside the function you refer to the arguments using the names to the right of the colons (i.e. `b` an `h`).
    In Objective-C you call functions using the identifiers to the left of the colons (i.e. `triangleAreaWithBase` and `height`), and inside the function you refer to the arguments using the identifiers to the right of the colons (i.e. `b` an `h`).

    ```objectivec
    + (double) triangleAreaWithBase:(double)b height:(double)h {
    @@ -491,7 +491,7 @@ sayHello()

    **Ruby**

    In Ruby parenthesis for functions are optional. Even for declaring or calling functions with multiple parameters.
    In Ruby parenthesis for functions are optional. Even for declaring or calling functions with multiple parameters. Anyway, we have used parenthesis in this example.

    ```ruby
    def triangleArea(base, height)
  7. fmaylinch revised this gist Apr 29, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -27,14 +27,14 @@ This document was edited using the wonderful [Classeur](app.classeur.io).

    Instructions to install language interpreter and write a minimal runnable program.

    To edit files you may use editors like [Atom](https://atom.io/) or [Sublime Text](https://www.sublimetext.com/).
    To edit files you may use editors like [Brackets](http://brackets.io/), [Sublime Text](https://www.sublimetext.com/) or [Atom](https://atom.io/).

    **Java**

    * Install [Java JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html?ssSourceSiteId=otnes) (latest version: 8).
    * Create `HelloWorld.java` file with the content shown below.
    * From the command line, compile the file with `javac HelloWorld.java` (a file called `HelloWorld.class` will be created.
    * Execute the compiled file with `java HelloWorld`.
    * Execute the compiled file with `java HelloWorld` (note you don't add the file extension there, just the *class* name).

    In a Java program you need at least to declare a [class](#classes-and-objects) and a [function](#functions) called `main` (which is the program entry point). Any statement must go inside a function. And any function must go inside a class.

  8. fmaylinch revised this gist Apr 2, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Code cheat sheet

    Common lines of code in different languages for quick reference and comparison: *Java, JavaScript, Objective-C, Python, Ruby, Scala, Swift*.
    Common lines of code in different languages for quick reference and comparison: *Java, JavaScript, Kotlin, Objective-C, Python, Ruby, Scala, Swift*.

    ### Contents

    @@ -592,6 +592,11 @@ To do:
    ```javascript
    ```

    **Kotlin**

    ```kotlin
    ```

    **Objective-C**

    ```objectivec
  9. fmaylinch revised this gist Apr 2, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -68,7 +68,15 @@ console.log("Hello world!"); // semicolon is not mandatory but recommended

    **Kotlin**

    [Kotlin](https://kotlinlang.org/) can be used standalone or (most probably) integrated in a Java project in Eclipse or IntelliJ IDEA. You can also [try it here online](http://try.kotlinlang.org/).
    [Kotlin](https://kotlinlang.org/) can be used standalone or (most probably) integrated in a Java project inside Eclipse or IntelliJ IDEA. You can also [try it here online](http://try.kotlinlang.org/).

    For a standalone program you could create a `HelloWorld.kt` file with:

    ```kotlin
    fun main(args: Array<String>) {
    println("Hello world!")
    }
    ```

    **Objective-C**

  10. fmaylinch revised this gist Apr 2, 2016. 1 changed file with 81 additions and 16 deletions.
    97 changes: 81 additions & 16 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ Common lines of code in different languages for quick reference and comparison:
    * [Strings](#strings)
    * [Functions](#functions)
    * [Classes and objects](#classes-and-objects)
    * [More to do...](#more-to-do)
    * [More](#more)

    ## Intro

    @@ -51,7 +51,7 @@ public class HelloWorld {

    **JavaScript**

    You may execute JavaScript from the console of your browser. Find out how to open the console. In Chrome it's `Alt+Cmd+J`.
    You may execute JavaScript from the console of your browser. Find out how to open the console in your favourite browser. In Chrome it's `Alt+Cmd+J`.

    You can include JavaScript files from an HTML file. When the HTML is loaded by the browser, the JavaScript files will be executed.

    @@ -66,6 +66,10 @@ You can also execute scripts from the command line. To do that:
    console.log("Hello world!"); // semicolon is not mandatory but recommended
    ```

    **Kotlin**

    [Kotlin](https://kotlinlang.org/) can be used standalone or (most probably) integrated in a Java project in Eclipse or IntelliJ IDEA. You can also [try it here online](http://try.kotlinlang.org/).

    **Objective-C**

    Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS apps. Consider also using Swift instead of Objective-C, since it's a clearer and more modern language, and also easier to use from the command line.
    @@ -121,6 +125,8 @@ print("Hello world!")

    Basic variable declaration and initialization using some common types (numbers, strings, booleans). Here you can also see name conventions (`camelCase`, `snake_case`) and line comments.

    Some languages are *statically typed*:; every variable has a permanent type, specified when it's declared. In some languages like Kotlin, Scala or Swift you don't need to specify the type; it is inferred from the value you assign to it in the initialization.

    **Java**

    ```java
    @@ -139,6 +145,15 @@ var height = 1.75;
    var hasChildren = false;
    ```

    **Kotlin**

    ```kotlin
    var age = 18
    val name = "John Smith" // Constant (val)
    var height : Double = 1.75 // Explicit type is optional
    var hasChildren = false
    ```

    **Objective-C**

    ```objectivec
    @@ -168,8 +183,6 @@ has_children = false

    **Scala**

    Scala is statically typed; every variable has a type. If you don't specify the type it is inferred from the value you assign to it in the initialization.

    ```scala
    var age = 18
    val name = "John Smith" // Constant (val)
    @@ -179,8 +192,6 @@ var hasChildren = false

    **Swift**

    Swift is statically typed; every variable has a type. If you don't specify the type it is inferred from the value you assign to it in the initialization.

    ```swift
    var age = 18
    let name = "John Smith" // Constant (let)
    @@ -208,6 +219,13 @@ var user = database.find(email, pwd);
    console.log("Hello " + user.name);
    ```

    **Kotlin**

    ```kotlin
    var user = database.find(email, pwd)
    println("Hello " + user.name)
    ```

    **Objective-C**

    ```objectivec
    @@ -274,6 +292,20 @@ var parts = text.split(" "); // Split

    ```

    **Kotlin**

    ```kotlin
    val length = name.length // Length
    var text = "$name is $age" // Interpolate
    text += ", height: " + height // Concatenate
    var found = name.contains("Smith") // Find
    found = name.indexOf(" ") >= 0 // Index
    val firstName = name.substring(0, 4) // Substring
    val c = name(4) // Char at
    val parts = text.split("\\s".toRegex()) // Split (explicit regex)
    text = parts.joinToString(" ") // Join
    ```

    **Objective-C**

    ```objectivec
    @@ -355,17 +387,21 @@ How to declare and call functions.
    We use the `static` modifier here (it's for class functions). See [Classes and objects](#classes-and-objects) for more info.

    ```java
    static double triangleArea(double base, double height) {
    return base * height / 2;
    }
    public class Example {

    static void sayHello() {
    System.out.println("Hello!");
    }
    static double triangleArea(double base, double height) {
    return base * height / 2;
    }

    // Put this code inside some function
    double area = triangleArea(10, 15);
    sayHello();
    static void sayHello() {
    System.out.println("Hello!");
    }

    public static void main(String[] args) {
    double area = triangleArea(10, 15);
    sayHello();
    }
    }
    ```

    **JavaScript**
    @@ -383,6 +419,35 @@ var area = triangleArea(10, 15);
    sayHello();
    ```

    **Kotlin**

    ```kotlin
    fun triangleArea(base: Double, height: Double): Double {
    return base * height / 2
    }

    fun sayHello(): Unit {
    println("Hello!")
    }

    fun main(args: Array<String>) {
    val area = triangleArea(10.0, 15.0)
    sayHello()
    }
    ```

    Shorter way to define functions:

    ```kotlin
    // For one-liners, return type is inferred
    fun triangleArea(base: Double, height: Double) = base * height / 2

    // If return type is Unit you can omit it
    fun sayHello() {
    println("Hello!")
    }
    ```

    **Objective-C**

    In Objective-C you call functions using the texts to the left of the colons (i.e. `triangleAreaWithBase` and `height`), and inside the function you refer to the arguments using the names to the right of the colons (i.e. `b` an `h`).
    @@ -498,7 +563,7 @@ let area3 = triangleAreaImplicit(10, 15)

    TODO

    ## More...
    ## More

    To do:
    * conditions and flow control (if/else, while, for)
  11. fmaylinch revised this gist Apr 2, 2016. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,12 @@ Common lines of code in different languages for quick reference and comparison:

    ## Intro

    This document shows common code constructions in some popular languages. One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.
    This document shows common code constructions in some popular languages. You can use it as a quick reference or to see languages side by side, to compare their differences and similarities.

    If you see something that could be improved, added or fixed please [let me know](mailto:[email protected]). I will gladly consider your suggestions.

    This document was edited using the wonderful [Classeur](app.classeur.io).

    [Back to top](#code-cheat-sheet)

    ## Getting started
    @@ -499,11 +501,13 @@ TODO
    ## More...

    To do:
    * Define classes and create objects
    * conditions and flow control (if/else, while, for)
    * Classes and objects
    * Arrays/lists
    * Maps/dictionaries
    * Dates
    * Pass functions as arguments
    * Wrapped primitives
    * primitives/classes and wrapping

    **Java**

  12. fmaylinch revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Common lines of code in different languages for quick reference and comparison:
    ### Contents

    * [Intro](#intro)
    * [Getting started](#start)
    * [Getting started](#getting-started)
    * [Variables](#variables)
    * [Functions and properties](#functions-and-properties)
    * [Strings](#strings)
  13. fmaylinch revised this gist Mar 18, 2016. No changes.
  14. fmaylinch revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ If you see something that could be improved, added or fixed please [let me know]

    [Back to top](#code-cheat-sheet)

    ## <a name="start"></a> Getting started
    ## Getting started

    Instructions to install language interpreter and write a minimal runnable program.

  15. fmaylinch revised this gist Mar 13, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Common lines of code in different languages for quick reference and comparison:
    ### Contents

    * [Intro](#intro)
    * [Getting started](#getting-started)
    * [Getting started](#start)
    * [Variables](#variables)
    * [Functions and properties](#functions-and-properties)
    * [Strings](#strings)
    @@ -21,7 +21,7 @@ If you see something that could be improved, added or fixed please [let me know]

    [Back to top](#code-cheat-sheet)

    ## <a name="getting-started"></a> Getting started
    ## <a name="start"></a> Getting started

    Instructions to install language interpreter and write a minimal runnable program.

  16. fmaylinch revised this gist Mar 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ If you see something that could be improved, added or fixed please [let me know]

    [Back to top](#code-cheat-sheet)

    ## Getting started
    ## <a name="getting-started"></a> Getting started

    Instructions to install language interpreter and write a minimal runnable program.

  17. fmaylinch revised this gist Mar 13, 2016. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -60,13 +60,13 @@ You can also execute scripts from the command line. To do that:
    * Execute the script with `node hello_world.js`.

    ```javascript
    "use strict" // start your scripts with this to avoid bad practices
    console.log("Hello world!");
    "use strict" // start your scripts with this to avoid bad practices
    console.log("Hello world!"); // semicolon is not mandatory but recommended
    ```

    **Objective-C**

    Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS apps. Consider also using Swift instead of Objective-C, since it's a clearer and more modern language.
    Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS apps. Consider also using Swift instead of Objective-C, since it's a clearer and more modern language, and also easier to use from the command line.

    **Python**

    @@ -256,6 +256,7 @@ String text = name + " is " + age; // Concatenate
    boolean b = name.contains("Smith"); // Find
    b = name.indexOf("Smith") >= 0; // Index
    String first = name.substring(0, 4); // Substring
    char c = name.charAt(4); // Char at
    String[] parts = text.split(" "); // Split
    ```

    @@ -266,6 +267,7 @@ var length = name.length(); // Length
    var text = name + " is " + age; // Concatenate
    var b = name.indexOf("Smith") >= 0; // Find/Index
    var first = name.substring(0, 4); // Substring
    var c = name[4]; // Char at
    var parts = text.split(" "); // Split

    ```
    @@ -290,7 +292,9 @@ length = len(name) # length
    text = name + " is " + str(age) # Concatenate
    found = "Smith" in name # Find
    found = name.find(" ") >= 0 # Index
    first_name = name[0:4] # Substring
    first_name = name[0:4] # Substring (also `name[:4]`)
    last_name = name[-5:] # Substring
    c = name[4] # Char at
    parts = text.split(" ") # Split
    ```

    @@ -303,6 +307,8 @@ text += ", height: " + height.to_s # Concatenate
    found = name.include? "Smith" # Find
    index = name.index(" ") # Index (truthy if found)
    first_name = name[0...4] # Substring
    last_name = name[-5..-1] # Substring
    c = name[4] # Char at
    parts = text.split(" ") # Split
    text = parts.join(" ") # Join
    ```
    @@ -316,6 +322,7 @@ text += ", height: " + height // Concatenate
    var found = name.contains("Smith") // Find
    found = name.indexOf(" ") >= 0 // Index
    val firstName = name.substring(0, 4) // Substring
    val c = name(4) // Char at
    val parts = text.split(" ") // Split
    text = parts.mkString(" ") // Join
    ```
    @@ -343,7 +350,7 @@ How to declare and call functions.

    **Java**

    Use the `static` modifier. See [Classes and objects](#classes-and-objects) for more info.
    We use the `static` modifier here (it's for class functions). See [Classes and objects](#classes-and-objects) for more info.

    ```java
    static double triangleArea(double base, double height) {
  18. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -310,6 +310,7 @@ text = parts.join(" ") # Join
    **Scala**

    ```scala
    val length = name.length // Length
    var text = s"$name is $age" // Interpolate
    text += ", height: " + height // Concatenate
    var found = name.contains("Smith") // Find
    @@ -322,7 +323,7 @@ text = parts.mkString(" ") // Join
    **Swift**

    ```swift
    var length = name.characters.count // Length
    let length = name.characters.count // Length
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, String(age)) // Interpolate
    text += ", height: " + String(height) // Concatenate
    @@ -482,16 +483,20 @@ let area2 = triangleAreaExplicit(b: 10, h:15)
    let area3 = triangleAreaImplicit(10, 15)
    ```


    [Back to top](#code-cheat-sheet)

    ## Classes and objects

    TODO

    ## More...

    To do:
    * Define classes and create objects
    * Arrays/lists
    * Maps/dictionaries
    * Pass functions as arguments
    * Wrapped primitives

    **Java**

  19. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 34 additions and 10 deletions.
    44 changes: 34 additions & 10 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,10 @@ Common lines of code in different languages for quick reference and comparison:
    ### Contents

    * [Intro](#intro)
    * [Basic things to consider](#basic-things-to-consider)
    * [Variable declaration](#variable-declaration)
    * [Function calls, property access, print things](#function-calls-property-access-print-things)
    * [String manipulation](#string-manipulation)
    * [Getting started](#getting-started)
    * [Variables](#variables)
    * [Functions and properties](#functions-and-properties)
    * [Strings](#strings)
    * [Functions](#functions)
    * [Classes and objects](#classes-and-objects)
    * [More to do...](#more-to-do)
    @@ -21,7 +21,7 @@ If you see something that could be improved, added or fixed please [let me know]

    [Back to top](#code-cheat-sheet)

    ## Install and minimum program
    ## Getting started

    Instructions to install language interpreter and write a minimal runnable program.

    @@ -115,9 +115,9 @@ print("Hello world!")
    [Back to top](#code-cheat-sheet)


    ## Variable declaration
    ## Variables

    Basic variable for some common types (numbers, strings, booleans). Here you can also see name conventions (`camelCase`, `snake_case`) and line comments.
    Basic variable declaration and initialization using some common types (numbers, strings, booleans). Here you can also see name conventions (`camelCase`, `snake_case`) and line comments.

    **Java**

    @@ -166,6 +166,8 @@ has_children = false

    **Scala**

    Scala is statically typed; every variable has a type. If you don't specify the type it is inferred from the value you assign to it in the initialization.

    ```scala
    var age = 18
    val name = "John Smith" // Constant (val)
    @@ -175,6 +177,8 @@ var hasChildren = false

    **Swift**

    Swift is statically typed; every variable has a type. If you don't specify the type it is inferred from the value you assign to it in the initialization.

    ```swift
    var age = 18
    let name = "John Smith" // Constant (let)
    @@ -184,9 +188,9 @@ var hasChildren = false

    [Back to top](#code-cheat-sheet)

    ## Function calls, property access, print things
    ## Functions and properties

    Basic usage of functions, properties, and how to print messages on screen/console. I added this section as an introduction for the following ones.
    Basic usage of functions and properties. I added this section as an introduction for the following ones.

    **Java**

    @@ -240,7 +244,9 @@ print("Hello \(user.name)")

    [Back to top](#code-cheat-sheet)

    ## String manipulation
    ## Strings

    Common operations using strings.

    **Java**

    @@ -459,6 +465,24 @@ let area = triangleArea(10, height:15)
    sayHello()
    ```

    When calling a function in Swift, you need to specify the name of all parameters except for the first one. This is the default, but it's configurable:

    ```swift
    // Here we declare external parameter names
    func triangleAreaExplicit(b base: Double, h height: Double) -> Double {
    return base * height / 2
    }

    // Here the call will be similar to other languages
    func triangleAreaImplicit(base: Double, _ height: Double) -> Double {
    return base * height / 2
    }

    let area2 = triangleAreaExplicit(b: 10, h:15)
    let area3 = triangleAreaImplicit(10, 15)
    ```


    [Back to top](#code-cheat-sheet)

    ## More...
  20. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 179 additions and 4 deletions.
    183 changes: 179 additions & 4 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@ Common lines of code in different languages for quick reference and comparison:
    * [Variable declaration](#variable-declaration)
    * [Function calls, property access, print things](#function-calls-property-access-print-things)
    * [String manipulation](#string-manipulation)
    * [Functions](#functions)
    * [Classes and objects](#classes-and-objects)
    * [More to do...](#more-to-do)

    ## Intro
    @@ -26,15 +28,22 @@ Instructions to install language interpreter and write a minimal runnable progra
    To edit files you may use editors like [Atom](https://atom.io/) or [Sublime Text](https://www.sublimetext.com/).

    **Java**

    * Install [Java JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html?ssSourceSiteId=otnes) (latest version: 8).
    * Create `HelloWorld.java` file with the content below.
    * Create `HelloWorld.java` file with the content shown below.
    * From the command line, compile the file with `javac HelloWorld.java` (a file called `HelloWorld.class` will be created.
    * Execute the compiled file with `java HelloWorld`.

    In a Java program you need at least to declare a [class](#classes-and-objects) and a [function](#functions) called `main` (which is the program entry point). Any statement must go inside a function. And any function must go inside a class.

    ```java
    public class HelloWorld {

    public static void main(String... args) {
    System.out.println("Hello world!");
    }

    // More functions here
    }
    ```

    @@ -45,10 +54,13 @@ You may execute JavaScript from the console of your browser. Find out how to ope
    You can include JavaScript files from an HTML file. When the HTML is loaded by the browser, the JavaScript files will be executed.

    You can also execute scripts from the command line. To do that:

    * Install [node.js](https://nodejs.org).
    * Create `hello_world.js` file with the content below.
    * Execute the script with `node hello_world.js`.

    ```javascript
    "use strict" // start your scripts with this to avoid bad practices
    console.log("Hello world!");
    ```

    @@ -57,25 +69,31 @@ console.log("Hello world!");
    Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS apps. Consider also using Swift instead of Objective-C, since it's a clearer and more modern language.

    **Python**

    * Download and install [Python](https://www.python.org) (version 2 is recommended).
    * Create a file `hello_world.py` with content below.
    * Execute the script with `python hello_world.py`.

    ```python
    print "Hello world!"
    ```

    **Ruby**

    * Download and install [Ruby](https://www.ruby-lang.org).
    * Create a file `hello_world.rb` with content below.
    * Execute the script with `ruby hello_world.rb`.

    ```ruby
    puts "Hello world!"
    ```

    **Scala**

    * Download and install [Scala](http://www.scala-lang.org).
    * Create a file `HelloWorld.scala` with content below.
    * Execute the script with `scala HelloWorld.scala`.

    ```scala
    println("Hello world!")
    ```
    @@ -88,8 +106,8 @@ Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS a
    * Execute the script with `swift HelloWorld.swift`.

    ```swift
    // You usually need to import Foundation
    // Or import UIKit if you want to use view classes
    // You usually need to import Foundation.
    // Or import UIKit if you want to use UI classes.
    import Foundation
    print("Hello world!")
    ```
    @@ -111,6 +129,7 @@ boolean hasChildren = false;
    ```

    **JavaScript**

    ```javascript
    var age = 18;
    var name = "John Smith"; // Also with ''
    @@ -119,6 +138,7 @@ var hasChildren = false;
    ```

    **Objective-C**

    ```objectivec
    int age = 18;
    NSString* name = @"John Smith";
    @@ -127,6 +147,7 @@ BOOL hasChildren = NO; // also `false`
    ```

    **Python**

    ```python
    age = 18
    name = "John Smith" # Also with ''
    @@ -135,6 +156,7 @@ has_children = False
    ```

    **Ruby**

    ```ruby
    age = 18
    name = 'John Smith' # With "" you can interpolate
    @@ -143,6 +165,7 @@ has_children = false
    ```

    **Scala**

    ```scala
    var age = 18
    val name = "John Smith" // Constant (val)
    @@ -151,6 +174,7 @@ var hasChildren = false
    ```

    **Swift**

    ```swift
    var age = 18
    let name = "John Smith" // Constant (let)
    @@ -165,42 +189,49 @@ var hasChildren = false
    Basic usage of functions, properties, and how to print messages on screen/console. I added this section as an introduction for the following ones.

    **Java**

    ```java
    User user = database.find(email, pwd);
    System.out.println("Hello " + user.getName());
    ```

    **JavaScript**

    ```javascript
    var user = database.find(email, pwd);
    console.log("Hello " + user.name);
    ```

    **Objective-C**

    ```objectivec
    User* user = [database find:email pass:pwd]; // Call syntax is quite Weird
    NSLog(@"Hello %@", user.name); // Sometimes you use this C-style syntax
    ```
    **Python**
    ```python
    user = database.find(email, pwd)
    print "Hello " + user.name
    ```

    **Ruby**

    ```ruby
    user = database.find(email, pwd)
    puts "Hello #{user.name}"
    ```

    **Scala**

    ```scala
    val user = database.find(email, pwd)
    println("Hello " + user.name)
    ```

    **Swift**

    ```swift
    // From 2nd parameter you write `parameter:argument`
    let user = database.find(email, pass:pwd)
    @@ -212,6 +243,7 @@ print("Hello \(user.name)")
    ## String manipulation

    **Java**

    ```java
    int length = name.length(); // Length
    String text = name + " is " + age; // Concatenate
    @@ -222,6 +254,7 @@ String[] parts = text.split(" "); // Split
    ```

    **JavaScript**

    ```javascript
    var length = name.length(); // Length
    var text = name + " is " + age; // Concatenate
    @@ -232,6 +265,7 @@ var parts = text.split(" "); // Split
    ```

    **Objective-C**

    ```objectivec
    int lenght = [name length]; // Length
    NSString* text = [NSString stringWithFormat:
    @@ -244,6 +278,7 @@ NSArray<NSString*>* parts = [text componentsSeparatedByString:@" "]; // Split
    ```
    **Python**
    ```python
    length = len(name) # length
    text = name + " is " + str(age) # Concatenate
    @@ -254,6 +289,7 @@ parts = text.split(" ") # Split
    ```

    **Ruby**

    ```ruby
    length = name.length # length
    text = "#{name} is #{age}" # Interpolate
    @@ -266,6 +302,7 @@ text = parts.join(" ") # Join
    ```

    **Scala**

    ```scala
    var text = s"$name is $age" // Interpolate
    text += ", height: " + height // Concatenate
    @@ -277,6 +314,7 @@ text = parts.mkString(" ") // Join
    ```

    **Swift**

    ```swift
    var length = name.characters.count // Length
    var text = "\(name) is \(age)" // Interpolate
    @@ -292,40 +330,177 @@ let parts = text.componentsSeparatedByString(" ") // Split

    [Back to top](#code-cheat-sheet)

    ## Functions

    How to declare and call functions.

    **Java**

    Use the `static` modifier. See [Classes and objects](#classes-and-objects) for more info.

    ```java
    static double triangleArea(double base, double height) {
    return base * height / 2;
    }

    static void sayHello() {
    System.out.println("Hello!");
    }

    // Put this code inside some function
    double area = triangleArea(10, 15);
    sayHello();
    ```

    **JavaScript**

    ```javascript
    function triangleArea(base, height) {
    return base * height / 2;
    }

    function sayHello() {
    console.log("Hello!");
    }

    var area = triangleArea(10, 15);
    sayHello();
    ```

    **Objective-C**

    In Objective-C you call functions using the texts to the left of the colons (i.e. `triangleAreaWithBase` and `height`), and inside the function you refer to the arguments using the names to the right of the colons (i.e. `b` an `h`).

    ```objectivec
    + (double) triangleAreaWithBase:(double)b height:(double)h {
    return b * h / 2;
    }

    + (void) sayHello {
    NSLog(@"Hello!");
    }

    // Put this code inside some function
    double area = [self triangleAreaWithBase:10 height:15];
    [self sayHello];
    ```
    **Python**
    Blocks of code in python are recognised by indentation. There's no `end` keyword or `{ }` symbols to delimit them.
    ```python
    def triangleArea(base, height):
    return base * height / 2
    def sayHello():
    print "Hello!"
    area = triangleArea(10, 15)
    sayHello()
    ```

    **Ruby**

    In Ruby parenthesis for functions are optional. Even for declaring or calling functions with multiple parameters.

    ```ruby
    def triangleArea(base, height)
    return base * height / 2
    end

    def sayHello
    puts 'Hello!'
    end

    area = triangleArea(10, 15)
    sayHello
    ```

    **Scala**

    ```scala
    def triangleArea(base: Double, height: Double): Double = {
    return base * height / 2
    }

    def sayHello(): Unit = {
    println("Hello!")
    }

    val area = triangleArea(10, 15)
    sayHello()
    ```

    Shorter way to define functions:

    ```scala
    // For one-liners, return type is inferred
    def triangleArea(base: Double, height: Double) = base * height / 2

    // If return type is Unit you can omit it
    def sayHello() {
    println("Hello!")
    }
    ```

    **Swift**

    ```swift
    func triangleArea(base: Double, height: Double) -> Double {
    return base * height / 2
    }

    func sayHello() {
    print("Hello!")
    }

    let area = triangleArea(10, height:15)
    sayHello()
    ```

    [Back to top](#code-cheat-sheet)

    ## More...

    To do:
    * Define functions
    * Define classes and create objects
    * Arrays/lists
    * Maps/dictionaries
    * Pass functions as arguments

    **Java**

    ```java
    ```

    **JavaScript**

    ```javascript
    ```

    **Objective-C**

    ```objectivec
    ```

    **Python**

    ```python
    ```

    **Ruby**

    ```ruby
    ```

    **Scala**

    ```scala
    ```

    **Swift**

    ```swift
    ```

  21. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -102,6 +102,7 @@ print("Hello world!")
    Basic variable for some common types (numbers, strings, booleans). Here you can also see name conventions (`camelCase`, `snake_case`) and line comments.

    **Java**

    ```java
    int age = 18;
    final String name = "John Smith"; // Constant (final)
  22. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ Common lines of code in different languages for quick reference and comparison:

    ## Intro

    This document shows common code constructions in different languages (for now, those that I use more frequently). One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.
    This document shows common code constructions in some popular languages. One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.

    If you see something that could be improved, added or fixed please [let me know](mailto:[email protected]). I will gladly consider your suggestions.

  23. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ Common lines of code in different languages for quick reference and comparison:

    This document shows common code constructions in different languages (for now, those that I use more frequently). One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.

    If you see something that could be improved, added or fixed please let me know. I will gladly consider your suggestions.
    If you see something that could be improved, added or fixed please [let me know](mailto:[email protected]). I will gladly consider your suggestions.

    [Back to top](#code-cheat-sheet)

  24. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 90 additions and 22 deletions.
    112 changes: 90 additions & 22 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -19,15 +19,79 @@ If you see something that could be improved, added or fixed please let me know.

    [Back to top](#code-cheat-sheet)

    ## Basic things to consider
    ## Install and minimum program

    **Objective-C** and **Swift**
    Always import `Foundation`, or `UIKit` if using view classes:
    ```objectivec
    @import UIKit; // Objective-C
    Instructions to install language interpreter and write a minimal runnable program.

    To edit files you may use editors like [Atom](https://atom.io/) or [Sublime Text](https://www.sublimetext.com/).

    **Java**
    * Install [Java JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html?ssSourceSiteId=otnes) (latest version: 8).
    * Create `HelloWorld.java` file with the content below.
    * From the command line, compile the file with `javac HelloWorld.java` (a file called `HelloWorld.class` will be created.
    * Execute the compiled file with `java HelloWorld`.
    ```java
    public class HelloWorld {
    public static void main(String... args) {
    System.out.println("Hello world!");
    }
    }
    ```

    **JavaScript**

    You may execute JavaScript from the console of your browser. Find out how to open the console. In Chrome it's `Alt+Cmd+J`.

    You can include JavaScript files from an HTML file. When the HTML is loaded by the browser, the JavaScript files will be executed.

    You can also execute scripts from the command line. To do that:
    * Install [node.js](https://nodejs.org).
    * Create `hello_world.js` file with the content below.
    * Execute the script with `node hello_world.js`.
    ```javascript
    console.log("Hello world!");
    ```

    **Objective-C**

    Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS apps. Consider also using Swift instead of Objective-C, since it's a clearer and more modern language.

    **Python**
    * Download and install [Python](https://www.python.org) (version 2 is recommended).
    * Create a file `hello_world.py` with content below.
    * Execute the script with `python hello_world.py`.
    ```python
    print "Hello world!"
    ```

    **Ruby**
    * Download and install [Ruby](https://www.ruby-lang.org).
    * Create a file `hello_world.rb` with content below.
    * Execute the script with `ruby hello_world.rb`.
    ```ruby
    puts "Hello world!"
    ```

    **Scala**
    * Download and install [Scala](http://www.scala-lang.org).
    * Create a file `HelloWorld.scala` with content below.
    * Execute the script with `scala HelloWorld.scala`.
    ```scala
    println("Hello world!")
    ```

    **Swift**

    Read about how to use [Xcode](https://developer.apple.com/xcode) to create iOS apps. If you installed Xcode, you may also run Swift code from the command line:

    * Create a file `HelloWorld.swift` with content below.
    * Execute the script with `swift HelloWorld.swift`.

    ```swift
    import UIKit // Swift
    // You usually need to import Foundation
    // Or import UIKit if you want to use view classes
    import Foundation
    print("Hello world!")
    ```

    [Back to top](#code-cheat-sheet)
    @@ -184,19 +248,20 @@ length = len(name) # length
    text = name + " is " + str(age) # Concatenate
    found = "Smith" in name # Find
    found = name.find(" ") >= 0 # Index
    first_name = name[0:index] # Substring
    first_name = name[0:4] # Substring
    parts = text.split(" ") # Split
    ```

    **Ruby**
    ```ruby
    length = name.length # length
    text = "#{name} is #{age}" # Interpolate
    text += ", height: " + height # Concatenate
    found = name.include? "Smith" # Find
    index = name.index(" ") # Index (truthy if found)
    first_name = name[0...4] # Substring
    parts = text.split(" ") # Split
    length = name.length # length
    text = "#{name} is #{age}" # Interpolate
    text += ", height: " + height.to_s # Concatenate
    found = name.include? "Smith" # Find
    index = name.index(" ") # Index (truthy if found)
    first_name = name[0...4] # Substring
    parts = text.split(" ") # Split
    text = parts.join(" ") # Join
    ```

    **Scala**
    @@ -207,18 +272,21 @@ var found = name.contains("Smith") // Find
    found = name.indexOf(" ") >= 0 // Index
    val firstName = name.substring(0, 4) // Substring
    val parts = text.split(" ") // Split
    text = parts.mkString(" ") // Join
    ```

    **Swift**
    ```swift
    int length = name.length // Length
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, age) // Interpolate
    text += ", height: " + height // Concatenate
    let range = string.rangeOfString("Smith") // Index
    let found = range != nil // Find
    // Try: http://stackoverflow.com/a/24046551/1121497
    let parts = text.componentsSeparatedByString(" ") // Split
    var length = name.characters.count // Length
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, String(age)) // Interpolate
    text += ", height: " + String(height) // Concatenate
    let range = string.rangeOfString("Smith") // Index
    let found = range != nil // Find
    // About substrings
    // http://stackoverflow.com/questions/24029163
    // http://stackoverflow.com/a/24046551/1121497
    let parts = text.componentsSeparatedByString(" ") // Split
    ```

    [Back to top](#code-cheat-sheet)
  25. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 22 additions and 14 deletions.
    36 changes: 22 additions & 14 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -101,44 +101,45 @@ Basic usage of functions, properties, and how to print messages on screen/consol

    **Java**
    ```java
    User user = database.find(email, pwd); // Call function
    System.out.println("Hello " + user.getName()); // Print property
    User user = database.find(email, pwd);
    System.out.println("Hello " + user.getName());
    ```

    **JavaScript**
    ```javascript
    var user = database.find(email, pwd); // Call function
    console.log("Hello " + user.name); // Print property
    var user = database.find(email, pwd);
    console.log("Hello " + user.name);
    ```

    **Objective-C**
    ```objectivec
    User* user = [database find:email pass:pwd]; // Call function
    NSLog(@"Hello %@", user.name); // Print property
    User* user = [database find:email pass:pwd]; // Call syntax is quite Weird
    NSLog(@"Hello %@", user.name); // Sometimes you use this C-style syntax
    ```
    **Python**
    ```python
    user = database.find(email, pwd) # Call function
    print "Hello " + user.name # Print property
    user = database.find(email, pwd)
    print "Hello " + user.name
    ```

    **Ruby**
    ```ruby
    user = database.find(email, pwd) # Call function
    puts "Hello #{user.name}" # Print property
    user = database.find(email, pwd)
    puts "Hello #{user.name}"
    ```

    **Scala**
    ```scala
    val user = database.find(email, pwd) // Call function
    println("Hello " + user.name) // Print property
    val user = database.find(email, pwd)
    println("Hello " + user.name)
    ```

    **Swift**
    ```swift
    let user = database.find(email, pass:pwd) // Call function
    print("Hello \(user.name)") // Print property
    // From 2nd parameter you write `parameter:argument`
    let user = database.find(email, pass:pwd)
    print("Hello \(user.name)")
    ```

    [Back to top](#code-cheat-sheet)
    @@ -224,6 +225,13 @@ let parts = text.componentsSeparatedByString(" ") // Split

    ## More...

    To do:
    * Define functions
    * Define classes and create objects
    * Arrays/lists
    * Maps/dictionaries
    * Pass functions as arguments

    **Java**
    ```java
    ```
  26. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,7 @@ BOOL hasChildren = NO; // also `false`
    **Python**
    ```python
    age = 18
    name = 'John Smith'
    name = "John Smith" # Also with ''
    height = 1.75
    has_children = False
    ```
  27. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 22 additions and 22 deletions.
    44 changes: 22 additions & 22 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -114,7 +114,7 @@ console.log("Hello " + user.name); // Print property
    **Objective-C**
    ```objectivec
    User* user = [database find:email pass:pwd]; // Call function
    NSLog(@"Hello %@", user.name); // Print property
    NSLog(@"Hello %@", user.name); // Print property
    ```
    **Python**
    @@ -138,7 +138,7 @@ println("Hello " + user.name) // Print property
    **Swift**
    ```swift
    let user = database.find(email, pass:pwd) // Call function
    print("Hello \(user.name)") // Print property
    print("Hello \(user.name)") // Print property
    ```

    [Back to top](#code-cheat-sheet)
    @@ -148,7 +148,7 @@ print("Hello \(user.name)") // Print property
    **Java**
    ```java
    int length = name.length(); // Length
    String text = name + " is " + age; // Concatenate
    String text = name + " is " + age; // Concatenate
    boolean b = name.contains("Smith"); // Find
    b = name.indexOf("Smith") >= 0; // Index
    String first = name.substring(0, 4); // Substring
    @@ -158,7 +158,7 @@ String[] parts = text.split(" "); // Split
    **JavaScript**
    ```javascript
    var length = name.length(); // Length
    var text = name + " is " + age; // Concatenate
    var text = name + " is " + age; // Concatenate
    var b = name.indexOf("Smith") >= 0; // Find/Index
    var first = name.substring(0, 4); // Substring
    var parts = text.split(" "); // Split
    @@ -169,8 +169,8 @@ var parts = text.split(" "); // Split
    ```objectivec
    int lenght = [name length]; // Length
    NSString* text = [NSString stringWithFormat:
    @"%@ is %@", name, @(age)]; // Interpolate
    text = [text stringByAppendingString:@" years old"]; // Concatenate
    @"%@ is %@", name, @(age)]; // Interpolate
    text = [text stringByAppendingString:@" years old"]; // Concatenate
    BOOL b = [name rangeOfString:@"Smith"].location != NSNotFound; // Find/Index
    NSString* first = [name substringToIndex:4]; // Substring
    NSString* last = [name substringWithRange:NSMakeRange(5, 10)]; // Substring
    @@ -180,7 +180,7 @@ NSArray<NSString*>* parts = [text componentsSeparatedByString:@" "]; // Split
    **Python**
    ```python
    length = len(name) # length
    text = name + " is " + str(age) # Concatenate
    text = name + " is " + str(age) # Concatenate
    found = "Smith" in name # Find
    found = name.find(" ") >= 0 # Index
    first_name = name[0:index] # Substring
    @@ -189,19 +189,19 @@ parts = text.split(" ") # Split

    **Ruby**
    ```ruby
    length = name.length # length
    text = "#{name} is #{age}" # Interpolate
    text += ", height: " + height # Concatenate
    found = name.include? "Smith" # Find
    index = name.index(" ") # Index (truthy if found)
    first_name = name[0...4] # Substring
    parts = text.split(" ") # Split
    length = name.length # length
    text = "#{name} is #{age}" # Interpolate
    text += ", height: " + height # Concatenate
    found = name.include? "Smith" # Find
    index = name.index(" ") # Index (truthy if found)
    first_name = name[0...4] # Substring
    parts = text.split(" ") # Split
    ```

    **Scala**
    ```scala
    var text = s"$name is $age" // Interpolate
    text += ", height: " + height // Concatenate
    var text = s"$name is $age" // Interpolate
    text += ", height: " + height // Concatenate
    var found = name.contains("Smith") // Find
    found = name.indexOf(" ") >= 0 // Index
    val firstName = name.substring(0, 4) // Substring
    @@ -210,12 +210,12 @@ val parts = text.split(" ") // Split

    **Swift**
    ```swift
    int length = name.length // Length
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, age)
    text += ", height: " + height // Concatenate
    let range = string.rangeOfString("Smith") // Index
    let found = range != nil // Find
    int length = name.length // Length
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, age) // Interpolate
    text += ", height: " + height // Concatenate
    let range = string.rangeOfString("Smith") // Index
    let found = range != nil // Find
    // Try: http://stackoverflow.com/a/24046551/1121497
    let parts = text.componentsSeparatedByString(" ") // Split
    ```
  28. fmaylinch revised this gist Mar 12, 2016. 1 changed file with 72 additions and 69 deletions.
    141 changes: 72 additions & 69 deletions Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,8 @@

    # Code cheat sheet

    Common lines of code in different languages for quick reference and comparison: *Java, Javascript, Objective-C, Python, Ruby, Scala, Swift*.
    Common lines of code in different languages for quick reference and comparison: *Java, JavaScript, Objective-C, Python, Ruby, Scala, Swift*.

    #### Contents
    ### Contents

    * [Intro](#intro)
    * [Basic things to consider](#basic-things-to-consider)
    @@ -12,205 +11,209 @@ Common lines of code in different languages for quick reference and comparison:
    * [String manipulation](#string-manipulation)
    * [More to do...](#more-to-do)

    ### Intro
    ## Intro

    This document shows common code constructions in different languages (for now, those that I use more frequently). One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.

    If you see something that could be improved, added or fixed please let me know. I will gladly consider your suggestions.

    [Back to top](#code-cheat-sheet)

    ### Basic things to consider
    ## Basic things to consider

    **Objective-C** and **Swift**
    Always import `Foundation`, or `UIKit` if using view classes:
    ```objectivec
    @import UIKit; // Objective-C
    ```
    ```swift
    // Swift
    // You usually need Foundation or UIKit
    import Foundation
    import UIKit // Swift
    ```

    [Back to top](#code-cheat-sheet)


    ### Variable declaration
    ## Variable declaration

    Basic variable declaration for some common types (numbers, strings, booleans). Here you can also see name conventions (`camelCase`, `snake_case`) and line comments.
    Basic variable for some common types (numbers, strings, booleans). Here you can also see name conventions (`camelCase`, `snake_case`) and line comments.

    **Java**
    ```java
    // Java
    int minAge = 18;
    int age = 18;
    final String name = "John Smith"; // Constant (final)
    double height = 1.75;
    boolean male = true;
    boolean hasChildren = false;
    ```

    **JavaScript**
    ```javascript
    // Javascript
    var minAge = 18;
    var age = 18;
    var name = "John Smith"; // Also with ''
    var height = 1.75;
    var male = true;
    var hasChildren = false;
    ```

    **Objective-C**
    ```objectivec
    // Objective C
    int minAge = 18;
    int age = 18;
    NSString* name = @"John Smith";
    double height = 1.75;
    BOOL male = YES;
    BOOL hasChildren = NO; // also `false`
    ```

    **Python**
    ```python
    # Python
    min_age = 18
    age = 18
    name = 'John Smith'
    height = 1.75
    male = True
    has_children = False
    ```

    **Ruby**
    ```ruby
    # Ruby
    min_age = 18
    age = 18
    name = 'John Smith' # With "" you can interpolate
    height = 1.75
    male = true
    has_children = false
    ```

    **Scala**
    ```scala
    // Scala
    var minAge = 18
    var age = 18
    val name = "John Smith" // Constant (val)
    var height : Double = 1.75 // Explicit type is optional
    var male = true
    var hasChildren = false
    ```

    **Swift**
    ```swift
    // Swift
    var minAge = 18
    var age = 18
    let name = "John Smith" // Constant (let)
    var height : Double = 1.75 // Explicit type is optional
    var male = true
    var hasChildren = false
    ```

    [Back to top](#code-cheat-sheet)

    ### Function calls, property access, print things
    ## Function calls, property access, print things

    Basic usage of functions, properties, and how to print messages on screen/console. I added this section as an introduction for the following ones.

    **Java**
    ```java
    // Java
    User user = database.find(email, pwd); // Call function
    System.out.println("Hello " + user.getName()); // Print property
    ```

    **JavaScript**
    ```javascript
    // Javascript
    var user = database.find(email, pwd); // Call function
    console.log("Hello " + user.name); // Print property
    ```

    **Objective-C**
    ```objectivec
    // Objective C
    User* user = [database find:email pass:pwd]; // Call function
    NSLog(@"Hello %@ at %@", user.name, user.city); // Print property
    NSLog(@"Hello %@", user.name); // Print property
    ```
    **Python**
    ```python
    # Python
    user = database.find(email, pwd) # Call function
    print "Hello " + user.name # Print property
    ```

    **Ruby**
    ```ruby
    # Ruby
    user = database.find(email, pwd) # Call function
    puts "Hello #{user.name}" # Print property
    ```

    **Scala**
    ```scala
    // Scala
    val user = database.find(email, pwd) // Call function
    println("Hello " + user.name) // Print property
    ```

    **Swift**
    ```swift
    // Swift
    let result = object.someMethod(arg1, param2:arg2) // Call function
    print("Hello " + name) // Print property
    let user = database.find(email, pass:pwd) // Call function
    print("Hello \(user.name)") // Print property
    ```

    [Back to top](#code-cheat-sheet)

    ### String manipulation
    ## String manipulation

    **Java**
    ```java
    // Java
    int length = name.length(); // Length
    String text = name + " likes " + food; // Concatenate
    String text = name + " is " + age; // Concatenate
    boolean b = name.contains("Smith"); // Find
    b = name.indexOf("Smith") >= 0; // Index
    String first = name.substring(0, 4); // Substring
    String[] parts = text.split(" "); // Split
    ```

    **JavaScript**
    ```javascript
    // Javascript
    var length = name.length(); // Length
    var text = name + " likes " + food; // Concatenate
    var text = name + " is " + age; // Concatenate
    var b = name.indexOf("Smith") >= 0; // Find/Index
    var first = name.substring(0, 4); // Substring
    var parts = text.split(" "); // Split

    ```

    **Objective-C**
    ```objectivec
    // Objective C
    int lenght = [name length]; // Length
    NSString* text = [NSString stringWithFormat:
    @"%@ likes %@", name, food]; // Interpolate
    text = [text stringByAppendingString:@" and drinks"]; // Concatenate
    @"%@ is %@", name, @(age)]; // Interpolate
    text = [text stringByAppendingString:@" years old"]; // Concatenate
    BOOL b = [name rangeOfString:@"Smith"].location != NSNotFound; // Find/Index
    NSString* first = [name substringToIndex:4]; // Substring
    NSString* last = [name substringWithRange:NSMakeRange(5, 10)]; // Substring
    NSArray<NSString*>* parts = [text componentsSeparatedByString:@" "]; // Split
    ```
    **Python**
    ```python
    # Python
    length = len(name) # length
    text = name + " likes " + food # Concatenate
    text = name + " is " + str(age) # Concatenate
    found = "Smith" in name # Find
    found = name.find(" ") >= 0 # Index
    first_name = name[0:index] # Substring
    parts = text.split(" ") # Split
    ```

    **Ruby**
    ```ruby
    # Ruby
    length = name.length # length
    text = "#{name} likes #{food}" # Interpolate
    text += " and " + drink # Concatenate
    text = "#{name} is #{age}" # Interpolate
    text += ", height: " + height # Concatenate
    found = name.include? "Smith" # Find
    index = name.index(" ") # Index (truthy if found)
    first_name = name[0...4] # Substring
    parts = text.split(" ") # Split
    ```

    **Scala**
    ```scala
    // Scala
    var text = s"$name likes $food" // Interpolate
    text += " and " + drink // Concatenate
    var text = s"$name is $age" // Interpolate
    text += ", height: " + height // Concatenate
    var found = name.contains("Smith") // Find
    found = name.indexOf(" ") >= 0 // Index
    val firstName = name.substring(0, 4) // Substring
    val parts = text.split(" ") // Split
    ```

    **Swift**
    ```swift
    // Swift
    int length = name.length // Length
    var text = "\(name) likes \(food)" // Interpolate
    text += " and " + drink // Concatenate
    var text = "\(name) is \(age)" // Interpolate
    text = String(format: "%@ is %@", name, age)
    text += ", height: " + height // Concatenate
    let range = string.rangeOfString("Smith") // Index
    let found = range != nil // Find
    // Try: http://stackoverflow.com/a/24046551/1121497
    @@ -219,34 +222,34 @@ let parts = text.componentsSeparatedByString(" ") // Split

    [Back to top](#code-cheat-sheet)

    ### More to do...
    ## More...

    **Java**
    ```java
    // Java
    ```

    **JavaScript**
    ```javascript
    // Javascript
    ```

    **Objective-C**
    ```objectivec
    // Objective C
    ```

    **Python**
    ```python
    # Python
    ```

    **Ruby**
    ```ruby
    # Ruby
    ```

    **Scala**
    ```scala
    // Scala
    ```

    **Swift**
    ```swift
    // Swift
    ```

    [Back to top](#code-cheat-sheet)
  29. fmaylinch revised this gist Mar 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code cheat sheet.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ Common lines of code in different languages for quick reference and comparison:

    ### Intro

    This document shows common code in different languages (for now, those that I use more frequently). One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.
    This document shows common code constructions in different languages (for now, those that I use more frequently). One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.

    If you see something that could be improved, added or fixed please let me know. I will gladly consider your suggestions.

  30. fmaylinch revised this gist Mar 7, 2016. No changes.