Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

121–130 of 539 posts

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

#121
post #77

I want more implicit typing in typed languages. Quite often the compiler knows exactly what type a function will return, but I still need to write it there. Sometimes it’s easy („int“), but what about HashSet >> Typescript does it well. F# (completely statically typed) too…

Nowadays even Java will do that, var list = yourFunctionReturningThatSet();

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

#122
post #79

Earlier quoted context omitted.

Any language that supports overriding the index operation should support this. You should be able to do this in C# with a struct with a backing array, for instance. If you're going to do this, use the word "Circular" in it, and I would also insist that if a has 4 elements, then a[0] == a[4] == a[8]. In other words, you always just take the (positive) index modulo the size of the area. Then a[-1] is the same as a[N-1]…

Where would the circular indexing like that be useful?

It's frequently used in signal processing to the point where it's considered one of the defining features of DSPs. One common case is filtering over a fixed size buffer of samples. If you have circular indexing, you can simply overwrite the earliest sample and increment the base reference to the next element.

I'm not sure I'd want it for every list, but there are certain places it's nice.

[1] https://www.allaboutcircuits.com/technical-articles/circular...

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

#125

One of the best truly micro features I've seen recently (can't remember which langauge unfortunately - it wasn't a mainstream one), is general binary literal syntax of the form: 0x[de ad be ef 00] So much nicer than the usual condensed format. And I think it'd be valid syntax in any language that allows binary integer literal.

ohhh that's cool.. in Ruby you can do 0x_dead_beef_00 or any other combo

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

#126
post #118
post #110

local const variables like JavaScript has them. You can declare variables with „let“ (mutable) or „const“ (immutable). This is really great when reading code, because you never have to check if some code may change the variable at some point. And you usually declare most variables as const. A lot of languages provide immutable variables only for class members or statics, but not for local variables.

I really like Scala’s `val` vs `var`.

Okay, that would confuse me. Looks very similar and easy to overlook.

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

#127
post #76

Python's `with` Function composition operators eg a |> b # Call `b` with `a` as an argument b Then you can do something like let x : Map = collect map(entries) |> flatten

> Python's `with`

It's very common already: try-with-resource (java), using (C#), bracket (haskell), unwind-protect (common-lisp), ... though it the latter two it's more of a building block.

Also building block: languages with a convenient and "unrestricted" syntax for anonymous function can just use that e.g. Smalltalk, Ruby, ... in Ruby a "with" is usually just passing a block to the corresponding object's constructor:

    # python 
    with open(...) as f:
        ...

    # ruby
    File::open(...) do |f|
      ...
    end

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

#129

I have long wondered why Ruby's symbols aren't in every language.

They exist in K/Q. A single-word identifier-shaped symbol begins with a backtick, or a multi-word symbol can be created with a backtick and double quotes. A sequence of symbols is a vector literal, and is stored compactly. For example: `apple `"cherry pie" `one`two`three Many languages will intern string literals implicitly, or allow a programmer to explicitly intern a string; for example Java's "String.intern()". Th…

> A long-running program which generates new interned strings on the fly risks exhausting this pool or system memory.

So does a long-running program which generates new symbols on the fly.

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

#130
post #42

Earlier quoted context omitted.

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()?

> doesn't that cause ambiguity with a previously imported global function fun()? Yes, although overloading can mitigate the issue.

Yah I ran into that issue more in Julia. There's a built-in function to help find clashing functions.

In Nim however I've only had it happen a handful of times in a few years. Then you just need to use the module name to qualify it, or change your imports.

Post reply on HN