Skip to content

Commit

Permalink
re folone#16 Initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
folone committed Sep 12, 2012
1 parent fb617bd commit 5d76b07
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/main/scala/info/folone/scala/poi/package.scala
Expand Up @@ -3,6 +3,7 @@ package info.folone.scala
import scalaz._

package object poi {
// Typeclass instances
implicit val cellInstance = new Semigroup[Cell] with Equal[Cell] with Show[Cell] {
override def append(f1: Cell, f2: Cell) = f2
override def equal(a1: Cell, a2: Cell): Boolean = a1 == a2
Expand All @@ -20,9 +21,9 @@ package object poi {
Sheet(f2.name)(mergeSets(f1.rows, f2.rows, (_: Row).index))
override def equal(a1: Sheet, a2: Sheet): Boolean =
a1.name == a2.name &&
(a1.rows.toIndexedSeq.sortBy((x: Row) => x.index) zip
a2.rows.toIndexedSeq.sortBy((x: Row) => x.index))
.foldLeft (true) { (acc, v) =>
(a1.rows.toIndexedSeq.sortBy((x: Row) x.index) zip
a2.rows.toIndexedSeq.sortBy((x: Row) x.index))
.foldLeft (true) { (acc, v)
acc && Equal[Row].equal(v._1, v._2)
}
override def shows(as: Sheet) = "Sheet (\"" + as.name + "\")(" + as.rows + ")"
Expand All @@ -32,14 +33,24 @@ package object poi {
override def append(f1: Workbook, f2: Workbook) =
Workbook(mergeSets(f1.sheetSet, f2.sheetSet, (_: Sheet).name))
override def equal(a1: Workbook, a2: Workbook): Boolean =
(a1.sheetSet.toIndexedSeq.sortBy((x: Sheet) => x.name) zip
a1.sheetSet.toIndexedSeq.sortBy((x: Sheet) => x.name))
.foldLeft (true) { (acc, v) =>
(a1.sheetSet.toIndexedSeq.sortBy((x: Sheet) x.name) zip
a1.sheetSet.toIndexedSeq.sortBy((x: Sheet) x.name))
.foldLeft (true) { (acc, v)
acc && Equal[Sheet].equal(v._1, v._2)
}
override def shows(as: Workbook) = "Workbook(" + as.sheetSet + ")"
}

// Lenses
import Lens._
import StoreT._
val cellLens: Cell @> String =
lens(c store(c.data)(changed c.copy(data = changed)))
val rowLens = setLens[Row, Cell] (lens(r store(r.cells)(changed Row(r.index) (changed))))
val sheetLens = setLens[Sheet, Row] (lens(s store(s.rows) (changed Sheet(s.name)(changed))))
val wbLens = setLens[Workbook, Sheet] (lens(wb store(wb.sheetSet)(changed Workbook(changed))))

// Utility functions
private def mergeSets[A: Semigroup, B](list1: Set[A], list2: Set[A], on: A B): Set[A] =
combine(list1.map(l (on(l), l)).toMap, list2.map(l (on(l), l)).toMap)
.map { case(_, y) y } toSet
Expand Down

0 comments on commit 5d76b07

Please sign in to comment.