News for hackers. Stuff that matters.
Re:Pipelining might be my favorite programming language feature by mrkeen on Monday April 21, 2025 @09:07AM (#43751596)

  data.iter()
      .filter(|w| w.alive)
      .map(|w| w.id)
      .collect()

  collect(map(filter(iter(data), |w| w.alive), |w| w.id))
The second approach is open for extension - it allows you to write new functions on old datatypes.

> Quick challenge for the curious Rustacean, can you explain why we cannot rewrite the above code like this, even if we import all of the symbols?

Probably for lack of

> weird operators like <$>, <*>, $, or >>=

Re:Pipelining might be my favorite programming language feature by esafak on Monday April 21, 2025 @09:17AM (#43751682)
Re:Pipelining might be my favorite programming language feature by vips7L on Monday April 21, 2025 @12:43PM (#43753894)
I really wish you couldn't write extensions on nullable types. It's confusing to be able to call what look like instance functions on something clearly nullable without checking.

    fun main() {
        val s: String? = null
        println(s.isS()) // false
    }

    fun String?.isS() = "s" == this
Re:Pipelining might be my favorite programming language feature by usrusr on Tuesday April 22, 2025 @12:42AM (#43759125)
The difference between .let{} and ?.let{} has great utility. You'd either have to give that up or promote let from regular code in the standard library to magic language feature.

And you'd lose all those cases of extension methods where the convenience of accepting null left of the dot is their sole reason to be. Null is a valid state, not something incredibly scary best dealt with with a full reboot or better yet throwing away the container. Kotlin is about making peace with null, instead of pretending that null does not exist. (yes, I'm looking at you, Scala)

What I do agree with is that extension methods should be a last ditch solution. I'd actually like to see a way to do the nullable receiver thing defined more like regular functions. Perhaps something like

   fun? name() = if (this==null) "(Absent)" else this.name
that is defined inside the regular class block, imported like a regular method (as part of the class) and even present in the class object e.g. for reflection on the non-null case (and for Java compat where that still matters)
Re:Pipelining might be my favorite programming language feature by mrkeen on Tuesday April 22, 2025 @07:06AM (#43760806)
> Null is a valid state, not something incredibly scary best dealt with with a full reboot or better yet throwing away the container.

Agreed. It should be a first-class construct in a language with its own own proper type,

  Null null;
rather than needing to hitch a ride with the Integers and Strings like a second-class construct.
Re:Pipelining might be my favorite programming language feature by vips7L on Tuesday April 22, 2025 @10:20AM (#43762540)
Personally I find ?.let et al to be terrible for readability; most of the time you're better off doing a standard != null check. The same with nullable extension functions; they hurt readability.

> Null is a valid state, not something incredibly scary best dealt with with a full reboot or better yet throwing away the container. Kotlin is about making peace with null, instead of pretending that null does not exist. (yes, I'm looking at you, Scala)

I honestly find this to be such a weird thing to say or imply. No one is "scared" of null.

Re:Pipelining might be my favorite programming language feature by rikthevik on Monday April 21, 2025 @01:08PM (#43754136)
Came here for the Uniform function call syntax link. This is one of the little choices that has a big impact on a language! I love it!

I wrote a little pipeline macro in https://nim-lang.org/ for Advent of Code years ago and as far as I know it worked okay.

``` import macros

  macro `|>`\* (left, right : expr): expr =
    result = newNimNode(nnkCall)

    case right.kind
    of nnkCall:
      result.add(right[0])
      result.add(left)
      for i in 1..<right.len:
        result.add(right[i])
    else:
      error("Unsupported node type")
```

Makes me want to go write more nim.

Re:Pipelining might be my favorite programming language feature by Mond_ on Monday April 21, 2025 @09:57AM (#43752080)
> The second approach is open for extension - it allows you to write new functions on old datatypes.

I prefer to just generalize the function (make it generic, leverage traits/typeclasses) tbh.

> Probably for lack of > weird operators like <$>, <*>, $, or >>=

Nope btw. I mean, maybe? I don't know Haskell well enough to say. The answer that I was looking for here is a specific Rust idiosyncrasy. It doesn't allow you to import `std::iter::Iterator::collect` on its own. It's an associated function, and needs to be qualified. (So you need to write `Iterator::collect` at the very least.)

Re:Pipelining might be my favorite programming language feature by higherhalf on Monday April 21, 2025 @10:40AM (#43752541)
> It doesn't allow you to import `std::iter::Iterator::collect` on its own. It's an associated function, and needs to be qualified.

You probably noticed, but it should become a thing in RFC 3591: https://github.com/rust-lang/rust/issues/134691

So it does kind of work on current nightly:

  #![feature(import_trait_associated_functions)]
  
  use std::iter::Iterator::{filter, map, collect};

  fn get_ids2(data: Vec<Widget>) -> Vec<Id> {
      collect(map(filter(Vec::into_iter(data), |w| w.alive), |w| w.id))
  }  

  fn get_ids3(data: impl Iterator<Item = Widget>) -> Vec<Id> {
      collect(map(filter(data, |w| w.alive), |w| w.id))
  }
Re:Pipelining might be my favorite programming language feature by Mond_ on Monday April 21, 2025 @10:55AM (#43752687)
Oh, interesting! Thank you, I did not know about that, actually.
Re:Pipelining might be my favorite programming language feature by pornel on Monday April 21, 2025 @05:33PM (#43756744)
Rust has such open extensibility through traits. The prime example is Itertools that already adds a bunch of extra pipelining helper methods.