小编Ash*_*Ash的帖子

C#传递函数作为参数

我在C#中编写了一个函数来进行数值微分.它看起来像这样:

public double Diff(double x)
{
    double h = 0.0000001;

    return (Function(x + h) - Function(x)) / h;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够传递任何功能,如:

public double Diff(double x, function f)
{
    double h = 0.0000001;

    return (f(x + h) - f(x)) / h;
}
Run Code Online (Sandbox Code Playgroud)

我认为这对代表来说是可能的(也许?)但是我不确定如何使用它们.

任何帮助将不胜感激.

c# arguments

123
推荐指数
3
解决办法
22万
查看次数

C++ delete - 删除我的对象,但我仍然可以访问数据?

我写了一个简单的,工作的俄罗斯方块游戏,每个块作为类单块的一个实例.

class SingleBlock
{
    public:
    SingleBlock(int, int);
    ~SingleBlock();

    int x;
    int y;
    SingleBlock *next;
};

class MultiBlock
{
    public:
    MultiBlock(int, int);

    SingleBlock *c, *d, *e, *f;
};

SingleBlock::SingleBlock(int a, int b)
{
    x = a;
    y = b;
}

SingleBlock::~SingleBlock()
{
    x = 222;
}

MultiBlock::MultiBlock(int a, int b)
{
    c = new SingleBlock (a,b);
    d = c->next = new SingleBlock (a+10,b);
    e = d->next = new SingleBlock (a+20,b);
    f = e->next = new SingleBlock (a+30,b);
}
Run Code Online (Sandbox Code Playgroud)

我有一个扫描完整行的函数,并运行删除相关的块的链接列表并重新分配 - >下一个指针. …

c++ pointers c++-faq undefined-behavior

30
推荐指数
5
解决办法
1万
查看次数

在Haskell过度使用fromIntegral

每当我使用双精度和整数编写函数时,我发现这个问题,我不断在我的函数中到处使用'fromIntegral'.例如:

import Data.List

roundDouble
    :: Double
    -> Int 
    -> Double
roundDouble x acc = fromIntegral (round $ x * 10 ** fromIntegral acc) / 10 ** fromIntegral acc
Run Code Online (Sandbox Code Playgroud)

有没有更简单的方法来写这个?(我知道可能有更简单的方法来舍入数字,如果有,请告诉我!但我主要感兴趣的是如何避免使用这么多'fromIntegrals'.)

谢谢,阿什

double haskell integer

27
推荐指数
3
解决办法
4791
查看次数

C#Hashset包含非唯一对象

使用这个类

public class Foo
{
    public string c1, c2;

    public Foo(string one, string two)
    {
        c1 = one;
        c2 = two;
    }

    public override int GetHashCode()
    {
        return (c1 + c2).GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

而这个HashSet

HashSet<Foo> aFoos = new HashSet<Foo>();
Foo aFoo = new Foo("a", "b");

aFoos.Add(aFoo);
aFoos.Add(new Foo("a", "b"));

label1.Text = aFoos.Count().ToString();
Run Code Online (Sandbox Code Playgroud)

我得到答案2,肯定它应该是1.有没有办法解决这个问题,所以我的HashSet只包含唯一的对象?

谢谢,阿什.

c# hashset

16
推荐指数
2
解决办法
1万
查看次数

MongoDB的预测效率如何?

在查询mongo数据库时,排除文档中的几乎所有数据会有很多开销吗?

例如,在我只需要field1field2的情况下,对于具有以下文档结构的集合:

{
    "field1" : 1
    "field2" : true
    "field3" : ["big","array",...]
    "field4" : ["another","big","array",...]
}
Run Code Online (Sandbox Code Playgroud)

我会从中受益更多:

  1. 在此集合旁边创建一个单独的集合,其中仅包含field1和field2,或
  2. 使用包含/排除参数在原始文档上使用.find()

注意:保存相同数据两次的低效率对我来说并不像我查询数据的效率那么重要

非常感谢!

projection mongodb

13
推荐指数
1
解决办法
1119
查看次数

俄罗斯方块:类的布局

我写了一个工作的俄罗斯方块克隆,但它有一个非常凌乱的布局.我可以获得有关如何重构我的类以使我的编码更好的反馈.我专注于使我的代码尽可能通用,试图使它更像是仅使用块的游戏引擎.

每个块都是在游戏中单独创建的.我的游戏有2个BlockLists(链表):StaticBlocks和Tetroid.StaticBlocks显然是所有非移动块的列表,而tetroid是当前tetroid的4个块.

  1. 主要是世界被创造.

  2. 首先创建一个新的tetroid(列表Tetroid中的4个块)(NewTetroid)

  3. 通过(***碰撞)函数检测碰撞,通过使用(If*****)函数比较每个Tetroid和所有StaticBlock.

  4. 当tetroid停止(击中底部/块)时,它被复制(CopyTetroid)到StaticBlocks并且Tetroid变为空,然后通过用(SearchY)搜索StaticBlocks来测试完整的行,销毁/删除块等.

  5. 创建了一个新的tetroid.

(TranslateTetroid)和(RotateTetroid)逐个对Tetroid列表中的每个块执行操作(我认为这是不好的做法).

(DrawBlockList)只是遍历一个列表,为每个块运行Draw()函数.

通过在调用(NewTetroid)时相对于Tetroid中的第一个块设置旋转轴来控制旋转.我的每个程序段的旋转功能(旋转)使其围绕轴旋转,使用输入+ -1进行左/右旋转.RotationModes和States用于以2种或4种不同方式旋转的块,定义它们当前处于什么状态,以及它们是应该向左还是向右旋转.我不满意这些在"世界"中的定义,但我不知道在哪里放置它们,同时仍然保持我的(旋转)功能对每个块都是通用的.

我的课程如下

class World
{
    public:
    /* Constructor/Destructor */
    World();
    ~World();

    /* Blocks Operations */
    void AppendBlock(int, int, BlockList&);
    void RemoveBlock(Block*, BlockList&);;

    /* Tetroid Operations */
    void NewTetroid(int, int, int, BlockList&);
    void TranslateTetroid(int, int, BlockList&);
    void RotateTetroid(int, BlockList&);
    void CopyTetroid(BlockList&, BlockList&);

    /* Draw */
    void DrawBlockList(BlockList&);
    void DrawWalls();

    /* Collisions */
    bool TranslateCollide(int, int, BlockList&, BlockList&);
    bool RotateCollide(int, BlockList&, BlockList&);
    bool OverlapCollide(BlockList&, BlockList&); // For end …
Run Code Online (Sandbox Code Playgroud)

c++ class linked-list tetris

7
推荐指数
1
解决办法
3346
查看次数

改进代码以生成分发

我是Haskell的新手,我想知道如何使这段代码变得更加高效和整洁.这似乎不必要地长而不整洁.

我的脚本生成10个平均10个硬币翻转的列表.

import Data.List
import System.Random

type Rand a = StdGen -> Maybe (a,StdGen)

output = do
    gen <- newStdGen
    return $ distBernoulli 10 10 gen

distBernoulli :: Int -> Int -> StdGen -> [Double]
distBernoulli m n gen = [fromIntegral (sum x) / fromIntegral (length x) | x <- lst]
    where lst = splitList (randomList (n*m) gen) n

splitList :: [Int] -> Int -> [[Int]]
splitList [] n = []
splitList lst n = take n lst : splitList …
Run Code Online (Sandbox Code Playgroud)

statistics haskell

5
推荐指数
1
解决办法
312
查看次数

从Haskell中的元组列表中选择数据

:: [((a, b), (a, b), (a, b))]在Haskell中有一个类型元组的元组列表.

对于某些上下文,3个点(a, b)表示具有第一个点的U形曲线上的(时间,值)对,在初始时间t1在曲线x1上具有最大值,第三个点具有更大的时间t3 = t1 + dt和值x3

我想[((t1, x1), (t2, x2), (t3, x3))]通过获取最大值的元组找到列表中最宽U的范围t3 - t1,然后返回值x2 - x1.

我能想到的唯一方法是先将列表中的每个元组映射到t3 - t1,找到它的索引,然后在原始列表中计算该索引的x2 - x1.有没有更优雅的方式来做到这一点?

findMaxTime :: [((a, b), (a, b), (a, b))] -> Int
findMaxTime list = elemIndex (==min) $ map (\(x, y, z) -> fst z - fst x)
                       where min = minimum $ map (\(x, y, z) -> fst z - fst …
Run Code Online (Sandbox Code Playgroud)

haskell tuples list

5
推荐指数
1
解决办法
2273
查看次数

如何使 git diff 更准确地显示相同行的更改?

有时 git diff 会返回如下内容:

diff --git a/file.x b/file.x
index aaaaaaa..bbbbbbb 000000
--- a/file.x
+++ b/file.x

    for (int i = 0; i < 5; i++)
-   {
-       // code
-   }
-
-   // other code
-
-   for (int i = 0; i < 5; i++)
    {
        // more code
    }
Run Code Online (Sandbox Code Playgroud)

有什么方法可以强制 git 显示如下所示的差异,这更准确地代表了真正的变化?

diff --git a/file.x b/file.x
index aaaaaaa..bbbbbbb 000000
--- a/file.x
+++ b/file.x

-   for (int i = 0; i < 5; i++)
-   {
-       // …
Run Code Online (Sandbox Code Playgroud)

git diff

5
推荐指数
0
解决办法
971
查看次数

指向列表内外的第一个对象?

以下哪一项是将第一个对象存储在链表中的更正确的方法?或者有人可以指出每个的优点/缺点.谢谢.

class Node
{
    int var;
    Node *next;

    static Node *first;

    Node()
    {
        if (first == NULL)
        {
            first = this;
            next = NULL;
        }
        else
            //next code here
        }
    }
}

Node* Node::first = NULL;
new Node();
Run Code Online (Sandbox Code Playgroud)

- 要么 -

class Node
{
    int var;
    Node *next;

    Node()
    {
        //next code here
    }
}

Node* first = new Node();
Run Code Online (Sandbox Code Playgroud)

c++ linked-list list

1
推荐指数
1
解决办法
2126
查看次数

类型定义中的F#递归

我在尝试在F#中实现自动微分时遇到了一些问题.我认为问题在于评估不是"懒惰".

这是我的代码:

type Diff =
    {d : double; df : Diff}
    static member (+) (x : Diff, y : Diff) =
        {d = x.d + y.d; df = x.df + y.df}
    static member (-) (x : Diff, y : Diff) =
        {d = x.d - y.d; df = x.df - y.df}
    static member (*) (x : Diff, a : double) =
        {d = x.d * a; df = x.df * a}
    static member (*) (x : Diff, y : Diff) …
Run Code Online (Sandbox Code Playgroud)

recursion f#

0
推荐指数
1
解决办法
364
查看次数