Skip to content

Instantly share code, notes, and snippets.

@SrPeixinho
Last active August 29, 2015 14:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SrPeixinho/3c34f66fd86f12af165f to your computer and use it in GitHub Desktop.
Save SrPeixinho/3c34f66fd86f12af165f to your computer and use it in GitHub Desktop.
Syntax considered harmful
-- When your code grows too big for a line:
separateParens = foldr (\ head tail -> if head == '(' then head : ' ' : tail else if head == ')' then ' ' : head : tail else head : tail) []
-- Your natural instinct is to add new lines and comments:
separateParens =
foldr (\ head tail ->
if head == '('
then head : ' ' : tail -- inserts space after left parens
else if head == ')'
then ' ' : head : tail -- inserts space before right parens
else head : tail)
[]
-- Or even use special syntax:
separateParens = foldr detacher [] where
detacher '(' tail = '(' : ' ' : tail -- inserts space after left parens
detacher ')' tail = ' ' : ')' : tail -- inserts space before left parens
detacher head tail = head : tail
-- But, when you use the proper abstractions...
insertAfter el ins = concatMap (\x -> if x == el then [x,ins] else [x])
insertBefore el ins = concatMap (\x -> if x == el then [ins,x] else [x])
-- No comments, no formatting, no special syntax:
separateParens = insertAfter '(' ' ' . insertBefore ')' ' '
-- One line is all you ever need.
-- .
-- .
-- .
-- This is just a particular example, but I've challenged myself to write every
-- function in single line since a month ago and I'm yet to see one occasion where
-- this didn't work. Indeed, so far it only lead to more readable code and better
-- abstractions. After this experience, I'd argue needing comments/lines/syntax is
-- a good indicative something isn't properly abstracted. After all, if functional
-- programming is so inherently powerful, why would it need to be supplmented with
-- so many special features? Think about it, and give it a try!
-- ~SrPx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment