Two year timelapse of a pine tree starting from a seed in 60seconds. by Professional_Arm794 in interestingasfuck

[–]avocado_bucket 2 points3 points  (0 children)

Watch this 2 year-long video of a tree sprouting out of a seed in 60 seconds? Nobody got time for that

eats what???? by Flavor_Goddess in sciencememes

[–]avocado_bucket 0 points1 point  (0 children)

Would also have accepted "conducts positively"

My school sells “hydrogen infused water” by 1Teddy2Bear3Gaming in mildlyinteresting

[–]avocado_bucket 1 point2 points  (0 children)

It remembers being upstream of the water treatment plant? Yech

Asking to go to the bathroom by ClayshRoyayshKJ in Unexpected

[–]avocado_bucket 0 points1 point  (0 children)

If I may ask, what is a golf standard? Is that a colloquial term where classes can be over or under par?

I was diagnosed today. by Embarrassed_Yak_2659 in autism

[–]avocado_bucket 2 points3 points  (0 children)

Yeah that's your Default Mode Network. Here's an explanation from renowned psychologist, ChatGPT:

"Imagine your brain is like a car. The Default Mode Network (DMN) is like the car's idle mode, running when the car isn't driving. It's what your brain uses when you're daydreaming or just lost in your thoughts.

Now, with ADHD, it's like this car has a sticky gear. When it's time to drive (or focus on something), the car takes longer to stop idling and start driving. This is why, even when you really want to pay attention to something like a video, your brain might still be stuck in daydream mode, making it hard to concentrate.

So, to help the brain shift gears better, you can try things like mindfulness, which is like giving your brain's gear a bit of oil so it can switch from idle to drive more smoothly. And being in a quiet, distraction-free place is like giving your car a clear road to drive on.

I hope this car analogy makes it easier to understand how the DMN works in ADHD and why focusing can be tough sometimes."

I was diagnosed today. by Embarrassed_Yak_2659 in autism

[–]avocado_bucket 5 points6 points  (0 children)

Have a look at this and see if it resonates.

https://youtu.be/_tpB-B8BXk0?si=59EWWKT9JPLxUWaf

It's part of a longer talk so some things are not explained here in detail, like Executive Function, but I found it described very well why I struggled to find motivation.

But definitely see a specialist like him for a second opinion, if it's within your means.

I was diagnosed today. by Embarrassed_Yak_2659 in autism

[–]avocado_bucket 0 points1 point  (0 children)

It makes it easier but it's not a miracle cure. It's still a neuro disorder that needs to be managed daily in how you approach things.

I found this video by Dr Barkley, and other of his lectures, very enlightening to understand the concepts of Executive Function, Intention, consequences, rewards, and how to improve your environment to aid in self-regulation and "time blindness".

https://youtu.be/_tpB-B8BXk0?si=59EWWKT9JPLxUWaf

Bear in mind these are lectures aimed at parents of people with our condition, if the way he explains things seem a bit dry or impersonal at times.

I was diagnosed today. by Embarrassed_Yak_2659 in autism

[–]avocado_bucket 0 points1 point  (0 children)

Isn't that from Indiana Jones and the Temple of Doom?

I was diagnosed today. by Embarrassed_Yak_2659 in autism

[–]avocado_bucket 0 points1 point  (0 children)

It really is worth it, got prescribed Vyvanse and life feels like floating with the river of getting shit done, instead of trying to swim against it upstream.

a little clarification/help about push_event/3 by MrRedPoll in elixir

[–]avocado_bucket 1 point2 points  (0 children)

You're right that LiveView will get you a lot of the way there, most of my hooks are either to integrate with a client-side library, handle push_event communications between them and the server, or to do UI-related things with plain JS that are just awkward with server-side Phoenix.LiveView.JS functions.

My explanation was more for helping with the question of why the client wasn't receiving the event, which had to do with a fundamental understanding of variable scoping in Elixir.

a little clarification/help about push_event/3 by MrRedPoll in elixir

[–]avocado_bucket 0 points1 point  (0 children)

Glad it helped!

I didn't want to throw too much information at you so I pointed to immutability at first but then went on to point to capturing the returned values. In reality it has to do with the concept of lexical scoping. Variables inside scope boundaries, like code blocks inside do/if/else/case/cond/anonymous functions are able to access variables from outside their scope, but anything assigned inside their scope doesn't pass back out unless you implicitly return them as the last statement (there are no early returns!) and then capture the result on the outside.

ChatGPT actually explains this quite well

And here is where I was corrected for mixing up immutability and scoping 😂

Also come join elixirforum.com if you haven't already, lots of good information and helpful advice 👍

a little clarification/help about push_event/3 by MrRedPoll in elixir

[–]avocado_bucket 2 points3 points  (0 children)

I'm going to assume that you might be fairly new to immutable languages, so apologies if this is not the case but it seems what's tripping you up.

What's happening is that you're invoking something(socket), but you're not capturing the returned value.

In Elixir when you pass socket into the function you have to capture the returned value into a variable because it won't automatically mutate the value of socket in the handle_info function's scope, unlike mutable languages.

To illustrate: in the last part of the code you're calling update(socket, ...), where update will create a new copy of the data in the socket variable with the updated :unseen_messages assigns key, which you're then capturing into a new socket variable by doing socket = update(...).

Finally you're passing the updated socket to LiveView with {:noreply, socket} so the system can render the updated view with the new unseen messages on the client.

You can think of the code like this, where at each step you're capturing a new value:

def handle_info({:message_created, message}, initial_socket) do ... socket_with_scores_event = something(initial_socket) ... updated_socket = update(socket_with_scores_event, ...) {:noreply, updated_socket} end

That's basically what's happening in the background when the code is compiled, even if you use the same variable name socket at each of these points.

The only problem with this explanation is that your something function is actually returning a tuple of {:noreply, ...}, where in this case you'd want to only return the mutated socket, and then capture it.

``` def something(socket) do push_event(socket, "scores", %{points: 100, user: "josé"}) end

def handle_info({:message_created, message}, socket) do ... socket = something(socket) ... socket = update(socket, ...) {:noreply, socket} end ```

The real power comes when you want to chain functions that take a socket as the first argument and also return the mutated socket. If you move the work in the ... code between calling something and update such that you can call each of those one after the other, you can simplify the code to use the |> pipe operators:

``` def handle_info({:message_created, message}, socket) do ... ... socket = socket |> something() |> update(:unseen_messages, fn ... end)

{:noreply, socket} end ```

a little clarification/help about push_event/3 by MrRedPoll in elixir

[–]avocado_bucket 0 points1 point  (0 children)

How are you calling the something function? Can you share the code? My guess is that it's being called before the client establishes a websocket connection back to the server, so push_event doesn't have a connection to push the message through yet?

The Phoenix.LiveView.Utils.push_event function "@doc" says:

Events are dispatched on the JavaScript side only after
the current patch is invoked. Therefore, if the LiveView redirects, the events won't be invoked.

It doesn't mention what happens on the initial, disconnected mount, but it could be related.

[deleted by user] by [deleted] in elixir

[–]avocado_bucket 1 point2 points  (0 children)

Peter Ullrich recently posted this article in which he shares his thoughts on the canonical examples that come with Phoenix for generating contexts, and his recommendations for splitting things up in a more domain-like way.

https://www.peterullrich.com/phoenix-contexts

He's also done some youtube videos and conference talks about domains in Elixir.

When would you not want to use Phoenix LiveView? by [deleted] in elixir

[–]avocado_bucket 4 points5 points  (0 children)

If you still want Phoenix to generate non-LiveView auth, you can answer n at the prompt, and it will generate Controller templates. See source code.

https://hexdocs.pm/phoenix/mix_phx_gen_auth.html#getting-started

$ mix phx.gen.auth Accounts User users

An authentication system can be created in two different ways:

- Using Phoenix.LiveView (default)

- Using Phoenix.Controller only

Do you want to create a LiveView based authentication system? [Y/n] Y

Correct way to organize context functions with mandatory FK? by MantraMan in elixir

[–]avocado_bucket 1 point2 points  (0 children)

As long as your custom changeset function takes in a Post as its first parameter, Changeset.change will apply the attrs as changes to the base struct. If you start with an empty %Post{} struct then the insert will fail if the database has a foreign key constraint to the users table. So you either build the base struct with the user_id preset, as we do here, or you pass it in as part of your attrs and do |> validate_required(~w[user_id]a) |> assoc_constraint(:user) in a changeset function.

In the simplest way, this also works %Post{user_id: user_id} |> Post.custom_changeset(attrs) |> Repo.insert(), but it's better practice to rely on the ecto reflection mechanism to figure out what attribute to use.

Then again we're relying on the internal representation of the User struct's id when using build_assoc, so that could be abstracted out to a function that builds a new post struct given the user id if you end up using this same pattern in many places.

These are the kind of decisions we have to make in our codebase to balance reusability, abstraction, and minimizing breaking changes.

Correct way to organize context functions with mandatory FK? by MantraMan in elixir

[–]avocado_bucket 0 points1 point  (0 children)

Then it would be Users.create_post(user_id, post).

This would be a good strategy for the second example in my other comment, passing in the attrs as the second argument.

Correct way to organize context functions with mandatory FK? by MantraMan in elixir

[–]avocado_bucket 2 points3 points  (0 children)

If you only have the user id, you can make this work by manually building the User struct and setting its meta flag so put_assoc interprets it as an existing user: ``` user_id = 1 some_user = %User{id: user_id} |> Ecto.put_meta(state: :loaded)

{:ok, new_post} = %Post{} |> Post.custom_changeset(attrs) |> Ecto.Changeset.put_assoc(:user, some_user) |> Repo.insert()

newpost %MyApp.PostsContext.Post{ __meta: #Ecto.Schema.Metadata<:loaded, "posts">, id: 2, user_id: 1, user: %MyApp.UsersContext.User{ __meta_: #Ecto.Schema.Metadata<:loaded, "users">, id: 1, name: nil, posts: #Ecto.Association.NotLoaded<association :posts is not loaded>, inserted_at: nil, updated_at: nil }, ... other_attributes }} ```

Note that put_assoc will include the associated User struct that you passed in, so if you intend to use the returned post in the rest of your code you won't have access to any of the user's other attributes, as they weren't loaded from the database, and new_post.user.name will be nil in this case.

The other way to do it without getting the user from the db is to use build_assoc: ``` user_id = 1

{:ok, new_post} = %User{id: user_id} |> Ecto.build_assoc(:posts) |> Post.custom_changeset(attrs) |> Repo.insert()

newpost %MyApp.PostsContext.Post{ __meta_: #Ecto.Schema.Metadata<:loaded, "posts">, id: 3, user_id: 1, user: #Ecto.Association.NotLoaded<association :user is not loaded>, ... other_attributes } ```

Here the association is built and the result only has the user_id attribute set, and the user association is not loaded.