Int -> [Cluster] -> [Point] -> Vector PointSum
                  
                  
                  assign nclusters clusters points = Vector.create $ do
                  
                  
                    vec <- MVector.replicate nclusters (PointSum 0 0 0)
                  
                  
                    let addpoint p = do
                  
                  
                          let c   = nearest p
                  
                  
                              cid = clId c
                  
                  
                          ps <- MVector.read vec cid
                  
                  
                          MVector.write vec cid $! addToPointSum ps p
                  
                  
                  
                  
                  
                    mapM_ addpoint points
                  
                  
                    return vec
                  
                  
                   where
                  
                  
                    nearest p = fst $ minimumBy
                  
                  
                      (compare `on` snd)
                  
                  
                      [ (c, sqDistance (clCent c) p) | c <- clusters ]
                  
                  
                  
                  
                  
                  И коммент:
                  
                  
                  Given a set of clusters and a set of points, the job of assign is to decide, for each point,
                  
                  
                  which cluster is closest. For each cluster, we build up a PointSum of the points that were
                  
                  
                  found to be closest to it. The code has been carefully optimized, using mutable vectors
                  
                  
                  from the vector package; the details aren’t important here.
                  
                  
                  
                  
                  
                  Зачем здесь оптимизация через мутабельность? Ghc разве не оптимизирует операции со списком в данном случае (не создавая новый каджый раз)?
                  
                  
                  Как понять, когда нужно делать такую оптимизацию, когда — нет?
                  
                  
                
тут же random access по индексу, как оптимизировать?
 Wsevolod
                          
                        
                      
                    
                    
                    
                    
                      Автор вопроса
                      
                      
                        
                          Wsevolod
                          
                        
                      
                    
                    
                    
                    
                      Автор вопроса
                    
                    
                  Почему не использовать, к примеру, Data.Set из containers?
Обсуждают сегодня