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
xs = takeCycle(n, cs)
end
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
map(cycleReport, samples))))
end run
-- GENERIC FUNCTIONS
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
a & b▼
end lambda▼
end script▼
if length of xs > 0 and class of (item 1 of xs) is string then
set
else
set
end if
repeat with i from 1 to length of xs
acc
end concat
Line 238 ⟶ 235:
on curry(f)
script
on
script
on
end
end script
end
end script
end curry
Line 255 ⟶ 252:
repeat with i from 1 to lng
set v to item i of xs
if
end repeat
return lst
Line 267 ⟶ 264:
set lng to length of xs
repeat with i from 1 to lng
set v to
end repeat
return v
Line 277 ⟶ 274:
on inits(xs)
script elemInit
on
items 1 thru i of xs
end
end script
script charInit
on
text 1 thru i of xs
end
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
end repeat
return lst
Line 331 ⟶ 328:
else
script
property
end script
end if
Line 340 ⟶ 337:
xs = {}
end isNull
-- min :: Ord a => a -> a -> a
on min(x, y)
y
else
x
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)
text 1 thru min(n, length of xs) of xs
▲ if n > 0 then
▲ text 1 thru n of xs
else
end if
else
if
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
|