2016-11-25 57 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)