Curious case of ‘in’ in Elixir

A laptop and a mounted television.
A laptop and a mounted television.
Not related to the article. Just added to make thumbnail look good ;)


'in' is usually used with list or range. But here we

1. Implement Enumerable for another data type to use ‘in’.

2. Define macro with ‘in’ to improve readability.

Don’t try this at home

Just kidding, we can use this, but we might introduce unnecessary bugs in the code and might make the code unreadable as this is not widely practiced. Let’s do this as an experiment to know more about Elixir.

Using ‘in’ with string

Let’s try to find whether a substring is present inside a string.

"lix" in "Elixir"
** (Protocol.UndefinedError) protocol Enumerable not implemented for "ab" of type BitString
(elixir 1.10.4) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir 1.10.4) lib/enum.ex:166: Enumerable.member?/2
(elixir 1.10.4) lib/enum.ex:1682: Enum.member?/2
"lix" in "Elixir"


History detour

This curiosity started while I was debugging a code related to ‘from’ in ecto query. How ecto allows us to write code like

from(c in City, select: c)
Image for post
Image for post

Define macro with ‘in’

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store