--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
parallel : List (Task error value) -> Task error (List value)
You could use spawn also. Each thread sends to the same mailbox, and you collect them in the signal graph.
parallel : Address a -> List (Task error a) -> Task error (List ThreadID)parallel address tasks = let sendToAddress task = spawn (task `andThen` send address) in sequence (List.map sendToAddress tasks)parallel = sequence << List.map spawnWe could do better by implementing a native MVar primitive, which would allow inter-"thread" communication, enabling the construction of proper async combinators.
We could do better by implementing a native MVar primitive, which would allow inter-"thread" communication, enabling the construction of proper async combinators.
foldT : (a -> b -> Task x b) -> b -> List a -> Task x b
foldT f value list = case list of
[] -> succeed value
x :: xs -> f x value `andThen` (\value' -> foldT f value' xs)
type alias Dispatcher a x =
{ batchSize : Int,
count : Int,
list : List a,
process : List a -> Task x ()
}
makeDispatcher : (List a -> Task x ()) -> Int -> Dispatcher a x
makeDispatcher process batchSize = Dispatcher batchSize 1 [] process
dispatchFlush : Dispatcher a x -> Task x ()
dispatchFlush d = d.process (List.reverse d.list)
dispatch : a -> Dispatcher a x -> Task x (Dispatcher a x)
dispatch message d =
let
list = message :: d.list
in
if d.batchSize == d.count then
d.process (List.reverse list)
`andThen` (\_ -> succeed (Dispatcher d.batchSize 1 [] d.process))
else
succeed (Dispatcher d.batchSize (d.count + 1) list d.process)
progressivelyDispatch : (List a -> Task x ()) -> Int -> List a -> Task x ()
progressivelyDispatch process batchSize list =
foldT dispatch (makeDispatcher process batchSize) list
`andThen` dispatchFlush
progressivelyGetStories : List Int -> Task Error ()
progressivelyGetStories list =
progressivelyDispatch (processStories newStoryMailbox.address) 10 list
processStories : Signal.Address (Maybe Story) -> List Int -> Task Http.Error ()
processStories address list =
sequence (List.map getStory list)
`andThen` (sequence << List.map (send newStoryMailbox.address << Just))
`andThen` \_ -> succeed ()
spawn : Task x a -> Task y (ThreadID x a)
await : ThreadID x a -> Task x aparallel : List (Task x a) -> Task y (List (ThreadID x a))
parallel = sequence << List.map spawn
processStories : Signal.Address (Maybe Story) -> List Int -> Task Http.Error ()
processStories address list =
parallel (List.map getStory list)
`andThen` (sequence << List.map await)
`andThen` (sequence << List.map (send newStoryMailbox.address << Just))
`andThen` \_ -> succeed ()--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.