News for hackers. Stuff that matters.
Re:Clojure vs. Scala by lmm on Tuesday December 24, 2013 @07:04AM (#6959108)
Scala guy here

Suits would surely be case objects rather than classes, or I'd be tempted to just use a Java enum (Scala could really do with an equivalent). With card ranks, either you want to be able to compare them - in which case integers are the correct representation - or they're just 13 "things", in which case again an enum-like representation is good; I can't imagine why you'd ever want to represent a card rank as int-or-string.

Case classes are java-serializable by default. Any number of other serializers (json, database...) exist that work seamlessly with case classes. I do think it would be nice to have an abstraction for something weaker than a class, something that was guaranteed to be inert data, but still typed.

And yeah, I might implement a deck as something that contains a sequence. But I could then derive typeclasses for sequence operations quite cheaply using lenses. (I do wish there was a nicer way of doing this kind of delegation though).

Sure, Scala steers you towards making a type straightjacket for yourself; you definitely can operate on stringly typed or maply typed data, as it sounds like Clojure encourages you to do all the time. And maybe it's not the best language for exploratory manipulation of semi-structured data. But when the time comes to write a production-ready system, the strong types that Scala gives you are a much faster way to achieve the level of reliability you need than the level of testing you need in a less typed language.

(And even if you don't need the safety, I find types are a great aid to figuring out WTF you were doing if you come back to the code in six months' time)

Re:Clojure vs. Scala by brudgers on Tuesday December 24, 2013 @08:01AM (#6959229)
I can't imagine why you'd ever want to represent a card rank as int-or-string.

I think this illustrates the author's point that languages structure our thinking. The value of a King as 13 and the Queen as 12 is determined by the rules of the card game, not the number of elements displayed on its face as is the case for the non-face cards - e.g. the face cards all have a value of 10 in blackjack.

Some languages better allow for the messy details of the world and allow the programmer to treat the partial and occasional isomorphism between names of and the values assigned to cards in a particular context uncomplected.

None of which is to say that strong static typing does not offer a particular set of advantages, only to suggest that it can become forced at a certain level of abstraction where real world objects are represented without a specific context for interpretation.

Imagine a program which begins by asking, "Which game do you want to play?" and includes a card guessing game, blackjack, Uno!, go fish, and crazy eights. We want the values of the cards to lie in their use within each game, not where we shuffle and deal and discard from hands.

Re:Clojure vs. Scala by lmm on Tuesday December 24, 2013 @08:48AM (#6959322)
> Imagine a program which begins by asking, "Which game do you want to play?" and includes a card guessing game, blackjack, Uno!, go fish, and crazy eights. We want the values of the cards to lie in their use within each game, not where we shuffle and deal and discard from hands.

I maintain that you gain nothing (except the possibility of errors) by representing the cards as int-or-string here. Think about how you'd actually use the cards in implementing those games. With my enum-like scala representation you could do anything you could do with the clojure version, and you'd get the advantage of e.g. compiler warnings if you defined incomplete match/case statements.

Re:Clojure vs. Scala by brudgers on Tuesday December 24, 2013 @12:50PM (#6960311)
The author's point isn't that int-or-string is a good choice, it's that there are a lot of choices and none of them allow natural abstraction over a deck of cards, the static typing encourages the abstraction down to a lower level - like ints or into a mashup like string-or-int.

And this is the really important point, this pushing the abstraction down to a lower level causes it to leak into the implementation of card games in ways that make the card games themselves harder to reason about.

Decks of cards have Kings, not 13's, and using 13 to represent the King is of type leaky abstraction. It's a muddy road leading into the swamp.

When I implement blackjack I get to write little balls of mud:

    If card = 13 then 10
And in my card guessing game

    If card = 13 and guess = King then "Yes, the card is a King!"
All that for a warning when I don't have a card 7, which is going to make Pinochle have to work around the abstraction - probably by bypassing the entire implementation when it chokes on two Jacks of Diamonds etc. in a deck.
Re:Clojure vs. Scala by lmm on Wednesday December 25, 2013 @05:01AM (#6962721)
I said the enum-style approach is what you want for something like that. "case KING | QUEEN | JACK | TEN => 10; case ValueCard(x) => x" is just as readable as anything you'd do in clojure, and the warning (not error - you can ignore it if your game really doesn't need to handle that card) if you forgot one is valuable.
Re:Clojure vs. Scala by brudgers on Wednesday December 25, 2013 @11:56AM (#6963383)
The enum is a bad abstraction because when I play GoFish, the computer asks, "Do you have any 13's?" and when I ask, "Do you have any Jacks?", I am always told to GoFish. It's an abstraction that sometimes returns a meaningful value, and other times doesn't, and the abstraction is built to optimize exactly the cases that are easy for me to reason about when writing a program and returns something that only looks correct in all the difficult cases, such as the Ace and so I wind up having to do all the heavy lifting myself anyway but only after having been misled to the wrong assumption that the abstraction was robust and well thought out.

There's no value to a warning that's irrelevant and perhaps even negative value if it encourages the idea of adding a wildcard last case just so I don't have it interrupting my thoughts each and every time I build my program since doing so opens the door to very run time errors it was supposed to prevent or conversely creates the habit of ignoring the compiler warnings in the very worst situation - when I think I know what I am doing.

If enums were good enough and I wanted to ignore compiler warnings, I could have used C.

Re:Clojure vs. Scala by lmm on Wednesday December 25, 2013 @04:05PM (#6964241)
> The enum is a bad abstraction because when I play GoFish, the computer asks, "Do you have any 13's?" and when I ask, "Do you have any Jacks?", I am always told to GoFish.

What? No it doesn't. Scala case objects don't have numerical values.

Re:Clojure vs. Scala by reginaldjcooper on Tuesday December 24, 2013 @12:05PM (#6960125)
I am having an incredibly difficult time understanding why int-or-string cards are desirable also. You'd end up with multiple functions like heartsGameCardValue and euchreGameCardValue but with int cards you need to write the same thing and without a type system to catch errors. If it was truly simpler to reason in terms of int, you could also do a cardIntegerRepresentation function and it would add very little length to the codebase.
Re:Clojure vs. Scala by profdemarco on Tuesday December 24, 2013 @07:34AM (#6959171)
> Scala gives you are a much faster way to achieve the level of reliability you need than the level of testing you need in a less typed language

I have serious doubts about this since practically everything in the Scala standard library is horribly broken (collections, views, everything in scala.io, scala.xml and scala.actors etc.). The number of bugs found by Paul Phillips alone is absolutely ludicrous.

As it stands now, both Haskell and Clojure have vastly superior libraries, build systems that actually work, and implementations that people actually understand and can modify.

Re:Clojure vs. Scala by octo_t on Tuesday December 24, 2013 @08:01AM (#6959230)
How are collections broken? Views are deprecated/set to be deprecated, I don't really use scala.io, I find java io sufficient for my needs, scala.xml is being split out in 2.11 and macros will let people use a third party implementation, scala.actors is also deprecated, having been replaced by Akka.

Paul has definitely found a lot of "bugs", he has certainly contributed a huge amount to the cleanliness of the code base, and has been a big pusher for removing certain levels of unsoundness in the type system, but if you were looking for bug-finders + fixers, Simon Ochsenreither is probably a better bet.

Re:Clojure vs. Scala by Hakeashar on Tuesday December 24, 2013 @09:43AM (#6959498)
Just a quick question - if the views are set to be deprecated, what would be the way to do a (long) series of collection transformation without spawning multiple intermediate collections? So, essentially, equivalent of .view, transformations, .force?
Re:Clojure vs. Scala by profdemarco on Tuesday December 24, 2013 @09:49AM (#6959525)
You use an iterator, which has its own caveats, or you simply accept that chaining collection transformations is going to be ridiculously inefficient.
Re:Clojure vs. Scala by lmm on Tuesday December 24, 2013 @08:44AM (#6959312)
The scala collections library is the most powerful/flexible I've ever seen in a strongly typed language; it combines the safety of Haskell with the flexibility of Clojure. Given the amount of ongoing complaints I see around cabal I don't think it's fair to say it "actually works" (fwiw I'm very happily using maven to build my Scala projects, so I never really know what the sbt fuss is about). And the rate of features and other progress in recent scala releases clearly demonstrates there are plenty of people who understand the implementation well enough to modify it.
Re:Clojure vs. Scala by profdemarco on Tuesday December 24, 2013 @10:03AM (#6959573)
> it combines the safety of Haskell with the flexibility of Clojure

Foldable, Traversable, Monoid etc. are all far better abstractions than what Scala collections provide, and they don't lead to unmanageable inheritance hierarchies and ridiculous APIs. How many bugs have been caused by that? A few hundred maybe? Does Set equality work in the current release or is that broken again? Is it possible to do thread safe efficient merges in immutable collections yet?

The only thing Scala collections offers that Haskell doesn't is the CanBuildFrom travesty. At least in Clojure you can chain transformations together without generating tons of intermediate values. In Scala you're forced to choose between being ridiculously inefficient or using iterators.

Re:Clojure vs. Scala by happy4crazy on Tuesday December 24, 2013 @11:31AM (#6959985)
Can you expand on the CanBuildFrom travesty?

(It seems like you've got interesting things to say, but you're being a bit strident. It bums me out that Scala discussions on HN take such a heated tone.)

Re:Clojure vs. Scala by profdemarco on Tuesday December 24, 2013 @02:14PM (#6960690)
CanBuildFrom is essentially a typeclass in Scala that is used for converting between collection types. Ignoring the coherency issues associated with using implicits for typeclasses, it makes the implementation of the collections library significantly more complicated and causes issues with type inference.

In the rare cases where you actually need to convert between collection types, it's usually trivial and more efficient to do it manually. Essentially they've added tons of complexity for negligible benefit. One of my main gripes with the Scala collections library is that it's overly complex and that this complexity has been the source of hundreds of bugs.

If there was any real benefit to the way it's currently done they wouldn't be hiding the true type signature for things like `List.map` in the official Scaladocs.

Re:Clojure vs. Scala by frowaway001 on Tuesday December 24, 2013 @06:03PM (#6961468)
You are kind of either ignoring or not knowing the core use-case of CanBuildFrom. Why talk about things you don't understand?
Re:Clojure vs. Scala by profdemarco on Tuesday December 24, 2013 @08:24PM (#6961837)
The reasons other than the ones I've outlined are caused by implementation details and wouldn't exist if they chose the correct abstractions.
Re:Clojure vs. Scala by frowaway001 on Wednesday December 25, 2013 @02:31AM (#6962501)
That's wrong.
Re:Clojure vs. Scala by Sssnake on Tuesday December 24, 2013 @02:24PM (#6960722)
You should try haskell sometime, their version of collections is much better. Complaints about cabal are of the same nature as complaints about every package manager "hey I tried to do something that can't be done and it said it can't be done". Having used all three languages, I still use haskell daily, I would use clojure again if demanded without complaint, and I would never in a million years consider touching scala again, ever.
Re:Clojure vs. Scala by frowaway001 on Tuesday December 24, 2013 @06:12PM (#6961498)
The last time I looked chaining collection operations still required unnecessary boilerplate and was not even the default way to use collections. Did they fix that? As far as I know, they didn't.

Additionally, it seems that moving from lists to a different collection type still involves rewriting your whole code due to the insane idea of using different functions by default (fmap/map, mempty/[], mappend/++).

I just don't have to deal with any of these design mistakes in Scala. Having sane defaults and stuff that "just works" consistently are a huge benefit.

Re:Clojure vs. Scala by Sssnake on Tuesday December 24, 2013 @09:34PM (#6961962)
>The last time I looked chaining collection operations still required unnecessary boilerplate and was not even the default way to use collections. Did they fix that?

I can't even imagine what you are talking about, so I can't say if they "fixed it" or not.

>Additionally, it seems that moving from lists to a different collection type still involves rewriting your whole code due to the insane idea of using different functions by default

"I did something dumb and now I don't like it". So, don't do it? Why were you using map in the first place?

>I just don't have to deal with any of these design mistakes in Scala

I don't have to deal with them in haskell either. But I do have to deal with the hundreds of other mistakes scala made if I use it.

Re:Clojure vs. Scala by koeninger on Tuesday December 24, 2013 @09:42AM (#6959496)
"build systems that actually work"

Maybe I'm feeding a troll here, but I've been building production software for over 4 years using SBT. It works.

Re:Clojure vs. Scala by saryant on Tuesday December 24, 2013 @01:15PM (#6960438)
Scala.xml and scala.actors are both deprecated. XML has been moved out of the language and scala.actors has been replaced with Akka actors (which are awesome, BTW).
Re:Clojure vs. Scala by profdemarco on Tuesday December 24, 2013 @01:36PM (#6960534)
They were (or are) being deprecated because they were broken beyond repair. That's pretty damning considering that all of the things I mentioned have been done properly in plenty of other languages. They aren't treading new ground here.

> Akka actors (which are awesome, BTW).

PartialFunction[Any, Unit]. Ridiculous.

Re:Clojure vs. Scala by saryant on Tuesday December 24, 2013 @02:48PM (#6960833)
The found a better solution so they switched to it?

That's bad?

As for the PartialFunction[Any, Unit] comment: well, yeah. That's the way actors work. They can be sent any message but may or may not respond. The company I work for has a very large Akka-based backend and that has honestly never been an issue for us. Moreover, it's necessary to facilitate hop-swapping (changing an actor's behavior dynamically during runtime). That is most certainly not a feature I would be willing to sacrifice. "context.become" is an incredibly powerful tool within Akka, especially in how it makes modeling a finite-state machine quite trivial in practice.

Re:Clojure vs. Scala by lmm on Wednesday December 25, 2013 @04:08PM (#6964248)
FWIW as a big scala fan I've given up on akka actors, until they manage to make typed ones that work (the current ones rely on an experimental reflection library that has threading bugs). The biggest advantage of Scala is type safety; if I wanted an unsafe language with actors I'd use Erlang.
Re:Clojure vs. Scala by saryant on Wednesday December 25, 2013 @04:32PM (#6964331)
Quite honestly, it's never a problem. In fact, I think it's a huge advantage. I simply would not be willing to give up the ability to change an actor's behavior dynamically.

We use the FSM helper [1] heavily in our backend at Conspire. For data-pipeline use cases, it's invaluable. Keeps our code concise, readable and testable. Even basic become/unbecome operations are big for us, I've written up one of their use cases on our blog [2].

[1] http://doc.akka.io/docs/akka/2.2.3/scala/fsm.html

[2] http://blog.goconspire.com/post/64901258135/akka-at-conspire...