Can you plz, explain to me part of this code ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Can you plz, explain to me part of this code ?

Abdel Latif
Hi,

I found this code for sorting an array :

p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
     
.flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

ouptut

#⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]


I know that .partition{|x| x.to_i.zero? } will create an array of arrays :
[["AB_1020", "AB_950", "AB_50"], ["1000", "570"]]


But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

I could understand it.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can you plz, explain to me part of this code ?

Walter Lee Davis

> On Apr 26, 2018, at 12:12 PM, Abdel Latif <[hidden email]> wrote:
>
> Hi,
>
> I found this code for sorting an array :
>
> p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
>       .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}
> ouptut
>
> #⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]
>
>
> I know that .partition{|x| x.to_i.zero? } will create an array of arrays :
> [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]]
>
>
> But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

flat_map: https://apidock.com/ruby/Enumerable/flat_map

Then that passes a block, which acts on each member of the Enumerable that called flat_map. That block sorts by the numerical value of x, then reverses.

Does that help?

Walter

>
> I could understand it.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F80F3086-EFC6-4E2F-99E8-07C60AAD479D%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can you plz, explain to me part of this code ?

Abdel Latif
But [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]] has non numerical values too.

On Thursday, April 26, 2018 at 1:05:53 PM UTC-4, Walter Lee Davis wrote:

> On Apr 26, 2018, at 12:12 PM, Abdel Latif <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="MAhqLxxrBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mlotf...@...> wrote:
>
> Hi,
>
> I found this code for sorting an array :
>
> p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
>       .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}
> ouptut
>
> #⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]
>
>
> I know that .partition{|x| x.to_i.zero? } will create an array of arrays :
> [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]]
>
>
> But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

flat_map: <a href="https://apidock.com/ruby/Enumerable/flat_map" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapidock.com%2Fruby%2FEnumerable%2Fflat_map\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLzj5m9aJVfED2zbbH3lk0RnjMiA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapidock.com%2Fruby%2FEnumerable%2Fflat_map\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLzj5m9aJVfED2zbbH3lk0RnjMiA&#39;;return true;">https://apidock.com/ruby/Enumerable/flat_map

Then that passes a block, which acts on each member of the Enumerable that called flat_map. That block sorts by the numerical value of x, then reverses.

Does that help?

Walter

>
> I could understand it.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="MAhqLxxrBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="MAhqLxxrBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f6fa4cc4-37b2-4828-ba1c-f9ac0d780221%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can you plz, explain to me part of this code ?

Rob Biedenharn-3


On 2018-Apr-26, at 13:47 , Abdel Latif <[hidden email]> wrote:

But [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]] has non numerical values too.

On Thursday, April 26, 2018 at 1:05:53 PM UTC-4, Walter Lee Davis wrote:

> On Apr 26, 2018, at 12:12 PM, Abdel Latif <[hidden email]> wrote:
>
> Hi,
>
> I found this code for sorting an array :
>
> p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
>       .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

You're missing that String#[] when given a Regexp returns the substring that matches that Regexp.

[1] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/]}
=> ["1020", "950", "50"]
[2] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/]}.sort
=> ["1020", "50", "950"]
[3] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/]}.sort.reverse
=> ["950", "50", "1020"]
[4] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/].to_i}.sort.reverse
=> [1020, 950, 50]
[5] pry(main)> ["AB_1020", "AB_950", "AB_50",].sort_by {|x| x[/\d+/].to_i}
=> ["AB_50", "AB_950", "AB_1020"]

-Rob

> ouptut
>
> #⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]
>
>
> I know that .partition{|x| x.to_i.zero? } will create an array of arrays :
> [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]]
>
>
> But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

flat_map: https://apidock.com/ruby/Enumerable/flat_map

Then that passes a block, which acts on each member of the Enumerable that called flat_map. That block sorts by the numerical value of x, then reverses.

Does that help?

Walter

>
> I could understand it.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f6fa4cc4-37b2-4828-ba1c-f9ac0d780221%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F584BA16-90B8-40DD-8C04-A92CBF336241%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can you plz, explain to me part of this code ?

botp
In reply to this post by Abdel Latif

On Fri, Apr 27, 2018 at 12:12 AM, Abdel Latif <[hidden email]> wrote:
 
But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}



​two things look suspicious:
1) /d+/​
​2) reverse

have you tried randomly rearranging your arrays first?​

​m​
any thanks,

--botp

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAwHHQica6sEZ-OPOnbGUyG_MGOPKW21iOd%3D_HxacnSmeTeRCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can you plz, explain to me part of this code ?

Abdel Latif
In reply to this post by Rob Biedenharn-3
Thank you Rob, I appreciate your help, good explanation.

On Thu, Apr 26, 2018 at 5:02 PM, Rob Biedenharn <[hidden email]> wrote:


On 2018-Apr-26, at 13:47 , Abdel Latif <[hidden email]> wrote:

But [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]] has non numerical values too.

On Thursday, April 26, 2018 at 1:05:53 PM UTC-4, Walter Lee Davis wrote:

> On Apr 26, 2018, at 12:12 PM, Abdel Latif <[hidden email]> wrote:
>
> Hi,
>
> I found this code for sorting an array :
>
> p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
>       .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

You're missing that String#[] when given a Regexp returns the substring that matches that Regexp.

[1] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/]}
=> ["1020", "950", "50"]
[2] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/]}.sort
=> ["1020", "50", "950"]
[3] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/]}.sort.reverse
=> ["950", "50", "1020"]
[4] pry(main)> ["AB_1020", "AB_950", "AB_50",].map {|x| x[/\d+/].to_i}.sort.reverse
=> [1020, 950, 50]
[5] pry(main)> ["AB_1020", "AB_950", "AB_50",].sort_by {|x| x[/\d+/].to_i}
=> ["AB_50", "AB_950", "AB_1020"]

-Rob

> ouptut
>
> #⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]
>
>
> I know that .partition{|x| x.to_i.zero? } will create an array of arrays :
> [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]]
>
>
> But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

flat_map: https://apidock.com/ruby/Enumerable/flat_map

Then that passes a block, which acts on each member of the Enumerable that called flat_map. That block sorts by the numerical value of x, then reverses.

Does that help?

Walter

>
> I could understand it.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f6fa4cc4-37b2-4828-ba1c-f9ac0d780221%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/C3hSRwet4tY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F584BA16-90B8-40DD-8C04-A92CBF336241%40gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPzaUfPOPEdR-WOqX%3D_ugk%2BjB1BDRtgv3i20z1Xfji9LiDKuUw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.