[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

The inject function

Shendelzare Silkwood

3/26/2008 2:19:00 PM

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!
--
Posted via http://www.ruby-....

10 Answers

Eustaquio 'TaQ' Rangel

3/26/2008 2:33:00 PM

0

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

Radoslaw Bulat

3/26/2008 2:36:00 PM

0

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

Robert Dober

3/26/2008 2:39:00 PM

0

On Wed, Mar 26, 2008 at 3:19 PM, Shendelzare Silkwood
<zerruellx@yahoo.com> 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-....
>
>



--
http://ruby-smalltalk.blo...

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

James Gray

3/26/2008 2:42:00 PM

0

On Mar 26, 2008, at 9:35 AM, Rados=B3aw Bu=B3at 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 =20
fully answer the question:

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

James Edward Gray II=

Dave Thomas

3/26/2008 2:45:00 PM

0


On Mar 26, 2008, at 9:35 AM, Rados=B3aw Bu=B3at 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 =20=

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


Dave=

Eustaquio 'TaQ' Rangel

3/26/2008 2:52:00 PM

0

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

Robert Klemme

3/26/2008 6:35:00 PM

0

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

Robert Klemme

3/26/2008 6:35:00 PM

0

On 26.03.2008 15:52, Eustáquio 'TaQ' Rangel wrote:
> -----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



Eustaquio 'TaQ' Rangel

3/26/2008 7:05:00 PM

0

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

Thufir Hawat

4/2/2008 8:25:00 AM

0

On Thu, 27 Mar 2008 03:34:55 +0900, Robert Klemme 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 :)


-Thufir