Skip to content

Instantly share code, notes, and snippets.

@VienosNotes
Created October 19, 2012 10:29
Show Gist options
  • Select an option

  • Save VienosNotes/3917406 to your computer and use it in GitHub Desktop.

Select an option

Save VienosNotes/3917406 to your computer and use it in GitHub Desktop.
Sum of Number List, with capable of nesting in AppleScript
on run
sum({1, 2, 3, {1, 2}})
end run
on sum(arg)
-- 数値リストの総和を求める
-- リストが含まれている場合は再帰的に中身も合計する
if arg is equal to {} then
set result to 0
else if class of arg's first item is equal to list then
-- 先頭の要素がリストなら、そちらにもsumを適用
set result to sum(arg's first item) + sum(arg's rest)
else
try
set result to (arg's first item as number) + sum(arg's rest)
on error
--数値に変換できない要素は無視
set result to sum(arg's rest)
end try
end if
end sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment