lainchan / λ
(untitled)
Eraz · 2025-10-05 12:20
What’s the most elegant piece of code you’ve ever written or seen?
Like, that ‘holy soykaf the universe just compressed itself’ moment.
Iverson · 2025-10-05 13:53
If I want to be generic I'd say that "Maxwell's Equations of Software" are a strong contender. But honestly, I believe that J's Incunabulum is the thing. It's an APL style "thing" written in plain old C. It's beautiful, because it's so dense.
Junichi Fujiwara · 2025-10-06 16:15
https://ideone.com/9iPro0
main = print =<< (+) <$> readLn <*> readLn
Case · 2025-10-07 04:36
https://ideone.com/UK1bFX
(print (+ (read) (read)))
Wirth · 2025-10-07 04:41
https://ideone.com/MLGkQZ
read a; read b; echo $((a + b))
St. IGNUcius · 2025-10-10 13:15
>>43279
>return negated void
How is that elegant?
Ivan Horvat · 2025-10-10 16:39
>>43297
I don't think OP's screenshot was supposed to represent the topic to be honest
Cypher · 2025-10-11 03:09
>>43297
That main is meant to fill the void in your soul.
Jim Hilger · 2025-10-11 04:04
Man I love Zig.
>>43297
>return negated void
It's a void function that has a possibility of failing due to the add function. The `add` function OP wrote will either return an error or a u8 (even though there's nothing in there that will make the function fail).
Yu-ichi · 2025-10-13 06:02
>>43279
I don't think Zig is a good language; it seems to be more of an imitation than something addressing the core issues of UNIX and C. It lacks semantic clarity, appearing instead as a patchwork of features. Notably absent are try blocks or catch mechanisms for error handling, which is done on an expression-by-expression basis – a suboptimal solution that introduces code clutter, especially when dealing with nested errors. Additionally, resource management (defer) requires manual handling, making it feel more like a superficial C skin than a thoughtfully designed language.
Hacker · 2025-10-13 19:28
>>43297
>return negated void
Please learn the thing before you criticize the thing. Also I'm not aware that there's any reliance on the JVM.
>>43316
Submit a PR or feature request. Iirc you can use a switch block where you catch the error. But Zig's goal is to be minimalistic with its syntax and undefined behavior which makes it a promising modern C alternative so I hope they can get to version 1.0 eventually.
Also comptime is pretty neat.
drop · 2025-10-14 07:00
>>43317
>Submit a PR or feature request.
You can't solve the fundamental philosophical problems with a PR, the primary issue is conceptual poverty. ZIG is not a C alternative it's just another C.
Nemissa · 2025-10-15 11:52
>>43316
>>43320
To be fair, Zig is designed to be an extremely low-level language. Its approach to errors, to me, seems less like a stubborn refusal to have exceptions fully featured exceptions, and more like a compromise to retain their functionality in a domain where they're often unviable.
Ivan Horvat · 2025-10-15 17:02
>>43327
You don't need traditional exceptions to implement robust error handling. Swift, for instance, utilizes a construct similar to Zig's pattern matching and sum type return mechanism but it has a exception like control flow construct that is more clean and semantic that pattern matching.The notion that a language should strictly adhere to specific niches is misguided; there's no inherent reason why Zig couldn't incorporate high-level constructs. This seems more like an attempt to rationalize design flaws.
Koichiro · 2025-10-16 10:04
>>43279
Game of life in APL is very beautiful. It got me interested in APL but learning APL is still in my to-do list. You can find a lot of learning resources from the APL wiki. I suggest the content from Dyalog's site. (Alternatively, there is the work-in-progress APL MOOC when/if it's ready).
apl
life ← {⊃1 ⍵ ∨.∧ 3 4 = +/ +⌿ ¯1 0 1 ∘.⊖ ¯1 0 1 ⌽¨ ⊂⍵} >https://aplwiki.com/wiki/Conway%27s_Game_of_Life
If you don't like new symbols, you should try the J programming language. It's basically like APL but it uses ASCII and more traditional programming keywords. There is also a book on J called "Fractals, Visualization and J, Fourth Edition, Part 1".
Delilah · 2025-10-16 10:55
>>43327
Ladies and getleman, I present to you...
Maybe (though un/boxing is slowish, even if automatic, but not as slow as pseudo-exceptions for sure).
nil · 2025-10-17 03:59
>>43333
I am starting to think one could represent Collatz Conjecture as a proper sorting algoritm as well - given Conway was working on Collatz Conjecture at some point. No hailstorm numbers, no halting problem, just a joke sorting algoritm similar to "Stalin's Sort".
Crash Override · 2025-10-17 04:06
>>43279
My line of code is so elegant Lain-chan refuses to allow it on this board since it's in Pascal and causes system crash.
Thing is, such a line of code, a mere 18 symbols, is supposed to be used to *reboot* a controller which reboots once crashing thanks to a specialized hardware - yet lacks a special remote reboot command.
John Doe · 2025-10-17 09:25
>>43334
There is no boxing with Maybe (Option) in C++ and Rust. Did Zig fuarrrk up here? Do they not stackalloc structs?
Lunch · 2025-10-17 11:08
>>43337
I don't know if zig has Option, I was just joking that it's !u8 meaning an error of u8 approach is superspecific Option monad.
Netrunner · 2025-10-17 14:44
>>43338
Maybe their !t and "try" are syntax sugar for Either monad. That's a nice way to do it. I mean try without catch is just doing something with Right case of Either, and catch is a handler for Left (in Haskell terms).
HELIOS · 2025-10-17 14:53
>>43339
after some reading, it seems like it's Either in clever disguise, difference being that trying to ignore potential Left in computation will not short-circuit but instead crash-and-burn at the compile stage.
Lady Jessica · 2025-10-18 11:52
>>43335
Sounds interesting, would you elaborate?
St. IGNUcius · 2025-11-15 13:32
>What’s the most elegant piece of code you’ve ever written
Any branchless code looks elegant so of the top of my head here's an example:
for(i = 0; i < font.height; i++)
{
b = font.data[(ch - 32) * font.height + i];
for(j = 0; j < font.width; j++)
{
// if((b << j) & 0x8000) {
// data[0] = color >> 8;
// data[1] = color & 0xFF;
// } else {
// data[0] = bgcolor >> 8;
// data[1] = bgcolor & 0xFF;
// }
/* Branchless version
* If(MSB of b << j is 1 then we use color else bgcolor)
* Use bitwise operations to directly set last 8 bits without a conditional */
mask = ((-((b << j) & 0x8000)) >> 8);
// Set all bits to negative of MSB
mask = (mask >> 7) - 1;
// Calculate the high byte and low byte of the data without conditionals
data[0] = (color >> 8 & ~(mask)) | (bgcolor >> 8 & mask); // high byte
data[1] = (color & 0xFF & ~(mask)) | (bgcolor & 0xFF & mask); // low byte
WriteData(data, sizeof(data));
}
} This soykaf gave me a noticable performance boost for drawing words on a slow ass microcontroller. Sadly it rarely makes sense to do branchless code but when it does you feel like a genius
Church of CTMU · 2025-11-20 06:02
>>43279
The whole of Object Orientation in 38 cons cells of standard Scheme.
(define (fix p b) (define f (p (lambda i (apply f i)) b)) f)
(define (mix c p) (lambda (f b) (c f (p f b)))) http://fare.tunes.org/files/cs/poof.pdf
Hiro Protagonist · 2025-11-20 13:20
hash table · 2025-11-21 12:55
>>43546
Kool stuff, reminds me of https://wiki.tcl-lang.org/page/Thingy%3A+a+one%2Dliner+OO+system
Dozer · 2025-12-11 16:26
if you have a rust struct, (say, a struct that contains your app config options) you can mostly use macros and derive serialization/deserialization code via Serde for a myriad of targets, such as Json.
You can also use the schemars crate or others to generate a JSON schema that describes valid JSON objects matching yours.
You can then use a javascript library to generate a HTML form from the json schema.
Voila, you have a web interface! (maybe to tweak your app config parameters? Who knows?)
Very cool if you want to impress your colleagues
Splicer · 2025-12-11 19:10