Magic squares of doubly even order: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
(Added Julia language)
(→‎{{header|AppleScript}}: Updated primitives)
Line 116:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
<lang AppleScript>-- magicSquareMAGIC ::SQUARE IntOF ->DOUBLY [[Int]]EVEN ORDER -----------------------------------------
 
-- magicSquare :: Int -> [[Int]]
on magicSquare(n)
if n mod 4 > 0 then
Line 134 ⟶ 136:
script scale
on lambda|λ|(x)
replicate(n / 4, x)
end lambda|λ|
end script
set truthSeries to ¬
flatten(scale's lambda|λ|(map(scale, splitEvery(4, magicSeries(4)))))
end if
set limit to sqr + 1
script inOrderOrReversed
on lambda|λ|(x, i)
cond(x, i, limit - i)
end lambda|λ|
end script
Line 155 ⟶ 157:
end if
end magicSquare
 
 
-- magicSeries :: Int -> [Bool]
on magicSeries(n)
script boolToggle
on lambda|λ|(x)
not x
end lambda|λ|
end script
Line 174 ⟶ 175:
 
 
-- TEST ---------------------------------------------------------------------------
on run
Line 193 ⟶ 194:
on wikiTable(lstRows, blnHdr, strStyle)
script fWikiRows
on lambda|λ|(lstRow, iRow)
set strDelim to cond(blnHdr and (iRow = 0), "!", "|")
set strDbl to strDelim & strDelim
linefeed & "|-" & linefeed & strDelim & space & ¬
intercalate(space & strDbl & space, lstRow)
end lambda|λ|
end script
Line 208 ⟶ 209:
 
 
-- GENERIC FUNCTIONS ---------------------------------------------------------------------------
 
-- splitEvery :: Int -> [a] -> [[a]]
on splitEvery(n, xs)
if length of xs ≤ n then
{xs}
else
set {gp, t} to splitAt(n, xs)
{gp} & splitEvery(n, t)
end if
end splitEvery
 
-- isPowerOf :: Int -> Int -> Bool
on isPowerOf(k, n)
set v to k
script remLeft
on lambda(x)
x mod v is not 0
end lambda
end script
script integerDiv
on lambda(x)
x div v
end lambda
end script
|until|(remLeft, integerDiv, n) = 1
end isPowerOf
 
-- asPowerOfTwo :: Int -> maybe Int
Line 248 ⟶ 221:
end asPowerOfTwo
 
-- mapconcatMap :: (a -> [b]) -> [a] -> [b]
on mapconcatMap(f, xs)
tellscript mReturn(f)append
seton lng|λ|(a, to length of xsb)
set lst to {} a & b
repeatend with i from 1 to lng|λ|
end script
set end of lst to lambda(item i of xs, i, xs)
end repeat
foldl(append, {}, map(f, xs))
return lst
end tellconcatMap
end map
 
-- cond :: Bool -> a -> a -> a
Line 277 ⟶ 249:
end if
end flatten
 
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
script append
on lambda(a, b)
a & b
end lambda
end script
foldl(append, {}, map(f, xs))
end concatMap
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 295 ⟶ 256:
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 301 ⟶ 262:
end foldl
 
-- untilintercalate :: (aText -> Bool)[Text] -> (a -> a) -> a -> aText
on |until|intercalate(pstrText, f, xlstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set mp to mReturn(p)
set vstrJoined to xlstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
 
-- isPowerOf :: Int -> Int -> Bool
on isPowerOf(k, n)
set v to k
script remLeft
on |λ|(x)
x mod v is not 0
end |λ|
end script
script integerDiv
on |λ|(x)
x div v
end |λ|
end script
|until|(remLeft, integerDiv, n) = 1
end isPowerOf
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
repeatset untillng mp'sto lambda(v)length of xs
set vlst to lambda(v){}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
return v
 
end |until|
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 321 ⟶ 308:
else
script
property lambda|λ| : f
end script
end if
Line 361 ⟶ 348:
end splitAt
 
-- intercalatesplitEvery :: TextInt -> [Texta] -> Text[[a]]
on intercalatesplitEvery(strTextn, lstTextxs)
if length of xs ≤ n then
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text{xs}
else
set my text item delimiters to dlm
set {gp, t} to splitAt(n, xs)
return strJoined
{gp} & splitEvery(n, t)
end intercalate</lang>
end if
end splitEvery
 
-- 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 |λ|(v)
set v to |λ|(v)
end repeat
end tell
return v
end |until|</lang>
{{Out}}
magic(8)
9,659

edits