Hacker News new | ask | show | jobs
by vladev 4747 days ago
This is very dense code, and takes some getting-used-to in order to read. What it achieves is beyond the reach of many languages.

All it does is convert a Seq (sequence) to a lazy stream that will infinitely cycle through the values.

    Seq(1, 2, 3).cycle.take(8).toList
    res3: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2)
It will work for any seq-like structure (including List. Vector, Queue, etc.)

If you try to use it with anything else, like a Map, it won't compile.

Also, note from my example that it's generic and the resulting List is of the correct type.

Just like novice JavaScript developers struggle with "Why 'this' changes here?", it requires some experience with the language.

2 comments

> This is very dense code, and takes some getting-used-to in order to read.

I don't think so. It is full of syntactic noise, for what is really just notation for building a cyclic structure.

* type annotations stated explicitly, not inferred

* type variables given multiple times

* too much non-verb, non-noun syntax e.g. `#:::`

* and the actual construction of the cycle is stated imperatively with great ceremony (despite it being an applicative)

It is the opposite of dense.

Brainfuck language is even denser.