1

I have an an object as

%{
  shares: []
}

I want to add a struct %Cs{name: "Junaid1"}, %Cs{name: "Junaid2"} into the list through Enum.each or map, is that possible to do?

Okay for just more brief I am updating question again..

 Enum.each(email_array, fn (email) ->
    with {:found_user, sharee} <- ensure_user(email)
    do
      case CameraShare.create_share(camera, sharee, caller, params["rights"], params["message"]) do
        {:ok, camera_share} ->
           #Add camera_share to list
        {:error, changeset} ->
          #Add error to a list
      end
    else
      {:not_found, email} ->
        case CameraShareRequest.create_share_request(camera, email, caller, params["rights"], params["message"]) do
          {:ok, camera_share_request} ->
            #Add camera_share to list
          {:error, changeset} ->
            #Add error to list
        end
    end
  end)

and then at the end of Enum.each.. there will be a map as

%{
   shares: [all shares],
   errors: [all errors]
}

where as each share is of type %CameraShare{__meta__: #Ecto.Schema.Metadata<:loaded, "camera_shares">}

2
  • What have you tried so far and what's the result you're currently getting? Commented Jul 7, 2017 at 1:48
  • Okay thanks. I have updated it again.. in very easy possible way, where you can understand it Commented Jul 7, 2017 at 2:44

2 Answers 2

2
map = %{
  shares: [%{name: "Junaid0"}]
}
add_shares = [%{name: "Junaid1"}, %{name: "Junaid2"}]

Straigt list concatenation

%{map | shares: map[:shares] ++ add_shares}

Comprehension

%{map | shares: (for share <- add_shares, into: map[:shares], do: share)}

Enum.reduce

Enum.reduce(add_shares, map, fn share, acc ->
  %{acc | shares: [share | acc[:shares]]}
end)

Enum.into

%{map | shares: Enum.into(add_shares, map[:shares], & &1)}

FWIW:

Adding an element to already existing list:

list = []
element = 42

list1 = [element | list]
#⇒ [42]
list2 = [element | list1]
#⇒ [42, 42]

Answer to updated question (simplified):

Enum.reduce(email_array, {[], []}, fn
  email, {shares, changes} = _acc ->
    with {:found_user, sharee} <- ensure_user(email) do
      case :camera_share do
        {:ok, camera_share} ->
           {[camera_share | shares], changes}
        {:error, changeset} ->
           {shares, [changeset | changes]}
      end
    else
      # basically the same
      # make sure ALL branches return an acumulator
    end
  end)

The above would return a tuple {shares, changes} that you might assign to anything.

Sign up to request clarification or add additional context in comments.

15 Comments

Actually, the question is starting from that point. [%{name: "Junaid1"}, %{name: "Junaid2"}] How can all these values first can be in a list? as after one operation,, user is getting one %{name: "Junaid1"}, question is how to save all of these in a list
add_shares = [%{name: "Junaid1"}, %{name: "Junaid2"}] the goal is to achieve this state.. how to hold these values together in an Enum.each or map
Thanks for your answer really appreciate it. But I have updated it.. a little bit more explanation that What I am trying to achieve.
This is the actually place where I am stuck at the moment [%{name: "Junaid1"}, %{name: "Junaid2"}] how to make that
as you see everything is in an each loop, where I want together all camera shares and errors together
|
0

You can create a simple method like this:

def add_share(%{shares: shares} = map, share) do
  %{ map | shares: [share | shares] }
end

And call it to add items to the lists in the map:

%{shares: []}
|> add_share(%Cs{name: "Junaid1"})
|> add_share(%Cs{name: "Junaid2"})

Result:

%{shares: [%Cs{name: "Junaid2"}, %Cs{name: "Junaid1"}]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.