MarioAriasC / funKTionale

Functional constructs for Kotlin
913 stars 71 forks source link

Pipe operator implementation #28

Closed BenjaminEarley closed 7 years ago

BenjaminEarley commented 7 years ago

Passes the result of an expression as the first parameter of another expression.

martin-g commented 7 years ago

Where are the tests ?!

BenjaminEarley commented 7 years ago

@martin-g Thanks Martin. Is it convention here I implement this method out to 22 arguments?

martin-g commented 7 years ago

I am not really sure! I am not a maintainer here, but just someone interested in Kotlin and FP. But I think it is a convention. And I think it comes from Scala.

MarioAriasC commented 7 years ago

I need on your description a use case. Bonus points if is a realistic one

BenjaminEarley commented 7 years ago

@MarioAriasC

Piping is for composing operations in the order that they are performed. Left to right and top to bottom, instead of inside out. Languages like F# and Elixir have syntax for this.

Simple Example:

2 pipe { it + 1 } pipe { it * 2 }

Real World Example from a common Rx use-case:

Before the magic of piping
disposables.add(
        fab
                .clicks()
                .switchMap {
                    ApiInterface
                            .getStuff(ids = retrofitListOf(1234))
                            .subscribeOn(Schedulers.io())
                            .toObservable()
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ deals ->
                    Log.d("Debug", "Success: " + stuff.toString())
                }, { error ->
                    Log.d("Debug", "Error: " + stuff.toString())
                })
)
After
fab
    .clicks()
    .switchMap {
        ApiInterface
            .getStuff(ids = retrofitListOf(1234))
            .subscribeOn(Schedulers.io())
            .toObservable()
    }
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe({ deals ->
        Log.d("Debug", "Success: " + stuff.toString())
    }, { error ->
        Log.d("Debug", "Error: " + stuff.toString())
    }) pipe { disposables.add(it) }
MarioAriasC commented 7 years ago

@jkpl check this out

jpallari commented 7 years ago

Examples from other languages:

It's similar to reverse compose. I think it's pretty useful.

jpallari commented 7 years ago

So instead of:

foo(bar(zap(x)))

You have:

zap(x).pipe(bar).pipe(foo)
MarioAriasC commented 7 years ago

Can you add a wiki page for pipe?

Cheers