The functional equivalent of parallel multicast

In another question , I asked about parallel multimaras for Java.

Is there some kind of functional programmable (immutable) template that can be used instead in Scala or Clojure? I think the Scala solution is likely to include participants and Clojure one a atom, refor agent, but there may be a better way. Since both languages ​​allow you to “step back” from Java interaction and simply use the Java solution, I can use any answer I get for my first question, but this will not correspond to the functional programming paradigm. How do Haskell programmers solve this?

+5
source share
3 answers

Standard Clojure cards and sets are immutable (and constant) [1], so they work just as well in parallel programs. You may want to store them in ref / agent / var / atom depending on your requirements, and you can simply update ref / agent / var / atom as always.

You may have a more volatile map if the values ​​are really refs, for example:

{:a (ref #{1 2 3})
 :b (ref #{4 5 6})}

In this case, you can actually add values ​​to an existing key (in a transaction, of course). Adding and removing keys will still return new maps that will use the same links as the original maps, and therefore changes to one of them will be visible to the other:

user=> (def mmap {:a (ref #{1 2 3}) :b (ref #{4 5 6})})
#'user/mmap
user=> mmap
{:a #<Ref@be0446: #{1 2 3}>, :b #<Ref@10aa282: #{4 5 6}>}
user=> (def mmap2 (assoc mmap :c (ref #{7 8 9})))
#'user/mmap2
user=> mmap2
{:c #<Ref@405f6: #{7 8 9}>, :a #<Ref@be0446: #{1 2 3}>, :b #<Ref@10aa282: #{4 5 6}>}
user=> mmap
{:a #<Ref@be0446: #{1 2 3}>, :b #<Ref@10aa282: #{4 5 6}>}
user=> (dosync (alter (:a mmap2) conj 0))
#{0 1 2 3}
user=> mmap
{:a #<Ref@be0446: #{0 1 2 3}>, :b #<Ref@10aa282: #{4 5 6}>}
user=> mmap2
{:c #<Ref@405f6: #{7 8 9}>, :a #<Ref@be0446: #{0 1 2 3}>, :b #<Ref@10aa282: #{4 5 6}>}

[1] That is, adding / removing / changing keys, and the values ​​actually return a new card without changing the original.

+7

. concurrency, .

+3
0
source

All Articles