Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

191–200 of 539 posts

Re: Microfeatures I'd like to see in more languages

#191

Earlier quoted context omitted.

That's for languages that can't define arrays with custom start/stop indexes. But those that have custom indexes they can very easily expand/implement as helper class (for example array.indexFromLast(1) which means array[Length(array)]. This way you can have best of both worlds.

Surely if your language has custom indexes / ranges `Length(array)` is completely broken and the language provides something like "Index`Last" you can hook on? Because an array with indexes [3, 7) has length 4, but 4 is not the index of the last element.

That's what Ada does, yes. You'd let the array (or whatever collection) do the work for you:

  for I in A'Range loop
    A(I) = A(I) + A(I);
  end loop;
Whatever the range is, this will work. If you really need the first and last elements or want to be explicit:

  Start := A'First;
  End := A'Last;
And if the type of the range (since any discrete type can be used) doesn't support simple incrementing with +1 or similar, you can use 'Succ to step through:

  Index := A'First;
  Index := Whatever_Type'Succ(Index);
Also 'Pred to work backwards. Those can be wrapped up in a simpler function if desired.

Re: Microfeatures I'd like to see in more languages

#192
post #96
post #82

Earlier quoted context omitted.

I'm in the same boat as GP. Typically, I grep such things when I am not familiar with the codebase, so I can't change where constants are defined, and I do not know where to find such a file.

For what it's worth when I dive into a new codebase, the first thing I do is try to guess what files exist and then find them and get a feel for structure. Constants are high in the list. But there are many ways to skin a cat.

The problem is not finding where the constants are defined, it's about finding what the code does based on a hardware datasheet, the constants could be in a file, or could be scattered throughout, it doesn't matter at all.

You can also use the magic of base 16 to search for lexical subsets of a value to find where code uses things with the same mask, which are probably related. Extremely effective in reverse engineering a hardware device.

Re: Microfeatures I'd like to see in more languages

#194
post #15

> Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 if you’re Indian. I hate this so much. It means I can't grep for a constant.

Out of interest, how often do you do this, and what are the semantics of the numbers you're grepping for? I literally can't remember a time I've ever tried to grep for a number.

When I was doing bare metal programming I was doing this all then time, depending on what I was doing maybe even tens of times an hour. And it's not just source code either, these days even debugging tools print values in this way making it a total PITA to reverse engineer things because you can't easily match values coming in from different tools, or from the tool and source code, etc.

Re: Microfeatures I'd like to see in more languages

#195

Negative array subscripts. So a[-1] means the last element of an array, a[-2] means the second last, and so on.

if it works on literals only, so a[x] doesnt work if x is negative, then ok. otherwise seems like an errors that are hard to spot.

Ruby happily allows this and I can't recall it ever being an issue. It's no more prone to errors than `x` being greater than the number of elements.

> arr = ["a", "b", "c", "d", "e"]

> x = -2

> arr[x]

=> "d"

Re: Microfeatures I'd like to see in more languages

#196
post #131

> [In Chapel] there’s the config keyword. If you write config var n=1, the compiler will automatically add a --n flag to the binary. As someone who 1) loves having configurable program, and 2) hates wrangling CLI libraries, a quick-and-dirty way to add single-variable flags seems like an obvious win. Letting people define configurable variables at their call site is incredibly valuable, even if you don't have compile…

> The first time it's seen in any environment, it thread-safely inserts-if-not-present the default value into a key-value storage That seems like a great way to get amazingly hard to replicate bugs or odd behaviours if different subsystems use different values for the default.

I would want that system to log those changes to whatever monitoring system is being used, or integrate with the deployment system as a "deploy", so that when some oncall person is trying to figure out why the entire fleet is pegging their CPU, they can trace it back to the flag change.

Re: Microfeatures I'd like to see in more languages

#197

# Function Shorthand #### I like how functions in js can be `arg => result`. In F# I have to do `fun arg -> result` with the `fun` keyword. It makes sense since `MyArgType -> MyResType` is a type signature in f#, but I feel like the compiler can just check if the arguments are references to types or are argument bindings. # Multiline Lists/Arrays #### I like how F# doesn't require delimiters for multiline lists. So I…

> I feel like the compiler can just check if the arguments are references to types or are argument bindings. 1. this is absolutely terrible because now you need feedback from the type checker to know how to parse the program 2. it is furthermore also ambiguous with function application , requiring arbitrary lookahead to disambiguate, also not a fun thing to do JS gets away with it because the sigil was not previously…

Yeah, then maybe just shorten `fun` to `f` or `fn` like elixir. I think even just one char makes a difference over thousands of LoC.

Re: Microfeatures I'd like to see in more languages

#198
post #190
post #32

Earlier quoted context omitted.

If you have constants repeated throughout the codebase, you can pull them into a constants file, and then you can go to that file, navigate to the definition of interest, and use your IDE of choice to find usages. You'll also be able to give these constants semantically significant names, and comment next to them providing derivations or citations. And of course, if it's a mistaken or outdated value, you can change i…

You haven't understood the problem at all. No wonder, few people do any sort of bare metal programming. It's not about defining constants, or even writing code at all, it's about figuring out what this arbitrary piece of code is doing based on hardware datasheets . You search for constants defined in the datasheet in the piece of code you are analyzing to determine what it does, or where does it do specific things...

I'd gently suggest that if you wanted me to have that context when interpreting your statement, you could have provided it in your original statement or provided it now blamelessly, rather than framing this as a deficiency on my part that I failed to use my crystal ball to determine you were an embedded programmer. (I do very similar things at the application level, for what it's worth.)

I believe the solution I proposed remains viable in that context or for that usage. If I defined a constant for the magic memory address one writes to to configure the MMU, and you and to understand how I implemented context switching, you could navigate to my constant and find usages.

If that solution doesn't work for you, no worries, it was just a suggestion/observation.

Re: Microfeatures I'd like to see in more languages

#199

Earlier quoted context omitted.

> I feel like the compiler can just check if the arguments are references to types or are argument bindings. 1. this is absolutely terrible because now you need feedback from the type checker to know how to parse the program 2. it is furthermore also ambiguous with function application , requiring arbitrary lookahead to disambiguate, also not a fun thing to do JS gets away with it because the sigil was not previously…

Yeah, then maybe just shorten `fun` to `f` or `fn` like elixir. I think even just one char makes a difference over thousands of LoC.

In haskell (and elm) it's `\` which is quite OK (and also a nod to the lambda symbol λ).

But yes "fn" is quite nice (taking over "f" is a bit much). And a few characters can definitely degrade the experience, especially as "u" and "n" are typed with the exact same finger.

Anonymous functions were definitely one of my least favorite features in Erlang, not because they don't work well but because their leading keyword is "fun" and there's an arrow between the (parenthesised) parameters and body and they also have a closing keyword "end":

    map(fun(X) -> 2 * X end, [1,2,3,4,5]).
That's a bit much.

But HoFs in general are quite awkward, as referring to a named function also requires the `fun` leading keyword, and requires specifying the arity, so

    map(fun double/1, [1,2,3,4,5]).
after having defined the function as

    double(X) -> 2 * X.
(as you can see Erlang would really rather you defined named functions).

Re: Microfeatures I'd like to see in more languages

#200
post #190

Earlier quoted context omitted.

You haven't understood the problem at all. No wonder, few people do any sort of bare metal programming. It's not about defining constants, or even writing code at all, it's about figuring out what this arbitrary piece of code is doing based on hardware datasheets . You search for constants defined in the datasheet in the piece of code you are analyzing to determine what it does, or where does it do specific things...

I'd gently suggest that if you wanted me to have that context when interpreting your statement, you could have provided it in your original statement or provided it now blamelessly, rather than framing this as a deficiency on my part that I failed to use my crystal ball to determine you were an embedded programmer. (I do very similar things at the application level, for what it's worth.) I believe the solution I prop…

Yes, exactly, now you need special tools (you mentioned IDEs) when previously you could have used grep.
Post reply on HN