Last active
December 18, 2015 05:29
-
-
Save fedesilva/5732964 to your computer and use it in GitHub Desktop.
I love this language.
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
implicit class In[T](t:T) { | |
def in(opts:T*): Boolean = opts contains t | |
} | |
scala> 1 in ( 2, 3 ) | |
res1: Boolean = false | |
scala> 1 in ( 2, 3, 2, 1 ) | |
res2: Boolean = true | |
scala> 501 in ((500 to 504) : _*) | |
res3: Boolean = true |
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
class In[T](t:T) { | |
def in(opts:T*): Boolean = opts contains t | |
} | |
implicit def ToIn[T](t:T) = new In(t) | |
scala> 1 in ( 2, 3 ) | |
res5: Boolean = false | |
scala> 1 in ( 2, 3, 2, 1 ) | |
res6: Boolean = true | |
Nice!
Minor improvement: opts.find(_ == any).isDefined
=> opts contains any
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is scala 2.9.2. Had I been using 2.10 I would have omitted the
implicit def
and used directlyimplicit class
.