SlideShare uma empresa Scribd logo
1 de 32
Gr             ou
 pi             ng
Ruby Method of the Month
Kevin Munc - @muncman
in_groups

in_groups_of
r
                    o s t
                  p n
                p o
          e e u si
             S n
      t
    c Exiv t
 A e                 y
                   a g
        r
      o :Ar i    r n
 : :C      :         p
               r o u
          : :G
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html
Arrays into Groups
Follow Along in IRB!


• You’re going to need to use ActiveSupport:

  •require ‘active_support’
    • Note: require ‘activesupport’ (no underscore) is deprecated
       for removal in Rails 3.
A Colorful Array

seq = %w{turquoise ultramarine chartreuse
lavender crimson burgundy coral teal magenta
maroon cerulean fuchsia emerald taupe auburn
vermillion}

seq.size
=> 16
in_groups
seq.in_groups(4)
=>
[
["turquoise", "ultramarine", "chartreuse",
"lavender"],
["crimson", "burgundy", "coral", "teal"],
["magenta", "maroon", "cerulean", "fuchsia"],
["emerald", "taupe", "auburn", "vermillion"]
]
in_groups_of
seq.in_groups_of(4)
=>
[
["turquoise", "ultramarine", "chartreuse",
"lavender"],
["crimson", "burgundy", "coral", "teal"],
["magenta", "maroon", "cerulean", "fuchsia"],
["emerald", "taupe", "auburn", "vermillion"]
]
in_groups
seq.in_groups(3)
=>
[
["turquoise", "ultramarine", "chartreuse",
"lavender", "crimson", "burgundy"],
["coral", "teal", "magenta", "maroon", "cerulean",
nil],
["fuchsia", "emerald", "taupe", "auburn",
"vermillion", nil]
]
in_groups_of
seq.in_groups_of(3)
=>
[
["turquoise", "ultramarine", "chartreuse"],
["lavender", "crimson", "burgundy"],
["coral", "teal", "magenta"],
["maroon", "cerulean", "fuchsia"],
["emerald", "taupe", "auburn"],
["vermillion", nil, nil]
]
in_groups with false
seq.in_groups(3, false)
=>
[
["turquoise", "ultramarine", "chartreuse",
"lavender", "crimson", "burgundy"],
["coral", "teal", "magenta", "maroon", "cerulean"],
["fuchsia", "emerald", "taupe", "auburn",
"vermillion"]
]
in_groups_of with false
seq.in_groups_of(3, false)
=>
[
["turquoise", "ultramarine", "chartreuse"],
["lavender", "crimson", "burgundy"],
["coral", "teal", "magenta"],
["maroon", "cerulean", "fuchsia"],
["emerald", "taupe", "auburn"],
["vermillion"]
]
in_groups with filler
seq.in_groups(3, 'white')
=>
[
["turquoise", "ultramarine", "chartreuse",
"lavender", "crimson", "burgundy"],
["coral", "teal", "magenta", "maroon", "cerulean",
"white"],
["fuchsia", "emerald", "taupe", "auburn",
"vermillion", "white"]
]
in_groups_of with filler
seq.in_groups_of(3, 'white')
=>
[
["turquoise", "ultramarine", "chartreuse"],
["lavender", "crimson", "burgundy"],
["coral", "teal", "magenta"],
["maroon", "cerulean", "fuchsia"],
["emerald", "taupe", "auburn"],
["vermillion", "white", "white"]
]
Warning:
The next two slides
       have
   Information
     Overload
Another Look:
                      in_groups
gen = []; 20.times { |i| gen << i+1 }; gen
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]


gen.in_groups(4) # into four groups
=> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

gen.in_groups(3) # into three groups
=> [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, nil]]

gen.in_groups(3, false) # into three groups, unpadded
=> [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20]]

gen.in_groups(3, 99) # into three groups, with filler
=> [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 99]]
Another Look:
                     in_groups_of
gen = []; 20.times { |i| gen << i+1 }; gen
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]


gen.in_groups_of(4) # into groups of four
=> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]

gen.in_groups_of(3) # into groups of three
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, nil]]

gen.in_groups_of(3, false) # into groups of three, unpadded
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20]]

gen.in_groups_of(3, 99) # into groups of three, with filler
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 99]]
Bonus Method!
split

• Divide an array based on:
  • delimiter
  • or block
  • but not both
    • a block is used if present
• This is the third and final method in
  ActiveSupport::CoreExtensions::Array::
  Grouping
split it. split it good!

a_ray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a_ray.split(5)
=> [[1, 2, 3, 4], [6, 7, 8, 9, 10]]
split it. split it good!

a_ray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a_ray.split(5)
=> [[1, 2, 3, 4], [6, 7, 8, 9, 10]]

b_ray = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
b_ray.split(3)
=> [[1, 2], [4, 5, 1, 2], [4, 5]]
split it like it’s hot
a_ray
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a_ray.split { |elem| elem % 2 == 0 }
=> [[1], [3], [5], [7], [9], []]
split it like it’s hot
a_ray
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a_ray.split { |elem| elem % 2 == 0 }
=> [[1], [3], [5], [7], [9], []]

a_ray
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a_ray.split { |elem| elem % 2 == 1 }
=> [[], [2], [4], [6], [8], [10]]
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]

      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]

      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]
            3
      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]
            3 4
      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]
            3 4 5
      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]
            3 4 5           3
      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]
            3 4 5           3 4
      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
splitting hairs
b_ray
=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]                                                  Matches are
                                                                                excluded, starting a
                                                                                 new array group.
b_ray.split do |elem|
 (3..5).include? elem
end

=> [[1, 2], [], [], [1, 2], [], [], []]
            3 4 5           3 4 5
      # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90
  90:         def split(value = nil)
  91:           using_block = block_given?
  92:
  93:           inject([[]]) do |results, element|
  94:             if (using_block && yield(element)) || (value == element)
  95:               results << []
  96:             else
  97:               results.last << element
  98:             end
  99:
 100:             results
Questions?


               Image Credits
        all from clickykbd@flickr
http://www.flickr.com/photos/clickykbd

Mais conteúdo relacionado

Mais procurados (7)

An Introduction to Data Mining with R
An Introduction to Data Mining with RAn Introduction to Data Mining with R
An Introduction to Data Mining with R
 
Python idioms
Python idiomsPython idioms
Python idioms
 
Session 02
Session 02Session 02
Session 02
 
Aimsstrand1po6gr3expandednotation
Aimsstrand1po6gr3expandednotationAimsstrand1po6gr3expandednotation
Aimsstrand1po6gr3expandednotation
 
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
 
Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)
 
Data types
Data typesData types
Data types
 

Destaque

Lookin Out - Program Overview
Lookin Out - Program OverviewLookin Out - Program Overview
Lookin Out - Program Overview
mbresee
 

Destaque (10)

Milieuproblematiek
MilieuproblematiekMilieuproblematiek
Milieuproblematiek
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)
 
Lookin Out - Program Overview
Lookin Out - Program OverviewLookin Out - Program Overview
Lookin Out - Program Overview
 
Shellwords
ShellwordsShellwords
Shellwords
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
 
Lean manufacturing &amp; lean supply chain awareness workshop
Lean manufacturing &amp; lean supply chain awareness workshopLean manufacturing &amp; lean supply chain awareness workshop
Lean manufacturing &amp; lean supply chain awareness workshop
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & Infinities
 
Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013
 

Semelhante a Grouping (MOTM 2010.02)

R is a very flexible and powerful programming language, as well as a.pdf
R is a very flexible and powerful programming language, as well as a.pdfR is a very flexible and powerful programming language, as well as a.pdf
R is a very flexible and powerful programming language, as well as a.pdf
annikasarees
 
Lec13 Clustering.pptx
Lec13 Clustering.pptxLec13 Clustering.pptx
Lec13 Clustering.pptx
Khalid Rabayah
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Activesupport - Leckerbissen (Kurzvortrag)
Activesupport - Leckerbissen (Kurzvortrag)Activesupport - Leckerbissen (Kurzvortrag)
Activesupport - Leckerbissen (Kurzvortrag)
Robert Gogolok
 
Unit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive propertyUnit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive property
mlabuski
 
Unit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive propertyUnit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive property
mlabuski
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)
Ohgyun Ahn
 
Ruby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と HashRuby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と Hash
higaki
 

Semelhante a Grouping (MOTM 2010.02) (20)

Ruby's Arrays and Hashes with examples
Ruby's Arrays and Hashes with examplesRuby's Arrays and Hashes with examples
Ruby's Arrays and Hashes with examples
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
 
K means cluster ML
K means cluster  MLK means cluster  ML
K means cluster ML
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLP
 
R is a very flexible and powerful programming language, as well as a.pdf
R is a very flexible and powerful programming language, as well as a.pdfR is a very flexible and powerful programming language, as well as a.pdf
R is a very flexible and powerful programming language, as well as a.pdf
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Lec13 Clustering.pptx
Lec13 Clustering.pptxLec13 Clustering.pptx
Lec13 Clustering.pptx
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
Activesupport - Leckerbissen (Kurzvortrag)
Activesupport - Leckerbissen (Kurzvortrag)Activesupport - Leckerbissen (Kurzvortrag)
Activesupport - Leckerbissen (Kurzvortrag)
 
Unit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive propertyUnit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive property
 
Unit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive propertyUnit 4 lesson 5 distributive property
Unit 4 lesson 5 distributive property
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
019# mean, median, mode
019# mean, median, mode019# mean, median, mode
019# mean, median, mode
 
Inside Enumerable
Inside EnumerableInside Enumerable
Inside Enumerable
 
Ruby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と HashRuby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と Hash
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Poodr ch8-composition
Poodr ch8-compositionPoodr ch8-composition
Poodr ch8-composition
 

Mais de Kevin Munc (8)

Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setInterval
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Grouping (MOTM 2010.02)

  • 1. Gr ou pi ng Ruby Method of the Month Kevin Munc - @muncman
  • 3. r o s t p n p o e e u si S n t c Exiv t A e y a g r o :Ar i r n : :C : p r o u : :G http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html
  • 5. Follow Along in IRB! • You’re going to need to use ActiveSupport: •require ‘active_support’ • Note: require ‘activesupport’ (no underscore) is deprecated for removal in Rails 3.
  • 6. A Colorful Array seq = %w{turquoise ultramarine chartreuse lavender crimson burgundy coral teal magenta maroon cerulean fuchsia emerald taupe auburn vermillion} seq.size => 16
  • 7. in_groups seq.in_groups(4) => [ ["turquoise", "ultramarine", "chartreuse", "lavender"], ["crimson", "burgundy", "coral", "teal"], ["magenta", "maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn", "vermillion"] ]
  • 8. in_groups_of seq.in_groups_of(4) => [ ["turquoise", "ultramarine", "chartreuse", "lavender"], ["crimson", "burgundy", "coral", "teal"], ["magenta", "maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn", "vermillion"] ]
  • 9. in_groups seq.in_groups(3) => [ ["turquoise", "ultramarine", "chartreuse", "lavender", "crimson", "burgundy"], ["coral", "teal", "magenta", "maroon", "cerulean", nil], ["fuchsia", "emerald", "taupe", "auburn", "vermillion", nil] ]
  • 10. in_groups_of seq.in_groups_of(3) => [ ["turquoise", "ultramarine", "chartreuse"], ["lavender", "crimson", "burgundy"], ["coral", "teal", "magenta"], ["maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn"], ["vermillion", nil, nil] ]
  • 11. in_groups with false seq.in_groups(3, false) => [ ["turquoise", "ultramarine", "chartreuse", "lavender", "crimson", "burgundy"], ["coral", "teal", "magenta", "maroon", "cerulean"], ["fuchsia", "emerald", "taupe", "auburn", "vermillion"] ]
  • 12. in_groups_of with false seq.in_groups_of(3, false) => [ ["turquoise", "ultramarine", "chartreuse"], ["lavender", "crimson", "burgundy"], ["coral", "teal", "magenta"], ["maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn"], ["vermillion"] ]
  • 13. in_groups with filler seq.in_groups(3, 'white') => [ ["turquoise", "ultramarine", "chartreuse", "lavender", "crimson", "burgundy"], ["coral", "teal", "magenta", "maroon", "cerulean", "white"], ["fuchsia", "emerald", "taupe", "auburn", "vermillion", "white"] ]
  • 14. in_groups_of with filler seq.in_groups_of(3, 'white') => [ ["turquoise", "ultramarine", "chartreuse"], ["lavender", "crimson", "burgundy"], ["coral", "teal", "magenta"], ["maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn"], ["vermillion", "white", "white"] ]
  • 15. Warning: The next two slides have Information Overload
  • 16. Another Look: in_groups gen = []; 20.times { |i| gen << i+1 }; gen => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] gen.in_groups(4) # into four groups => [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]] gen.in_groups(3) # into three groups => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, nil]] gen.in_groups(3, false) # into three groups, unpadded => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20]] gen.in_groups(3, 99) # into three groups, with filler => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 99]]
  • 17. Another Look: in_groups_of gen = []; 20.times { |i| gen << i+1 }; gen => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] gen.in_groups_of(4) # into groups of four => [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]] gen.in_groups_of(3) # into groups of three => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, nil]] gen.in_groups_of(3, false) # into groups of three, unpadded => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20]] gen.in_groups_of(3, 99) # into groups of three, with filler => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 99]]
  • 19. split • Divide an array based on: • delimiter • or block • but not both • a block is used if present • This is the third and final method in ActiveSupport::CoreExtensions::Array:: Grouping
  • 20. split it. split it good! a_ray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split(5) => [[1, 2, 3, 4], [6, 7, 8, 9, 10]]
  • 21. split it. split it good! a_ray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split(5) => [[1, 2, 3, 4], [6, 7, 8, 9, 10]] b_ray = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] b_ray.split(3) => [[1, 2], [4, 5, 1, 2], [4, 5]]
  • 22. split it like it’s hot a_ray => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split { |elem| elem % 2 == 0 } => [[1], [3], [5], [7], [9], []]
  • 23. split it like it’s hot a_ray => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split { |elem| elem % 2 == 0 } => [[1], [3], [5], [7], [9], []] a_ray => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split { |elem| elem % 2 == 1 } => [[], [2], [4], [6], [8], [10]]
  • 24. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 25. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 26. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 27. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 28. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 29. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 3 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 30. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 3 4 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 31. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 3 4 5 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
  • 32. Questions? Image Credits all from clickykbd@flickr http://www.flickr.com/photos/clickykbd

Notas do Editor