我试图理解GLSL中的dFdx和dFdy函数.
我理解以下内容:
我不明白变化率是指什么.它是片段坐标的变化率吗?
可能是你可以在片段着色器的两个调用之间找到任意变量的变化率吗?着色器调用是否从相邻调用中"读取"变量?对于(简单)示例:
// invokation for fragment 1
float x = 1.0;
float d = dFdx(x);
// invokation for fragment next to fragment 1 along the x axis.
float x = 2.0;
float d = dFdx(x);
Run Code Online (Sandbox Code Playgroud)
d分别为-1.0和1.0?
我想从字符串中读取比率,但我不希望我的程序在分母为零时崩溃。如何检测零分母并避免错误?只是使用readMaybe
不起作用:
Prelude Text.Read> readMaybe "1 % 0" :: Maybe Rational
Just *** Exception: Ratio has zero denominator
Run Code Online (Sandbox Code Playgroud)
我创造了这个远非完美的解决方案:
readMaybeRational :: String -> Maybe Rational
readMaybeRational s =
case ((readMaybe $ drop 1 $ dropWhile (/='%') s) :: Maybe Int)
of Just 0 -> Nothing
_ -> readMaybe s
Run Code Online (Sandbox Code Playgroud)
但我不知道如何很好地处理嵌套的比率:
"Just (1 % 0)"
Run Code Online (Sandbox Code Playgroud)
如果我可以覆盖 Ratio 的 Read 实例,当分母为零时,我可以让 readMaybe 返回 Nothing :
instance (Integral a, Read a) => Read (Ratio a) where
readPrec =
parens
( prec ratioPrec …
Run Code Online (Sandbox Code Playgroud) 如果我创建一个新类型:
newtype Bloo a = Bloo a
Run Code Online (Sandbox Code Playgroud)
并使其成为函子
instance Functor Bloo where
fmap f (Bloo a) = Bloo $ f a
Run Code Online (Sandbox Code Playgroud)
将要
fmap f (Bloo a)
Run Code Online (Sandbox Code Playgroud)
转换为
f a
Run Code Online (Sandbox Code Playgroud)
在编译的程序中?
本页指出 Random 类的最小完整定义是“Nothing”
\n但如果我不提供它们,它们就不起作用:
\ndata Color = Red | Blue deriving (Bounded, Show)\n\ninstance Random Color\n
Run Code Online (Sandbox Code Playgroud)\n产生编译器警告:
\ntest.hs:5:10: warning: [-Wmissing-methods]\n \xe2\x80\xa2 No explicit implementation for\n \xe2\x80\x98randomR\xe2\x80\x99 and \xe2\x80\x98random\xe2\x80\x99\n \xe2\x80\xa2 In the instance declaration for \xe2\x80\x98Random Color\xe2\x80\x99\n |\n5 | instance Random Color\n
Run Code Online (Sandbox Code Playgroud)\n当我运行代码时出现错误:
\n*Main> let g = mkStdGen 100\n*Main> (fst $ random g) :: Color\n*** Exception: test.hs:5:10-21: No instance nor default method for class operation random\n
Run Code Online (Sandbox Code Playgroud)\nrandom
为什么和没有randomR
列在最小完整定义下?
我有一个Trait,一个Companion对象和一个Scala中的类:
trait A {
protected var foo = "Foo"
}
object B extends A {
}
class B {
println(B.foo)
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能访问foo?我以为foo会成为对象"B"的一个字段.有没有办法做到这一点?
我测试了一些想法在这篇文章.
我想为Term类型派生一个Eq实例:
{-# LANGUAGE DeriveFunctor #-}
data Tree a = Branch Int [a] | Leaf Int deriving (Eq, Functor, Show)
data Term f = Term (f (Term f)) deriving (Eq)
Run Code Online (Sandbox Code Playgroud)
但得到这个错误:
No instance for (Eq (f (Term f)))
arising from the first field of ‘Term’ (type ‘f (Term f)’)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Eq (Term f))
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个独立的派生实例:
{-# LANGUAGE DeriveFunctor #-} …
Run Code Online (Sandbox Code Playgroud) 我有这三个功能
a :: Int -> Maybe Int
a i = if i < 100 then Just i else Nothing
b :: Int -> Maybe Int
b i = if i < 50 then Just i else Nothing
c :: Int -> Maybe Int
c i = if i > 0 then Just i else Nothing
Run Code Online (Sandbox Code Playgroud)
我想将它们链接在一起,以便当一个函数的结果导致Nothing
该函数的输入被返回时.
我可以用这个功能实现这个目的:
import Data.Maybe (fromMaybe)
e :: Int -> [Int -> Maybe Int] -> Int
e i [] = i
e i (f:fs) …
Run Code Online (Sandbox Code Playgroud) 我想将以下函数映射到 a 的键上 Map
f :: a -> Maybe b
Run Code Online (Sandbox Code Playgroud)
并丢弃Nothing
密钥并保留Just
密钥,但从Just
. 就像Map.mapMaybe
,但对于键
mapMaybeKeys :: (a -> Maybe b) -> Map a c -> Map b c
Run Code Online (Sandbox Code Playgroud)
我在 Hoogle 中搜索了这种类型的签名,但没有找到任何东西。
我可以这样做:
mapMaybeKeys f
= Map.toList
. catMaybes
. fmap (fmap swap . traverse f . swap)
. Map.toList
Run Code Online (Sandbox Code Playgroud)
或者:
mapMaybeKeys f
= Map.mapKeys fromJust
. Map.delete Nothing
. Map.mapKeys f
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式?
我想使用 do 表示法来组合伪随机值:
g :: StdGen
g = mkStdGen 100
example1 :: Bool
example1
= fst
$ runState
(do x <- state (uniformR (False,True))
y <- state (uniformR (False,True))
return $ x == y
)
g
Run Code Online (Sandbox Code Playgroud)
函数uniformR
是根据 System.Random.Stateful 模块定义的:
uniformR :: (RandomGen g, UniformRange a) => (a, a) -> g -> (a, g)
uniformR r g = runStateGen g (uniformRM r)
Run Code Online (Sandbox Code Playgroud)
因此,在我的示例中,创建并运行状态似乎很愚蠢uniformR
,只有我的示例再次创建并运行状态。
有没有办法使用 System.Random.Stateful 和 do 表示法重写示例 1?
这是我唯一可以开始工作的事情(这很荒谬):
example3 :: Bool
example3
= fst
$ …
Run Code Online (Sandbox Code Playgroud) 如何获取当前绑定的顶点数组对象的名称?
我查看了手册,但找不到与glGet()一起使用的枚举.