2010-01-25 14 views

उत्तर

26

आप Iterator है, जो कि आप next और hasNext तरीकों को लागू करने की आवश्यकता होगी का विस्तार कर सकते हैं:

class MyAnswer extends Iterator[Int] { 
    def hasNext = true 
    def next = 42 
    } 

लेकिन, यदि आप Iterable का विस्तार है, जो आपको 2.8 में elements (या iterator लागू की आवश्यकता है आप और अधिक लचीलापन मिल जाएगा):

class MyAnswer extends Iterable[Int] { 
    def iterator = new Iterator[Int] { 
     def hasNext = true 
     def next = 42 
    } 
    } 

एक आम मुहावरा, कुछ निजी संग्रह के लिए एक इटरेटर बेनकाब करने के लिए इस तरह प्रतीत हो रहा है:

class MyStooges extends Iterable[String] { 
    private val stooges = List("Moe", "Larry", "Curly") 
    def iterator = stooges.iterator 
    } 
+0

निजी संदेश लेकिन मैं एक सवाल खड़ा करना चाहते हैं भेजने के लिए एक तरह से वहाँ is't: आप शायद मुझे आम मुहावरा तुम उल्लेख के उपयोग को इंगित कर सकता है? यदि नहीं, तो यह किसके लिए उपयोगी हो सकता है? क्यों न केवल सूची वापस? क्या यह मुहावरे कम कुशल नहीं होगा? (इसके अलावा: मैंने देखा है कि Iterable [ए] "चाल" कुछ बार देखा गया है और यह कुछ संग्रह बनाने के सबसे तेज़ तरीकों में से एक प्रतीत होता है, जैसे इस दृष्टिकोण के लिए कोई "विकल्प" है? मुझे लगता है क्योंकि इटरेटर देता है छोटी जानकारी इसलिए तरीकों को अच्छी तरह से अनुकूलित नहीं किया जा सकता है, अगर मुझे पता था कि मेरा छद्म कॉल रिटर्न आदेश देता है या तेजी से यादृच्छिक acces है) – Aktau

7

एक विधि के लिए, बस yield:

def odd(from: Int, to: Int): List[Int] = 
    for (i <- List.range(from, to) if i % 2 == 1) yield i 
+4

सही, लेकिन ... कोड उदाहरण वास्तव में सवाल का जवाब नहीं देता है। बस "इटरेटर" के साथ "सूची" के दोनों उदाहरणों को प्रतिस्थापित करें और यह पूरी तरह से काम करता है! –

0

इन दो सवालों के जवाब नीचे पोस्ट और धन्यवाद @Dima से मदद की थी।

चलें मान लें कि आपके एक वर्ग जुड़ा हुआ सूची है। और आवश्यकता सूची में सभी तत्वों को मुद्रित करना है।

trait LinkedList { 
    def nodeValue: Int 
    def tailList: LinkedList 
} 

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList 

object Nil extends LinkedList { 
    def nodeValue = throw new IllegalAccessException("head of Nil") 
    def tailList = throw new IllegalAccessException("tail of Nil") 
} 

val singleLinkedList = new Node(1,Nil) 
val chainedLinkedList = new Node(2,singleLinkedList) 
print(chainedLinkedList) 
[email protected]: Unit =() 

अब इस कक्षा में पुनरावर्तक को लागू करने दें।

trait LinkedList extends Iterator[Int]{ 
    def nodeValue: Int 
    def tailList: LinkedList 
} 

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList { 
    var ptr: LinkedList = this 

    //The following two are mandatory for extending Iterator 
    override def hasNext: Boolean = ptr match { case Nil => false; case _=> true} 

    override def next(): Int = { 
    val result = ptr.nodeValue 
    ptr = ptr.tailList 
    result 
    } 
} 

object Nil extends LinkedList { 
    def nodeValue = throw new IllegalAccessException("head of Nil") 
    def tailList = throw new IllegalAccessException("tail of Nil") 

    //The following two are mandatory for extending Iterator 
    override def hasNext: Boolean = false 
    override def next(): Int = throw new IllegalAccessException("next of Nil") 
} 

val singleLinkedList = new Node(1,Nil) 
val chainedLinkedList = new Node(2,singleLinkedList) 

//Printing this first Time 
chainedLinkedList.foreach(println) 
//Prints 2 1 

//Printing second Time 
chainedLinkedList.foreach(println) 
//No output 

इटरेटर कार्यान्वयन में, एक बार ptr अंत पर पहुंच गया, इसे वापस अग्रिम नहीं किया सकता है। इटेरेबल कार्यान्वयन यह हल करता है।

trait LinkedList extends Iterable[Int]{ 
    val nodeValue: Int 
    val tailList: LinkedList 
    override def toString(): String = this.mkString(" -> ") 
} 

class Node(val nodeValue: Int, val tailList: LinkedList) extends LinkedList { 

    override def iterator: Iterator[Int] = Iterator 
    .iterate(this: LinkedList)(_.tailList) 
    .takeWhile(_ != Nil) 
    .map(_.nodeValue) 
} 

object Nil extends LinkedList { 
    lazy val nodeValue= throw new IllegalAccessException("head of Nil") 
    lazy val tailList = throw new IllegalAccessException("tail of Nil") 

    override def iterator: Iterator[Int] = Iterator.empty 
} 

val singleLinkedList = new Node(1,Nil) 
val chainedLinkedList = new Node(2,singleLinkedList) 

//Printing this first Time 
chainedLinkedList.foreach(println) 
Output 2 -> 1 
chainedLinkedList.foreach(println) 
Output 2 -> 1 
संबंधित मुद्दे