I have a dictionary which contains a series of keys with names "ls-1", 
"ls-2", "ls-3", etc. and a key "ls-count" stating how many there are. When 
one of these is deleted, say "ls-2" is deleted, I want to bubble the 
remainder up so that the old "ls-3" will become "ls-2" and so on.

The code being used for this at the moment is:

updateDeleteField : String -> Model -> Model
updateDeleteField key model =
  let
    keyhyphenindex = String.indexes "-" key
    realKeyHyphenIndex = case (List.head keyhyphenindex) of
      Nothing -> Debug.crash "Missing hyphen in expandable form deletion 
process"
      Just x -> x
    keyPrehyphen = (String.slice 0 realKeyHyphenIndex key) ++ "-"
    countName = Debug.log "name of the count variable:" (keyPrehyphen ++ 
"count")
    removeResponse = Debug.log "remove the deleted item:" (killResponse 
model key)
    reduceCount = Debug.log "reduce the count:" (setResponse removeResponse 
countName (toString ((getResponseInt removeResponse countName 0) - 1)))
    -- Integrity bubble: check if we've reached the end of the list, if so 
we're done
    checkInteg value m = if value >= ((getResponseInt m countName 0)) then 
Debug.log "Completing.." m else
      let
        active = Debug.log "active" (keyPrehyphen ++ (toString value)) -- 
Name of the current item
        next = Debug.log "next" (keyPrehyphen ++ (toString (value+1))) -- 
Name of the next item
      in case (Dict.get active m.character) of
        -- If the current item is not missing, recurse onto the rest of the 
numbers
        Just x -> Debug.log ("Active is ok so moving on to " ++ (toString 
(value+1))) (checkInteg (value+1) m)
        -- If the current item is missing, look at the next one
        Nothing -> case (Dict.get next m.character) of
          -- If the next one is present, move it down to fill the current 
gap, then recurse
          Just higher -> Debug.log ("Active is missing, bubbling" ++ next 
++" to " ++ active) (checkInteg (value) (setResponse (killResponse m next) 
active higher))
          -- That being missing too should be impossible since only one 
thing can be deleted at a time
          Nothing -> Debug.crash ("More than one form item deleted at once? 
" ++ active ++ " unset, so is " ++ next ++ ", deleted key is " ++ key)
  in checkInteg 1 reduceCount

(killResponse m x deletes the tag key x from m.character, setResponse m x v 
sets the tag key x to v in m.character)

Everything up to checkInteg works fine, but at that point the debug output 
goes crazy. It'll often print the Completed message before any of the 
others, or just stop after bubbling for no reason, or say it is bubbling 
but then not change anything. Can anyone tell me what's wrong?


-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to