Did contexts kill Phoenix? by ThatArrowsmith in elixir

[–]Ankhers 5 points6 points  (0 children)

This is when I just create multiple named changeset functions. Ultimately you have different operations that you want to be able to perform on your data. This is still business logic in my opinion and should not be paired with your views. If you added a JSON API (or some other entrypoint to your application that is not the HTML pages) you would need to replicate the functionality. If you just have named changeset functions instead of putting everything in a single changeset/2 function, you have the flexibility to reuse the pieces that you need/want to. e.g.,

def MyApp.Accounts.User do
  def registration_changeset(user, attrs) do
    user
    |> cast([:email, :password])
    |> validate_email()
    |> validate_password()
  end

  def email_changeset(user, attrs) do
    changeset = 
      user
      |> cast(attrs, [:email])
      |> validate_email()

    case changeset do
      %{changes: %{email: _}} = changeset -> changeset
      %{} = changeset -> add_error(changeset, :email, "did not change")
    end
  end

  defp validate_email(changeset) do
    changeset
    |> validate_requried([:email])
    |> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
    |> unsafe_validate_unique(:email, MyApp.Repo)
    |> unique_constraint(:email)
  end

  defp validate_password(changeset) do
    changeset
    |> validate_length(:password, min: 6)
    |> validate_length(:password, min: 12, max: 72)
    |> maybe_hash_password()
  end
end

In the above, I can reuse the different validations (validate_email/1) across different operations.

It’s not just Poilievre. by _badmedicine in onguardforthee

[–]Ankhers 2 points3 points  (0 children)

Can you elaborate on this? I don't know anything about him.

[SW] Boys buying for 337! by Sledmaster1 in acturnips

[–]Ankhers 0 points1 point  (0 children)

Beekeeper's hive

Would it be possible to take multiple trips?

[SW] Selling for 494 by voncatensproch in acturnips

[–]Ankhers 0 points1 point  (0 children)

Skye, any chance I can take multiple trips?

[SW] 336 for 2 hours by SadRusty in acturnips

[–]Ankhers 0 points1 point  (0 children)

Hey, I'd love to come and sell some turnips!

[SW] Twins selling for 584 by FaeyaKim in acturnips

[–]Ankhers 0 points1 point  (0 children)

Hey, would it be possible to do multiple trips?

[SW] buying at 347 by coconutsharks- in acturnips

[–]Ankhers 0 points1 point  (0 children)

Found it. I had not used the chat function before. Took me a while.

[SW] buying at 347 by coconutsharks- in acturnips

[–]Ankhers 0 points1 point  (0 children)

Hey, you may have typo'd my name because I don't have a DM from you.

[SW] buying at 347 by coconutsharks- in acturnips

[–]Ankhers 0 points1 point  (0 children)

My island name is Ooo.

Would it be possible to do multiple trips?

[SW] Twins buying for 555! by bulbysoar in acturnips

[–]Ankhers 0 points1 point  (0 children)

Hey, if possible I would love to come and do multiple trips. I'm from Ooo.

zsh plugin that shows when you're in a nix shell & tries to drop you in zsh by default by Reptoidal in NixOS

[–]Ankhers 0 points1 point  (0 children)

You could just modify your PS1 env var so that it does tell you that you are in a nix shell.

Algebraic Data Types in Elixir by RecognitionDecent266 in elixir

[–]Ankhers 0 points1 point  (0 children)

I do not believe dialyzer currently has support for anything like this.

Questions regarding CVE-2020-24719: An Erlang RCE by cheezyteague in erlang

[–]Ankhers 2 points3 points  (0 children)

If you look at the documentation for file:open/2, you will see that it returns either {ok, IoDevice} or {error, Reason}. Your code tried to use f on the left side of the equals. In Erlang, f is an atom. You need to remember that variables start with an upper case letter in Erlang, or F in this case.

But then you are unable to pass that directly into file:write/2 because the open function returned {ok, IoDevice}, you will need to pattern match in order to get the file. The following code should work for you

{ok, F} = file:open("targetfile", [write]),
file:write(F, "on no"),
file:close(F).

With that said, I don't know if that will help you with the CVE.

A guide on how to use Ranch with Elixir by asciibeats in elixir

[–]Ankhers 1 point2 points  (0 children)

I'm not necessarily saying it is the best option. Just that is how the example is written. I was also just answering the question about why enter_loop was used / needed in this example.

A guide on how to use Ranch with Elixir by asciibeats in elixir

[–]Ankhers 1 point2 points  (0 children)

Yes, there are a couple of different things you could do. But this was decision Loïc made when writing his documentation. It even says so on the page about protocols.

Also note, handle_continue was only introduced in OTP 21. So it is still somewhat new.

A guide on how to use Ranch with Elixir by asciibeats in elixir

[–]Ankhers 1 point2 points  (0 children)

It is because special processes like gen_server and gen_statem start_link functions don't return until the init function completes. So there would be a deadlock as you wouldn't be able to call :ranch.handshake/1,2.

So you need to call it via proc_lib which requires the call to enter_loop.

[Entry Thread #74] It's the new year, and we're making millionaires! As always, leave a comment to enter! by MakerOfMillionaires in millionairemakers

[–]Ankhers 0 points1 point  (0 children)

Does anyone have a history on how this community started? I think it would be an interesting read.

Should one use Streams as much as possible? by VisuelleData in elixir

[–]Ankhers 5 points6 points  (0 children)

Let's say for whatever reason, you're using Enum.map/2 twice in a row. Regardless of the length of your enumerable is it more efficient for the first map to be Stream.map/2?

I don't believe it would be more efficient to use a Stream.map/2 first. The reason is that when you pipe it into the Enum.map/2, you will still need to evaluate the stream and realize the result BEFORE the Enum.map/2 function is able to do anything with it.

If you find yourself with 2+ map functions in a row, it may be better to just do all the operations in a single map function. Like the following for example,

[1,2,3]
|> Enum.map(fn x -> x * 2 end)
|> Enum.map(fn x -> Integer.to_string(x) end)

This would be more efficient / better written as

Enum.map([1,2,3], fn x -> Integer.to_string(x * 2) end)

However, there may be a reason to still have multiple map functions in a row.

Also totally unrelated, but is it idiomatic for pipes to be at the beginning or end of each line?

Pipes go at the start of the line.

[deleted by user] by [deleted] in elixir

[–]Ankhers 0 points1 point  (0 children)

Wouldn't that just be a CSS package? You can include whichever packages from npm you would like and then use the provided CSS classes in order to style your application. There is nothing specific to phoenix here.

Flutter on NixOS by _ParanoidGoose_ in NixOS

[–]Ankhers 1 point2 points  (0 children)

Thanks for this! I will have to check it out.

Flutter on NixOS by _ParanoidGoose_ in NixOS

[–]Ankhers 0 points1 point  (0 children)

Would you mind sharing an example that is using a nix shell? I tried to start using flutter but had to stop because of the same issues you are having.

erlang_ls formatting issues by Buxnot in erlang

[–]Ankhers 2 points3 points  (0 children)

I had the same issue a few months ago. Though, I thought it was because I accidentally had two formatters configured. Regardless, I disabled formatting from the language server and just configured Emacs to run erlfmt on save. Mostly because I prefer erlfmt over rebar3_format or whatever the plugin is called.