I find myself annoyed with the following verbose writing in Ruby:
polys.each { |poly| poly.edges.each {|edge| draw edge.start, edge.end } }
(polys is an Array of Polygons, edges is a method of Polygon returning an Array of Edges)
Ideally I would like to shorten this to something like this:
polys.each.edges.each { draw _.start, _.end }
More specifically I would like to know:
How could we write a method_missing hack as with the first each? (Upon being called with a missing method, the enumerator could call this method on each item returned from the enumeration)
Is there a way to get rid of the |x| using any symbol or default name?
No. Closest you can do would be:
polys.flat_map(&:edges).each { |_| draw _.start, _.end }
flat_map will convert an array in another array and flatten it to a single dimension array. If the inside of a block is calling a single method with no parameters, you can use the &:edges shortcut.
This being said, I would probably keep it closer to your initial proposal, as it's more readable:
polys.each do |poly|
poly.edges.each {|edge| draw edge.start, edge.end }
end
Remember, you write code once but it's read a lot, so readability trumps terseness.
Related
I'm wanting to modify a variable in place, replicating the methodname! syntax as opposed to reassigning a new modified value to the same var. Can I do this with a proc? I'm still learning procs and see them as quite useful is used properly.
a = "Santol bag 85.88 www.example.com/products/16785
Shaddock kg 2.94 www.example.com/products/4109
Palm Fig 5kg 94.34 www.example.com/products/23072
Litchee lb 95.85 www.example.com/products/2557"
a = a.split("\n")
linebreak = Proc.new { |text| text.split("\n") }
linebreak![a]
that first reassignment seems cumbersome. The proc version I would like to see if I can perform it inline. Is this possible?
This is surely possible, you just need to modify the string inplace
linebreak = ->(text) { text.replace text.split("\n").join(",") }
a = "foo\nbar"
linebreak[a]
#⇒ "foo,bar"
a
#⇒ "foo,bar"
What is not possible, is to change the class in place, that’s why split won’t work (called on a string, it returns an array.)
methodname! is just a convention - usually there are two flavours of the same method - one without bang and one with bang. If you want to have a proc that mutates its params, you need to implement it using mutating methods.
And in this case it's not possible, because you're trying to transform a string into an array. You have to reassign the variable:
linebreak = Proc.new { |text| text.split("\n") }
a = linebreak.call(a)
In ruby, the following expression:
x.filter {|n| n.even?}
can also be written as:
x.filter(&:even?)
so, I am wondering how I would write this expression?
x.filter {|n| !n.even?}
without using odd? method
As Sam and engineerskmnky said in the comments below question, it is not possible to perform x.filter { |n| !n.even? } operation directly (and in fact two operations inside the block).
I guess that this was only a trivial example and not a real code so if you have method that does not have the inverse one and you don't want to create one, you can create a lambda or proc in the following way:
not_even = -> (n) { !n.even? }
and then call it on filter as:
x.filter(¬_even)
You can also use reject method which should give you the same result without the magic of using lambda.
Let's say I have an array of N elements. I call a recursive function somehow like this: (no specific language here, just pseudocode)
recursive(myArray){
// do something awesome and provide base case etc
// also get mySecondArray based on myArray
for(i=0;i<mySecondArray.length;i++){
recursive(mySecondArray[i];
}
}
As you can see I need to call this function on every element of another array created inside based on some conditions and other functions called on myArray.
The problem I am having is that mySecondArray always has some of the elements that were already in myArray. I do not want to call recursion again on those elements.
Q: What would be the best algorithm approach to solve this?
If you need more info just let me know (I didn't get into details since it gets more complicated)
Thanks
You can have a hashmap/set/dictionary/whatever-you-call-it to look up the elements.
Python solution:
def recursive(myArray, mySet = None):
if mySet is None:
mySet = { myArray }
else:
mySet.add(myArray)
for mySecondArray in myArray:
if mySecondArray not in mySet:
recursive(myArray, mySet)
By the way writing recursive functions like that is a very bad idea in general. You should use a single function and a stack of the arguments if possible.
P.S.: Your code was incomplete by the way but the idea is the same.
Does Groovy have something similar to bang methods on Ruby?
From this blog post:
In Ruby, you can write methods whose names end in ! (exclamation point or “bang”). There’s a lot of confusion surrounding the matter of when, and why, you would want to do so.
The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name.*
And this site:
You'll find a number of pairs of methods, one with the bang and one without. Those without the bang perform an action and return a freshly minted object, reflecting the results of the action (capitalizing a string, sorting an array, and so on). The bang versions of the same methods perform the action, but they do so in place: Instead of creating a new object, they transform the original object.
This is not a convention in Groovy like it is in Ruby. However you can write methods with names that contain characters like ! with the limitation that it must always be quoted like a string:
// define method with quoted name
def 'dangerous!'() {
// do something dangerous
}
// invoke method with quoted name
'dangerous!'()
No, groovy (currently as of v2.1.4) doesn't have anything like this
To add to your options, another solution that would be more Groovy-like or Java-like would be to include an optional parameter that enabled in-place (a.k.a. dangerous) modification, like so:
def processFoo(Foo item, mutate = false) {
if(!mutate) {
Foo temp = new Foo()
// copy item properties
item = temp
}
item.bar = 'blah blah'
// process item here
return item
}
processFoo(myFoo) // makes a copy
processFoo(myFoo, true) // modifies original
This pattern is used — albeit in the opposite manner — with the sort method on collections. Calling sort(false) on Lists prevents changing the original array. Calling sort() or sort(true) will modify it directly.
Right now the code below produces the output below it, but how would I override the default output to a more logical one for my given situation. I understand that I could just append the string "Hz" after the range but I want to incorporate this into a module which can be included to the Range class when needed or for use with refinements.
Code:
("20Hz"..."40Hz").each { |hz| p hz }
Output:
"20Hz"
"20Ia"
"20Ib"
...etc
Wanted output:
"20Hz"
"21Hz"
"22Hz"
...etc
This is absolutely a bad idea, but just for the sake of experimenting:
class String
alias_method :succ_orig, :succ
def succ
self.gsub(/\d+/, &:succ_orig)
end
end
p ("20Hz".."40Hz").to_a
#=> ["20Hz", "21Hz", "22Hz", "23Hz", "24Hz", "25Hz", "26Hz", "27Hz", "28Hz", "29Hz", "30Hz", "31Hz", "32Hz", "33Hz", "34Hz", "35Hz", "36Hz", "37Hz", "38Hz", "39Hz", "40Hz"]
As you can see, it is not the Range class that should be altered, but String#succ method.
But in real project, you better create a class for your Hertz-strings and define its succ method appropriately.
I think its quite simple.
("20"..."40").each { |hz| p hz + 'Hz'}
I would recommend creating your own function or class for this rather that changing the way in which Ruby ranges behave. There is probably a lot of other code that depends on ranges working in a specific way, and changing the range definition would result in that code breaking. You might want to aim for something like this:
HzRange.new("20Hz", "40Hz").each{ |hz| p hz }
The creation of the HzRange class is up to you, but you should probably delegate to the Array or Range object so that you can inherit some default behavior like Enumerable.