Janiczek / elm-list-cartesian

Specialized List `mapN` and `andMap` functions that give all combinations of list elements (Cartesian product) instead of zipping them

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Janiczek/elm-list-cartesian

Specialized List mapN and andMap functions that give all combinations of list elements (Cartesian product) instead of zipping them

The List.mapN functions and the List.Extra.andMap function are of the zipping variety: they combine the first elements of all lists, then the second elements, then the third ones and so on, and stop as soon as any list runs out of items.

List.map2 (+) [ 1, 2 ] [ 100, 200, 300 ]
--> [ 101, 202 ]

The functions in the List.Cartesian module instead give you the Cartesian product -- all combinations of the list elements you provide:

List.Cartesian.map2 (+) [ 1, 2 ] [ 100, 200, 300 ]
--> [ 101, 201, 301, 102, 202, 302 ]

The functions in the List.Zip module are just aliases of the elm/core functions and only serve a didactic purpose. But they might be useful to make your code more explicit if you need to use both variants in the same file!

import List.Cartesian
import List.Zip


myFn list1 list2 =
    let
        allCombinations : List (Int, Int)
        allCombinations =
            List.Cartesian.map2 Tuple.pair list1 list2

        pairings : List (Int, Int)
        pairings =
            List.Zip.map2 Tuple.pair list1 list2
    in
    -- ...

About

Specialized List `mapN` and `andMap` functions that give all combinations of list elements (Cartesian product) instead of zipping them

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Elm 100.0%