The inject function

I want to add together numbers in an array with the inject command. I
want only the even numbers using only inject.

So, something like [1,2,3,4].inject…but don’t know what to do so
help!

Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]

I’m confused…help!

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

| I want to add together numbers in an array with the inject command. I
| want only the even numbers using only inject.
| So, something like [1,2,3,4].inject…but don’t know what to do so
| help!

Use select before inject:

[1,2,3,4,5,6].select {|i| i%2==0}.inject {|m,v| m+v}

You could use a test to sum the number only when it’s even on inject,
but

[1,2,3,4,5,6].inject {|m,v| m + (v%2==0?v:0) }

results to 13, because seems that the value of m is automatically the
first
element of the array. On this case, you can tell it to start with 0:

[1,2,3,4,5,6].inject(0) {|m,v| m + (v%2==0?v:0) }

| Second, I need to reverse an array using only the inject command. So,
| something like above with the result [4,3,2,1]

Why don’t you use reverse?

[1,2,3,4,5,6].reverse

Regards,
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFH6l6Hb6UiZnhJiLsRAjwlAJ9NGeatugJ2r9b/VJGWy7dUrTmXAACghLOA
Vfmi0X7wtEArwWQmgMshhmo=
=qKJi
-----END PGP SIGNATURE-----

I want to add together numbers in an array with the inject command. I
want only the even numbers using only inject.

So, something like [1,2,3,4].inject…but don’t know what to do so
help!

puts [1, 2, 3, 4].inject(0) {|a, e| (e % 2 == 0) ? e + a : a }

Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]

Use .reverse method. inject doesn’t fit in.


Rados³aw Bu³at

http://radarek.jogger.pl - mój blog

On Mar 26, 2008, at 9:35 AM, Rados³aw Bu³at wrote:

Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]

Use .reverse method. inject doesn’t fit in.

I agree that reverse() is the correct tool for this job, but just to
fully answer the question:

[1, 2, 3, 4].inject([]) { |rev, item| [item] + rev }
=> [4, 3, 2, 1]

James Edward G. II

I want to add together numbers in an array with the inject command. I
want only the even numbers using only inject.

a=[1,2,3,4]
a.inject(0) {|x,y| y % 2 == 0 ? x+y : x}
=> 6

Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]

a=[1,2,3,4]
a.inject([]) {|x,y| x.unshift(y)}
=> [4, 3, 2, 1]

It seems like an artificial requirement to use only “inject”.

On Wed, Mar 26, 2008 at 3:19 PM, Shendelzare Silkwood
[email protected] wrote:

I want to add together numbers in an array with the inject command. I
want only the even numbers using only inject.

So, something like [1,2,3,4].inject…but don’t know what to do so
help!

Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]
Hmm making your homework here?

inject([]){ |a,e| [e]+a }
that of course is a st****d way to reverse an array.

HTH
Robert

I’m confused…help!

Posted via http://www.ruby-forum.com/.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

| I agree that reverse() is the correct tool for this job, but just to
| fully answer the question:
| >> [1, 2, 3, 4].inject([]) { |rev, item| [item] + rev }

Just to use the same approach but with insert:

|> [1,2,3,4,5,6].inject([]){|m,v| m.insert(0,v)}
=> [6, 5, 4, 3, 2, 1]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFH6mMbb6UiZnhJiLsRAsmcAKCJL0YI4hyf1/37tP7PCs7rQx/fsQCdFFcW
ponhdDBGbDpbA5JsPMnu+Wk=
=I8Go
-----END PGP SIGNATURE-----

On 26.03.2008 15:32, Eustáquio ‘TaQ’ Rangel wrote:

You could use a test to sum the number only when it’s even on inject, but

[1,2,3,4,5,6].inject {|m,v| m + (v%2==0?v:0) }

results to 13, because seems that the value of m is automatically the first
element of the array. On this case, you can tell it to start with 0:

[1,2,3,4,5,6].inject(0) {|m,v| m + (v%2==0?v:0) }

Actually these two are not equivalent! You should usually use the
second form because the first form will return nil for an empty Array.

Btw, Radoslav’s solution omits unnecessary additions of 0:

[1,2,3,4,5,6].inject(0) {|m,v| v % 2 == 0 ? m : m+v }

| Second, I need to reverse an array using only the inject command. So,
| something like above with the result [4,3,2,1]

Why don’t you use reverse?

I assume it is some kind of task assignment - which is why I regret that
solutions were posted so quickly.

Kind regards

robert

On 26.03.2008 15:52, Eustáquio ‘TaQ’ Rangel wrote:

=> [6, 5, 4, 3, 2, 1]
While we’re improving…

irb(main):001:0> [1,2,3,4,5,6].inject([]) {|a,x| a.unshift x}
=> [6, 5, 4, 3, 2, 1]

Oh, I just see Dan has it already.

Kind regards

robert

On Mar 26, 2008, at 9:35 AM, Rados³aw Bu³at wrote:

Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]

Use .reverse method. inject doesn’t fit in.

I’m guessing this is a homework assignment (otherwise why would inject
be required). You can reverse using inject if the accumulator is an
array…

Dave

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert K. wrote:
| On 26.03.2008 15:32, Eustáquio ‘TaQ’ Rangel wrote:
|> You could use a test to sum the number only when it’s even on inject, but
|> [1,2,3,4,5,6].inject {|m,v| m + (v%2==0?v:0) }
|> results to 13, because seems that the value of m is automatically the
|> first
|> element of the array. On this case, you can tell it to start with 0:
|> [1,2,3,4,5,6].inject(0) {|m,v| m + (v%2==0?v:0) }
| Actually these two are not equivalent! You should usually use the
| second form because the first form will return nil for an empty Array.

Hi Robert!

Yeah, and I just put the first form because at first glance they look
equivalent
for newbies on the language and can make some confusion for “where are
the 1
value coming from”. :slight_smile:

Regards,
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFH6p+pb6UiZnhJiLsRAhwnAJ96znoMtgWQSmGg/R7fNzkNbo0tmACgnuXQ
EB5r2goC0lh3WSxTsoLBN64=
=vvYN
-----END PGP SIGNATURE-----

On Thu, 27 Mar 2008 03:34:55 +0900, Robert K. wrote:

I assume it is some kind of task assignment - which is why I regret that
solutions were posted so quickly.

I for one am curious about who assigned it :slight_smile:

-Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs