##### Differences

This shows you the differences between two versions of the page.

 sesiuni:scala:lab8 [2016/07/06 16:46]ataleanu sesiuni:scala:lab8 [2016/07/06 18:55] (current)dciocirlan [Practice] Both sides previous revision Previous revision 2016/07/06 18:55 dciocirlan [Practice] 2016/07/06 18:54 ataleanu [Practice] 2016/07/06 16:57 ataleanu [Practice] 2016/07/06 16:46 ataleanu 2016/07/06 12:08 dciocirlan [8 - Implicits] 2016/07/06 12:07 dciocirlan created Next revision Previous revision 2016/07/06 18:55 dciocirlan [Practice] 2016/07/06 18:54 ataleanu [Practice] 2016/07/06 16:57 ataleanu [Practice] 2016/07/06 16:46 ataleanu 2016/07/06 12:08 dciocirlan [8 - Implicits] 2016/07/06 12:07 dciocirlan created Line 20: Line 20: - Define the class ''​RichInteger''​ that takes an Int parameter: - Define the class ''​RichInteger''​ that takes an Int parameter: class RichInteger(val x: Int)​ class RichInteger(val x: Int)​ - * Define the sqrt method on it. It should do the square root of x. Use an implicit conversion from ''​Int''​ to ''​String''​ to enable this operation on integers. + * Define the sqrt method on it. It should do the square root of x. Use an implicit conversion from ''​Int''​ to ''​RichInteger''​ to enable this operation on integers. * Convert the previous code to an implicit class. How does this help you? * Convert the previous code to an implicit class. How does this help you? * Add an isPrime method to ''​RichInteger''​. It should check whether x is prime or not. * Add an isPrime method to ''​RichInteger''​. It should check whether x is prime or not. * Add the ''​gcd''​ method to ''​RichInteger''​. It should receive another y parameter of type ''​Int''​ and compute the greatest common divisor of the two. * Add the ''​gcd''​ method to ''​RichInteger''​. It should receive another y parameter of type ''​Int''​ and compute the greatest common divisor of the two. * Test the new magic methods you just created. * Test the new magic methods you just created. - - Define a data type for {{http://​semver.org/|semantic versioning}}. + - Define a data type for {{http://​semver.org |semantic versioning}}. * Use a case class and name it ''​Version''​. It should have 3 fields, all integers: ''​major'',​ ''​minor''​ and ''​patch''​. * Use a case class and name it ''​Version''​. It should have 3 fields, all integers: ''​major'',​ ''​minor''​ and ''​patch''​. * Define the ''<''​ operator on the new data type. * Define the ''<''​ operator on the new data type. - * Define a value for a ''​Version''​ ordering. Its type should be ''​Ordering[Version]''​ + * Define a value for a ''​Version''​ ordering. Its type should be ''​Ordering[Version]''​ + val order: Ordering[Version] = ???​ * Create a list of versions. Try calling the ''​sorted''​ method on it. Does it work? * Create a list of versions. Try calling the ''​sorted''​ method on it. Does it work? * Promote the ordering defined earlier to an implicit value. What's the recommended place to put the implicit? * Promote the ordering defined earlier to an implicit value. What's the recommended place to put the implicit? - - Define the ''​Eq''​ typeclass to offer support for equality testing. + - Define the ''​Eq''​ typeclass to offer support for equality testing. + trait Eq[A] { + def ===(l: A, r: A): Boolean + def =!=(l: A, r: A): Boolean + }​ + * The two operations should check for equality. Tip - you can use the existing ''​==''​ and ''​!=''​ in the actual implementation. + * Typeclasses usually define additional ''​Ops''​ traits in order to provide an operator like notation to the implementing types. Use the following companion object for Eq in order to enable ''​===''​ and ''​=!=''​ on any type class instance.<​code scala> + object Eq { + + def apply[A](implicit instance: Eq[A]): Eq[A] = instance + + trait Ops[A] { + def typeClassInstance:​ Eq[A] + def self: A + def ===(other: A) = typeClassInstance.===(self,​ other) + def =!=(other: A) = typeClassInstance.=!=(self,​ other) + } + + trait ToEqOps { + implicit def toEqOps[A](target:​ A)(implicit instance: Eq[A]) = new Ops[A] { + override val typeClassInstance = instance + override val self              = target + } + } + + object ops extends ToEqOps + }​ + * Define an instance for ''​Int''​s. Use the ''​Eq''​ object to define it. Compare two integers using the ''​===''​ and ''​=!=''​ methods. Does it work for other types as well (e.g. Strings)? + * Define an universal instance that compares generic types. What's the difference between ''​==''​ vs. ''​===''​ and ''​!=''​ vs ''​=!=''?​
sesiuni/scala/lab8.1467812766.txt.gz · Last modified: 2016/07/06 16:46 by ataleanu