What's a good solution to drop nil value in a struct for Elixir?

what'd
how to pronounce what
what w
what does
what c
what's
what p
what meme

For example, I have a struct

post = %Post{title: "Title", desc: nil}

And I want to get

%{title: "Title"}

My solution is like

post
  |> Map.delete(:__struct__) # change the struct to a Map
  |> Enum.filter(fn {_, v} -> v end)
  |> Enum.into(%{})

It works, but is there a better one?

Update:

I feel it annoying transforming from Struct to Map, then Enum, then Map again. Is there a concise way?

Instead of doing

post
  |> Map.delete(:__struct__) # change the struct to a Map
  |> Enum.filter(fn {_, v} -> v end)
  |> Enum.into(%{})

You can do

post
  |> Map.from_struct
  |> Enum.filter(fn {_, v} -> v != nil end)
  |> Enum.into(%{})

It's just slightly cleaner than deleting the __struct__ key manually.

What, what as a pronoun, it can be the subject, object, or complement of a verb. It can also be the object of a preposition. Quickly send and receive WhatsApp messages right from your computer.

You could also do this with comprehensions:

for {k, v} <- Map.from_struct(post), v != nil, into: %{}, do: {k, v}

What for definition and meaning, is commonly classified as an adjective if it is used to introduce a noun or a noun phrase. noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.

Also can be written like this:

post
    |> Map.from_struct
    |> Enum.reject(fn {_, v} -> is_nil(v) end)
    |> Map.new

What, I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)  informal a punishment or reprimand (esp in the phrase give (a person) what for) 7. what have you someone, something, or somewhere unknown or unspecified: cars, motorcycles, or what have you. 8.

Another option is:

post
  |> Map.from_struct()
  |> Enum.filter(fn {_key, value} -> !is_nil(value) end)
  |> Map.new()

Map.new/1 is thought exactly to convert a keyword list into a map.

What Part of Speech is “WHAT”, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. In current usage that refers to persons or things, which chiefly to things and rarely to subhuman entities, who chiefly to persons and sometimes to animals. The notion that that should not be used to refer to persons is without foundation; such use is entirely standard.

What's dictionary definition, Your questions answered on what type of mask to wear to cut the risk of getting Covid-19. This is the ORIGINAL "What What (In the Butt)" video. The South Park version aired over a year after we made this video. There's been a lot of confusion about this.

what, Minneapolis City Council members haven't specified what or who will replace it if the department disbands. Camden, New Jersey, may be the  WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

WHAT, It is plural if a plural noun or noun phrase completes the sentence, as in He sometimes makes what seem to be gestures of reconciliation. · Clauses with what as  Find out what your public IPv4 and IPv6 address is revealing about you! My IP address information shows your location; city, region, country, ISP and location on a map.

Comments
  • Not very clear what you're asking - better how? One thing I would change is use Map.from_struct instead of Map.delete(...)
  • Thanks, I feel it annoying transforming from Struct to Map, then Enum, then Map. Is there a concise way?
  • @Tony your solution is ok I think, but it has a flaw: It will also discard entries where the value is false. You need to change the filtering to Enum.filter(fn {_, v} -> v != nil end) or Enum.reject(fn {_, v} -> v == nil end)
  • @PatrickOscity Yeah, you're right. Thanks :)
  • You should change the filtering in the second example to either v != nil or use reject instead of filter.
  • Btw, StackOverflow uses four spaces indentation to format code blocks, backticks are only for inline code. Edited your answer.
  • I accepts this answer because I can't figure out a better way and the comprehensions solution is a little difficult to read.
  • You could use the terse, using the anonymous function shorthand: |> Enum.filter(&(&1))
  • Thanks for your answer, which is inspired!
  • Three years later, still a hot answer.