Вопрос по ruby – Ruby: проблема с внедрением при превращении массива в хеш

12
a = [[1, 'a'],[2, 'b'],[3, 'c'], [4, 'd']]
a.inject({}) {|r, val| r[val[0]] = val[1]}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

a.inject({}) {|r, val| r[val[0]] = val[1]; r}

Это тогда работает.

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded KChaloux

Ваш Ответ

4   ответа
2

a.inject({}) { |r, val| r.merge({ val[0] => val[1] }) }
9

a = [[1, 'a'],[2, 'b'],[3, 'c'], [4, 'd']]
b = Hash[a] # {1=>"a", 2=>"b", 3=>"c", 4=>"d"}

rr{}.

Error: User Rate Limit ExceededisError: User Rate Limit Exceeded
13

Enumerable#injectreduce

reduce :: [a] → (a → a → a) → a

Enumerable[A]#inject {|A, A| A } → A

Enumerable

Enumerable#inject withfold

fold :: [b] → a → (a → b → a) → a

Enumerable[B]#inject(A) {|A, B| A } → A

different

Enumerable#inject

the type of the accumulator and the return type of the block must be the same when not passing an explicit accumulator, the type of the accumulator is the same as the element type

acc[key] = value

acc.tap { acc[key] = value }

a.inject({}) {|r, (key, value)| r[key] = value; r }
Error: User Rate Limit Exceeded That Umbrella Guy
Error: User Rate Limit ExceededinjectError: User Rate Limit Exceededfold / reduceError: User Rate Limit ExceedednotError: User Rate Limit ExceededinjectError: User Rate Limit ExceededfoldError: User Rate Limit Exceeded
4

Error: User Rate Limit ExceededinjectError: User Rate Limit Exceeded

Error: User Rate Limit ExceededinjectError: User Rate Limit Exceededeach_with_object instead.

Error: User Rate Limit Exceededa.inject({}) { |r, (k, v)| r.merge(k => v) }Error: User Rate Limit Exceededmerge!Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы