This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Rename the import to TFConfig | |
| import com.example.config.TraversalFailureConfiguration as TFConfig | |
| // Can now use it anywhere in the file as if TFConfig was it's actual name | |
| fun main(args: Array<String>) { | |
| val config: TFConfig = TFConfig() | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // A sealed class is a special kind of abstract class. The difference being that for a sealed class, all subclasses must | |
| // be declared in the same file. This means that the compiler knows about the fixed set of subclasses. | |
| sealed class Shape { | |
| abstract val area: Double | |
| } | |
| data class Circle(val radius: Double): Shape() { | |
| override val area = Math.PI * radius * radius | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The buildString function simplifies the use of StringBuilder | |
| val s = buildString { | |
| append("Hello") | |
| append(" ") | |
| append("World") | |
| } | |
| // Is equivalent to | |
| val stringBuilder = StringBuilder() | |
| stringBuilder.append("Hello") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write to a file using the `use` method to close the PrintWriter when finished or if an error occurs. | |
| // Semantics are pretty much the same as Java's try-with-resource | |
| File("hello.txt").printWriter().use { out -> | |
| out.println("Hello World") | |
| } | |
| // Which is roughly equivalent to | |
| val file = File("hello.txt") | |
| val outputStreamWriter = OutputStreamWriter(FileOutputStream(file), Charsets.UTF_8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Define a data class to hold the result | |
| data class FoundThings(val x: Int, val y: String, val z: Double) | |
| // Your function returns an instance of the data class | |
| fun findThings(): FoundThings { | |
| // Do lookups | |
| return FoundThings(2, "hello", 4.0) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed abstract class Side | |
| case object Left extends Side | |
| case object Right extends Side | |
| sealed abstract class Thing | |
| case object Cabbage extends Thing | |
| case object Goat extends Thing | |
| case object Wolf extends Thing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "\e[A": history-search-backward | |
| "\e[B": history-search-forward | |
| set show-all-if-ambiguous on | |
| set completion-ignore-case on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| public class NotRandom { | |
| public static void main(String[] args) { | |
| System.out.println(randomString(0xf24ab354)+' '+randomString(0xf72f13ef)); | |
| //Prints "hello world" | |
| } | |
| public static String randomString(int seed) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sort(xs: Array[Int]): Array[Int] = { | |
| if (xs.length <= 1) xs | |
| else { | |
| val pivot = xs(xs.length / 2) | |
| Array.concat( | |
| sort(xs filter (pivot >)), | |
| xs filter (pivot ==), | |
| sort(xs filter (pivot <))) | |
| } | |
| } |