purescript / purescript-tailrec Goto Github PK
View Code? Open in Web Editor NEWA type class which captures stack-safe monadic tail recursion
License: BSD 3-Clause "New" or "Revised" License
A type class which captures stack-safe monadic tail recursion
License: BSD 3-Clause "New" or "Revised" License
This library gets installed when i try to install purescript-arrays
with pulp 10 and psc 0.10.2, but it fails during compilation with the following error:
purescript-invariant#2.0.0 bower_components/purescript-invariant
└── purescript-prelude#2.5.0
* Building project in /home/francesco/repos/crumbs
Compiling Control.Monad.Rec.Class
Error found:
in module Control.Monad.Rec.Class
at /home/francesco/repos/crumbs/bower_components/purescript-tailrec/src/Control/Monad/Rec/Class.purs line 128, column 8 - line 128, column 31
Unknown value U.unsafeCoerceEff
See https://github.com/purescript/purescript/wiki/Error-Code-UnknownName for more information,
or to contribute content related to this error.
* ERROR: Subcommand terminated with exit code 1
Probably i'm doing something obviously wrong like using the wrong compiler version or something similar, but i'm not sure. The version of purescript-arrays
that my Bower client is trying to install is the most recent, 3.2.1
Would this work?
instance monadEffectRec :: MonadEffect m => MonadRec m where
tailRecM = liftEffect <<< tailRecM
In sanctuary-type-classes we have made function instance of MonadRec so it could be implemented here too.
https://github.com/purescript/purescript-tailrec/blob/master/src/Control/Monad/Rec/Class.purs#L126
The reference to a
will be unnecessarily held while the loop is run.
I have defined this helpers in different projects, and others have done the same too, for example untilJust
is same as loop, filtered,untilSuccessful. I would like to add this basic and useful helpers to this lib:
-- | While supplied computation evaluates to `Just _`, it will be
-- | evaluated repeatedly and results will be combined using monoid instance.
whileJust :: forall a m. Monoid a => MonadRec m => m (Maybe a) -> m a
whileJust m = tailRecM (\v -> m <#> maybe (Done v) (Loop <<< (v <> _))) mempty
-- | Supplied computation will be executed repeatedly until it evaluates
-- | to `Just value` and then that `value` will be returned.
untilJust :: forall a m. MonadRec m => m (Maybe a) -> m a
untilJust m = tailRecM (\_ -> m <#> maybe (Loop unit) Done) unit
They are defined in here too http://hackage.haskell.org/package/monad-loops-0.4.3/docs/Control-Monad-Loops.html, There is also it's PS version of this lib, but looks like it's unmaintained https://github.com/mlang/purescript-monad-loops
One argument against adding this funcitons is that it will open can of warms and we would wanna add more and more later and it should instead be done in separate purescript-monad-loops like lib.
I get that the that point, but I think adding whileJust
and untilJust
will be worthwhile as they are useful and sort of basic constructs.
to convince why I think {while,until}Just are basic functions see this implementations:
whileM :: forall a m. MonadRec m => Monoid a => m Boolean -> m a -> m a
whileM mCondition mRes = whileJust do
condition <- mCondition
if condition then mRes <#> Just else pure $ Nothing
untilM :: forall a m. Monoid a => MonadRec m => m a -> m Boolean -> m a
untilM mRes mCondition = whileJust do
res <- mRes
condition <- mCondition
pure $ if condition then Nothing else Just res
iterateWhile :: forall a m. MonadRec m => (a -> Boolean) -> m a -> m a
iterateWhile p = iterateUntil $ not p
iterateUntil :: forall a m. MonadRec m => (a -> Boolean) -> m a -> m a
iterateUntil p x = x >>= iterateUntilM p (const x)
iterateUntilM :: forall a m. MonadRec m => (a -> Boolean) -> (a -> m a) -> a -> m a
iterateUntilM p f = execStateT $ whileM (gets p) (get >>= f >>> lift >>= put)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.