Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,107 @@
|
|||
use AppleScript version "2.4"
|
||||
use framework "Foundation"
|
||||
use scripting additions
|
||||
|
||||
-- succString :: Bool -> String -> String
|
||||
on succString(blnPruned, s)
|
||||
script go
|
||||
on |λ|(w)
|
||||
try
|
||||
if w contains "." then
|
||||
set v to w as real
|
||||
else
|
||||
set v to w as integer
|
||||
end if
|
||||
{(1 + v) as string}
|
||||
on error
|
||||
if blnPruned then
|
||||
{}
|
||||
else
|
||||
{w}
|
||||
end if
|
||||
end try
|
||||
end |λ|
|
||||
end script
|
||||
unwords(concatMap(go, |words|(s)))
|
||||
end succString
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
on run
|
||||
script test
|
||||
on |λ|(bln)
|
||||
succString(bln, ¬
|
||||
"41 pine martens in 1491.3 -1.5 mushrooms ≠ 136")
|
||||
end |λ|
|
||||
end script
|
||||
unlines(map(test, {true, false}))
|
||||
end run
|
||||
|
||||
--> 42 1492.3 -0.5 137
|
||||
--> 42 pine martens in 1492.3 -0.5 mushrooms ≠ 137
|
||||
|
||||
-- GENERIC ------------------------------------------------
|
||||
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
set lng to length of xs
|
||||
set acc to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set acc to acc & |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
end tell
|
||||
return acc
|
||||
end concatMap
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end unlines
|
||||
|
||||
-- unwords :: [String] -> String
|
||||
on unwords(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, space}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
return s
|
||||
end unwords
|
||||
|
||||
-- words :: String -> [String]
|
||||
on |words|(s)
|
||||
set ca to current application
|
||||
(((ca's NSString's stringWithString:(s))'s ¬
|
||||
componentsSeparatedByCharactersInSet:(ca's ¬
|
||||
NSCharacterSet's whitespaceAndNewlineCharacterSet()))'s ¬
|
||||
filteredArrayUsingPredicate:(ca's ¬
|
||||
NSPredicate's predicateWithFormat:"0 < length")) as list
|
||||
end |words|
|
||||
|
|
@ -0,0 +1 @@
|
|||
("12345" + 1) as text --> "12346"
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
|
||||
use framework "Foundation"
|
||||
|
||||
-- Using the machine's own locale setting. The numerical text must be compatible with this.
|
||||
-- Params: Numerical text or NSString, AppleScript or Objective-C number or text equivalent.
|
||||
on incrementNumericalString:str byAmount:increment
|
||||
set localeID to current application's class "NSLocale"'s currentLocale()'s localeIdentifier()
|
||||
return my incrementNumericalString:str byAmount:increment localeID:localeID
|
||||
end incrementNumericalString:byAmount:
|
||||
|
||||
-- Including the locale ID as an additional parameter.
|
||||
-- Params: As above plus locale ID (text or NSString).
|
||||
on incrementNumericalString:str byAmount:increment localeID:localeID
|
||||
set |⌘| to current application
|
||||
set str to |⌘|'s class "NSString"'s stringWithString:(str)
|
||||
set locale to |⌘|'s class "NSLocale"'s localeWithLocaleIdentifier:(localeID)
|
||||
set decSeparator to locale's objectForKey:(|⌘|'s NSLocaleDecimalSeparator)
|
||||
set regex to |⌘|'s NSRegularExpressionSearch
|
||||
-- Use an NSNumberFormatter to generate the NSDecimalNumber objects for the math,
|
||||
-- as its number/string conversions are more flexible than NSDecimalNumber's own.
|
||||
tell |⌘|'s class "NSNumberFormatter"'s new()
|
||||
its setGeneratesDecimalNumbers:(true)
|
||||
its setLocale:(locale)
|
||||
set symbolRange to str's rangeOfString:("[Ee]| ?[x*] ?10 ?\\^ ?") options:(regex)
|
||||
if (symbolRange's |length|() > 0) then
|
||||
-- Catered-for exponent symbol in the input string. Set the output style to "scientific".
|
||||
its setNumberStyle:(|⌘|'s NSNumberFormatterScientificStyle)
|
||||
its setExponentSymbol:(str's substringWithRange:(symbolRange))
|
||||
else
|
||||
-- Straight numerical text, with or without separators as per the input and locale.
|
||||
its setNumberStyle:(|⌘|'s NSNumberFormatterDecimalStyle)
|
||||
set groupingSeparator to locale's objectForKey:(|⌘|'s NSLocaleGroupingSeparator)
|
||||
its setUsesGroupingSeparator:(str's containsString:(groupingSeparator))
|
||||
its setMinimumFractionDigits:(str's containsString:(decSeparator))
|
||||
end if
|
||||
-- Derive NSDecimalNumbers from the inputs, add together, convert the result back to NSString.
|
||||
set increment to (|⌘|'s class "NSArray"'s arrayWithArray:({increment}))'s firstObject()
|
||||
if ((increment's isKindOfClass:(|⌘|'s class "NSNumber")) as boolean) then ¬
|
||||
set increment to its stringFromNumber:(increment)
|
||||
set sum to (its numberFromString:(str))'s decimalNumberByAdding:(its numberFromString:(increment))
|
||||
set output to its stringFromNumber:(sum)
|
||||
end tell
|
||||
-- Adjustments for AppleScript norms the NSNumberFormatter may omit from scientific notation output:
|
||||
if (symbolRange's |length|() > 0) then
|
||||
-- If no decimal separator in the output mantissa, insert point zero or not to match the input style.
|
||||
if ((output's containsString:(decSeparator)) as boolean) then
|
||||
else if ((str's containsString:(decSeparator)) as boolean) then
|
||||
set output to output's stringByReplacingOccurrencesOfString:("(?<=^-?\\d)") ¬
|
||||
withString:((decSeparator as text) & "0") options:(regex) range:({0, output's |length|()})
|
||||
end if
|
||||
-- If no sign in an E-notation exponent, insert "+" or not ditto.
|
||||
if (((output's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
|
||||
else if (((str's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
|
||||
set output to output's stringByReplacingOccurrencesOfString:("(?<=[Ee])") ¬
|
||||
withString:("+") options:(regex) range:({0, output's |length|()})
|
||||
end if
|
||||
end if
|
||||
|
||||
return output as text -- Return as AppleScript text.
|
||||
end incrementNumericalString:byAmount:localeID:
|
||||
|
||||
return {¬
|
||||
(my incrementNumericalString:"12345" byAmount:1), ¬
|
||||
(my incrementNumericalString:"999,999,999,999,999" byAmount:5), ¬
|
||||
(my incrementNumericalString:"-1.234" byAmount:10 localeID:"en"), ¬
|
||||
(my incrementNumericalString:"-1,234E+1" byAmount:10 localeID:"fr_FR"), ¬
|
||||
(my incrementNumericalString:"-1.234 x 10^1" byAmount:"10") ¬
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{"12346", "1,000,000,000,000,004", "8.766", "-2,34E+0", "-2.34 x 10^0"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue