Browse files

re #16 Adding back lenses after refactoring, including lens families.

  • Loading branch information...
1 parent d51b6db commit 2a643c6973e8c415bd1b88eafe9b56ef6da60c73 @folone committed Dec 26, 2012
Showing with 42 additions and 0 deletions.
  1. +31 −0 README.md
  2. +11 −0 src/main/scala/info/folone/scala/poi/package.scala
View
31 README.md
@@ -77,6 +77,37 @@ res1: Boolean = true
scala> List(Workbook(path), sheetOne, sheetTwo).suml === (sheetOne |+| sheetTwo)
res2: Boolean = true
+// We also have some lenses, though they are a work in progress (and only available since 0.8-SNAPSHOT). Some examples:
+scala> cellLens.get(Cell(1, "data"))
+res3: scalaz.Id.Id[String] = data
+
+scala> cellLens.set(Cell(1, "data"), "new data")
+res4: scalaz.Id.Id[info.folone.scala.poi.Cell] = Cell(1,new data)
+
+scala> rowLens.contains(Cell(2, "data1")).get(Row(1)(Set(Cell(1,"data"), Cell(2, "data1"))))
+res8: scalaz.Id.Id[Boolean] = true
+
+scala> rowLens.contains(Cell(2, "data1")).get(Row(1)(Set(Cell(1,"data"), Cell(2, "data"))))
+res9: scalaz.Id.Id[Boolean] = false
+
+scala> (rowLens += Cell(2, "data1")).run(Row(1)(Set(Cell(1, "data"), Cell(3, "data3"))))
+res14: (info.folone.scala.poi.Row, Set[info.folone.scala.poi.Cell]) = (Row (1)(Set(Cell(1,data), Cell(3,data3), Cell(2,data1))),Set(Cell(1,data), Cell(3,data3), Cell(2,data1)))
+
+scala> (rowLens &= Set(Cell(2, "data1"))).run(Row(1)(Set(Cell(1, "data"), Cell(2, "data1"))))
+res17: (info.folone.scala.poi.Row, Set[info.folone.scala.poi.Cell]) = (Row (1)(Set(Cell(2,data1))),Set(Cell(2,data1)))
+
+scala> (rowLens &~= Set(Cell(2, "data1"))).run(Row(1)(Set(Cell(1, "data"), Cell(2, "data1"))))
+res18: (info.folone.scala.poi.Row, Set[info.folone.scala.poi.Cell]) = (Row (1)(Set(Cell(1,data))),Set(Cell(1,data)))
+
+scala> (rowLens |= Set(Cell(2, "data1"))).run(Row(1)(Set(Cell(1, "data"), Cell(2, "data1"))))
+res20: (info.folone.scala.poi.Row, Set[info.folone.scala.poi.Cell]) = (Row (1)(Set(Cell(1,data), Cell(2,data1))),Set(Cell(1,data), Cell(2,data1)))
+
+scala> (rowLens |= Set(Cell(2, "data1"))).run(Row(1)(Set(Cell(1, "data"), Cell(2, "data2"))))
+res21: (info.folone.scala.poi.Row, Set[info.folone.scala.poi.Cell]) = (Row (1)(Set(Cell(1,data), Cell(2,data2), Cell(2,data1))),Set(Cell(1,data), Cell(2,data2), Cell(2,data1)))
+
+scala> (rowLens -= Cell(2, "data1")).run(Row(1)(Set(Cell(1, "data"), Cell(2, "data1"))))
+res23: (info.folone.scala.poi.Row, Set[info.folone.scala.poi.Cell]) = (Row (1)(Set(Cell(1,data))),Set(Cell(1,data)))
+
```
View
11 src/main/scala/info/folone/scala/poi/package.scala
@@ -41,6 +41,17 @@ package object poi {
override def shows(as: Workbook) = "Workbook(" + as.sheets + ")"
}
+ // Lenses
+ import Lens._
+ import StoreT._
+ val cellLens: Cell @> String =
+ lensFamily(c store(c.data)(changed c.copy(data = changed)))
+ val rowLens = setLensFamily[Row, Row, Cell](lens(r store(r.cells)(changed Row(r.index) (changed))))
+ val sheetLens =
+ setLensFamily[Sheet, Sheet, Row](lens(s store(s.rows) (changed Sheet(s.name)(changed))))
+ val wbLens =
+ setLensFamily[Workbook, Workbook, Sheet](lens(wb store(wb.sheets)(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)

0 comments on commit 2a643c6

Please sign in to comment.