├── basics ├── 05 for loop.scala ├── 07 printing with for loop.scala ├── 09 using two or more increments.scala ├── 04 do while loop.scala ├── 08 yield even numbers with for loop.scala ├── 06 for loop string.scala ├── 02 conditional examples.scala ├── 10 function for printing prime numbers.scala ├── 03 while loop.scala └── 01 syntax.scala └── LICENSE /basics/05 for loop.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | for (i <- 1 to 10) 6 | println(i) 7 | } 8 | } 9 | 10 | // note we're not using curly brackets here -------------------------------------------------------------------------------- /basics/07 printing with for loop.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | val aList = List(1,2,3,4,5) 6 | for(i <- aList){ 7 | println("List items " + i) 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /basics/09 using two or more increments.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | for (i <- 1 to 5; j <- 6 to 10){ 6 | println("i: " + i) 7 | println("j: " + j) 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /basics/04 do while loop.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | // use do while loop when you want to go through the loop at least once 6 | do { 7 | println(i) 8 | i += 1 9 | } while (i <= 20) 10 | } 11 | } -------------------------------------------------------------------------------- /basics/08 yield even numbers with for loop.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | var evenList = for { i <- 1 to 20 6 | if (i % 2) == 0 7 | } yield i 8 | 9 | for(i <- evenList) 10 | println(i) 11 | } 12 | } -------------------------------------------------------------------------------- /basics/06 for loop string.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | val randLetters = "ASBAJHFKJAHSDAKJSDKASDH" 6 | 7 | for(i <- 0 until randLetters.length) 8 | println(randLetters(i)) 9 | } 10 | } 11 | 12 | // note we're not using curly brackets here -------------------------------------------------------------------------------- /basics/02 conditional examples.scala: -------------------------------------------------------------------------------- 1 | var age = 18 2 | 3 | val canVote = if(age >= 18) "yes" else "no" 4 | 5 | if ((age >=5) && (age <=6)) { 6 | println("Go to kindergarten") 7 | } else if ((age > 6) && (age <= 7)) { 8 | println("Go to grade 1") 9 | } else { 10 | println("Go to grade " + (age - 5)) 11 | } 12 | 13 | true || false // returns true -------------------------------------------------------------------------------- /basics/10 function for printing prime numbers.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | def printPrimes(){ 6 | val primeList = List(1,2,3,5,7,11) 7 | for(i <- primeList){ 8 | if(i == 11){ 9 | return 10 | } 11 | 12 | if(i !=1){ 13 | println(i) 14 | } 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /basics/03 while loop.scala: -------------------------------------------------------------------------------- 1 | object ScalaTutorial { 2 | def main(args: Array[String]){ 3 | var i = 0 4 | 5 | while(i <=10){ 6 | println(i) 7 | i += 1 8 | } 9 | } 10 | } 11 | 12 | // 1. curly brackets are optional. For those hardcore java folks out there brackets are a must 13 | // 2. to compile type: 14 | // scalac ScalaTutorial.scala 15 | // or 16 | // scala ScalaTutorial.scala 17 | // it's all the same -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Andrew Stepin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /basics/01 syntax.scala: -------------------------------------------------------------------------------- 1 | 10 + 3 * 5 / 2 2 | 3 | "Your answer" + res0 4 | 5 | var myName = "Andrew" // var can change, not costant 6 | val myAge = 28 // val is constant 7 | 8 | /* Available data types are standard: 9 | 10 | Byte: -128 to 127 11 | Boolean 12 | Char 13 | Short 14 | Int 15 | Long 16 | Float 17 | Double 18 | 19 | Note: Scala can work with very large numbers like longs, floats and doubles 20 | 21 | */ 22 | 23 | // Large prime number: 24 | 25 | val lgprime = BigInt("7835478345983745973495873945736429374284923749234") 26 | 27 | lgprime + 1 28 | 29 | // Big decimal: 30 | 31 | val pi50 = BigDecimal("3.14159265389634563465384593453745973") 32 | 33 | 0.0000000000000000000000000001 + pi50 34 | 35 | var radInt = 100000 36 | 37 | randInt. //click Tab to get methods for it to use with 38 | 39 | // All data types are objects inside Scala 40 | // Scala is great for exploring functional programming w/ object orientation 41 | 42 | import scala.math._ // imports all math libs 43 | 44 | abs(-8) // absolute value 45 | ceil(5.45) 46 | round(5.45) 47 | floor(5.45) 48 | exp(1) 49 | pow(2,2) 50 | sqrt(pow(2,2) + pow(2,2)) 51 | hypot(2, 2) 52 | log10(1000) 53 | 54 | (random * (11 - 1) + 1).toInt // random value 55 | 56 | toRadians(90) 57 | toDegrees(1.5707963267948966) 58 | 59 | // Conditionals: 60 | // : == != > < >= <= 61 | 62 | // Logical operators: 63 | // && || ! (AND, OR and NOT) --------------------------------------------------------------------------------