Skip to main content

Passing Functions and Lambdas into Functions with Ruby

Ruby's New Style of Lambda Functions


f = ->( m ) { p m }
f.call( 1 )
#=> 1
Which of course means the same thing as:


f = lambda { |n| p n }
f.call(1)
#=> 1

Ruby Proc Objects


p = Proc.new { |n| p n + 2 }
p.call(2)
#=> 4

Using a Function as a Closure in Ruby


def domo( k )
  ->(m) { p m + k }
end

z = domo( 5 )
z.call( 5 )
#=> 10
Function :domo takes a single parameter. Within :domo, we create a lambda that takes a single parameter, and adds that parameter to the value :domo takes in as its parameter. Then, we assign z to be the result of the lambda in :domo with its 'k' parameter loaded with 5.

When z is called, we pass (another) 5 to it. This parameter loads the lambdas n parameter. The lambda executes, essentially adding n(5) + k(5) and yielding the result of 10.

The thing about closures such as this is, we can load the initial value of the lambda to be whatever we want it to be when assigning the function :domo's return value to z. That becomes a fixed value that z will always have. But..., we can call z with whatever value we wish to add z to. For example:


z = domo(6)
z.call(4)
#=> 10
z.call(6)
#=> 12

etc., etc. That's the power of the closure. You could set up a number of such closures with constant values set up, and then operate on those values with whatever numbers you wish to pass in. Each closure represents its own individual processing with its own loaded value.

Passing Functions to Functions in Ruby


def fritter( f, n )
  func = method( f )
  func.call( :n => n )
end

def mac( opts = {} )
  v = opts[ :n ]
  v + 6
end

p fritter( :mac, 6 )
# => 12

Passing one function to another function as a parameter is how Ruby can be used in a functional programming style. The function :fritter takes the function :mac, as well as a number, as parameters.

Inside :fritter, on the first line, the symbol passed in to parameter f is converted to a method object via the :method function. This method object is stored in the variable, func.
Next, on the second line of :fritter, we call the method object, which is the method, :mac, sending it the parameter 6
To pass a function to another function in Ruby, the name of the function is passed. To name a function, a symbol must be used. It is the symbol that represents the functions name. So, the name of the mac function is :mac.

Now, consider the function, :mac. :mac takes a hash as a parameter. Inside the function, the local variable, v is assigned either the value attached to the key :n, or 1 if :n has no value. :mac then returns the sum of its local variable v, and the number 6.

Finally, the function call of :fritter, with the parameters of the :mac function, and the numeral 6 ( note that this 6 id different from the 6 within the body of :mac. So, :fritter is called, and it returns the return value of :mac called itself with a parameter of 6!

Somewhat convoluted? Admittedly, this example is. But its the idea that is important. You can change the functions to solve whatever problem you're working on. Here I wanted to show just the mechanics of passing a function a function.

Passing Lambdas to a Function in Ruby


@even = ->( n ) { n % 2 == 0 }

def fun( e, n )
  e.call( n )
end

def negate( e, n )
  !e.call( n )
end

The lambda here, @even, simply takes an input parameter of n, and tests to see if it is even.
The function, :fun, simply calls the @even lambda with the numeral parameter, n that is passed in.

The function, :negate, literally does the opposite of :fun. It does this in a very simple way: it literally
negates the @even function called with the numerical parameter, n.


fun( @even, 2 )
#=> true
fun( @even, 1 )
#=> false
negate( @even, 2 )
#=> false
negate( @even, 1 )
#=> true

Both of the functions are called, each with an even value, and an odd value. They return the expected boolean results.

Comments

Popular posts from this blog

Fun with Meteor, React, and React-Bootstrap

React-Bootstrap is pretty cool. I decided to play with it a bit.  Here are the basics. In an already set up Meteor project (set up for React), it is added thus: npm install --save react-bootstrap Once this is done, you also need to add a bootstrap library. It could either be the twitter bootstrap meteor package, or you can link to it. For the purpose of my demo, I just grabbed a couple links from the React-Bootstrap site that they had handy for pulling in from a CMS: index.html <head> <!-- Latest compiled and minified CSS --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" rel="stylesheet"> </link> <!-- Optional theme --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap-theme.min.css" rel="stylesheet"> </link> </head> Now, let's make a layout, and then create a component tha...

OS X Process-Fu 101

OS X is of course a variety of BSD Unix, not Linux. Often is the case where Linux commands either don't work, or don't give the same results on OS X as they do on Linux. For example, a basic netstat will give you a long list of the processes making network connections on your box. On Linux, various command line options can help you drill down from there, to get the answers to more specific queries. On OS X, those same queries don't give you the answers you're seeking. This isn't a compare and contrast article between Linux and OSX. Instead, I'm just noting various commands that can give answers to some basic questions. On OS X, for example, to get an answer to the question: "What process is running on port 50224?" we would use: sudo lsof -Pn | grep 50224 This will give you a list of everything connected to port 50224, sans any kind of headings. A much more abbreviated command that will give you essentially the same things, but with column...