Added alt foldLeft algo for Pi calculation in tutorial
This commit is contained in:
parent
ca1fc49758
commit
e58aae5f59
3 changed files with 6 additions and 4 deletions
|
|
@ -56,12 +56,15 @@ object Pi extends App {
|
|||
// ==================
|
||||
class Worker() extends Actor {
|
||||
// define the work
|
||||
|
||||
// FIXME tail-recursive fun instead
|
||||
val calculatePiFor = (arg: Int, nrOfElements: Int) => {
|
||||
val range = (arg * nrOfElements) to ((arg + 1) * nrOfElements - 1)
|
||||
var acc = 0.0D
|
||||
range foreach (i => acc += 4 * math.pow(-1, i) / (2 * i + 1))
|
||||
acc
|
||||
//range map (j => 4 * math.pow(-1, j) / (2 * j + 1)) sum
|
||||
// Use this for more functional style but is twice as slow
|
||||
// range.foldLeft(0.0D)( (acc, i) => acc + 4 * math.pow(-1, i) / (2 * i + 1) )
|
||||
}
|
||||
|
||||
def receive = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue