Determine if a string has all unique characters: Difference between revisions
Content added Content deleted
(→{{header|JavaScript}}: Updated alternative to grouping and sorting) |
m (→{{header|JavaScript}}: Updated output of group and sort example, used sortOn in lieu of sortBy) |
||
Line 569: | Line 569: | ||
duplicates = filter(g => 1 < g.length)( |
duplicates = filter(g => 1 < g.length)( |
||
groupBy(on(eq)(snd))( |
groupBy(on(eq)(snd))( |
||
sortOn(snd)( |
|||
zip(enumFrom(0))(chars(s)) |
zip(enumFrom(0))(chars(s)) |
||
) |
) |
||
Line 576: | Line 576: | ||
return 0 < duplicates.length ? Just( |
return 0 < duplicates.length ? Just( |
||
fanArrow(compose(snd, fst))(map(fst))( |
fanArrow(compose(snd, fst))(map(fst))( |
||
sortOn(compose(fst, fst))( |
|||
duplicates |
duplicates |
||
)[0] |
)[0] |
||
Line 587: | Line 587: | ||
console.log( |
console.log( |
||
fTable('First duplicated character, if any:')( |
fTable('First duplicated character, if any:')( |
||
s => `'${s}'` |
s => `'${s}' (${s.length})` |
||
)(maybe('None')(tpl => { |
)(maybe('None')(tpl => { |
||
const [c, ixs] = Array.from(tpl); |
const [c, ixs] = Array.from(tpl); |
||
Line 623: | Line 623: | ||
// chars :: String -> [Char] |
// chars :: String -> [Char] |
||
const chars = s => s.split(''); |
const chars = s => s.split(''); |
||
// comparing :: (a -> b) -> (a -> a -> Ordering) |
|||
const comparing = f => |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
}; |
|||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
||
Line 730: | Line 721: | ||
const snd = tpl => tpl[1]; |
const snd = tpl => tpl[1]; |
||
// |
// sortOn :: Ord b => (a -> b) -> [a] -> [a] |
||
const |
const sortOn = f => |
||
// Equivalent to sortBy(comparing(f)), but with f(x) |
|||
xs.slice() |
|||
// evaluated only once for each x in xs. |
|||
.sort(uncurry(f)); |
|||
// ('Schwartzian' decorate-sort-undecorate). |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// take :: Int -> [a] -> [a] |
// take :: Int -> [a] -> [a] |
||
Line 775: | Line 772: | ||
{{Out}} |
{{Out}} |
||
<pre>First duplicated character, if any: |
<pre>First duplicated character, if any: |
||
'' -> None |
'' (0) -> None |
||
'.' -> None |
'.' (1) -> None |
||
'abcABC' -> None |
'abcABC' (6) -> None |
||
'XYZ ZYX' -> 'X' (0x58) at 0, 6 |
'XYZ ZYX' (7) -> 'X' (0x58) at 0, 6 |
||
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' -> '0' (0x30) at 9, 24</pre> |
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' (36) -> '0' (0x30) at 9, 24</pre> |
||