function_composition.sf 282 B

123456789101112131415161718192021
  1. #!/usr/bin/ruby
  2. # https://rosettacode.org/wiki/Functional_Composition
  3. func compose (f, g) {
  4. func(n){ f(g(n)) }
  5. }
  6. func f(n) {
  7. n / 64;
  8. }
  9. func g(n) {
  10. n * 32;
  11. }
  12. var fg = compose(f, g); # fg(x) is equivalent with: f(g(x))
  13. assert_eq(fg(4), 2);
  14. say "** Test passed!";