Skip to content

Commit

Permalink
Merge pull request #47 from andreas-roehler/master
Browse files Browse the repository at this point in the history
  • Loading branch information
winitzki authored Dec 28, 2023
2 parents e157002 + 4205206 commit 7a3fdb0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
25 changes: 25 additions & 0 deletions chapter01/worksheets/solution1.1.1.1_as_object_AR.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** author: Andreas Röhler */

/**
Factorial of 10
Find the product of integers from 1 to 10 (the factorial of 10).
*/

object MyFactorial {
def myFactorial(a: Int): Int = {
(1 to a).foldRight(1)(_ * _)
}
def main(args: Array[String]) {
val expected = 3628800
val result = this.myFactorial(10)
println("result: %s".format(result))
assert(result == expected)
}
}

MyFactorial.main(Array())

// scala> :load solution1.1.1.1_as_object_AR.scala
// Loading solution1.1.1.1_as_object_AR.scala...
// defined object MyFactorial
// result: 3628800
23 changes: 23 additions & 0 deletions chapter01/worksheets/solution1.1.1.1_function_AR.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** author: Andreas Röhler */

/**
Factorial of 10
Find the product of integers from 1 to 10 (the factorial of 10).
*/

def myFactorial(a: Int): Int = {
(1 to a).foldRight(1)(_ * _)
}

val result = myFactorial(10)
println("result: %s".format(result))

val expected = 3628800
assert(result == expected)

// scala> :load solution1.1.1.1_function_AR.scala
// Loading solution1.1.1.1_function_AR.scala...
// myFactorial: (a: Int)Int
// result: Int = 3628800
// result: 3628800
// expected: Int = 3628800

0 comments on commit 7a3fdb0

Please sign in to comment.