News for hackers. Stuff that matters.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by j1elo on Wednesday January 03, 2024 @04:46AM (#38852298)
Go still doesn't have Set types. The usual thing to do is using a map of your type to either an empty struct, or maybe a bool, if I got that correctly (have only been using Go for a couple months so far)

Did you implement your own Set type and primitive functions?

Re:50 Algorithms Every Programmer Should Know (Second Edition) by jmaker on Wednesday January 03, 2024 @05:09AM (#38852405)
Yes in Go I think I’m mostly tempted to reinvent the wheel every few lines of code, for the sheer lack of stdlib abstractions I’ve grown used to.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by IggleSniggle on Wednesday January 03, 2024 @07:18AM (#38853214)
This is funny to me, because Go is often touted as a language that has "everything you need" in the stdlib. Of course, I'm coming from the nodejs world, where before you can start working you first need to evaluate and choose: typescript or not typescript and if yes what build tool chain, which test runner, which assertion lib, which mocking lib, etc etc. Not really that long ago when you had to choose which async lib (now you just need to choose which async color).

Thankfully a lot of that stuff is in nodejs now, and there's a commitment to maintain parity with Web APIs which also helps, but there's still a lot of half-done work and path-dependencies on outside libs.

Re:50 Algorithms Every Programmer Should Know (Second Edition) by vitus on Wednesday January 03, 2024 @08:13AM (#38853650)
> This is funny to me, because Go is often touted as a language that has "everything you need" in the stdlib.

My usual example of something missing from the standard library is

    func max(a, b int) int {
      if a > b {
        return a
      } else {
        return b
      }
    }
math.Max only operates on two float64s (due to the absence of function overloads; many standard library functions such as this predated generics), so if you want to avoid casting (and/or potential loss of precision if you're working with int64), you need to reinvent the wheel.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by pyth0 on Wednesday January 03, 2024 @08:25AM (#38853771)
Go has had generic forms of the `min` and `max` functions as builtins since 1.21. https://pkg.go.dev/builtin#max
Re:50 Algorithms Every Programmer Should Know (Second Edition) by j1elo on Wednesday January 03, 2024 @01:03PM (#38857332)
Go 1.21 is the version from which it is noticeable that they are pushing towards (re)implementing things that were lacking before due to missing support for generics.

Like the new slices package, meant to do common operations on slices, that is brand new on 1.21.

I wouldn't be surprised to see more and more new additions leveraging generics in next releases.

Re:50 Algorithms Every Programmer Should Know (Second Edition) by vitus on Wednesday January 03, 2024 @06:23PM (#38861186)
Good to know! I admittedly haven't written much Go in the past year, and while min / max were some of the most egregious examples, I'm sure there are plenty more that still exist.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by prosqlinjector on Wednesday January 03, 2024 @12:00PM (#38856423)
You actually don't need any of that stuff, you can just write .js files and run them in node. The node.js standard library is quite extensive.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by IggleSniggle on Wednesday January 03, 2024 @03:37PM (#38859509)
It's much much better now, especially if you are willing to adopt the experimental modules.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by gchamonlive on Wednesday January 03, 2024 @06:24AM (#38852880)
I did it using maps and generics and implemented a subset of the convenience methods that python has in sets.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by porridgeraisin on Wednesday January 03, 2024 @05:01AM (#38852366)
Most languages implement sets using maps. The only major difference you can optimise for is the fact that in majority usage, keys in maps are likely to exist, whereas sets are used for membership checks. Bar that, you can just as well use a map with a dummy value.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by zogrodea on Wednesday January 03, 2024 @05:49AM (#38852666)
Can you give a reference for that? It sounds very unlikely to me, although I've only read/gone through two relevant standard library implementations: F# and OCaml.

Neither of them use dummy values as you suggest. They have similar implementations (both are AVL trees) but sets really do contain only one value while maps contain two. I would be surprised if many other languages did as you suggest.

Re:50 Algorithms Every Programmer Should Know (Second Edition) by alpaca128 on Wednesday January 03, 2024 @08:10AM (#38853627)
Rust's HashSet is a HashMap mapping to "()", which is Rust's equivalent to "void".
Re:50 Algorithms Every Programmer Should Know (Second Edition) by progbits on Wednesday January 03, 2024 @10:19AM (#38855064)
That is true for the internal implementation details but HashSet provides a different API, it is not just a type alias. So you don't have to worry about semantics of what the values mean (like the bools in Go), and it has no size overhead.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by djur on Wednesday January 03, 2024 @06:25AM (#38852892)
Python, Ruby, Perl. Languages where hashmaps are a builtin.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by brabel on Wednesday January 03, 2024 @07:06AM (#38853139)
Java as well: https://github.com/pengisgood/jdk-source-code/blob/master/sr...

EDIT : and Rust: https://docs.rs/hashbrown/latest/hashbrown/hash_set/index.ht... (this is used by the default implementation of HashSet).

Re:50 Algorithms Every Programmer Should Know (Second Edition) by eesmith on Wednesday January 03, 2024 @07:03AM (#38853119)
Python has its own set type not based on hashmap/dict, and that's been the case for years.

The set implementation points out some of the differences, at https://github.com/python/cpython/blob/main/Objects/setobjec... :

   Unlike the dictionary implementation, the lookkey function can return
   NULL if the rich comparison returns an error.

   Use cases for sets differ considerably from dictionaries where looked-up
   keys are more likely to be present.  In contrast, sets are primarily
   about membership testing where the presence of an element is not known in
   advance.  Accordingly, the set implementation needs to optimize for both
   the found and not-found case.
Re:50 Algorithms Every Programmer Should Know (Second Edition) by zogrodea on Wednesday January 03, 2024 @05:55PM (#38860953)
Update: Looks like I had good reason to be surprised. Thanks everyone for the examples.