News for hackers. Stuff that matters.
Re:Making the move from Scala to Go by WkndTriathlete on Tuesday January 24, 2017 @09:32PM (#13477932)
Agreed, but they also complained about map() and flatMap(). I have to think that eventually every developer can understand the more straightforward monads, functors, and Either - which, along with type aliases, IMO, can make code a lot more readable.

I think the more esoteric features should be reserved for complex code, especially if it's possible that those features can prove runtime correctness at compile time and result in easily-composable modules. I wouldn't expect more than one or two developers to need to develop such a subsystem, however, so the "clever" is used in isolation.

Re:Making the move from Scala to Go by faitswulff on Tuesday January 24, 2017 @09:50PM (#13478038)
The full comment is actually:

> No map, no flatMap, no fold, no generics, no inheritance… Do we miss them? Perhaps we did, for about two weeks.

So they weren't complaining about map and flatMap, they just missed them.

Re:Making the move from Scala to Go by nine_k on Wednesday January 25, 2017 @01:17AM (#13479130)
They only missed these features for 2 weeks?

Good for them! Their problem domain is likely so simple and neat that it fits the Go's limited built-in types well, and does not lead to frequent copy-paste programming. If so, Scala has been an overkill.

Re:Making the move from Scala to Go by unscaled on Wednesday January 25, 2017 @05:57AM (#13480371)
Lucky bunch indeed. I'm 9 months in and still missing map(), flatMap(), let alone more advanced FP. I guess this is a matter of personal preference, but I definitely consider:

  users.filter(_.active).map(_.email)
to be easier to read (and not a pain in the ass to type) than:

  emails := make([]string, 0)
  for _, u := range users {
    if u.active {
      emails = append(emails, u.email)
    }
  }
  return email
The first style is more explicit about what you want to do, the second style is more explicit about how you want to do it. Go made imperative vogue again, but for me it feels like a throwback all the way to Turbo Pascal. Sure, the Go compiler is really fast, but Turbo Pascal would beat it hands down - on 20Mhz machines with 1MB of RAM.
Re:Making the move from Scala to Go by pjmlp on Wednesday January 25, 2017 @07:32AM (#13480862)
> Sure, the Go compiler is really fast, but Turbo Pascal would beat it hands down - on 20Mhz machines with 1MB of RAM.

With more features (TP 7)!

Re:Making the move from Scala to Go by jim_d on Wednesday January 25, 2017 @11:23PM (#13488684)
completely agree. There is a certain beauty to the first version that once you've learned the concepts it seems a shame to have to write the second one.
Re:Making the move from Scala to Go by nunobrito on Wednesday January 25, 2017 @08:54AM (#13481340)
The second example is vastly more readable in terms of understanding what it is supposed to do.

I mention this as neither a programmer on either Scala nor Go.

If it is of any relevance, I've been writing code for about 20 years across C, Java, Javascript, PHP and shell scripts. I'm sure other FP developers will roll their eyes to a traditional developer like me but the one-liner you mention is simply not self-explanatory for an outsider when reading it.

Re:Making the move from Scala to Go by spdionis on Wednesday January 25, 2017 @09:16AM (#13481476)
"I've been speaking italian, french and spanish for 20 years. I'm sure people who speak germanic languages will roll their eyes to a traditional latin-languages guy like me, but the german sentence you mention is simply not self-explanatory for an outsider when reading it."
Re:Making the move from Scala to Go by kaoD on Wednesday January 25, 2017 @09:17AM (#13481487)
The second one is so readable it contains a subtle bug nobody noticed.
Re:Making the move from Scala to Go by gribbly on Wednesday January 25, 2017 @11:15AM (#13482613)
What 'subtle' bug ?

It returns 'email' instead of 'emails' which I assume was a typo, which of course the compiler would complain about, are you claiming that a variable name typo is somehow a 'subtle bug' ?

Only other thing I see directly is that 'users' is undefined, but this is a code snippet after all, not a working program.

Re:Making the move from Scala to Go by kornish on Wednesday January 25, 2017 @05:18PM (#13486778)
I think kaoD's point is that more code will always produce more errors, and more code (in the name of "readability") can give more surface area for bugs.
Re:Making the move from Scala to Go by sacheendra on Wednesday January 25, 2017 @09:42AM (#13481684)
But that would be caught by the compiler. The error would be variable not defined.
Re:Making the move from Scala to Go by matt_kantor on Thursday January 26, 2017 @12:36PM (#13493349)
Out of curiosity, what specifically do you find unreadable about the first example?
Re:Making the move from Scala to Go by kjksf on Wednesday January 25, 2017 @01:25AM (#13479171)
As simple as a distributed, horizontally scalable SQL database, perhaps? (https://github.com/cockroachdb/cockroach).

Those dumb Go programmers and their toy programs (https://quicknotes.io/n/1XB0).

Re:Making the move from Scala to Go by pjmlp on Wednesday January 25, 2017 @04:59AM (#13480126)
I program since the mid-80's, many of the features in modern languages weren't always available outside research institute walls, and yet we managed to deliver any sort of software with the tools we had at our disposal.

However, now that those features are part of the majority of mainstream languages, I surely don't want to go back to how I used to write software in the first two decades of my career.

Re:Making the move from Scala to Go by unscaled on Wednesday January 25, 2017 @06:13AM (#13480461)
Developers have been using C and a bunch of other imperative languages for years. They obviously, created software two order magnitudes more complex than CockroachDB (or Docker, or Kubernetes, or etcd). We used to write entire operating systems in assembly, heavens forbid. Just look at the PostgreSQL codebase (all C) and compare that to cockroach DB.

Being "a better C" was the original battle cry of Go. It's definitely better than C (well, at least for some thing), but it doesn't mean all of these complex C software people used to write are toys.

Go is obviously not a useless toy language either. But that doesn't mean you won't miss features from more expressive languages. The amount of calls for generics from Go users tell a different story.

Re:Making the move from Scala to Go by geodel on Wednesday January 25, 2017 @09:10AM (#13481429)
> The amount of calls for generics from Go users tell a different story

I think it is from Non-Go users who would use Go if only it had generics.

Re:Making the move from Scala to Go by unscaled on Thursday January 26, 2017 @05:23AM (#13490179)
Some of it, probably. But some are probably using Go out of necessity (your company enforces Go), or because they still think Go is their best pragmatic option, lack of generics notwithstanding.
Re:Making the move from Scala to Go by sanjayts on Wednesday January 25, 2017 @04:28PM (#13486325)
I do competitive programming using Go and sorely miss "generics" so I'm pretty sure that your thought is not entirely correct.
Re:Making the move from Scala to Go by nollbit on Wednesday January 25, 2017 @02:15AM (#13479408)
Software can be complex without having very complex types
Re:Making the move from Scala to Go by nine_k on Wednesday January 25, 2017 @03:04AM (#13479639)
The guys who wrote CockroachDB never, afaik, tried to write it in Scala, and never told us whether they miss flatMap, generics, or something like that.

They guys from Movio did; their problem domain is likely different.

Re:Making the move from Scala to Go by skybrian on Tuesday January 24, 2017 @10:25PM (#13478200)
A for-each loop is only slightly longer, more familiar (just about every language has it), and more flexible - it covers map, flatMap, and reduce. So why have three or more specialized constructs when one will do?

There are cases when the restrictions of map() and reduce() are necessary, most famously for a map-reduce in distributed programming, but that's not really relevant for implementing a simple function.

Re:Making the move from Scala to Go by Daishiman on Tuesday January 24, 2017 @11:30PM (#13478534)
Map, fold, reduce and friends are statements of intent, and reading a line of transformations composed on multiple maps and such is generally much clearer than reading a series of loops in which you have to figure out intent by looking and what's going on inside each loop.

Also, error density is relatively constant per line of code according to most studies (this is a very general statement, but we can assume that most code just isn't trying to be clever). I've forgotten to add(), append() and drop items inside a loop far more than I've made mistakes inside more functional methods.

Re:Making the move from Scala to Go by macintux on Tuesday January 24, 2017 @10:36PM (#13478239)
I would argue that for loops are imperative, map/fold/etc are more declarative. That leads to various benefits such as less code, fewer bugs, fewer off-by-one errors.

And, man, tail recursion in a language with function head pattern matching (ML family, Erlang) is so much easier to read than any complicated for loop.

(Update: realized afterwards that "foreach" isn't quite the same as "for", but the larger point still stands, mostly.)

Re:Making the move from Scala to Go by pjmlp on Wednesday January 25, 2017 @12:28AM (#13478834)
No it doesn't, because map, flatMap, and reduce can be lazy, distributed across local processors, or even the network.

They describe the intent of what to do with the data, not how it is done.

Just like using SQL versus an old xBase database to access data records.

Re:Making the move from Scala to Go by ptx on Wednesday January 25, 2017 @06:10AM (#13480436)
And a goto statement is even more general, covering all kinds of looping constructs, exceptions and even the eliminating the need for functions! Why not just use those?
Re:Making the move from Scala to Go by unscaled on Wednesday January 25, 2017 @06:21AM (#13480509)
For loops are yet another of these useless abstractions introduced by the academia, this time straight from the design-by-committee language ALGOL 60.

We all know real programmers used gotos.

Re:Making the move from Scala to Go by lostcolony on Wednesday January 25, 2017 @09:33AM (#13481593)
Technically, reduce covers map, flatmap, filter, etc. It's every bit as flexible as a for-each loop (the only difference is it implies no side effects, but does not guarantee them if the language doesn't).

The very reason those other functional constructs were split out was not due to need, but due to -clarity-. And that same clarity applies to why to use one of those constructs instead of a for-each loop.

Re:Making the move from Scala to Go by danellis on Tuesday January 24, 2017 @10:39PM (#13478252)
The problem with for-each in a lot of languages is that it's a statement, not an expression, so it only works with mutable data.
Re:Making the move from Scala to Go by hyperpape on Tuesday January 24, 2017 @11:31PM (#13478536)
I'm definitely in camp map/filter/fold, but if you instantiate a list/accumulator, fill/operate on it inside the loop, then consume it, you're essentially writing pure code. The fact that you're doing it on top of mutable foundations doesn't matter (at that scale).
Re:Making the move from Scala to Go by lmm on Wednesday January 25, 2017 @09:01AM (#13481384)
The trouble is that safe mutation and dangerous mutation look very similar. It's possible to write a function that performs safe, locally encapsulated mutation, sure - but it's much harder for a reader to confirm those properties in code review or when debugging compared to just seeing that the code doesn't do any mutation at all.
Re:Making the move from Scala to Go by fpgeek on Wednesday January 25, 2017 @03:48AM (#13479822)
As long as you get your mutable code right. If there are any unintentional holes in your instantiate/operate/consume box...
Re:Making the move from Scala to Go by hyperpape on Wednesday January 25, 2017 @09:20AM (#13481503)
There just isn't that much to get wrong in pure for-each loops:

    def filter_broken(widgets):
        broken_widgets = []
        for widget in widgets:
            if widget.is_broken():
                broken_widgets.append(widget)
        return broken_widgets
The actual errors with loops and mutable objects come from complex nested loops, continues/breaks (multi-level ones especially!), extra boolean conditions, and sharing mutable objects between parts of the code base.

I think that overstating the supposed risks of for-loops doesn't help anything. The problem is not that you're going to mess up a simple for loop. It's the ways that your code doesn't compose well that are more problematic. Oh, and the verbosity sucks too.

Re:Making the move from Scala to Go by macintux on Wednesday January 25, 2017 @02:53PM (#13485353)
Hm. Wouldn't your code reverse the order of the list?

Maybe that's right, maybe that's wrong, but it's indeterminate from the code whether it's desired, whereas a filter function and a reverse function would make it explicit.

Re:Making the move from Scala to Go by hyperpape on Wednesday January 25, 2017 @03:44PM (#13485865)
Append is a function that adds it to the end of the list. (I think my code is working Python, but I haven't actually run it).

Btw: I'd venture to guess that you're not a Python person, and that's why it's not obvious that it constructs the method in the same order. In imperative languages (where Python isn't the perfect example b/c it has filter and list comprehensions), you get so used to these loops that you don't have to worry about questions like that.

If you wanted a reversed list, you'd either name your function appropriately, or call reverse after you run the filter method. It's a little painful to do the first bit.

Re:Making the move from Scala to Go by macintux on Wednesday January 25, 2017 @04:08PM (#13486114)
Yeah, sorry, got my mental model mixed up. I worry about reversing lists a lot since I deal with TCO so much.
Re:Making the move from Scala to Go by lmm on Wednesday January 25, 2017 @05:13AM (#13480190)
> So why have three or more specialized constructs when one will do?

Because having three specialized functions for three specialized operations makes it easier to see what a given piece of code is doing. One function per function.

Re:Making the move from Scala to Go by jghn on Tuesday January 24, 2017 @10:36PM (#13478240)
When I was first introduced to scala my very favorite thing was the existence of map. I'd been using java for a while and missed it dearly
Re:Making the move from Scala to Go by pjmlp on Wednesday January 25, 2017 @12:26AM (#13478817)
Not allowed to use Java 8?
Re:Making the move from Scala to Go by jghn on Wednesday January 25, 2017 @08:04AM (#13481050)
Didn't exist at the time. While I still prefer scala to java 8, had the latter been available at the time I likely never would have dabbled in scala in the first place, if that makes sense
Re:Making the move from Scala to Go by pjmlp on Wednesday January 25, 2017 @01:58PM (#13484684)
Sure it makes sense.

I dabble in what sorts of programming languages, but when comes to work, it is mostly C#, Java, JavaScript and some occasional C++, because that is what customers pay for.

Hence why I see a big value in mainstream languages slowly evolving into multi-paradigm ones, as many of us don't have the luxury to move beyond the first class languages of each platform.

Re:Making the move from Scala to Go by agumonkey on Tuesday January 24, 2017 @11:26PM (#13478510)
side effects ? testing ? decoupling ? (thus reuse)

If your brain is organized enough to write clean for loops then ... maybe. But it's a bit opportunity for problems. At least to me; but then I may not be smart enough.

Re:Making the move from Scala to Go by deepGem on Tuesday January 24, 2017 @11:47PM (#13478615)
I think for tasks such as JSON parsing, map and flatmap like functions are really helpful. You write less code and the code is way easier to understand (for those who are familiar with map functions).

That being said, map and flatmap force immutability in some way. So you pay a price for this, either in speed or in memory, even with tail recursion.