├── .gitignore └── source-code ├── scala-for-beginners ├── project │ └── build.properties ├── build.sbt ├── .gitignore └── src │ ├── main │ ├── scala │ │ └── com │ │ │ └── allaboutscala │ │ │ └── learn │ │ │ └── scala │ │ │ └── seven │ │ │ └── days │ │ │ └── day │ │ │ ├── six │ │ │ └── DonutStore.scala │ │ │ ├── one │ │ │ ├── familiar │ │ │ │ ├── Tutorial_01_HelloWorld.scala │ │ │ │ ├── Tutorial_03_HelloWorldWithMain.scala │ │ │ │ ├── Tutorial_04_DebugHelloWorld.scala │ │ │ │ └── Tutorial_02_HelloWorldWithArguments.scala │ │ │ └── scala │ │ │ │ └── basics │ │ │ │ ├── Tutorial_02_ClassAndTypeHierarchy.scala │ │ │ │ ├── Tutorial_11_Option.scala │ │ │ │ ├── Tutorial_03_TypeInference.scala │ │ │ │ ├── Tutorial_01_OverviewVariablesTypes.scala │ │ │ │ ├── Tutorial_13_JavaWithScala.scala │ │ │ │ ├── Tutorial_06_IfAndElse.scala │ │ │ │ └── Tutorial_04_StringInterpolation.scala │ │ │ ├── two │ │ │ ├── package.scala │ │ │ ├── Tutorial_12_SingletonAndPackageObjects.scala │ │ │ ├── Tutorial_05_PartialFunction.scala │ │ │ └── Tutorial_08_RecursiveFunctions.scala │ │ │ ├── five │ │ │ ├── Tutorial_51_WithFilter.scala │ │ │ ├── Tutorial_03_Drop.scala │ │ │ ├── Tutorial_53_ZipWithIndex.scala │ │ │ ├── Tutorial_43_Take.scala │ │ │ ├── Tutorial_44_TakeRight.scala │ │ │ ├── Tutorial_50_View.scala │ │ │ ├── Tutorial_37_Size.scala │ │ │ ├── Tutorial_32_Reverse.scala │ │ │ ├── Tutorial_49_Unzip3.scala │ │ │ ├── Tutorial_33_ReverseIterator.scala │ │ │ ├── Tutorial_39_SortBy.scala │ │ │ ├── Tutorial_16_IsEmpty.scala │ │ │ ├── Tutorial_25_NonEmpty.scala │ │ │ ├── Tutorial_20_Max.scala │ │ │ ├── Tutorial_01_Collect.scala │ │ │ ├── Tutorial_24_MkString.scala │ │ │ ├── Tutorial_38_Slice.scala │ │ │ ├── Tutorial_42_Tail.scala │ │ │ ├── Tutorial_22_Min.scala │ │ │ ├── Tutorial_46_Transpose.scala │ │ │ ├── Tutorial_45_TakeWhile.scala │ │ │ ├── Tutorial_47_Union.scala │ │ │ ├── Tutorial_04_DropWhile.scala │ │ │ ├── Tutorial_06_FilterFilterNot.scala │ │ │ ├── Tutorial_13_ForEach.scala │ │ │ ├── Tutorial_15_Head.scala │ │ │ ├── Tutorial_18_Last.scala │ │ │ ├── Tutorial_48_Unzip.scala │ │ │ ├── Tutorial_34_Scan.scala │ │ │ ├── Tutorial_17_Intersect.scala │ │ │ ├── Tutorial_35_ScanLeft.scala │ │ │ ├── Tutorial_36_ScanRight.scala │ │ │ ├── Tutorial_19_Map.scala │ │ │ ├── Tutorial_10_Fold.scala │ │ │ ├── Tutorial_07_Find.scala │ │ │ ├── Tutorial_09_Flatten.scala │ │ │ ├── Tutorial_14_GroupBy.scala │ │ │ ├── Tutorial_21_MaxBy.scala │ │ │ ├── Tutorial_23_MinBy.scala │ │ │ ├── Tutorial_08_FlatMap.scala │ │ │ ├── Tutorial_28_Partition.scala │ │ │ ├── Tutorial_40_Sorted.scala │ │ │ ├── Tutorial_02_Diff.scala │ │ │ ├── Tutorial_26_Par.scala │ │ │ ├── Tutorial_11_FoldLeft.scala │ │ │ ├── Tutorial_12_FoldRight.scala │ │ │ └── Tutorial_41_SortWith.scala │ │ │ ├── four │ │ │ ├── Tutorial_09_Immutable_Sequence.scala │ │ │ ├── Tutorial_04_Immutable_Map.scala │ │ │ ├── Tutorial_26_Mutable_Queue.scala │ │ │ ├── Tutorial_22_Mutable_Map.scala │ │ │ ├── Tutorial_03_Immutable_ListMap.scala │ │ │ ├── Tutorial_17_Immutable_Vector.scala │ │ │ ├── Tutorial_20_Mutable_Stack.scala │ │ │ ├── Tutorial_02_Immutable_ListSet.scala │ │ │ ├── Tutorial_15_Immutable_Stack.scala │ │ │ ├── Tutorial_23_Mutable_HashMap.scala │ │ │ ├── Tutorial_24_Mutable_ListMap.scala │ │ │ ├── Tutorial_16_Immutable_Stream.scala │ │ │ └── Tutorial_05_Immutable_HashMap.scala │ │ │ └── three │ │ │ ├── Tutorial_08_TraitFactoryPattern.scala │ │ │ ├── Tutorial_07_EnumsAndADT.scala │ │ │ └── Tutorial_06_TraitMixinAndLinearization.scala │ └── java │ │ └── com │ │ └── allaboutscala │ │ └── learn │ │ └── scala │ │ └── seven │ │ └── days │ │ └── day │ │ └── java │ │ └── DonutJ.java │ └── test │ └── scala │ └── DonutStoreWordSpecStyle.scala ├── multi-project └── donut-store │ ├── project │ └── build.properties │ ├── config │ └── src │ │ ├── main │ │ └── resources │ │ │ └── application.conf │ │ └── test │ │ └── scala │ │ └── com │ │ └── allaboutscala │ │ └── donutstore │ │ └── config │ │ └── DonutStoreConfigTest.scala │ ├── httpServer │ └── src │ │ ├── main │ │ ├── resources │ │ │ ├── welcome.html │ │ │ └── logback.xml │ │ └── scala │ │ │ └── com │ │ │ └── allaboutscala │ │ │ └── donutstore │ │ │ └── httpserver │ │ │ ├── DonutStoreApp.scala │ │ │ └── routes │ │ │ ├── DefaultRoute.scala │ │ │ ├── HttpRoute.scala │ │ │ └── ApiVersionRoute.scala │ │ └── test │ │ └── scala │ │ └── com │ │ └── allaboutscala │ │ └── donutstore │ │ └── httpserver │ │ └── routes │ │ ├── TestBase.scala │ │ ├── DefaultRouteTest.scala │ │ └── ApiVersionRouteTest.scala │ ├── .gitignore │ └── common │ └── src │ └── main │ └── scala │ └── com │ └── allaboutscala │ └── donutstore │ └── common │ ├── Donut.scala │ └── marshalling │ └── JsonSupport.scala └── single-project └── donut-store ├── project └── build.properties ├── src └── main │ ├── resources │ ├── application.conf │ ├── welcome.html │ └── logback.xml │ └── scala │ └── com │ └── allaboutscala │ └── donutstore │ ├── httpserver │ └── DonutStoreApp.scala │ ├── routes │ ├── DefaultRoute.scala │ ├── HttpRoute.scala │ └── ApiVersionRoute.scala │ ├── common │ └── Donut.scala │ └── marshalling │ └── JsonSupport.scala ├── .gitignore └── build.sbt /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 1.2.8 -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 1.2.8 -------------------------------------------------------------------------------- /source-code/single-project/donut-store/project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 1.2.8 -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/resources/application.conf: -------------------------------------------------------------------------------- 1 | donut-store { 2 | app = "donut-store" 3 | http-server { 4 | ip = 127.0.0.1 5 | port = 8080 6 | api-version = 1.0.0.0 7 | } 8 | } -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/config/src/main/resources/application.conf: -------------------------------------------------------------------------------- 1 | donut-store { 2 | app = "donut-store" 3 | http-server { 4 | ip = 127.0.0.1 5 | port = 8080 6 | api-version = 1.0.0.0 7 | } 8 | } -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/resources/welcome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Donut Store Application 4 | 5 | 6 |

Welcome to the Donut Store Application!

7 | 8 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/main/resources/welcome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Donut Store Application 4 | 5 | 6 |

Welcome to the Donut Store Application!

7 | 8 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/build.sbt: -------------------------------------------------------------------------------- 1 | name := "scala-for-beginners" 2 | 3 | version := "0.1" 4 | 5 | scalaVersion := "2.13.0" 6 | 7 | libraryDependencies ++= Seq( 8 | "org.scala-lang.modules" %% "scala-parallel-collections" % "0.2.0", 9 | "org.scalatest" %% "scalatest" % "3.0.8" % Test 10 | ) -------------------------------------------------------------------------------- /source-code/scala-for-beginners/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | 4 | # sbt specific 5 | .cache 6 | .history 7 | .lib/ 8 | dist/* 9 | target/ 10 | lib_managed/ 11 | src_managed/ 12 | project/boot/ 13 | project/plugins/project/ 14 | 15 | # Scala-IDE specific 16 | .scala_dependencies 17 | .worksheet 18 | .idea 19 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | 4 | # sbt specific 5 | .cache 6 | .history 7 | .lib/ 8 | dist/* 9 | target/ 10 | lib_managed/ 11 | src_managed/ 12 | project/boot/ 13 | project/plugins/project/ 14 | 15 | # Scala-IDE specific 16 | .scala_dependencies 17 | .worksheet 18 | .idea 19 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | 4 | # sbt specific 5 | .cache 6 | .history 7 | .lib/ 8 | dist/* 9 | target/ 10 | lib_managed/ 11 | src_managed/ 12 | project/boot/ 13 | project/plugins/project/ 14 | 15 | # Scala-IDE specific 16 | .scala_dependencies 17 | .worksheet 18 | .idea 19 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | /tmp/log/app/donutstore/donutstore.log 4 | 5 | /tmp/log/app/donustore/donutstore.%d{yyyyMMdd}.log 6 | 30 7 | 8 | 9 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %message%n%xException 10 | 11 | 12 | 13 | 14 | 15 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %message%n%xException 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | /tmp/log/app/donutstore/donutstore.log 4 | 5 | /tmp/log/app/donustore/httpserver.%d{yyyyMMdd}.log 6 | 30 7 | 8 | 9 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %message%n%xException 10 | 11 | 12 | 13 | 14 | 15 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %message%n%xException 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/six/DonutStore.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.six 2 | 3 | import scala.concurrent.Future 4 | 5 | class DonutStore { 6 | 7 | def favoriteDonut(): String = "vanilla donut" 8 | 9 | def donuts(): Seq[String] = Seq("vanilla donut", "plain donut", "glazed donut") 10 | 11 | def printName(): Unit = { 12 | throw new IllegalStateException("Some Error") 13 | } 14 | 15 | def donutPrice(donut: String): Option[Double] = { 16 | val prices = Map( 17 | "vanilla donut" -> 2.0, 18 | "plain donut" -> 1.0, 19 | "glazed donut" -> 3.0 20 | ) 21 | val priceOfDonut = prices.get(donut) 22 | priceOfDonut.map { price => price * (1 - discountByDonut(donut)) } 23 | } 24 | 25 | private def discountByDonut(donut: String): Double = { 26 | val discounts = Map( 27 | "vanilla donut" -> 0.2, 28 | "plain donut" -> 0.1, 29 | "glazed donut" -> 0.3 30 | ) 31 | discounts.getOrElse(donut, 0) 32 | } 33 | 34 | import scala.concurrent.ExecutionContext.Implicits.global 35 | def donutSalesTax(donut: String): Future[Double] = Future { 36 | Thread.sleep(3000) // assume an external call to calculate sales tax 37 | 0.15 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/scala/com/allaboutscala/donutstore/httpserver/DonutStoreApp.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 7 | * 8 | * - [[DonutStoreApp]] is the main entry point while the [[DonutStoreHttpServer]] object is a more suitable 9 | * bootstrapping closure to kick-start the Donut Store application. 10 | * 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object DonutStoreApp extends App { 27 | DonutStoreHttpServer.startAndBind() 28 | } 29 | 30 | object DonutStoreHttpServer extends DonutStoreHttpController with DonutStoreServices 31 | 32 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/familiar/Tutorial_01_HelloWorld.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.familiar 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Your first Scala Hello World application 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-1-getting-familiar-intellij-ide/scala-tutorial-first-hello-world-application/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_01_HelloWorld extends App { 28 | 29 | println("Hello World from http://allaboutscala.com") 30 | 31 | } 32 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/main/scala/com/allaboutscala/donutstore/httpserver/DonutStoreApp.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 7 | * 8 | * - [[DonutStoreApp]] is the main entry point while the [[DonutStoreHttpServer]] object is a more suitable 9 | * bootstrapping closure to kick-start the Donut Store application. 10 | * 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object DonutStoreApp extends App { 27 | 28 | DonutStoreHttpServer.startAndBind() 29 | } 30 | 31 | object DonutStoreHttpServer extends DonutStoreHttpController with DonutStoreServices 32 | 33 | 34 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/test/scala/DonutStoreWordSpecStyle.scala: -------------------------------------------------------------------------------- 1 | import org.scalatest.{Matchers, WordSpec} 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: ScalaTest 9 | * 10 | * [[http://allaboutscala.com/scala-cheatsheet/#scalatest-introduction Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | class DonutStoreWordSpecStyle extends WordSpec with Matchers { 27 | 28 | "A DonutStore" can { 29 | "have a dummy boolean test" in { 30 | "DonutStore" === "DonutStore" 31 | } 32 | 33 | "should have a Seq of 2 donut items" in { 34 | Seq("plain donut", "vanilla donut").length shouldEqual 2 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/familiar/Tutorial_03_HelloWorldWithMain.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.familiar 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Where is the main method to launch Scala application 9 | * 10 | * http://allaboutscala.com/tutorials/chapter-1-getting-familiar-intellij-ide/where-is-main-method-launch-scala-application/ 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_03_HelloWorldWithMain { 28 | 29 | def main(args: Array[String]): Unit = { 30 | println("Hello World from http://allaboutscala.com") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/familiar/Tutorial_04_DebugHelloWorld.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.familiar 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: IntelliJ Debug Configuration - Debugging Your Scala Application 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-1-getting-familiar-intellij-ide/intellij-debug-configuration-scala-application/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_04_DebugHelloWorld extends App { 28 | 29 | println("Hello World from http://allaboutscala.com") 30 | 31 | println(args.mkString(", ")) 32 | } 33 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/two/package.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Declare And Use Singleton Object 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-3-beginner-tutorial-using-classes-scala/scala-tutorial-learn-declare-use-singleton-objects/ Tutorial]] 11 | * 12 | * Tutorial: Learn How To Use Package Object 13 | * 14 | * [[http://allaboutscala.com/tutorials/chapter-3-beginner-tutorial-using-classes-scala/scala-package-object/ Tutorial]] 15 | * 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | package object two { 32 | 33 | type DonutCartItem = Tuple2[String, Int] 34 | } 35 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/scala/com/allaboutscala/donutstore/routes/DefaultRoute.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.routes 2 | 3 | import akka.http.scaladsl.server.Directives._ 4 | import akka.http.scaladsl.server._ 5 | import com.allaboutscala.donutstore.config.DonutStoreConfig 6 | import com.allaboutscala.donutstore.data.DataApi 7 | 8 | /** 9 | * Created by Nadim Bahadoor on 15/10/2018. 10 | * 11 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 12 | * 13 | * - This class provides a default welcome message to anyone who accesses the root end point at, say, 14 | * http://localhost:8080 15 | * 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | 32 | class DefaultRoute extends HttpRoute { 33 | override def routes()(implicit config: DonutStoreConfig, dataApi: DataApi): Route = { 34 | path("") { 35 | getFromResource("welcome.html") 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/main/scala/com/allaboutscala/donutstore/httpserver/routes/DefaultRoute.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver.routes 2 | 3 | import akka.http.scaladsl.server.Directives._ 4 | import akka.http.scaladsl.server._ 5 | import com.allaboutscala.donutstore.config.DonutStoreConfig 6 | import com.allaboutscala.donutstore.data.DataApi 7 | 8 | /** 9 | * Created by Nadim Bahadoor on 15/10/2018. 10 | * 11 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 12 | * 13 | * - This class provides a default welcome message to anyone who accesses the root end point at, say, 14 | * http://localhost:8080 15 | * 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | 32 | class DefaultRoute extends HttpRoute { 33 | override def routes()(implicit config: DonutStoreConfig, dataApi: DataApi): Route = { 34 | path("") { 35 | getFromResource("welcome.html") 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/common/src/main/scala/com/allaboutscala/donutstore/common/Donut.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.common 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 7 | * 8 | * - Classes [[Donut]] and [[Donuts]] represent the Abstract Data Type (ADT) for the JSON payload that makes up the 9 | * HTTP GET request at the path http://localhost:8080/donuts. 10 | * 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | 27 | /** A Donut item for the Donut Store application. 28 | * 29 | * @param name the name of the particular donut 30 | * @param ingredients the [[List]] of ingredients that make up this donut 31 | */ 32 | final case class Donut(name: String, ingredients: List[String]) 33 | 34 | 35 | /** A list of Donut items. 36 | * 37 | * @param donuts the root of the JSON payload with a [[Seq]] of [[Donut]] items 38 | */ 39 | final case class Donuts(donuts: Seq[Donut]) -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/scala/com/allaboutscala/donutstore/common/Donut.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.common 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 7 | * 8 | * - Classes [[Donut]] and [[Donuts]] represent the Abstract Data Type (ADT) for the JSON payload that makes up the 9 | * HTTP GET request at the path http://localhost:8080/donuts. 10 | * 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | 27 | /** A Donut item for the Donut Store application. 28 | * 29 | * @param name the name of the particular donut 30 | * @param ingredients the [[List]] of ingredients that make up this donut 31 | */ 32 | final case class Donut(name: String, ingredients: List[String]) 33 | 34 | 35 | /** A list of Donut items. 36 | * 37 | * @param donuts the root of the JSON payload with a [[Seq]] of [[Donut]] items 38 | */ 39 | final case class Donuts(donuts: Seq[Donut]) 40 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/test/scala/com/allaboutscala/donutstore/httpserver/routes/TestBase.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver.routes 2 | 3 | import com.allaboutscala.donutstore.config.{DonutConfig, DonutStoreConfig} 4 | import com.allaboutscala.donutstore.data.{DataApi, DonutDao} 5 | 6 | /** 7 | * Created by Nadim Bahadoor on 15/10/2018. 8 | * 9 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 10 | * 11 | * - A basic placeholder for the shared components used in various test classes. 12 | * 13 | * 14 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 17 | * use this file except in compliance with the License. You may obtain a copy of 18 | * the License at 19 | * 20 | * [http://www.apache.org/licenses/LICENSE-2.0] 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 25 | * License for the specific language governing permissions and limitations under 26 | * the License. 27 | */ 28 | 29 | trait TestBase { 30 | /** Load configuration for DonutStore and lift into the implicit scope **/ 31 | val (cfg, _) = new DonutConfig{}.load().fold(error => sys.error(error.toString), identity) 32 | implicit val donutStoreConfig: DonutStoreConfig = cfg 33 | 34 | /** Load the [[DonutDao]] and lift into the implicit scope **/ 35 | implicit val dataApi: DataApi = new DonutDao() 36 | } 37 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/scala/com/allaboutscala/donutstore/routes/HttpRoute.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.routes 2 | 3 | import akka.http.scaladsl.server.Route 4 | import com.allaboutscala.donutstore.config.DonutStoreConfig 5 | import com.allaboutscala.donutstore.data.{DataApi, DonutDao} 6 | import com.typesafe.scalalogging.LazyLogging 7 | 8 | /** 9 | * Created by Nadim Bahadoor on 15/10/2018. 10 | * 11 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 12 | * 13 | * - This trait acts as a contract for all types that needs to expose a particular Akka HTTP Route that will 14 | * form part of the overall Donut Store API endpoints. 15 | * 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | 32 | trait HttpRoute extends LazyLogging { 33 | 34 | logRoute() 35 | 36 | def routes()(implicit config: DonutStoreConfig, dataApi: DataApi): Route 37 | 38 | def logRoute(): Unit = { 39 | logger.info(s"Loading route for ${getClass.getSimpleName}") 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/main/scala/com/allaboutscala/donutstore/httpserver/routes/HttpRoute.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver.routes 2 | 3 | import akka.http.scaladsl.server._ 4 | import com.allaboutscala.donutstore.config.DonutStoreConfig 5 | import com.allaboutscala.donutstore.data.DataApi 6 | import com.typesafe.scalalogging.LazyLogging 7 | 8 | /** 9 | * Created by Nadim Bahadoor on 15/10/2018. 10 | * 11 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 12 | * 13 | * - This trait acts as a contract for all types that needs to expose a particular Akka HTTP Route that will 14 | * form part of the overall Donut Store API endpoints. 15 | * 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | 32 | trait HttpRoute extends LazyLogging { 33 | 34 | logRoute() 35 | 36 | def routes()(implicit config: DonutStoreConfig, dataApi: DataApi): Route 37 | 38 | def logRoute(): Unit = { 39 | logger.info(s"Loading route for ${getClass.getSimpleName}") 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/familiar/Tutorial_02_HelloWorldWithArguments.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.familiar 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: IntelliJ Run Configuration - Running your Scala application 9 | * 10 | * [http://allaboutscala.com/tutorials/chapter-1-getting-familiar-intellij-ide/intellij-run-configuration-scala-application/ Tutorial 11 | * 12 | * 13 | * Tutorial: Where is the main method to launch Scala application 14 | * 15 | * [[http://allaboutscala.com/tutorials/chapter-1-getting-familiar-intellij-ide/where-is-main-method-launch-scala-application/ Tutorial]] 16 | * 17 | * 18 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 19 | * 20 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 21 | * use this file except in compliance with the License. You may obtain a copy of 22 | * the License at 23 | * 24 | * [http://www.apache.org/licenses/LICENSE-2.0] 25 | * 26 | * Unless required by applicable law or agreed to in writing, software 27 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 28 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 29 | * License for the specific language governing permissions and limitations under 30 | * the License. 31 | */ 32 | object Tutorial_02_HelloWorldWithArguments extends App { 33 | 34 | println("Hello World from http://allaboutscala.com") 35 | 36 | println(args.mkString(", ")) 37 | } 38 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_51_WithFilter.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use WithFilter Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-withfilter-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_51_WithFilter extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = List("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to filter elements using the withFilter function") 35 | donuts 36 | .withFilter(_.charAt(0) == 'P') 37 | .foreach(donut => println(s"Donut starting with letter P = $donut")) 38 | } 39 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/java/com/allaboutscala/learn/scala/seven/days/day/java/DonutJ.java: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.java; 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * 9 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 12 | * use this file except in compliance with the License. You may obtain a copy of 13 | * the License at 14 | * 15 | * [http://www.apache.org/licenses/LICENSE-2.0] 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 20 | * License for the specific language governing permissions and limitations under 21 | * the License. 22 | */ 23 | public class DonutJ { 24 | private String name; 25 | private Long productCode; 26 | 27 | public DonutJ(String name, Long productCode) { 28 | this.name = name; 29 | this.productCode = productCode; 30 | } 31 | 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | 40 | public Long getProductCode() { 41 | return productCode; 42 | } 43 | 44 | public void setProductCode(Long productCode) { 45 | this.productCode = productCode; 46 | } 47 | 48 | public void print() { 49 | System.out.println("Donut name = " + name + ", productCode = " + productCode); 50 | } 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_03_Drop.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Drop Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-drop-function/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_03_Drop extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to drop the first element using drop function") 35 | println(s"Drop the first element in the sequence = ${donuts.drop(1)}") 36 | 37 | 38 | 39 | println("\nStep 3: How to drop the first two elements using the drop function") 40 | println(s"Drop the first and second elements in the sequence = ${donuts.drop(2)}") 41 | } 42 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/scala/com/allaboutscala/donutstore/marshalling/JsonSupport.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.marshalling 2 | 3 | import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport 4 | import com.allaboutscala.donutstore.common.{Donut, Donuts} 5 | import spray.json.RootJsonFormat 6 | 7 | /** 8 | * Created by Nadim Bahadoor on 15/10/2018. 9 | * 10 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 11 | * 12 | * - Trait [[JsonSupport]] acts as a convenient closure for the Abstract Data Type (ADT), namely, [[Donut]] and 13 | * [[Donuts]]. These classes have been lifted into the implicit scope in order to benefit from the 14 | * automatic JSON encoding/decoding that is built-in with Akka HTTP through the use of the 15 | * [[SprayJsonSupport]] trait. 16 | * 17 | * 18 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 19 | * 20 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 21 | * use this file except in compliance with the License. You may obtain a copy of 22 | * the License at 23 | * 24 | * [http://www.apache.org/licenses/LICENSE-2.0] 25 | * 26 | * Unless required by applicable law or agreed to in writing, software 27 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 28 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 29 | * License for the specific language governing permissions and limitations under 30 | * the License. 31 | */ 32 | 33 | trait JsonSupport extends SprayJsonSupport { 34 | 35 | import spray.json.DefaultJsonProtocol._ 36 | 37 | implicit val donutJsonFormat: RootJsonFormat[Donut] = jsonFormat2(Donut) 38 | implicit val donutsJsonFormat: RootJsonFormat[Donuts] = jsonFormat1(Donuts) 39 | } 40 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_53_ZipWithIndex.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use ZipWithIndex Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-zipwithindex-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_53_ZipWithIndex extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to zip the donuts Sequence with their corresponding index using zipWithIndex method") 35 | val zippedDonutsWithIndex: Seq[(String, Int)] = donuts.zipWithIndex 36 | zippedDonutsWithIndex.foreach{ case (donut, index) => 37 | println(s"Donut element = $donut is at index = $index") 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/common/src/main/scala/com/allaboutscala/donutstore/common/marshalling/JsonSupport.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.common.marshalling 2 | 3 | 4 | import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport 5 | import com.allaboutscala.donutstore.common.{Donut, Donuts} 6 | import spray.json.RootJsonFormat 7 | 8 | /** 9 | * Created by Nadim Bahadoor on 15/10/2018. 10 | * 11 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 12 | * 13 | * - Trait [[JsonSupport]] acts as a convenient closure for the Abstract Data Type (ADT), namely, [[Donut]] and 14 | * [[Donuts]]. These classes have been lifted into the implicit scope in order to benefit from the 15 | * automatic JSON encoding/decoding that is built-in with Akka HTTP through the use of the 16 | * [[SprayJsonSupport]] trait. 17 | * 18 | * 19 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 20 | * 21 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 22 | * use this file except in compliance with the License. You may obtain a copy of 23 | * the License at 24 | * 25 | * [http://www.apache.org/licenses/LICENSE-2.0] 26 | * 27 | * Unless required by applicable law or agreed to in writing, software 28 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 29 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 30 | * License for the specific language governing permissions and limitations under 31 | * the License. 32 | */ 33 | 34 | trait JsonSupport extends SprayJsonSupport { 35 | 36 | import spray.json.DefaultJsonProtocol._ 37 | 38 | implicit val donutJsonFormat: RootJsonFormat[Donut] = jsonFormat2(Donut) 39 | implicit val donutsJsonFormat: RootJsonFormat[Donuts] = jsonFormat1(Donuts) 40 | } 41 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_43_Take.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Take Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-take-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_43_Take extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to select elements from the Sequence using the take function") 35 | println(s"Take the first donut element in the Sequence = ${donuts.take(1)}") 36 | println(s"Take the first and second donut elements in the Sequence = ${donuts.take(2)}") 37 | println(s"Take the first, second and third donut elements in the Sequence = ${donuts.take(3)}") 38 | } 39 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_44_TakeRight.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use TakeRight Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-takeright-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_44_TakeRight extends App { 27 | 28 | println("Step 1: How to initialize a List of donuts") 29 | val donuts = List("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to take the last N elements using the takeRight function") 35 | println(s"Take the last donut element in the Sequence = ${donuts.takeRight(1)}") 36 | println(s"Take the last two donut elements in the Sequence = ${donuts.takeRight(2)}") 37 | println(s"Take the last three donut elements in the Sequence = ${donuts.takeRight(3)}") 38 | } 39 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_50_View.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use View Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-view-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_50_View extends App { 27 | 28 | println("Step 1: How to create a large numeric range and take the first 10 odd numbers") 29 | val largeOddNumberList = (1 to 1000000).filter(_ % 2 != 0).take(10).toList 30 | println(s"First 100 odd numbers from largeOddNumberList = $largeOddNumberList") 31 | 32 | 33 | 34 | println(s"\nStep 2: How to lazily create a large numeric range and take the first 10 odd numbers") 35 | val lazyLargeOddNumberList = (1 to 1000000000).view.filter(_ % 2 != 0).take(10).toList 36 | println(s"Lazily take the first 100 odd numbers from lazyLargeOddNumberList = $lazyLargeOddNumberList") 37 | } 38 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_37_Size.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Size Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-size-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_37_Size extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to count the number of elements in the Sequence using size function") 35 | println(s"Size of donuts Sequence = ${donuts.size}") 36 | println(s"Length of donuts Sequence = ${donuts.length}") 37 | 38 | 39 | 40 | println("\nStep 3: How to use the count function") 41 | println(s"Number of times element Plain Donut appear in donuts sequence = ${donuts.count(_ == "Plain Donut")}") 42 | } 43 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_02_ClassAndTypeHierarchy.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn The Scala Class And Type Hierarchy 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-tutorial-learn-scala-class-type-hierarchy/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_02_ClassAndTypeHierarchy extends App { 28 | 29 | println("Step 1: Declare an immutable value of type Any") 30 | val favoriteDonut: Any = "Glazed Donut" 31 | println(s"favoriteDonut of type Any = $favoriteDonut") 32 | 33 | 34 | 35 | println("\nStep 2: Declare an immutable value of type AnyRef") 36 | val donutName: AnyRef = "Glazed Donut" 37 | println(s"donutName of type AnyRef = $donutName") 38 | 39 | 40 | 41 | println("\nStep 3: Declare an immutable value of type AnyVal") 42 | val donutPrice: AnyVal = 2.50 43 | println(s"donutPrice of type AnyVal = $donutPrice") 44 | } 45 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_32_Reverse.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | import scala.collection.immutable.HashMap 4 | 5 | /** 6 | * Created by Nadim Bahadoor on 15/10/2018. 7 | * 8 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 9 | * 10 | * Tutorial: Learn How To Use Reverse Function With Examples 11 | * 12 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-reverse-example/ Tutorial]] 13 | * 14 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 17 | * use this file except in compliance with the License. You may obtain a copy of 18 | * the License at 19 | * 20 | * [http://www.apache.org/licenses/LICENSE-2.0] 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 25 | * License for the specific language governing permissions and limitations under 26 | * the License. 27 | */ 28 | object Tutorial_32_Reverse extends App { 29 | 30 | println("Step 1: How to initialize a Sequence of donuts") 31 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 32 | println(s"Elements of donuts = $donuts") 33 | 34 | 35 | 36 | println("\nStep 2: How to get the elements of the Sequence in reverse using the reverse function") 37 | println(s"Elements of donuts in reversed order = ${donuts.reverse}") 38 | 39 | 40 | 41 | println("\nStep 3: How to access each reversed element using reverse and foreach functions") 42 | donuts.reverse.foreach(donut => println(s"donut = $donut")) 43 | } 44 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_49_Unzip3.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Unzip3 Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-unzip3-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_49_Unzip3 extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of Tuple3 elements") 29 | val donuts: Seq[(String, Double, String)] = Seq(("Plain Donut",1.5,"Tasty"), ("Glazed Donut",2.0,"Very Tasty"), ("Strawberry Donut",2.5,"Very Tasty")) 30 | println(s"Donuts tuple3 elements = $donuts") 31 | 32 | 33 | println("\nStep 2: How to call unzip3 function to unzip Tuple3 elements") 34 | val unzipped: (Seq[String], Seq[Double], Seq[String]) = donuts.unzip3 35 | println(s"Unzipped donut names = ${unzipped._1}") 36 | println(s"Unzipped donut prices = ${unzipped._2}") 37 | println(s"Unzipped donut taste = ${unzipped._3}") 38 | } 39 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/config/src/test/scala/com/allaboutscala/donutstore/config/DonutStoreConfigTest.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.config 2 | 3 | import org.scalatest.{FlatSpec, Matchers} 4 | 5 | /** 6 | * Created by Nadim Bahadoor on 15/10/2018. 7 | * 8 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 9 | * 10 | * - Test class for verifying the corresponding values for the configurations that are defined within 11 | * the application.conf file. 12 | * 13 | * 14 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 17 | * use this file except in compliance with the License. You may obtain a copy of 18 | * the License at 19 | * 20 | * [http://www.apache.org/licenses/LICENSE-2.0] 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 25 | * License for the specific language governing permissions and limitations under 26 | * the License. 27 | */ 28 | 29 | class DonutStoreConfigTest 30 | extends FlatSpec 31 | with Matchers { 32 | 33 | behavior of "DonutStoreConfig" 34 | 35 | /** Load configuration for DonutStore and lift into the implicit scope **/ 36 | val (cfg, _) = new DonutConfig{}.load().fold(error => sys.error(error.toString), identity) 37 | implicit val donutStoreConfig: DonutStoreConfig = cfg 38 | 39 | 40 | "Configurations from application.conf" should "match their expected domain values" in { 41 | donutStoreConfig.app shouldEqual "donut-store" 42 | donutStoreConfig.httpServer.ip shouldEqual "127.0.0.1" 43 | donutStoreConfig.httpServer.port === 8080 44 | donutStoreConfig.httpServer.apiVersion shouldEqual "1.0.0.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/test/scala/com/allaboutscala/donutstore/httpserver/routes/DefaultRouteTest.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver.routes 2 | 3 | 4 | import akka.http.scaladsl.testkit.ScalatestRouteTest 5 | import org.scalatest.{Matchers, WordSpec} 6 | 7 | import scala.io.Source 8 | 9 | /** 10 | * Created by Nadim Bahadoor on 15/10/2018. 11 | * 12 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 13 | * 14 | * - Test class to validate the welcome message that is displayed at the root end point, 15 | * such as, http://localhost:8080. 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | 32 | class DefaultRouteTest 33 | extends WordSpec 34 | with Matchers 35 | with ScalatestRouteTest 36 | with TestBase { 37 | 38 | val defaultRoutes = new DefaultRoute().routes() 39 | 40 | "DonutStore" can { 41 | "have a welcome page at the root end point" in { 42 | Get("/") ~> defaultRoutes ~> check { 43 | responseAs[String] shouldEqual welcomePage() 44 | } 45 | } 46 | } 47 | 48 | private def welcomePage(): String = { 49 | val path = sys.env.getOrElse("WELCOME_PAGE_PATH", "httpServer/src/main/resources/welcome.html") 50 | Source.fromFile(path).mkString 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_33_ReverseIterator.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use ReverseIterator Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-reverseiterator-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_33_ReverseIterator extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to print all elements in reverse order using reverseIterator function") 35 | println(s"Elements of donuts in reversed order = ${donuts.reverseIterator.toList}") 36 | 37 | 38 | 39 | println("\nStep 3: How to iterate through elements using foreach method") 40 | val reverseIterator: Iterator[String] = donuts.reverseIterator 41 | reverseIterator.foreach(donut => println(s"donut = $donut")) 42 | } 43 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_39_SortBy.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use SortBy Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-sortby-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_39_SortBy extends App { 27 | 28 | println("\nStep 1: How to create a case class to represent Donut objects") 29 | case class Donut(name: String, price: Double) 30 | 31 | 32 | 33 | println("\nStep 2: How to create a Sequence of type Donut") 34 | val donuts = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5)) 35 | println(s"Elements of donuts = $donuts") 36 | 37 | 38 | 39 | println("\nStep 3: How to sort a Sequence of case class objects using the sortBy function") 40 | implicit val doubleOrdering = Ordering.Double.TotalOrdering 41 | println(s"Sort a sequence of case class objects of type Donut, sorted by price = ${donuts.sortBy(donut => donut.price)}") 42 | } 43 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_16_IsEmpty.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use IsEmpty Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-isempty-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_16_IsEmpty extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to find out if a sequence is empty using isEmpty function") 35 | println(s"Is donuts sequence empty = ${donuts.isEmpty}") 36 | 37 | 38 | 39 | println("\nStep 3: How to create an empty sequence") 40 | val donuts2 = Seq.empty[String] 41 | println(s"Elements of donuts2 = $donuts2") 42 | 43 | 44 | 45 | println("\nStep 4: How to find out if a sequence is empty using isEmpty function") 46 | println(s"Is donuts2 sequence empty = ${donuts2.isEmpty}") 47 | } 48 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_25_NonEmpty.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use NonEmpty Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-nonempty-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_25_NonEmpty extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to check if a sequence is not empty using nonEmpty function") 35 | println(s"Is donuts sequence NOT empty = ${donuts.nonEmpty}") 36 | 37 | 38 | 39 | println("\nStep 3: How to create an empty Sequence") 40 | val emptyDonuts = Seq.empty[String] 41 | println(s"Elements of emptyDonuts = $emptyDonuts") 42 | 43 | 44 | 45 | println("\nStep 4: How to find out if Sequence is empty using nonEmpty function") 46 | println(s"Is emptyDonuts Sequence empty = ${emptyDonuts.nonEmpty}") 47 | } 48 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_20_Max.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Max Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-max-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_20_Max extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to find the maximum element in the sequence using the max function") 35 | println(s"Max element in the donuts sequence = ${donuts.max}") 36 | 37 | 38 | 39 | println("\nStep 3: How to initialize donut prices") 40 | val prices = Seq(1.50, 2.0, 2.50) 41 | println(s"Elements of prices = $prices") 42 | 43 | 44 | 45 | println("\nStep 4: How to find the maximum element in the sequence using the max function") 46 | println(s"Max element in the donut prices sequence = ${prices.max(Ordering.Double.TotalOrdering)}") 47 | } 48 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_01_Collect.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Collect Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-collect-function/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_01_Collect extends App { 27 | 28 | println("Step 1: How to initialize a Sequence which contains donut names and prices") 29 | val donutNamesandPrices = Seq("Plain Donut", 1.5, "Strawberry Donut", 2.0, "Glazed Donut", 2.5) 30 | println(s"Elements of donutNamesAndPrices = $donutNamesandPrices") 31 | 32 | 33 | 34 | println("\nStep 2: How to use collect function to cherry pick all the donut names") 35 | val donutNames = donutNamesandPrices.collect{ case name: String => name } 36 | println(s"Elements of donutNames = $donutNames") 37 | 38 | 39 | 40 | println("\nStep 3: How to use collect function to cherry pick all the donut prices") 41 | val donutPrices = donutNamesandPrices.collect{ case price: Double => price } 42 | println(s"Elements of donutPrices = $donutPrices") 43 | } 44 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_24_MkString.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use MkString Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-mkstring-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_24_MkString extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to concatenate the elements of a sequence into a String using mkString function") 35 | val donutsAsString = donuts.mkString(" and ") 36 | println(s"Donuts elements using mkString function = $donutsAsString") 37 | 38 | 39 | 40 | println("\nStep 3: How to concatenate the elements of a sequence into a String using mkString and specifying prefix and suffix") 41 | val donutsWithPrefixAndSuffix = donuts.mkString("My favorite donuts namely ", " and ", " are very tasty!") 42 | println(s"$donutsWithPrefixAndSuffix") 43 | } 44 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_38_Slice.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Slice Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-slice-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_38_Slice extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to take a section from the Sequence using the slice function") 35 | println(s"Take elements from the sequence from index 0 to 1 = ${donuts.slice(0,1)}") 36 | println(s"Take elements from the sequence from index 0 to 2 = ${donuts.slice(0,2)}") 37 | println(s"Take elements from the sequence from index 0 to 3 = ${donuts.slice(0,3)}") 38 | 39 | 40 | 41 | println("\nStep 3: How to use slice function where the index is out of range") 42 | println(s"Take elements from the sequence from index 0 to 4 = ${donuts.slice(0,4)}") 43 | } 44 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_42_Tail.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Tail Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-tail-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_42_Tail extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to return all elements in the Sequence except the head using the tail function") 35 | println(s"Elements of donuts excluding the head = ${donuts.tail}") 36 | 37 | 38 | 39 | println("\nStep 3: How to access the last element of the donut Sequence by using the last function") 40 | println(s"Last element of donut Sequence = ${donuts.last}") 41 | 42 | 43 | 44 | println("\nStep 4: How to access the first element of the donut Sequence by using the head function") 45 | println(s"First element of donut Sequence = ${donuts.head}") 46 | } 47 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_22_Min.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Min Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-min-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_22_Min extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to find the minimum element in the sequence using the min function") 35 | println(s"Min element in the donuts sequence = ${donuts.min}") 36 | 37 | 38 | 39 | println("\nStep 3: How to initialize a Sequence of donut prices") 40 | val prices = Seq(1.50, 2.0, 2.50) 41 | println(s"Elements of prices = $prices") 42 | 43 | 44 | 45 | println("\nStep 4: How to find the minimum element in the sequence using the min function") 46 | implicit val doubleOrdering = Ordering.Double.TotalOrdering 47 | println(s"Min element in the donut prices sequence = ${prices.min}") 48 | } 49 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_46_Transpose.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Transpose Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-transpose-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_46_Transpose extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize donut prices") 35 | val prices = Seq(1.50, 2.0, 2.50) 36 | println(s"Elements of prices = $prices") 37 | 38 | 39 | 40 | println("\nStep 3: How to create a List of donuts and prices") 41 | val donutList = List(donuts, prices) 42 | println(s"Sequence of donuts and prices = $donutList") 43 | 44 | 45 | 46 | println("\nStep 4: How to pair each element from both donuts and prices Sequences using the transpose function") 47 | println(s"Transposed list of donuts paired with their individual prices = ${donutList.transpose}") 48 | } 49 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_45_TakeWhile.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use TakeWhile Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-takewhile-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_45_TakeWhile extends App { 27 | 28 | println("Step 1: How to initialize a List of donuts") 29 | val donuts = List("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to take elements from the List using the takeWhile function") 35 | println(s"Take donut elements which start with letter P = ${donuts.takeWhile(_.charAt(0) == 'P')}") 36 | 37 | 38 | 39 | println("\nStep 3: How to declare a predicate function to be passed-through to the takeWhile function") 40 | val takeDonutPredicate: (String) => Boolean = (donutName) => donutName.charAt(0) == 'P' 41 | 42 | 43 | println("\nStep 4: How to take elements using the predicate function from Step 3") 44 | println(s"Take elements using function from Step 3 = ${donuts.takeWhile(takeDonutPredicate)}") 45 | } 46 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_47_Union.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Union Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-union-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_47_Union extends App { 27 | 28 | println("Step 1: How to initialize a Set of donuts") 29 | val donuts1 = Set("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts1 = $donuts1") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize another Set of donuts") 35 | val donuts2 = Set("Plain Donut", "Chocolate Donut", "Vanilla Donut") 36 | println(s"Elements of donuts2 = $donuts2") 37 | 38 | 39 | 40 | println("\nStep 3: How to merge two Sets using union function") 41 | println(s"Union of Sets donuts1 and donuts2 = ${donuts1 union donuts2}") 42 | println(s"Union of Sets donuts2 and donuts1 = ${donuts2 union donuts1}") 43 | 44 | 45 | 46 | println("\nStep 4: How to merge two Sets using ++ function") 47 | println(s"Union of Sets donuts1 and donuts2 = ${donuts1 ++ donuts2}") 48 | println(s"Union of Sets donuts2 and donuts1 = ${donuts2 ++ donuts1}") 49 | } 50 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_04_DropWhile.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use DropWhile Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-dropwhile-function/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_04_DropWhile extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut 1", "Plain Donut 2", "Strawberry Donut", "Plain Donut 3", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to drop elements from the sequence using the dropWhile function") 35 | println(s"Drop donut elements whose name starts with letter P = ${donuts.dropWhile(_.charAt(0) == 'P')}") 36 | 37 | 38 | 39 | println("\nStep 3: How to declare a predicate function to be passed-through to the dropWhile function") 40 | val dropElementsPredicate: String => Boolean = donutName => donutName.charAt(0) == 'P' 41 | 42 | 43 | 44 | println("\nStep 4: How to drop elements using the predicate function from Step 3") 45 | println(s"Drop elements using function from Step 3 = ${donuts.dropWhile(dropElementsPredicate)}") 46 | } 47 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_06_FilterFilterNot.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Filter And FilterNot Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-filter-filternot-function/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_06_FilterFilterNot extends App { 28 | 29 | println("Step 1: How to initialize a Sequence of donuts") 30 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut", "Vanilla Donut") 31 | println(s"Elements of donuts = $donuts") 32 | 33 | 34 | 35 | println("\nStep 2: How to keep only Plain and Glazed Donuts using the filter method") 36 | val sequenceWithPlainAndGlazedDonut = donuts.filter { donutName => 37 | donutName.contains("Plain") || donutName.contains("Glazed") 38 | } 39 | println(s"Sequence with Plain and Glazed donuts only = $sequenceWithPlainAndGlazedDonut") 40 | 41 | 42 | 43 | println("\nStep 3: How to filter out element Vanilla Donut using the filterNot function") 44 | val sequenceWithoutVanillaDonut = donuts.filterNot(donutName => donutName == "Vanilla Donut" ) 45 | println(s"Sequence without vanilla donut = $sequenceWithoutVanillaDonut") 46 | } 47 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/test/scala/com/allaboutscala/donutstore/httpserver/routes/ApiVersionRouteTest.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver.routes 2 | 3 | import akka.http.scaladsl.model._ 4 | import akka.http.scaladsl.testkit.ScalatestRouteTest 5 | import org.scalatest.{FlatSpec, Matchers} 6 | 7 | /** 8 | * Created by Nadim Bahadoor on 15/10/2018. 9 | * 10 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 11 | * 12 | * - Test class for the /api-version route. 13 | * 14 | * 15 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 16 | * 17 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 18 | * use this file except in compliance with the License. You may obtain a copy of 19 | * the License at 20 | * 21 | * [http://www.apache.org/licenses/LICENSE-2.0] 22 | * 23 | * Unless required by applicable law or agreed to in writing, software 24 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 25 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 26 | * License for the specific language governing permissions and limitations under 27 | * the License. 28 | */ 29 | 30 | class ApiVersionRouteTest 31 | extends FlatSpec 32 | with Matchers 33 | with ScalatestRouteTest 34 | with TestBase { 35 | 36 | behavior of "ApiVersionRoute" 37 | 38 | 39 | val apiVersionRoute = new ApiVersionRoute().routes() 40 | 41 | 42 | "The api version route" should "have status code of OK and HTTP code of 200" in { 43 | Get("/api-version?prettyPrint=false") ~> apiVersionRoute ~> check { 44 | status shouldEqual StatusCodes.OK 45 | status.intValue shouldBe 200 46 | } 47 | } 48 | 49 | 50 | 51 | "The api version route content" should "be of JSON type and match the version number" in { 52 | Get("/api-version?prettyPrint=false") ~> apiVersionRoute ~> check { 53 | contentType shouldEqual ContentTypes.`application/json` 54 | val apiRouteExpectedJson = """{"app":"donut-store","version":"1.0.0.0"}""" 55 | entityAs[String] shouldEqual apiRouteExpectedJson 56 | } 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_13_ForEach.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Foreach Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-foreach-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_13_ForEach extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to loop through all the elements in the sequence using the foreach function") 35 | donuts.foreach(println(_)) 36 | 37 | 38 | 39 | println("\nStep 3: How to loop through and access all the elements in the sequence using the foreach function") 40 | donuts.foreach(donutName => println(s"donutName = $donutName")) 41 | 42 | 43 | 44 | println("\nStep 4: How to declare a value function to format a donut names into upper case format") 45 | val uppercase: String => Unit = s => { 46 | val upper = s.toUpperCase 47 | println(upper) 48 | } 49 | 50 | 51 | 52 | println("\nStep 5: How to format all donuts to uppercase using value function from Step 4") 53 | donuts.foreach(uppercase) 54 | } 55 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_15_Head.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Head Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-head-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_15_Head extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to access the first element of the donut sequence") 35 | println(s"First element of donut sequence = ${donuts(0)}") 36 | 37 | 38 | 39 | println("\nStep 3: How to access the first element of the donut sequence using the head method") 40 | println(s"First element of donut sequence using head method = ${donuts.head}") 41 | 42 | 43 | 44 | println("\nStep 4: How to create an empty Sequence") 45 | val donuts2 = Seq.empty[String] 46 | println(s"Elements of donuts2 = $donuts2") 47 | 48 | 49 | 50 | println("\nStep 5: How to access the first element of the donut sequence using the headOption function") 51 | println(s"First element of empty sequence = ${donuts2.headOption.getOrElse("No donut was found!")}") 52 | } 53 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_18_Last.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Last Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-last-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_18_Last extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to access the last element of the donut sequence by index") 35 | println(s"Last element of donut sequence = ${donuts(donuts.size - 1)}") 36 | 37 | 38 | 39 | println("\nStep 3: How to access the last element of the donut sequence by using the last function") 40 | println(s"Last element of donut sequence = ${donuts.last}") 41 | 42 | 43 | 44 | println("\nStep 4: How to create an empty sequence") 45 | val donuts2 = Seq.empty[String] 46 | println(s"Elements of donuts2 = $donuts2") 47 | 48 | 49 | 50 | println("\nStep 5: How to access the last element of the donut sequence using the lastOption function") 51 | println(s"Last element of empty sequence = ${donuts2.lastOption.getOrElse("No donut was found!")}") 52 | } 53 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_48_Unzip.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Unzip Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-unzip-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_48_Unzip extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts = $donuts") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize a Sequence of donut prices") 35 | val donutPrices = Seq(1.5, 2.0, 2.5) 36 | println(s"Elements of donut prices = $donutPrices") 37 | 38 | 39 | 40 | println("\nStep 3: How to zip the donuts Sequence with their corresponding prices") 41 | val zippedDonutsAndPrices: Seq[(String, Double)] = donuts zip donutPrices 42 | println(s"Zipped donuts and prices = $zippedDonutsAndPrices") 43 | 44 | 45 | 46 | println("\nStep 4: How to unzip the zipped donut sequence into separate donuts names and prices Sequences") 47 | val unzipped: (Seq[String], Seq[Double]) = zippedDonutsAndPrices.unzip 48 | println(s"Donut names unzipped = ${unzipped._1}") 49 | println(s"Donut prices unzipped = ${unzipped._2}") 50 | } 51 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_34_Scan.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scan Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-scan-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_34_Scan extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of numbers") 29 | val numbers = (1 to 5).toSeq 30 | println(s"Elements of numbers = ${numbers.mkString(", ")}") 31 | 32 | 33 | 34 | println("\nStep 2: How to create a running total using the scan function") 35 | val runningTotal = numbers.scan(0)(_ + _) 36 | println(s"Running total of all elements in the collection = $runningTotal") 37 | // NOTE:  scan method iterations 38 | // 0 + 1             =   1 39 | // 1 + 2          =   3 40 | // 1 + 2 + 3      =   6 41 | // 1 + 2 + 3 + 4  =   10 42 | // 1 + 2 + 3 + 4 + 5 =   15 43 | 44 | 45 | 46 | println("\nStep 3: How to create a running total using the scan function explicitly") 47 | val runningTotal2 = numbers.scan(0) {case (acc, num) => 48 | val runningTotal = acc + num 49 | println(s"$acc + $num = $runningTotal") 50 | runningTotal 51 | } 52 | println(s"Running total of all elements in the collection = $runningTotal2") 53 | } 54 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_17_Intersect.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Intersect Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-intersect-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_17_Intersect extends App { 27 | 28 | println("Step 1: How to initialize a Set of donuts") 29 | val donuts1 = Set("Plain Donut", "Strawberry Donut", "Glazed Donut") 30 | println(s"Elements of donuts1 = $donuts1") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize another Set of donuts") 35 | val donuts2 = Set("Plain Donut", "Chocolate Donut", "Vanilla Donut") 36 | println(s"Elements of donuts2 = $donuts2") 37 | 38 | 39 | 40 | println("\nStep 3: How to find the common elements between two Sets using intersect function") 41 | println(s"Common elements between donuts1 and donuts2 = ${donuts1 intersect donuts2}") 42 | println(s"Common elements between donuts2 and donuts1 = ${donuts2 intersect donuts1}") 43 | 44 | 45 | 46 | println("\nStep 4: How to find the common elements between two Sets using & function") 47 | println(s"Common elements between donuts1 and donuts2 = ${donuts1 & donuts2}") 48 | println(s"Common elements between donuts2 and donuts1 = ${donuts2 & donuts1}") 49 | } 50 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_35_ScanLeft.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use ScanLeft Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-scanleft-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_35_ScanLeft extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of numbers") 29 | val numbers = (1 to 5).toSeq 30 | println(s"Elements of numbers = ${numbers.mkString(", ")}") 31 | 32 | 33 | 34 | println("\nStep 2: How to create a running total using the scanLeft function") 35 | val runningTotal = numbers.scanLeft(0)(_ + _) 36 | println(s"Running total of all elements in the collection = $runningTotal") 37 | // NOTE: scanLeft method iterations 38 | // 0 + 1            =   1 39 | // 1 + 2          =   3 40 | // 1 + 2 + 3      =   6 41 | // 1 + 2 + 3 + 4  =   10 42 | // 1 + 2 + 3 + 4 + 5 =   15 43 | 44 | 45 | 46 | println("\nStep 3: How to create a running total using the scanLeft function explicitly") 47 | val runningTotal2 = numbers.scanLeft(0) {case (acc, num) => 48 | val runningTotal = acc + num 49 | println(s"$acc + $num = $runningTotal") 50 | runningTotal 51 | } 52 | println(s"Running total of all elements in the collection = $runningTotal2") 53 | } 54 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_11_Option.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Option – Avoid Null 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-tutorial-learn-use-option-avoid-null/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_11_Option extends App { 28 | 29 | 30 | println("Step 1: How to use Option and None - a basic example") 31 | val glazedDonutTaste: Option[String] = Some("Very Tasty") 32 | println(s"Glazed Donut taste = ${glazedDonutTaste.get}") 33 | 34 | 35 | 36 | println("\nStep 2: How to use Option and None - a basic example") 37 | val glazedDonutName: Option[String] = None 38 | println(s"Glazed Donut name = ${glazedDonutName.getOrElse("Glazed Donut")}") 39 | 40 | 41 | 42 | println("\nStep 3: How to use Pattern Matching with Option") 43 | glazedDonutName match { 44 | case Some(name) => println(s"Received donut name = $name") 45 | case None => println(s"No donut name was found!") 46 | } 47 | 48 | 49 | def orderDonut(donutName: Option[String]): Unit = ??? 50 | 51 | 52 | def orderDonut2(donutName: String): Unit = ??? 53 | 54 | 55 | 56 | println("\nTip: Filter None using map function") 57 | glazedDonutTaste.map(taste => println(s"$taste")) 58 | glazedDonutName.map(name => println(s"$name")) 59 | } 60 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_36_ScanRight.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use ScanRight Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-scanright-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_36_ScanRight extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of numbers") 29 | val numbers = (1 to 5).toSeq 30 | println(s"Elements of numbers = ${numbers.mkString(", ")}") 31 | 32 | 33 | 34 | println("\nStep 2: How to create a running total using the scanRight function") 35 | val runningTotal = numbers.scanRight(0)(_ + _) 36 | println(s"Running total of all elements in the collection = $runningTotal") 37 | // NOTE: scanRight method iterations 38 | // 5 + 4 + 3 + 2 + 1 = 15 39 | // 5 + 4 + 3 + 2 = 14 40 | // 5 + 4 + 3 = 12 41 | // 5 + 4 = 9 42 | // 5 + 0 = 5 43 | // 0 = 0 44 | 45 | 46 | 47 | println("\nStep 3: How to create a running total using the scanRight function explicitly") 48 | val runningTotal2 = numbers.scanRight(0){ case (num, acc) => 49 | val runningTotal = acc + num 50 | println(s"$acc + $num = $runningTotal") 51 | runningTotal 52 | } 53 | println(s"Running total of all elements in the collection = $runningTotal2") 54 | } 55 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_09_Immutable_Sequence.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable Sequence 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-sequence/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_09_Immutable_Sequence extends App { 28 | 29 | println("Step 1: How to initialize a Sequence with 3 elements") 30 | val seq1 = Seq("Plain Donut","Strawberry Donut","Chocolate Donut") 31 | println(s"Elements of seq1 = $seq1") 32 | 33 | 34 | 35 | println("\nStep 2: How to access elements in Sequence at specific index") 36 | println(s"Element at index 0 = ${seq1(0)}") 37 | println(s"Element at index 1 = ${seq1(1)}") 38 | println(s"Element at index 2 = ${seq1(2)}") 39 | 40 | 41 | 42 | println("\nStep 3: How to add elements to Sequence using :+") 43 | val seq2 = seq1 :+ "Vanilla Donut" 44 | println(s"Adding elements to Sequence using :+ = $seq2") 45 | 46 | 47 | 48 | println("\nStep 4: How to add two Sequence together using ++") 49 | val seq3 = seq1 ++ Seq("Vanilla Donut", "Glazed Donut") 50 | println(s"Add two sequences together using ++ = $seq3") 51 | 52 | 53 | 54 | println("\nStep 5: How to initialize an empty Sequence") 55 | val emptySeq = Seq.empty[String] 56 | println(s"Empty Sequence = $emptySeq") 57 | } 58 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_19_Map.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Map Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-map-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_19_Map extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts1 = Seq("Plain", "Strawberry", "Glazed") 30 | println(s"Elements of donuts1 = $donuts1") 31 | 32 | 33 | 34 | println("\nStep 2: How to append the word Donut to each element using the map function") 35 | val donuts2 = donuts1.map(_ + " Donut") 36 | println(s"Elements of donuts2 = $donuts2") 37 | 38 | 39 | 40 | println("\nStep 3: How to create a donut sequence with one None element") 41 | val donuts3 = Seq("Plain", "Strawberry", None) 42 | donuts3.foreach(println(_)) 43 | 44 | 45 | 46 | println("\nStep 4: How to filter out the None element using map function") 47 | val donuts4 = donuts3.map { 48 | case donut: String => donut + " Donut" 49 | case None => "Unknown Donut" 50 | } 51 | println(s"Elements of donuts4 = $donuts4") 52 | 53 | 54 | 55 | println("\nStep 5: How to create a Sequence with each Donut length using the map function") 56 | val donutsLengths: Seq[Int] = donuts1.map(_.length) 57 | println(s"Sequence of Donut lengths = $donutsLengths") 58 | } 59 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_10_Fold.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Fold Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-fold-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_10_Fold extends App { 27 | 28 | println("Step 1: How to initialize a sequence of donut prices") 29 | val prices = Seq(1.5, 2.0, 2.5) 30 | println(s"Donut prices = $prices") 31 | 32 | 33 | 34 | println("\nStep 2: How to sum all the donut prices using fold function") 35 | val sum = prices.fold(0.0)(_ + _) 36 | println(s"Sum = $sum") 37 | 38 | 39 | 40 | println("\nStep 3: How to initialize a Sequence of donuts") 41 | val donuts = Seq("Plain", "Strawberry", "Glazed") 42 | println(s"Elements of donuts1 = $donuts") 43 | 44 | 45 | 46 | println("\nStep 4: How to create a String of all donuts using fold function") 47 | println(s"All donuts = ${donuts.fold("")((acc, s) => acc + s + " Donut ")}") 48 | 49 | 50 | 51 | println("\nStep 5: How to declare a value function to create the donut string") 52 | val concatDonuts: (String, String) => String = (acc, s) => acc + s + " Donut " 53 | 54 | 55 | 56 | println("\nStep 6: How to create a String of all donuts using value function from Step 5 and fold function") 57 | println(s"All donuts = ${donuts.fold("")(concatDonuts)}") 58 | } 59 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_07_Find.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | 4 | /** 5 | * Created by Nadim Bahadoor on 15/10/2018. 6 | * 7 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 8 | * 9 | * Tutorial: Learn How To Use Find Function 10 | * 11 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-find-function/ Tutorial]] 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_07_Find extends App { 28 | 29 | println("Step 1: Initialize a Sequence of donuts") 30 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 31 | println(s"Elements of donuts = $donuts") 32 | 33 | 34 | 35 | println("\nStep 2: How to find a particular element in the sequence using the find function") 36 | val plainDonut: Option[String] = donuts.find(donutName => donutName == "Plain Donut") 37 | println(s"Find Plain Donut = ${plainDonut.get}") 38 | 39 | 40 | 41 | println("\nStep 3: How to find element Vanilla Donut which does not exist in the sequence using the find function") 42 | // val vanillaDonut: String = donuts.find(_ == "Vanilla Donut").get 43 | // println(s"Find Vanilla Donuts = $vanillaDonut") 44 | // NOTE: we get an exception Exception in thread "main" java.util.NoSuchElementException: None.get 45 | 46 | 47 | 48 | println("\nStep 4: How to find element Vanilla Donut using the find function and getOrElse") 49 | val vanillaDonut2 = donuts.find(_ == "Vanilla Donut").getOrElse("Vanilla Donut was not found!") 50 | println(s"Find Vanilla Donuts = $vanillaDonut2") 51 | } 52 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/two/Tutorial_12_SingletonAndPackageObjects.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.two 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Declare And Use Singleton Object 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-3-beginner-tutorial-using-classes-scala/scala-tutorial-learn-declare-use-singleton-objects/ Tutorial]] 11 | * 12 | * Tutorial: Learn How To Use Package Object 13 | * 14 | * [[http://allaboutscala.com/tutorials/chapter-3-beginner-tutorial-using-classes-scala/scala-package-object/ Tutorial]] 15 | * 16 | * 17 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 20 | * use this file except in compliance with the License. You may obtain a copy of 21 | * the License at 22 | * 23 | * [http://www.apache.org/licenses/LICENSE-2.0] 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 28 | * License for the specific language governing permissions and limitations under 29 | * the License. 30 | */ 31 | object Tutorial_12_SingletonAndPackageObjects extends App { 32 | 33 | println("Step 1: How to declare a Singleton Object") 34 | object DonutShoppingCartCalculator { 35 | 36 | // Step 2: How to define a global field 37 | val discount = 0.01 38 | 39 | 40 | // Step 3: How to define utility function called calculateTotalCost 41 | def calculateTotalCost(donuts: List[String]): Double = { 42 | println(s"calculateTotalCost for donuts = $donuts") 43 | 1 44 | } 45 | } 46 | 47 | 48 | 49 | println("\nStep 4: How to call global fields and utility methods of Singleton Objects") 50 | println(s"Global discount = ${DonutShoppingCartCalculator.discount}") 51 | println(s"Call to calculateTotalCost function = ${DonutShoppingCartCalculator.calculateTotalCost(List())}") 52 | 53 | 54 | 55 | println("\nStep 5: How to use types define in a Package Object") 56 | val donutItem = new DonutCartItem("Plain Donut", 1) 57 | } 58 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_03_TypeInference.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: An Overview Of Scala Type Inference 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-tutorial-overview-scala-type-inference/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_03_TypeInference extends App { 28 | 29 | println("Step 1: Immutable value") 30 | val donutsToBuy: Int = 5 31 | val donutsBought = 5 32 | 33 | 34 | 35 | println("\nStep 2: Scala Supported Types") 36 | val boolEatingDonut = true 37 | val donutsBoughtToday = 5 38 | val bigNumberOfDonuts = 100000000L 39 | val smallNumberOfDonuts = 1 40 | val priceOfDonut = 2.50 41 | val donutPrice = 2.50f 42 | val donutStoreName = "allaboutscala Donut Store" 43 | val donutByte = 0xa 44 | val donutFirstLetter = 'D' 45 | val nothing = () 46 | 47 | 48 | 49 | println("\nStep 3: Using Scala compiler to convert from one data type to another") 50 | val numberOfDonuts: Short = 1 51 | val minimumDonutsToBuy: Int = numberOfDonuts 52 | 53 | 54 | 55 | println("\nStep 4: Manual conversion from one data type to another ") 56 | // NB: You cannot convert from an Int to a String 57 | // val minimumDonutsToSell: String = numberOfDonuts 58 | 59 | val minimumDonutsToSell: String = numberOfDonuts.toString 60 | val minimumDonutsShort: Short = minimumDonutsToSell.toShort 61 | val minimumDonutsLong: Long = minimumDonutsToSell.toLong 62 | } 63 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_09_Flatten.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Flatten Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-flatten-function-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_09_Flatten extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts1 = Seq("Plain", "Strawberry", "Glazed") 30 | println(s"Elements of donuts1 = $donuts1") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize another Sequence of donuts") 35 | val donuts2 = Seq("Vanilla", "Glazed") 36 | println(s"Elements of donuts2 = $donuts2") 37 | 38 | 39 | 40 | println("\nStep 3: How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2") 41 | val listDonuts: List[Seq[String]] = List(donuts1, donuts2) 42 | println(s"Elements of listDonuts = $listDonuts") 43 | 44 | 45 | 46 | println("\nStep 4: How to return a single list of donut using the flatten function") 47 | val listDonutsFromFlatten: List[String] = listDonuts.flatten 48 | println(s"Elements of listDonutsFromFlatten = $listDonutsFromFlatten") 49 | 50 | 51 | 52 | println("\nStep 5: How to append the word Donut to each element of listDonuts using flatten and map functions") 53 | val listDonutsFromFlatten2: List[String] = listDonuts.flatten.map(_ + " Donut") 54 | println(s"Elements of listDonutsFromFlatten2 = $listDonutsFromFlatten2") 55 | } 56 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_01_OverviewVariablesTypes.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Scala Basic Tutorial – How To Declare Variables And Types 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-basic-tutorial-declare-variables-types/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_01_OverviewVariablesTypes extends App { 28 | 29 | println("Step 1: Immutable values") 30 | val donutsToBuy: Int = 5 31 | 32 | // donutsToBuy = 10 // you will get compile error because donutToBuy is immutable 33 | 34 | 35 | 36 | println("\nStep 2: Mutable variables") 37 | var favoriteDonut: String = "Glazed Donut" 38 | favoriteDonut = "Vanilla Donut" 39 | 40 | 41 | 42 | println("\nStep 3: Declare a variable without a default value") 43 | var leastFavoriteDonut: String = _ 44 | leastFavoriteDonut = "Plain Donut" 45 | 46 | 47 | 48 | println("\nStep 4: lazy initialization") 49 | lazy val donutService = "initialize some donut service" 50 | 51 | 52 | 53 | println("\nStep 5: Scala supported types") 54 | val boolEatingDonut: Boolean = true 55 | val donutsBought: Int = 5 56 | val bigNumberOfDonuts: Long = 100000000L 57 | val smallNumberOfDonuts: Short = 1 58 | val priceOfDonut: Double = 2.50 59 | val donutPrice: Float = 2.50f 60 | val donutStoreName: String = "Allaboutscala Donut Store" 61 | val donutByte: Byte = 0xa 62 | val donutFirstLetter: Char = 'D' 63 | val nothing: Unit = () 64 | } 65 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_13_JavaWithScala.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * 9 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 12 | * use this file except in compliance with the License. You may obtain a copy of 13 | * the License at 14 | * 15 | * [http://www.apache.org/licenses/LICENSE-2.0] 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 20 | * License for the specific language governing permissions and limitations under 21 | * the License. 22 | */ 23 | object Tutorial_13_JavaWithScala extends App { 24 | println("Step 1: Create a Java class in your Scala IntelliJ project") 25 | 26 | 27 | 28 | println("\nStep 2: Create an instance of DonutJ Java class") 29 | import com.allaboutscala.learn.scala.seven.days.day.java._ 30 | val glazedDonutName = "Glazed Donut" 31 | val glazedDonutProductCode = 1111L 32 | val glazedDonut = new DonutJ(glazedDonutName, glazedDonutProductCode) 33 | 34 | 35 | 36 | println("\nStep 3: Access DonutJ Java class get and set methods") 37 | glazedDonut.setProductCode(1010L) 38 | println(s"Glazed Donut product code = ${glazedDonut.getProductCode}") 39 | 40 | 41 | 42 | println("\nStep 4: Call DonutJ Java class print() method") 43 | glazedDonut.print() 44 | 45 | 46 | 47 | println("\nStep 5: Import and use java.util.HashMap") 48 | import java.util.HashMap 49 | val javaHashMap: java.util.HashMap[Long, String] = new HashMap[Long, String]() 50 | javaHashMap.put(glazedDonut.getProductCode, glazedDonut.getName) 51 | println(javaHashMap) 52 | 53 | // import scala.collection.JavaConverters._ // deprecated as of Scala 2.13.0 54 | import scala.jdk.CollectionConverters._ 55 | val scalaMap: scala.collection.mutable.Map[Long, String] = javaHashMap.asScala 56 | println(scalaMap) 57 | 58 | val backToJavaHashMap: java.util.Map[Long, String] = scalaMap.asJava 59 | println(backToJavaHashMap) 60 | } 61 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_14_GroupBy.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use GroupBy Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-groupby-example/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_14_GroupBy extends App { 28 | 29 | println("\nStep 1: How to initialize a Sequence of donuts") 30 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 31 | println(s"Elements of donuts = $donuts") 32 | 33 | 34 | 35 | println("\nStep 2: How to group elements in a sequence using the groupBy function") 36 | val donutsGroup: Map[Char, Seq[String]] = donuts.groupBy(_.charAt(0)) 37 | println(s"Group elements in the donut sequence by the first letter of the donut name = $donutsGroup") 38 | 39 | 40 | 41 | println("\nStep 3: How to create a case class to represent Donut objects") 42 | final case class Donut(name: String, price: Double) 43 | 44 | 45 | 46 | println("\nStep 4: How to create a Sequence of type Donut") 47 | val donuts2 = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5)) 48 | println(s"Elements of donuts2 = $donuts2") 49 | 50 | 51 | 52 | println(s"\nStep 5: How to group case classes donut objects by the name property") 53 | val donutsGroup2: Map[String, Seq[Donut]] = donuts2.groupBy(_.name) 54 | println(s"Group element in the sequence of type Donut grouped by the donut name = $donutsGroup2") 55 | } 56 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_21_MaxBy.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use MaxBy Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-maxby-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_21_MaxBy extends App { 27 | 28 | println("Step 1: How to create a case class to represent Donut object") 29 | case class Donut(name: String, price: Double) 30 | 31 | 32 | 33 | println("\nStep 2: How to create a Sequence of type Donut") 34 | val donuts = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5)) 35 | println(s"Elements of donuts = $donuts") 36 | 37 | 38 | 39 | println("\nStep 3: How to find the maximum element in a sequence of case classes objects using the maxBy function") 40 | implicit val doubleOrdering = Ordering.Double.TotalOrdering 41 | println(s"Maximum element in sequence of case class of type Donut, ordered by price = ${donuts.maxBy(donut => donut.price)}") 42 | 43 | 44 | 45 | println("\nStep 4: How to declare a value predicate function for maxBy function") 46 | val donutsMaxBy: Donut => Double = donut => donut.price 47 | println(s"Value function donutMaxBy = $donutsMaxBy") 48 | 49 | 50 | 51 | println("\nStep 5: How to find the maximum element using maxBy function and pass through the predicate function from Step 4") 52 | println(s"Maximum element in sequence using function from Step 3 = ${donuts.maxBy(donutsMaxBy)}") 53 | } 54 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_23_MinBy.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use MinBy Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-minby-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_23_MinBy extends App { 27 | 28 | println("Step 1: How to create case class to represent Donut object") 29 | case class Donut(name: String, price: Double) 30 | 31 | 32 | 33 | println("\nStep 2: How to create a Sequence of type Donut") 34 | val donuts = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5)) 35 | println(s"Elements of donuts = $donuts") 36 | 37 | 38 | 39 | println("\nStep 3: How to find the minimum element in a sequence of case classes using the minBy function") 40 | implicit val doubleOrdering = Ordering.Double.TotalOrdering 41 | println(s"Minimum element in sequence of case class of type Donut, ordered by price = ${donuts.minBy(donut => donut.price)}") 42 | 43 | 44 | 45 | println("\nStep 4: How to declare a value predicate function for minBy function") 46 | val donutsMinBy: (Donut) => Double = (donut) => donut.price 47 | println(s"Value function donutMinBy = $donutsMinBy") 48 | 49 | 50 | 51 | println("\nStep 5: How to find the minimum element using minBy function and passing through the predicate function from Step 4") 52 | println(s"Minimum element in sequence using function from Step 3 = ${donuts.minBy(donutsMinBy)}") 53 | } 54 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_08_FlatMap.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use FlatMap Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-flatmap-function/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_08_FlatMap extends App { 27 | 28 | println("Step 1: How to initialize a Sequence of donuts") 29 | val donuts1 = Seq("Plain", "Strawberry", "Glazed") 30 | println(s"Elements of donuts1 = $donuts1") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize another Sequence of donuts") 35 | val donuts2 = Seq("Vanilla", "Glazed") 36 | println(s"Elements of donuts2 = $donuts2") 37 | 38 | 39 | 40 | println("\nStep 3: How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2") 41 | val listDonuts: List[Seq[String]] = List(donuts1, donuts2) 42 | println(s"Elements of listDonuts = $listDonuts") 43 | 44 | 45 | 46 | println("\nStep 4: How to append the Donut literal using the map function") 47 | val listDonutsFromMap: List[Seq[String]] = listDonuts.map(donutSeq => donutSeq.map(_ + " Donut")) 48 | println(s"Elements of listDonutsFromMap = $listDonutsFromMap") 49 | 50 | 51 | 52 | println("\nStep 5: How to return a single list of donut using the flatMap function") 53 | val listDonutsFromFlatMap: List[String] = listDonuts.flatMap(donutSeq => donutSeq.map(_ + " Donut")) 54 | println(s"Elements of listDonutsFromFlatMap as a flatMap as a single list = $listDonutsFromFlatMap") 55 | } 56 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_28_Partition.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Partition Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-partition-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_28_Partition extends App { 27 | 28 | println("Step 1: How to initialize a sequence which contains donut names and prices") 29 | val donutNamesAndPrices = Seq("Plain Donut", 1.5, "Strawberry Donut", 2.0, "Glazed Donut", 2.5) 30 | println(s"Elements of donutNamesAndPrices = $donutNamesAndPrices") 31 | 32 | 33 | 34 | println("\nStep 2: How to split the sequence by the element types") 35 | val namesAndPrices = donutNamesAndPrices.partition { 36 | case name: String => true 37 | case price: Double => false 38 | } 39 | println(s"Elements of namesAndPrices = $namesAndPrices") 40 | 41 | 42 | 43 | println("\nStep 3: How to access the donut String Sequence from Step 2") 44 | println(s"Donut names = ${namesAndPrices._1}") 45 | 46 | 47 | 48 | println("\nStep 4: How to access the donut prices Sequence from Step 2") 49 | println(s"Donut prices = ${namesAndPrices._2}") 50 | 51 | 52 | 53 | println("\nStep 5: How to extract the pair returned by partition function") 54 | val (donutNames, donutPrices) = donutNamesAndPrices.partition { 55 | case name: String => true 56 | case _ => false 57 | } 58 | println(s"donutNames = $donutNames") 59 | println(s"donutPrices = $donutPrices") 60 | } 61 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_40_Sorted.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Sorted Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-sorted-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_40_Sorted extends App { 27 | 28 | println("Step 1: How to initialize Donut prices") 29 | val prices = Seq(1.50, 2.0, 2.50) 30 | println(s"Elements of prices = $prices") 31 | 32 | 33 | 34 | println("\nStep 2: How to sort a Sequence of type Double using the sorted function") 35 | implicit val doubleOrdering = Ordering.Double.TotalOrdering 36 | println(s"Sort a sequence of type Double by their natural ordering = ${prices.sorted}") 37 | 38 | 39 | 40 | println("\nStep 3: How to sort a Sequence of type Double in descending order using the sorted function") 41 | object ReverseNumericalOrdering extends Ordering[Double] { 42 | def compare(key1: Double, key2: Double) = key2.compareTo(key1) 43 | } 44 | println(s"Sort a Sequence of type Double in descending order = ${prices.sorted(ReverseNumericalOrdering)}") 45 | 46 | 47 | 48 | println("\nStep 4: How to initialize a Sequence of Donuts") 49 | val donuts = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut") 50 | println(s"Elements of donuts = $donuts") 51 | 52 | 53 | 54 | println("\nStep 5: How to sort a Sequence of type String using the sorted function") 55 | println(s"Sort a sequence of type String by their natural ordering = ${donuts.sorted}") 56 | } 57 | -------------------------------------------------------------------------------- /source-code/single-project/donut-store/build.sbt: -------------------------------------------------------------------------------- 1 | // ARTIFACTS or DEPENDENCIES 2 | lazy val artifacts = new { 3 | 4 | // artifacts versions 5 | val scalaV = "2.13.0" 6 | val donutStoreV = "1.0" 7 | val akkaActorV = "2.5.23" 8 | val akkaHttpV = "10.1.8" 9 | val akkaHttpTestkitV = "10.1.8" 10 | val scalaLoggingV = "3.9.2" 11 | val logbackV = "1.2.3" 12 | val scalaTestV = "3.0.8" 13 | val pureconfigV = "0.11.1" 14 | 15 | // artifacts 16 | val akkaHttp = Seq( 17 | "com.typesafe.akka" %% "akka-actor" % akkaActorV, 18 | "com.typesafe.akka" %% "akka-stream" % akkaActorV, 19 | "com.typesafe.akka" %% "akka-http" % akkaHttpV, 20 | "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpV, 21 | "com.typesafe.akka" %% "akka-testkit" % akkaActorV % "test", 22 | "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpTestkitV % "test" 23 | ) 24 | 25 | val scalaLogging = Seq( 26 | "com.typesafe.scala-logging" %% "scala-logging" % scalaLoggingV, 27 | "ch.qos.logback" % "logback-classic" % logbackV 28 | ) 29 | 30 | val scalatest = Seq("org.scalatest" %% "scalatest" % scalaTestV % "test") 31 | val pureconfig = Seq("com.github.pureconfig" %% "pureconfig" % pureconfigV) 32 | } 33 | 34 | 35 | // SETTINGS 36 | lazy val commonSettings = Seq( 37 | organization := "com.allaboutscala.donutstore", 38 | scalaVersion := artifacts.scalaV, 39 | version := artifacts.donutStoreV, 40 | logLevel := Level.Info, 41 | scalacOptions ++= Seq( 42 | "-encoding", "UTF-8", 43 | "-Xfatal-warnings", 44 | "-deprecation", 45 | "-feature", 46 | "-unchecked", 47 | "-language:implicitConversions", 48 | "-language:higherKinds", 49 | "-language:existentials", 50 | "-language:postfixOps", 51 | "-Ywarn-dead-code" 52 | ) 53 | ) 54 | 55 | lazy val testSettings = Seq( 56 | fork in Test := false, 57 | parallelExecution in Test := false, 58 | libraryDependencies ++= artifacts.scalatest 59 | ) 60 | 61 | 62 | // PROJECT or PROJECTS 63 | lazy val donutStore = (project in file(".")) 64 | .settings(name:= "donut-store") 65 | .settings(commonSettings: _*) 66 | .settings(testSettings: _*) 67 | .settings(libraryDependencies ++= artifacts.scalaLogging ++ artifacts.akkaHttp ++ artifacts.pureconfig) 68 | .settings(resolvers += Resolver.sonatypeRepo("releases")) 69 | .settings(resolvers += Resolver.sonatypeRepo("snapshots")) -------------------------------------------------------------------------------- /source-code/single-project/donut-store/src/main/scala/com/allaboutscala/donutstore/routes/ApiVersionRoute.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.routes 2 | 3 | 4 | import akka.http.scaladsl.model.{ContentTypes, HttpEntity} 5 | import akka.http.scaladsl.server.Directives.{complete, get, path, _} 6 | import akka.http.scaladsl.server.Route 7 | import com.allaboutscala.donutstore.config.DonutStoreConfig 8 | import com.allaboutscala.donutstore.data.DataApi 9 | 10 | import scala.util.Try 11 | 12 | /** 13 | * Created by Nadim Bahadoor on 15/10/2018. 14 | * 15 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 16 | * 17 | * - This class exposes an Akka HTTP route at /api-version. It is typical for other services to rely on, say, this 18 | * specified version, for driving their particular interactions with the Donut Store API or endpoints. 19 | * 20 | * 21 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 22 | * 23 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 24 | * use this file except in compliance with the License. You may obtain a copy of 25 | * the License at 26 | * 27 | * [http://www.apache.org/licenses/LICENSE-2.0] 28 | * 29 | * Unless required by applicable law or agreed to in writing, software 30 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 31 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 32 | * License for the specific language governing permissions and limitations under 33 | * the License. 34 | */ 35 | 36 | class ApiVersionRoute extends HttpRoute { 37 | import spray.json._ 38 | 39 | override def routes()(implicit config: DonutStoreConfig, dataApi: DataApi): Route = { 40 | val apiVersion = 41 | s""" 42 | |{ 43 | | "app": "${config.app}", 44 | | "version": "${config.httpServer.apiVersion}" 45 | |}""".stripMargin 46 | 47 | path("api-version") { 48 | get { 49 | parameter("prettyPrint" ? "true") { prettyPrint => // the prettyPrint parameter is optional and we also default to true 50 | val shouldPrettyPrint = Try(prettyPrint.toBoolean).getOrElse(true) // we'll default to pretty print 51 | val apiVersionOutput = if (shouldPrettyPrint) apiVersion.parseJson.prettyPrint else apiVersion.parseJson.toString 52 | complete(HttpEntity(ContentTypes.`application/json`, apiVersionOutput)) 53 | } 54 | } 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_02_Diff.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Diff Function 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-diff-function/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_02_Diff extends App { 28 | 29 | println("Step 1: How to initialize a Set containing 3 donuts") 30 | val donutBasket1 = Set("Plain Donut", "Strawberry Donut", "Glazed Donut") 31 | println(s"Elements of donutBasket1 = $donutBasket1") 32 | 33 | 34 | 35 | println("\nStep 2: How to initialize a Set containing 2 donuts") 36 | val donutBasket2 = Set("Glazed Donut", "Vanilla Donut") 37 | println(s"Elements of donutBasket2 = $donutBasket2") 38 | 39 | 40 | 41 | println("\nStep 3: How to find the difference between two Sets using the diff function") 42 | val diffDonutBasket1From2 = donutBasket1 diff donutBasket2 43 | println(s"Elements of diffDonutBasket1From2 = $diffDonutBasket1From2") 44 | 45 | 46 | 47 | println("\nStep 4: How to find the difference between two Sets using the diff function") 48 | val diffDonutBasket2From1 = donutBasket2 diff donutBasket1 49 | println(s"Elements of diff DonutBasket2From1 = $diffDonutBasket2From1") 50 | 51 | 52 | 53 | println("\nStep 5: How to find the difference between two Sets using the --") 54 | println(s"Difference between donutBasket1 and donutBasket2 = ${donutBasket1 -- donutBasket2}") 55 | println(s"Difference between donutBasket2 and donutBasket1 = ${donutBasket2 -- donutBasket1}") 56 | } 57 | -------------------------------------------------------------------------------- /source-code/multi-project/donut-store/httpServer/src/main/scala/com/allaboutscala/donutstore/httpserver/routes/ApiVersionRoute.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.donutstore.httpserver.routes 2 | 3 | 4 | import akka.http.scaladsl.model.{ContentTypes, HttpEntity} 5 | import akka.http.scaladsl.server.Directives.{complete, get, path, _} 6 | import akka.http.scaladsl.server._ 7 | import com.allaboutscala.donutstore.config.DonutStoreConfig 8 | import com.allaboutscala.donutstore.data.DataApi 9 | 10 | import scala.util.Try 11 | 12 | /** 13 | * Created by Nadim Bahadoor on 15/10/2018. 14 | * 15 | * The content was inspired by the original tutorials and feedback from readers at http://allaboutscala.com. 16 | * 17 | * - This class exposes an Akka HTTP route at /api-version. It is typical for other services to rely on, say, this 18 | * specified version, for driving their particular interactions with the Donut Store API or endpoints. 19 | * 20 | * 21 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 22 | * 23 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 24 | * use this file except in compliance with the License. You may obtain a copy of 25 | * the License at 26 | * 27 | * [http://www.apache.org/licenses/LICENSE-2.0] 28 | * 29 | * Unless required by applicable law or agreed to in writing, software 30 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 31 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 32 | * License for the specific language governing permissions and limitations under 33 | * the License. 34 | */ 35 | 36 | class ApiVersionRoute extends HttpRoute { 37 | import spray.json._ 38 | 39 | override def routes()(implicit config: DonutStoreConfig, dataApi: DataApi): Route = { 40 | val apiVersion = 41 | s""" 42 | |{ 43 | | "app": "${config.app}", 44 | | "version": "${config.httpServer.apiVersion}" 45 | |}""".stripMargin 46 | 47 | path("api-version") { 48 | get { 49 | parameter("prettyPrint" ? "true") { prettyPrint => // the prettyPrint parameter is optional and we also default to true 50 | val shouldPrettyPrint = Try(prettyPrint.toBoolean).getOrElse(true) // we'll default to pretty print 51 | val apiVersionOutput = if (shouldPrettyPrint) apiVersion.parseJson.prettyPrint else apiVersion.parseJson.toString 52 | complete(HttpEntity(ContentTypes.`application/json`, apiVersionOutput)) 53 | } 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_06_IfAndElse.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: How To Use If Else Statement And Expression 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-tutorial-use-if-else-statement-expression/ Tutorial]]* 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_06_IfAndElse extends App { 28 | 29 | println("Step 1: Using an if clause") 30 | val numberOfPeople = 20 31 | val donutsPerPerson = 2 32 | if (numberOfPeople > 10) 33 | println(s"Number of donuts to buy = ${numberOfPeople * donutsPerPerson}") 34 | 35 | 36 | 37 | println(s"\nStep 2: Using if and else clauses") 38 | val defaultDonutsToBuy = 8 39 | if (numberOfPeople > 10) 40 | println(s"Number of donuts to buy = ${numberOfPeople * donutsPerPerson}") 41 | else 42 | println(s"Number of donuts to buy = $defaultDonutsToBuy") 43 | 44 | 45 | 46 | println("\nStep 3: Using if, else if, and else clauses") 47 | if (numberOfPeople > 10) { 48 | println(s"Number of donuts to buy = ${numberOfPeople * donutsPerPerson}") 49 | } else if (numberOfPeople == 0) { 50 | println("Number of people is zero.") 51 | println("No need to buy donuts.") 52 | } else { 53 | println(s"Number of donuts to buy = $defaultDonutsToBuy") 54 | } 55 | 56 | 57 | 58 | println("\nStep 4: Using if and else clause as expression") 59 | val numberOfDonutsToBuy = if (numberOfPeople > 10) numberOfPeople * donutsPerPerson else defaultDonutsToBuy 60 | println(s"Number of donuts to buy = $numberOfDonutsToBuy") 61 | } 62 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_26_Par.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Par Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-par-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_26_Par extends App { 27 | 28 | println("Step 1: How to initialize an Immutable Sequence of various donut flavours") 29 | val donuts = Seq("Plain", "Strawberry", "Glazed") 30 | println(s"Elements of donuts immutable Sequence = $donuts") 31 | 32 | 33 | println("\nStep 2: Convert the Immutable donuts Sequence into a Parallel Collection") 34 | import scala.collection.parallel.ParSeq 35 | import scala.collection.parallel.CollectionConverters._ 36 | 37 | val donutsParallel: ParSeq[String] = donuts.par 38 | println(s"Elements of donutsParallel = $donutsParallel") 39 | 40 | 41 | println("\nStep 3: How to use a Scala Parallel Collection") 42 | val donutsParSeq: ParSeq[String] = donutsParallel.map(d => s"$d Donut") 43 | println(s"Elements of donutsParSeq parallel collection = $donutsParSeq") 44 | 45 | 46 | println("\nStep 4: Explicitly create a Parallel Collection") 47 | 48 | import scala.collection.parallel.immutable.ParVector 49 | 50 | val donutsParVector = ParVector[Double](1.50, 2.0, 2.50) 51 | println(s"Elements of donutParVector = $donutsParVector") 52 | 53 | 54 | println("\nStep 5: Find the sum of all Donut prices in parallel") 55 | val donutsPricesSum = donutsParVector.fold(0.0)(_ + _) 56 | println(s"The sum of all Donut prices = $donutsPricesSum") 57 | 58 | } 59 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/three/Tutorial_08_TraitFactoryPattern.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.three 2 | 3 | 4 | /** 5 | * Created by Nadim Bahadoor on 15/10/2018. 6 | * 7 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 8 | * 9 | * Tutorial: Traits, Companion Object, Factory Pattern 10 | * 11 | * [[http://allaboutscala.com/tutorials/chapter-5-traits/scala-traits-companion-object-factory-pattern/]] 12 | * 13 | * 14 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 17 | * use this file except in compliance with the License. You may obtain a copy of 18 | * the License at 19 | * 20 | * [http://www.apache.org/licenses/LICENSE-2.0] 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 25 | * License for the specific language governing permissions and limitations under 26 | * the License. 27 | */ 28 | object Tutorial_08_TraitFactoryPattern extends App { 29 | 30 | // Step 1: Define a top-level closure to hold various types of cakes 31 | object Cakes { 32 | // Step 2: Define a base trait to represent a Cake 33 | sealed trait Cake { 34 | def name: String 35 | } 36 | 37 | // Step 3: Define sub-types of Cake 38 | final case class UnknownCake() extends Cake { 39 | override def name: String = "Unknown Cake ... but still delicious!" 40 | } 41 | 42 | final case class Cupcake() extends Cake { 43 | override def name: String = "Cupcake" 44 | } 45 | 46 | final case class Donut() extends Cake { 47 | override def name: String = "Donut" 48 | } 49 | } 50 | 51 | 52 | 53 | // Step 4: The Factory Pattern in action to create various types of cake") 54 | object CakeFactory { 55 | import Cakes._ 56 | def apply(cake: String): Cake = { 57 | cake match { 58 | case "cupcake" => new Cupcake 59 | case "donut" => new Donut 60 | case _ => new UnknownCake 61 | } 62 | } 63 | } 64 | 65 | 66 | // Step 5: Call the CakeFactory 67 | println(s"A cupcake = ${CakeFactory("cupcake").name}") 68 | println(s"A donut = ${CakeFactory("donut").name}") 69 | println(s"Unknown cake = ${CakeFactory("coconut tart").name}") 70 | } 71 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/three/Tutorial_07_EnumsAndADT.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.three 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Create And Use Enumerations (enum) 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/learn-to-create-use-enumerations/ Tutorial]] 11 | * 12 | * Tutorial: Akka Tutorials 13 | * 14 | * [http://allaboutscala.com/scala-frameworks/akka/ Tutorial] 15 | * 16 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 17 | * 18 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 19 | * use this file except in compliance with the License. You may obtain a copy of 20 | * the License at 21 | * 22 | * [http://www.apache.org/licenses/LICENSE-2.0] 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 26 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 27 | * License for the specific language governing permissions and limitations under 28 | * the License. 29 | */ 30 | object Tutorial_07_EnumsAndADT extends App { 31 | 32 | println("Step 1: How to create and use an enumeration") 33 | object Donut extends Enumeration { 34 | type Donut = Value 35 | 36 | val Glazed = Value("Glazed") 37 | val Strawberry = Value("Strawberry") 38 | val Plain = Value("Plain") 39 | val Vanilla = Value("Vanilla") 40 | } 41 | 42 | println(s"Vanilla Donut string value = ${Donut.Vanilla}") 43 | println(s"Vanilla Donut's id = ${Donut.Vanilla.id}") 44 | 45 | 46 | 47 | println("\nStep 2: Defining Algebraic Data Types using sealed traits") 48 | sealed trait BakingEvents 49 | final case object BakeDonut extends BakingEvents 50 | final case object AddTopping extends BakingEvents 51 | final case object StopBaking extends BakingEvents 52 | 53 | 54 | def prettyPrintBakingEvent(bakingEvent: BakingEvents): Unit = bakingEvent match { 55 | case BakeDonut => println("BakeDonut event was used in Pattern Matching") 56 | case AddTopping => println("AddTopping event was used in Pattern Matching") 57 | case StopBaking => println("StopBaking event was used in Pattern Matching") 58 | } 59 | 60 | prettyPrintBakingEvent(BakeDonut) 61 | } 62 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_04_Immutable_Map.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable Map 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-map/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_04_Immutable_Map extends App { 27 | 28 | println("Step 1: How to initialize a Map with 3 elements") 29 | val map1 = Map(("PD", "Plain Donut"), ("SD", "Strawberry Donut"), ("CD", "Chocolate Donut")) 30 | println(s"Elements of map1 = $map1") 31 | 32 | 33 | 34 | println("\nStep 2: How to initialize Map using key -> value notation") 35 | val map2 = Map("VD" -> "Vanilla Donut", "GD" -> "Glazed Donut") 36 | println(s"Elements of map1 = $map2") 37 | 38 | 39 | 40 | println("\nStep 3: How to access elements by specific key") 41 | println(s"Element by key VD = ${map2("VD")}") 42 | println(s"Element by key GD = ${map2("GD")}") 43 | 44 | 45 | 46 | println("\nStep 4: How to add elements using +") 47 | val map3 = map1 + ("KD" -> "Krispy Kreme Donut") 48 | println(s"Element in map3 = $map3") 49 | 50 | 51 | 52 | println("\nStep 5: How to add two Maps together using ++") 53 | val map4 = map1 ++ map2 54 | println(s"Elements in map4 = $map4") 55 | 56 | 57 | 58 | println("\nStep 6: How to remove key and its value from map using -") 59 | val map5 = map4 - "CD" 60 | println(s"Map without the key CD and its value = $map5") 61 | 62 | 63 | 64 | println("\nStep 7: How to initialize an empty Map") 65 | val emptyMap = Map.empty[String, String] 66 | println(s"Empty Map = $emptyMap") 67 | } -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_26_Mutable_Queue.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Mutable Queue 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-7-beginner-tutorial-using-scala-mutable-collection/scala-tutorial-learn-use-mutable-queue/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_26_Mutable_Queue extends App { 28 | 29 | import scala.collection.mutable.Queue 30 | println("\nStep 1: How to initialize a Queue with 3 elements") 31 | val queue1 = Queue("Plain Donut", "Strawberry Donut", "Chocolate Donut") 32 | println(s"Elements of queue1 = $queue1") 33 | 34 | 35 | 36 | println("\nStep 2: How to access elements of Queue at specific index") 37 | println(s"Element at index 0 = ${queue1(0)}") 38 | println(s"Element at index 0 = ${queue1(1)}") 39 | println(s"Element at index 0 = ${queue1(2)}") 40 | 41 | 42 | 43 | println("\nStep 3: How to add elements to Queue using +=") 44 | queue1 += "Glazed Donut" 45 | println(s"Elements of queue1 = $queue1") 46 | 47 | 48 | 49 | println("\nStep 4: How to add elements to Queue using enqueue") 50 | queue1.enqueue("Vanilla Donut") 51 | println(s"Enqueue element Vanilla Donut onto queue1 = $queue1") 52 | 53 | 54 | 55 | println("\nStep 5: How to take the first element or head from the Queue") 56 | val dequeuedElement: String = queue1.dequeue 57 | println(s"Dequeued element = $dequeuedElement") 58 | println(s"Elements of queue1 after dequeue = $queue1") 59 | 60 | 61 | 62 | println("\nStep 6: How to initialize an empty Queue") 63 | val emptyQueue = Queue.empty[String] 64 | println(s"Empty Queue = $emptyQueue") 65 | } 66 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/one/scala/basics/Tutorial_04_StringInterpolation.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.one.scala.basics 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Scala String Interpolation – Print And Format Variables 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-string-interpolation-print-format-variables/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_04_StringInterpolation extends App { 28 | 29 | println("Step 1: Using String interpolation to print a variable") 30 | val favoriteDonut = "Glazed Donut" 31 | println(s"My favorite donut = $favoriteDonut") 32 | 33 | 34 | 35 | println("\nStep 2: Using String interpolation on object properties") 36 | case class Donut(name: String, tasteLevel: String) 37 | 38 | val favoriteDonut2 = Donut("Glazed Donut", "Very Tasty") 39 | println(s"My favorite donut name = ${favoriteDonut2.name}, tasteLevel = ${favoriteDonut2.tasteLevel}") 40 | 41 | 42 | 43 | println("\nStep 3: Using String interpolation to evaluate expressions") 44 | val qtyDonutsToBuy = 10 45 | println(s"Are we buying 10 donuts = ${qtyDonutsToBuy == 10}") 46 | 47 | 48 | 49 | println("\nStep 4: Using String interpolation for formatting text") 50 | val donutName = "Vanilla Donut" 51 | val donutTasteLevel = "Tasty" 52 | println(f"$donutName%20s $donutTasteLevel") 53 | 54 | 55 | 56 | println("\nStep 5: Using f interpolation to format numbers") 57 | val donutPrice = 2.50 58 | println(s"Donut price = $donutPrice") 59 | println(f"Formatted donut price = $donutPrice%.2f") 60 | 61 | 62 | 63 | println("\nStep 6: Using raw interpolation") 64 | println(raw"Favorite donut\t$donutName") 65 | } 66 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_22_Mutable_Map.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Mutable Map 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-7-beginner-tutorial-using-scala-mutable-collection/scala-tutorial-learn-use-mutable-map/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_22_Mutable_Map extends App { 28 | 29 | import scala.collection.mutable.Map 30 | println("\nStep 1: How to initialize a Map with 3 elements") 31 | val map1 = Map(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut")) 32 | println(s"Elements of map1 = $map1") 33 | 34 | 35 | println("\nStep 2: How to initialize a Map using key -> value notation") 36 | val map2 = Map("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut") 37 | println(s"Elements of map2 = $map2") 38 | 39 | 40 | println("\nStep 3: How to access elements of Map by specific key") 41 | println(s"Element by key VD = ${map2("VD")}") 42 | println(s"Element by key GD = ${map2("GD")}") 43 | 44 | 45 | println("\nStep 4: How to add elements to Map using +=") 46 | map1 += ("KD" -> "Krispy Kreme Donut") 47 | println(s"Element in map1 = $map1") 48 | 49 | 50 | 51 | println("\nStep 5: How to add elements from a Map to an existing Map using ++=") 52 | map1 ++= map2 53 | println(s"Elements in map1 = $map1") 54 | 55 | 56 | println("\nStep 6: How to remove key and its value from Map using -=") 57 | map1 -= "CD" 58 | println(s"Map without the key CD and its value = $map1") 59 | 60 | 61 | println("\nStep 7: How to initialize an empty Map") 62 | val emptyMap = Map.empty[String,String] 63 | println(s"Empty Map = $emptyMap") 64 | } 65 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_03_Immutable_ListMap.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable ListMap 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-listmap/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_03_Immutable_ListMap extends App { 27 | 28 | println("Step 1: How to initialize a ListMap with 3 elements using key -> value notation") 29 | import scala.collection.immutable.ListMap 30 | val listMap1 = ListMap("PD" -> "Plain Donut", "SD" ->"Strawberry Donut", "CD" -> "Chocolate Donut") 31 | println(s"Elements of listMap1 = $listMap1") 32 | 33 | 34 | 35 | println("\nStep 2: How to access elements by specific key in the ListMap") 36 | println(s"Element by key PD = ${listMap1("PD")}") 37 | println(s"Element by key SD = ${listMap1("SD")}") 38 | 39 | 40 | 41 | println("\nStep 3: How to add elements to ListMap using +") 42 | val listMap2 = listMap1 + ("KD" -> "Krispy Kreme Donut") 43 | println(s"Elements of listMap2 = $listMap2") 44 | 45 | 46 | 47 | println("\nStep 4: How to add two ListMaps together using ++") 48 | val listMap3 = listMap1 ++ listMap2 49 | println(s"Elements of listMap3 = $listMap3") 50 | 51 | 52 | 53 | println("\nStep 5: How to remove key and value from ListMap using -") 54 | val listMap4 = listMap1 - "CD" 55 | println(s"ListMap without the key CD and its value = $listMap4") 56 | 57 | 58 | 59 | println("\nStep 6: How to initialize an empty ListMap") 60 | val emptyListMap = ListMap.empty[String,String] 61 | println(s"Empty ListMap with key type String and value also of type String= $emptyListMap") 62 | } 63 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_17_Immutable_Vector.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable Vector 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-vector/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_17_Immutable_Vector extends App { 27 | 28 | println("Step 1: How to initialize a Vector with 3 elements") 29 | val vector1 = Vector("Plain Donut", "Strawberry Donut", "Chocolate Donut") 30 | println(s"Elements of vector1 = $vector1") 31 | 32 | 33 | 34 | println("\nStep 2: How to access elements of Vector at specific index") 35 | println(s"Element at index 0 = ${vector1(0)}") 36 | println(s"Element at index 1 = ${vector1(1)}") 37 | println(s"Element at index 2 = ${vector1(2)}") 38 | 39 | 40 | 41 | println("\nStep 3: How to append elements at the end of Vector using :+") 42 | val vector2 = vector1 :+ "Vanilla Donut" 43 | println(s"Adding elements to Vector using :+ = $vector2") 44 | 45 | 46 | 47 | println("\nStep 4: How to prepend elements in front of Vector using +:") 48 | val vector3 = "Vanilla Donut" +: vector1 49 | println(s"Adding elements to Vector using :+ = $vector3") 50 | 51 | 52 | 53 | println("\nStep 5: How to add two Vectors together using ++") 54 | val vector4 = vector1 ++ Vector("Glazed Donut") 55 | println(s"Add two vectors together using ++ = $vector3") 56 | // NOTE: this return a new Vector(...), elements from the second vector) 57 | 58 | 59 | 60 | println("\nStep 6: How to initialize an empty Vector") 61 | val emptyVector = Vector.empty[String] 62 | println(s"Empty vector of type String = $emptyVector") 63 | } 64 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_11_FoldLeft.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use FoldLeft Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-foldleft-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_11_FoldLeft extends App { 27 | 28 | println("Step 1: How to initialize a sequence of donut prices") 29 | val prices = Seq(1.5, 2.0, 2.5) 30 | println(s"Donut prices = $prices") 31 | 32 | 33 | 34 | println("\nStep 2: How to sum all the donut prices using foldLeft function") 35 | val sum = prices.foldLeft(0.0)(_ + _) 36 | println(s"Sum = $sum") 37 | 38 | 39 | 40 | println("\nStep 3: How to initialize a Sequence of donuts") 41 | val donuts = Seq("Plain", "Strawberry", "Glazed") 42 | println(s"Elements of donuts1 = $donuts") 43 | 44 | 45 | 46 | println("\nStep 4: How to create a String of all donuts using foldLeft function") 47 | println(s"All donuts = ${donuts.foldLeft("")((acc, s) => acc + s + " Donut ")}") 48 | 49 | 50 | 51 | println("\nStep 5: How to declare a value function to create the donut string") 52 | val concatDonuts: (String, String) => String = (acc, s) => { 53 | println(s"Accumulator = $acc") 54 | val appendDonutLiteral = s + " Donut " 55 | println(s"Append Donut literal = $appendDonutLiteral") 56 | val mutatedAccumulator = acc + appendDonutLiteral 57 | println(s"Accumulator after mutation = $mutatedAccumulator") 58 | mutatedAccumulator 59 | } 60 | 61 | 62 | 63 | println("\nStep 6: How to create a String of all donuts using value function from Step 5 and fold function") 64 | println(s"All donuts = ${donuts.foldLeft("")(concatDonuts)}") 65 | } 66 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_12_FoldRight.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use FoldRight Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-foldright-example/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_12_FoldRight extends App { 28 | 29 | println("Step 1: How to initialize a sequence of donut prices") 30 | val prices = Seq(1.5, 2.0, 2.5) 31 | println(s"Donut prices = $prices") 32 | 33 | 34 | 35 | println("\nStep 2: How to sum all the donut prices using foldRight function") 36 | val sum = prices.foldRight(0.0)(_ + _) 37 | println(s"Sum = $sum") 38 | 39 | 40 | 41 | println("\nStep 3: How to initialize a Sequence of donuts") 42 | val donuts = Seq("Plain", "Strawberry", "Glazed") 43 | println(s"Elements of donuts1 = $donuts") 44 | 45 | 46 | 47 | println("\nStep 4: How to create a String of all donuts using foldRight function") 48 | println(s"All donuts = ${donuts.foldRight("")((s, acc) => acc + " Donut " + s)}") 49 | 50 | 51 | 52 | println("\nStep 5: How to declare a value function to create the donut string") 53 | val concatDonuts: (String, String) => String = (s, acc) => { 54 | println(s"Accumulator = $acc") 55 | val prependDonutLiteral = " Donut " + s 56 | println(s"Prepend Donut literal = $prependDonutLiteral") 57 | val mutatedAccumulator = acc + prependDonutLiteral 58 | println(s"Accumulator after mutation = $mutatedAccumulator") 59 | mutatedAccumulator 60 | } 61 | 62 | 63 | 64 | println("\nStep 6: How to create a String of all donuts using value function from Step 5 and foldRight function") 65 | println(s"All donuts = ${donuts.foldRight("")(concatDonuts)}") 66 | } 67 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_20_Mutable_Stack.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Mutable ArrayStack 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-7-beginner-tutorial-using-scala-mutable-collection/scala-tutorial-learn-use-mutable-arraystack/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_20_Mutable_Stack extends App { 27 | 28 | import scala.collection.mutable.Stack 29 | println("Step 1: How to initialize a Stack with 3 elements") 30 | 31 | 32 | val stack = Stack("Plain Donut", "Strawberry Donut", "Chocolate Donut") 33 | println(s"Elements of stack = $stack") 34 | 35 | 36 | 37 | println("\nStep 2: How to check elements at specific index of a Stack") 38 | println(s"Element at index 0 = ${stack(0)}") 39 | println(s"Element at index 1 = ${stack(1)}") 40 | println(s"Element at index 2 = ${stack(2)}") 41 | 42 | 43 | 44 | println("\nStep 3: How to add elements to a Stack using +=") 45 | stack += "Vanilla Donut" 46 | println(s"Elements of stack = $stack") 47 | 48 | 49 | 50 | println("\nStep 4: How to add elements from a List to a Stack using ++=") 51 | stack ++= List("Glazed Donut", "Krispy creme") 52 | println(s"Elements of stack = $stack") 53 | 54 | 55 | 56 | println("\nStep 5: How to take an element from a Stack using pop method") 57 | println(s"Pop element from stack = ${stack.pop}") 58 | println(s"Elements of stack = $stack") 59 | 60 | 61 | 62 | println("\nStep 6: How to push one element at the top of the a Stack using push method") 63 | stack.push("Krispy Creme") 64 | println(s"Elements after push = $stack") 65 | 66 | 67 | 68 | println("\nStep 7: How to initialize an empty Stack") 69 | val emptyStack = Stack.empty 70 | println(s"Empty Stack = $emptyStack") 71 | } 72 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_02_Immutable_ListSet.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | import scala.collection.immutable.ListSet 4 | 5 | /** 6 | * Created by Nadim Bahadoor on 15/10/2018. 7 | * 8 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 9 | * 10 | * Tutorial: Learn How To Use Scala's Immutable ListSet 11 | * 12 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-listset/ Tutorial]] 13 | * 14 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 17 | * use this file except in compliance with the License. You may obtain a copy of 18 | * the License at 19 | * 20 | * [http://www.apache.org/licenses/LICENSE-2.0] 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 25 | * License for the specific language governing permissions and limitations under 26 | * the License. 27 | */ 28 | object Tutorial_02_Immutable_ListSet extends App { 29 | 30 | println("Step 1: How to initialize an immutable ListSet with 4 elements") 31 | val listSet1 = ListSet("Plain Donut","Strawberry Donut","Chocolate Donut", "Plain Donut") 32 | println(s"Elements of listSet1 = $listSet1") 33 | 34 | 35 | 36 | println("\nStep 2: How to check elements of immutable ListSet") 37 | println(s"Element Plain Donut = ${listSet1("Plain Donut")}") 38 | println(s"Element Strawberry Donut = ${listSet1("Strawberry Donut")}") 39 | println(s"Element Chocolate Donut = ${listSet1("Chocolate Donut")}") 40 | 41 | 42 | 43 | println("\nStep 3: How to add elements of immutable ListSet using +") 44 | val listSet2 = listSet1 + "Vanilla Donut" 45 | println(s"Adding element Vanilla to ListSet using + = $listSet2") 46 | 47 | 48 | 49 | println("\nStep 4: How to add two ListSet together using ++") 50 | val listSet3 = listSet1 ++ ListSet("Glazed Donut") 51 | println(s"Add two lists together using ++ = $listSet3") 52 | 53 | 54 | 55 | println("\nStep 5: How to remove an element from the ListSet using -") 56 | val listSet4 = listSet1 - "Plain Donut" 57 | println(s"ListSet without the element Plain Donut = $listSet4") 58 | 59 | 60 | 61 | println("\nStep 6: How to initialize an empty ListSet") 62 | val emptyListSet = ListSet.empty[String] 63 | println(s"Empty ListSet of type String = $emptyListSet") 64 | } 65 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_15_Immutable_Stack.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable Stack 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-stack/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_15_Immutable_Stack extends App { 28 | 29 | println("\nStep 1: How to initialize a Stack using an Immutable List") 30 | val stack1 = List("Plain Donut","Strawberry Donut","Chocolate Donut") 31 | println(s"Using an Immutable List for stack, elements are = $stack1") 32 | 33 | 34 | 35 | println("\nStep 2: Push one element at the top of the stack using :: of Immutable List") 36 | val stack2 = "Vanilla Donut" :: stack1 37 | println(s"Using an Immutable List for stack, elements after push = $stack2") 38 | 39 | 40 | 41 | println("\nStep 3: Push N elements at the top of the stack using :: of Immutable List") 42 | val stack3 = "Glazed Donut" :: "Vanilla Donut" :: stack1 43 | println(s"Using an Immutable List for stack, elements after pushing N elements = $stack3") 44 | 45 | 46 | 47 | println("\nStep 4: Pop element from the Stack using tail method of Immutable List") 48 | val stack4 = stack1.tail 49 | println(s"Using an Immutable List for stack, elements after tail method to simulate Stack pop = $stack4") 50 | 51 | 52 | 53 | println("\nStep 5: Capture the popped element") 54 | val popped :: stack = stack1 55 | println(s"Popped element = $popped, the stack after the pop operation = $stack") 56 | 57 | 58 | 59 | println("\nStep 6: How to initialize an empty Stack using Immutable List") 60 | val emptyStack = List.empty[String] 61 | println(s"Using an Immutable List for stack, the empty stack = $emptyStack") 62 | } 63 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_23_Mutable_HashMap.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Mutable HashMap 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-7-beginner-tutorial-using-scala-mutable-collection/scala-tutorial-learn-use-mutable-hashmap/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_23_Mutable_HashMap extends App { 28 | 29 | import scala.collection.mutable.HashMap 30 | println("\nStep 1: How to initialize a HashMap with 3 elements") 31 | val hashMap1 = HashMap(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut")) 32 | println(s"Elements of hashMap1 = $hashMap1") 33 | 34 | 35 | 36 | println("\nStep 2: How to initialize HashMap using key -> value notation") 37 | val hashMap2 = HashMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut") 38 | println(s"Elements of hashMap2 = $hashMap2") 39 | 40 | 41 | 42 | println("\nStep 3: How to access elements of HashMap by specific key") 43 | println(s"Element by key VD = ${hashMap2("VD")}") 44 | println(s"Element by key GD = ${hashMap2("GD")}") 45 | 46 | 47 | 48 | println("\nStep 4: How to add elements to HashMap using +=") 49 | hashMap1 += "KD" -> "Krispy Kreme Donut" 50 | println(s"Element in hashMap1 = $hashMap1") 51 | 52 | 53 | 54 | println("\nStep 5: How to add elements from a HashMap to an existing HashMap using ++=") 55 | hashMap1 ++= hashMap2 56 | println(s"Elements in hashMap1 = $hashMap1") 57 | 58 | 59 | 60 | println("\nStep 6: How to remove key and its value from HashMap using -=") 61 | hashMap1 -= "CD" 62 | println(s"HashMap without the key CD and its value = $hashMap1") 63 | 64 | 65 | 66 | println("\nStep 7: How to initialize an empty HashMap") 67 | val emptyMap = HashMap.empty[String,String] 68 | println(s"Empty HashMap = $emptyMap") 69 | } 70 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/five/Tutorial_41_SortWith.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.five 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use SortWith Function With Examples 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-8-beginner-tutorial-using-scala-collection-functions/scala-sortwith-example/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_41_SortWith extends App { 27 | 28 | println("\nStep 1: How to create a case class to represent Donut objects") 29 | case class Donut(name: String, price: Double) 30 | 31 | 32 | 33 | println("\nStep 2: How to create a Sequence of type Donut") 34 | val donuts = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5)) 35 | println(s"Elements of donuts = $donuts") 36 | 37 | 38 | 39 | println("\nStep 3: How to sort a Sequence of case class objects using the sortWith function") 40 | println(s"Sort a Sequence of case classes of type Donut, sorted with price = ${donuts.sortWith(_.price < _.price)}") 41 | 42 | 43 | 44 | println("\nStep 4: How to sort a Sequence of case class objects in ascending order using the sortWith function") 45 | println(s"Sort a Sequence of case classes of type Donut, sorted with price in ascending order = ${donuts.sortWith(_.price < _.price)}") 46 | println(s"Sort a Sequence of case classes of type Donut, sorted with price in ascending order explicitly = ${donuts.sortWith((d1,d2) => d1.price < d2.price)}") 47 | 48 | 49 | 50 | println("\nStep 5: How to sort a Sequence of case class objects in descending order using the sortWith function") 51 | println(s"Sort a Sequence of case classes of type Donut, sorted with price in descending order = ${donuts.sortWith(_.price > _.price)}") 52 | println(s"Sort a Sequence of case classes of type Donut, sorted with price in descending order explicitly = ${donuts.sortWith((d1,d2) => d1.price > d2.price)}") 53 | } 54 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_24_Mutable_ListMap.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Mutable ListMap 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-7-beginner-tutorial-using-scala-mutable-collection/scala-tutorial-learn-use-mutable-listmap/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_24_Mutable_ListMap extends App { 28 | 29 | import scala.collection.immutable.ListMap 30 | println("\nStep 1: How to initialize a ListMap with 3 elements") 31 | var listMap1 = ListMap("PD" -> "Plain Donut", "SD" -> "Strawberry Donut", "CD" -> "Chocolate Donut") 32 | println(s"Elements of listMap1 = $listMap1") 33 | 34 | 35 | 36 | println("\nStep 2: How to initialize ListMap using key -> value notation") 37 | var listMap2 = ListMap("VD" -> "Vanilla Donut", "GD" -> "Glazed Donut") 38 | println(s"Elements of listMap1 = $listMap2") 39 | 40 | 41 | 42 | println("\nStep 3: How to access elements of ListMap by specific key") 43 | println(s"Element by key VD = ${listMap2("VD")}") 44 | println(s"Element by key GD = ${listMap2("GD")}") 45 | 46 | 47 | 48 | println("\nStep 4: How to add elements to ListMap using +=") 49 | listMap1 += "KD" -> "Krispy Kreme Donut" 50 | println(s"Element of listMap1 = $listMap1") 51 | 52 | 53 | 54 | println("\nStep 5: How to add elements from a ListMap to an existing ListMap using ++=") 55 | listMap1 ++= listMap2 56 | println(s"Element of listMap1 = $listMap1") 57 | 58 | 59 | 60 | println("\nStep 6: How to remove key and its value from ListMap using -=") 61 | listMap1 -= "CD" 62 | println(s"ListMap without the key CD and its value = $listMap1") 63 | 64 | 65 | 66 | println("\nStep 7: How to initialize an empty ListMap") 67 | val emptyListMap = ListMap.empty[String, String] 68 | println(s"Empty ListMap of type String = $emptyListMap") 69 | } -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/two/Tutorial_05_PartialFunction.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.two 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Create Partial Function Using the PartialFunction Trait 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-3-beginner-tutorial-using-functions-scala/scala-tutorial-learn-create-partial-function-trait/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_05_PartialFunction extends App { 27 | 28 | println("Step 1: Review of Pattern Matching in Scala") 29 | val donut = "Glazed Donut" 30 | val tasteLevel = donut match { 31 | case "Glazed Donut" | "Strawberry Donut" => "Very tasty" 32 | case "Plain Donut" => "Tasty" 33 | case _ => "Tasty" 34 | } 35 | println(s"Taste level of $donut = $tasteLevel") 36 | 37 | 38 | 39 | println("\nStep 2: How to define a Partial Function named isVeryTasty") 40 | val isVeryTasty: PartialFunction[String, String] = { 41 | case "Glazed Donut" | "Strawberry Donut" => "Very Tasty" 42 | } 43 | 44 | 45 | 46 | println("\nStep 3: How to call the Partial Function named isVeryTasty") 47 | println(s"Calling partial function isVeryTasty = ${isVeryTasty("Glazed Donut")}") 48 | 49 | 50 | 51 | println("\nStep 4: How to compose PartialFunction using orElse") 52 | val isTasty: PartialFunction[String, String] = { 53 | case "Plain Donut" => "Tasty" 54 | } 55 | 56 | val unknownTaste: PartialFunction[String, String] = { 57 | case donut @ _ => s"Unknown taste for donut = $donut" 58 | } 59 | 60 | val donutTaste = isVeryTasty orElse isTasty orElse unknownTaste 61 | println(donutTaste("Glazed Donut")) 62 | println(donutTaste("Plain Donut")) 63 | println(donutTaste("Chocolate Donut")) 64 | 65 | 66 | 67 | println("\nStep 5: Lifting a PartialFunction that returns an Option") 68 | val unknownTasteResult = isTasty.lift("unknown taste") 69 | println(unknownTasteResult) 70 | } 71 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_16_Immutable_Stream.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable Stream 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-stream/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_16_Immutable_Stream extends App { 28 | 29 | println("Step 1: How to create a Stream with 3 numbers using #::") 30 | val stream1 = 1 #:: 2 #:: 3 #:: LazyList.empty[Int] 31 | println(s"Elements of stream1 = $stream1") 32 | 33 | 34 | 35 | import scala.collection.immutable.LazyList.cons 36 | println("\nStep 2: How to create a Stream with 3 numbers using Stream.cons") 37 | val stream2 = cons(1, cons(2, cons(3, LazyList.empty) ) ) 38 | println(s"Elements of stream2 = $stream2") 39 | 40 | 41 | 42 | println("\nStep 3: How to print all 3 numbers from stream2 using the take function") 43 | print("Take first 3 numbers from stream2 = ") 44 | stream2 take 3 foreach println 45 | 46 | print("\nTake first 10 numbers from stream2 = ") 47 | stream2 take 10 foreach println 48 | 49 | 50 | 51 | println("\n\nStep 4: How to define an infinite Stream using LazyList.from") 52 | println("Take only the first 20 numbers from the infinite number stream = ") 53 | val stream3 = LazyList.from(1) 54 | stream3 take 20 foreach println 55 | 56 | 57 | 58 | println("\n\nStep 5: How to define an infinite number stream with 2 increments using LazyList.from") 59 | val stream4 = LazyList.from(1, 2) 60 | println("Take only the first 20 numbers from the infinite number stream of 2 increments = ") 61 | stream4 take 20 foreach println 62 | 63 | 64 | 65 | println("\n\nStep 6: How to initialize an empty stream") 66 | val emptyStream = LazyList.empty[Int] 67 | println(s"Empty stream = $emptyStream") 68 | } 69 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/four/Tutorial_05_Immutable_HashMap.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.four 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Scala's Immutable HashMap 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-6-beginner-tutorial-using-scala-immutable-collection/scala-tutorial-learn-use-immutable-hashmap/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_05_Immutable_HashMap extends App { 27 | 28 | import scala.collection.immutable.HashMap 29 | println("Step 1: How to initialize a HashMap with 3 elements using Tuples of key and value") 30 | val hashMap1 = HashMap(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut")) 31 | println(s"Elements of hashMap1 = $hashMap1") 32 | 33 | 34 | 35 | println("\nStep 2: How to initialize HashMap using key -> value notation") 36 | val hashMap2 = HashMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut") 37 | println(s"Elements of hashMap2 = $hashMap2") 38 | 39 | 40 | 41 | println("\nStep 3: How to access elements in HashMap by specific key") 42 | println(s"Element by key VD = ${hashMap2("VD")}") 43 | println(s"Element by key GD = ${hashMap2("GD")}") 44 | 45 | 46 | 47 | println("\nStep 4: How to add elements to HashMap using +") 48 | val hashMap3 = hashMap1 + ("KD" -> "Krispy Kreme Donut") 49 | println(s"Element in hashMap3 = $hashMap3") 50 | 51 | 52 | 53 | println("\nStep 5: How to add two HashMaps together using ++") 54 | val hashMap4 = hashMap1 ++ hashMap2 55 | println(s"Elements in hashMap4 = $hashMap4") 56 | 57 | 58 | 59 | println("\nStep 6: How to remove key and its value from HashMap using -") 60 | val hashMap5 = hashMap4 - "CD" 61 | println(s"HashMap without the key CD and its value = $hashMap5") 62 | 63 | 64 | 65 | println("\nStep 7: How to initialize an empty HashMap") 66 | val emptyHashMap = HashMap.empty[String,String] 67 | println(s"Empty HashMap = $emptyHashMap") 68 | } 69 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/two/Tutorial_08_RecursiveFunctions.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.two 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Create Tail Recursive Function - @annotation.tailrec 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-3-beginner-tutorial-using-functions-scala/scala-tutorial-learn-create-tail-recursive-function-tailrec-annotation/ Tutorial]] 11 | * 12 | * 13 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy of 17 | * the License at 18 | * 19 | * [http://www.apache.org/licenses/LICENSE-2.0] 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | object Tutorial_08_RecursiveFunctions extends App { 28 | 29 | println("Step 1: How to define an Array of type String") 30 | val donutsVector = Vector("Vanilla Donut", "Strawberry Donut", "Plain Donut", "Glazed Donut") 31 | 32 | 33 | 34 | println("\nStep 2: How to define a tail recursive function") 35 | @annotation.tailrec 36 | def search(donutName: String, donuts: Vector[String], index: Int): Boolean = { 37 | if(donuts.length == index) 38 | false 39 | else if(donuts(index) == donutName) 40 | true 41 | else { 42 | val nextIndex = index + 1 43 | search(donutName, donuts, nextIndex) 44 | } 45 | } 46 | 47 | 48 | 49 | println("\nStep 3: How to call a tail recursive function") 50 | val found = search("Glazed Donut", donutsVector, 0) 51 | println(s"Find Glazed Donut = $found") 52 | 53 | val notFound = search("Chocolate Donut", donutsVector, 0) 54 | println(s"Find Chocolate Donut = $notFound") 55 | 56 | 57 | 58 | println("\nStep 4: Compile time checks using @annotation.tailrec") 59 | // @annotation.tailrec 60 | def searchNotTailRecursive(donutName: String, donuts: Vector[String], index: Int): Boolean = { 61 | if(donuts.length == index) 62 | throw new IllegalStateException(s"$donutName was not found!") 63 | else if(donuts(index) == donutName) 64 | true 65 | else { 66 | val nextIndex = index + 1 67 | search(donutName, donuts, nextIndex) 68 | } 69 | } 70 | 71 | 72 | 73 | println(s"${donutsVector.contains("Glazed Donut")}") 74 | } 75 | -------------------------------------------------------------------------------- /source-code/scala-for-beginners/src/main/scala/com/allaboutscala/learn/scala/seven/days/day/three/Tutorial_06_TraitMixinAndLinearization.scala: -------------------------------------------------------------------------------- 1 | package com.allaboutscala.learn.scala.seven.days.day.three 2 | 3 | /** 4 | * Created by Nadim Bahadoor on 15/10/2018. 5 | * 6 | * The content was inspired by the original tutorial below, and feedback from readers at http://allaboutscala.com. 7 | * 8 | * Tutorial: Learn How To Use Traits For Dependency Injection Part 2 - Avoid Cake Pattern 9 | * 10 | * [[http://allaboutscala.com/tutorials/chapter-5-traits/scala-traits-depedency-injection-avoid-cake-pattern/ Tutorial]] 11 | * 12 | * Copyright 2016 - 2019 Nadim Bahadoor (http://allaboutscala.com) 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 15 | * use this file except in compliance with the License. You may obtain a copy of 16 | * the License at 17 | * 18 | * [http://www.apache.org/licenses/LICENSE-2.0] 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 23 | * License for the specific language governing permissions and limitations under 24 | * the License. 25 | */ 26 | object Tutorial_06_TraitMixinAndLinearization extends App { 27 | 28 | println("Step 1: Review of inheritance using traits") 29 | case class Donut(name: String) 30 | 31 | trait ShoppingCart[T] { 32 | def printItems(items: Seq[T]): Unit 33 | } 34 | 35 | class DonutShoppingCart extends ShoppingCart[Donut] { 36 | override def printItems(items: Seq[Donut]): Unit = { 37 | items.foreach(println(_)) 38 | } 39 | } 40 | 41 | val donuts = Seq(Donut("Plain Donut"), Donut("Vanilla Donut")) 42 | val donutCart = new DonutShoppingCart() 43 | donutCart.printItems(donuts) 44 | 45 | 46 | 47 | println("\nStep 2: Define trait PrettyPrintUpperCase and mixin with DonutShoppingCart") 48 | trait PrettyPrintUpperCase[T] extends ShoppingCart[T] { 49 | override def printItems(items: Seq[T]): Unit = items.foreach(item => println(item.toString.toUpperCase)) 50 | } 51 | 52 | val donutCart2 = new DonutShoppingCart() with PrettyPrintUpperCase[Donut] 53 | donutCart2.printItems(donuts) 54 | 55 | 56 | 57 | println("\nStep 3: Linearization when mixin multiple traits") 58 | trait PrettyPrintLowerCase[T] extends ShoppingCart[T] { 59 | override def printItems(items: Seq[T]): Unit = items.foreach(item => println(item.toString.toLowerCase)) 60 | } 61 | 62 | val donutCart3 = new DonutShoppingCart() with PrettyPrintUpperCase[Donut] with PrettyPrintLowerCase[Donut] 63 | donutCart3.printItems(donuts) 64 | 65 | val donutCart4 = new DonutShoppingCart() with PrettyPrintLowerCase[Donut] with PrettyPrintUpperCase[Donut] 66 | donutCart4.printItems(donuts) 67 | } 68 | --------------------------------------------------------------------------------