在Elixir中将Integer转换为Atom

时间:2017-04-26 09:53:32

标签: elixir

在Erlang中,可以编写'1'来获取一个整数命名的原子。 Elixir使用语法:<name>来定义原子,但:1是不可能的:

iex(1)> :1
** (SyntaxError) iex:1: unexpected token: ":" (column 1, codepoint U+003A)

Elixir有没有办法生成一个整数命名的原子?

3 个答案:

答案 0 :(得分:5)

您可以将# # OpenSSL configuration file. # # Establish working directory. dir = . [ ca ] default_ca = CA_default [ CA_default ] serial = $dir/serial database = $dir/certindex.txt new_certs_dir = $dir/certs certificate = $dir/cacert.pem private_key = $dir/private/cakey.pem default_days = 365 default_md = md5 preserve = no email_in_dn = no nameopt = default_ca certopt = default_ca policy = policy_match [ policy_match ] countryName = match stateOrProvinceName = match organizationName = match organizationalUnitName = optional commonName = supplied emailAddress = optional [ req ] default_bits = 1024 # Size of keys default_keyfile = key.pem # name of generated keys default_md = md5 # message digest algorithm string_mask = nombstr # permitted characters distinguished_name = req_distinguished_name req_extensions = v3_req [ req_distinguished_name ] # Variable name Prompt string #------------------------- ---------------------------------- 0.organizationName = Organization Name (company) organizationalUnitName = Organizational Unit Name (department, division) emailAddress = Email Address emailAddress_max = 40 localityName = Locality Name (city, district) stateOrProvinceName = State or Province Name (full name) countryName = Country Name (2 letter code) countryName_min = 2 countryName_max = 2 commonName = Common Name (hostname, IP, or your name) commonName_max = 64 # Default values for the above, for consistency and less typing. # Variable name Value #------------------------ ------------------------------ 0.organizationName_default = My Company localityName_default = My Town stateOrProvinceName_default = State or Providence countryName_default = US [ v3_ca ] basicConstraints = CA:TRUE subjectKeyIdentifier = hash authorityKeyIdentifier = keyid:always,issuer:always [ v3_req ] basicConstraints = CA:FALSE subjectKeyIdentifier = hash 放在引号中并添加1以在Erlang中获得等效的:

'1'

答案 1 :(得分:3)

Dogbert提供的答案是最简单的。如果您不想使用函数,那就没有功能了。 您可以通过首先将int转换为Sting然后转换为Atom来实现。

1
|> Integer.to_string()
|> String.to_atom()
# :"1"

答案 2 :(得分:1)

您还可以利用字符串插值:
String.to_atom("#{your_number}")