Non-decimal radices/Convert: Difference between revisions
→{{header|AppleScript}}: Updated primitives
m (→{{header|jq}}: typo) |
(→{{header|AppleScript}}: Updated primitives) |
||
Line 289:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
For more flexibility with digit variants (upper and lower case hex, digits in other languages/scripts etc) we can define '''toBase'''(intBase, n) in terms of a more general '''inBaseDigits'''(strDigits, n) which derives the base from the number of digits to be used:
<lang AppleScript>-- toBase :: Int -> Int -> String
Line 300 ⟶ 298:
end if
end toBase
-- inBaseDigits :: String -> Int -> [String]
Line 307 ⟶ 304:
script nextDigit
on
set {divided, remainder} to quotRem(residue, intBase)
{nothing:true}
▲ {valid:divided > 0, value:(item (remainder + 1) of strDigits), new:divided}
▲ end lambda
end script
Line 317 ⟶ 318:
end inBaseDigits
-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits -------------------------------▼
▲-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits
-- inUpperHex :: Int -> String
Line 331 ⟶ 330:
end inDevanagariDecimal
-- TEST ----------------------------------------------------------------------
on run
script
on
{{binary:toBase(2, x), octal:toBase(8, x), hex:toBase(16, x)}, ¬
{upperHex:inUpperHex(x), dgDecimal:inDevanagariDecimal(x)}}
end
end script
Line 344 ⟶ 343:
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
on unfoldr(f, v)
set mf to mReturn(f)▼
set lst to {}
set recM to
set recM to
if not nothing of recM then set end of lst to just of recM
end repeat▼
lst▼
end unfoldr
Line 369:
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
Line 382:
else
script
property
end script
end if
end mReturn</lang>
▲ set v to lambda(v)
▲ end repeat
▲ end tell
{{Out}}
<lang AppleScript>{{{binary:"11111111", octal:"377", hex:"ff"}, {upperHex:"FF", dgDecimal:"२५५"}},
|