Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Conservative Language Use For Fun and Profit

Conservative Language Use For Fun and Profit

Scala provides many tools for building programs. Depending on how you
look at it, this can be a great thing, or a terrifying thing. I will
take a look at the scala "sub-set" that I adopted over years of pain,
and hopefully demonstrate that you can get more benefit from scala by
adopting less of it. In the end my sub-set may be different to yours,
but it might give you some different ideas on how to approach
programming in scala (or in general).

Mark Hibberd

February 10, 2016
Tweet

More Decks by Mark Hibberd

Other Decks in Programming

Transcript

  1. You can make better use of tools by having deep

    knowledge of a tool, but only applying a specialised subset of its features. Claim #1
  2. You can’t effectively control your use of tools to a

    specialised subset unless you have very deep knowledge of the tool. Claim #2
  3. 1 scala> val x = List("1", "2", "3", "4").toSet() +

    "5" 2 3 scala> println(x) 4 5 output: 6 7 ???
  4. 1 scala> val x = List("1", "2", "3", "4").toSet() +

    "5" 2 3 scala> println(x) 4 5 output: 6 7 false5
  5. 1 scala> val x = Option(1).zip(Option(1)) == 2 Option((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 ???
  6. 1 scala> val x = Option(1).zip(Option(1)) == 2 Option((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 false
  7. 1 scala> val x = Option(1).zip(Option(1)) == 2 List((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 ???
  8. 1 scala> val x = Option(1).zip(Option(1)) == 2 List((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 true
  9. 1 scala> val x = Option(1).zip(Option(1)) == 2 Vector((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 true
  10. 1 List("1", "2", "3", "4").toSet() + "5" 2 3 //

    example.scala:1: error: not enough arguments for 4 // method apply: (elem: Any)Boolean in trait GenSet. 5 // Unspecified value parameter elem. -Yno-adapted-args
  11. 1 def update(data: String): Future[Unit] = for { 2 n

    <- get 3 _ <- save(n, data) 4 } yield set(n + 1) 5 6 def get: Future[Int] = 7 ??? 8 9 def set(n: Int): Future[Int] = 10 ??? 11 12 def save(n: Int, data: String): Future[Unit] = 13 ???
  12. 1 def update(data: String): Future[Unit] = for { 2 n

    <- get 3 _ <- save(n, data) 4 } yield set(n + 1) 5 6 //example.scala:4: warning: discarded non-Unit value -Ywarn-value-discard