DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Functional Programming in Ruby 1.9


  • Blocks are just a special syntactic sugar in Ruby to create Procs.

  • Lambdas are really just Procs, but with two important differences:

  • Lambdas check the arguments they receive, just like methods. Procs do not. This means if you pass only one argument to a Lambda that takes two arguments, you’ll get an ArgumentError. If you do the same to a Proc, it will just blindly accept the ones you did give, and set the rest of the arguments to nil;

  • Any return statements used in a Proc will also return from the method that called that Proc. Lambdas, on the other hand, will not. This means you can call a Lambda, get its return value, and process it, all within the one method;

  • Currying is the ability to take a function that accepts n parameters and generate from it one of more functions with some parameter values already filled in. In RUby 1.9, you create a curry-able proc by calling the curry method on it. If you subsequently call this curried proc with fewer parameters than it expects, it will not execute. Instead, it returns a new proc with those parameters already bound;

Ref:

Functional Programming Techniques With Ruby: Part I, II and III

Fun with Procs in Ruby 1.9

Proc in Ruby 1.9.3 Documentation



Published

Dec 9, 2013

Last Updated

Dec 9, 2013

Category

Tech

Tags

  • curry 1
  • functional programming 9
  • lambda 3
  • ruby 9

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor