Chinese zodiac: Difference between revisions
m
→JavaScript ES6: Tidied, updated output.
No edit summary |
m (→JavaScript ES6: Tidied, updated output.) |
||
Line 2,169:
{{Trans|Haskell}}
<lang JavaScript>(() => {
//
//
const
// 天干 tiangan – 10 heavenly stems
zip(
chars("甲乙丙丁戊己庚辛壬癸")
);
//
const
// 地支 dizhi – 12 terrestrial branches
chars("子丑寅卯辰巳午未申酉戌亥")
)(
"zĭ chŏu yín măo chén sì " + (
"wŭ wèi shēn yŏu xū hài"
)
// aws :: Array Int (String, String, String)
const aws =
)
words("mù huǒ tǔ jīn shuǐ")
)(
words("wood fire earth metal water")
);
// axs :: Array Int (String, String, String)
const axs =
zip3(
chars("鼠牛虎兔龍蛇馬羊猴鸡狗豬")
)
words(
"shǔ niú hǔ tù lóng shé " + (
"mǎ yáng hóu jī gǒu zhū"
)
)
)(
words(
"rat ox tiger rabbit dragon snake " + (
"horse goat monkey rooster dog pig"
)
)
);
// ays :: Array Int (String, String)
const ays =
chars("阳阴")
)(
words("yáng yīn")
);
//
const zodiac = y => {
const
Line 2,267 ⟶ 2,245:
iStem = iYear % 10,
iBranch = iYear % 12,
[hStem, pStem] = ats()[iStem],
[hBranch, pBranch] = ads()[iBranch],
[hElem, pElem, eElem] = aws()[quot(iStem
[hAnimal, pAnimal, eAnimal] = axs()[iBranch],
[hYinyang, pYinyang] = ays()[iYear % 2];
return [
[
],
["", pStem + pBranch, pElem, pAnimal, pYinyang],
[
"", `${show((iYear % 60) + 1)}/60`,
eElem, eAnimal, ""
]
];
};
//
const main = () => [
1935, 1938, 1968, 1972, 1976, 1984,
new Date().getFullYear()
]
.map(showYear)
.join("\n\n");
// ------------------- FORMATTING --------------------
// fieldWidths :: [[Int]]
const fieldWidths = [
Line 2,289 ⟶ 2,283:
// showYear :: Int -> String
const showYear = y =>
.map(
([n, s]) => justifyLeft(n)(" ")(s)
)
.join("")
)
.join("\n");
// ---------------- GENERIC FUNCTIONS ----------------
// chars :: String
const chars = s => [...s];
// justifyLeft :: Int -> Char -> String -> String
const justifyLeft = n =>
// The string s, followed by enough padding (with
// the character c) to reach the string length n.
c => s => n > s.length ? (
s.padEnd(n, c)
) : s;
// quot :: Integral a => a -> a -> a
const quot = n =>
m => Math.trunc(n / m);
// show :: Int -> a -> Indented String
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [
x[1], null, x[0]
] : x
);
// words :: String -> [String]
const words = s =>
// List of space-delimited sub-strings.
s.split(/\s+/u);
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
// zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
const zip3 = xs =>
ys => zs => xs.slice(
0,
Math.min(...[xs, ys, zs].map(x => x.length))
)
.map((x, i) => [x, ys[i], zs[i]]);
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = f =>
// A list constructed by zipping with a
// custom function, rather than with the
// default tuple constructor.
xs => ys => xs.map(
(x, i) => f(x)(ys[i])
).slice(
0, Math.min(xs.length, ys.length)
);
// MAIN ---
return main();
})();</lang>
{{Out}}
Line 2,329 ⟶ 2,387:
1/60 wood rat
=={{header|Julia}}==
|