Ruby inject to create array - ruby-on-rails

I have this code
notebooks.inject([]) do |res, nb|
res << nb.guid if Recipe::NOTEBOOKS.include?(nb.name)
end
The first nb has matches the condition and res looks like this
["xxx1234"]
The second nb does not match the condition which then delete/clears res
nil
From my understanding, the first value should remain in the array.
I'm also assigning this to a variable and want it to be a one liner.

inject works a little differently from how you're imagining. It simply returns the last return value of the loop as it loops through each item. An easy way to fix this is:
notebooks.inject([]) do |res, nb|
res << nb.guid if Recipe::NOTEBOOKS.include?(nb.name)
res # Returns the res array
end
That said, you should probably use select for your use case as you seem to be just filtering down which set of notebooks you want.. That is:
notebooks.select{|nb| Recipe::NOTEBOOKS.include?(nb.name)}.map(&:guid)
Generally, I've used inject when I need to run math on a group of items. e.g.
[1,2,3,4].inject(0) {|res, x| x * 2 + res}

If you're open to two loops, but cleaner and still one-liner:
notebooks.select { |nb| Recipe::NOTEBOOKS.include?(nb.name) }.map(&:guid)

The accumulator must be returned on each loop iteration:
notebooks.inject([]) do |res, nb|
Recipe::NOTEBOOKS.include?(nb.name) ? res << nb.guid : res
end
Actually, on each subsequent loop iteration, the accumulator passed to res block parameter is exactly what was returned from the previous iteration.
In your example, on the second iteration if returns false and
res << nb.guid if Recipe::NOTEBOOKS.include?(nb.name)
line is not executed at all. That said, after the second iteration, the accumulator takes a brand new value, that is apparently nil.

Related

No Output for the Elixir Program

I am trying to solve a dynamic problem finding the subsets i have written the code but i didn't know why i am not getting anything it just blinks after running Todos.sum_of_one(arr_of_digits, sum_val), I think the problem is in the terminating case when n==0, can anyone please tell me where is the mistake
def Todos do
#find all the subsets whose sum is equal to sum_val
def sumofone(arr_of_digits,n,v,sum)do
if(sum==0) do
for i <- v do
i
end
end
#return if n becomes 0
if(n==0) do
v
end
sumofone(arr_of_digits,n-1,v,sum)
k = Enum.at(arr_of_digits,n-1)
#inserting the element in the list
[k | v]
sumofone(arr_of_digits,n-1,v,sum - arr_of_digits[n-1]);
end
def sum_of_one(arr_of_digits, sum_val) do
v = []
sumofone(arr_of_digits,l,v,sum_val)
end
end
It looks like you're trying to return from the function in the two if expressions. Elixir doesn't work that way - it always* runs through the entire function and returns the value of the last expression in the function.
One way to get around this is to break up the code into different function clauses, where each clause matches one of the conditions you're testing for:
# This clause executes when the fourth argument is 0
def sumofone(_arr_of_digits,_n,v,0) do
for i <- v do
i
end
end
# This clause executes when the second argument is 0
def sumofone(_arr_of_digits,0,v,_sum) do
v
end
# This clause executes in all other cases, as long as n is greater than 0
def sumofone(arr_of_digits,n,v,sum) when n > 0 do
sumofone(arr_of_digits,n-1,v,sum)
k = Enum.at(arr_of_digits,n-1)
#inserting the element in the list
[k | v]
sumofone(arr_of_digits,n-1,v,sum - arr_of_digits[n-1]);
end
With this change, it's guaranteed that the function will actually terminate. It still won't do what you expect it to do, since there are two lines that calculate a value but throw it away. In Elixir, if you want to update the value of a variable, you need to do so explicitly. Did you mean something like this?
sum = sumofone(arr_of_digits,n-1,v,sum)
and
#inserting the element in the list
v = [k | v]
But I'll leave that for you to debug.
Note that I prefixed some of the argument names with an underscore. Without that, the compiler would give a warning about the variable being unused. With the underscore, it's clear that this is in fact intended.
* Except if you're using errors, throws and exits. But try not to use them - it's often clearer not to.

[lua]: Key with value 1 doesnt do the same as regular 1

Ive been coding for a mod Im making for a game but I ran into an issue with tables not returning values when the key is entered:
for k, v in pairs(self.math) do
print(self.exce[1])
print(self.exce[k])
print(k)
if self.exce[k] ~= nil then
self.math[k] = nil
end
end
This is the specific part of the script that is breaking. When I run these in the game it returns:
[lua]: true
[lua]: nil
[lua]: 1
Which means is basically saying that 1 is not equal to 1.
The function I used to store my data is
function filterExceptions.server_onException( self, id )
if self.exce[id] == nil then
self.exce[id] = true
self.network:sendToClients( "client_onList", id )
else
self.exce[id] = true
self.network:sendToClients( "client_offList", id )
end
end
In this code the self is a table made by the game you can acces and get game data from or store it in and the id comes from a function I made to get the players id. This id in this case is a 1 (I printed it multiple times).I know that every part of this code is working except for the code in the first block, and escpecialy the part where it tries to do self.exce[k]. Ive tried a lot like going trough every variable in self.exce to see if it was in there and then do stuff, but it still wouldn't work. Its very annoying how lua thinks that k ~= 1 while it definitely is, ive even used similar code in a part that is working.
So what is wrong about this code that its not printing the self.exce[k] while self.exce[1] does work? Dont worry about the creation of the table and stuff, cuz that is already happening whenever it is needed, else it would have given errors about that too.
Putting together a couple different comments and your code here, it looks like the index value of the array in some particular iteration of the "for in pairs" loop (or perhaps all of them, but I'll touch on that in a minute) is a string instead of an integer.
To summarize if you don't want to read the entire thing, "for k, v in pairs" loops will iterate through an entire array, setting k to the index of the value v. It appears your "for in pairs" loop is attempting to iterate through a value of nil where k is a string instead of an integer. You may also want to look into using ipairs instead of pairs in your for loop.
The value of someArray[1]is different than the value of someArray["1"].
The index [1] is a completely different index than the index ["1"] for any given array.
A simple fix would be to use
ind = tonumber(k)
print(self.exce[ind])
This converts the string k to a number type. Be aware this may throw an error if k is a non-numerical string. If the array has any values where the index is a non-numerical string, you may get an error. As the other answer suggests, converting the index k to a string instead of an integer would work as well, and would not throw errors if you used a non-numerical value for your indices.
My guess as to why this is happening would be that the function that you're using to store your data to an array, filterExceptions.server_onException( self, id ), is being passed a string instead of an integer, which would result in the k value being set to a string in that particular iteration of the "for in pairs" loop.
To help better understand this, here's a bit of example code:
a = {true, false, false}
a[1] = true
a["1"] = true
print("Raw for in pairs loop")
for k, v in pairs (a) do
print(type(k)..k)
end
print("For in pairs converting k to a number")
for k, v in pairs (a) do
ind = tonumber(k)
print(type(ind)..ind)
end
print("For in ipairs")
--which I'm not sure I completely understand but
--it seems to skip over any iteration where k is not a number
for k, v in ipairs(a) do
print(type(k)..k)
end
This code produces the following output:
Raw for in pairs loop
number1
number2
number3
string1
For in pairs converting k to a number
number1
number2
number3
number1
For in ipairs
number1
number2
number3
EDIT: Not sure what's going on in the self.math table so I can't comment on that.
EDIT2: I'd also refer you to the following link: lua: iterate through all pairs in table
The top answer there should help understand the difference between pairs and ipairs, if you don't already. You may want to use ipairs to prevent values of k where v == nil from being iterated through with pairs. pairs will iterate through every key/value pair, whereas ipairs will iterate through integer keys starting at 1 and going until it hits a nil value.
EDIT3: I'm sorry this is such a long answer...I just wanted to be thorough.
It apears converting the id to a string fixes this, tough im still confused as to why this same code worked on another block and not this one.
function filterExceptions.server_onException( self, id )
local id2 = tostring(id)
if self.exce[id2] == nil then
self.exce[id2] = true
self.network:sendToClients( "client_onList", id )
else
self.exce[id2] = true
self.network:sendToClients( "client_offList", id )
end
end

Rails inject() doesn't work on an array of one item

I have this line
week[1].inject{ |sum, jog| jog.distance }
Which gets the total distance run in week[1], which is an array of Jog records. This works when there are multiple records, but if there is only one for that week, inject simply returns the record itself, not its distance..
Is there any way for inject to work on arrays of single items, the same as larger arrays?
From the fine manual:
inject(initial, sym) → obj
inject(sym) → obj
inject(initial) { |memo, obj| block } → obj
inject { |memo, obj| block } → obj
[...]
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
So if a is a one element array then:
a.inject { anything_at_all }
is equivalent to
a.first
This behavior is really just a shortcut to make things like:
a.inject(:+)
and such quick and easy to say.
If you need your block to always run then you just have to supply the initial value and make sure your block does what it needs to do:
week[1].inject(0) { |sum, jog| sum + jog.distance }
# -------------^ ^^^^^
# -----------------------------^^^^^
Without the sum + in the block you'll just end up with week[1].distance (assuming you've supplied the 0 initial value of course).
You could also say something like:
week[1].map(&:distance).inject(:+)
week[1].map(&:distance).sum
to solve the problem another way. You could also do it in the database if you don't need the individual records.

Ruby method returns hash values in binary

I wrote a method that takes six names then generates an array of seven random numbers using four 6-sided dice. The lowest value of the four 6-sided dice is dropped, then the remainder is summed to create the value. The value is then added to an array.
Once seven numbers have been generated, the array is then ordered from highest to lowest and the lowest value is dropped. Then the array of names and the array of values are zipped together to create a hash.
This method ensures that the first name in the array of names receives the highest value, and the last name receives the lowest.
This is the result of calling the method:
{:strength=>1, :dexterity=>1, :constitution=>0, :intelligence=>0, :wisdom=>0, :charisma=>1}
As you can see, all the values I receive are either "1" or "0". I have no idea how this is happening.
Here is the code:
module PriorityStatGenerator
def self.roll_stats(first_stat, second_stat, third_stat, fourth_stat, fifth_stat, sixth_stat)
stats_priority = [first_stat, second_stat, third_stat, fourth_stat, fifth_stat, sixth_stat].map(&:to_sym)
roll_array = self.roll
return Hash[stats_priority.zip(roll_array)]
end
private
def self.roll
roll_array = []
7.times {
roll_array << Array.new(4).map{ 1 + rand(6) }.sort.drop(1).sum
}
roll_array.reverse.delete_at(6)
end
end
This is how I'm calling the method while I'm testing:
render plain: PriorityStatGenerator.roll_stats(params[:prioritize][:first_stat], params[:prioritize][:second_stat], params[:prioritize][:third_stat], params[:prioritize][:fourth_stat], params[:prioritize][:fifth_stat], params[:prioritize][:sixth_stat])
I added require 'priority_stat_generator' where I'm calling the method, so it is properly calling it.
Can someone help me make it return proper values between 1 and 18?
Here's a refactoring to simplify things and use an actually random number generator, as rand is notoriously terrible:
require 'securerandom'
module PriorityStatGenerator
def self.roll_stats(*stats)
Hash[
stats.map(&:to_sym).zip(self.roll(stats.length).reverse)
]
end
private
def self.roll(n = 7)
(n + 1).times.map do
4.times.map { 1 + SecureRandom.random_number(6) }.sort.drop(1).inject(:+)
end.sort.last(n)
end
end
This makes use of inject(:+) so it works in plain Ruby, no ActiveSupport required.
The use of *stats makes the roll_stats function way more flexible. Your version has a very rigid number of parameters, which is confusing and often obnoxious to use. Treating the arguments as an array avoids a lot of the binding on the expectation that there's six of them.
As a note it's not clear why you're making N+1 roles and then discarding the last. That's the same as generating N and discarding none. Maybe you meant to sort them and take the N best?
Update: Added sort and reverse to properly map in terms of priority.
You need to learn to use IRB or PRY to test snippets of your code, or better, learn to use a debugger. They give you insight into what your code is doing.
In IRB:
[7,6,5,4,3,2,1].delete_at(6)
1
In other words, delete_at(6) is doing what it's supposed to, but that's not what you want. Instead, perhaps slicing the array will behave more like you expect:
>> [7,6,5,4,3,2,1][0..-2]
[
[0] 7,
[1] 6,
[2] 5,
[3] 4,
[4] 3,
[5] 2
]
Also, in your code, it's not necessary to return a value when that operation is the last logical step in a method. Ruby will return the last value seen:
Hash[stats_priority.zip(roll_array)]
As amadan said, I can't see how you are getting the results you are, but their is a definite bug in your code.
The last line in self.roll is the return value.
roll_array.reverse.delete_at(6)
Which is going to return the value that was deleted. You need to add a new lines to return the roll_array instead of the delete_at value. You are also not sorting your array prior to removing that last item which will give you the wrong values as well.
def self.roll
roll_array = []
7.times {
roll_array << Array.new(4).map{ 1 + rand(6) }.sort.drop(1).sum
}
roll_array.sort.drop(1)
roll_array
end

How do I retain the value of a for loop variable when the loop is over?

I'm trying to find something in a loop in Lua, and when I'm done I need to use the location I found.
for j = 1,100 do
<do some stuff>
if <some test> then
break
end
end
if j >= 100 then
return
end
Unfortunately, I get an error which suggests that after the for loop exits, the value of j is nil. How do I use the value that j ended at? Obviously I could create an extra variable and assign it right before I break, but that just seems wrong, and I've never seen another language set the loop variable to nil when the loop ends, so I'm wondering if there isn't a better way to accomplish this.
The loop variable is only visible inside the for loop block. You can get around this by creating another variable as suggested in PIL 4.3.4 Numeric For.
local index
for j=1,100 do
if j == 10 then
index = j
end
end
Alternatively, if you are a doing a common operation then using a function with an early return may be best.
function find(tbl, val)
for i, v in ipairs(tbl) do
if v == val then
return i
end
end
end

Resources