扰流警报:这与Project Euler的问题14有关.
以下代码需要大约15秒才能运行.我有一个在1s内运行的非递归Java解决方案.我想我应该能够更接近这个代码.
import Data.List
collatz a 1 = a
collatz a x
| even x = collatz (a + 1) (x `div` 2)
| otherwise = collatz (a + 1) (3 * x + 1)
main = do
print ((foldl1' max) . map (collatz 1) $ [1..1000000])
Run Code Online (Sandbox Code Playgroud)
我已经分析过,+RHS -p并注意到分配的内存很大,并随着输入的增长而增长.对于n = 100,0001gb分配(!),分配n = 1,000,00013gb(!!).
然后再次-sstderr表明,尽管分配了大量字节,但总内存使用量为1mb,生产率为95%+,因此13gb可能是红鲱鱼.
我可以想到几个可能性:
有些事情并不像它需要的那么严格.我已经发现了
foldl1',但也许我需要做更多的事情?是否有可能标记collatz
为严格(甚至有意义吗?
collatz不是尾部调用优化.我认为它应该是但不知道确认的方法.
编译器没有做我认为应该进行的一些优化 - 例如collatz,任何一次只需要在内存中的两个结果(最大值和当前值)
有什么建议?
这几乎是一个重复的 …
我试图将活塞纹理存储在结构中.
struct TextureFactory<R> where R: gfx::Resources {
block_textures: Vec<Rc<Texture<R>>>,
}
impl<R> TextureFactory<R> where R: gfx::Resources {
fn new(window: PistonWindow) -> Self {
let texture = Rc::new(gfx_texture::Texture::from_path(
&mut *window.factory.borrow_mut(),
"assets/element_red_square.png",
Flip::None, &TextureSettings::new()
).unwrap());
let block_textures = Vec::new();
block_textures.push(texture);
TextureFactory {
block_textures: block_textures,
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不编译:
src/main.rs:37:9: 39:10 error: mismatched types:
expected `TextureFactory<R>`,
found `TextureFactory<gfx_device_gl::Resources>`
(expected type parameter,
found enum `gfx_device_gl::Resources`)
Run Code Online (Sandbox Code Playgroud)
gfx_device_gl::Resources gfx::Resources虽然实现(我认为它只是设备特定的实现.)我实际上并不关心这是什么类型,但我需要知道,以便我可以将它存储在结构中.
(我怀疑Rust的特征/特征:"预期'Foo <B>',发现'Foo <Foo2>'"是同一个问题,但我无法弄清楚如何将它应用到我的问题中.)
有没有更惯用的方式来实现以下内容?我觉得我错过了摆脱lambda的方法,但无法找到将其转换为无点的方法.也许还有另一种非适用的方式更直接?
import Data.Maybe
import Control.Applicative
foldl (\x y -> pure (+) <*> x <*> y) (Just 0) [Just 3, Just 4]
-- Just 7
foldl (\x y -> pure (+) <*> x <*> y) (Just 0) [Just 3, Just 4, Nothing]
-- Nothing
Run Code Online (Sandbox Code Playgroud) HLint建议我使用forM_而不是forM.为什么?我看到他们有不同类型的签名,但没有找到一个很好的理由使用一个而不是另一个.
forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
Run Code Online (Sandbox Code Playgroud) 在开发模式下运行良好.
错误:
Uncaught TypeError: (void 0) is not a function
at main.aff8cee52d3bd3903f34.bundle.js:1
at Object.cDNt (main.aff8cee52d3bd3903f34.bundle.js:1)
at n (inline.0da255c51a7d5ae908f0.bundle.js:formatted:10)
at Object.0 (main.aff8cee52d3bd3903f34.bundle.js:1)
at n (inline.0da255c51a7d5ae908f0.bundle.js:formatted:10)
at window.webpackJsonp (inline.0da255c51a7d5ae908f0.bundle.js:formatted:25)
at main.aff8cee52d3bd3903f34.bundle.js:1
Run Code Online (Sandbox Code Playgroud)
发生错误的bundle.js代码:
webpackJsonp([1], {
....
wi = new yi.l("UseV4Plurals"), Ei = function() {}, Mi = function(e) {
function t(t, n) {
var r = e.call(this) || this;
return r.locale = t,
r.deprecatedPluralFn = n,
r
}
return (void 0)(t, e), <-------------- Error
Run Code Online (Sandbox Code Playgroud)
我的package.json文件:
{
"name": "angular-front-end",
"version": "0.0.0",
"license": "MIT",
"scripts": { …Run Code Online (Sandbox Code Playgroud) 我正在尝试在haskell中建模一些多态类型的数据.我理解为什么以下代码不起作用,但我希望它说明我正在尝试做什么.我的问题是:使用Haskell对此进行建模的惯用方法是什么?(如果有更好的方法,您不需要保持输入格式相同 - 我没有任何现有的代码或数据.)
data Running = Sprint | Jog deriving (Show)
data Lifting = Barbell | Dumbbell deriving (Show)
data Time = Time Integer deriving (Show)
data Pounds = Pounds Integer deriving (Show)
data TimedActivity = TimedActivity Running Time deriving (Show)
data WeightedActivity = WeightedActivity Lifting Pounds deriving (Show)
class Activity a
instance Activity TimedActivity
instance Activity WeightedActivity
-- I have a list of activities
main :: IO ()
main = putStrLn $ show [ TimedActivity Sprint (Time 10)
, …Run Code Online (Sandbox Code Playgroud) 我有两个记录,都有一个我想提取显示的字段.我如何安排事情,以便可以使用相同的功能操作它们?因为他们有不同的领域(在这种情况下,firstName和buildingName)是他们的名字领域,他们每个人都需要一些"适配器"代码映射firstName到name.这是我到目前为止:
class Nameable a where
name :: a -> String
data Human = Human {
firstName :: String
}
data Building = Building {
buildingName :: String
}
instance Nameable Human where
name x = firstName x
instance Nameable Building where
-- I think the x is redundant here, i.e the following should work:
-- name = buildingName
name x = buildingName x
main :: IO ()
main = do
putStr $ …Run Code Online (Sandbox Code Playgroud) 我有这个代码(在happstack里面,但可能只是IO monad):
accountHandler conn = do
sessionId <- optional $ readCookieValue "sessionId"
case sessionId of
Nothing -> seeOther ("/" :: String) $ toResponse ()
Just s -> do
result <- loggedInUserId conn s
case result of
Just userId -> seeOther ("/account/" ++ unUserId userId) $ toResponse ()
Nothing -> seeOther ("/" :: String) $ toResponse ()
Run Code Online (Sandbox Code Playgroud)
我想删除嵌套的case语句并写下如下内容:
accountHandler conn = do
let action = do
sessionId <- optional $ readCookieValue "sessionId"
userId <- loggedInUserId conn sessionId
return $ seeOther ("/account/" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 播放 WAV 文件rust-sdl2。
我发现AudioSpecWAV,但没有一个音频初始化方法似乎将其作为一种类型,并且它没有实现AudioCallback。我尝试用自己的回调来实现这个功能,如下所示:
struct MyWav {
wav: AudioSpecWAV,
volume: f32,
pos: usize,
}
impl AudioCallback for MyWav {
type Channel = f32;
fn callback(&mut self, out: &mut [f32]) {
for x in out.iter_mut() {
*x = match self.wav.buffer().get(self.pos) {
Some(v) => { self.pos += 1; v as f32 },
None => { 0.0 }
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
...但我不知道如何解决出现的以下错误:
the traitcore::marker::Sync is not implemented for the type *mut u8
这似乎是audio_buf的字段 …
我有一个使用Firestore和Cloud Functions的客户端Web应用程序.
我想设置规则,使得如果用户具有用户能够写入的文档的秘密URL,则无需任何其他类型的登录或身份验证.像(伪代码,我刚刚编写request.params.secret_token):
service cloud.firestore {
match /databases/{database}/documents {
match /cities/{city} {
allow read, write: if resource.data.secret_token == request.params.secret_token;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我对各种可用的身份验证选项感到困惑,并且无法通过最好的方式推理.
感觉接近的潜在选择: