Skip to content

Commit 0945869

Browse files
author
Alejandro Gómez
committed
👶
0 parents  commit 0945869

File tree

84 files changed

+6873
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+6873
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
project/project
2+
project/target
3+
target
4+
.idea
5+
.tmp
6+
7+
*.iml
8+
/out
9+
.idea_modules
10+
.classpath
11+
.project
12+
/RUNNING_PID
13+
.settings
14+
.sass-cache
15+
scalajvm/upload/*
16+
17+
# temp files
18+
.~*
19+
*~
20+
*.orig
21+
22+
# eclipse
23+
.scala_dependencies
24+
.buildpath
25+
.cache
26+
.target
27+
bin/
28+
.ensime
29+
.ensime_cache
30+
31+
# OSX
32+
.DS_Store

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: scala
2+
scala:
3+
- 2.11.7
4+
jdk:
5+
- oraclejdk8

build.sbt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
lazy val stdlib = (project in file("."))
2+
.enablePlugins(ExerciseCompilerPlugin)
3+
.settings(
4+
organization := "org.scalaexercises",
5+
name := "content-stdlib",
6+
scalaVersion := "2.11.7",
7+
version := "0.0.0-SNAPSHOT",
8+
resolvers ++= Seq(
9+
Resolver.sonatypeRepo("snapshots")
10+
),
11+
libraryDependencies ++= Seq(
12+
"com.chuusai" %% "shapeless" % "2.2.5",
13+
"org.scalatest" %% "scalatest" % "2.2.4",
14+
"org.scalaexercises" %% "runtime" % "0.0.0-SNAPSHOT" changing(),
15+
"org.scalaexercises" %% "definitions" % "0.0.0-SNAPSHOT" changing(),
16+
"org.scalacheck" %% "scalacheck" % "1.12.5",
17+
"com.github.alexarchambault" %% "scalacheck-shapeless_1.12" % "0.3.1"
18+
)
19+
)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=0.13.9

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("org.scalaexercises" % "sbt-exercise" % "0.0.0-SNAPSHOT", "0.13", "2.10")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defaultLib.Library_cats$1$
2+
defaultLib.Library_shapeless$1$
3+
defaultLib.Library_stdlib$1$

src/main/scala/stdlib/Asserts.scala

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package stdlib
2+
3+
import org.scalatest._
4+
5+
/** @param name asserts
6+
*/
7+
object Asserts extends FlatSpec with Matchers with exercise.Section {
8+
9+
/** ScalaTest makes three assertions available by default in any style trait. You can use:
10+
*
11+
* - `assert` for general assertions;
12+
* - `assertResult` to differentiate expected from actual values;
13+
* - `intercept` to ensure a bit of code throws an expected exception.
14+
*
15+
* In any Scala program, you can write assertions by invoking `assert` and passing in a `Boolean` expression:
16+
*
17+
* {{{
18+
* val left = 2
19+
* val right = 1
20+
* assert(left == right)
21+
* }}}
22+
*
23+
* If the passed expression is `true`, `assert` will return normally. If `false`,
24+
* Scala's `assert` will complete abruptly with an `AssertionError`. This behavior is provided by
25+
* the `assert` method defined in object `Predef`, whose members are implicitly imported into every Scala source file.
26+
*
27+
*
28+
* ScalaTest provides a domain specific language (DSL) for expressing assertions in tests
29+
* using the word `should`. ScalaTest matchers provides five different ways to check equality, each designed to address a different need. They are:
30+
*
31+
* {{{
32+
* result should equal (3) // can customize equality
33+
* result should === (3) // can customize equality and enforce type constraints
34+
* result should be (3) // cannot customize equality, so fastest to compile
35+
* result shouldEqual 3 // can customize equality, no parentheses required
36+
* result shouldBe 3 // cannot customize equality, so fastest to compile, no parentheses required
37+
* }}}
38+
*
39+
* Come on, your turn: true and false values can be compared with should matchers
40+
*/
41+
def scalaTestAsserts(res0: Boolean) {
42+
true should be(res0)
43+
}
44+
45+
/** Booleans in asserts can test equality.
46+
*/
47+
def booleansAsserts(res0: String) {
48+
val v1 = 4
49+
val v2 = 4
50+
v1 === res0
51+
52+
/** `===` is an assert. It is from ScalaTest, not from the Scala language. */
53+
}
54+
55+
/** Sometimes we expect you to fill in the values
56+
*/
57+
def valuesAsserts(res0: Int) {
58+
assert(res0 == 1 + 1)
59+
}
60+
61+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package stdlib
2+
3+
import org.scalatest._
4+
5+
/** @param name byname_parameter
6+
*/
7+
object ByNameParameter extends FlatSpec with Matchers with exercise.Section {
8+
9+
/** `() => Int` is a Function type that takes a `Unit` type. `Unit` is known as `void` to a Java programmer. The function returns an `Int`. You can place this as a method parameter so that you can you use it as a block, but still it doesn't look quite right.
10+
*/
11+
def takesUnitByNameParameter(res0: Either[Throwable, Int]) {
12+
def calc(x: () Int): Either[Throwable, Int] = {
13+
try {
14+
Right(x()) //An explicit call the x function
15+
} catch {
16+
case b: Throwable Left(b)
17+
}
18+
}
19+
20+
val y = calc { () //Having explicitly declaring that Unit is a parameter with ()
21+
14 + 15
22+
}
23+
24+
y should be(res0)
25+
}
26+
27+
/** A by-name parameter does the same thing as the previous koan but there is no need to explicitly handle `Unit` or `()`. This is used extensively in scala to create blocks.
28+
*/
29+
def byNameParameter(res0: Either[Throwable, Int]) {
30+
def calc(x: Int): Either[Throwable, Int] = {
31+
//x is a call by name parameter
32+
try {
33+
Right(x)
34+
} catch {
35+
case b: Throwable Left(b)
36+
}
37+
}
38+
39+
val y = calc {
40+
//This looks like a natural block
41+
println("Here we go!") //Some superfluous call
42+
val z = List(1, 2, 3, 4) //Another superfluous call
43+
49 + 20
44+
}
45+
46+
y should be(res0)
47+
}
48+
49+
/** By name parameters can also be used with an *Object* and apply to make interesting block-like calls
50+
*/
51+
def withApplyByNameParameter(res0: String) {
52+
object PigLatinizer {
53+
def apply(x: String) = x.tail + x.head + "ay"
54+
}
55+
56+
val result = PigLatinizer {
57+
val x = "pret"
58+
val z = "zel"
59+
x ++ z //concatenate the strings
60+
}
61+
62+
result should be(res0)
63+
}
64+
65+
}

0 commit comments

Comments
 (0)