Skip to main content
added 17 characters in body
Source Link
Alex L
  • 5.8k
  • 2
  • 26
  • 69

I'm a Scala beginner, so it'd be great if anyone would be so kind andto give some feedback.

Right now, I would like to get toa Scala mindset, and get to know the tricks and tips. I have a background in PHP and JavaScript, and Scala is quite harder to learn, but way more elegant, so I want to master it!

I'm Scala beginner, so if anyone would be so kind and give some feedback.

Right now, I would like to get to Scala mindset, get to know tricks and tips. I have background in PHP and JavaScript, and Scala is quite harder to learn, but way more elegant, so I want to master it

I'm a Scala beginner, so it'd be great if anyone would be so kind to give some feedback.

Right now, I would like to get a Scala mindset, and get to know the tricks and tips. I have a background in PHP and JavaScript, and Scala is quite harder to learn, but way more elegant, so I want to master it!

Tweeted twitter.com/#!/StackCodeReview/status/480572630517682176
More general implementation + example os usage
Source Link
Forex
  • 161
  • 4
package heap

import scala.collection.mutable.ListBuffer

class HeapHeap[T](compareFunction: (IntT, IntT) => Int) {

private val HIGHER = 1
private val LOWER = -1

private val heapRepresentation = new ListBuffer[Int]ListBuffer[T]()

private def lastIndex = heapRepresentation.size - 1

private def valueOf(index: Int): IntT = heapRepresentation(index)

def add(item: IntT): HeapHeap[T] = {
    heapRepresentation.append(item)
    bubbleUp(lastIndex)
    this
}

def swap(index1: Int, index2: Int) {
    val temp = valueOf(index1)
    heapRepresentation.update(index1, valueOf(index2))
    heapRepresentation.update(index2, temp)
}

def bubbleUp(currentIndex: Int) {
    def getParent(i: Int) = (i - 1) / 2

    if (currentIndex > 0) {
        val parentIndex = getParent(currentIndex)

        compareFunction(valueOf(currentIndex), valueOf(parentIndex)) match {
            case LOWER =>
                swap(currentIndex, parentIndex)
                bubbleUp(parentIndex)
            case _ =>
        }
    }
}

def bubbleDown(currentIndex: Int) {
    getLowerChild(currentIndex) match {
        case Some((lowerChildIndex, lowerChildValue)) =>
            if (compareFunction(valueOf(currentIndex), lowerChildValue) == HIGHER) {
                swap(currentIndex, lowerChildIndex)
                bubbleDown(lowerChildIndex)
            }
        case None =>
    }
}

def getLowerChild(index: Int): Option[(Int, IntT)] = {
    def getChildrenIndices(parentIndex: Int): (Int, Int) = (2 * parentIndex + 1, 2 * parentIndex + 2)

    val (leftChildIndex, rightChildIndex) = getChildrenIndices(index)

    val areChildrenInBoundsOfHeap = (rightChildIndex <= lastIndex) && (leftChildIndex <= lastIndex)
    if (!areChildrenInBoundsOfHeap) return None

    val (leftChildValue, rightChildValue) = (heapRepresentation(leftChildIndex), heapRepresentation(rightChildIndex))

    compareFunction(leftChildValue, rightChildValue) match {
        case LOWER => Some((leftChildIndex, leftChildValue))
        case _ => Some((rightChildIndex, rightChildValue))
    }
}

def pop: Option[Int]Option[T] = heapRepresentation.size match {
    case 0 => None
    case _ =>

        val firstValue = heapRepresentation(0)
        if (heapRepresentation.size == 1) {
            heapRepresentation.remove(0)
            return Some(firstValue)
        }
        swap(0, lastIndex)
        heapRepresentation.remove(lastIndex)
        bubbleDown(0)
        Some(firstValue)
}


override def toString = {
    s"[$heapRepresentation](${heapRepresentation.size})"
}

}

Usage of heap class

def compareInts(first: Int, second: Int): Int =
    if (first > second) 1
    else if (first == second) 0
    else -1

val heap = new Heap[Int](compareInts)

for (i <- 0 to 3) {
    heap.add((Math.random() * 100).toInt)
}
heap
heap.pop
heap.pop
heap.pop
heap.pop


def compareStrings(first: String, second: String): Int =
    if (first > second) 1
    else if (first == second) 0
    else -1
var heap2 = new Heap[String](compareStrings)
heap2.add("a")
heap2.add("cc")
heap2.add("")

heap2.pop
heap2.pop
heap2.pop
heap2.pop

Right now, I would like to get to Scala mindset, get to know tricks and tips. I have background in PHP and JavaScript, and Scala is quite harder to learn, but way more elegant, so I want to master it

package heap

import scala.collection.mutable.ListBuffer

class Heap(compareFunction: (Int, Int) => Int) {

private val HIGHER = 1
private val LOWER = -1

private val heapRepresentation = new ListBuffer[Int]()

private def lastIndex = heapRepresentation.size - 1

private def valueOf(index: Int): Int = heapRepresentation(index)

def add(item: Int): Heap = {
  heapRepresentation.append(item)
  bubbleUp(lastIndex)
  this
}

def swap(index1: Int, index2: Int) {
  val temp = valueOf(index1)
  heapRepresentation.update(index1, valueOf(index2))
  heapRepresentation.update(index2, temp)
}

def bubbleUp(currentIndex: Int) {
  def getParent(i: Int) = (i - 1) / 2

  if (currentIndex > 0) {
    val parentIndex = getParent(currentIndex)

    compareFunction(valueOf(currentIndex), valueOf(parentIndex)) match {
      case LOWER =>
        swap(currentIndex, parentIndex)
        bubbleUp(parentIndex)
      case _ =>
    }
  }
}

def bubbleDown(currentIndex: Int) {
  getLowerChild(currentIndex) match {
    case Some((lowerChildIndex, lowerChildValue)) =>
      if (compareFunction(valueOf(currentIndex), lowerChildValue) == HIGHER) {
        swap(currentIndex, lowerChildIndex)
        bubbleDown(lowerChildIndex)
      }
    case None =>
  }
}

def getLowerChild(index: Int): Option[(Int, Int)] = {
  def getChildrenIndices(parentIndex: Int): (Int, Int) = (2 * parentIndex + 1, 2 * parentIndex + 2)

  val (leftChildIndex, rightChildIndex) = getChildrenIndices(index)

  val areChildrenInBoundsOfHeap = (rightChildIndex <= lastIndex) && (leftChildIndex <= lastIndex)
  if (!areChildrenInBoundsOfHeap) return None

  val (leftChildValue, rightChildValue) = (heapRepresentation(leftChildIndex), heapRepresentation(rightChildIndex))

  compareFunction(leftChildValue, rightChildValue) match {
    case LOWER => Some((leftChildIndex, leftChildValue))
    case _ => Some((rightChildIndex, rightChildValue))
  }
}

def pop: Option[Int] = heapRepresentation.size match {
  case 0 => None
  case _ =>

    val firstValue = heapRepresentation(0)
    if (heapRepresentation.size == 1) {
      heapRepresentation.remove(0)
      return Some(firstValue)
    }
    swap(0, lastIndex)
    heapRepresentation.remove(lastIndex)
    bubbleDown(0)
    Some(firstValue)
}


override def toString = {
  s"[$heapRepresentation](${heapRepresentation.size})"
}
}
package heap

import scala.collection.mutable.ListBuffer

class Heap[T](compareFunction: (T, T) => Int) {

private val HIGHER = 1
private val LOWER = -1

private val heapRepresentation = new ListBuffer[T]()

private def lastIndex = heapRepresentation.size - 1

private def valueOf(index: Int): T = heapRepresentation(index)

def add(item: T): Heap[T] = {
    heapRepresentation.append(item)
    bubbleUp(lastIndex)
    this
}

def swap(index1: Int, index2: Int) {
    val temp = valueOf(index1)
    heapRepresentation.update(index1, valueOf(index2))
    heapRepresentation.update(index2, temp)
}

def bubbleUp(currentIndex: Int) {
    def getParent(i: Int) = (i - 1) / 2

    if (currentIndex > 0) {
        val parentIndex = getParent(currentIndex)

        compareFunction(valueOf(currentIndex), valueOf(parentIndex)) match {
            case LOWER =>
                swap(currentIndex, parentIndex)
                bubbleUp(parentIndex)
            case _ =>
        }
    }
}

def bubbleDown(currentIndex: Int) {
    getLowerChild(currentIndex) match {
        case Some((lowerChildIndex, lowerChildValue)) =>
            if (compareFunction(valueOf(currentIndex), lowerChildValue) == HIGHER) {
                swap(currentIndex, lowerChildIndex)
                bubbleDown(lowerChildIndex)
            }
        case None =>
    }
}

def getLowerChild(index: Int): Option[(Int, T)] = {
    def getChildrenIndices(parentIndex: Int): (Int, Int) = (2 * parentIndex + 1, 2 * parentIndex + 2)

    val (leftChildIndex, rightChildIndex) = getChildrenIndices(index)

    val areChildrenInBoundsOfHeap = (rightChildIndex <= lastIndex) && (leftChildIndex <= lastIndex)
    if (!areChildrenInBoundsOfHeap) return None

    val (leftChildValue, rightChildValue) = (heapRepresentation(leftChildIndex), heapRepresentation(rightChildIndex))

    compareFunction(leftChildValue, rightChildValue) match {
        case LOWER => Some((leftChildIndex, leftChildValue))
        case _ => Some((rightChildIndex, rightChildValue))
    }
}

def pop: Option[T] = heapRepresentation.size match {
    case 0 => None
    case _ =>

        val firstValue = heapRepresentation(0)
        if (heapRepresentation.size == 1) {
            heapRepresentation.remove(0)
            return Some(firstValue)
        }
        swap(0, lastIndex)
        heapRepresentation.remove(lastIndex)
        bubbleDown(0)
        Some(firstValue)
}


override def toString = {
    s"[$heapRepresentation](${heapRepresentation.size})"
}

}

Usage of heap class

def compareInts(first: Int, second: Int): Int =
    if (first > second) 1
    else if (first == second) 0
    else -1

val heap = new Heap[Int](compareInts)

for (i <- 0 to 3) {
    heap.add((Math.random() * 100).toInt)
}
heap
heap.pop
heap.pop
heap.pop
heap.pop


def compareStrings(first: String, second: String): Int =
    if (first > second) 1
    else if (first == second) 0
    else -1
var heap2 = new Heap[String](compareStrings)
heap2.add("a")
heap2.add("cc")
heap2.add("")

heap2.pop
heap2.pop
heap2.pop
heap2.pop

Right now, I would like to get to Scala mindset, get to know tricks and tips. I have background in PHP and JavaScript, and Scala is quite harder to learn, but way more elegant, so I want to master it

Post Reopened by Jamal
deleted 123 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

You can make pull requests or open an issue and i will be happy to improve my code and scala knowledge.

Thank you

You can make pull requests or open an issue and i will be happy to improve my code and scala knowledge.

Thank you

added 2616 characters in body
Source Link
Forex
  • 161
  • 4
Loading
Post Closed as "Not suitable for this site" by Jamal
Source Link
Forex
  • 161
  • 4
Loading