Live data from Hacker News

Scala 2.11.0 Release Notes

scala-lang.org

71–77 of 77 posts

Re: Scala 2.11.0 Release Notes

#71
post #54
post #53

Earlier quoted context omitted.

I've used ScalaZ a bit (don't remember exactly why, but something to do with Future transformations), but I found it to crush the compiler. Especially combined with IntelliJ. I like the async/await stuff. Especially after attending the ScalaDays presentation on it. The idea that it produces a state machine in the background feels like it's very easy to reason about. I actually (personally) find for-comprehensions the…

For comprehension is probably Scala's most powerful feature aside from higher kinded types. You may not see the advantage of for comprehension for sequencing a few operations over Future. However, when you have a large number of calls you have to sequence along with filter (which for comprehension can do) it's indispensable. Check this out: https://www.youtube.com/watch?v=MHw-dDxC8Z4

Watching the video now. Thanks for the tip.

I'm curious why you'd call for-comprehensions powerful though. AFAIK they're just sugar over map/flatten/filter.

IME it's almost always more succinct and more readable to just call the methods you want directly.

Plus, you can say: map over an Option and transform both cases. You could also map then getOrElse, but readability suffers if your map is multi-line IMO. In the for-comprehension version you can't transform the None case.

I use for-comprehensions with Extractors in testing, because whatever. It's a test. So:

  for {
    Some(user) 
Is fine in that case.

Pattern Matching and Lifting are probably Scala's best features off the top of my head. Type Classes a close third.

But for-comprehensions are just sugar. They don't enable you to do anything you couldn't without them, and they actually make some flows impossible to write. I find that you can usually tame a nested mess with partial functions and a collect(). Or a fold() to handle your dual-transform.

That's just me though. Only been at the Scala job for a little over a year.

edit: @noelwelsh

I would nest yeah. But I'd see it more as a refactoring opportunity. Should authorization be in a for-comprehension? I'd instead add an AuthorizedAction in Play. That authenticated and provided a User from cache. So your example would look more like:

  def show = AuthorizedAction.async { (user, permission) =>
    actions.user(Read(permission, user))
  }
I think I'd have to agree with another poster that doing all that inside of a for-comprehension would look like a code-smell to me.

More than that, is map/flatten the right tool for the job for all this? Even if I wanted to do it inline, I'd probably prefer:

  val perm = loginActions.mandatoryAuth(req)
  val queryString = req.mandatoryParam[String](uuidParam).toClientProblem
  
  (perm zip queryString) map {
    case (Some(perm), CachedUser(user)) => actions.user(Read(perm, user))
    case _ => BadRequest()
  }
It's definitely subjective. I wouldn't fault anyone for using the for-comprehension (though I would encourage them to consider if it should rather be an Action), but describing it as "powerful" just doesn't sit right with me for some reason.

Plus while you'll see for-comprehensions in the wild on occasion, I think it's a stretch to call them idiomatic. Unless you were going to constrain yourself to projects with ScalaZ as a dependency I suppose.

Re: Scala 2.11.0 Release Notes

#72
post #43
post #38

Earlier quoted context omitted.

I really hope that you guys aren't planning to pull a Python 3 with the 3.x series. We're using Scala quite heavily in our production systems and the naysayers will have a great "I told you so" moment of we end up sitting on a ton of critical Scala code which no longer compiles in a future version.

I kind of hope the Scala guys do that, actually. There is too many ways to do certain things. "list.map(_+2)" is legal while "list.map((_+2)+3)" is not. Instead, you need to use an anonymous function like this: "list.map(x => (x+2)+3)". Why is the _-style even legal if it's so inflexible? Why not force functions all the time? Why have two ways to do the same simple thing?

The _ style is legal because it's really useful. Most of the time you're mapping with small functions, and the extra few characters really add up - more than enough to be worth the extra learning.

Re: Scala 2.11.0 Release Notes

#73
post #71
post #54

Earlier quoted context omitted.

For comprehension is probably Scala's most powerful feature aside from higher kinded types. You may not see the advantage of for comprehension for sequencing a few operations over Future. However, when you have a large number of calls you have to sequence along with filter (which for comprehension can do) it's indispensable. Check this out: https://www.youtube.com/watch?v=MHw-dDxC8Z4

Watching the video now. Thanks for the tip. I'm curious why you'd call for-comprehensions powerful though. AFAIK they're just sugar over map/flatten/filter. IME it's almost always more succinct and more readable to just call the methods you want directly. Plus, you can say: map over an Option and transform both cases. You could also map then getOrElse, but readability suffers if your map is multi-line IMO. In the for…

When you're doing functional programming you represent (almost) everything as a value. Say you're working in a concurrent system, (e.g. a web app) so you're dealing with Futures everywhere. Are you going to write 4 or 5 nested flatMaps? It's unreadable. For comprehensions are much easier to parse. Here's an example from real shipping code

          for {
            perm        
Then you get into nested monads (e.g. Either can represent a computation that succeeds or fails, which you want to contain inside a Future) and you use monad transformers to squish them into one single monad, to avoid nesting for comprehensions.

Re: Scala 2.11.0 Release Notes

#74
post #71
post #54

Earlier quoted context omitted.

For comprehension is probably Scala's most powerful feature aside from higher kinded types. You may not see the advantage of for comprehension for sequencing a few operations over Future. However, when you have a large number of calls you have to sequence along with filter (which for comprehension can do) it's indispensable. Check this out: https://www.youtube.com/watch?v=MHw-dDxC8Z4

Watching the video now. Thanks for the tip. I'm curious why you'd call for-comprehensions powerful though. AFAIK they're just sugar over map/flatten/filter. IME it's almost always more succinct and more readable to just call the methods you want directly. Plus, you can say: map over an Option and transform both cases. You could also map then getOrElse, but readability suffers if your map is multi-line IMO. In the for…

0. Drop me an email (see profile) if you want to continue this conversation. My full response is too large for a textarea.

1. Not using Play, so that's moot.

2. The monad type is not an Option, it's more like Future[Either[Problem, Response]], Caches, for instance, return Futures.

3. That was the simplest for comp I could find. Most of them are much longer. You'd have to be batshit crazy to write nested flatMaps for them.

Re: Scala 2.11.0 Release Notes

#75
post #67
post #55

Earlier quoted context omitted.

I concur. I loved the Functional Programming Principles in Scala course and recommend it to everyone I can. However, I'm not at all sold on the Reactive Programming course. Erik Meijer's lectures -- at least in the first iteration of the course; maybe they've gotten better -- were riddled with errors, confusing exercises, and an overall lack of coherence with the rest of the course. It pains me to say this because Er…

I've always found this to be a strange duality in the Scala community. On the one hand they praise strong, static type checking in general Scala programming, and on the other hand they praise an almost untyped paradigm in the actor model. And few people seem to take issue with this.

Plenty of people don't like this aspect of Akka. Most of them are using scalaz-stream instead.

See http://noelwelsh.com/programming/2013/03/04/why-i-dont-like-... and http://www.chrisstucchio.com/blog/2013/actors_vs_futures.htm...

Re: Scala 2.11.0 Release Notes

#76
post #43
post #38

Earlier quoted context omitted.

I really hope that you guys aren't planning to pull a Python 3 with the 3.x series. We're using Scala quite heavily in our production systems and the naysayers will have a great "I told you so" moment of we end up sitting on a ton of critical Scala code which no longer compiles in a future version.

I kind of hope the Scala guys do that, actually. There is too many ways to do certain things. "list.map(_+2)" is legal while "list.map((_+2)+3)" is not. Instead, you need to use an anonymous function like this: "list.map(x => (x+2)+3)". Why is the _-style even legal if it's so inflexible? Why not force functions all the time? Why have two ways to do the same simple thing?

It would also be nice if "<-" were replaced with "in". It would just be more helpful and easier to remember. Special signs should be avoided at every opportunity

Re: Scala 2.11.0 Release Notes

#77

Earlier quoted context omitted.

Have you seen the follow up course "Principles of Reactive Programming" too? It's also by Martin Odersky, but also with Erik Meijer and Roland Kuhn. https://www.coursera.org/course/reactive

I took both classes, but I really didn't care for Reactive as much. The first Coursera class really turned me on to the elegance of functional programming with Scala's unique type system, but much of that elegance is really lost dealing with some of the structures introduced in the second class, in my opinion. Reactive is split into three "subclasses". The beginning part, taught again by Odersky, was a pretty useful…

You are most welcome.
Post reply on HN