Change e letters to i in words: Difference between revisions
Content added Content deleted
(→{{header|JavaScript}}: Added a version in JavaScript) |
|||
Line 1,094: | Line 1,094: | ||
("vector","victor") |
("vector","victor") |
||
("welles","willis")</pre> |
("welles","willis")</pre> |
||
=={{header|JavaScript}}== |
|||
{{Works with| macOS}} |
|||
ECMAScript defines no file access interface. |
|||
Here we use the ObjC interface of macOS ''JavaScript for Automation'' to define a '''readFile''' function. |
|||
<lang javascript>(() => { |
|||
"use strict"; |
|||
// ieTwins :: String -> [(String, String)] |
|||
const ieTwins = s => { |
|||
const |
|||
shortWords = s.split("\n") |
|||
.filter(x => 5 < x.length), |
|||
lexicon = new Set( |
|||
shortWords.filter(w => w.includes("i")) |
|||
), |
|||
rgx = /e/gu; |
|||
return shortWords.flatMap( |
|||
w => w.includes("e") ? (() => { |
|||
const x = w.replace(rgx, "i"); |
|||
return lexicon.has(x) ? [ |
|||
[w, x] |
|||
] : []; |
|||
})() : [] |
|||
); |
|||
}; |
|||
// ---------------------- TEST ----------------------- |
|||
// main :: IO () |
|||
const main = () => { |
|||
const s = readFile("unixdict.txt"); |
|||
return ieTwins(s) |
|||
.map(JSON.stringify) |
|||
.join("\n"); |
|||
}; |
|||
// --------------------- GENERIC --------------------- |
|||
// readFile :: FilePath -> IO String |
|||
const readFile = fp => { |
|||
// The contents of a text file at the |
|||
// filepath fp. |
|||
const |
|||
e = $(), |
|||
ns = $.NSString |
|||
.stringWithContentsOfFileEncodingError( |
|||
$(fp).stringByStandardizingPath, |
|||
$.NSUTF8StringEncoding, |
|||
e |
|||
); |
|||
return ObjC.unwrap( |
|||
ns.isNil() ? ( |
|||
e.localizedDescription |
|||
) : ns |
|||
); |
|||
}; |
|||
// MAIN --- |
|||
return main(); |
|||
})();</lang> |
|||
{{Out}} |
|||
<pre>["analyses","analysis"] |
|||
["atlantes","atlantis"] |
|||
["bellow","billow"] |
|||
["breton","briton"] |
|||
["clench","clinch"] |
|||
["convect","convict"] |
|||
["crises","crisis"] |
|||
["diagnoses","diagnosis"] |
|||
["enfant","infant"] |
|||
["enquiry","inquiry"] |
|||
["frances","francis"] |
|||
["galatea","galatia"] |
|||
["harden","hardin"] |
|||
["heckman","hickman"] |
|||
["inequity","iniquity"] |
|||
["inflect","inflict"] |
|||
["jacobean","jacobian"] |
|||
["marten","martin"] |
|||
["module","moduli"] |
|||
["pegging","pigging"] |
|||
["psychoses","psychosis"] |
|||
["rabbet","rabbit"] |
|||
["sterling","stirling"] |
|||
["synopses","synopsis"] |
|||
["vector","victor"] |
|||
["welles","willis"]</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |