Skip to content

Instantly share code, notes, and snippets.

@yuanw
Last active April 14, 2017 13:56
Show Gist options
  • Select an option

  • Save yuanw/bca233bc8e393abc6b8e91e8a6f20092 to your computer and use it in GitHub Desktop.

Select an option

Save yuanw/bca233bc8e393abc6b8e91e8a6f20092 to your computer and use it in GitHub Desktop.
Beginning Haskell Chapter 6
import Data.List
import qualified Data.Map as M
-- Data.List.minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a --
-- M.adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a --
clusterAssignmentPhase :: (Vector v, Vectorizable e v) => [v] -> [e] -> M.Map v [e]
clusterAssignmentPhase centroids points =
let initialMap = M.fromList $ zip centroids (repeat [])
in foldr (\p m -> let chosenCentroid = minimumBy (\x y -> compare (distance x $ toVector p)
(distance y $ toVector p))
centroids
in M.adjust (p:) chosenCentroid m)
initialMap points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment