3 ms·
Completely unrelated to the topic but I'm struck by the Ada solution. So many other languages either make a map or make an array and then back out characters fr
by Avshalom 11y ago
Completely unrelated to the topic but I'm struck by the Ada solution. So many other languages either make a map or make an array and then back out characters from numeric indexes.
Ada just lets you use characters as indexes. Ada will actually let you use damn near anything as an index. I use enumerated types to build lookup tables for stuff a lot when playing around with it.
- pmontra 11y agoMany languages have associative arrays that can take any data type as index. A Ruby example, intentionally written to make it easy to read (and maintain): array_of_characters = ["a", "b", "a", "c"] count = {} array_of_characters.each do |character| if count.has_key?(character) count[character] += 1 else count[character] = 1 end end count => {"a"=>2, "b"=>1, "c"=>1} The one on rosettacode is smarter but it takes some time to be understood.
- Avshalom 11y agoAda has associative arrays as well. But it will also let you have normal old boring arrays with enums, chars and modular types as indexes.
- pmontra 11y agoInteresting. Maybe naively, I always assumed that the difference between those two data structures is that an array has integer indices to be mapped trivially to RAM and CPU index registers, and associative arrays are hash tables. I googled this https://en.wikibooks.org/wiki/Ada_Programming/Types/array https://en.wikibooks.org/wiki/Ada_Programming/Types/array and it seems that arrays in Ada can have only discrete indices. Example from there type Character_Counter is array (Character) of Natural; I guess that the compiler maps Character to integers and uses index registers to access RAM. That would be character.ord in Ruby and in many other languages but it's a lower level construct.
- Avshalom 11y agoI can for instance do type throw is (ROCK, PAPER, SCISSORS); type result is (ME, YOU, DRAW); winner : array (throw, throw) of result := ( (DRAW, YOU, ME), (YOU, DRAW, ME), (ME, YOU, DRAW)); YOU = winner(ROCK, PAPER); --would evaluate to true because types are cool.