0 Members and 3 Guests are viewing this topic.
The only place that I heard about ruby was in RPG Maker XP
Maybe I'm misunderstanding something here (since I don't use Ruby), but couldn't you do exactly the same thing with regular (non-lambda) functions? Or are lambda functions somehow different than regular functions in Ruby? I thought one of the points of lambda functions was the ability to pass a (lambda) function to a second function as an argument, wherein the second function calls the lambda function inside of it. I don't see any of that going on in your code, which would make using lambda functions pointless.
I did it purely for practice of point since they can be easily misused, I'm practicing to make sure I have them down right.Yeah, it's a bit more complex, but they're more useful for higher-level math concepts, and even better for managing code snippets.EDIT: I only answered part of your question methinks so I'll go over how lambda functions can be invaluable:let's say you have this working piece of code:Code: [Select]def Exponent(number,root) return number**root.to_f;endif you want to load a variable such as 'Peanut' with an expression like "2 ** 5" you would constantly have to do:Code: [Select]Peanut = Exponent(2,5)which is actually no problem. But calling to lambdas is a better Ruby approach. the exact same working code:Code: [Select]Peanut = lambda{|number,root| number**root}Peanut.call(2,5)you see, with this method, Peanut is directly attached to the function -- lambda functions are considered actual values, not functions themselves. This allows for a higher level of abstraction in code, and nesting lambdas can be extremely useful when solving for complex equations -- it's easier to read and keep track of. Such as in my Quadratic solver (which looks difficult but really is just a simple example of what I'm talking about) -- the lambda function isn't really a function -- it's a value that is determined by calling 'arguments' (I use that loosely, I forgot the true word for it)This is one way to achieve 'anonymous functions' in Ruby, but actually isn't the most powerful way. Another way that provides even less hassle is by declaring a Proc Function:Code: [Select]def ProcExample(input, NUMBAR) return lambda {|input| input.chomp.flatten.reverse}, lambda {||NUMBAR.to_s.reverse};enduno, dos = ProcExampleThe only real difference between a normal lambda statement and a proc statement is actually usage -- lambdas are when you simply just bind a function to a value. Procs are when you use them on the fly to return a value based on 'arguments' (again, loosely used) like lambdas, but the difference is that they are usually more block-like and aren't directly bound to an actual variable -- very often seen as arguments of other lambdas or procs or even normal functions, or as returned values (with a lambda, you would return the value by using a call statement, procs would simply be declared at the moment).I hope that clears a few things up
def Exponent(number,root) return number**root.to_f;end
Peanut = Exponent(2,5)
Peanut = lambda{|number,root| number**root}Peanut.call(2,5)
def ProcExample(input, NUMBAR) return lambda {|input| input.chomp.flatten.reverse}, lambda {||NUMBAR.to_s.reverse};enduno, dos = ProcExample