Skip to main content
deleted 6 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25

I'd say thatI think the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions, instead of doing a imperative processing from scratch, algorithms aretend to be more clear. 

In this case, it's a pity Ruby hasn't gotwe can use the abstraction Enumerable#map_by (a group_by variation where you can control both the grouping function and the accumulated values), then you could and write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea, every single time you have to work with one of those values, you need to check whether it's an array or not, not nice. Here it's better just to use always an array.

I'd say that the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control both the grouping function and the accumulated values), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea, every single time you have to work with one of those values, you need to check whether it's an array or not, not nice. Here it's better just to use always an array.

I think the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions, instead of doing a imperative processing from scratch, algorithms tend to be more clear. 

In this case, we can use the abstraction Enumerable#map_by (a group_by variation where you can control both the grouping function and the accumulated values) and write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have.

added 26 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25

I'd say that the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control what you want to accumulateboth the grouping function and the accumulated values), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea, every single time you have to work with one of those values, you need to check whether it's an array or not, not nice. Here it's better just to use always an array.

I'd say that the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control what you want to accumulate), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea, every single time you have to work with one of those values, you need to check whether it's an array or not, not nice.

I'd say that the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control both the grouping function and the accumulated values), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea, every single time you have to work with one of those values, you need to check whether it's an array or not, not nice. Here it's better just to use always an array.

added 44 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25

I'd say that the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control what you want to accumulate), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returnreturns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea:, every single time you have to work with a valueone of those values, you need to check whether it's an array or not, not nice.

I'd say the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control what to accumulate), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this always return an array as value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea: every time you have to work with a value, you need to check whether it's an array or not, not nice.

I'd say that the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions instead of doing a imperative processing from scratch, algorithms are more clear. In this case, it's a pity Ruby hasn't got Enumerable#map_by (a group_by variation where you can control what you want to accumulate), then you could write:

require 'facets'

class Array
  def merge_hashes
    flat_map(&:to_a).map_by { |k, v| [k, v] }
  end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have. Having mixed types in data structures is usually a bad idea, every single time you have to work with one of those values, you need to check whether it's an array or not, not nice.

added 8 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25
Loading
added 241 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25
Loading
added 42 characters in body
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25
Loading
Source Link
tokland
  • 11.1k
  • 1
  • 20
  • 25
Loading