[英]Applicative functors and records in purescript
In Haskell, I'm used to doing stuff like this.在Haskell,我习惯做这样的事情。
data Foo = Foo { foo :: String, bar :: String }
mFoo :: (Monad m) => m String -> m String -> m Foo
mFoo foo bar = Foo <$> foo <*> bar
The same does not work in purescript, however.但是,这在 purescript 中不起作用。 Is there a way to achieve the same outcome, ie keep the record syntax while allowing partial application via applicative functors when building the instance?有没有办法实现相同的结果,即在构建实例时保留记录语法,同时允许通过应用函子进行部分应用?
Thanks!谢谢!
In PureScript you don't have to define such a record type upfront.在 PureScript 中,您不必预先定义这样的记录类型。 Polymorphic version:多态版本:
mXY :: forall a b m. Apply m => m a -> m b -> m { x :: a, y :: b }
mXY foo bar = { x: _, y: _ } <$> foo <*> bar
-- | which can be rewritten using lift2:
mXY' = Control.Apply.lift2 { x: _, y: _ }
And a monomoprhic equivalent:和一个等价的:
type Foo = { x :: String, y :: String }
mFoo :: forall m. Apply m => m String -> m String -> m Foo
mFoo = Control.Apply.lift2 {x: _, y: _ }
A "live demo" using wonderful try.purescript.org + custom gist:使用精彩的 try.purescript.org + 自定义要点的“现场演示”:
https://try.purescript.org/?gist=a37f5f0c50e0640e34ea5a4788c0c999 https://try.purescript.org/?gist=a37f5f0c50e0640e34ea5a4788c0c999
Of course there is a practical value in using a newtype
around Record
too like:当然,在newtype
周围使用新Record
也有实际价值,例如:
newtype Foo' = Foo' { x :: String, y :: String }
and in such a case I would propose something like:在这种情况下,我会提出类似的建议:
mFoo'' foo bar = map Foo' $ { x: _ , y: _ } <$> foo <*> bar
but I think that this could be expressed in a more elegant and shorter way which I don't know:-)但我认为这可以用我不知道的更优雅和更短的方式来表达:-)
EDIT:编辑:
This is probably even nicer syntax using ado
:这可能是使用ado
更好的语法:
ado
x <- foo
y <- bar
in Foo { x, y }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.