0
\$\begingroup\$

This is a homework exercise and therefore has no further purpose. The writing to me feels clunky, and the typescript types on addParenthesisFactory are unreadable, but it is the type of the function. Can anything be done?

For those unfamiliar with ramda, ramda.pipe(...functions) simply pipes the input through the given functions, and ramda.split(str) splits a string as expected.

import * as R from "ramda";
const stringToArray = R.split("");
const addParenthesisFactory: (paren1: string, paren2: string) => (acc:number, curr: string) => number = (paren1, paren2) =>
    (acc, curr) =>  acc < 0 ? acc:
            curr === paren1 ? acc + 1 :
            curr === paren2 ? acc - 1 : 
            acc

const addParenthesis1 = addParenthesisFactory("(",")")
const addParenthesis2 = addParenthesisFactory("{","}")
const addParenthesis3 = addParenthesisFactory("[","]")
         
const parenthesisChecker: (s: string[]) => boolean =
    (s) => s.reduce(addParenthesis1, 0) === 0 && 
    s.reduce(addParenthesis2, 0) === 0 && 
    s.reduce(addParenthesis3, 0) === 0 

export const isPaired = (l: string): boolean =>
    R.pipe( stringToArray, parenthesisChecker) (l)


\$\endgroup\$
1
  • 1
    \$\begingroup\$ What problem is this code supposed to solve? Can you provide some sample inputs and outputs? Thanks. \$\endgroup\$
    – ggorlen
    Commented Apr 18 at 15:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.