-
-
Save 3limin4t0r/8d90fda851e43dc80b03353f9542e4f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
months = ["2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"] | |
monthly_doc_count_for_topic = [ | |
["foo","2019-02-01: 186904","2019-03-01: 196961"], | |
["bar","2019-01-01: 8876","2019-04-01: 8694"] | |
] | |
monthly_doc_count_for_topic.map { |array| array + months } | |
#=> [ | |
# ["foo", "2019-02-01: 186904", "2019-03-01: 196961", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"], | |
# ["bar", "2019-01-01: 8876", "2019-04-01: 8694", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"] | |
# ] | |
# +monthly_doc_count_for_topic+ should be unchanged, since we | |
# only used non-mutating methods. | |
# Your solution also works, but mutates each array inside | |
# +monthly_doc_count_for_topic+. I've replaced #map with #each | |
# here. Which only effects the return value, which we aren't | |
# using here. | |
monthly_doc_count_for_topic.each do |topic_set| | |
months.each { |month| topic_set << month } | |
end | |
monthly_doc_count_for_topic | |
#=> [ | |
# ["foo", "2019-02-01: 186904", "2019-03-01: 196961", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"], | |
# ["bar", "2019-01-01: 8876", "2019-04-01: 8694", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"] | |
# ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment