mirror of
https://github.com/bvanroll/scalaUdemy.git
synced 2025-08-28 19:42:48 +00:00
scala udemy
This commit is contained in:
2
.idea/.gitignore
generated
vendored
Normal file
2
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Default ignored files
|
||||
/workspace.xml
|
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/udemyScala.iml" filepath="$PROJECT_DIR$/udemyScala.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
BIN
out/production/udemyScala/exercises/expressionExercises$.class
Normal file
BIN
out/production/udemyScala/exercises/expressionExercises$.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/exercises/expressionExercises.class
Normal file
BIN
out/production/udemyScala/exercises/expressionExercises.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/lectures/part1basics/Expressions.class
Normal file
BIN
out/production/udemyScala/lectures/part1basics/Expressions.class
Normal file
Binary file not shown.
BIN
out/production/udemyScala/lectures/part1basics/Functions$.class
Normal file
BIN
out/production/udemyScala/lectures/part1basics/Functions$.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/lectures/part1basics/Functions.class
Normal file
BIN
out/production/udemyScala/lectures/part1basics/Functions.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/lectures/part1basics/Recursion$.class
Normal file
BIN
out/production/udemyScala/lectures/part1basics/Recursion$.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/lectures/part1basics/Recursion.class
Normal file
BIN
out/production/udemyScala/lectures/part1basics/Recursion.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/playground/JavaPlayground.class
Normal file
BIN
out/production/udemyScala/playground/JavaPlayground.class
Normal file
Binary file not shown.
BIN
out/production/udemyScala/playground/ScalaPlayground$.class
Normal file
BIN
out/production/udemyScala/playground/ScalaPlayground$.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
out/production/udemyScala/playground/ScalaPlayground.class
Normal file
BIN
out/production/udemyScala/playground/ScalaPlayground.class
Normal file
Binary file not shown.
18
src/exercises/expressionExercises.scala
Normal file
18
src/exercises/expressionExercises.scala
Normal file
@@ -0,0 +1,18 @@
|
||||
package exercises
|
||||
|
||||
object expressionExercises extends App {
|
||||
// difference between "hello world" vs println("hello world")?
|
||||
// first is a definition second an expression? aka hello world returns a string type, println returns unity but executes the actual print to the screen
|
||||
|
||||
val someValue = {
|
||||
2 < 3
|
||||
}
|
||||
val someOtherValue = {
|
||||
if (someValue) 239 else 986
|
||||
42
|
||||
}
|
||||
|
||||
//someValue = true
|
||||
//someOtherValue = 42
|
||||
|
||||
}
|
64
src/lectures/part1basics/Expressions.scala
Normal file
64
src/lectures/part1basics/Expressions.scala
Normal file
@@ -0,0 +1,64 @@
|
||||
package lectures.part1basics
|
||||
|
||||
object Expressions extends App {
|
||||
|
||||
val x = 1 + 2 //this is called an expression
|
||||
println(x)
|
||||
println(2+3*4)
|
||||
// + - * / & | ^ << >> >>> (right shift with zero extension)
|
||||
|
||||
println(1 == x)
|
||||
// == != > >= < <=
|
||||
|
||||
println(!(1 == x))
|
||||
// ! && ||
|
||||
|
||||
var aVariable = 2
|
||||
aVariable += 3 // also works with -= *= /= ... side effects
|
||||
println(aVariable)
|
||||
|
||||
// Instructions (DO) vs Expressions (VALUE)
|
||||
|
||||
// IF Expression
|
||||
val aCondition = true
|
||||
val aConditionedValue = if(aCondition) 5 else 3 // IF Expression
|
||||
println(aConditionedValue)
|
||||
println(if(aCondition) 5 else 3)
|
||||
|
||||
var i = 0
|
||||
while (i < 10) {
|
||||
println(i)
|
||||
i += 1
|
||||
}
|
||||
|
||||
// loops are discouraged
|
||||
// iteration will be explained later
|
||||
|
||||
// everything is an expression
|
||||
// only definitions arent
|
||||
|
||||
val aWeirdValue = (aVariable = 3) // Unit === void
|
||||
|
||||
// since everything is an expression everything has a return value. Everything that is useless (side effects) or has a useless return value is called a unit.
|
||||
// this assignemnt for instance returns a unit, i can save that while loop in a var and it will also return a unit
|
||||
|
||||
|
||||
println(aWeirdValue)
|
||||
|
||||
|
||||
// Code Blocks
|
||||
|
||||
val aCodeBlock = {
|
||||
val y = 2
|
||||
val z = y+1
|
||||
if (z > 2) "hello" else "goodbye"
|
||||
}
|
||||
|
||||
//value of a block is the value of teh last expression
|
||||
|
||||
println(aCodeBlock)
|
||||
|
||||
// Instructions are executed (think Java), expressions are evaluated(Scala)
|
||||
|
||||
|
||||
}
|
91
src/lectures/part1basics/Functions.scala
Normal file
91
src/lectures/part1basics/Functions.scala
Normal file
@@ -0,0 +1,91 @@
|
||||
package lectures.part1basics
|
||||
|
||||
object Functions extends App {
|
||||
|
||||
def aFunction(a: String, b: Int) = {
|
||||
a + " " + b
|
||||
}
|
||||
|
||||
|
||||
println(aFunction("hello", 3))
|
||||
def aParameterlessFunction(): Int = 42
|
||||
println(aParameterlessFunction())
|
||||
println(aParameterlessFunction)
|
||||
|
||||
|
||||
def aRepeatedFunction(aString: String, n: Int) : String = {
|
||||
if (n == 1) aString
|
||||
else aString + aRepeatedFunction(aString, n-1)
|
||||
}
|
||||
|
||||
println(aRepeatedFunction("hello", 5))
|
||||
// we loop using recursion
|
||||
|
||||
def aFunctionWithSideEffects(aString: String): Unit = {
|
||||
println(aString)
|
||||
}
|
||||
|
||||
def aBigFunction(n: Int): Int = {
|
||||
def aSmallerFunction(a: Int, b: Int): Int = a + b
|
||||
aSmallerFunction(n, n-1)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// greeting function (name, age) => "Hi, my name is name, and i am age years old"
|
||||
|
||||
def greeting(name: String, age: Int) = "Hi, my name is " + name + ", and i am " + age + " years old"
|
||||
|
||||
greeting("beppe", 22)
|
||||
|
||||
//factorial function 1 * 2 * 3 * .. * n
|
||||
|
||||
def fact(n: Int): Int = {
|
||||
if (n == 1) n
|
||||
else n * fact(n-1)
|
||||
}
|
||||
|
||||
println(fact(5))
|
||||
|
||||
// a fibonacci function f(1) = 1 f(2) = 1
|
||||
|
||||
def feb(n: Int): Int = {
|
||||
if (n <= 2) 1
|
||||
else feb(n-1) + feb(n-2)
|
||||
}
|
||||
|
||||
println(feb(8))
|
||||
|
||||
|
||||
// check Prime
|
||||
|
||||
def isPrime(n: Int) : Boolean = {
|
||||
def checkNum(m: Int) : Int = {
|
||||
if (m <= 1) throw IsPrimeException("Finished, it is prime")
|
||||
if (n % m == 0 && m != 1) throw IsNotPrimeException(m + "")
|
||||
else checkNum(m-1)
|
||||
}
|
||||
try {
|
||||
checkNum(n-1)
|
||||
} catch {
|
||||
case int: IsPrimeException => return true
|
||||
case ex: IsNotPrimeException => return false
|
||||
}
|
||||
true
|
||||
}
|
||||
println(isPrime(1))
|
||||
println(isPrime(5))
|
||||
println(isPrime(4))
|
||||
println(isPrime(3))
|
||||
println(isPrime(7))
|
||||
println(isPrime(37))
|
||||
println(isPrime(2003))
|
||||
println(isPrime(37 * 17))
|
||||
|
||||
|
||||
}
|
||||
|
||||
case class IsPrimeException(value: "Finished, it is prime") extends Throwable
|
||||
|
||||
case class IsNotPrimeException(str: String) extends Throwable
|
83
src/lectures/part1basics/Recursion.scala
Normal file
83
src/lectures/part1basics/Recursion.scala
Normal file
@@ -0,0 +1,83 @@
|
||||
package lectures.part1basics
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Recursion extends App {
|
||||
|
||||
|
||||
def factorial(n: Int): Int ={
|
||||
if (n <= 1) 1
|
||||
else {
|
||||
println("Computing factorial of " + n + " - I first need factorial of " + (n-1))
|
||||
val result = n * factorial(n-1)
|
||||
println("computed factorial of " + n)
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
println(factorial(10))
|
||||
//println(factorial(5000))
|
||||
|
||||
|
||||
def anotherFactorial(n: Int): BigInt = {
|
||||
@tailrec
|
||||
def factHelper(x: Int, accumulator: BigInt): BigInt = {
|
||||
if (x <= 1) accumulator
|
||||
else factHelper(x-1, x * accumulator)
|
||||
}
|
||||
|
||||
factHelper(n, 1)
|
||||
}
|
||||
//tail recursion = use recursive call as last expression, less expensive on stack
|
||||
println(anotherFactorial(10))
|
||||
println(anotherFactorial(5000))
|
||||
|
||||
// when you need loops, use tail recursion
|
||||
|
||||
|
||||
//concat a string n times using tail recursion
|
||||
|
||||
@tailrec
|
||||
def aRepeatedFunction(aString: String, n: Int, accumulator: String) : String = {
|
||||
if (n <= 1) accumulator
|
||||
else aRepeatedFunction(aString, n-1, accumulator + aString)
|
||||
}
|
||||
|
||||
println(aRepeatedFunction("beppe",5,""))
|
||||
|
||||
|
||||
|
||||
//isprime tail recursive
|
||||
|
||||
def isPrime(n: Int) : Boolean = {
|
||||
@tailrec
|
||||
def checkNum(m: Int) : Int = {
|
||||
if (m <= 1) throw IsPrimeException("Finished, it is prime")
|
||||
if (n % m == 0 && m != 1) throw IsNotPrimeException(m + "")
|
||||
else checkNum(m-1)
|
||||
}
|
||||
try {
|
||||
checkNum(n-1)
|
||||
} catch {
|
||||
case int: IsPrimeException => return true
|
||||
case ex: IsNotPrimeException => return false
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
println(isPrime(2003))
|
||||
|
||||
//fibonacci function tail recursive
|
||||
def fib(n: Int): Int = {
|
||||
@tailrec
|
||||
def fibo(i: Int, acc: Int, acc2: Int): Int = {
|
||||
if (i >= n) acc
|
||||
else fibo(i+1, acc + acc2, acc)
|
||||
}
|
||||
if (n <= 2) 1
|
||||
else fibo(2, 1, 1)
|
||||
}
|
||||
println(fib(8))
|
||||
|
||||
|
||||
}
|
29
src/lectures/part1basics/ValuesVariablesTypes.scala
Normal file
29
src/lectures/part1basics/ValuesVariablesTypes.scala
Normal file
@@ -0,0 +1,29 @@
|
||||
package lectures.part1basics
|
||||
|
||||
object ValuesVariablesTypes extends App {
|
||||
|
||||
val x = 42
|
||||
println(x)
|
||||
//VALS ARE IMMUTABLE cant change after set
|
||||
//val types are optional (compiler can infer types)
|
||||
|
||||
val aString: String = "hello"
|
||||
println(aString); val twoString: String = "semicolons are to seperate statements, but not necessary"
|
||||
|
||||
|
||||
val aBoolean: Boolean = true
|
||||
|
||||
var aChar: Char = 'q'
|
||||
val anInt: Int = aChar
|
||||
val aShort: Short = 495
|
||||
val aLong: Long = 394198431432809L
|
||||
val aFloat: Float = 2.02f
|
||||
val aDouble: Double = 3.1415
|
||||
// variables
|
||||
var aVariable: Int = 4
|
||||
aVariable += 2
|
||||
aVariable = 5 //side effects? prolly some functional shit
|
||||
|
||||
//prefer vals over vars
|
||||
|
||||
}
|
9
src/playground/JavaPlayground.java
Normal file
9
src/playground/JavaPlayground.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package playground;
|
||||
|
||||
public class JavaPlayground {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, java!");
|
||||
}
|
||||
|
||||
}
|
5
src/playground/ScalaPlayground.scala
Normal file
5
src/playground/ScalaPlayground.scala
Normal file
@@ -0,0 +1,5 @@
|
||||
package playground
|
||||
|
||||
object ScalaPlayground extends App {
|
||||
println("Hello, Scala!");
|
||||
}
|
12
udemyScala.iml
Normal file
12
udemyScala.iml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="scala-sdk-2.13.1" level="application" />
|
||||
</component>
|
||||
</module>
|
Reference in New Issue
Block a user