Skip to content
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

add rotateLeft and rotateRight #98

Open
pzp1997 opened this issue May 1, 2018 · 3 comments
Open

add rotateLeft and rotateRight #98

pzp1997 opened this issue May 1, 2018 · 3 comments

Comments

@pzp1997
Copy link
Contributor

pzp1997 commented May 1, 2018

I think it would be good to add functions for rotating a matrix by 90 degrees. Here are possible implementations of the functions:

rotateLeft : List (List a) -> List (List a)
rotateLeft =
    List.reverse << transpose
rotateRight : List (List a) -> List (List a)
rotateRight listOfLists =
    List.foldl (List.map2 (::)) (List.repeat (rowsLength listOfLists) []) listOfLists

I omitted the helpers here for brevity, but made an Ellie with the full implementations and tests https://ellie-app.com/cyQ6L4kgta1/2. (Note that transpose and rowsLength are functions that I wrote that are already part of List.Extra.)

As for my use case, I was making a Tic-Tac-Toe game last night for fun and it turns out that having either of these functions make checking for a win very easy. Check it out! https://ellie-app.com/3zjbyv2y6a1/2 (scroll down to the checkStatus function).

One other thing worth discussing is the names. I do not think that the current ones make it clear that we are working with matrices and not 1-dimensional lists. For example, rotateRight [1, 2, 3, 4] might be misunderstood to be [4, 1, 2, 3] (which might also be a helpful function?).

@Qata
Copy link

Qata commented Jul 12, 2018

I came here looking for discussion of a 1-dimensional rotate function, so it'd definitely be helpful if that was also included!

@Chadtech
Copy link
Collaborator

Chadtech commented Jul 18, 2018

Nice @pzp1997 ! Looks good to me. Lets do it

But regarding the name, I can also see how its confusing. Maybe rotateMatrixRight instead?

@Chadtech
Copy link
Collaborator

@Qata A 1-dimensional rotate also sounds good. Someone just needs to make the case for it, show how it helps in practice, and then a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants