2016-11-25 55 views
2

我完全新的函數式編程,我使用Scala工作的關鍵。我目前正在爲我的大學課程寫一個課程。查找地圖,其值滿足功能

我已經輸入以下地圖:

val mapdata = Map(
    "SK1" -> List(9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1), 
    "SK2" -> List(0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1), 
    "SK3" -> List(8, 7, 1, 8, 0, 5, 8, 3, 5, 9, 7, 5, 4, 7, 9, 8, 1, 4, 6, 5, 6, 6, 3, 6, 8, 8, 7, 4, 0, 6), 
    "SK4" -> List(2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 9), 
    "SK5" -> List(2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3), 
    "SK6" -> List(2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9), 
    "SK7" -> List(6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0), 
    "SK8" -> List(2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6), 
    "SK9" -> List(7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6) 
    ) 

我想輸出的關鍵(股票代號),其中包含的最大增幅。

我設法使用下面的代碼以找到最大增幅:

def mostRecent(Is:List[Int]): Int = { 
     if (Is.tail == Nil) 
     return Is.head 
     else 
     mostRecent(Is.tail) 
    } 

    def penultimate(x: List[Int]): Int = x(x.length - 2) 

    //this definition allow me to subtract the mostRecentValues and the penultimate values 
    def subtractLast2(pen: Iterable[Int], last: Iterable[Int]): Iterable[Int] = { 
     pen zip last map(x => x._1 - x._2) 
    } 

    //outputs a list with containing the last values 
    val MostRecentPrices = mapdata.values.map(x => mostRecent(x)) 

    //outputs a list with containing the second last values 
    val penultimatePrices = mapdata.values.map(x => penultimate(x)) 

    //determines the maximum increase 
    val maxIncrease = (subtractLast2(MostRecentPrices, penultimatePrices)).max 


    //output the stock that has increased the most in the last day of the period 
    println("MaxIncrease = " + maxIncrease) 

我以爲我只是在那裏,直到我發現我不得不輸出與計算出的最大增加對應的關鍵。

在考慮使用getOrElse的,但真的不知道,因爲我在Scala和函數式編程初學者。

我希望這是有道理的,請讓我知道如果我需要澄清什麼。

由於

+0

啊,我們最近看到很多SKn數據。很高興知道這是一門課程。我公司可提供的代碼,但如果你研究了API文檔一點會更好(比如,你的'mostRecent(X)''是x.last'和'penultimate'是'x.init.last')。另外,你所需增加的定義是否真的只是最後一天的增長?相反,如果它是任何一天中漲幅最大的,你應該看看「滑動」。還有一個'筆拉鍊的最後一個貼圖(X => x._1 - x._2)'只是'最後 - pen',(嗯,包裹在一個'Iterable'),不知道爲什麼你的房子周圍有打算。 –

回答

3

可以計算通過使用圖案匹配最後兩個元件:

def mostRecent(is: List[Int]): (Int, Int) = 
    is match { 
    case a +: b +: Nil => (a, b) 
    case head +: tail => mostRecent(tail) 
    case Nil => throw new Exception("Can't calculate for an empty list") 
    } 

首先,它在a +: b +: Nil其提取兩個第一元件如果尾巴是列表的末尾匹配。如果它不能匹配這種情況,它會嘗試將列表分解爲head +:tail,因此它可以對尾部進行遞歸調用。如果它首先被一個空列表調用,它將引發一個異常。

然後,對於mapdata每個條目就可以計算出最近的兩個元素,並減去他們:

val increases = 
    for { 
    (k, v) <- mapdata 
    (a, b) = mostRecent(v) 
    } yield (k, b - a) 

作爲最後一步,你可以找到最大使用maxBy方法:

val max = increases.maxBy(_._2) 
max: (String, Int) = ("SK4", 7)