diff --git a/mobile/kotlin.kt b/mobile/kotlin.kt new file mode 100644 index 0000000..faec72b --- /dev/null +++ b/mobile/kotlin.kt @@ -0,0 +1,265 @@ +// Kotlin is a statically typed programming language for modern multiplatform applications. +// https://kotlinlang.org/ + +// Mutability +var mutableString: String = "Adam" +val immutableString: String = "Adam" +val inferredString = "Adam" + +// Strings +val name = "Adam" +val greeting = "Hello, " + name +val greetingTemplate = "Hello, $name" +val interpolated = "Hello, ${name.toUpperCase()}" + +// Numbers +val intNum = 10 +val doubleNum = 10.0 +val longNum = 10L +val floatNum = 10.0F + +// Booleans +val trueBoolean = true +val falseBoolean = false +val andCondition = trueBoolean && falseBoolean +val orCondition = trueBoolean || falseBoolean + +// Static Fields +class Person { + companion object { + val NAME_KEY = "name_key" + } +} + +val key = Person.NAME_KEY + + +// Nullable properties +val cannotBeNull: String = null // Invalid +val canBeNull: String? = null // Valid + +val cannotBeNull: Int = null // Invalid +val canBeNull: Int? = null // Valid + +// Checking for null +val name: String? = "Adam" + +if (name != null && name.length > 0) { + print("String length is ${name.length}") +} else { + print("String is empty.") +} + +// Safe Operator +val nullableStringLength: Int? = nullableString?.length +val nullableDepartmentHead: String? = person?.department?.head?.name + +// Elvis Operator +val nonNullStringLength: Int = nullableString?.length ?: 0 +val nonNullDepartmentHead: String = person?.department?.head?.name ?: "" +val nonNullDepartmentHead: String = person?.department?.head?.name.orEmpty() + +// Safe Casts +// Will not throw ClassCastException +val nullableCar: Car? = (input as? Car) + +/// Collections + +// Creation +val numArray = arrayOf(1, 2, 3) +val numList = listOf(1, 2, 3) +val mutableNumList = mutableListOf(1, 2, 3) + +// Accessing +val firstItem = numList[0] +val firstItem = numList.first() +val firstItem = numList.firstOrNull() + +// Maps +val faceCards = mutableMapOf("Jack" to 11, "Queen" to 12, "King" to 13) +val jackValue = faceCards["Jack"] // 11 +faceCards["Ace"] = 1 + +// Mutability +val immutableList = listOf(1, 2, 3) +val mutableList = immutableList.toMutableList() + +val immutableMap = mapOf("Jack" to 11, "Queen" to 12, "King" to 13) +val mutableMap = immutableMap.toMutableMap() + +// Iterating +for (item in myList) { + print(item) +} + +myList.forEach { + print(it) +} + +myList.forEachIndexed { index, item -> + print("Item at $index is: $item") +} + +// Filtering & Searching +val evenNumbers = numList.filter { it % 2 == 0 } +val containsEven = numList.any { it % 2 == 0 } +val containsNoEvens = numList.none { it % 2 == 0 } +val containsNoEvens = numList.all { it % 2 == 1 } +val firstEvenNumber: Int = numList.first { it % 2 == 0 } +val firstEvenOrNull: Int? = numList.firstOrNull { it % 2 == 0 } +// Note: it is the implicit name for a single parameter. + +/// Functions + +// Parameters & Return Types +fun printName() { + print("Adam") +} + +fun printName(person: Person) { + print(person.name) +} + +fun getGreeting(person: Person): String { + return "Hello, ${person.name}" +} + +fun getGreeting(person: Person): String = "Hello, ${person.name}" +fun getGreeting(person: Person) = "Hello, ${person.name}" + +// Higher Order Functions +fun callbackIfTrue(condition: Boolean, callback: () -> Unit) { + if (condition) { + callback() + } +} + +callbackIfTrue(someBoolean) { + print("Condition was true") +} + +/ Extension Functions +fun Int.timesTwo(): Int { + return this * 2 +} + +val four = 2.timesTwo() + +// Default Parameters +fun getGreeting(person: Person, intro: String = "Hello,") { + return "$intro ${person.name}" +} + +// Returns "Hello, Adam" +val hello = getGreeting(Person("Adam")) + +// Returns "Welcome, Adam" +val welcome = getGreeting(Person("Adam"), "Welcome,") + +// Named Parameters +class Person(val name: String = "", age: Int = 0) + +// All valid +val person = Person() +val person = Person("Adam", 100) +val person = Person(name = "Adam", age = 100) +val person = Person(age = 100) +val person = Person(age = 100, name = "Adam") + +// Static Functions +class Fragment(val args: Bundle) { + companion object { + fun newInstance(args: Bundle): Fragment { + return Fragment(args) + } + } +} + +val fragment = Fragment.newInstance(args) +// Companion Objects + + +// Classes +// Primary Constructor +class Person(val name: String, val age: Int) +val adam = Person("Adam", 100) + +// Secondary Constructors +class Person(val name: String) { + private var age: Int? = null + + constructor(name: String, age: Int) : this(name) { + this.age = age + } +} + +// Above can be replaced with default params +class Person(val name: String, val age: Int? = null) + +// Inheritance & Implementation +open class Vehicle +class Car : Vehicle() + +interface Runner { + fun run() +} + +class Machine : Runner { + override fun run() { + // ... + } +} +// Control Flow +If Statements +if (someBoolean) { + doThing() +} else { + doOtherThing() +} + +// For Loops +for (i in 0..10) { } // 1 - 10 +for (i in 0 until 10) // 1 - 9 +(0..10).forEach { } +for (i in 0 until 10 step 2) // 0, 2, 4, 6, 8 + +When Statements +when (direction) { + NORTH -> { + print("North") + } + SOUTH -> print("South") + EAST, WEST -> print("East or West") + "N/A" -> print("Unavailable") + else -> print("Invalid Direction") +} +While Loops +while (x > 0) { + x-- +} + +do { + x-- +} while (x > 0) + +// Destructuring Declarations +// Objects & Lists +val person = Person("Adam", 100) +val (name, age) = person + +val pair = Pair(1, 2) +val (first, second) = pair + +val coordinates = arrayOf(1, 2, 3) +val (x, y, z) = coordinates + +// ComponentN Functions +class Person(val name: String, val age: Int) { + operator fun component1(): String { + return name + } + + operator fun component2(): Int { + return age + } +} \ No newline at end of file diff --git a/mobile/swift.swift b/mobile/swift.swift new file mode 100644 index 0000000..8b80645 --- /dev/null +++ b/mobile/swift.swift @@ -0,0 +1,186 @@ +/******************************************************************************************** + * Swift CHEATSHEET + * https://www.swift.org + ********************************************************************************************/ + +// Variables + var myInt = 1 + var myExplicitInt: Int = 1 // explicit type + var x = 1, y = 2, z = 3 // declare multiple integers + myExplicitInt = 2 // set to another integer value + +// Constants + let myInt = 1 + myInt = 2 // compile-time error! + +// Strings + var myString = "a" + let myImmutableString = "c" + myString += "b" // ab + myString = myString + myImmutableString // abc + myImmutableString += "d" // compile-time error! + + let count = 7 + let message = "There are \(count) days in a week" + Logical Operators + var happy = true + var sad = !happy // logical NOT, sad = false + var everyoneHappy = happy && sad // logical AND, everyoneHappy = false + var someoneHappy = happy || sad // logical OR, someoneHappy = true + +// Printing + let name = "swift" + println("Hello") + println("My name is \(name)") + print("See you ") + print("later") + /* Hello + My name is swift + See you later + */ + +// Arrays + var colors = ["red", "blue"] + var moreColors: String[] = ["orange", "purple"] // explicit type + colors.append("green") // [red, blue, green] + colors += "yellow" // [red, blue, green, yellow] + colors += moreColors // [red, blue, green, yellow, orange, purple] + + var days = ["mon", "thu"] + var firstDay = days[0] // mon + days.insert("tue", atIndex: 1) // [mon, tue, thu] + days[2] = "wed" // [mon, tue, wed] + days.removeAtIndex(0) // [tue, wed] + Dictionaries + var days = ["mon": "monday", "tue": "tuseday"] + days["tue"] = "tuesday" // change the value for key "tue" + days["wed"] = "wednesday" // add a new key/value pair + + var moreDays: Dictionary = ["thu": "thursday", "fri": "friday"] + moreDays["thu"] = nil // remove thu from the dictionary + moreDays.removeValueForKey("fri") // remove fri from the dictionary + Conditionals + +// IF STATEMENT + let happy = true + if happy { + println("We're Happy!") + } else { + println("We're Sad :('") +} + +// We're Happy! + + let speed = 28 + if speed <= 0 { + println("Stationary") + } else if speed <= 30 { + println("Safe speed") + } else { + println("Too fast!") + } + +// Safe speed + + +// SWITCH STATEMENT + let n = 2 + switch n { + case 1: + println("It's 1!") + case 2...4: + println("It's between 2 and 4!") + case 5, 6: + println("It's 5 or 6") + default: + println("Its another number!") + } + +// It's between 2 and 4! + For Loops + for var index = 1; index < 3; ++index { + // loops with index taking values 1,2 + } + for index in 1..3 { + // loops with index taking values 1,2 + } + for index in 1...3 { + // loops with index taking values 1,2,3 + } + + let colors = ["red", "blue", "yellow"] + for color in colors { + println("Color: \(color)") + } + // Color: red + // Color: blue + // Color: yellow + + let days = ["mon": "monday", "tue": "tuesday"] + for (shortDay, longDay) in days { + println("\(shortDay) is short for \(longDay)") + } + // mon is short for monday + // tue is short for tuesday + While Loops + var count = 1 + while count < 3 { + println("count is \(count)") + ++count + } + // count is 1 + // count is 2 + + count = 1 + while count < 1 { + println("count is \(count)") + ++count + } + // + + + count = 1 + do { + println("count is \(count)") + ++count + } while count < 3 + // count is 1 + // count is 2 + +count = 1 +do { + println("count is \(count)") + ++count +} while count < 1 + +// count is 1 + +// Functions + func iAdd(a: Int, b: Int) -> Int { + return a + b + } + iAdd(2, 3) // returns 5 + + func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) { + return (n-1, n+1) + } + eitherSide(5) // returns the tuple (4,6) + +// Classes + class Counter { + var count: Int = 0 + func inc() { + count++ + } + func add(n: Int) { + count += n + } + func printCount() { + println("Count: \(count)") + } + } + + var myCount = Counter() + myCount.inc() + myCount.add(2) + myCount.printCount() // Count: 3 \ No newline at end of file