News for hackers. Stuff that matters.
Re:John Carmack on Inlined Code by ninetyninenine on Wednesday October 09, 2024 @04:06AM (#41785590)
Not according to jon carmack. He stated he switched to pure functional programming in the intro which is basically stating all his logic is in the form of unit testable pure functions.
Re:John Carmack on Inlined Code by foldr on Wednesday October 09, 2024 @05:41AM (#41786049)
Nothing about pure functional programming requires unit testing all of your functions. You can decide to unit test larger or smaller units of code, just as you can in any other paradigm.
Re:John Carmack on Inlined Code by ninetyninenine on Wednesday October 09, 2024 @11:13AM (#41788760)
In pure functional programming a pure function is unit testable by definition of what a pure function is. I never said it requires functions to be tested. Just that it requires functions to be testable.

In other paradigms do not do this. As soon as a module touches IO or state it becomes entangled with that and NOT unit testable.

Is it still testable? Possibly. But not as a unit.

Re:John Carmack on Inlined Code by int_19h on Wednesday October 09, 2024 @07:16PM (#41793838)
How do you unit test a local function that is a closure in pure functional code?
Re:John Carmack on Inlined Code by ninetyninenine on Wednesday October 09, 2024 @07:29PM (#41793930)
Of course you can't unit test things with restricted scope.

f(x) = x + 2 + 4

How do you unit test x + 2 or (+ 4) even if the operation is pure? You can't. Because it's not callable. It's the same thing with the closure.

The only things that are testable are things on unrestricted scope. AKA global scope. Think about what happens if you have a "closure" on global scope.

If you really want to test it then your "unit tests" which typically live on global scope, need to be moved to local scope. That's just the rules of scope.

There is one special case here. If the parent function returns the local function as a value. But even in this case the parent and local function have to be treated as a unit. The unit test will involve first calling the parent, then calling the local. The parent and child function form a "unit" thanks to shared state and the parent is essentially "moving" the local function into global scope.

Generally best practice is to use combinators if you want to maximize the granularity in which you can modularize your logic. I would even argue that closures stradle the line between pure and impure, so I actually avoid closures whenever possible.

Re:John Carmack on Inlined Code by nuancebydefault on Wednesday October 09, 2024 @08:38AM (#41787252)
I found this [] article of Carmack. While reading, I understood there is a large set of gray shades to pureness of "pure functional" code. He calls being functional a useful abstraction, a function() is never purely functional.

[] https://web.archive.org/web/20120501221535/http://gamasutra....

Re:John Carmack on Inlined Code by ninetyninenine on Wednesday October 09, 2024 @11:10AM (#41788727)
When people say pure functional programming they never mean the entire program is like this.

Because if it were your program would have no changing state and no output.

What they mean is that your code is purely functional as much as possible. And there is high segregation between functional code and non functional code in the sense that state and IO is segregated as much as possible away into very small very general functionality.

Re:John Carmack on Inlined Code by tightbookkeeper on Wednesday October 09, 2024 @09:39AM (#41787862)
> pure fp

No he didn’t.