New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for list multiplication like in Python #580

Closed
ebekebe opened this Issue Apr 29, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@ebekebe

ebekebe commented Apr 29, 2016

In Python you can write

2 * ['a', 'b']

which will result in ['a', 'b', 'a', 'b']. This is a useful operation for initializing large lists. For example, 100 * [0] will give me a zero vector with 100 dimensions. I used this method on a regular basis in Python and would love to see it included in Elm's basics.

I propose the following implementation draft

(**) : Int -> List a -> List a
(**) count list =
    if count < 2
    then list
    else case list of
        []     -> []
        [a]    -> a :: ( (**) (count-1) list )
        h::t   -> append list ( (**) (count-1) list )

This enables a similar syntax as in python and is fit for "piping":

2 **  ['a', 'b']
[ 'a', 'b'] |> (**) 2

What do you think? Would you like to see this in Elm, too?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Apr 29, 2016

Contributor

Please read https://github.com/elm-lang/core/blob/master/CONTRIBUTING.md#adding-new-functions and follow what is said there. That is, bring this up as an issue or pull request in https://github.com/elm-community/elm-list-extra first instead.

Contributor

jvoigtlaender commented Apr 29, 2016

Please read https://github.com/elm-lang/core/blob/master/CONTRIBUTING.md#adding-new-functions and follow what is said there. That is, bring this up as an issue or pull request in https://github.com/elm-community/elm-list-extra first instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment