Giter VIP home page Giter VIP logo

scala-exercises'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.

scala-exercises's People

Contributors

47erbot avatar adrianrafo avatar alejandrohdezma avatar alexandrnikitin avatar andyscott avatar angoglez avatar antoniomateogomez avatar benfradet avatar calvellido avatar cblp avatar d1egoaz avatar dcy2003 avatar drmabulle avatar fada21 avatar franciscodr avatar hasumedic avatar idelpivnitskiy avatar israelperezglez avatar jorgegalindocruces avatar juanpedromoreno avatar justinelsberry avatar kiroco12 avatar mergify[bot] avatar mikela avatar netzwerg avatar padilo avatar peterneyens avatar rafaparadela avatar raulraja avatar scala-steward 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  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

scala-exercises's Issues

Shapeless exercises

We need a set of exercises about Shapeless so we can show it on our Presentation Demo in TypeLevel Summit.

Having a set of 5 exercises is fair enough for the demo, we'll add more exercises in the future.

Add license footer/header

We need to add a license text to all our files.

@raulraja, a couple of questions to answer before considering it ready to work on:

  • What's the text? Do we have a template? What kind of license are we going to set?
  • Are we going to add it as a footer or as a header?

Create a basic README file

We need to have a README file where we explain how to compile/build the project.
Bear in mind that we will change master with our current V2 branch, so our current README file will be out of date.

Create new GitHub Application

We should create new GitHub application with the 47 account, in order to have more appropriate app_id/secret in the production environment.

Also we should define a properly Authorization callback URL in order to enable oAuth in production.

Extra: This should be created here

Create Cats exercises

We should start working on create the library Cats, organize its sections and add some exercises.

Integrate Codacy

It would be interesting having Codacy reviews in our pull request.

Prod / Test Interpreters

Free algebras will have both Production and test interpreters. Prod interpreters lift the Free monads/aplicatives to an effect capturing monad such as scalaz.concurrent.Task whereas test interpreters will mock persistence, etc... and use cats.Id when lifting the algebras.

Add "Progress" persistence layer

We should be able to:

  • Save/Update the progress: userId, library, section, method, args, state (succeeded/failed) and date
  • Given an userId, library, section, retrieve all the methods and their args (also the state could be useful).
  • Given an userId and a library, retrieve the percentage of exercises completed.

Put exercise-related routes under an URL fragment

The URL path for exercises is under GET /:librayName/, since the library name is matched to an arbitrary string on the first fragment of the URL it acts like a wildcard for URLs that don't match previous routes.

Boolean coercion uses `false` as the default case and it shouldn't

When coercing user-submitted inputs to boolean (for example in the "Literal Booleans" exercise), strings that are converted to boolean will be false by default unless they are the text "true". Coercing a string to a boolean should ensure that the input is either "true" or "false" and fail otherwise.

This means that the inputs that are expected to be false will be considered as good even when containing bogus strings that aren't "true".

Block/Implementation style exercises

Exercises that require longer code blocks should be allowed.
For example when showing how to implement typeclasses a koan style approach may not be appropriate and the user should be able to provide a longer code style impl.

Out of order

The order doesn't make sense. For example: Classes, then Option, then Objects?

Koan style exercise

Exercises should be able to include simple placeholders such as:

/** <code>var</code> parameters in class definition define getter and setter:
    *
    * ###Something to set where the description ends and where the explanation starts.###
    *
    * You can define class with private fields:
    *
    * {{{
    * class ClassWithPrivateFields(name: String)
    * val aClass = new ClassWithPrivateFields(\"name\")
    * }}}
    *
    * NOTE: <code>aClass.name</code> is not accessible
    */
  def classWithVarParameter(res0: String, res1: String): ExerciseResult[Unit] = ExerciseRunner("Class With Var Parameter") {

    class ClassWithVarParameter(var description: String)

    val aClass = new ClassWithVarParameter("Flying character")
    aClass.description should be(res0)

    aClass.description = "Flying white character"
    aClass.description should be(res1)
  }(∞)

Evaluate if resX convention is the best approach.

Create Heroku app

We need a new application in heroku like that: https://scala-exercises.herokuapp.com/

sameElements compares the elements, not just the number of elements

http://scala-exercises.47deg.com/koans#iterables says that "sameElements will return true if the two iterables have the same number of elements"

That's a necessary, but not sufficient condition. The elements have to be the same, too. Whether the ordering matters depends on the collection:

scala> List(1,2,3) sameElements List(1,2,3)
res13: Boolean = true

scala> List(1,2,3) sameElements List(3,2,1)
res14: Boolean = false

scala> Set(1,2,3) sameElements Set(3,2,1)
res15: Boolean = false // WHAT?

scala> Set(1,2,3,4,5,6) sameElements Set(6,5,4,3,2,1)
res16: Boolean = true

Also, sameElements for Set produces unexpected results - the behaviour changes depending on the size of the Set, as you can see above! This is arguably a bug in the Scala library: https://groups.google.com/forum/#!topic/scala-debate/je4vZbR3WbA

Exercise parser: Move the explanation into the description

Some exercises have and convenient explanation after the code, that are taken from a 'scalaDoc` within the code. I think that we should move the explanation to the same scaladoc of description, something like that:

From:

  /** <code>var</code> parameters in class definition define getter and setter:
    */
  def classWithVarParameter(res0: String, res1: String): ExerciseResult[Unit] = ExerciseRunner("Class With Var Parameter") {

    class ClassWithVarParameter(var description: String)

    val aClass = new ClassWithVarParameter("Flying character")
    aClass.description should be(res0)

    aClass.description = "Flying white character"
    aClass.description should be(res1)

    /** You can define class with private fields:
      *
      * {{{
      * class ClassWithPrivateFields(name: String)
      * val aClass = new ClassWithPrivateFields(\"name\")
      * }}}
      *
      * NOTE: <code>aClass.name</code> is not accessible
      */
  }(∞)

To:

  /** <code>var</code> parameters in class definition define getter and setter:
    *
    * ###Something to set where the description ends and where the explanation starts.###
    *
    * You can define class with private fields:
    *
    * {{{
    * class ClassWithPrivateFields(name: String)
    * val aClass = new ClassWithPrivateFields(\"name\")
    * }}}
    *
    * NOTE: <code>aClass.name</code> is not accessible
    */
  def classWithVarParameter(res0: String, res1: String): ExerciseResult[Unit] = ExerciseRunner("Class With Var Parameter") {

    class ClassWithVarParameter(var description: String)

    val aClass = new ClassWithVarParameter("Flying character")
    aClass.description should be(res0)

    aClass.description = "Flying white character"
    aClass.description should be(res1)
  }(∞)

Show code blocks with proper indentation

Exercises are showing up without proper indentation. Indentation of all exercises even those provided by third parties should be automatically formatted with something like scalariform to provide users with a consistent experience.

Consolidate Cats and Scalaz versions

We should make sure we are using the latest and greatest versions of both of these libraries. IMHO-- we should ideally just use one or the other-- and it seems we're migrating towards Cats.

At the very least, I'd like to adjust the builds so that Scalaz and Cats versions are shared across both core and site. Maybe common dependencies/versions should be defined in common/BuildCommon.scala or common/Dependencies.scala?

Thoughts?

Enhance test cases for method code extraction

Enhance the test cases that show how method code extraction should work. Then we can more easily seek help for the best way to fully identify the block of code that belongs to a given tree.

Add GitHub user authentication

The current Github integration and login came from an example which is far from clean. We need to integrate the login ops as its own algebra in the context Free as we have done with other Biz logic ops.

Make tests run and pass in the CI environment

We are getting strange sbt exceptions on Travis CI that cause the process to exit with a non-zero status code and fail the build. As it can be seen in this build even when the tests are passing failure is caused by an sbt issue with the following stacktrace:

Exception in thread "Thread-21" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.net.SocketInputStream.read(SocketInputStream.java:203)
    at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2296)
    at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:2589)
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2599)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at sbt.React.react(ForkTests.scala:114)
    at sbt.ForkTests$$anonfun$mainTestTask$1$Acceptor$2$.run(ForkTests.scala:74)
    at java.lang.Thread.run(Thread.java:745)

V2 Backend

Placeholder to track parsing, evaluation and discovery of exercises in the Classpath

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.