Atomic Kotlin Examples

repository·master·Indexed 43 days ago

https://github.com/bruceeckel/atomickotlinexamples

Automatically extracted code examples and tests from the Atomic Kotlin book. Includes instructions for running examples via IntelliJ IDEA, the command line using Gradle, and the Kotlin REPL.

Tokens
2K
Snippets
10
Records
11
Agent score
6%

What's inside Atomic Kotlin

  1. Overview of Atomic Kotlin Examples

    master

    This repository contains code examples extracted directly from the book Atomic Kotlin. It is designed for developers who want to experiment with the book's code and includes tests to verify the correctness of the examples.

    Note for students: If you intend to solve the exercises provided in the book, it is recommended to install the educational course instead, as it provides both the exercises and the examples in one package.

  2. Use the Kotlin REPL (Read-Evaluate-Print-Loop)

    master

    The REPL provides an interactive environment for immediate Kotlin feedback. To start it, run kotlinc without any arguments.

    Inside the REPL:

    • Use :help to see available commands.
    • Use :quit to exit the REPL.
    • You can execute expressions directly (e.g., arithmetic).
    kotlinc
    
    # Inside REPL
    >>> 42 * 11.3
    474.6
    >>> :quit
  3. Build Examples via Command Line (Quick Version)

    master

    To build and test the examples using the Gradle wrapper, ensure you have Java installed and an active internet connection (required for the first run to download Gradle and dependencies).

    1. Download and unzip the repository.
    2. Open a shell/command window and cd into the root directory (where gradlew and gradlew.bat are located).
    3. Run the tests:

    Mac/Linux:

    ./gradlew test

    If you get a 'Permission denied' error, run chmod +x ./gradlew first.

    Windows:

    gradlew test
    # Mac/Linux
    ./gradlew test
    
    # Windows
    gradlew test
  4. Run individual examples using the `run` script

    master

    Each atom-named subdirectory under the Examples directory contains run.bat (Windows) and run.sh (Mac/Linux) scripts. These scripts provide a convenience method to compile and execute Kotlin examples using Gradle.

    Prerequisites

    • Python 3: You must have a recent version of Python 3 installed to use these scripts.

    Usage

    • Run all examples in the directory: Execute the script without arguments.
    • Run a specific example: Pass the filename of the Kotlin file (e.g., AnExample.kt) as an argument. Note that the file must contain a main() function.

    Troubleshooting

    If you encounter a Permission denied error on macOS or Linux, you must grant execution permissions to the script using chmod.

    # On Mac/Linux, run all examples in the current directory
    ./run.sh
    
    # On Mac/Linux, run a specific example
    ./run.sh AnExample.kt
    
    # On Windows
    run.bat
    
    # Fix permission denied error on Mac/Linux
    chmod +x ./run.sh
  5. Run project tests using Gradle

    master

    The project uses Gradle to manage and run tests. You can run all tests using the test task.

    Note: The test system involves a GenerateTests task that creates TestExamples.java from the repository sources. While you can run these steps separately, running the standard test command is sufficient for validation.

    # Windows
    gradlew test
    
    # Mac/Linux
    ./gradlew test
  6. Install Java (JDK) for Command Line Usage

    master

    Kotlin requires a Java Development Kit (JDK) to run. Follow the instructions for your operating system:

    Windows

    Use Chocolatey to install via a shell prompt:

    choco install jdk8

    (Note: jdk11 or newer is also acceptable).

    Macintosh

    Use Homebrew to install via a shell prompt:

    brew update
    brew cask install java

    Linux

    Use your standard package manager:

    Ubuntu/Debian:

    sudo apt-get update
    sudo apt-get install default-jdk

    Fedora/Redhat:

    su -c "yum install java-1.8.0-openjdk"
    # Windows (via Chocolatey)
    choco install jdk8
    
    # Macintosh (via Homebrew)
    brew update
    brew cask install java
    
    # Ubuntu/Debian
    sudo apt-get update
    sudo apt-get install default-jdk
    
    # Fedora/Redhat
    su -c "yum install java-1.8.0-openjdk"
  7. Compiling and Running Examples in IntelliJ IDEA

    master

    The fastest way to use these examples is via IntelliJ IDEA.

    1. Install IntelliJ IDEA: Follow the official JetBrains installation guide.
    2. Open the Project: Download the zipped repository, unzip it, and in IntelliJ IDEA, select File | Open. Navigate to the unzipped folder and select the build.gradle file. Choose Open as Project.
    3. Locate Code: Open the Examples folder in the Project window. Navigate to a specific atom folder (e.g., HelloWorld) and open the .kt file.
    4. Run: Click the green triangle in the gutter next to fun main() and select Run.

    Note: The first run will take longer as IntelliJ builds the entire project. If you encounter errors regarding a missing JDK, you can install one directly within IntelliJ IDEA.

    File | Open -> build.gradle -> Open as Project
  8. Compile and run Kotlin programs via command line

    master

    To compile a Kotlin source file, use the kotlinc command. The compiler generates a .class file where the name is the original filename with Kt appended. To run the resulting program, use the kotlin command followed by the name of the generated class (without the .class extension).

    If the source file contains a package statement (e.g., package bar), you must include the package name when running the program using dot notation: kotlin package.FileNameKt.

    # Compile
    kotlinc HelloWorld.kt
    
    # Run (no package)
    kotlin HelloWorldKt
    
    # Run (with package 'bar')
    kotlin bar.FooKt
  9. Verify Java Installation

    master

    To ensure the JDK is correctly installed and accessible in your path, open a new shell and run:

    java -version

    You should see output similar to:

    openjdk version "11" 2018-09-25
    OpenJDK Runtime Environment 18.9 (build 11+28)
    OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
    java -version
  10. Essential shell operations reference

    master

    Commonly used shell commands for navigating and managing files across different operating systems.

    # Change directory
    cd <directory>
    cd ..
    pushd <directory>  # Save current dir and move
    popd                # Return to saved dir
    
    # List directory contents
    ls                 # Mac/Linux
    dir                # Windows
    ls *.kt            # List all .kt files
    
    # Create/Remove directories
    mkdir <name>       # Mac/Linux
    md <name>           # Windows
    rm -r <name>       # Mac/Linux (recursive)
    deltree <name>     # Windows
    
    # Remove files
    rm <file>          # Mac/Linux
    del <file>         # Windows
    
    # Command history
    !!                 # Mac/Linux: repeat last command
    !n                  # Mac/Linux: repeat nth command
    # F7               # Windows: view history