Skip to content

Instantly share code, notes, and snippets.

@knewter
Forked from hoelzro/Main.elm
Last active January 27, 2024 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knewter/2fd9225abda2984229849f5e86657d70 to your computer and use it in GitHub Desktop.
Save knewter/2fd9225abda2984229849f5e86657d70 to your computer and use it in GitHub Desktop.
Bug in elm-lang/core `Dict.merge`?

This is an example of what I believe is a bug in core's Dict.merge implementation.

To reproduce

Expected outcome

Dict should contain both u1 and u2 keys.

Actual outcome

Dict only contains u1 key.

Discussion

Am I missing something here? It's the first time I've used Dict.merge. I note that there are no tests in core for this function

What am I doing now?

I've pulled the function in locally to the module and am adding debugging statements to see if I can determine where things go south.

module Main exposing (..)
import Html.App as App
import Html exposing (Html, text)
import Dict exposing (Dict)
type alias Model =
()
type alias Msg =
()
existsInBoth : comparable -> List a -> List a -> Dict comparable (List a) -> Dict comparable (List a)
existsInBoth key leftValue rightValue dict =
Dict.insert key (leftValue ++ rightValue) dict
magicMerge : Dict comparable (List a) -> Dict comparable (List a) -> Dict comparable (List a)
magicMerge left right =
Dict.merge Dict.insert existsInBoth Dict.insert left right Dict.empty
init : ( Model, Cmd Msg )
init =
( (), Cmd.none )
update : Msg -> Model -> ( Model, Cmd Msg )
update _ _ =
( (), Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
view : Model -> Html Msg
view model =
let
-- this is input that behaves as desired...not important here, but perhaps as justification for the use of merge in the first place
-- result =
-- magicMerge (Dict.empty |> Dict.insert "a" [ 1 ]) (Dict.empty |> Dict.insert "a" [ 2 ] |> Dict.insert "b" [ 3 ])
result =
magicMerge (Dict.empty |> Dict.insert "u1" [ 1 ])
(Dict.empty |> Dict.insert "u2" [ 1 ])
in
text <| toString <| result
main : Program Never
main =
App.program
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment