Skip to content

Piping data to functions #2049

@andrewmarx

Description

@andrewmarx

Something I've seen elsewhere is the concept of piping data to functions, which can drastically increase readability in cases where someone would either use intermediate variables or call functions as parameters.

Pseudocode for a contrived example:

x = {1,-7,-5,3}

// Without pipes, intermediate variables.

a = sum(x)
b = abs(a)
c = sqrt(b)
result = round(c, 2)


// Without pipes, nesting functions.

result = round(sqrt(abs(sum(x))), 2)


// With pipes the result on the left of the pipe is passed as the first parameter
//of the function on the right, allowing the code to be read left to right.

result = x %>% sum() %>% abs() %>% sqrt() %>% round(2)


// It's particularly nice for splitting code across lines

result = 
  x %>% 
  sum() %>% 
  abs() %>% 
  sqrt() %>% 
  round(2)

The %>% operator is just an example (from R), it could be something else

I'm just learning rust, and know nothing about language development, so the complexities involved might make this impractical. At the same time, it would also be really interesting to see how something like this could be incorporated to work with tuples and multiple return values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions