News for hackers. Stuff that matters.
Re:Everyday hassles in Go by tarikjn on Tuesday December 30, 2014 @08:48AM (#8814550)
I couldn't agree more with this article.

I did some Go a year ago and liked it. Then coming back to it a year later after having done some functional programming in Clojure, it's not just the lack of generics that disrupt my flow but also having to think about all sorts of imperative programming details like naming and creating variables and scope placements in cases that would otherwise be unnecessary in a functional language.

I feel like I have been tainted by functional programming, and now Go feel like a joyless programming language which inevitably affect productivity.

I am looking for a replacement programming language to solve small problems for which Python/Ruby would have traditionally been used, good file system, stream and networking APIs, but concurrency as a first class citizen, garbage collected, fast startup time. Doesn't need to be good at long running programs, but it would be great if it can evolve to play a repeat role in a large system/process without the hassles of having to duplicate every written line of code into tests to prevent small changes from breaking things.

I am considering giving Haskell a try, but wondering if it might be overkill.

Any suggestions?

Re:Everyday hassles in Go by danidiaz on Tuesday December 30, 2014 @11:11AM (#8815132)
I think Haskell is a possible alternative to Go/Python/Ruby for these kinds of tasks.

But nothing's perfect. Some possible annoyances you might encounter:

* Much of the functionality required to be a valid "Python alternative" is not on the base packages. You'll have to install extra packages much sooner than with Python. And you'll have to know what packages to install.

* Despite having proper sum types, most I/O libraries in Haskell rely on exceptions for signalling errors. And Haskell has no proper stack traces. If you are not careful, you might find yourself pining for Go's multiple return values.

(Some would add "having to use the IO monad" to the list of annoyances, but I think it actually helps.)

Re:Everyday hassles in Go by bstpierre on Tuesday December 30, 2014 @11:42AM (#8815302)
I found ocaml to be a more flexible alternative -- I can sprinkle printf for quick debugging without having to alter types. (I think this is what you mean by "having to use the IO monad". I didn't work with haskell much, so it's possible there's an easy way around this there.) And stack traces in ocaml are reasonable, though not quite as verbose as python's.

But your point about needing to install extra packages stands for either language.

Re:Everyday hassles in Go by kxs on Tuesday December 30, 2014 @12:26PM (#8815565)
You should look into Debug.Trace for quick printf-like debugging. Usage: trace ("error occurs here") (f x)
Re:Everyday hassles in Go by gnuvince on Tuesday December 30, 2014 @02:04PM (#8816065)
OCaml is currently my language of choice. I love functional programming, been doing it for a while now, but it's nice to know that if I read an algorithm in a textbook, I can just implement it as-is instead of doing a translation to be purely functional (and sometimes having to fight to get back the asymptotic complexity). The module system is also absolutely great, and should be copied shamelessly by more languages.
Re:Everyday hassles in Go by Peaker on Tuesday December 30, 2014 @02:53PM (#8816356)
Haskell doesn't ban imperative programming, it simply distinguishes it in the types.
Re:Everyday hassles in Go by gnuvince on Tuesday December 30, 2014 @03:04PM (#8816424)
True, what I mean is that I can be "messy" in OCaml for the sake of expediency. In principle, I prefer Haskell's clean approach, in practice I'm a bad programmer who sometimes does naughty things.
Re:Everyday hassles in Go by Peaker on Tuesday December 30, 2014 @03:12PM (#8816476)
Well, Haskell does force you to go and update your types about your effects, and fix the use sites to do proper lifting.

I don't feel this costs much, though, so even in quick&dirty mode, I feel that Haskell is quite productive enough.

Re:Everyday hassles in Go by Peaker on Tuesday December 30, 2014 @02:52PM (#8816349)
Proper sum types aren't enough to replace exceptions.

Imagine every IO action having its own error type in a sum type. How do you compose them?

Composing the different error types wouldn't unify.

The best attempt I've seen in Haskell is the control-monad-exception[1] library, but I'm not sure how well that would interact with everything.

[1] https://hackage.haskell.org/package/control-monad-exception

Re:Everyday hassles in Go by danidiaz on Tuesday December 30, 2014 @03:49PM (#8816667)
> Imagine every IO action having its own error type in a sum type. How do you compose them?

Perhaps by converting each error type to a common sum type. Something like "Either (Either error1 error2) result". Typeclasses like "Bifunctor" are quite useful for massaging the errors around:

http://hackage.haskell.org/package/bifunctors-4.2/docs/Data-...

Asynchronous exceptions do pose a problem for this approach though, as they can pop up in any function at any time. And perhaps a fully sum-type-based approach would clutter function signatures too much anyway.

Re:Everyday hassles in Go by adrusi on Tuesday December 30, 2014 @03:40PM (#8816619)
Check out how rust handles error composition using its Result type
Re:Everyday hassles in Go by chrismccord on Tuesday December 30, 2014 @08:59AM (#8814588)
Elixir sounds like it might be worth a look. Nice for scripting and as good as it gets for concurrent programming. Some of its design is inspired by clojure so you might feel right at home. http://elixir-lang.org
Re:Everyday hassles in Go by wtf_is_up on Tuesday December 30, 2014 @09:47AM (#8814752)
> I am looking for a replacement programming language to solve small problems for which Python/Ruby would have traditionally been used, good file system, stream and networking APIs, but concurrency as a first class citizen, garbage collected, fast startup time.

You just described Go, mostly. I can't think of a more fitting language given your stated requirements.

To your first point: I don't know what Go has to do with your preference of functional programming to imperative. Go doesn't claim to be a functional language so I just am not seeing the connection.

Re:Everyday hassles in Go by tarikjn on Tuesday December 30, 2014 @10:04AM (#8814838)
Yes, I did describe what Go is good at, that's because I am trying to find something to put it against for what I use it for currently.

Let's say I prefer Haskell's syntax/approach. Coming from Go, what could I be missing in Haskell?

Re:Everyday hassles in Go by delluminatus on Tuesday December 30, 2014 @10:51AM (#8815035)
I quite like F# - it's similar to Haskell, but since it's a .NET language, you have an extensive STL and tons of 3rd party libraries. It works great with C#, so you can switch between functional and OO programming depending on your domain, while keeping the same STL and technology stack.

Also, you get the full power of Visual Studio (they recently changed it to be completely free), which can really help with productivity in my experience.

Re:Everyday hassles in Go by sacado2 on Tuesday December 30, 2014 @05:25PM (#8817086)
The ability to easily solve problems Go was made to solve (mainly, server programs and OS utilities). Those problems are, often, inherently easier to express with an imperative style (because they deal a lot with the external, imperative world and all its nasty aspects).

You might also loose the ability to know how much memory and time a given task requires at most, which can be a problem when dealing with servers (it's much harder to know how many requests you will be able to serve per second or how long a given request will take to be treated, for instance).

Re:Everyday hassles in Go by rrrrob on Tuesday December 30, 2014 @11:56AM (#8815386)
Fast compile times, for one.
Re:Everyday hassles in Go by xasos on Tuesday December 30, 2014 @10:33AM (#8814944)
> solve small problems for which Python/Ruby would have traditionally been used, good file system, stream and networking APIs, but concurrency as a first class citizen, garbage collected, fast startup time

I think for the most part, you won't be missing a lot in Haskell. If you are looking for what you say above, Go should satisfy your needs greatly. Haskell is amazing for functional programming needs - fast prototyping and great at doing mathematical calculations. It helps you think in another way, but I certainly wouldn't use it for building something like an API or networking.

Re:Everyday hassles in Go by nine_k on Tuesday December 30, 2014 @10:52AM (#8815040)
While it's kind of off-topic, let me still ask: what are the problems you see in using Haskell for networking? I see that building a library with the C calling convention might be hard, but a web service must be pretty language-agnostic.
Re:Everyday hassles in Go by codygman on Tuesday December 30, 2014 @02:11PM (#8816116)
I'm not sure why the xasos says that. For me Haskell has been great for apis. Regarding calling C, the Haskell ffi is very easy to use.

Try Nim, Ocaml, or Haskell to replace Go in that order. I order it in terms of probable familiarity.

Re:Everyday hassles in Go by nine_k on Tuesday December 30, 2014 @03:17PM (#8816508)
Calling C is easy. Calling from C might be not.
Re:Everyday hassles in Go by llambda on Tuesday December 30, 2014 @11:43AM (#8815305)
> I am looking for a replacement programming language to solve small problems for which Python/Ruby would have traditionally been used, good file system, stream and networking APIs, but concurrency as a first class citizen, garbage collected, fast startup time.

You may want to have a look at ClojureScript, which has come a long way in the last few years. While the tooling is still not as nice as Clojure-on-the-JVM in my opinion, it continues to get better everyday--this week support for a better REPL via NodeJS[1] was introduced, for instance. A lot of smart people are putting good effort into the compiler and ecosystem and I suspect we'll see a continued uptrend in cljs adoption. Anyway, it may be just what you want: an elegant, productive, pragmatic language with first-class support for concurrency (atoms, core.async) and well-suited to short-lived and scripting-domain problems. (Bonus you can write your frontend and backend in the same language if you want!)

[1] http://swannodette.github.io/2014/12/29/nodejs-of-my-dreams/

Re:Everyday hassles in Go by oahziur on Tuesday December 30, 2014 @12:42PM (#8815656)
The "REPL via NodeJS" is amazing!!! I want to try ClojureScript on NodeJS for a long time. Now I can play with the REPL.
Re:Everyday hassles in Go by Cyther606 on Tuesday December 30, 2014 @09:04AM (#8814599)
Re:Everyday hassles in Go by stonemetal on Tuesday December 30, 2014 @10:41AM (#8814984)
If you don't need windows support, O'Caml with Jane Street's Core is pretty good. The Async module handles the concurrency bits, and the Pipe module handles the streaming parts.
Re:Everyday hassles in Go by ufo on Tuesday December 30, 2014 @02:48PM (#8816333)
Note that for now Ocaml is still single threaded so the concurrency is cooperative, similar to how it is in Python or Nodejs. BTW, I'd recommend Lwt instead of Async because its more popular (kind of sad that there are two competing concurrency libs though)
Re:Everyday hassles in Go by rcarmo on Tuesday December 30, 2014 @09:03AM (#8814595)
Have a go at Hy (hylang.org). You can have your cake and eat it too. :)

Other than that, I'm fiddling with LISP Flavored Erlang (lfe.io), but I've had previous Erlang exposure.

Re:Everyday hassles in Go by tarikjn on Tuesday December 30, 2014 @09:41AM (#8814727)
That looks pretty interesting, but I don't have that much experience with Python, so the benefits are diminished.

I am looking for a runtime which can take full advantage of multiple cores for a single program, something I believe Python would fall short at.

Re:Everyday hassles in Go by felipehummel on Tuesday December 30, 2014 @08:59AM (#8814583)
You may try out using Scala as script language like this: "scala myScript.scala". The first startup times are slower, and even after that it may be slower than Python/Ruby but it still may be sufficient for your use case.

Using "sbt ~run" (re-runs your program automatically at every file change) may also be an option.

Re:Everyday hassles in Go by tarikjn on Tuesday December 30, 2014 @09:30AM (#8814676)
I don't see a reason why Clojure couldn't achieve the same startup time? I am trying to avoid the JVM startup time itself so that leaves out Scala/Clojure.

I'd like to be able to have a fast startup time when, say, running a script with its containerized environnement, when it is done the container exits, so I can't leave a vm running.

So I believe that leaves out JVM languages.

Independently from that, I did try Scala before Clojure and found its grammar to be overkill and unintuitive so it wouldn't be my first choice.

Re:Everyday hassles in Go by friendly_chap on Tuesday December 30, 2014 @09:32AM (#8814686)
This question is not easy. Most languages I consider well designed do not become mainstream - which comes with a host of problems. Nowadays I think if having no type system at all is better than having a broken type system, thus I am considering picking up a mainstream dynamic language (JS being an obvious candidate - but the runtime puts me off).
Re:Everyday hassles in Go by tarikjn on Tuesday December 30, 2014 @09:51AM (#8814777)
JS could fill the role but both the runtime and language are unideal, we end up dealing with a lot of browser baggage.

JS is still my tool of choice for building web apps. Just as for long running services, Clojure is a no brainer for me.

I am pretty open to ML or LISP based syntaxes, not necessarily looking for something Clojure-like.

Re:Everyday hassles in Go by jbooth on Tuesday December 30, 2014 @09:36AM (#8814703)
Go has first-class functions, if it had generics it'd be trivial to put together libraries for most of the functional-collection goodies, while still having imperative style available for when you care about performance or when it's just simpler for the problem.
Re:Everyday hassles in Go by nine_k on Tuesday December 30, 2014 @10:55AM (#8815054)
This, and using something like `Either` / `Result` instead of the `result, error := MethodCall()`. (Alas, the authors of Go seem to consider notions like 'monad' or 'Kleisli category', both pretty simple, impractical.)
Re:Everyday hassles in Go by jbooth on Tuesday December 30, 2014 @11:00AM (#8815077)
Words like 'monad' and 'kleisli category' (which I've never heard of) absolutely REEK of impracticality to me, regardless of the concepts.

There's a rule for academic papers where if the author is writing in clear language, he has something interesting to say, and if he's writing in fancy academese, he's probably saying absolutely nothing.

I don't really see a huge additional value for your proposed method call semantics, can you explain more? Some(Result) can make nil go away, but you've still got the error to deal with. So you're talking some type system where a single value encapsulates Result/Nil/Error?

Re:Everyday hassles in Go by nine_k on Tuesday December 30, 2014 @11:22AM (#8815191)
These words are merely unfamiliar. Do you think that 'pointer arithmetic', or 'open-closed principle', or 'abstract class', or 'exception propagation' sound less intimidating for a layman? But in reality none of these are complex after you've put a small effort to comprehend the notions behind them.

Specifically, try reading these completely un-academic un-papers.

'Either' monad, with nice kid-friendly pictures: http://fsharpforfunandprofit.com/posts/recipe-part2/

Bonus: Martin Fowler, of all men, endorses the above approach, in Java, of all languages: http://martinfowler.com/articles/replaceThrowWithNotificatio...

A Kleisli category, with examples in down-to-earth, compact, un-hairy C++: http://bartoszmilewski.com/2014/12/23/kleisli-categories/

This stuff is not hard and is utterly practical. It can very well be used in current industrial languages.

The worst service you can render to yourself is to start thinking that you already know all what is worth knowing, and close your mind to new concepts. Guys that thought that Fortran and Cobol are enough to get by for foreseeable future were technically correct — both are still in some demand! Unfortunately, their market share has dramatically shrunk, deservedly, as have employment opportunities.

Re:Everyday hassles in Go by AnimalMuppet on Tuesday December 30, 2014 @01:46PM (#8815973)
I've been trying to get my mind around this kind of stuff for maybe a year. And I have to say that monads don't look like the solution to any of the problems that I actually face or have ever faced, in a thirty-year career.

So, no, it's not just that the words are unfamiliar. It's that the problems that they're trying to solve are so abstract that they're completely disconnected from the work that I actually do.

And before you tell me that I'm really doing this stuff without realizing it: Maybe so. That doesn't mean that my life would be improved by doing it explicitly.

It seems to me that the "everything is category theory" approach has the same over-abstraction problem that the Java "AbstractFactoryFactoryFactory" approach has, and deserves to be as frequently mocked.

Re:Everyday hassles in Go by lmm on Tuesday December 30, 2014 @02:15PM (#8816144)
When I found out about monads it was after I'd already implemented the same abstraction. It actually comes up quite a lot (async calls, error handling, audit logging, database access). http://m50d.github.io/2013/01/16/generic-contexts.html . Just like when I was told about "the visitor pattern", my reaction was mostly "that? I've been doing that for years. So that's what it's called".

Still, it's worth knowing the name for a concept so that you can talk to people about it, and it's certainly worth knowing the library tools that exist and can save you time. Pretty often I'll write two or three lines of code, look at it, and then realise "oh, that's just traverseM" or some such. Even if it doesn't save me any time writing, replacing three lines with one library call is a huge bonus to maintainability.

Re:Everyday hassles in Go by nine_k on Tuesday December 30, 2014 @03:16PM (#8816498)
Did you ever work with a list? Did you ever do things like

   foo = [bar(x) for x in quux]
Or did you ever flatten a nested list?

Congratulations, you've been using one of the most widespread monads.

Monads are not artificial constructs. Monads, like functions, or loops, or conditional statements, emerge naturally when you try to describe a computation, even a simple one. This is like speaking prose without knowing that.

Re:Everyday hassles in Go by dllthomas on Sunday January 04, 2015 @03:31PM (#8835703)

    foo = [bar(x) for x in quux]
This is effectively a map, so it's strange to call it "using monads". "Using functors" would be more accurate.

You can get "monad" out of list comprehensions though, when you have multiple "for" clauses.

    foo = [y for x in quux for y in bar(x)]
seems equivalent to foo = quux >>= bar
Re:Everyday hassles in Go by AnimalMuppet on Tuesday December 30, 2014 @03:31PM (#8816578)
No, actually, I don't do things like that (see my parallel reply to codygman).

The Haskell types often seem to think that the computing they do is the same as the computing that everybody else does, and therefore that the problems that they want to solve are the ones that everybody else wants to solve. That's not a very accurate assumption.

Re:Everyday hassles in Go by codygman on Tuesday December 30, 2014 @06:13PM (#8817289)
> The Haskell types often seem to think that the computing they do is the same as the computing that everybody else does

Seeing as Haskell is used for general computing (including embedded software) the computing is the same. I know what you ate getting at, but I don't think it is based on facts or anything rational.

Re:Everyday hassles in Go by nine_k on Tuesday December 30, 2014 @05:27PM (#8817093)
Just for reference: what are the kinds of problems you solve?

Note that the code I quoted above is Python, and I have lots of stuff like that in my mundane production code (processing files, tracking transactions, counting money, etc).

Re:Everyday hassles in Go by codygman on Tuesday December 30, 2014 @02:18PM (#8816159)
If you give some examples of the work that you do perhaps we can make some of the abstractions concrete.
Re:Everyday hassles in Go by AnimalMuppet on Tuesday December 30, 2014 @02:58PM (#8816388)
Perhaps you could. That's not the same as making them useful for me, though.

I work primarily in embedded systems, where sequence is critical, many things are stateful, and there are multiple threads of control. So, for example, I could take the sequential aspects and re-write them as a monad. Or I could just do nothing, and let them be sequential imperative code.

I know that in a pure functional world, sequence can be written as a monad. But why should I care? In my world, it does nothing for me. I'm not going to try to write imperative code in a monad to build an illusion of non-sequential purity when the essence of what I need to be doing is so sequential.

Re:Everyday hassles in Go by ufo on Tuesday December 30, 2014 @08:00PM (#8817769)
One neat thing you can do with monads thats useful in your setting is coroutines/generators as a library. You get to write code that looks like its regular sequential code and behind the scenes it gets expanded into the "callback hell" you would have had to write if you decided to code the things in event driven style by hand.

Anyway, I do have to say that the Monad abstraction is not that useful outside of Haskell because you kinda really need Type Classes to get monads "right" and most languages don't have that. What you might end up is with specific monads like promises in JS but then the monads are just a design pattern instead of being a concrete abstraction like they are in Haskell.

In addition to that, in Haskell monads are the only way to do sequential computations with side effects because by default the language is Lazy. This means that Haskell programmers have to learn this abstraction whether they like it or not while in other languages you can avoid it better :)

Re:Everyday hassles in Go by codygman on Tuesday December 30, 2014 @03:06PM (#8816436)
The purity is not an illusion though. Writing those sequences as a monad gives you the ability to compose actions, creating a new sequence from code you've already written for one.
Re:Everyday hassles in Go by AnimalMuppet on Tuesday December 30, 2014 @03:26PM (#8816552)
But that's not a net win for me, compared to just having the action in a function that is called from several places.

Worse, composing the actions means I have to think through how the composing is going to affect the sequencing of events. Just having a function doesn't give me that problem - the sequencing is explicit, not hidden, and therefore is easier to reason about.

Re:Everyday hassles in Go by codygman on Tuesday December 30, 2014 @06:06PM (#8817265)
Can you give me some example code? I think our discussion would be a lot more productive and mutually beneficial with some code examples/output.
Re:Everyday hassles in Go by AnimalMuppet on Tuesday December 30, 2014 @06:34PM (#8817377)
I can't give actual example code (proprietary, company confidential, blah blah blah). So I'm going to have to come up with some fake example.

[Thinking...]

How about a robotic hand that's supposed to pick up a ball. The (C/C++) code might look like :

  void pickUpBall()
  {
    openHand();
    moveHandToBall();
    closeHand();
  }
You have to have that sequence. If the hand isn't open, moving it to the ball just bumps the ball away. If you close the hand before you move to the ball, you don't pick up the ball, you just close on air.

So, in Haskell (if I had the hardware control libraries), I could put that in a monad to guarantee the sequence, and in an IO monad so that I could do things that had side effects like affecting the external world. But in fact I get both of those for free, just by not writing it in Haskell. (The pure functional nature of Haskell also doesn't do much for me in this situation. Yes, for the functions that can in fact be purely functional, it can make them easier to reason about and prove correct. But that isn't the difficult part of the code.)

Composability: It's unclear to me how you're going to compose something like this in any way that's a) useful in this environment and b) dramatically different from simply making a higher-level function that calls pickUpBall() and some other functions.

Re:Everyday hassles in Go by codygman on Tuesday December 30, 2014 @07:11PM (#8817566)
Cool, now we have something to work with. However at such a high level no paradigm's differences (much less pros/cons) would be apparent. Would you mind implementing those individual functions?

Actually it might be easier to use some open source embedded code.

For completeness sake, I'd write the snippet above almost identically:

    pickUpBall = do
        openHand
        moveHandToBall
        closeHand
Re:Everyday hassles in Go by AnimalMuppet on Tuesday December 30, 2014 @08:25PM (#8817840)
OK, now you've got me really confused. Is that Haskell code? I thought that Haskell couldn't do sequence except inside a monad. (Or is "do" a monad?)

But if that isn't a monad, then why did you write it that way? My original claim was that a monad wouldn't do much for me in my world, and if you wrote this without a monad, that kind of seems like you're agreeing with me.

You said that you'd gain composability from writing this as a monad. In this example, how would that work? What would it buy me that I couldn't write (as easily) using what to me is the normal way?

Re:Everyday hassles in Go by codygman on Wednesday December 31, 2014 @12:57AM (#8818404)
It would most likely be in the IO monad since IO would be happening.

As for whether monads would help your code (and I still think they would) I'll need a larger example. In a small example abstraction and reuse aren't really apparent.

If you can provide a larger example, we can find out who's right ;)

Alternatively I'll try to find a larger example if you can't or don't want to.

Re:Everyday hassles in Go by AnimalMuppet on Wednesday December 31, 2014 @12:41PM (#8820212)
Well, I just made up this example, so feel free to embellish it...

But as you do, remember the question. We're in an embedded system, with lots of situations where sequence matters, and lots of stored state. The question is, what do monads buy me in that environment?

Re:Everyday hassles in Go by LBarret on Tuesday December 30, 2014 @11:33AM (#8815249)
While I understand your gut reaction, the fact that those words are unknown to you does not imply that they cover complicated concepts. It's just a new word, nothing more. it has nothing to do with .

Monad is just a way to establish a (type verified) set of rules for a sequence of functions. This set of rules can be -for example- about the management of return value of these functions : executing the next function if we get a result, returning immediately if we have an error.

What is powerful with Monads, is their genericity. They can be applied to many different contexts, and still provide the same type safety.

You're likely already building very limited/specific monads without knowing it.

Re:Everyday hassles in Go by Chinjut on Tuesday December 30, 2014 @01:38PM (#8815933)
Why should words you've never heard of reek of anything to you? You've never heard of them; you have no idea what they mean!
Re:Everyday hassles in Go by jramnani on Tuesday December 30, 2014 @10:36AM (#8814961)
You may want to take a look at Pixie.

"Pixie is a lightweight lisp suitable for both general use as well as shell scripting."

https://github.com/pixie-lang/pixie

Re:Everyday hassles in Go by zodiakzz on Tuesday December 30, 2014 @03:04PM (#8816426)
Re:Everyday hassles in Go by dominotw on Tuesday December 30, 2014 @01:56PM (#8816024)
F# ?
Re:Everyday hassles in Go by frowaway001 on Tuesday December 30, 2014 @10:57AM (#8815060)
Scala is pretty nice. Concerning the JVM startup time: It's usually in the range of milliseconds for your use-case.

What gave the JVM a bad name regarding startup time are large JavaEE application servers which had dependencies to hundreds of MBs of libraries. In this case, startup is slow, otherwise: not so much.