June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -7,25 +7,26 @@ on toBase(intBase, n)
end if
end toBase
-- inBaseDigits :: String -> Int -> [String]
on inBaseDigits(strDigits, n)
set intBase to length of strDigits
script nextDigit
on lambda(residue)
on |λ|(residue)
set {divided, remainder} to quotRem(residue, intBase)
if divided > 0 then
{just:(item (remainder + 1) of strDigits), new:divided, nothing:false}
else
{nothing:true}
end if
{valid:divided > 0, value:(item (remainder + 1) of strDigits), new:divided}
end lambda
end |λ|
end script
reverse of unfoldr(nextDigit, n) as string
end inBaseDigits
-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits
-- OTHER FUNCTIONS DERIVABLE FROM inBaseDigits -------------------------------
-- inUpperHex :: Int -> String
on inUpperHex(n)
@ -37,31 +38,32 @@ on inDevanagariDecimal(n)
inBaseDigits("०१२३४५६७८९", n)
end inDevanagariDecimal
-- TEST
-- TEST ----------------------------------------------------------------------
on run
script
on lambda(x)
on |λ|(x)
{{binary:toBase(2, x), octal:toBase(8, x), hex:toBase(16, x)}, ¬
{upperHex:inUpperHex(x), dgDecimal:inDevanagariDecimal(x)}}
end lambda
end |λ|
end script
map(result, [255, 240])
end run
-- GENERIC FUNCTIONS
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
on unfoldr(f, v)
set mf to mReturn(f)
set lst to {}
set recM to mf's lambda(v)
repeat while (valid of recM) is true
set end of lst to value of recM
set recM to mf's lambda(new of recM)
end repeat
lst & value of recM
set recM to {nothing:false, new:v}
tell mReturn(f)
repeat while (not (nothing of recM))
set recM to |λ|(new of recM)
if not nothing of recM then set end of lst to just of recM
end repeat
end tell
lst
end unfoldr
-- quotRem :: Integral a => a -> a -> (a, a)
@ -75,7 +77,7 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -88,20 +90,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set mp to mReturn(p)
set v to x
tell mReturn(f)
repeat until mp's lambda(v)
set v to lambda(v)
end repeat
end tell
return v
end |until|

View file

@ -0,0 +1,31 @@
#converts a number to a given based represented by a string
to_base := proc(num, based)
local i;
local chart := "0123456789abcdefghijklmnopqrstuvwxyz";
local conversion := ListTools:-Reverse((convert(num,base,based)));
local str := StringTools:-StringBuffer();
for i in conversion do
str:-append(chart[i+1]);
end do;
return str;
end proc:
#find the location of char in chart
find_digit := proc(char)
if (StringTools:-HasAlpha(char)) then
return (StringTools:-Ord(char) - 87);
else
return (StringTools:-Ord(char) - 48);
end if;
end proc:
#converts a string with given base to a number
from_base := proc(str, base)
local char;
local result := 0;
for char in str do
result *= base;
result += find_digit(char);
end do;
return result;
end proc:

View file

@ -0,0 +1,26 @@
int2str <- function(x, b) {
if(x==0) return("0")
if(x<0) return(paste0("-", base(-x,b)))
map <- c(as.character(0:9), letters)
res <- ""
while (x>0) {
res <- c(map[x %% b + 1], res)
x <- x %/% b
}
return(paste(res, collapse=""))
}
str2int <- function(s, b) {
map <- c(as.character(0:9), letters)
s <- strsplit(s,"")[[1]]
res <- sapply(s, function(x) which(map==x))
res <- as.vector((res-1) %*% b^((length(res)-1):0))
return(res)
}
## example: convert 255 to hex (ff):
int2str(255, 16)
## example: convert "1a" in base 16 to integer (26):
str2int("1a", 16)

View file

@ -0,0 +1,32 @@
# Project : Non-decimal radices/Convert
# Date : 2017/10/20
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
see "0 (decimal) -> " + hex(0) + " (base 16)" + nl
see "26 (decimal) -> " + hex(26) + " (base 16)" + nl
see "383 (decimal) -> " + hex(383) + " (base 16)" + nl
see "26 (decimal) -> " + tobase(26, 2) + " (base 2)" + nl
see "383 (decimal) -> " + tobase(383, 2) + " (base 2)" + nl
see "1a (base 16) -> " + dec("1a") + " (decimal)" + nl
see "1A (base 16) -> " + dec("1A") + " (decimal)" + nl
see "17f (base 16) -> " + dec("17f") + " (decimal)" + nl
see "101111111 (base 2) -> " + bintodec("101111111") + " (decimal)" + nl
func tobase(nr, base)
binary = 0
i = 1
while(nr != 0)
remainder = nr % base
nr = floor(nr/base)
binary= binary + (remainder*i)
i = i*10
end
return string(binary)
func bintodec(bin)
binsum = 0
for n=1 to len(bin)
binsum = binsum + number(bin[n]) *pow(2, len(bin)-n)
next
return binsum

View file

@ -0,0 +1,3 @@
def backToBig(num: String, oldBase: Int): BigInt = BigInt(num, oldBase)
def bigToBase(num: BigInt, newBase: Int): String = num.toString(newBase)