0

How does one approach this type of concurrent stream communication program?

local InS in
{Browse {Counter InS}}
InS=a|b|a|c|_
end

One should see in the browser [a#1]|[a#1 b#1]|[a#2 b#1]|[a#2 b#1 c#1]|_

A sequential program would be trivial, but without the full definition of the InS list, I can not think of an acceptable approach. Any tips would be greatly appreciated!

I tried to implement the concurrent map function, but this definition is kind of strange

declare
fun {ConcMap Xs F A}
   case Xs of nil then nil
   [] X|Xr then thread {F X A} end|{ConcMap Xr F X|A}
   end
end

local Xs Ys in
   Xs=a|b|c|d|_
   thread Ys={ConcMap Xs fun {$ X A} X|A end nil} end
   {Browse Ys} % prints accumulated list [a]|[b a]|[c b a]|[d c b a]|_
end

it is unclear to me how to proceed further

scsi
  • 13
  • 3

1 Answers1

0

I will answer my question myself, maybe it will be useful for someone. However, it would be interesting to see better solutions to the same problem.

declare

fun {Accumulate Xs A}
   case Xs of nil then A
   [] X|Xr then thread X|A end|{Accumulate Xr X|A}
   end
end

fun {MyMember N Xs}
   case Xs of nil then false
   [] X|Xr then
      if N==X then true
      else {MyMember N Xr}
      end
   end
end
   
fun {Count Xs Y N}
   case Xs of nil then N
   [] X|Xr then
      if X==Y then {Count Xr Y N+1}
      else {Count Xr Y N}
      end
   end
end

fun {BuildL Xs Ys}
   case Xs of nil then nil
   [] X|Xr then Z in
      thread Z={Count Ys X 0} end
      if {MyMember X Xr} then {BuildL Xr Ys}
      else thread X#Z end|{BuildL Xr Ys} end
   end
end

fun {CMap Xs Ys F}
   case Xs of nil then nil
   [] X|Xr then thread {F X X} end|{CMap Xr Ys F}
   end
end

fun {Counter Xs}
   local Ys Zs in
      thread Ys={Accumulate Xs nil} end
      thread Zs={CMap Ys Ys BuildL} end
      Zs
   end
end

local Ins in
   {Browse {Counter Ins}}
   Ins=a|b|a|c|_
end
scsi
  • 13
  • 3