ABC words: Difference between revisions

Content added Content deleted
m (→‎{{header|JavaScript}}: Added a solution in JS)
Line 1,766: Line 1,766:
25: pummel 26: supremum
25: pummel 26: supremum
</pre>
</pre>

=={{header|JavaScript}}==
<lang javascript>(() => {
"use strict";

// -------------------- ABC WORDS --------------------

// isABC :: String -> Bool
const isABC = s =>
// True if the string contains each of 'a' 'b' 'c',
// and their first occurrences in the string are
// in that alphabetical order.
bind(
bind(
residue("a")("bc")(s)
)(
residue("b")("c")
)
)(
r => r.includes("c") || null
) !== null;


// residue :: Char -> String -> String -> Maybe String
const residue = c =>
// Any characters remaining in a given string
// after the first occurrence of c, or null
// if c is not found, or is preceded by any
// excluded characters.
excluded => {
const go = t =>
(0 < t.length) ? (() => {
const x = t[0];

return excluded.includes(x) ? (
null
) : c === x ? (
t.slice(1)
) : go(t.slice(1));
})() : null;

return go;
};


// ---------------------- TEST -----------------------
const main = () =>
lines(readFile("~/unixdict.txt"))
.filter(isABC)
.map((x, i) => `(${1 + i}, ${x})`)
.join("\n");


// --------------------- GENERIC ---------------------

// bind (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
const bind = mb =>
// Null if mb is null, or the application of the
// (a -> Maybe b) function mf to the contents of mb.
mf => null === mb ? (
mb
) : mf(mb);


// lines :: String -> [String]
const lines = s =>
// A list of strings derived from a single string
// which is delimited by \n or by \r\n or \r.
Boolean(s.length) ? (
s.split(/\r\n|\n|\r/u)
) : [];


// readFile :: FilePath -> IO String
const readFile = fp => {
// The contents of a text file at the
// given file path.
const
e = $(),
ns = $.NSString
.stringWithContentsOfFileEncodingError(
$(fp).stringByStandardizingPath,
$.NSUTF8StringEncoding,
e
);

return ObjC.unwrap(
ns.isNil() ? (
e.localizedDescription
) : ns
);
};


// MAIN ---
return main();
})();</lang>
{{Out}}
<pre>(1, aback)
(2, abacus)
(3, abc)
(4, abdicate)
(5, abduct)
(6, abeyance)
(7, abject)
(8, abreact)
(9, abscess)
(10, abscissa)
(11, abscissae)
(12, absence)
(13, abstract)
(14, abstracter)
(15, abstractor)
(16, adiabatic)
(17, aerobacter)
(18, aerobic)
(19, albacore)
(20, alberich)
(21, albrecht)
(22, algebraic)
(23, alphabetic)
(24, ambiance)
(25, ambuscade)
(26, aminobenzoic)
(27, anaerobic)
(28, arabic)
(29, athabascan)
(30, auerbach)
(31, diabetic)
(32, diabolic)
(33, drawback)
(34, fabric)
(35, fabricate)
(36, flashback)
(37, halfback)
(38, iambic)
(39, lampblack)
(40, leatherback)
(41, metabolic)
(42, nabisco)
(43, paperback)
(44, parabolic)
(45, playback)
(46, prefabricate)
(47, quarterback)
(48, razorback)
(49, roadblock)
(50, sabbatical)
(51, snapback)
(52, strabismic)
(53, syllabic)
(54, tabernacle)
(55, tablecloth)</pre>


=={{header|J}}==
=={{header|J}}==
Line 1,824: Line 1,977:
tabernacle
tabernacle
tablecloth</lang>
tablecloth</lang>

=={{header|jq}}==
=={{header|jq}}==
{{works with|jq}}
{{works with|jq}}