News for hackers. Stuff that matters.
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by BonoboBoner on Thursday June 13, 2013 @05:00AM (#5872965)
Scala is a fantastic language, but my heart seems to resist due to things like this (slide 27/42):

  implicit class RichSeq[A, C[A] <: Seq[A]](underlying: C[A]{
    def cycle: Iterator[A] = {
      lazy val circular: Stream[A] = underlying.toStream #::: circular
      circular.iterator
    }
  }
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by vladev on Thursday June 13, 2013 @05:08AM (#5872993)
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.

Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by dons on Thursday June 13, 2013 @05:55AM (#5873138)
> 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.

Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by Stranger2013 on Thursday June 13, 2013 @05:13AM (#5873005)
Brainfuck language is even denser.
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by shangaslammi on Thursday June 13, 2013 @06:18AM (#5873216)
The same thing in Haskell:

    cycle seq = circular where circular = seq ++ circular
(or if you want to really golf it)

    cycle = fix . (++)
Ok, to be fair the Scala code is a bit more generic as it works for any abstract sequence, but still, Scala's syntax is awfully heavy compared to Haskell.

That's one reason I still prefer Haskell even though Scala is easier to sneak into enterprise projects thanks to JVM. Not to mention that Haskell gives stronger static guarantees and has a more sophisticated type inference system.

Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by pja on Thursday June 13, 2013 @06:34AM (#5873274)
oo. I'd forgotten about fix.

  cycleM = fix . mplus
gives a nice generic version that works on Lists & Sequences but falls afoul of the monomorphism restriction, so it needs a type:

  cycleM :: (MonadPlus m) => m a -> m a
  cycleM = fix . mplus
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by trailfox on Thursday June 13, 2013 @08:28AM (#5873648)
That's not the sort of thing you'll commonly see in real world Scala code. It's the sort of thing that might be used internally by a library, but not likely something you will come across day-to-day in Scala code. Every language has examples of code where the syntax is hard to follow, but you need to look at common code rather than "ooh let's try some tricks" kind of code.
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by eloisant on Thursday June 13, 2013 @05:39AM (#5873086)
What makes it a bit more complex than it would be in Ruby is the typesafety (what comes after RichSeq). So it's a bit harder to write such a library, but once it's written it's a breeze to use for users because the compiler tells them right away if something is wrong with the type.
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by prollyignored on Thursday June 13, 2013 @05:18AM (#5873028)
Attention to syntax is what ruby is good at.

Scala, Haskell treat syntax as a chore.

Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by dons on Thursday June 13, 2013 @05:37AM (#5873079)
You don't know Haskell.

> take 8 $ cycle [1,2,3]

Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by pja on Thursday June 13, 2013 @06:29AM (#5873257)

  cycleM x = x `mplus` (cycleM x)

  take 8 $ cycleM [1,2,3]
might be more generic?
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by lucian1900 on Thursday June 13, 2013 @05:50AM (#5873121)
Scala maybe, not Haskell (which barely has any syntax).