Created
October 19, 2012 10:29
-
-
Save VienosNotes/3917406 to your computer and use it in GitHub Desktop.
Sum of Number List, with capable of nesting in AppleScript
This file contains hidden or 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
| 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