Skip to content

Commit

Permalink
Merge pull request #100 from purescript/span-tco
Browse files Browse the repository at this point in the history
Restore compiler-optimized TCO for span
  • Loading branch information
garyb authored Mar 29, 2017
2 parents e3abdd1 + a9dd884 commit 9a5770d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import Prelude
import Control.Alt ((<|>))
import Control.Alternative (class Alternative)
import Control.Lazy (class Lazy, defer)
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2, tailRec)
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2)
import Control.Monad.ST (pureST)
import Data.Array.ST (unsafeFreeze, emptySTArray, pushSTArray)
import Data.Array.ST.Iterator (iterator, iterate, pushWhile)
Expand Down Expand Up @@ -521,13 +521,13 @@ span p arr =
{ init: arr, rest: [] }
where
breakIndex = go 0
go = tailRec \i ->
go i =
-- This looks like a good opportunity to use the Monad Maybe instance,
-- but it's important to write out an explicit case expression here in
-- order to ensure that TCO is triggered.
case index arr i of
Just x -> if p x then Loop (i + 1) else Done (Just i)
Nothing -> Done Nothing
Just x -> if p x then go (i + 1) else Just i
Nothing -> Nothing

-- | Group equal, consecutive elements of an array into arrays.
-- |
Expand Down

0 comments on commit 9a5770d

Please sign in to comment.