Spiral matrix: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied, added main.) |
m (→{{header|AppleScript}}: Tidied) |
||
Line 430: | Line 430: | ||
{{Trans|JavaScript}} (ES6) |
{{Trans|JavaScript}} (ES6) |
||
<lang AppleScript>-- |
<lang AppleScript>---------------------- SPIRAL MATRIX --------------------- |
||
-- spiral :: Int -> [[Int]] |
|||
on spiral(n) |
on spiral(n) |
||
script go |
script go |
||
Line 437: | Line 439: | ||
{enumFromTo(start, start + pred(cols))} & ¬ |
{enumFromTo(start, start + pred(cols))} & ¬ |
||
map(my |reverse|, ¬ |
map(my |reverse|, ¬ |
||
transpose(|λ|(cols, pred(rows), start + cols))) |
|||
else |
else |
||
{{}} |
{{}} |
||
Line 447: | Line 449: | ||
end spiral |
end spiral |
||
--------------------------- TEST ------------------------- |
|||
on run |
on run |
||
⚫ | |||
wikiTable(spiral(5), ¬ |
wikiTable(spiral(5), ¬ |
||
false, ¬ |
false, ¬ |
||
Line 455: | Line 457: | ||
end run |
end run |
||
⚫ | |||
⚫ | |||
-- wikiTable :: [Text] -> Bool -> Text -> Text |
-- wikiTable :: [Text] -> Bool -> Text -> Text |
||
Line 474: | Line 477: | ||
end wikiTable |
end wikiTable |
||
⚫ | |||
⚫ | |||
-- comparing :: (a -> b) -> (a -> a -> Ordering) |
-- comparing :: (a -> b) -> (a -> a -> Ordering) |
||
Line 494: | Line 498: | ||
end script |
end script |
||
end comparing |
end comparing |
||
-- concatMap :: (a -> [b]) -> [a] -> [b] |
-- concatMap :: (a -> [b]) -> [a] -> [b] |
||
on concatMap(f, xs) |
on concatMap(f, xs) |
||
set lng to length of xs |
set lng to length of xs |
||
⚫ | |||
⚫ | |||
set acc to "" |
|||
else |
|||
⚫ | |||
⚫ | |||
tell mReturn(f) |
tell mReturn(f) |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set acc to acc & |λ|(item i of xs, i, xs) |
set acc to acc & (|λ|(item i of xs, i, xs)) |
||
end repeat |
end repeat |
||
end tell |
end tell |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end concatMap |
end concatMap |
||
-- enumFromTo :: Int -> Int -> [Int] |
-- enumFromTo :: Int -> Int -> [Int] |
||
Line 523: | Line 529: | ||
end if |
end if |
||
end enumFromTo |
end enumFromTo |
||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
||
Line 535: | Line 542: | ||
end tell |
end tell |
||
end foldl |
end foldl |
||
-- if_ :: Bool -> a -> a -> a |
-- if_ :: Bool -> a -> a -> a |
||
Line 544: | Line 552: | ||
end if |
end if |
||
end if_ |
end if_ |
||
-- intercalateS :: String -> [String] -> String |
-- intercalateS :: String -> [String] -> String |
||
Line 552: | Line 561: | ||
return s |
return s |
||
end intercalateS |
end intercalateS |
||
-- length :: [a] -> Int |
-- length :: [a] -> Int |
||
Line 557: | Line 567: | ||
length of xs |
length of xs |
||
end |length| |
end |length| |
||
-- max :: Ord a => a -> a -> a |
-- max :: Ord a => a -> a -> a |
||
Line 566: | Line 577: | ||
end if |
end if |
||
end max |
end max |
||
-- maximumBy :: (a -> a -> Ordering) -> [a] -> a |
-- maximumBy :: (a -> a -> Ordering) -> [a] -> a |
||
Line 582: | Line 594: | ||
foldl(max, missing value, xs) |
foldl(max, missing value, xs) |
||
end maximumBy |
end maximumBy |
||
-- Lift 2nd class handler function into 1st class script wrapper |
-- Lift 2nd class handler function into 1st class script wrapper |
||
Line 594: | Line 607: | ||
end if |
end if |
||
end mReturn |
end mReturn |
||
-- map :: (a -> b) -> [a] -> [b] |
-- map :: (a -> b) -> [a] -> [b] |
||
Line 606: | Line 620: | ||
end tell |
end tell |
||
end map |
end map |
||
-- pred :: Enum a => a -> a |
-- pred :: Enum a => a -> a |
||
on pred(x) |
on pred(x) |
||
x - 1 |
|||
end pred |
end pred |
||
-- Egyptian multiplication - progressively doubling a list, appending |
-- Egyptian multiplication - progressively doubling a list, appending |
||
Line 639: | Line 655: | ||
end |reverse| |
end |reverse| |
||
-- If some of the rows are shorter than the following rows, |
|||
-- Simplified version - assuming rows of unvarying length. |
|||
-- their elements are skipped: |
|||
-- transpose({{10,11},{20},{},{30,31,32}}) -> {{10, 20, 30}, {11, 31}, {32}} |
|||
-- transpose :: [[a]] -> [[a]] |
-- transpose :: [[a]] -> [[a]] |
||
on transpose( |
on transpose(rows) |
||
set intMax to |length|(maximumBy(comparing(my |length|), xxs)) |
|||
set gaps to replicate(intMax, {}) |
|||
script padded |
|||
on |λ|(xs) |
|||
set lng to |length|(xs) |
|||
if lng < intMax then |
|||
xs & items (lng + 1) thru -1 of gaps |
|||
else |
|||
xs |
|||
end if |
|||
end |λ| |
|||
end script |
|||
set rows to map(padded, xxs) |
|||
script cols |
script cols |
||
on |λ|(_, iCol) |
on |λ|(_, iCol) |
||
Line 670: | Line 671: | ||
map(cols, item 1 of rows) |
map(cols, item 1 of rows) |
||
end transpose |
end transpose |
||
-- unlines :: [String] -> String |
-- unlines :: [String] -> String |
||
Line 679: | Line 681: | ||
str |
str |
||
end unlines |
end unlines |
||
-- unwords :: [String] -> String |
-- unwords :: [String] -> String |