Programming thread

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
preference
i know lua enough to write an interpreter/off-spec vm for it without checking the reference
the way i have setup my machines is incredibly autistic and a large part of the python ecosystem really hates it
i use lua(jit) because it's lightweight, flexible and fast and will run on anything i give it
generally if i feel i need something like python i will end up using node (usually for basic json processing and api calls) or rust (for threading or "data loss risk" scripts)
python is fine if you're familiar with it although i personally dislike python
I see python as just a better bash and I don't use it for anything else. I can't imagine writing a whole application in it or any weakly typed language like Lua or js. Maybe Typescript if someone were holding a gun to my head.
 
I see python as just a better bash and I don't use it for anything else. I can't imagine writing a whole application in it or any weakly typed language like Lua or js. Maybe Typescript if someone were holding a gun to my head.
You can use type hints in Python now to get bootleg static typing. I don't think anyone actually does though...
 
You can use type hints in Python now to get bootleg static typing. I don't think anyone actually does though...
I use type hints religiously, even though they're largely toothless. MyPy is interesting, but it's a buggy mess in my experience so it's not where it needs to be.
 
I can't imagine writing a whole application in it
You can use type hints in Python now to get bootleg static typing. I don't think anyone actually does though...
I would have liked if python didnt do the cringe 'oh just duck type' thing, but its not too huge of an issue the way I usually use it. For actual applications I have been taking a liking lately to using python to drive a PySide GUI and using the python C API to glue that to my actual application. Most of the complexity lives in C or C++ and then the annoying dynamic GUI stuff all lives in python land.
 
  • Like
Reactions: std::string
This might be a really dumb ignorant question. I am someone who knows a bit about programming and I have about 200-300 hours programming stuff like mathematical models and AI stuff in Python. So while the underlying stuff is complicated, the actual programming is not.

Python is a OOP language and I thought that meant I was doing OOP stuff but when I read more into what OOP is and I compared it to what I was doing I was doing a lot of Functional stuff especially because I really like using Numpy and I unironically think Vectors are easier to make than loops. I could use stuff like Classes but I inherently dislike them and find the piece by piece approach of functional programming to be easier to think about because I think it can be broken into smaller pieces.

But my question relates to functional languages and how they manage things like states, using something like Recursion to effectively implement OOP design patterns.

My question is can't you just make something a lot like OOP in Functional languages using an Array for every variable to represent the variable value and any OOP relationships? My idea is that the left most element of the array contains the variable value and you have enough 1s or 0s to the right of it to mark if a variable is in this class or has a certain trait or similar. And then you can duplicate everything to the right of the variable value and assign to another variable etc etc. If you want to implement something like state you can simply use an if trait_index == 1 then x else y etc. If if statements are too slow you can achieve a similar sort of thing using Digital Logic etc.

Is this a dumb question based on a retarded misunderstanding? Did this even make any sense whatsoever?

Please let me know.
 
  • Like
Reactions: Marvin and y a t s
Python is a OOP language and I thought that meant I was doing OOP stuff but when I read more into what OOP is and I compared it to what I was doing I was doing a lot of Functional stuff especially because I really like using Numpy and I unironically think Vectors are easier to make than loops. I could use stuff like Classes but I inherently dislike them and find the piece by piece approach of functional programming to be easier to think about because I think it can be broken into smaller pieces.
OOP encourages breaking things down into smaller pieces too. In the Smalltalk community, for example, methods are typically encouraged to be as short as possible. Knowing all of the tools available to you is wise. Anyway, I can't answer your question but Googling 'handling state in functional programming' turns up some interesting results, like this Hacker News thread.
 
  • Informative
Reactions: Napoleon III
My question is can't you just make something a lot like OOP in Functional languages using an Array for every variable to represent the variable value and any OOP relationships? My idea is that the left most element of the array contains the variable value and you have enough 1s or 0s to the right of it to mark if a variable is in this class or has a certain trait or similar. And then you can duplicate everything to the right of the variable value and assign to another variable etc etc. If you want to implement something like state you can simply use an if trait_index == 1 then x else y etc. If if statements are too slow you can achieve a similar sort of thing using Digital Logic etc.
You are correct for simple values which can be done by using linear algebra. Given a state a[1,2,1] where 1 represents a value of true and a[0] is the location of that value. A static value representing the function applied to state can be defined using another matrix. for our example f[0,2,-1] will change the states through simple addition. f[] is transformed using the state value in some way. a[0] x f = f if true, and a[0] x f = [0,0,0] if false. f2 is then applied to the original state a + (f x a[0]) = [1,4,0]. The same can be done for any other function you can represent using linear algebra.

The problem is that in OOP or even stateful languages pointers in memory are used to refer to other Objects, and that reference can exist on multiple objects. This isn't used very much in python, but in something like Java or C. It is used to share singletons or factories which only one can exist of. For example in large database applications, many classes can share the same transaction manager. Sure, you could store that object in a particular address in memory, but you would have to write a function to de-reference it from the pointer that you would need to pass with every operation. It's not pretty.

Edit:
You are not dumb for not knowing this. Many programmers who just pass by value don't know this, and struggle with the concepts of passing by value vs passing by reference. The funny thing is that dictionaries in python are mostly passed by reference as long as they are defined on the class level.
 
Last edited:
Yes and oftentimes that is more performant and overall better
Is this a standard practice that is used commonly? I have only seen Recursion to accomplish this granted I'm quite inexperienced and the Hacker News thread from what I have read hasn't answered very much.

I did try and look around first to find an answer to my question.
 
Is this a standard practice that is used commonly?
What's a concrete instance of that being done?
I am mostly referring to the "arrays" part, dividing an apparent "object" into separate arrays that can be processed sequentially by the same procedure is generally superior because any given operation generally speaking will only use a portion of the state of an object, meaning all of the rest of the structure is unhelpful and impacting your memory bus efficiency as irrelevant chunks of the object get paged into the cache.

If you have big complicated objects, there are generally several logical chunks it could be sliced up into to ensure the memory bus is pretty much only copying information useful to whatever its currently doing. Note this does not generally apply to scripting languages like python as those are so horrendously bad on the memory bus by default that there is nothing you can do about it.

See mike acton's cppcon 2014 talk where he disparages object oriented design and talks about efficiency in general at the tail end of the golden age of video games (not that anyone knew that at the time): https://www.youtube.com/watch?v=rX0ItVEVjHc

I have only seen Recursion to accomplish this
Recursion is generally considered kindof haram since you are blowing out the stack oftentimes unnecessarily
 
My question is can't you just make something a lot like OOP in Functional languages using an Array for every variable to represent the variable value and any OOP relationships? My idea is that the left most element of the array contains the variable value and you have enough 1s or 0s to the right of it to mark if a variable is in this class or has a certain trait or similar. And then you can duplicate everything to the right of the variable value and assign to another variable etc etc. If you want to implement something like state you can simply use an if trait_index == 1 then x else y etc. If if statements are too slow you can achieve a similar sort of thing using Digital Logic etc.
Sure. You can do pretty much anything purely functionally with enough determination. I would rather use something like a C struct or key/value pairs though. However, it's all just passing around values one way or another instead of individual references to a certain memory region.

The nuance here is how the statefulness is achieved. With a functional approach, the state is passed from one function to the next as an individual collection of values. With OOP, it's all a collection of pointers, so any modifications (mutations) to a value affect all functions that make reference to that value in memory. Functional is really good when you're doing async stuff because you can better guarantee the state of the data a given function accesses when you don't have 20+ functions writing to and reading from the same area in memory.

With C, you often have to pass by reference regardless, so the line is a bit blurred there. At the core of functional programming is the concept of mutability, and that's the main focus in all of this.

Recursion is generally considered kindof haram since you are blowing out the stack oftentimes unnecessarily
Recursion is really handy. This isn't much of an issue if you do it properly with tail calls. I will note that some languages don't use tail recursion under the hood, so it's very language dependent.
 
If you have big complicated objects, there are generally several logical chunks it could be sliced up into to ensure the memory bus is pretty much only copying information useful to whatever its currently doing. Note this does not generally apply to scripting languages like python as those are so horrendously bad on the memory bus by default that there is nothing you can do about it.

Does numpy or other such code that's a wrapper around systems programming language evade this limitation?

See mike acton's cppcon 2014 talk where he disparages object oriented design and talks about efficiency in general at the tail end of the golden age of video games (not that anyone knew that at the time): https://www.youtube.com/watch?v=rX0ItVEVjHc

I've seen quite a few demos on Commodore 64 and Amiga and figured out how to run the demos on my own which is somehow more "magical" even if it is through an emulator because you have to put in a little effort vs. just hitting the play button on a video. It's quite impressive what functionality the coders of demos (and some games too) managed to (EDIT: and still manage to) cram into what is now considered severely lacking hardware.

Recursion is generally considered kindof haram since you are blowing out the stack oftentimes unnecessarily

Tail calls were just mentioned but YMMV on whether you get it. It really got its start with Scheme AFAIK and I believe the Scheme standard requires tail call optimization to be implemented when applicable. Racket has probably the most well-developed Scheme implementation around (multiple languages now, actually) and has TCO. Python doesn't have TCO natively but some guy figured out how to implement it using a decorator. Realistically though if I wanted to implement fac(n) in Python I'd just use a while loop rather than using that hack or expanding the maximum recursion depth. OCaml, which is a functional programming language that prefers immutable data but lets the user use mutable data without faggy category theory concepts, has TCO and I suspect that's probably par for the course in most FP languages today.

Functional is really good when you're doing async stuff because you can better guarantee the state of the data a given function accesses when you don't have 20+ functions writing to and reading from the same area in memory.

I understand Clojure really focuses hard on that sort of thing but the thing about Clojure is that error messages come from transpiled Java code rather than from the Clojure source. The line numbers don't even match. That was more or less an immediate deal-breaker. Erlang and Elixir also have FP and a huge async focus but I really haven't used either.
 
Last edited:
Does numpy or other such code that's a wrapper around systems programming language evade this limitation?
Afaik numpy has no real problems, also its basically compiled C code (idk maybe some other languages are involved) so its side stepping the issue by doing all of the real work outside of python, which is actually pythons #1 strength is its ability smoothly pass work between modules like that
 
  • Like
Reactions: Belisarius Cawl
Afaik numpy has no real problems, also its basically compiled C code (idk maybe some other languages are involved)

Here's an exact answer:

Screenshot 2024-03-31 at 17-59-42 numpy_numpy The fundamental package for scientific computing...png

so its side stepping the issue by doing all of the real work outside of python, which is actually pythons #1 strength is its ability smoothly pass work between modules like that

Most languages like Python have a C interface or some other FFI or whatever the right term is. When I install R packages I regularly see gcc output. Possibly in an alternate universe something like Ruby could have become the general purpose / production data science language. (And maybe JavaScript would have been replaced by a Scheme dialect like Brendan Eich originally wanted.)
 
  • Feels
Reactions: y a t s
I believe the Scheme standard requires tail call optimization to be implemented when applicable.
That is correct. A proper Scheme implementation can recurse infinitely. To recurse infinitely, one must heavily restrict creation of stack frames where possible. This is typically done using continuation-passing style. It's pretty fun to implement in C as a project. Who doesn't love a good trampoline? :)

Realistically, it's best to use tail recursion as a rule instead of a case by case basis. Plus, I find TCO much easier to chain together in my head and reason about at a glance, since it works similarly to a simple loop.

Racket has probably the most well-developed Scheme implementation around (multiple languages now, actually) and has TCO.
Racket is more like a logical extension to Scheme than an implementation of it. It really is an excellent language, and it's good for getting acquainted with functional programming concepts. I like using it to write compilers and simple parsers. It makes building an AST a breeze.

Haskell is fun too.
 
See mike acton's cppcon 2014 talk where he disparages object oriented design and talks about efficiency in general at the tail end of the golden age of video games (not that anyone knew that at the time): https://www.youtube.com/watch?v=rX0ItVEVjHc
With OOP, it's all a collection of pointers, so any modifications (mutations) to a value affect all functions that make reference to that value in memory. Functional is really good when you're doing async stuff because you can better guarantee the state of the data a given function accesses when you don't have 20+ functions writing to and reading from the same area in memory.
The impetus for me to ask this question is basically that I really don't like too much abstraction. OOP is fast to write complex abstractions for but hard to understand what is going on if you didn't write it. Functional paradigms IMO are easier to understand because you do something piece by piece if I am understanding correctly. Which is why I have never gravitated towards things like classes or traits despite them existing in the language I use.

Does numpy or other such code that's a wrapper around systems programming language evade this limitation?
I'll say that I think if anything like memory or performance are a big issue, Python is generally not the right language. It is about ease of writing something quickly that works. The Python part of my program runs in less than a second and the Linear Programming problem solver I run which is built in C++ is inherently NP hard.

Python doesn't have TCO natively but some guy figured out how to implement it using a decorator. Realistically though if I wanted to implement fac(n) in Python I'd just use a while loop rather than using that hack or expanding the maximum recursion depth.
Well that offers a good explanation for the bug I had in Python when I was doing Recursion. I did fix it by messing with the order things were done in so that it worked. Honestly only took me an hour or two to find and fix which is really not that bad. Basically one element was not being treated as a recursive element correctly and so I needed to change the order things were done in to get it to work correctly.
 
Back