Hello

As you all know, I'm a true red ruby coder now, so I decided to set up a thread for us Rubiers. Anyways, I'll start by asking a quick question.

Here is my code showing off my understanding of how lambda functions work, and I was wondering -- at the point of the while loop between the while and the end (of the nested lambda) , would the fixing of the string be returned simply? or would it just keep on looping, or both (set the return item to be the string, but keep going)? Here is the code, which is a Ruby version of a Cemetech-style 'L O L' to '0x5' converter:


Code:
StringFunction = lambda {|stringInput, pat, rep| #outwards lambda
  replaceAll = lambda {|inputString, pattern, replacement| #nested
    while inputString.sub!(pattern, replacement)
      inputString = inputString.sub(pattern, replacement)
    end
  }
  replaceAll.call(stringInput, pat, rep)
}

String NonLolzed = StringFunction.call(gets.chomp, " lol ", " 0x5 ")
#finally use all those lambdas :roll:


It should work if it doesn't immediately return after the setting on line 4.

EDIT: it even blocks an L then an O and an L together, so that's why there's 5 0x5's there, not because I put them.
it seems as though it DOES return with the second lambda expression. What would be the best method of fixing this -- using a regular function to do the looping and return a value for the nested one to use, or something entirely different?
I did some more practice and made a Quadratic Solver:


Code:
Qvalue = lambda {|a, b, c|
  Qlambda = lambda{|a, b, c, plus|
    if plus; -b + Math.sqrt(b**2-(4*a*c)).div(2*a)
    else;-b - Math.sqrt(b**2-(4*a*c)).div(2*a);end
  }
  if a == 0;nil;else
    [Qlambda.call(a,b,c,true),Qlambda.call(a,b,c,nil)];end }

Args = Array.new(3);Names = ['A','B','C']

3.times do |location|
  puts "Input: " << Names[location] << "?"
  Args[location] = gets.chomp;end
Ans = Qvalue.call(Args[1].to_f.to_r,Args[2].to_f.to_r,Args[3].to_f.to_r)

if Ans;puts 'Answer is ' << Ans[0].to_s << ' and ' << Ans[1].to_s << ' '
else;puts 'No Real Answers';end
does anyone know a module that has Read/Write to files functions and classes?

EDIT: found out that a built in module, IO, has great support for this kind of stuff. All those interested should look here at http://www.ruby-doc.org/core/classes/IO.html

I keep forgetting to check Ruby-Doc.org first and keep questions for later Razz

A quick question: who else here does/has/will/wants to/has at one point considered programming in Ruby?
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 Surprised so I'll go over how lambda functions can be invaluable:

let's say you have this working piece of code:


Code:
def Exponent(number,root)
   return number**root.to_f;end


if you want to load a variable such as 'Peanut' with an expression like "2 ** 5" you would constantly have to do:


Code:
Peanut = Exponent(2,5)


which is actually no problem. But calling to lambdas is a better Ruby approach. the exact same working code:


Code:
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:
def ProcExample(input, NUMBAR)
  return lambda {|input| input.chomp.flatten.reverse},
                            lambda {||NUMBAR.to_s.reverse};end

uno, dos = ProcExample


The 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 Razz
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement