Skip to content

Instantly share code, notes, and snippets.

@fedesilva
Last active December 18, 2015 05:29
Show Gist options
  • Save fedesilva/5732964 to your computer and use it in GitHub Desktop.
Save fedesilva/5732964 to your computer and use it in GitHub Desktop.
I love this language.
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
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
@fedesilva
Copy link
Author

This is scala 2.9.2. Had I been using 2.10 I would have omitted the implicit def and used directly implicit class.

@soc
Copy link

soc commented Jun 7, 2013

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