chronium / aatbe-lang

Åtbe language compiler. Written in Rust.
MIT License
5 stars 0 forks source link

Pipes #13

Open chronium opened 4 years ago

chronium commented 4 years ago

An idea from @SplittyDev was implementing pipes. similar to F# piping. Useful for method chaining in a functional way, and creating partially applied functions.

fn toInt str -> i128 // transforms from srt to i128

"42" |> toInt
// and the reverse would be
toInt <| "42"

// equivalent to
toInt "42"

That was example syntax, the use case is not the most appliable, but the syntax is what it shows.

SplittyDev commented 4 years ago

This can be especially useful if there is a big chain of functions:

getRandomString |> xorWith (_, "very secret key") |> toSha256Bytes |> toHex

Otherwise, this would be:

toHex (toSha256Bytes (xorWith (getRandomString(), "very secret key")))

One question is how the semantics should be with multiple parameters.

Consider the following setup:

extern fn add (a: i32, b: i32) -> i32

@entry
fn main () -> () = {
    val a = 2
    val b = 4
    ...
}

If we fill from left:

a |> add b // add(a, b)

If we fill from right:

a |> add b // add(b, a)

If we let the user specify the entry point:

a |> add (_, b) // add(a, b)
a |> add (b, _) // add (b, a)

I think the standard semantics of the backwards pipe is to fill from right:

foo a <| b // foo (a, b)

So I guess the semantics of the forwards pipe should be to fill from left:

a |> foo b // foo (a, b)

But giving the user freedom over the filled slot(s) could be useful too.

chronium commented 4 years ago

Fill from left on |> and fill from right on <| Other than that, all perfect examples of use cases. Cheers!