garrison

garrison

I think I disagree here, a lot of languages allow assignment in if/for, and it’s much more intuitive in Elixir because variables are scoped much more carefully (the values must be returned from the cond block).

0
Post #11
D4no0

D4no0

There are no variables in elixir, and = is a match operator, not assignment as in many other languages. When you start to see people go to town with more advanced matches inside of these flow conditions, it becomes really hard to reason about, even if the intent of that code was simple to begin with.

I think this is one of the reasons why people prefer working with erlang, heard it from multiple people in the last few years, that doesn’t have this kind of magic.

0
Post #12
sodapopcan

sodapopcan

Oh are you talking about this:

if foo = bar() do
  foo
end

?

If that’s the case then ya, I do that very sparingly.

I thought you meant this:

foo =
  if bar() do
    "bar"
  else
    "baz"
  end
1
Post #13
garrison

garrison

Both the Elixir and Erlang syntax references call them variables, but I see what you’re getting at. What I meant was that in another language, say JS, this is confusing:

let x = 0;
if (x =1) { x = 2; }

What is x here? What if we defined x inside the condition? And so on.

But this can’t happen in Elixir because the assignment in the if condition is properly scoped and those scopes are very consistent because the language is designed well.

But certainly I can see a point in Elixir where this could start to get ugly, like this:

cond do
  ({:ok, {x, _y, _z}} = do_thing()) && x -> x
end

Definitely don’t do that lol

3
Post #14
cevado

cevado

you’re right, my bad :pensive:

wait what? :joy:
i changed the old one(cover of an album from a brazilian band called papangu) to the current one(a frame of the video for “freak of the week” from freak kitchen, a swedish band).

1
Post #15
sodapopcan

sodapopcan

I believe it was a browser glitch on my end. It wasn’t rendering properly (even though all others were) and made it look like ones of those avatars people use where it looks like there is dirt on your screen so you try and wipe it :upside_down_face:

1
Post #16
arcanemachine

arcanemachine

To answer the question in the title of the thread: Yes, every project I make accumulates little patterns and representations that I then reuse in other projects.

These build up very quickly, at least one new pattern per week. It’s like I’m building a dialect of idioms. (Of course, I prefer to use existing idioms when possible to preserve a common dialect.)

I don’t really have any specific examples to share since the applications are pretty niche until you actually need them, but I have found through trial and error what others are saying about using || to prevent expensive operations. e.g. use some.thing || some_expensive_operation() instead of Map.get(some, :thing, some_expensive_operation()) to avoid calling the function unless it is absolutely necessary.

2
Post #17
jswanner

jswanner

There is Map.get_lazy/3 for this type of case

2
Post #18
— All posts loaded —