Re:What Makes Code Hard to Read: Visual Patterns of Complexi...
by yongjik on Tuesday March 11, 2025 @04:49PM
(#43336866)
Maybe it's because I'm not familiar with such style, but I don't like how the code hides operational details. That is, if `books` contains one billion books, and the final result should contain about a hundred authors, how much extra memory does this use for intermediate results?
[ Parent
| on HN ]
Re:What Makes Code Hard to Read: Visual Patterns of Complexi...
by mrkeen on Tuesday March 11, 2025 @05:50PM
(#43337439)
The best way to kick the tyres on this kind of question is to plug in something literally infinite. That way if you arrive at an answer you're probably doing something right with regard to space and time usage.
For example, use all the prime numbers as an expression in your chain.
import Data.Function
import Data.Numbers.Primes
main = do
let result :: [Int] = primes
& filter (startingWithDigit '5')
& asPairs
& map pairSum
& drop 100000
& take 10
print result
asPairs xs = zip xs (tail xs)
pairSum (a, b) = a + b
startingWithDigit d x = d == head (show x)
> [100960734,100960764,100960792,100960800,100960812]> 3 MiB total memory in use (0 MB lost due to fragmentation)
Re:What Makes Code Hard to Read: Visual Patterns of Complexi...
by yxhuvud on Tuesday March 11, 2025 @04:58PM
(#43336931)
This is a valid concern I also reacted a little bit on. One thing to note though is that it is often possible to tell such chains to be lazy and only collect the end result at the end without ever generating any intermediary arrays.
Which require the author to actually have an idea how big the numbers are, but that is very often the case regardless of how you write your code.