Skip to content

Instantly share code, notes, and snippets.

@homam
Last active July 8, 2016 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save homam/02b034bdcdf3667dc99a5fad8b3b6846 to your computer and use it in GitHub Desktop.
Save homam/02b034bdcdf3667dc99a5fad8b3b6846 to your computer and use it in GitHub Desktop.
class Func {
constructor(f) {
// this.run = f
this.run = x => f(x)
// this :: Cat (x ↣ y)
// Cat (y ↣ z) -> Cat (x ↣ z)
this.pipe = g => new Func(x => g.run(this.run(x)))
// utility function that pipes Func to a normal function
// this :: Cat (x ↣ y)
// (y -> z) -> Cat (x ↣ z)
this.pipeTo = g => new Func(x => g(this.run(x)))
}
}
// Cat (x ↣ x)
Func.id = new Func(x => x)
@homam
Copy link
Author

homam commented Jul 8, 2016

Usage example: https://jsbin.com/kecoxa/edit?js,console

const operation = new Func(x => x + 1)
  .pipe(new Func(Math.sqrt))
  .pipe(new Func(x => x * 6))
  .pipeTo(x => x - 1)

const result = operation.run(48) // = 41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment