The inject function

  • Thread starter Shendelzare Silkwood
  • Start date
S

Shendelzare Silkwood

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!
 
E

Eustáquio 'TaQ' Rangel

-----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-----
 
R

Rados³aw Bu³at

PiBJIHdhbnQgdG8gYWRkIHRvZ2V0aGVyIG51bWJlcnMgaW4gYW4gYXJyYXkgd2l0aCB0aGUgaW5q
ZWN0IGNvbW1hbmQuIEkKPiAgd2FudCBvbmx5IHRoZSBldmVuIG51bWJlcnMgdXNpbmcgb25seSBp
bmplY3QuCj4KPiAgU28sIHNvbWV0aGluZyBsaWtlIFsxLDIsMyw0XS5pbmplY3QuLi4uLmJ1dCBk
b24ndCBrbm93IHdoYXQgdG8gZG8gc28KPiAgaGVscCEKCnB1dHMgWzEsIDIsIDMsIDRdLmluamVj
dCgwKSB7fGEsIGV8IChlICUgMiA9PSAwKSA/IGUgKyBhIDogYSB9Cgo+ICBTZWNvbmQsIEkgbmVl
ZCB0byByZXZlcnNlIGFuIGFycmF5IHVzaW5nIG9ubHkgdGhlIGluamVjdCBjb21tYW5kLiBTbywK
PiAgc29tZXRoaW5nIGxpa2UgYWJvdmUgd2l0aCB0aGUgcmVzdWx0IFs0LDMsMiwxXQoKVXNlIC5y
ZXZlcnNlIG1ldGhvZC4gaW5qZWN0IGRvZXNuJ3QgZml0IGluLgoKCi0tIApSYWRvc7NhdyBCdbNh
dAoKaHR0cDovL3JhZGFyZWsuam9nZ2VyLnBsIC0gbfNqIGJsb2cK
 
R

Robert Dober

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
 
D

Dave Thomas

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 =20=

be required). You can reverse using inject if the accumulator is an =20
array...


Dave=
 
E

Eustáquio 'TaQ' Rangel

-----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-----
 
R

Robert Klemme

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
 
R

Robert Klemme

-----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]

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
 
E

Eustáquio 'TaQ' Rangel

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

Robert Klemme 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". :)

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

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

thufir

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 :)


-Thufir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,121
Latest member
ColinHibne

Latest Threads

Top