Saturday, April 29, 2017

Scala Dessign Patterns: Pimp My Library

Introduction

We need something extra without modifying original library Or we want to add some additional methods in our existing classes without modifying existing code.

Agenda:

Pimp My Library using Scala

Code:


package object pimplib {

  implicit class StringExtension(val string: String) extends AnyVal {
    def isAllUperCase: Boolean = {
      (0 to string.size - 1 ) find {
        case index => !string.charAt(index).isUpper
      }
    }.isEmpty
  }
}
object StringExtensionLib extends App {

  import pimplib._

  var string = "test"
  println(s"${string} is all upper ${string.isAllUperCase}")
  string = "Test"
  println(s"${string} is all upper ${string.isAllUperCase}")
  string = "TESt"
  println(s"${string} is all upper ${string.isAllUperCase}")
  string = "TEST"
  println(s"${string} is all upper ${string.isAllUperCase}")
}

Download Code from Github Repo

 

References:

Scala Design Patterns By Ivan Nikolov

No comments:

Post a Comment