I am interested in defining a parallel map operator for my language. It transforms a list into a new list given an expression. It woul开发者_运维技巧d have a syntax similar to a generator. Unlike generators in C# and Python, it would potentially be evaluated in parallel, if the compiler desires (e.g. there is an idle core, and the list is really big). It would be called witheach
to distinguish it from foreach
which is executed sequentially.
For example consider:
var f = function(int x) : int { return x * 2; }
var my_list = 0..1000000;
var my_mapped_list = witheach (i in mylist) yield f(i);
My question is, is this going to be too unintuitive for programmers who may put side-effects in f
? Of course, I would say not to do this in the documentation, but most programmers don't read language documentation. :-)
I guess the bigger question, is can modern day programmers adapt to implicit parallel list processing semantics in their language, or do they need things to be more explicit?
You're right in that most users won't read the documentation, and as such will probably put non-thread safe code in a witheach
block (especially with a name and syntax so similar to foreach
). You'll either have to trust your users, or perform some static analysis on the concurrent safety of that block. If you chose the later, then you may as well not bother with witheach
- just automatically parallelise foreach
when possible.
As to whether users are ready for the responsibility of choosing to be potentially dangerous, I would wager "yes", as long as they know up front what is safe. Many languages force you to make this choice each day (pointers, manual memory management, shared memory concurrency). You might want to make your syntax less ambiguous (i.e. call it parallelforeach
), so that people know what they're signing up for.
You mean something like pmap?
精彩评论