[英]How can i convert a BitString into a String in Elixir?
I have a function that receives a list of strings and concatenates each string into a new string, i do that using Enum.join.我有一个接收字符串列表并将每个字符串连接成一个新字符串的函数,我使用 Enum.join 来做到这一点。 But when i try this operation, i get the following error:但是当我尝试此操作时,出现以下错误:
** (Protocol.UndefinedError) protocol Enumerable not implemented for "int main(){return 2;}" of type BitString. This protocol is implemented for the following type(s): Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
My way around this was trying to convert the BitString into a String, but i can't find anything for doing this in Elixir's documentation.我解决这个问题的方法是尝试将 BitString 转换为字符串,但我在 Elixir 的文档中找不到任何用于执行此操作的内容。 My other solution was trying to not get that BitString at all but i don't even know why i'm getting that BitString to begin with.我的另一个解决方案是试图根本不获取该 BitString,但我什至不知道为什么我要从该 BitString 开始。
The process i'm doing is to receive a list like this: [{"int main(){return 2;}", 1}]
Then i make a list but only using the string text=Enum.map(words, fn {string, _} -> string end)
我正在做的过程是接收这样的列表: [{"int main(){return 2;}", 1}]
然后我创建一个列表但只使用字符串text=Enum.map(words, fn {string, _} -> string end)
I tried printing the result so i'm sure i'm giving the correct argument;我尝试打印结果,所以我确定我给出了正确的论点; by using IO.inspect(text)
, i got ["int main(){return 2;}"]
, which looks like a list of strings to me.通过使用IO.inspect(text)
,我得到了["int main(){return 2;}"]
,对我来说它看起来像一个字符串列表。
Then i pass that to a function using Enum.flat_map(text, &lex_raw_tokens(&1, line))
然后我将它传递给使用Enum.flat_map(text, &lex_raw_tokens(&1, line))
的函数
Inside of that function, i do def lex_raw_tokens(program,line) when program != "" do textString=Enum.join(program, " ")
在该函数内部, def lex_raw_tokens(program,line) when program != "" do textString=Enum.join(program, " ")
,我执行def lex_raw_tokens(program,line) when program != "" do textString=Enum.join(program, " ")
This is where i get the error.这是我得到错误的地方。 Is there any way of turning that BitString back into a String or not get that BitString?有没有办法将该 BitString 转换回 String 或不获取该 BitString?
Sorry, i'm still learning Elixir and honestly so far it's the most dificcult lenguage i've learned and i'm having a lot of troubles with it.抱歉,我仍在学习 Elixir,老实说,到目前为止,这是我学过的最难的语言,而且我遇到了很多麻烦。 Also, this whole thing is part of a small C compiler i'm doing as a school proyect此外,这整个事情是我作为学校项目的一个小型 C 编译器的一部分
You have text
bound to ["int main(){return 2;}"]
, then you're doing an Enum.flat_map/2
over text
, so inside lex_raw_tokens/2
, program
is bound to "int main(){return 2;}"
.您将text
绑定到["int main(){return 2;}"]
,然后您正在对text
执行Enum.flat_map/2
,因此在lex_raw_tokens/2
, program
绑定到"int main(){return 2;}"
。 You're then trying to do an Enum.join/2
on program
, but since it's a string (which is a kind of BitString
), it's not enumerable.然后您尝试在program
上执行Enum.join/2
,但由于它是一个字符串(它是一种BitString
),因此它不可枚举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.