Word wheel: Difference between revisions
Content added Content deleted
m (→{{header|JavaScript}}: Updated primitives, tidied.) |
|||
Line 1,595: | Line 1,595: | ||
{{Works with|JXA}} |
{{Works with|JXA}} |
||
<lang JavaScript>(() => { |
<lang JavaScript>(() => { |
||
"use strict"; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// gridWords :: [String] -> [String] -> [String] |
// gridWords :: [String] -> [String] -> [String] |
||
Line 1,609: | Line 1,603: | ||
lexemes => { |
lexemes => { |
||
const |
const |
||
wheel = sort(toLower |
wheel = sort(toLower(grid.join(""))), |
||
wSet = new Set(wheel), |
wSet = new Set(wheel), |
||
mid = wheel[4]; |
mid = wheel[4]; |
||
return lexemes.filter(w => { |
return lexemes.filter(w => { |
||
const cs = chars(w); |
const cs = chars(w); |
||
return 2 < cs.length && cs.every( |
return 2 < cs.length && cs.every( |
||
c => wSet.has(c) |
c => wSet.has(c) |
||
Line 1,621: | Line 1,617: | ||
}); |
}); |
||
}; |
}; |
||
// wheelFit :: [Char] -> [Char] -> Bool |
// wheelFit :: [Char] -> [Char] -> Bool |
||
Line 1,632: | Line 1,629: | ||
go(ws.slice(1), cs.slice(1)) |
go(ws.slice(1), cs.slice(1)) |
||
) : go(ws.slice(1), cs); |
) : go(ws.slice(1), cs); |
||
return go(wheel, sort(word)); |
return go(wheel, sort(word)); |
||
}; |
}; |
||
⚫ | |||
// ---------------------- TEST ----------------------- |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// ---------------- GENERIC FUNCTIONS ---------------- |
|||
// chars :: String -> [Char] |
// chars :: String -> [Char] |
||
const chars = s => |
const chars = s => [...s]; |
||
⚫ | |||
// concat :: [[a]] -> [a] |
|||
// concat :: [String] -> String |
|||
const concat = xs => ( |
|||
ys => 0 < ys.length ? ( |
|||
ys.every(Array.isArray) ? ( |
|||
[] |
|||
) : '' |
|||
).concat(...ys) : ys |
|||
)(list(xs)); |
|||
// elem :: Eq a => a -> [a] -> Bool |
// elem :: Eq a => a -> [a] -> Bool |
||
Line 1,655: | Line 1,654: | ||
// True if xs contains an instance of x. |
// True if xs contains an instance of x. |
||
xs => xs.some(y => x === y); |
xs => xs.some(y => x === y); |
||
// lines :: String -> [String] |
// lines :: String -> [String] |
||
const lines = s => |
const lines = s => |
||
// A list of strings derived from a single |
// 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(/ |
s.split(/\r\n|\n|\r/u) |
||
) : []; |
) : []; |
||
// list :: StringOrArrayLike b => b -> [a] |
|||
const list = xs => |
|||
// xs itself, if it is an Array, |
|||
// or an Array derived from xs. |
|||
Array.isArray(xs) ? ( |
|||
xs |
|||
) : Array.from(xs || []); |
|||
// readFile :: FilePath -> IO String |
// readFile :: FilePath -> IO String |
||
const readFile = fp => { |
const readFile = fp => { |
||
// The contents of a text file at the |
// The contents of a text file at the |
||
// |
// given file path. |
||
const |
const |
||
e = $(), |
e = $(), |
||
Line 1,684: | Line 1,677: | ||
e |
e |
||
); |
); |
||
return ObjC.unwrap( |
return ObjC.unwrap( |
||
ns.isNil() ? ( |
ns.isNil() ? ( |
||
Line 1,690: | Line 1,684: | ||
); |
); |
||
}; |
}; |
||
// sort :: Ord a => [a] -> [a] |
// sort :: Ord a => [a] -> [a] |
||
const sort = xs => |
const sort = xs => |
||
. |
Array.from(xs).slice() |
||
⚫ | |||
// toLower :: String -> String |
// toLower :: String -> String |
||
Line 1,699: | Line 1,696: | ||
// Lower-case version of string. |
// Lower-case version of string. |
||
s.toLocaleLowerCase(); |
s.toLocaleLowerCase(); |
||
// unlines :: [String] -> String |
// unlines :: [String] -> String |
||
Line 1,704: | Line 1,702: | ||
// A single string formed by the intercalation |
// A single string formed by the intercalation |
||
// of a list of strings with the newline character. |
// of a list of strings with the newline character. |
||
xs.join( |
xs.join("\n"); |
||
// MAIN --- |
// MAIN --- |