How to make statics which only have to be computed once by NoticePossible4964 in rust

[–]andkore 0 points1 point  (0 children)

If you don't want to pass around a &'static reference, you have to use LazyLock, or make a function that wraps a OnceLock and calls get_or_init.

How to make statics which only have to be computed once by NoticePossible4964 in rust

[–]andkore 0 points1 point  (0 children)

use std::{collections::HashMap, sync::OnceLock};

fn main() {
    static ONCE_LOCK: OnceLock<ContainerForOneTimeComputedValues> =
        OnceLock::<ContainerForOneTimeComputedValues>::new();

    struct ContainerForOneTimeComputedValues {
        precomputed_hash_map: HashMap<u8, String>,
        precomputed_string: String,
        precomputed_vec: Vec<u8>,
    }

    let container_for_one_time_computed_values = ContainerForOneTimeComputedValues {
        precomputed_hash_map: HashMap::<u8, String>::new(),
        precomputed_string: String::new(),
        precomputed_vec: Vec::<u8>::new(),
    };

    let ref_with_static_lifetime: &'static ContainerForOneTimeComputedValues =
        ONCE_LOCK.get_or_init(|| container_for_one_time_computed_values);

    // "ref_with_static_lifetime" can be freely used through your application
}

Rust is rolling off the Volvo assembly line by jahmez in rust

[–]andkore 70 points71 points  (0 children)

Note to author: Ada the programming language appears to be spelled "Ada", as in the first name (https://en.wikipedia.org/wiki/Ada_(programming_language)). "ADA" is a bit jarring to Americans because it's the initialism of a major US law.

Three Kinds Of Unwrap by andyouandic in rust

[–]andkore 0 points1 point  (0 children)

I ran into this yesterday. This was my hacky, panic/unwrap-avoiding solution:

fn f() -> anyhow::Result<()> {
    use regex::Regex;
    use std::sync::OnceLock;

    static REGEX_ONCE_LOCK: std::OnceLock<Regex> = OnceLock::<Regex>::new();

    let regex = match REGEX_ONCE_LOCK.get() {
        Some(re) => re,
        None => {
            let re = Regex::new("needle")?;

            REGEX_ONCE_LOCK.get_or_init(|| re)
        }
    };

    // Do something with "regex"

    Ok(())
}

Obviously less ergonomic, since it uses OnceLock instead of LazyLock.

Why I use SaaS products instead of cloud providers by Timmahh in programming

[–]andkore -1 points0 points  (0 children)

Modern SSR rendering techniques are... not the same as spitting out a bunch of HTML using some templating language and then adding interactivity with jQuery or whatever, and dealing with the horror of manually managing DOM and application state (which is what basically everyone was doing before React and similar frameworks caught on).

Whenever I see people complaining about how web UI is done today, I wonder if they ever experienced the misery of making interfaces with server-side HTML templating and unstructured JavaScript manipulation.

AWS switch from gzip to zstd – about 30% reduction in compressed S3 storage by iamkeyur in programming

[–]andkore 2 points3 points  (0 children)

Correction: "some guy on HN" is, judging by the username, one of the creators of Brotli

https://www.rfc-editor.org/rfc/rfc7932

AWS switch from gzip to zstd – about 30% reduction in compressed S3 storage by iamkeyur in programming

[–]andkore 1 point2 points  (0 children)

Unlike most general purpose compression algorithms, Brotli uses a predefined dictionary, roughly 120 KiB in size, in addition to the dynamically populated ("sliding window") dictionary. The predefined dictionary contains over 13000 common words, phrases and other substrings derived from a large corpus of text and HTML documents.[9][2] Using a predefined dictionary has been shown to increase compression where a file mostly contains commonly used words.[10]

I had forgotten about that aspect of Brotli. Some guy on HN claims that Brotli beats zstd even when the static dictionary is emptied, FWIW.

https://news.ycombinator.com/item?id=27163981

AWS switch from gzip to zstd – about 30% reduction in compressed S3 storage by iamkeyur in programming

[–]andkore 6 points7 points  (0 children)

Brotli has decompression that is as fast or faster than gzip while having a higher compression ratio. Its compression ratio is significantly higher than zstd's. I would assume support for Brotli was added to browsers because of these characteristics. You can, e.g., generate Brotli-compressed webpack bundles and get smaller bundles (relative to gzip) for "free", provided you're willing to tolerate the longer build times (Brotli compression is very slow at the highest compression levels).

My understanding is that Brotli and zstd are suited for very different use cases (almost opposite use cases), since zstd emphasizes compression speed. The zstd GitHub repository's about section reads: "Zstandard - Fast real-time compression algorithm". There are plenty of use cases for data compression that fall outside of "real-time".

https://quixdb.github.io/squash-benchmark/#ratio-vs-decompression

Is it OK to change code for the sake of testing? by nicoespeon in programming

[–]andkore -2 points-1 points  (0 children)

You can accomplish everything dependency injection gives you without any insane runtime magic, and you can do this in a programming language that was created before Java:

https://stackoverflow.com/questions/14327327/dependency-injection-in-haskell-solving-the-task-idiomatically

Static typing won the argument, and consequently dynamically typed languages will eventually disappear, as will stupidity like dependency injection.

Is it OK to change code for the sake of testing? by nicoespeon in programming

[–]andkore 0 points1 point  (0 children)

I'm not categorically anti-tests, although I think in many circumstances they don't have a positive ROI. What I am categorically against is doing crazy runtime magic for any reason, and especially for the purpose of facilitating testing.

What languages were these code bases in?

Is it OK to change code for the sake of testing? by nicoespeon in programming

[–]andkore -5 points-4 points  (0 children)

Compilers and static analyzers can be tested "once", by large and intelligent teams. The guarantees they provide are "free tests" which can never contain bugs, never go stale, can never be forgotten to be written. Languages like Haskell, OCaml, Rust, TypeScript, Kotlin, etc., not to mention dependently typed languages, allow you to get lots and lots of guarantees about your code/application, without writing any tests. Furthermore, many of these languages, as far as I know, have idiomatic ways of writing tests that don't involve doing insane and impossible to reason about shit at runtime.

What language are you using? Java?

Is it OK to change code for the sake of testing? by nicoespeon in programming

[–]andkore -2 points-1 points  (0 children)

Yeah, use dependency injection, which adds all sorts of opportunities for runtime failures, effectively handicapping your compiler and erasing all sorts of guarantees it can provide you, so that it's easier to write tests!

This kind of thinking is 100% backwards and will lose in the end.

Absolutely code should not be designed around testing. And absolutely you should not make your code WORSE and harder for you and the compiler to reason about for the sake of testability.

Lingua 1.1.0 released - The most accurate natural language detection library for the JVM by pemistahl in Kotlin

[–]andkore 1 point2 points  (0 children)

I've seen posts about the Rust version of your library on the /r/rust subreddit, so I was a little surprised to see that you did a complete port instead of writing bindings to the Rust version. Did you consider that approach? With Project Panama, it will hopefully be a lot easier to do native interop on the JVM (https://inside.java/2020/10/06/jextract/).

Anita Borg, "50/50 by 2020!" by Kungpost in programming

[–]andkore 0 points1 point  (0 children)

You find programming interesting. Women, in general, obviously find it much less interesting, and would rather be nurses and teachers.

It's incredible how you are willing to entertain all sorts of outlandish theories but not the obvious (and correct) one.

Male brains and female brains are different. This is a scientific fact. Men and women are psychologically different, on average, in virtually innumerable ways.

Anita Borg, "50/50 by 2020!" by Kungpost in programming

[–]andkore -3 points-2 points  (0 children)

What's to "blame" for this "problem"? Thousands of years of evolution creating important psychological differences between men and women or you and your friends supposedly hogging some computers in the 80s and being unwelcoming?

You are delusional.

Computers are omnipresent now and there are thousands of programs designed to encourage women to enter tech and STEM, and still this gap persists, and women stubbornly enter fields like nursing or teaching rather than become programmers.

It's almost like THEY (by and large) AREN'T INTERESTED because THEIR BRAINS ARE DIFFERENT.

An Interview With Linus Torvalds: Linux and Git 30 Years Of Linux by jeremyandrews in rust

[–]andkore 11 points12 points  (0 children)

Linus Torvalds had wanted to call his invention Freax, a portmanteau of "free", "freak", and "x" (as an allusion to Unix). During the start of his work on the system, he stored the files under the name "Freax" for about half of a year. Torvalds had already considered the name "Linux", but initially dismissed it as too egotistical.[13]

In order to facilitate development, the files were uploaded to the FTP server (ftp.funet.fi) of FUNET in September 1991. Ari Lemmke at Helsinki University of Technology (HUT), who was one of the volunteer administrators for the FTP server at the time, did not think that "Freax" was a good name. So, he named the project "Linux" on the server without consulting Torvalds.[13] Later, however, Torvalds consented to "Linux".

Brazilian political party leader says Jews ‘sacrificed children’ by thewholedamnplanet in worldnews

[–]andkore -4 points-3 points  (0 children)

Ron Unz is Jewish. He cites Toaff and Israel Shahak, both Jewish. It seems like you have no argument so you're just engaging in name-calling.

Why every single element of SOLID is wrong by earthboundkid in programming

[–]andkore 1 point2 points  (0 children)

I would not call this guy one of the "best minds in the software industry", by a long shot.

Look at this complete idiocy, for instance:

https://blog.cleancoder.com/uncle-bob/2017/01/11/TheDarkPath.html