Elixir理解返回一个星形字符"*" [英] Elixir comprehension returning a star character '*'
问题描述
我有一个在 p.followings
中返回的Persona模型的列表,我想从该模型列表中将 followed_id
字段提取到一个单独的列表中.
I have a list of Persona models being returned in p.followings
and I want to extract the followed_id
field from this list of models into a separate list.
p.followings
returns...
[
%Poaster.Personas.Following{
__meta__: #Ecto.Schema.Metadata<:loaded, "followings">,
followed: %Poaster.Personas.Persona{
__meta__: #Ecto.Schema.Metadata<:loaded, "personas">,
background_image_url: nil,
bio: "ASDF",
followings: #Ecto.Association.NotLoaded<association :followings is not loaded>,
id: 42,
inserted_at: ~N[2020-08-14 01:52:17],
name: nil,
profile_image_url: nil,
updated_at: ~N[2020-08-14 16:19:56],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 1,
username: "test"
},
followed_id: 42,
id: 1,
inserted_at: ~N[2020-08-12 20:35:09],
persona: #Ecto.Association.NotLoaded<association :persona is not loaded>,
persona_id: 1,
updated_at: ~N[2020-08-12 20:35:09]
}
]
我只是想在此处获取followed_id的列表,因此我可以进行查询以获取我所关注的角色的帖子列表.
I simply want to get a list of the followed_id here so I can make a query to get a list of posts from those personas I am following.
我想找回 [42]
之类的东西.
当我执行 Enum.map(ps.followings,fn follow-> follow.followed_id end)
时,这是我期望能够运行的功能,我回来了在控制台中,只需'*'
When I do Enum.map(ps.followings, fn follow -> follow.followed_id end)
, which is what I expected to be able to run to get this, I am getting back in the console just '*'
当我尝试对 into
选项使用理解时,进入一个空列表,这也是我得到的.
When I tried to use a comprehension with the into
option, into an empty list, this is also what I got.
persona_ids = []
for p <- p.followings, into: persona_ids, do: p.followed_id
IO.inspect(persona_ids)
[]
但是,当我对 p.followed
进行上述理解时,它将返回角色的列表:
However, when I run the above comprehension with p.followed
, it returns a list of the Personas:
for p <- p.followings, into: persona_ids, do: p.followed
[
%Poaster.Personas.Persona{
__meta__: #Ecto.Schema.Metadata<:loaded, "personas">,
background_image_url: nil,
bio: "ASDF",
followings: #Ecto.Association.NotLoaded<association :followings is not loaded>,
id: 42,
inserted_at: ~N[2020-08-14 01:52:17],
name: nil,
profile_image_url: nil,
updated_at: ~N[2020-08-14 16:19:56],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 1,
username: "test"
}
]
我需要ID列表,而不是Persona模型列表,以便我可以进行适当的Ecto查询以从我关注的Personas中获取帖子.
I need the list of IDs, not the list of Persona models, so that I can make an appropriate Ecto query to get the posts from the Personas I follow.
这是怎么回事?我究竟做错了什么?有没有更好的方法来做我想做的事?
What is going on here? What am I doing wrong? Is there a better way to do what I am trying to do?
推荐答案
As I mentioned in the comment, and as discussed on this other post, the '*'
you're receiving is in fact the list you expect: [42]
.
之所以会这样,是因为42是 *
字符的代码点(您可以通过在iex会话中执行?*
进行验证).在Elixir和Erlang中,当您有一个整数列表并且所有整数都是字符的有效代码点时,使用 IO.inspect
时它将打印字符列表,但这是一个列表,您可以像使用任何列表一样使用它.
This happens because 42 is the codepoint of the *
character (you can verify this by doing ?*
in an iex session). In Elixir and Erlang, when you have a list of integers and all of the integers are valid codepoints for characters, it will print the charlist when you use IO.inspect
, but it is a list and you can use it like you would use any list.
例如,如果您在iex提示符下键入 [104、101、108、108、111]
,您将返回'hello'
,但单个引号表示它是一个字符列表,您可以对其执行任何列表操作.
For instance, if you type [104, 101, 108, 108, 111]
into the iex prompt, you will get back 'hello'
, but the single quotes denote that it's a charlist, and you can perform any list operations you'd like on it.
这篇关于Elixir理解返回一个星形字符"*"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!