Skip to content

Commit 33095a0

Browse files
Add nested sequences, func applications
Add a references section as well with links to any relevant discussions, documents.
1 parent a5590c4 commit 33095a0

File tree

1 file changed

+96
-8
lines changed

1 file changed

+96
-8
lines changed

coding-style.rst

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ out the rest, the whole library still keeps compiling.
6060
Line Length
6161
-----------
6262

63-
Maximum 80 columns. This is very helpful when seeing diffs side by side.
63+
Maximum 80 columns. This is very helpful when seeing diffs side by side.
64+
65+
Below, we have listed styles for single line and multi line cases. The
66+
single line case is used when the whole construct fits into a single
67+
line and multi line when it becomes longer than 80 columns and needs to
68+
be broken into more than one line. In some cases, single line is meant
69+
for a nested construct rather than for the outer one.
6470

6571
Indentation
6672
-----------
@@ -122,14 +128,15 @@ Single line::
122128

123129
import Control.Concurrent (killThread, myThreadId, takeMVar, threadDelay)
124130

125-
Multi line::
131+
Multi line, list style to avoid rearrangement when adding new items::
126132

127133
import Control.Exception
128-
(assert, Exception, SomeException, AsyncException, fromException, mask_)
129-
130-
import Prelude hiding
131-
(map, mapM, mapM_, repeat, foldr, last, take, filter, takeWhile, drop
132-
, concatMap, replicate, enumFromTo, concat, reverse, iterate, splitAt
134+
( assert
135+
, Exception
136+
, SomeException
137+
, AsyncException
138+
, fromException
139+
, mask_
133140
)
134141

135142
Delineating Sections
@@ -217,6 +224,34 @@ Multi line::
217224
, Three
218225
)
219226

227+
Nested::
228+
229+
list =
230+
[ Group1
231+
[ One
232+
, Two
233+
, Three
234+
]
235+
, Group2
236+
[ One
237+
, Two
238+
, Three
239+
]
240+
]
241+
242+
tuple =
243+
(
244+
( One
245+
, Two
246+
, Three
247+
)
248+
,
249+
( One
250+
, Two
251+
, Three
252+
)
253+
)
254+
220255
Expressions
221256
-----------
222257

@@ -324,7 +359,8 @@ Cascading ::
324359
else v
325360

326361
Its preferable to not mix single line and multi-line formats, but sometimes you
327-
can, use your judgement.
362+
can, especially the first or last line could be in a single line format even if
363+
the rest are in multiline format.
328364

329365
Variable Naming
330366
---------------
@@ -566,12 +602,30 @@ Function Application & Composition
566602

567603
Single line::
568604

605+
y = f (g (h x))
569606
y = f $ g $ h x
570607
y = h x & g & f
571608
k = f . g . h
572609

573610
Multi line::
574611

612+
lookup e m =
613+
foldrM
614+
(\(a, b) xs -> if e == a then return (Just b) else xs)
615+
(return Nothing)
616+
m
617+
618+
func =
619+
S.drain
620+
(encodeLatin1Lax
621+
(S.concatUnfold A.read
622+
(S.concatMapWith parallel use
623+
(S.unfold TCP.acceptOnPort 8090
624+
)
625+
)
626+
)
627+
)
628+
575629
func =
576630
( S.drain
577631
$ encodeLatin1Lax
@@ -580,6 +634,14 @@ Multi line::
580634
$ S.unfold TCP.acceptOnPort 8090
581635
)
582636

637+
func =
638+
( S.drain
639+
. encodeLatin1Lax
640+
. S.concatUnfold A.read
641+
. S.concatMapWith parallel use
642+
. S.unfold TCP.acceptOnPort
643+
) 8090
644+
583645
Multi line in `do` block::
584646

585647
func = do
@@ -591,6 +653,27 @@ Multi line in `do` block::
591653
& S.drain
592654
)
593655

656+
Multi line with lambdas, the last application could be a multi line expr::
657+
658+
return $ Skip $
659+
if done
660+
then (FromSVarDone sv)
661+
else (FromSVarRead sv)
662+
663+
f x =
664+
g $ h $ \y -> do
665+
putStrLn "hello "
666+
return y
667+
668+
-- alternatively it can be formatted like a sequence
669+
f x =
670+
( g
671+
$ h
672+
$ \y -> do
673+
putStrLn "hello "
674+
return y
675+
)
676+
594677
Haddock
595678
-------
596679

@@ -621,3 +704,8 @@ Haddock
621704
fromAddr# :: Int -> Addr# -> IO (Array Word8)
622705
fromAddr# n addr# = do
623706

707+
References
708+
----------
709+
710+
* https://www.joachim-breitner.de/blog/739-Avoid_the_dilemma_of_the_trailing_comma
711+
* https://stackoverflow.com/questions/10483635/why-do-lots-of-programmers-move-commas-to-the-next-line

0 commit comments

Comments
 (0)