Your SlideShare is downloading. ×
Implementing IN operator in Scala
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Implementing IN operator in Scala

1,549
views

Published on

Boston Area Scala Enthusiasts fourth meeting held on Feb 2, 2010. …

Boston Area Scala Enthusiasts fourth meeting held on Feb 2, 2010.
Talk delivered by Nermin Serifovic.

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,549
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Notes go here
  • As a Java developer, one thing I miss about Pascal is the in operator.Pascal offers set as a fundamental building block in the language.It’s hard to argue that code like this is elegant and easy to write and read.
  • Transcript

    • 1. Boston Area Scala Enthusiasts
      Implementing IN operator in Scala
      Nermin Serifovic
      February 2, 2010
    • 2. Pascal provides the in operator:
      if 5 in [1, 2, 3, 4, 5, 6] then write (“5 is in the set”);
      SQL allows it too:
      Select *
      From Employees
      Where LastName IN (‘Smith’, ‘Garcia’, ‘Johnson’, ‘Lee’)
    • 3. Ideally, would like to write something like this in Scala:
      if (5 in [1, 2, 3]) foundIt = true
      Or:
      if (5 in (1, 2, 3)) foundIt = true
      Valid in Groovy: def list = [5, 6, 7, 8]
      However, Scala has no literals for Lists, Sets, etc…
      All brackets and parenthesis are already reserved:
      []: type parameterization
      (): tuples
      {}: code block
      <>: relational methods
    • 4. Acceptable syntax:
      if (5 in List(1, 2, 3)) foundIt = true
      or:
      if (“Scala” in Array(“Programming”, “in”, “Scala”) foundIt = true
      Extra credit:
      if (5 not in List(1, 2, 3)) foundIt = false
      instead of:
      if !(5 in List(1, 2, 3)) foundIt = false
    • 5. Operands.scala:
      package in.nerm.scala.ltalk1
      class InOperand[A](a: A) {
      def in(seq: Seq[A]): Boolean = seq.contains(a)
      }
      object OperandConversions {
      implicit def toInOperand[A](a: A): InOperand[A] = new InOperand(a)
      }
      Note: parameterized, so it’s not possible to say:
      if (5 in List(“white”, “blue”) foundIt = true
    • 6. InOperandTests.scala:
      package in.nerm.scala.ltalk1
      import in.nerm.scala.ltalk1.OperandConversions._
      import org.testng.annotations.Test
      import org.scalatest.testng.TestNGSuite
      class InOperandTests extends TestNGSuite {
      @Test def verifyIn() {
      assert(5 in List(1, 2, 3, 4, 5, 6))
      assert(!(7 in Array(1, 2, 3, 4, 5, 6)))
      assert("Scala" in List("Programming", "in", "Scala"))
      }
      }
    • 7. Given that:
      5 not in List(1, 2, 3)
      gets interpreted as:
      5.not(in).List(1, 2, 3)
      is it really possible to implement it?
      If not, how close can we get?