Coder Social home page Coder Social logo

scala-exercises / exercises-scalatutorial Goto Github PK

View Code? Open in Web Editor NEW
161.0 161.0 386.0 591 KB

Exercises for the "Functional Programming Principles in Scala", part of the FP in Scala specialized program by EPFL.

License: Apache License 2.0

Scala 100.00%

exercises-scalatutorial's Introduction

Join the conversation on Gitter

Scala Exercises


How it works

"Scala Exercises" brings exercises for the Stdlib, Cats, Shapeless, and many other great libraries for Scala to your browser. This includes hundreds of solvable exercises organized into several categories covering the basics of the Scala language and its most important libraries.

  • LEARN: Each category includes an explanation of the basics. Learn the concepts through simple code samples.

  • SOLVE: Each exercise is a unit test that must pass successfully—complete the exercise by filling in the blanks. Receive instant feedback as your answers are validated in real-time.

  • SHARE: The system will consider the category complete when all its exercises are successfully done. Don't forget to share your progress on social networks before moving on to the next category!

  • EDIT: After completing a category, you'll be able to go back and edit it. Add new exercises or improve existing ones by sending a pull request.

Getting Started

Online

Scala Exercises is available at scala-exercises.org.

Local development

Prerequisites

Installing the app locally

Get the repository

First of all, either clone the repository via git

$ git clone https://github.com/scala-exercises/scala-exercises

or download it

$ wget https://github.com/scala-exercises/scala-exercises/archive/master.zip
Configure the database

You'll need a working PostgreSQL 9.4 database and user for running the app. Once the database is running,

  • Create a user called scalaexercises_dev_user
$ sudo -u postgres psql -c "CREATE USER scalaexercises_dev_user WITH PASSWORD 'a_password';"
  • Create a db called scalaexercises_dev and grant all privileges on it to scalaexercises_dev_user
$ sudo -u postgres createdb scalaexercises_dev
$ sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE scalaexercises_dev TO scalaexercises_dev_user;"

Alternatively, you can also use Docker to run the database. The following command creates a database container and exposes it:

$ docker run --name scala-exercises-db -e POSTGRES_DB=scalaexercises_dev -e POSTGRES_PASSWORD=scalaexercises_pass -e POSTGRES_USER=scalaexercises_dev_user -p 5432:5432 -d postgres:9.4
Configure the application

Edit the server/conf/application.dev.conf configuration file with your database information.

Running the app

Go into the project's root directory, run sbt server/run with -mem option to increase the memory.

$ sbt -mem 1500 server/run

After compilation, the application will be running, listening in the 9000 port. Point your browser to localhost:9000 and start having fun!

Running the tests

To run the tests (for the server project), you need to add a test database and a test user.

  • Create a user called scalaexercises_user
$ sudo -u postgres psql -c "CREATE USER scalaexercises_user WITH PASSWORD 'scalaexercises_pass';"
  • Create a db called scalaexercises_test and grant all privileges on it to scalaexercises_user
$ sudo -u postgres createdb scalaexercises_test
$ sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE scalaexercises_test TO scalaexercises_user;"

Adding more exercises

Currently, scala-exercises includes exercises for the Scala Standard Library, Cats, and Shapeless. However, more exercises are available, like for Doobie, Functional Programming in Scala, and ScalaCheck. See the scala-exercises on github, or you can include exercises from other parties or create your own (see Contributing section).

To add additional exercises to your locally running server:

  • clone the exercises repository to a local folder
  • 'cd' into the local repository folder.
  • run sbt compile publishLocal to create a jar in your local ivy repository.
    !Note: The compile task is mandatory here otherwise the exercises will not show up in the application.
  • add a dependency to the exersises jar in the server project in the build.sbt file (~L118).

Now run sbt server/run and the application index will also display the added exercises.

Troubleshooting

Additional exercises do not show up in the application

See the Adding more exercises section. Note that, currently, the compile step is required before publishLocal for the application to be able to pickup the exercises.

Ensime

If you use ensime and you have configured the sbt-ensime plugin in your sbt user global settings, it's likely you might have this issue running the application locally:

java.lang.NoClassDefFoundError: scalariform/formatter/preferences/SpacesAroundMultiImports$

In that case, you could solve this issue setting up your /.sbt/0.13/plugins/plugins.sbt file as follows:

addSbtPlugin("org.ensime" % "ensime-sbt" % "0.5.1")

dependencyOverrides in ThisBuild += "org.scalariform" %% "scalariform" % "0.1.8"

In order to avoid the error related to Github API rate limit exceeded during compilation of exercises, we recommend setting a local environment variable called GITHUB_TOKEN with a personal token that you can create here.

While creating the PostgreSQL database, you may run into problems following the previous instructions if developing on a MacOS X environment. In that case, we recommend using the following alternatives:

  • Create a user called scalaexercises_dev_user. Note that, if you installed PostgreSQL using Homebrew, your superuser may be different than postgres:
$ psql -U your_postgres_user -c "CREATE USER scalaexercises_dev_user WITH PASSWORD 'a_password';"
  • Create a db called scalaexercises_dev and grant all privileges on it to scalaexercises_dev_user:
$ createdb scalaexercises_dev
$ psql -U your_postgres_user -c "GRANT ALL PRIVILEGES ON DATABASE scalaexercises_dev TO scalaexercises_dev_user;"

Project structure

The project is split between a few directories, namely:

  • server, which contains the server code written using Play,
  • client, which contains ScalaJS code for a frontend part of the application,
  • shared, where code shared between the server and the client exists,
  • definitions, containing definitions used by other parts of the application and libraries containing exercises,
  • sbt-exercise is a sbt plugin that locates exercise libraries and processes their source code,
  • compiler for compiling exercises,
  • runtime for runtime evaluation of exercises.

The compiler and runtime directories allow exercises to be defined using regular Scala, which is compiled into an exercise library.

The site, client, and shared directories contain the website. These items depend on components in compiler and runtime.

At the moment, those subprojects are coupled tightly. Once this project is a bit more stable, the exercise compiler plugin will be published, and it will be easy to create new exercises for existing Scala libraries.

exercises-scalatutorial's People

Contributors

47erbot avatar adrianrafo avatar alejandrohdezma avatar angoglez avatar batakpout avatar caileend avatar calvellido avatar dominv avatar exp0nge avatar fedefernandez avatar frosner avatar iselind avatar jacobarchambault avatar jonmcoe avatar juanmabm avatar juanpedromoreno avatar julienrf avatar kiroco12 avatar maureenelsberry avatar mdolata avatar mergify[bot] avatar michad avatar rvilla87 avatar sauntimo avatar scala-steward avatar sckelemen avatar sloshy avatar tschis avatar xhudik avatar xiaowei-zhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

exercises-scalatutorial's Issues

list exercise error

According to https://github.com/scala-exercises/exercises-scalatutorial/blob/master/src/main/scala/scalatutorial/sections/StandardLibrary.scala#L72-L97

val nums = Nil.::(4).::(3).::(2).::(1)

nums match {
  // Lists of `Int` that starts with `1` and then `2`
  case 1 :: 2 :: xs => …
  // Lists of length 1
  case x :: Nil => …
  // Same as `x :: Nil`
  case List(x) => …
  // The empty list, same as `Nil`
  case List() =>
  // A list that contains as only element another list that starts with `2`
  case List(2 :: xs) => …
}

the last case of case List(2 :: xs) gives error because the inferred type is List[Int] but the case signature is expecting List[List[Int]]. Please correct me if I am wrong. If I am not then how should it be re-written?

type mismatch with example

Hi,

I am following this tutorial. Awesome work. However, I couldn't get this example working with the code provided.

trait Stream[+T] {
    def prepend[U >: T](elem: U) : Stream[U] = Stream.cons(elem, this)
}

Compiling the code above gives me error as following.

polymorphism.scala:15: error: type mismatch;
 found   : Stream[T]
 required: scala.collection.immutable.Stream[?]
           def prepend[U >: T](elem: U) : Stream[U] = Stream.cons(elem, this)

Did I miss something?

* def prepend [U >: T](elem: U): Stream[U] = Stream.cons(elem, this)

Unexpected value in Lazy Evaluation exercise

Dear Team,

One of the exercises in Lazy Evaluation section returns odd result:
image

Same function in tests:

class LazyEvaluationSpec extends RefSpec with Checkers {

  def `check lazy list range`(): Unit =
    check(Test.testSuccess(LazyEvaluation.llRangeExercise _, 3 :: HNil)

Factorial function seems to not work

I can't refer the variable result that's definitely in scope for the function for some reason.

Also, Looking at the source code, the code is actually different than the one shown here.

scala_exercises_factorial_error

I can't press run button

When i try to solve the exercise, i can't press run button, neither in the mobile browser nor in the computer browser.

That could be happening?

Apparent Error in Higher Order Functions section

https://github.com/scala-exercises/exercises-scalatutorial/blob/master/src/main/scala/scalatutorial/sections/HigherOrderFunctions.scala#L126-L138

If I try to run code like this, I get:

Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_162-ea).
Type in expressions for evaluation. Or try :help.

scala> { def f(x1: Int, x2: Int) = x1 + x2 ; f }
<console>:12: error: missing argument list for method f
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `f _` or `f(_,_)` instead of `f`.
       { def f(x1: Int, x2: Int) = x1 + x2 ; f }
                                             ^

scala> 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.