Syntax Proposal - "And-then-style" function-chain call

It already exists. There are 2 functions pipe and tap defined in scala.util.chaining.

2 Likes

Probably, it is hard to find them, when they are in “util.chaining” and not in Predef, so, I’ve never seen their usage in examples or books, and can’t imagine, where to search them.

And, probably, their letter-names make some visual noise, and need to have some symbolic pseudonyms, like “\\”.

(1, 2, 3)  pipe
 xyz       pipe
 (_ + _)   pipe
 factorial pipe
 println

is harder to read for me, than

(1, 2, 3)   \\
  xyz       \\
  (_ + _)   \\
  factorial \\
  println

Moreover, in today implementation “pipe” surely has overhead. “Extension-inline” implementation, on the other hand, has not.

But I’m glad, that one of them is already here.

Maybe, “@:” analog is here too, but I don’t see it in “ChainingOps” (“tap” is not analog, it is more universal version of “echo” in my example).

I prefer the plain old chaining of map, but pipe at least gives me a clue as to what’s going on. \, on the other hand, is utterly mysterious.

In my opinion, syntax that makes code completely unreadable for programmers used to other (mainstream) languages should be considered very carefully.

5 Likes

“map” is for containers, but I talk about how implements the same for all entities, because I love container’s “linear” syntax and want to use it everywhere.

list map (_ + 1) map (_ * 10)

10 pipe (_ + 1) pipe (_ * 10)

or

10 \\ (_ + 1) \\ (_ * 10)

“\\” is not the best symbolic name, I chose it for example, only because Wolfram’s one has name of “//”.

Probably, something like |> would be better. It looks Ok:

x |> f |> g

or

val res = 2
  |> (_ * 10)
  |> echo
  |> (_ / 2)

Intelij Idea shows “|>” as an arrow-like triangle.

x // f1 // f2 // f3 (from older post)

Can’t we use something like function composition in maths ? An operator like f ∘ g. Akka actually uses the ~> operator for its stream system. Maybe we can use it for functions.

Example:

val finalFunction = f ~> g ~> h //means f(g(h(_)))

Intelij Idea shows “|>” as an arrow-like triangle.

I think we shouldn’t consider IDE’s font as a plus. What if the user uses another IDE ? What if the changes his font ? etc…

1 Like

Since you mentioned |>, Elm has such operator with exactly the same meaning and the symmetric <| one: (quoting from docs/syntax)

In addition to the normal math operations for addition and subtraction, we have the (<|) and (|>) operators. They are aliases for function application, allowing you to write fewer parentheses.

viewNames1 names =
  String.join ", " (List.sort names)

viewNames2 names =
  names
    |> List.sort
    |> String.join ", "

-- (arg |> func) is the same as (func arg)
-- Just keep repeating that transformation!

Historical note: this is borrowed from F#, inspired by Unix pipes.

Relatedly, (<<) and (>>) are function composition operators.

1 Like

|> is the de-facto piping operator, since it still looks like a nice arrow without ligatures. Even then, I think most people do use fonts with them

2 Likes

I think, nowdays, if symbol is good looking as itself, IDEs support for it is a plus. Surely, IDE is not a rare thing, but rather mainstream, now.

Nice. Id didn’t know about it. Just tried some symbol combinations.

1 Like

That looks like Fira Code, not IntelliJ IDEA’s font Jetbrains Mono

1 Like

JetBrains Mono also supports this ligature

1 Like

Yes, I just wanted to say that it isn’t the font we were talking about just before that message

It actually doesn’t:
image

Edit: I’m in IDEA’s default font (Jetbrains Mono)

Ligatures are optional, but you can turn them on: https://www.jetbrains.com/lp/mono/#ligatures

2 Likes

Ok but we go back to the first problem. Altrough I find this symbol cool, what if the user uses another IDE ? Or another font ? Or even if he uses IDEA with JB Mono, what if he doesn’t uses ligature ?.

It still looks like an arrow/triangle

4 Likes

I think font ligatures can’t be a proper argument for this operator, it’s nice to have, but just that.

The argument here is that there are at least two other (FP) languages supporting this operator with the same semantics. (and this is why this ligature exists in the fonts)

4 Likes

Yes but I mean I don’t think a single (even the most popular) IDE’s default font has something to do with operator choice.

But this is a good argument for the |> operator.

Or just continue using . and extension methods. Even though I use F# daily, I think ship has already sailed with Scala. You also don’t get things like auto completion with the chaining style of things. Its bit of a foreign change to be introducing this style of code imho.

extension [A](a: A) def through(f: A => R) = f(a)

list
  .map(_ * 2)
  .through(sum)
  .through(_ + 1)
  .through(println)
1 Like

Proper argument is “This symbol is looking good as itself, and in some languages it is already means the same, and, by the way, it already has nice ligatures in some IDEs”. Last one is not a proper argument, if we get it as isolate suggestion, but it is seems a bonus, if first two arguments are true.

5 Likes