296
you are viewing a single comment's thread
view the rest of the comments
[-] loxdogs@lemmy.world 11 points 7 months ago

can someone explain please?

[-] noli@programming.dev 32 points 7 months ago

In functional programming, everything is seen as a mathematical function, which means for a given input there is a given output and there can be no side effects. Changing a variable's value is considered a side effect and is thus not possible in pure functional programming. To work around this, you typically see a lot of recursive and higher order functions.

Declaring all values as const values is something you would do if you're a diehard functional programmer, as you won't mutate any values anyway.

[-] loxdogs@lemmy.world 4 points 7 months ago

thanks, kinda understand

[-] Mir@programming.dev 2 points 7 months ago

What is the best practice then when you want to update a variable's value?

[-] noli@programming.dev 10 points 7 months ago

Depends on how deep down the rabbit hole you want to go :p

  • creating a new variable that contains the updated value
  • recursion (e.g. it's not possible to make a loop that increments i by 1, but it is possible to turn that loop into a function which calls itself with i+1 as argument)
  • avoiding typical types of operations that would update variable values. For example instead of a for loop that updates every element of a list, a functional programmer will use the map function, which takes a list and a function to apply to each element of that list to create an updated list. There's several more of these very typical functions that are very powerful once you get used to using them.
  • monads (I'm not even gonna try to explain them as I hardly grasp them myself)
[-] DaforLynx@lemmy.zip 4 points 7 months ago

You just dropped a mind bomb on me. Suddenly things make sense :o

[-] shield_gengar@sh.itjust.works 3 points 7 months ago

A monad is just a monoid in the category of endofunctors, what's the problem?

[-] Nomecks@lemmy.ca 1 points 7 months ago

j = i + something

this post was submitted on 21 Mar 2024
296 points (94.8% liked)

Programmer Humor

19450 readers
359 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS