RPG attributes generator: Difference between revisions
→Functional JS: Updated in translation of the Python version
m (→Python: Functional composition: (Edited type comments)) |
(→Functional JS: Updated in translation of the Python version) |
||
Line 459:
===Functional===
{{Trans|Python}} (Functional composition version)
<lang javascript>(() => {
'use strict';
// main :: IO ()
const main = () =>
// 10 random heroes drawn from
// a non-finite series.
unlines(map(
take(10, heroes(
seventyFivePlusWithTwo15s
));
//
const seventyFivePlusWithTwo15s = xs =>
// with two or more qualities scoring 15.
x => 15 === x, xs
));
function* heroes(p) {
// Non-finite list of heroes matching
// the requirements of predicate p.
while (true) {
yield hero(p)
}
}
// hero :: (Int -> Bool) -> IO (Int, Int, Int, Int, Int, Int)
const hero = p =>
// A random character matching the
// requirements of predicate p.
until(p, character, []);
// character :: () -> [Int]
const character = () =>
// A random character with six
// integral attributes.
map(() => sum(tail(sort(map(
randomRInt(1, 6),
enumFromTo(1, 4)
)))),
enumFromTo(1, 6)
);
// GENERIC FUNCTIONS ----------------------------------
// enumFromTo :: (Int, Int) -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
// Returns Infinity over objects without finite length.
// This enables zip and zipWith to choose the shorter
// argument when one is non-finite, like cycle, repeat etc
//
const
(Array.isArray(xs) || 'string' === typeof xs) ? (
//
const
(Array.isArray(xs) ? (
xs
) : xs.split('')).map(f);
// e.g. map(randomRInt(1, 10), enumFromTo(1, 20))
//
const
low + Math.floor(
(Math.random() * ((high - low) + 1))
);
//
const
//
const
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
//
const
//
const
//
// take :: Int -> String -> String
const take = (n, xs) =>
'GeneratorFunction' !== xs.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}));
//
const
//
const
let v = x;
while (!p(v)) v = f(v);
return v;
};
// MAIN ---
return main();
})();</lang>
{{Out}}
A sample of 10 character attribute sets:
<pre>79 -> [
92 -> [
82 -> [
84 -> [
83 -> [
81 -> [
79 -> [
76 -> [
=={{header|Julia}}==
|