Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tboutaour/54e4220216f405b2e6b2bf4809bd9377 to your computer and use it in GitHub Desktop.
Save tboutaour/54e4220216f405b2e6b2bf4809bd9377 to your computer and use it in GitHub Desktop.
Learning project SCALA
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="masterDetails">
<states>
<state key="ScopeChooserConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
<?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>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/rock-the-jvm-scala.iml" filepath="$PROJECT_DIR$/rock-the-jvm-scala.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="26ce2ee7-9b2b-4b47-a85a-ff93d35c6984" name="Default Changelist" comment="" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="CodeStyleSettingsInfer">
<option name="done" value="true" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
<option value="Package Object" />
</list>
</option>
</component>
<component name="ProjectCodeStyleSettingsMigration">
<option name="version" value="1" />
</component>
<component name="ProjectId" id="1RVzG5VNtyaMrPdm0b4ubYP3TUV" />
<component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="settings.github" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager" selected="Application.Recursion">
<configuration name="Expressions" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="lectures.Part1Basics.Expressions" />
<module name="rock-the-jvm-scala" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="lectures.Part1Basics.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Functions" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="lectures.Part1Basics.Functions" />
<module name="rock-the-jvm-scala" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="lectures.Part1Basics.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Recursion" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="lectures.Part1Basics.Recursion" />
<module name="rock-the-jvm-scala" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="lectures.Part1Basics.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ScalaPlayground" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="playground.ScalaPlayground" />
<module name="rock-the-jvm-scala" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="playground.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ValuesVariablesTypes" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="lectures.Part1Basics.ValuesVariablesTypes" />
<module name="rock-the-jvm-scala" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="lectures.Part1Basics.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Recursion" />
<item itemvalue="Application.Functions" />
<item itemvalue="Application.Expressions" />
<item itemvalue="Application.ValuesVariablesTypes" />
<item itemvalue="Application.ScalaPlayground" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="26ce2ee7-9b2b-4b47-a85a-ff93d35c6984" name="Default Changelist" comment="" />
<created>1569766952327</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1569766952327</updated>
</task>
<servers />
</component>
</project>
<?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>
package lectures.Part1Basics
object Expressions extends App {
val x = 1 + 2 //Expression
println(x)
println(2 + 3 * 4)
// + - * / & | ^ << >> >>> (Right shift with zero extension)
println(1 == 4)
// == != < >= < <=
println(!(1 == 4))
// ! && ||
var aVariable = 2
aVariable +=3 //Also works -= *= /=
println(aVariable)
//Instructions vs Expressions
//Instruction: Tell the comp. to do
//Expression: Compute a value
//IF expression: it gives a value.
val aCondition = false
val aConditionValue = if(aCondition) 5 else 3
println(if(aCondition) 5 else 3)
//DON'T DO THIS NEVER
var i = 0
while (i < 10) {
println(i)
i += 1
}
//EVERYTHING in Scala is an Expression!!
val aWeirdValue = (aVariable = 3) //Unit === void
//Side effects are expressions that return Unit!!!
//println(), whiles, reassigning
// Code blocks
//Value of block is value of the last expression
//Take care of visibility of vals
val aCodeBlock = {
val y = 2
val z = y + 1
if(z > 2) "hello" else "goodbye"
}
//1. Difference between "Hello world!" vs println("Hello world!")
//"Hello world!" is a String type
//println("Hello world!") is an expression that return a Unit value.
//2.
val someValue = {
2 < 3
}
//This return true
//3.
val someOtherValue = {
if(someValue) 239 else 986
42
}
//This return 42
}
package lectures.Part1Basics
object Functions extends App {
def aFuction(a: String, b: Int): String =
a + " " + b
/* def aFunction(a: String, b: Int): String = {
a + " " + b
}*/
println(aFuction("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",3))
//WHEN YOU NEED LOOPS, USE RECURSION.
//In recursion we need to define return type of function.
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)
}
/*
1. A greeting function (name, age) => "Hi, my name is $name and I am $age years old"
*/
def greetingFunction(name: String, age:Int): String =
"Hi, my name is " + name + " and I am " + age + " years old."
/*
2. Factorial function
*/
def factorialFunction(number: Int): Int =
if(number <= 0) 1
else number * factorialFunction(number-1)
println(factorialFunction(5))
/*
3. Fibonacci function
*/
def fibonacciFunction(number: Int): Int =
if (number == 1 || number == 2) 1
else fibonacciFunction(number-1) + fibonacciFunction(number-2)
println(fibonacciFunction(6))
/*
4. Test if a number is prime
*/
def isPrime(number: Int): Boolean = {
def isPrimeUntil(t: Int): Boolean =
if (t <= 1) true
else number % t != 0 && isPrimeUntil(t-1)
isPrimeUntil(number / 2)
}
println(isPrime(6))
println(isPrime(37))
println(isPrime(17))
}
package lectures.Part1Basics
import java.security.KeyStore.TrustedCertificateEntry
import scala.collection.convert.StreamExtensions.AccumulatorFactoryInfo
object Recursion extends App {
def factorialFunction(number: Int): Int =
if(number <= 0) 1
else {
println("Computing factorial of " + number + " - I first need factorial of " + (number-1))
val result = number * factorialFunction(number-1)
println("Computed factorial of " + number)
result
}
println(factorialFunction(5))
def anotherFactorial(number: Int): BigInt = {
def factorialHelper(x:Int, accumulator: BigInt):BigInt =
if (x <= 1) accumulator
else factorialHelper(x-1, x * accumulator) //TAIL RECURSION = use recursive call as the LAST expression.
factorialHelper(number,1)
}
//WHEN YOU NEED LOOPS, USE _TAIL_ RECURSION
/*
1. Concatenate a string n times
2. IsPrime function tail recursive
3. Fibonacci function tail recursive
*/
/*
1. Concatenate a string n times.
*/
def stringConcatenator(str: String, times: Int): String = {
def stringConcatenatorHelper(str: String, t: Int, result: String): String =
if (t <= 0) result
else stringConcatenatorHelper(str, t - 1, result + str)
stringConcatenatorHelper(str,times,"")
}
println(stringConcatenator("Hello",5))
def isPrime(number: Int): Boolean = {
def isPrimeUntil(t: Int, isStillPrime: Boolean): Boolean =
if (!isStillPrime) false
else if (t <= 1) true
else isPrimeUntil(t - 1,number % t != 0 && isStillPrime)
isPrimeUntil(number/2, true)
}
def fibonacci(number: Int): Int = {
def fibonacciHelper(n: Int, last: Int, nextToLast: Int): Int =
if (n >= number) last
else fibonacciHelper(n + 1, last + nextToLast, last)
if (number <= 2) 1
else fibonacciHelper(2, 1 ,1)
}
}
package lectures.Part1Basics
object ValuesVariablesTypes extends App {
//Vals
val x: Int = 42
println(x)
//vals are immutable!!!
//Type of vals are optional. Compiler can infer types
val aString: String = "Hello!"
val anotherString: String = "World!"
val aBoolean: Boolean = false
val aChar: Char = 'a'
val anInt: Int = x
val aShort: Short = 444
val aLong: Long = 565465465654L //L at the end to mark that is a Long
val aFloat: Float = 2.654f // f at the end to mark that is a Float
val aDouble: Double = 3.14
//Variables
var aVariable: Int = 4
//Mutable
aVariable = 5 //Side effects
}
package playground;
public class JavaPlayground {
public static void main(String args[]) {
System.out.println("Hello, Java world!");
}
}
package playground
object ScalaPlayground extends App {
println("Hello Scala world!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment