项目作者: harmboschloo

项目描述 :
Provides intersections of multiple dictionaries
高级语言: Elm
项目地址: git://github.com/harmboschloo/elm-dict-intersect.git
创建时间: 2019-03-02T09:58:50Z
项目社区:https://github.com/harmboschloo/elm-dict-intersect

开源协议:Other

下载


Dict.Intersect

Provides intersections of multiple dictionaries based on their key.

Example

  1. import Dict exposing (Dict)
  2. import Dict.Intersect
  3. type alias Car =
  4. { wheels : Int
  5. }
  6. type alias Boat =
  7. { speed : Float
  8. }
  9. type alias Amphibian =
  10. { car : Car
  11. , boat : Boat
  12. }
  13. cars : Dict Int Car
  14. cars =
  15. Dict.fromList [ ( 0, Car 4 ), ( 1, Car 4 ), ( 2, Car 6 ) ]
  16. boats : Dict Int Boat
  17. boats =
  18. Dict.fromList [ ( 2, Boat 20 ), ( 3, Boat 30 ) ]
  19. amphibians : List ( Int, Amphibian )
  20. amphibians =
  21. Dict.Intersect.foldr2
  22. (\id car boat list -> ( id, Amphibian car boat ) :: list)
  23. []
  24. cars
  25. boats
  26. -- amphibians == [ ( 2, { boat = { speed = 20 }, car = { wheels = 6 } } ) ]

Note on performance

When folding over multiple dictionaries it is generally faster to have the smallest dictionary as the first argument and the one with the least amount of intersections next. See these benchmarks for an example.