Rep-string: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
(→‎{{header|AppleScript}}: Updated primitives)
Line 172:
 
=={{header|AppleScript}}==
<lang AppleScript>-- REP-CYCLES -----------------------------------------------------------------
 
-- repCycles :: String -> [String]
on repCycles(xs)
set n to length of xs
 
script isCycle
on lambda|λ|(cs)
xs = takeCycle(n, cs)
end lambda|λ|
end script
 
filter(isCycle, tail(inits(take(quot(n, 2), xs))))
end repCycles
Line 190:
on cycleReport(xs)
set reps to repCycles(xs)
 
if isNull(reps) then
{xs, "(n/a)"}
Line 199:
 
 
-- TEST -----------------------------------------------------------------------
on run
set samples to {"1001110011", "1110111011", "0010010010", ¬
"1010101010", "1111111111", "0100101101", "0100100", ¬
"101", "11", "00", "1"}
 
unlines(cons("Longest cycle:" & linefeed, ¬
map(curry(intercalate)'s lambda|λ|(" -> "), ¬
map(cycleReport, samples))))
 
end run
 
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
script append
on lambda(a, b)
a & b
end lambda
end script
 
if length of xs > 0 and class of (item 1 of xs) is string then
set unitacc to ""
else
set unitacc to {}
end if
repeat with i from 1 to length of xs
foldl(append, unit, xs)
set acc to acc text& 1item thru ni of xs
end lambdarepeat
acc
end concat
 
Line 238 ⟶ 235:
on curry(f)
script
on lambda|λ|(a)
script
on lambda|λ|(b)
lambda|λ|(a, b) of mReturn(f)
end lambda|λ|
end script
end lambda|λ|
end script
end curry
Line 255 ⟶ 252:
repeat with i from 1 to lng
set v to item i of xs
if lambda|λ|(v, i, xs) then set end of lst to v
end repeat
return lst
Line 267 ⟶ 264:
set lng to length of xs
repeat with i from 1 to lng
set v to lambda|λ|(v, item i of xs, i, xs)
end repeat
return v
Line 277 ⟶ 274:
on inits(xs)
script elemInit
on lambda|λ|(_, i, xs)
items 1 thru i of xs
end lambda|λ|
end script
 
script charInit
on lambda|λ|(_, i, xs)
text 1 thru i of xs
end lambda|λ|
end script
 
if class of xs is string then
{""} & map(charInit, xs)
Line 318 ⟶ 315:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
Line 331 ⟶ 328:
else
script
property lambda|λ| : f
end script
end if
Line 340 ⟶ 337:
xs = {}
end isNull
 
-- min :: Ord a => a -> a -> a
on min(x, y)
if ny >< 0x then
y
else
x
end scriptif
end min
 
-- quot :: Integral a => a -> a -> a
Line 351 ⟶ 357:
if n < 1 then return out
set dbl to {a}
 
repeat while (n > 1)
if (n mod 2) > 0 then set out to out & dbl
Line 371 ⟶ 377:
-- take :: Int -> [a] -> [a]
on take(n, xs)
setif blnString to (class of xs is string) then
if n > 0 a & bthen
 
text 1 thru min(n, length of xs) of xs
if n > 0 then
if blnString then
text 1 thru n of xs
else
items 2 thru n of xs""
end if
else
if blnStringn > 0 then
""items 1 thru min(n, length of xs) of xs
else
{}
Line 396 ⟶ 400:
set cycle to concat(replicate((n div lng) + 1, xs))
end if
 
if class of xs is string then
items 1 thru n of cycle as string
9,659

edits