1
\$\begingroup\$

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)"
"my.coordinate"
\$\endgroup\$
2
  • 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\$ Commented Jan 26, 2015 at 3:04
  • \$\begingroup\$ I find indentChar = liftA2 (||) isAlpha (=='(') more readable. \$\endgroup\$ Commented Jan 30, 2015 at 20:52

1 Answer 1

2
\$\begingroup\$

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)

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.