Levenshtein distance: Difference between revisions
→JS ES6: Tidied, updated primitives.
(Add bruijn) |
(→JS ES6: Tidied, updated primitives.) |
||
Line 2,737:
By composition of generic functions:
<syntaxhighlight lang="javascript">(() => {
// ------------ LEVENSHTEIN EDIT DISTANCE ------------
// levenshtein :: String -> String -> Int
const levenshtein = sa =>
const go = (ns,
const calc = z
x + (c !== c1 ? 1 : 0)▼
: 1
)▼
};
▲ const [n, ns1] = [ns[0], ns.slice(1)];
return
);
};
return last(▼
chars(sb).reduce(▼
▲ go,
▲ enumFromTo(0)(cs.length)
▲ )
);▼
};▼
//
const main = () => [
["kitten", "sitting"],
Line 2,773 ⟶ 2,785:
//
b => ({▼
▲ '0': a,
▲ '1': b,
▲ n = args.length;
▲ [i]: x
▲ }), {
▲ length: n
▲ })
) : args.reduce((f, x) => f(x), Tuple);▼
// enumFromTo :: Int -> Int -> [Int]
Line 2,814 ⟶ 2,795:
// last :: [a] -> a
const last = xs =>
// The last item of a list.
▲ };
▲ ys.slice(1)
const length = xs =>▼
// scanl :: (b -> a -> b) -> b -> [a] -> [b]
const scanl = f
// The series of interim values arising
// from a catamorphism.
▲ return Tuple(v)(a[1].concat(v));
const v = f(a[0])(x);
}, [startValue, [startValue]]
Line 2,860 ⟶ 2,823:
// A function over a pair, derived
// from a curried function.
const
: args
▲ ) : args;
return f(
};
Line 2,872 ⟶ 2,835:
// zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
const zip3 = xs =>
ys => zs => xs.slice(
// MAIN ---
return JSON.stringify(main());
})();</syntaxhighlight>
{{Out}}
|