Last active
August 29, 2015 14:00
-
-
Save ScalaWilliam/11355676 to your computer and use it in GitHub Desktop.
Adding up XML as doubles. Shows a case for Monads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scalawilliam.examples | |
import scala.xml.Node | |
/** | |
* Add .sum and .product functionality to Scala XML nodes. | |
*/ | |
object NumericNodesExample extends App { | |
import NumericUtility._ | |
implicit val nodeDoubleIsomorphism = new Isomorphism[Node, Double] { | |
assert(to(from(2.5)) == 2.5) | |
def to(n: Node): Double = n.text.toDouble | |
def from(d: Double): Node = <double>{d}</double> | |
} | |
implicit val doubleNumericNode = new NumericIsomorphism[Node, Double] | |
val nums = <nums>{List(0.5, 5, -1.2).map(n => <num>{n}</num>)}</nums> | |
println((nums \ "num").sum.text) | |
println((nums \ "num").product.text) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scalawilliam.examples | |
object NumericUtility { | |
trait Isomorphism[T, V] { | |
def to(from: T): V | |
def from(to: V): T | |
} | |
class NumericIsomorphism[V, T](implicit iso: Isomorphism[V, T], num: Numeric[T]) extends Numeric[V] { | |
private implicit val from = iso.from _ | |
private implicit val to = iso.to _ | |
def plus(x: V, y: V) = num.plus(x, y) | |
def toDouble(x: V) = num.toDouble(x) | |
def toFloat(x: V) = num.toFloat(x) | |
def toInt(x: V) = num.toInt(x) | |
def negate(x: V) = num.negate(x) | |
def fromInt(x: Int) = num.fromInt(x) | |
def toLong(x: V) = num.toLong(x) | |
def times(x: V, y: V) = num.times(x, y) | |
def minus(x: V, y: V) = num.minus(x, y) | |
def compare(x: V, y: V) = num.compare(x, y) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4.3 | |
-3.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment