about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <git@lukasepple.de>2017-09-27 22:45:02 +0200
committersternenseemann <git@lukasepple.de>2017-09-27 22:45:02 +0200
commitcaaab45600741dace81a7cd6490eae5383dd68c4 (patch)
treeca6aabef3664590dcecc8cccbf8e50794b0947cd
parent84618f1e686a72dab43f8aa68d6b78309241d349 (diff)
Instead of throwing an error, if an edge is missing, just terminate
-rw-r--r--lib/Sound/Likely.hs20
1 files changed, 11 insertions, 9 deletions
diff --git a/lib/Sound/Likely.hs b/lib/Sound/Likely.hs
index 370209a..f8766f4 100644
--- a/lib/Sound/Likely.hs
+++ b/lib/Sound/Likely.hs
@@ -71,17 +71,19 @@ interpretation gen graph n = (nMusic n) :+:
   recurse (fromMaybe S.empty (M.lookup n (unGraph graph)))
   where (prob, gen') = randomR (0.0, 1.0) gen
         recurse edges =
-          if S.null edges
-            then emptyMusic
-            else interpretation gen' graph
-                 . eTo . edgeForRoll prob $ edges
+          case edgeForRoll prob edges of
+            Nothing -> emptyMusic
+            Just nextEdge ->
+              interpretation gen' graph . eTo $ nextEdge
 
-edgeForRoll :: Probability -> S.Set Edge -> Edge
+edgeForRoll :: Probability -> S.Set Edge -> Maybe Edge
 edgeForRoll prob set =
-  let curr = S.elemAt 0 set
-    in if prob <= eProb curr
-         then curr
-         else edgeForRoll (prob - eProb curr) (S.delete curr set)
+  if S.null set
+    then Nothing
+    else let curr = S.elemAt 0 set
+           in if prob <= eProb curr
+                then Just curr
+                else edgeForRoll (prob - eProb curr) (S.delete curr set)
 
 emptyMusic :: Music a
 emptyMusic = Prim (Rest 0)