Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

61–70 of 539 posts

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

#61
post #56

Earlier quoted context omitted.

1_?0_?00_?0_?500 yes it sucks

That’s not enough — the underscores can appear anywhere, so you need to cater for 500000_0 and 5_0_0_0_0 etc. basically an optional underscore between each digit.

just grep for `[0-9_]+`

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

#63
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.

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

#64
> Second, what if parameter blocks were abstractable?

Sounds like a great way to make unreadable code

    #define STANDARD_EXP_PARAMS ...

    // I hit gotodef on func(...) and got here. What are the arguments?
    void func (STANDARD_EXP_PARAMS) {
       // ... long function body ...
       x = y; // What is the type of x? Is it an argument or a local? 
    }
I'd really be irritated if someone ever used this feature, optimizing for lines written is shaky territory to begin with. When it's at an interface boundary it's not excusable. NB4 "use an IDE" - requiring an IDE to make code legible is dumb, and I'm an IDE shill!

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

#65

Earlier quoted context omitted.

Interestingly, I almost prefer Clojure's `recur` semantically. Means you don't have to change the function name twice if you rename it, and it's hard to miss that you're recursing.

If that's your worry then you can probably use the site's namesake. Though simple recursion is generally easy to spot.

Which site's namesake? Hacker News?

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

#66
post #17

I really liked the postfix and prefix notation in Mathematica. These three all mean the same: f[x] f@x x // f It matches the flow of thought more naturally when hammering out a couple of one-liners.

How is that third one readable at all? I’d assume it meant integer division.

It's usually used with the formatting functions, so you have something like:

// Column

Useful where the function in question is an "afterthought".

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

#67

> If you look at something like numpy functions, so many of them share the exact same parameter definitions. What if you could write def log(standard-exp-params) instead of having to write them out every single time? They're not actually written out every time, the issue is mostly documentary (and it would be nice if Python or Sphinx ever had a good solution). And numpy actually has a bunch of generators for that e.g…

I don't use Numpy but it sounds like they're describing *, ** operators.

    default_args = ('x', 'y', 'z')
    default_kwargs = {'p': 'p', 'q': 'q', 'r': 'r'}
    
    def printer(x, y, z, /, *, p, q, r):
     print(f'x={x} | y={y} | z={z} | p={p} | q={q} | r={r}')  
  
    printer(*default_args, **default_kwargs)  # x=x | y=y | z=z | p=p | q=q | r=r
EDIT: Formatting.

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

#68
post #51
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…

Depends on the size of your codebase and how many people work on it. Once you have 20 years of code written full time by 200 developers in your monolith, finding the constants file out of 400 different subsystem's constant files for a given subsystem that you've never seen before can become a legitimate and challenging pain.

Totally true. I don't know if IDEs commonly have a feature like, "find this value, regardless of how it's expressed" (even better yet, fuzzily, to catch typos), but I think that's the proper general solution. It's a good idea to consider a constants file earlyish, while everything still fits in your head.

I'd say in the case of such a sprawling system, make a constants module, and it can have different files for different topics. But keep them all together. Code style is an engineering tool you can use to prevent problems.

But I do understand this is cold comfort for those working on systems where the decision around this were made 15 years ago, and there's no possibility of refactoring the constants. That's quite annoying.

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

#69

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

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.

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

#70
post #42
post #4

My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. Another one from Nim is the implicit result variable. Instead of having to do this: func sum(nums: seq[int]): int = var result = 0 for num in nums: result += num return result you just do this: func sum(nums: seq[int]): int = for num in nums: result += num It…

and vice-versa Doesn't the reverse pollute the function namespace? If every obj.fun() can be written as fun(obj), doesn't that cause ambiguity with a previously imported global function fun()?

That sounds like no fun() at all.

Right, I'll show myself out...

Post reply on HN