在纯函数中使用var处理scala代码。这在没有var的情况下是可能的吗?

在纯函数中使用var处理scala代码。这在没有var的情况下是可能的吗?,scala,functional-programming,Scala,Functional Programming,是否有可能(甚至值得)尝试在没有var的情况下编写下面的代码块?它与var一起工作。这不是为了面试,这是我第一次尝试scala(来自java) 问题是:让人尽可能靠近剧院的前面,同时将每个请求(如琼斯,4张票)保留在一个剧院区。剧院部分从前面开始,大小为6、6、3、5、5。。。等等我试图通过将所有潜在的票务请求组放在一起,然后选择每个部分中最合适的组来实现这一点 这些是课程。seatingcomposition是SeatingRequest(仅ID)和其票数总和的一种可能组合: 当我们到达下面的

是否有可能(甚至值得)尝试在没有var的情况下编写下面的代码块?它与var一起工作。这不是为了面试,这是我第一次尝试scala(来自java)

问题是:让人尽可能靠近剧院的前面,同时将每个请求(如琼斯,4张票)保留在一个剧院区。剧院部分从前面开始,大小为6、6、3、5、5。。。等等我试图通过将所有潜在的票务请求组放在一起,然后选择每个部分中最合适的组来实现这一点

这些是课程。seatingcomposition是SeatingRequest(仅ID)和其票数总和的一种可能组合:

当我们到达下面的函数时。。。 1.)所有可能的座位组合请求都在座位组合列表中,并按降序排列。 2.)所有戏剧表演都按顺序列出

def getSeatingMap(groups: List[SeatingCombination], sections: List[TheatreSection]): HashMap[Int, TheatreSection] = {
    var seatedMap = new HashMap[Int, TheatreSection]
    for (sect <- sections) {
      val bestFitOpt = groups.find(g => { g.seatCount <= sect.sectionSize && !isAnyListIdInMap(seatedMap, g.idList) })
      bestFitOpt.filter(_.idList.size > 0).foreach(_.idList.foreach(seatedMap.update(_, sect)))
    }
    seatedMap 
}

def isAnyListIdInMap(map: HashMap[Int, TheatreSection], list: List[Int]): Boolean = {
    (for (id <- list) yield !map.get(id).isEmpty).reduce(_ || _)
  }
def getSeatingMap(组:List[seatingcomposition],节:List[theatheresection]):HashMap[Int,theatheresection]={
var seatedMap=newhashmap[Int,TheatreSection]
for(sect{g.seatCount 0).foreach(u.idList.foreach(seatedMap.update(u,sect)))
}
座位地图
}
def ISANYLISTINDIMAP(映射:HashMap[Int,Theatheresection],列表:列表[Int]):布尔={

(对于(id您可以使用
foldLeft
在运行状态下迭代
部分
(同样,在内部,在您的状态下迭代添加部分中的所有id):

}

list.exists(谓词:Int=>Boolean)
是一个布尔值,如果
列表中的任何元素的谓词为true,则该布尔值为true

map.contains(key)
检查
map
是否在
key
处定义

如果希望更简洁,则不需要为谓词的参数命名:

list.exists(map.contains)

下面是我如何在不使用mutable.HashMap的情况下实现的,注释建议使用
foldLeft
来实现这一点:

class SeatingCombination(val idList: List[Int], val seatCount: Int){}
class SeatingRequest(val id: Int, val partyName: String, val ticketCount: Int){}
class TheatreSection(val sectionSize: Int, rowNumber: Int, sectionNumber: Int) {
  def id: String = rowNumber.toString + "_"+ sectionNumber.toString;
}

def getSeatingMap(groups: List[SeatingCombination], sections: List[TheatreSection]): Map[Int, TheatreSection] = {
  sections.foldLeft(Map.empty[Int, TheatreSection]) { (m, sect) =>
    val bestFitOpt = groups.find(g => {
      g.seatCount <= sect.sectionSize && !isAnyListIdInMap(m, g.idList)
    }).filter(_.idList.nonEmpty)

    val newEntries = bestFitOpt.map(_.idList.map(_ -> sect)).getOrElse(List.empty)
    m ++ newEntries
  }
}

def isAnyListIdInMap(map: Map[Int, TheatreSection], list: List[Int]): Boolean = {
  (for (id <- list) yield map.get(id).isDefined).reduce(_ || _)
}
类座位组合(val idList:List[Int],val seatCount:Int){
类SeatingRequest(val-id:Int,val-partyName:String,val-ticketCount:Int){}
班级戏剧节(val sectionSize:Int,rowNumber:Int,sectionNumber:Int){
def id:String=rowNumber.toString+“”+sectionNumber.toString;
}
def getSeatingMap(组:列表[SeatingCombination],节:列表[TheatreSection]):映射[Int,TheatreSection]={
sections.foldLeft(Map.empty[Int,theateresection]){(m,sect)=>
val bestFitOpt=groups.find(g=>{
g、 seatCount sect().getOrElse(List.empty)
m++新条目
}
}
def ISANYLISTINDIMAP(映射:映射[Int,剧院区]、列表:列表[Int]):布尔={

(对于(id只需将
var
更改为
val
即可:) 我认为,您可能是在询问如何去掉可变映射,而不是
var
(代码中不需要
var

像其他答案所建议的那样,这类内容通常在scala中递归编写或使用
foldLeft
。下面是一个递归版本:

  @tailrec
  def getSeatingMap(
    groups: List[SeatingCombination], 
    sections: List[TheatreSection], 
    result: Map[Int, TheatreSection] = Map.empty): Map[Int, TheatreSection] = sections match {
    case Nil => result
    case head :: tail => 
      val seated = groups
        .iterator
        .filter(_.idList.nonEmpty)
        .filterNot(_.idList.find(result.contains).isDefined)     
        .find(_.seatCount <= head.sectionSize)
        .fold(Nil)(_.idList.map(id => id -> sect))
      getSeatingMap(groups, tail, result ++ seated)
  }

这看起来像是foldLeft的工作。
list.exists(map.contains)
class SeatingCombination(val idList: List[Int], val seatCount: Int){}
class SeatingRequest(val id: Int, val partyName: String, val ticketCount: Int){}
class TheatreSection(val sectionSize: Int, rowNumber: Int, sectionNumber: Int) {
  def id: String = rowNumber.toString + "_"+ sectionNumber.toString;
}

def getSeatingMap(groups: List[SeatingCombination], sections: List[TheatreSection]): Map[Int, TheatreSection] = {
  sections.foldLeft(Map.empty[Int, TheatreSection]) { (m, sect) =>
    val bestFitOpt = groups.find(g => {
      g.seatCount <= sect.sectionSize && !isAnyListIdInMap(m, g.idList)
    }).filter(_.idList.nonEmpty)

    val newEntries = bestFitOpt.map(_.idList.map(_ -> sect)).getOrElse(List.empty)
    m ++ newEntries
  }
}

def isAnyListIdInMap(map: Map[Int, TheatreSection], list: List[Int]): Boolean = {
  (for (id <- list) yield map.get(id).isDefined).reduce(_ || _)
}
  @tailrec
  def getSeatingMap(
    groups: List[SeatingCombination], 
    sections: List[TheatreSection], 
    result: Map[Int, TheatreSection] = Map.empty): Map[Int, TheatreSection] = sections match {
    case Nil => result
    case head :: tail => 
      val seated = groups
        .iterator
        .filter(_.idList.nonEmpty)
        .filterNot(_.idList.find(result.contains).isDefined)     
        .find(_.seatCount <= head.sectionSize)
        .fold(Nil)(_.idList.map(id => id -> sect))
      getSeatingMap(groups, tail, result ++ seated)
  }
    @tailrec
    def selectGroup(
      sect: TheatreSection, 
      groups: List[SeatingCombination], 
      result: List[SeatingCombination] = Nil
    ): (List[(Int, TheatreSection)], List[SeatingCombination]) = groups match {
     case Nil => (Nil, result)
     case head :: tail 
       if(head.idList.nonEmpty && head.seatCount <= sect.sectionSize) => (head.idList.map(_ -> sect), result.reverse ++ tail)
     case head :: tail => selectGroup(sect, tail, head :: result)
  }
   ...
   case head :: tail => 
     val(seated, remaining) => selectGroup(sect, groups)
     getSeatingMap(remaining, tail, result ++ seated)