Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

241–250 of 539 posts

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

#241

Haskell has almost all of these. > Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/nume... > Balanced string literals https://hackage.haskell.org/package/raw-strings-qq-1.1/docs/... > Generalized update syntax Use Lens. `fileName %~ max 2` > you can write the sequence 1, 2, … n-1 as 1.. Yup. `[1,2..n-1]` There's far more to it, you have acc…

>> Symbols > In Haskell you use hash has a prefix instead of colon. Can you give an example?

https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/over...

You can do all sorts of things with them. Use them like symbols in Scheme, say to name fields `get #name user` or to access database tables, etc.

But what's even more interesting is that the name is reflected up into the type. `get #name user` won't fail at runtime. Your database table name can be checked at compile time.

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

#242
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]…

What 'mod' is for, innit.

In c# -1 % 2 == -1. Doesn't help.

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

#244

For rust, it is probably the try (?) operator. Fundamentally, it's just syntax sugar for a match statement with an early return in the Error or None cases, but it really improves the ergonomics of dealing with Result and Option types.

Here's Rust's try (?) operator.[1] It seems sugar for handling short-circuiting return values.

A bit like Swift's try? which converts throws to nil.[2] Less so javascript's short-circuiting optional chainging `?.`[3], which I thought of first.

[1] https://doc.rust-lang.org/std/ops/trait.Try.html [2] https://docs.swift.org/swift-book/ReferenceManual/Expression... then /Try Operator/ - section anchors don't look stable. [3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

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

#246
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?

Sonic Pi, the live music coding environment, has a circular list structure type called a 'ring'. This proves curiously helpful for a bunch of musical scenarios.

Like:

- you can put a short chord sequence into a ring, and it now functions as a list of as many repetitions of that chord sequence as you like. You can just loop over it forever (which is kind of the essence of how sonic pi live-loop play works)

- you can put the notes that make up a scale into a ring, and use it to extract specific chords - like, take the 1st, 3rd, 5th, 7th and 9th note - from just a seven note scale.

- you can use rings of booleans to capture drum patterns and rings of notes to capture melodies, and loop them forever

- etc. etc.

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

#247
Keyword and optional arguments (seen in e.g. Python, Bash with --flags, and OCaml) are my favorite language superpower. They make code more self-documenting and let you add add optional behaviors to functions. This makes it really easy to make concise, highly usable APIs.

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

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

Julia also has

x |> f

as a syntax sugar for

f(x)

and is useful for the same reason as Mathematica.

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

#249

Some from Raku (formerly Perl 6) that I really like: * sub MAIN: sub MAIN(Int $x, :$verbose) { } generates a command line parser that expects an Integer plus an optional named switch --verbose * It has named params (as seen above), and there are abbreviations: instead of thing => $thing you can write :$thing to avoid duplicating the name (:thing also exists, though it create a pair "thing" => True, so ruby lovers nee…

phasers are brilliant too. so much nicer than all the clunky workarounds for "the first/last pass through this loop needs to be treated specially"

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

#250

Earlier quoted context omitted.

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"

If you intended to calculate an index and you accidentally get len(arr), you get a runtime error. But if you accidentally get -1 you silently get the last element instead. Similar to the argument about signed/unsigned indices in low level languages.
Post reply on HN