Skip to content

Add a Scala tutorial #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@

This repository hosts a library for the first course of the [Scala MOOC](https://www.coursera.org/specializations/scala) ("Functional Programming Principles in Scala").

## Run Locally

- Clone this repository, compile and publish the project:

~~~ sh
git clone git@github.com:scala-exercises/exercises-scalatutorial.git
cd exercises-scalatutorial/
sbt compile publishLocal # it is important to first run the `compile` command alone
~~~

- Clone the `evaluator` and run it:

~~~ sh
git clone git@github.com:scala-exercises/evaluator.git
cd evaluator/
sbt "project evaluator-server" run
~~~

- Clone the `scala-tutorial` branch of our `scala-exercises` fork:

~~~ sh
git clone -b scala-tutorial git@github.com:scalacenter/scala-exercises.git
~~~

- Follow the database setup instructions given
[here](https://github.com/scala-exercises/scala-exercises#configure-the-database)

- Add the following line the `server/conf/application.dev.conf`:

~~~
evaluator.secretKey="secretKey"
~~~

- Run the server:

~~~ sh
sbt -mem 1500 run
~~~

## About Scala exercises

"Scala Exercises" brings exercises for the Stdlib, Cats, Shapeless and many other great libraries for Scala to your browser. Offering hundreds of solvable exercises organized into several categories covering the basics of the Scala language and it's most important libraries.
Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
lazy val template = (project in file("."))
lazy val `scala-tutorial` = (project in file("."))
.enablePlugins(ExerciseCompilerPlugin)
.settings(
organization := "org.scala-exercises",
name := "exercises-fpprinciples",
name := "exercises-scalatutorial",
scalaVersion := "2.11.8",
version := "0.3.1-SNAPSHOT",
version := "0.3.0-SNAPSHOT",
resolvers ++= Seq(
Resolver.sonatypeRepo("snapshots"),
Resolver.sonatypeRepo("releases")
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/public/scala_tutorial/animals.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/main/resources/scala-tutorial.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 0 additions & 17 deletions src/main/scala/fpprincipleslib/FpPrinciplesLibrary.scala

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/scala/fpprincipleslib/FunctionsAndEvaluationSection.scala

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/scala/scalatutorial/ScalaTutorial.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package scalatutorial

import org.scalaexercises.definitions.Library

import sections._

/** Quickly learn Scala through an interactive tutorial.
*
* @param name scala_tutorial
*/
object ScalaTutorial extends Library {
val owner = "scala-exercises"
val repository = "exercises-scalatutorial"
override val color = Some("#f26527")
val logoPath = "scala-tutorial"

val sections = List(
TermsAndTypes,
DefinitionsAndEvaluation,
FunctionalLoops,
LexicalScopes,
TailRecursion,
StructuringInformation,
HigherOrderFunctions,
StandardLibrary,
SyntacticConveniences,
ObjectOrientedProgramming,
ImperativeProgramming,
ClassesVsCaseClasses,
PolymorphicTypes,
LazyEvaluation,
TypeClasses
)
}
16 changes: 16 additions & 0 deletions src/main/scala/scalatutorial/aux/BankAccount.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scalatutorial.aux

class BankAccount {

private var balance = 0

def deposit(amount: Int): Unit = {
if (amount > 0) balance = balance + amount
}

def withdraw(amount: Int): Int =
if (0 < amount && amount <= balance) {
balance = balance - amount
balance
} else throw new Error("insufficient funds")
}
23 changes: 23 additions & 0 deletions src/main/scala/scalatutorial/aux/IntSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package scalatutorial.aux

abstract class IntSet {
def incl(x: Int): IntSet
def contains(x: Int): Boolean
}

object Empty extends IntSet {
def contains(x: Int): Boolean = false
def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty)
}
class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet {

def contains(x: Int): Boolean =
if (x < elem) left contains x
else if (x > elem) right contains x
else true

def incl(x: Int): IntSet =
if (x < elem) new NonEmpty(elem, left incl x, right)
else if (x > elem) new NonEmpty(elem, left, right incl x)
else this
}
3 changes: 3 additions & 0 deletions src/main/scala/scalatutorial/aux/Note.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package scalatutorial.aux

case class Note(name: String, duration: String, octave: Int)
11 changes: 11 additions & 0 deletions src/main/scala/scalatutorial/aux/Rational.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scalatutorial.aux

class Rational(x: Int, y: Int) {

private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
private val g = gcd(x, y)

lazy val numer: Int = x / g
lazy val denom: Int = y / g

}
15 changes: 15 additions & 0 deletions src/main/scala/scalatutorial/aux/animals.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scalatutorial.aux

trait Animal {
def fitness: Int
}

trait Reptile extends Animal

trait Mammal extends Animal

trait Zebra extends Mammal {
def zebraCount: Int
}

trait Giraffe extends Mammal
20 changes: 20 additions & 0 deletions src/main/scala/scalatutorial/aux/sorting.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package scalatutorial.aux

object Sorting {

def insertionSort[A](xs: List[A])(implicit ord: Ordering[A]): List[A] = {
def insert(y: A, ys: List[A]): List[A] =
ys match {
case List() => y :: List()
case z :: zs =>
if (ord.lt(y, z)) y :: z :: zs
else z :: insert(y, zs)
}

xs match {
case List() => List()
case y :: ys => insert(y, insertionSort(ys))
}
}

}
Loading