Skip to content

Commit d468734

Browse files
author
Swapan Pramanick
committed
core-scala: initial commit
1 parent e35e842 commit d468734

21 files changed

Lines changed: 567 additions & 0 deletions

core-scala/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apply plugin: 'scala'
2+
3+
repositories {
4+
mavenCentral()
5+
}
6+
7+
dependencies {
8+
compile 'org.scala-lang:scala-library:2.12.6'
9+
testCompile 'org.scalatest:scalatest_2.12:3.0.5'
10+
testCompile 'junit:junit:4.12'
11+
}
12+
13+

core-scala/build.sbt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Dependencies._
2+
3+
lazy val root = (project in file(".")).
4+
settings(
5+
inThisBuild(List(
6+
organization := "com.example",
7+
scalaVersion := "2.12.6",
8+
version := "0.1.0-SNAPSHOT"
9+
)),
10+
name := "core-scala",
11+
libraryDependencies += scalaTest % Test
12+
)

core-scala/pom.xml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<artifactId>core-scala</artifactId>
4+
<version>1.0-SNAPSHOT</version>
5+
<packaging>jar</packaging>
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>parent-modules</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<properties>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<encoding>UTF-8</encoding>
17+
<scala.version>2.12.6</scala.version>
18+
<scala.compat.version>2.12</scala.compat.version>
19+
<spec2.version>4.2.0</spec2.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.scala-lang</groupId>
25+
<artifactId>scala-library</artifactId>
26+
<version>${scala.version}</version>
27+
</dependency>
28+
29+
<!-- Test -->
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>4.12</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.scalatest</groupId>
38+
<artifactId>scalatest_${scala.compat.version}</artifactId>
39+
<version>3.0.5</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.specs2</groupId>
44+
<artifactId>specs2-core_${scala.compat.version}</artifactId>
45+
<version>${spec2.version}</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.specs2</groupId>
50+
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
51+
<version>${spec2.version}</version>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<sourceDirectory>src/main/scala</sourceDirectory>
58+
<testSourceDirectory>src/test/scala</testSourceDirectory>
59+
<plugins>
60+
<plugin>
61+
<!-- see http://davidb.github.com/scala-maven-plugin -->
62+
<groupId>net.alchim31.maven</groupId>
63+
<artifactId>scala-maven-plugin</artifactId>
64+
<version>3.3.2</version>
65+
<executions>
66+
<execution>
67+
<goals>
68+
<goal>compile</goal>
69+
<goal>testCompile</goal>
70+
</goals>
71+
<configuration>
72+
<args>
73+
<arg>-dependencyfile</arg>
74+
<arg>${project.build.directory}/.scala_dependencies</arg>
75+
</args>
76+
</configuration>
77+
</execution>
78+
</executions>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>2.21.0</version>
84+
<configuration>
85+
<!-- Tests will be run with scalatest-maven-plugin instead -->
86+
<skipTests>true</skipTests>
87+
</configuration>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.scalatest</groupId>
91+
<artifactId>scalatest-maven-plugin</artifactId>
92+
<version>2.0.0</version>
93+
<configuration>
94+
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
95+
<junitxml>.</junitxml>
96+
<filereports>TestSuiteReport.txt</filereports>
97+
<!-- Comma separated list of JUnit test class names to execute -->
98+
<jUnitClasses>samples.AppTest</jUnitClasses>
99+
</configuration>
100+
<executions>
101+
<execution>
102+
<id>test</id>
103+
<goals>
104+
<goal>test</goal>
105+
</goals>
106+
</execution>
107+
</executions>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sbt._
2+
3+
object Dependencies {
4+
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.1.6
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.examples
2+
3+
object ClassExamples extends App {
4+
5+
import java.time._
6+
class Customer(val id:Int, val name:String, dob:LocalDate) {
7+
8+
private var _age = calculateAge(dob)
9+
10+
def age = _age // getter
11+
def age_= (age:Int) = _age = age //setter
12+
13+
private def calculateAge(dob:LocalDate) = Period
14+
.between(dob, LocalDate.now)
15+
.getYears
16+
17+
//override def toString: String = s"(id:${id}, name:${name}, age:${age})"
18+
}
19+
val c = new Customer(1, "Varun Sharma", LocalDate.parse("1980-02-27"))
20+
println(c)
21+
22+
case class Address(addressLn1:String, area:String, city:String, zip:String)
23+
24+
val address=Address("102, Raycon Lotus Apartment", "AECS Layout", "Bangalore", "560037")
25+
26+
val anotherAddress = address.copy(addressLn1="41/2, ITPL Road")
27+
28+
println(address)
29+
println(anotherAddress)
30+
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.examples
2+
3+
object CollectionExamples extends App {
4+
5+
val aList = List(5, 3, 9, 2) // create a list
6+
val sortedAscList = aList.sorted // sort ascending
7+
val sortedDescList = aList.sortWith(_ > _) // sort descending
8+
val evenList = aList.filter(_%2 == 0) // filter on some condition
9+
10+
val mySet = Set(10, 20, 5) // create a set
11+
val changedSet = mySet + 50 // add an element
12+
println(changedSet)
13+
val changedSet2 = mySet - 20 // remove an element
14+
println(changedSet2)
15+
16+
val capitals = Map("Japan" -> "Tokyo",
17+
"England" -> "London",
18+
"India" -> "New Delhi") // create a map
19+
val capitalOfJapan = capitals("Japan") // get from map
20+
println(capitalOfJapan)
21+
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.examples
2+
3+
object ExceptionHandlingExamples extends App {
4+
import java.io._
5+
def printLines(filename:String) = {
6+
var reader:Option[BufferedReader] = None
7+
try {
8+
reader = Some(new BufferedReader(new FileReader(new File(filename))))
9+
reader.get.lines().forEach(println(_))
10+
} catch {
11+
case e:FileNotFoundException => println(s"There was no file named ${filename}")
12+
case e:Throwable => throw new Exception("Some error occurred", e)
13+
} finally {
14+
if (reader.isDefined) reader.get.close()
15+
}
16+
}
17+
printLines("test.txt")
18+
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.examples
2+
3+
object FirstClassFunctionExamples extends App {
4+
5+
def sum(x:Double, y:Double, f:Double => Double) = f(x) + f(y)
6+
7+
println(sum(3, 4, x => x*x)) // returns 25.0
8+
9+
println(sum(3, 4, x => x*x*x)) // returns 91.0
10+
11+
println(sum(3, 4, Math.pow(_, 2))) // returns 25.0
12+
13+
def max(first:Int, second:Int) = if (first > second) first else second
14+
15+
println(max(10, -2))
16+
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.examples
2+
3+
object LoopAndCalculationExamples extends App {
4+
import CalculationUtils._
5+
println(sumWithWhile(Array(10, 20, 30)))
6+
println(sumWithFor(Array(10, 20, 30)))
7+
println(checkEvenOdd((1 to 10).toArray).toList)
8+
println(filterEvens((1 to 10).toArray).toList)
9+
}
10+
11+
object CalculationUtils {
12+
13+
def sumWithWhile(values:Array[Int]) = {
14+
var sum = 0
15+
var i = 0
16+
while ( i < values.length) {
17+
sum += values(i)
18+
i+=1
19+
}
20+
sum
21+
}
22+
23+
def sumWithFor(values:Array[Int]) = {
24+
var sum = 0
25+
for (i <- values) {
26+
sum += i
27+
}
28+
sum
29+
}
30+
31+
def checkEvenOdd(values:Array[Int]) = {
32+
for (i <- values) yield if (i%2 == 0) "Even" else "Odd"
33+
}
34+
35+
def filterEvens(values:Array[Int]) =
36+
for (i <- values if i%2 == 0)
37+
yield i
38+
39+
}

0 commit comments

Comments
 (0)