Hacker News new | ask | show | jobs
by oivaksef 1912 days ago
This is hilarious, and clearly a joke, but it does make one think. I've always thought that it would be nice to have variables which you don't name, which only live over a couple of lines, where it's clear the variable is just for some plumbing and doesn't need a name. I wonder if there is actually something in these two concepts combined
6 comments

> I've always thought that it would be nice to have variables which you don't name, which only live over a couple of lines, where it's clear the variable is just for some plumbing and doesn't need a name

There are several things that involve unnamed, shortlived plumbing vars; as well as various languages pipeline notations, therr is also this in scala lambdas:

  xs.map(x => x + 1)
can be written as:

  xs.map(_ + 1)
and even:

  (xs, ys).zipped.map((x,y)=>x+y)
as:

  (xs, ys).zipped.map(_ + _)
Clojure's "threading" macros are exactly this: https://clojure.org/guides/threading_macros

Elixir's pipe is the same thing, though Clojure has a few extra forms to facilitate other variable passing patterns.

We already have:

- partial application notation with numbered arguments

- pattern matching

- anaphoric if and related notations

- point-free form (no names at all)

tidyverse packages in R do this. Normally, you might have something like this:

    df = someFunctionThatReturnsADataFrame()
    newDf = someFunctionThatTakesAndReturnsADataFrame(df)
    doSomethingTo(df)
Since R lets you define your own operators, they took '%>%' and defined it as "pipe the return value from one function as the first argument to another function," so you can do this:

    someFunctionThatReturnsADataFrame() %>%
        someFunctionThatTakesAndReturnsADataFrame() %>%
        doSomethingTo()
No need to name either the temporaries or the function arguments.
IRB the interactive ruby shell has _ which references the return value of the last command. So

10 + 10 puts _ # prints 20

but that isn't available in ruby itself

I think a better format would be:

instead of:

    p(2 * 3 * 7)  #=> 42
      ^^^^^var
Having:

   p(var1:[2*3] * var2: 7) => 42

   p(var1) => 6

   p(var2) => 7 
The other requires ugly whitespacing.
It kind of works in Python 3.9 and perhaps earlier, unfortunately need extra parentheses ...

  >>> print((v1 := 2 * 3) * (v2 := 7))
  42
  >>> print(v1)
  6
  >>> print(v2)
  7
edit: formatting
How do you reference a variable for plumbing if it doesn't have a name?
Would Elixir's pipe - https://elixir-lang.org/getting-started/enumerables-and-stre... - or Haskell's function composition - https://wiki.haskell.org/Function_composition - fit the criteria?
Those would. The Elixir style feels nice. I wonder if something like that could make it into Ruby 4
Mentioned in another sub-comment, but Clojure's threading macros do this. They do it by making positional assumptions about the implicit argument: https://clojure.org/guides/threading_macros
Wouldn't any concatenate language fit the bill? https://concatenative.org/