Skip to content

Instantly share code, notes, and snippets.

@vgaltes
Created July 3, 2015 14:59
Show Gist options
  • Save vgaltes/342499b8c7f36f5eb4c7 to your computer and use it in GitHub Desktop.
Save vgaltes/342499b8c7f36f5eb4c7 to your computer and use it in GitHub Desktop.
Social network (temporal solution)
module SocialNetworkKata
let mutable timelines = Map.empty
let mutable followers = Map.empty
let post userName message =
let userTimeline = if (Map.containsKey userName timelines )
then Map.find userName timelines
else []
let newTimeline = message :: userTimeline
let newMap = Map.add(userName) newTimeline timelines
timelines <- newMap
let wall userName =
let filteredMap = Map.filter (fun key _ -> ( if Map.containsKey userName followers
then List.exists (fun elem -> elem = key) (Map.find userName followers)
else false ) || key = userName) timelines
Map.fold (fun state key value -> value@state) [] filteredMap
let follow follower followed =
let userFollows = if (Map.containsKey follower followers )
then Map.find follower followers
else []
let newFollows = followed :: userFollows
let newMap = Map.add(follower) newFollows followers
followers <- newMap
let initializeSocialNetwork () =
timelines <- Map.empty
followers <- Map.empty
module Tests =
open NUnit.Framework
open Swensen.Unquote
[<Test>]
let ``Alice can post message to a personal timeline`` () =
initializeSocialNetwork ()
post "Alice" "Message 1 from Alice"
post "Alice" "Message 2 from Alice"
let aliceTimeline = wall "Alice"
test <@ ["Message 2 from Alice"; "Message 1 from Alice"] = aliceTimeline @>
[<Test>]
let ``Bob can post message to a personal timeline`` () =
initializeSocialNetwork ()
post "Bob" "Message 1 from Bob"
post "Bob" "Message 2 from Bob"
let aliceTimeline = wall "Bob"
test <@ ["Message 2 from Bob"; "Message 1 from Bob"] = aliceTimeline @>
[<Test>]
let ``Charlie can subscribe to Alice timeline`` () =
initializeSocialNetwork ()
post "Alice" "Message 1 from Alice"
post "Bob" "Message 1 from Bob"
follow "Charlie" "Alice"
let charlieWall = wall "Charlie"
test <@ ["Message 1 from Alice"] = charlieWall @>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment