【问题标题】:creating a function, that takes a function as a paramter and calls it on each linkedlist item创建一个函数,将函数作为参数并在每个链表项上调用它
【发布时间】:2019-03-28 00:21:25
【问题描述】:

给定一个链表类,我们需要在这个类中创建一个函数,该函数将函数作为参数,并在链表的每个节点上调用该函数。我们可以递归或通过循环来完成它,我不知道为什么我不能让它工作。

目前我正在使用while循环,如果节点不等于null,则调用该节点上的函数,然后转到下一个节点。

   class LinkedListNode[A](var value: A, var next: LinkedListNode[A]) {
   def calleachnode(inputfunction : A => Unit): Unit = {
    var node = this
    while(node != null){
      inputfunction(node.value)
      node = node.next
    }

  }
}

【问题讨论】:

  • this 永远不会为 null。你的意思是node 吗?
  • 是的,抱歉,重新输入堆栈时出现错字。

标签: scala linked-list


【解决方案1】:

这种类型的任务最好用(尾)递归来完成(另外,永远不要使用var,除非你完全确定你为什么需要它......而且,null 是还有一些要避免的事情,请改用Option):

case class Node[A](value: A, next: Option[Node[A]]) {
   @tailrec
   final def calleachnode(f: A => Unit): Unit = {
     f(value)
     next match {
       case Some(next) => next.calleachnode(f)
       case None => ()

     }
   }
 }

【讨论】:

    【解决方案2】:

    这里有 3 种使用 Algebraic Data Types (ADT) 的可能性。

    首先是object-oriented 方式:

    sealed trait LinkedListNode[A] {
      def calleachnode(inputfunction : A => Unit): Unit 
    }
    
    case class BranchNode[A](value:A, next: LinkedListNode[A]) extends LinkedListNode[A] {
      def calleachnode(inputfunction : A => Unit): Unit = {
        inputfunction(value)
        next.calleachnode(inputfunction)
      }
    }
    
    case class LeafNode[A](value:A) extends LinkedListNode[A] {
      def calleachnode(inputfunction : A => Unit): Unit = {
        inputfunction(value)
      }
    }
    

    或者,如果您愿意,也可以使用模式匹配:

    sealed trait LinkedListNode[A] {
    
      def calleachnode(inputfunction : A => Unit): Unit = this match {
        case BranchNode(value, next) => 
            inputfunction(value)
            next.calleachnode(inputfunction)
        case LeafNode(value) =>
            inputfunction(value)
      }
    }
    
    case class BranchNode[A](value:A, next: LinkedListNode[A]) extends LinkedListNode[A]
    
    case class LeafNode[A](value:A) extends LinkedListNode[A]
    

    您可以使用以下方法测试这些解决方案:

     val lln = BranchNode(12, LeafNode(2))
     lln.calleachnode((i) => println(i * 2))
    

    现在有更多functional 使用ADT 的方式:

    sealed trait LinkedListNode[A] 
    
    case class BranchNode[A](value:A, next: LinkedListNode[A]) extends LinkedListNode[A]
    
    case class LeafNode[A](value:A) extends LinkedListNode[A]
    
    
    def calleachnode[A](listNode: LinkedListNode[A], inputfunction : A => Unit): Unit = listNode match {
      case BranchNode(value, next) => 
        inputfunction(value)
        calleachnode(next, inputfunction)
      case LeafNode(value) =>
        inputfunction(value)
    } 
    

    测试看起来有点不同:

    val lln = BranchNode(12, LeafNode(2))
    calleachnode[Int](lln, (i) => println(i * 2)) 
    

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多