question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add F#-style "pipe" operator |>

See original GitHub issue

Several times in the past, users (including me!) have requested an F#-style pipe operator, which accepts any type T has the left operand, and a unary function type F(T) as the right operand.

val |> fun

would be an abbreviation for:

let (_ = val) fun(_)

Thus, you could write print("hello") as "hello" |> print.

If I’m not mistaken, |> is naturally left-associative.

I’m now favorably-disposed toward this proposal, and the syntax seems viable. It looks like it can be implemented by desugaring and need not impact the backends in the initial implementation.

An open question is: is foo |> bar considered a legal statement? Can I write this:

void hello() {
    "hello" |> print;
}

I would say that we should accept this code.

Discussion on Gitter led to me proposing two additional variations of this operator, by analogy to the existing ?. and *. operators.

  • maybe ?|> fun would propagate nulls from left to right, being equivalent to if (exists _=val) then fun(_) else null
  • tuple *|> fun would spread a tuple over the parameters of an arbitrary-arity function, being equivalent to let (_=val) fun(*_)

(Note that these operators could also be easily defined in terms of |> and a higher-order function. For example, tuple *|> fun is equivalent to tuple |> unflatten(fun).)

Both of these are useful—I’ve wanted something like ?|> many times when writing real Ceylon code—but the objection was raised by @someth2say that they’re too ascii-arty for this language. That’s a reasonable objection, and we should give it some weight.

Feedback?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:91 (74 by maintainers)

github_iconTop GitHub Comments

8reactions
gavinkingcommented, Apr 16, 2018

This is now working well enough that you folks can try it out, if you like. For example, the following code:

shared void run() {
    "hello world how are you today, I'm doing great !!!! xxxxxyyy"
            |> String.size
            |> (Integer i) => Integer.format(i, 16) 
            |> String.uppercased 
            |> String.trimmed 
            |> print;
}

prints 3C with both ceylon run and ceylon run-js.

5reactions
gavinkingcommented, Oct 18, 2016

@FroMage My feeling is that we would introduce |> as a first step. I don’t think I would add ?|> and *|> initially, since I kinda agree with the thinking that they’re potentially cryptic.

I agree that bar(foo()) is clearer than foo() |> bar, however, I don’t think that reasoning holds when it’s

bar(foo(baz(fee(fi(fo(fum))))))

vs

fum |> fo |> fi |> fee |> baz |> foo |> bar
Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Pipes - The tidyverse style guide
4.1 Introduction. Use %>% to emphasise a sequence of actions, rather than the object that the actions are being performed on. Avoid using...
Read more >
18 Pipes | R for Data Science
Pipes are a powerful tool for clearly expressing a sequence of multiple operations. So far, you've been using them without knowing how they...
Read more >
A pipe operator for JavaScript: introduction and use cases
A pipe operator for JavaScript: introduction and use cases · F# by Microsoft is a functional programming language whose core is based on...
Read more >
Add a pipe separator after items in an unordered list unless ...
One solution is to style the left border like so: li { display: inline; } li + ...
Read more >
A note on magrittr pipe (`%>%`) code style | Credibly Curious
I recently wrote about ggplot2 style, and in this one I'd like to discuss the pipe operator, %>% . These notes are drawn...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found