Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
power: slight optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewleon committed Jan 28, 2018
1 parent 4df49ec commit 85986e2
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Data/Monoid.purs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ instance monoidArray :: Monoid (Array a) where
-- | For that, we would additionally need the ability to invert elements, i.e.
-- | a Group.
power :: forall m. Monoid m => m -> Int -> m
power x = go
power x p
| p <= 0 = mempty
| otherwise = go p
where
go :: Int -> m
go p
| p <= 0 = mempty
| p == 1 = x
| p `mod` 2 == 0 = let x' = go (p/2) in x' <> x'
| otherwise = let x' = go (p/2) in x' <> x' <> x
go p'
| p' == 1 = x
| p' `mod` 2 == 0 = let x' = go (p'/2) in x' <> x'
| otherwise = let x' = go (p'/2) in x' <> x' <> x'

-- | Allow or "truncate" a Monoid to its `mempty` value based on a condition.
guard :: forall m. Monoid m => Boolean -> m -> m
Expand Down

0 comments on commit 85986e2

Please sign in to comment.