nezzart

nezzart

What's the difference between variable binding and assignment?

What’s the difference between variable binding and assignment? How does that work on a low level?

Most Liked

OvermindDL1

OvermindDL1

Seems pretty good. :slight_smile:

I’d think the most ‘pure’ way of thinking of bindings are as unevaluated closures, so even doing just a = 4 means define a 0-arity function a in the current scope with the running environment that just returns a 4 and does nothing else, or that b = a * 64 is a 0-arity function like the above and uses the a in the current scope, calls it, takes its value and returns it multiplied by 64. In old functional design see:

let tester =
  let a = 4
  let b c = a * c
  let d e f = e * b f
  d 4 4

Is essentially just like a 0-arity function a that returns an integer, a 1-arity function b that returns an integer, a 1-arity function d that returns a 1-arity function <anonymous> that returns an integer, and the top 0-arity tester function that returns an integer. In Functional programming everything is a Function in concept. :slight_smile:

sysashi

sysashi

As I understand it (read it with scepticism), in case of Elixir:

  1. binding would be an expression, assignment is statement.
  2. In Elixir = is basically a pattern matching with binding(?)
  3. Since BEAM has only immutable values, binding making much more sense. Maybe I’m simplifying a lot - assignment assigns some literal (textual representation) to a place in memory, so changing variable X changes data in memory it was pointing to. In case of binding changing X changes only it’s pointer (so it points to another place in memory and does not affect original data it was pointing to)

Hope I did not write some non sense :smiley:

AstonJ

AstonJ

Great question :023:

I’ve also wondered the same thing.

So would it be safe to say the following?

Binding has to do with giving names to things (or values) in a given well delimited context. Assignment is about storing things (or values) in some location (a variable)

From: terminology - What is the difference between assignment, valuation and name binding? - Computer Science Stack Exchange

smpallen99

smpallen99

The code you showed works. However, we usually try to reduce the number of single use variables. So, the “Elixir way” of writing that code is

var1 = 
  123
  |> get_new_value(:something)
  |> get_new_value(:something2)

Furthermore, if it was the last statement in a function, then we would drop the var1 like this:

def my_fun do
  # ...
  123
   |> get_new_value(:something)
   |> get_new_value(:something2)
end
NobbZ

NobbZ

Since everything is immutable and also there is nothing shared between 2 BEAM processes[1], there is no issue with that code in terms of concurrent processing.

[1] Well, in fact, large binaries are shared across processes, but as they are still immutable we do not get any problems because of this as well.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement