Skip to content

Implementation of the Elixir pipe operator behavior in Java

License

Notifications You must be signed in to change notification settings

c-rack/java-pipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-pipe

Implementation of the Elixir pipe operator behavior in Java.

Functions with up to 3 (TriFunction) respectively 4 (QuadriFunction) arguments are supported.

Description

In Elixir, a value can be passed to a function using the pipe operator (|>), where the value is automatically used as first argument of the function.

While Java 8 has introduced function composition with .andThen(), it is not exactly what I was searching for when trying to mimic Elixir's pipe operator in Java. So I wrote this small class and hope someone finds it useful.

Usage example

apply("ab")
  .pipe((a) -> a.toUpperCase())                 // "AB"
  .pipe((a, b) -> a.replace('A', b), '1')       // "1B"
  .pipe((a, b, c) -> a.replace(b, c), 'B', '2') // "12"
  .pipe((a) -> Integer.valueOf(a))              // 12
  .pipe((a, b, c, d) -> a + b * c - d, 1, 2, 3) // 11
  .pipe((a) -> a.toString())                    // "11"
  .result()

About

Implementation of the Elixir pipe operator behavior in Java

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages