Live data from Hacker News

What Color Is Your Function?

journal.stuffwithstuff.com

61–70 of 153 posts

Re: What Color Is Your Function?

#61
post #60

A lot of commentors are mentioning that this is just a specific case of effect typing. Haskell and monads have been brought up as an example of effects typing, but I'd like to present another example that more closely resemble familiar static type systems. Nim[1], at least at one point (I'm looking at the current manual and can't find it documented), had support for tagging functions with a pragma and the compiler wo…

> It turns out that the are just a simple syntactic transform that isn't powerful enough to express everything that coroutines can.

Sure, but at least with async/await you can reuse control flow structures from the original language, like for loops and try-catch. With promises you need to reimplement all of those as a library.

Re: What Color Is Your Function?

#63
post #34

His solutions is threads ? Really? Has he read no history? There are problems with threads. That's why async I/O is hot right now. Threads is a limited resource. Threads are expensive to create and dispose. Context switches are espensive. Threads must be synchronized. Threads can have race conditions. Good rant, but I didn't expect him to serve such a shallow conclusion after a solid and insightful introduction.

>Threads is a limited resource. Threads are expensive to create and dispose. Context switches are espensive.

There is absolutely no reason they have to be. If you have an async callback API, you can plaster over it with green threads and they are very cheap, and don't have to involve any new context switches.

>Threads must be synchronized. Threads can have race conditions.

You don't have to allow your threads to run concurrently just because you added threads.

Re: What Color Is Your Function?

#65
post #57

You can always make a function that sync all async operations: sleep until a global variable is changed by the callback. A pain, but still not that bad.

Yes, I've used a similar technique myself; however I consider it only a last resort. But doesn't that require your language to have threads? Is it possible to do this in eg. Javascript?

Re: What Color Is Your Function?

#66
post #17
post #5

Earlier quoted context omitted.

These kind of distinctions happen all the time . The classic http://www.joelonsoftware.com/articles/Wrong.html - one colour for html-encoded strings, one colour for plain strings. Functions that might fail with an error or functions that never fail. Logging versus non-logging. Database-accessing or not. Callable from user scripts or not. Keeping track of colour - in a way where correctness is guaranteed by the comput…

I'm not sure if you're leading this or not, but to be clear... this is exactly a type system. (Edit: read your blog; it's clear you're leading it. Poe's Law is in effect sometimes here, sorry 'bout that!)

It's a good type system. Many programming languages don't support this kind of type information, notably C.

Re: What Color Is Your Function?

#67
A related way of composing code is railway oriented programming already commented on HN (1)

Underneath there are monads, sure, but the author has deliberately chosen a more mechanical metaphor which may help those for whom monads sound too abstract.

The post focuses more on how to handle errors that asynchrony, but it shows well the key steps to lift a blue function into red ones and to compose these constructions.

(1) https://news.ycombinator.com/item?id=7887134

Re: What Color Is Your Function?

#70
post #61
post #60

A lot of commentors are mentioning that this is just a specific case of effect typing. Haskell and monads have been brought up as an example of effects typing, but I'd like to present another example that more closely resemble familiar static type systems. Nim[1], at least at one point (I'm looking at the current manual and can't find it documented), had support for tagging functions with a pragma and the compiler wo…

> It turns out that the are just a simple syntactic transform that isn't powerful enough to express everything that coroutines can. Sure, but at least with async/await you can reuse control flow structures from the original language, like for loops and try-catch. With promises you need to reimplement all of those as a library.

You can recurse and branch just fine, you just lose for/while loops.
Post reply on HN