diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0024ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Scala template +*.class +*.log + +.gitignore diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/udemyScala/lectures/part1basics/Recursion$.class b/out/production/udemyScala/lectures/part1basics/Recursion$.class index 88b608f..71cf343 100644 Binary files a/out/production/udemyScala/lectures/part1basics/Recursion$.class and b/out/production/udemyScala/lectures/part1basics/Recursion$.class differ diff --git a/src/lectures/part1basics/CalledByValueVsCalledByName.scala b/src/lectures/part1basics/CalledByValueVsCalledByName.scala new file mode 100644 index 0000000..9adb952 --- /dev/null +++ b/src/lectures/part1basics/CalledByValueVsCalledByName.scala @@ -0,0 +1,31 @@ +package lectures.part1basics + +object CalledByValueVsCalledByName extends App { + + def calledByValue(x: Long) : Unit = { //value at time of passing as var + println("by value: " + x) + println("by value: " + x) + + } + + def calledByName(x: => Long):Unit = { //value at time of use in code, easier way to understand is x is a function that returns a long. + println("by name: " + x) + println("by name: " + x) + + } + + calledByValue(System.nanoTime()) + calledByName(System.nanoTime()) + // + + + def infinite(): Int = 1 + infinite() + def printFirst(x: Int, y: => Int): Unit = { + println(x) + } + + + printFirst(34, infinite()) //infinite is never resolved here + printFirst(infinite(), 34) + +} diff --git a/src/lectures/part1basics/DefaultArgs.scala b/src/lectures/part1basics/DefaultArgs.scala new file mode 100644 index 0000000..ff20709 --- /dev/null +++ b/src/lectures/part1basics/DefaultArgs.scala @@ -0,0 +1,31 @@ +package lectures.part1basics + + +import scala.annotation.tailrec + +object DefaultArgs extends App { + + @tailrec //acc standard 1 + def trFact(n: Int, acc: Int = 1): Int = { + if (n<=1) acc + else trFact(n-1, n*acc) + } + + val fact10 = trFact(10) + val fact210 = trFact(10,2) + + + def savePicture(format: String = "jpg", width: Int = 1920, height: Int = 1080): Unit = println("saving picture") + + + + savePicture(width=200,format="bmp") + + + + + + + + +} diff --git a/src/lectures/part1basics/Recursion.scala b/src/lectures/part1basics/Recursion.scala index bde7bd8..6a7e761 100644 --- a/src/lectures/part1basics/Recursion.scala +++ b/src/lectures/part1basics/Recursion.scala @@ -23,7 +23,7 @@ object Recursion extends App { @tailrec def factHelper(x: Int, accumulator: BigInt): BigInt = { if (x <= 1) accumulator - else factHelper(x-1, x * accumulator) + else factHelper(x-1, x * accumulator) //recursion on last line is better for performance } factHelper(n, 1) diff --git a/src/lectures/part1basics/StringOps.scala b/src/lectures/part1basics/StringOps.scala new file mode 100644 index 0000000..dc1d29b --- /dev/null +++ b/src/lectures/part1basics/StringOps.scala @@ -0,0 +1,49 @@ +package lectures.part1basics + +object StringOps extends App { + + val str: String = "Hello, I am learning Scala" + + println(str.charAt(2)) + println(str.substring(7,11)) + println(str.split(" ").toList) + println(str.startsWith("Hello")) + println(str.replace(" ", "_")) + println(str.toLowerCase) + println(str.toUpperCase) + println(str.length) + + + val aNumberString = "45" + val aNumber = aNumberString.toInt + println('a' +: aNumberString :+ 'z') + println(str.reverse) + println(str.take(2)) + + //scala specific string interpolators. + + // s-interpolators + + val name = "David" + val age = 12 + val greeting = s"Hello, my name is $name and i am $age years old" + println(greeting) + + println(s"Hello, my name is $name and i will be turning ${age + 1} years old") + + + // F-interpolators + val speed = 1.2f + val myth = f"$name can eat $speed%2.2f burgers per minute" + + println(myth) + + // raw interpolator + + println(raw"this is a \n newline") + val escaped = "This is a \n newline" + println(raw"$escaped") + + + +}