Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More OOP
- // - dynamic polymorphism
- // - inheritance
- // - subtyping
- // - encapsulation
- // trait aka abstract class
- trait Iterator[A] {
- def hasNext: Boolean
- def next(): A
- }
- // those are called stubs, you can have them in abstract class or trait but not in a class
- // they just sorta hold shit for when you define them in subclasses
- class ListIterator[A](list: List[A]) extends Iterator[A] {
- private var cur = list
- def hasNext: Boolean = cur.nonEmpty
- def next(): A = {
- val item = cur.head
- cur = cur.tail
- item
- }
- }
- class ArrayIterator[A](arr: Array[A]) extends Iterator[A] {
- private var i = 0
- def hasNext: Boolean = i < arr.length
- def next(): A = {
- val item = arr(i)
- i += 1
- item
- }
- }
- val iter = ...
- while(iter.hasNext) print(iter.next())
- // Algorithms Data Structures
- // ---------- ---------------
- // printall arrays
- // reverse lists
- // map/filter BSTs
- // adding files
- // findFirst strings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement