Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.logikujo
- package pebeeme
- import scalaz._
- import Scalaz._
- import java.io.File
- import scala.collection.Map
- /**
- *
- * pebeeme / LogiDev - [Fun Functional] / Logikujo.com
- *
- * com.logikujo 5/01/14 :: 11:50 :: eof
- *
- */
- case class PPM(pixels: List[Pixel]) extends PBM
- object PPM {
- type D = List[List[((Int,Int), Double)]]
- def pointDistances(l: List[Pixel], res: D, n:Int): D = (l.length <= 1)? res | {
- val listOfHeads = List.tabulate(l.length -1)(_ => l.head)
- val lSums = listOfHeads.zip(l.tail).map {
- case (p1,p2) => p1.distance(p2)
- }.zipWithIndex.map {
- case (s,i) => (n, i+1+n) -> s
- }
- pointDistances(l.tail, lSums :: res, n+1)
- }
- def getIndex(m:Map[(Int,Int),Double], idx:(Int,Int)) = idx match {
- case (i,j) => m.get(i->j).orElse(m.get(j->i))
- }
- /* DANGER: Double doesnt conform a Monoid. Addition is not associative! */
- implicit val doubleInstance: Monoid[Double] = new Monoid[Double] {
- def append(d1: Double, d2: => Double): Double = d1 + d2
- def zero: Double = 0
- }
- def minimumDistancePoint(ppm: PPM) = {
- val len = ppm.pixels.length
- val distances = pointDistances(ppm.pixels, List(), 1).flatten.toMap
- (for {
- i <- 1 to len
- } yield for {
- j <- 1 to len
- } yield getIndex(distances, i -> j)).map { l =>
- l.reduce(_ |+| _)
- }.toList.min
- }
Advertisement
Add Comment
Please, Sign In to add comment