How can I improve readability of this code? indentChar needs to be reused elsewhere.

toCoordinate = dropWhile ((||) <$> (not . indentChar) <*> (=='(')) >>> takeWhile(/= ' ')
indentChar = (||) <$> isAlpha <*> (=='(')

Desired result:

*Parse> toCoordinate " \\--(my.coordinate - from outerspace)"
  • 1
    \$\begingroup\$ It would help to know what your input format is supposed to be. It looks pretty arbitrary, does "\\--" actually mean anything or are those completely arbitrary characters? Is the coordinate text, or will it really be comprised of digits? Also, you have mismatched parentheses in the definition of toCoordinate. \$\endgroup\$ – bisserlis Jan 26 '15 at 3:04
  • \$\begingroup\$ I find indentChar = liftA2 (||) isAlpha (=='(') more readable. \$\endgroup\$ – Landei Jan 30 '15 at 20:52

The first thing I observe is that the dropWhile function argument is:

\x -> (not . indentChar) x || '(' == x

The comparison to '(' cancels out that same comparison in indentChar -- so the following definition of toCoordinate is identical in meaning:

toCoordinate = dropWhile (not . isAlpha) >>> takeWhile (/= ' ')

You did state that you need to use indentChar elsewhere -- but not using it here makes your code clearer.

Additionally, rather than use arrows, the above definition seems clearer to me when using function composition -- I would write it as:

toCoordinate = takeWhile (/= ' ') . dropWhile (not . isAlpha)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.