在过去的几周里,我一直在为一个将 monads(主要来自mtl
)移植到 arrows的库做出贡献。
这是一个StateT
来自 monad的快速示例mtl
:
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
-- arrowization -->
newtype StateTA s a b c = StateTA { runStateTA :: a (b, s) (c, s) }
Run Code Online (Sandbox Code Playgroud)
对于大多数 monad 来说,“箭头化”过程并不是很痛苦,但我无法根据 Store comonad 找出我的箭头。
每次我问这个问题时,人们都会将我重定向到Cokleisli
箭头,但将Cokleisli Store
等同于我正在寻找的箭头?
该库基于一种mtl
风格的体系结构(每个箭头都有一个通用类,如ArrowState
、ArrowReader
等...),我试图弄清楚我的函数在 中的签名是什么ArrowStore
,但同样,我不能。
我查看了arrows
实现与我正在研究的库中相同箭头的包,但是它们的CoState
箭头(我听说 CoState 是 Store 的另一个名称)定义了没有操作,所以我猜作者也遇到了这个问题.
tl;博士:
我有一个情节,我从点到点画箭头.我想把这个箭头放在线的末端,而不是中间.有没有一种简单的方法可以做到这一点,除了放置额外的箭头与相应的一半的长度?我的代码是这样的:
plot(x, y, xlim=range(x), ylim=range(y), xlab="x", ylab="y", pch=16,
main="Filled Plane")
for(i in 1:20){
arrows(x[i], y[i], x[i+1], y[i+1], length = 0.25, angle = 30, col = i)
}
Run Code Online (Sandbox Code Playgroud) 我试图理解箭头并创建以下示例:
{-# LANGUAGE Arrows #-}
module Main where
import Control.Arrow
import Control.Monad
import qualified Control.Category as Cat
import Data.List
import Data.Maybe
data IOArrow a b = IOArrow { runIOArrow :: a -> IO b }
instance Cat.Category IOArrow where
id = IOArrow return
IOArrow f . IOArrow g = IOArrow $ f <=< g
instance Arrow IOArrow where
arr f = IOArrow $ return . f
first (IOArrow f) = IOArrow $ \(a, c) -> do
x <- f a …
Run Code Online (Sandbox Code Playgroud) 我想用 R 中的绘图来表示以原点为根的 2D 向量。此外,我想根据分类变量为向量着色。问题是我可以创建颜色编码但没有箭头的线条:
library(plotly)
library(dplyr)
v <- c(1, 1)
b1 <- c(1, 0)
b2 <- c(0, 1)
df <- data.frame(
x = c(v[1], b1[1], b2[1]),
y = c(v[2], b1[2], b2[2]),
is_basis = c(FALSE, TRUE, TRUE)
)
df %>%
plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
add_segments(xend = ~x, yend = ~y, x = 0, y = 0, colors = c("red","black"))
Run Code Online (Sandbox Code Playgroud)
或者使用箭头但不使用颜色编码:
df %>%
plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
add_annotations(x = …
Run Code Online (Sandbox Code Playgroud) 我试着写下来joinArr :: ??? a => a r (a r b) -> a r b
。我想出了一个使用 的解决方案app
,因此将范围缩小a
到ArrowApply
's:
joinArr :: ArrowApply a => a r (a r b) -> a r b
joinArr g = g &&& Control.Category.id >>> app
Run Code Online (Sandbox Code Playgroud)
是否可以为箭头写下此功能?
我的猜测是否定的。
Control.Monad.join
可能>>=
是Monad
类型类定义中的一个很好的替代品:m >>= k = join $ k <$> m
.
有了joinArr :: Arrow a => a r (a r b) (a r b)
我们的手,就可以写下instance Arrow …
我正在使用Haskell和使用箭头语言扩展的Yampa FRP库.
如何在SF中执行简单的putStrLn?
mySF = proc x -> do
y <- identity -< x*x
putStrLn "Hello World!" ++ show y
returnA -< y
Run Code Online (Sandbox Code Playgroud)
箭头语法抱怨表达式不是箭头(当然),但即使使用箭头我也没有输出
output <- identity -< putStrLn "Hello World!"
Run Code Online (Sandbox Code Playgroud) 如果我有以下两个Kleisli箭头:
stdoutProcessA :: Kleisli Maybe String (IO String)
writeToFileA :: Kleisli Maybe (FilePath, String) (IO ())
Run Code Online (Sandbox Code Playgroud)
我希望能够写出像:
compile = proc src -> do
output <- stdoutProcessA -< "..."
writeToFileA -< ("...", output)
...
Run Code Online (Sandbox Code Playgroud)
这当然不起作用,因为String
不匹配IO String
.另一方面,可以定义两者stdoutProcessA
和writeToFileA
类型Kleisli IO ...
,但是我不能用类型的箭头组合它们Kleisli Maybe ...
,这是我需要的其他东西.
我对箭头还不是很有经验,所以我可能会遗漏一些明显的东西.如何进行上述操作?
前几天我被告知我不应该在c中以某种方式使用指针箭头.我做的是这样的:
struct info {
int x;
char *data;
}
int main() {
struct info *information;
information -> x = 0; /*Notice the spacing here between information and -> x*/
information -> data = "";
}
Run Code Online (Sandbox Code Playgroud)
我经常看到的
struct info *information;
information->x = 0;
Run Code Online (Sandbox Code Playgroud)
我只是想问一下,这只是一个常规的编码标准吗?我觉得 - >比p->东西更清洁.
我最近有想法建立一个计算计算经过的绑定数量的monad.我想出了以下内容:
newtype K a = K { runK :: Int -> (a, Int) }
instance Functor K where
fmap f k = K $ \s ->
let (iv, s') = runK k s
in (f iv, s')
instance Applicative K where
pure x = K $ \s -> (x, s)
f <*> b = K $ \s ->
let (f', s') = runK f s
(ih, s'') = runK b s'
in (f' ih, s'')
instance Monad K where
return …
Run Code Online (Sandbox Code Playgroud) 我有以下类型:
data S req rsp = Done rsp | Next req (rsp -> S req rsp)
Run Code Online (Sandbox Code Playgroud)
我们的想法是将其用作网络通信的纯粹表示,即:
... Next GetUser $ \uid -> Next (Login uid) $ \success -> Done success
Run Code Online (Sandbox Code Playgroud)
然后由一些不纯的函数评估eval
.
现在,这是什么(如果有的话?)就我所见,这不是一个单子,也不是一个箭头.它似乎是stream/pipe/automaton/fsm和continuation monad之间的东西.这让我觉得这种类型的东西可能有更好的代表性,但是什么呢?