Skip to content

Instantly share code, notes, and snippets.

@jewel12
Created July 11, 2012 18:20
Show Gist options
  • Save jewel12/3092147 to your computer and use it in GitHub Desktop.
Save jewel12/3092147 to your computer and use it in GitHub Desktop.
二次元配列内の数値を合計する
// total とリスト内の全ての数値に与えられた演算処理を行い、total を逐次更新する
// リスト内の要素が全て Number であることが前提
List op_inject := method(op_str,
Number inject_op := Number getSlot(op_str)
total := 0
call target foreach(v, total = total inject_op(v))
return total)
// ネストしているリストにも対応
List sum := method(
call target map( v,
if(v proto != List,
v,
v sum)) op_inject("+"))
l := list(1,2,3,4,5)
l sum println //==> 15
l := list(list(1,2,3), list(4,5,6), list(7,8,9))
l sum println //==> 45
l := list(list(1,2,3), list(4,5,6), list(7,8,9, list(10, 11)))
l sum println //==> 66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment