Skip to content

Instantly share code, notes, and snippets.

@tvon

tvon/bitwise-options.ex

Last active Dec 23, 2015
Embed
What would you like to do?
flags = %{
default: 0,
sourcepos: 1,
hardbreaks: 2,
normalize: 4,
smart: 8,
validate_utf8: 16,
safe: 32
}
options = [
sourcepos: false,
safe: false,
smart: true, # 8
hardbreaks: true # 2
]
# Finding the correct values works
for {enabled, true} <- options, value = flags[enabled], do: IO.puts "#{enabled}: #{value}"
# [2, 8]
# Gives correct result but seems inelegant
values = for {enabled, true} <- options, value = flags[enabled], do: value
use Bitwise
Enum.reduce(values, fn(x, acc) -> x ||| acc end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment