小编Fun*_*lad的帖子

INSERT RETURNING是否保证以"正确"的顺序返回内容?

例:

create table foo(
    id serial, 
    txt text
);

insert into foo(txt) values ('a'),('b'),('c') returning id;
Run Code Online (Sandbox Code Playgroud)

返回:

 id 
----
  1
  2
  3
(3 rows)
Run Code Online (Sandbox Code Playgroud)

似乎是在第一次 id的返回值永远是id'a',第二对'b'等,但该定义的行为insert into,抑或是可以在奇数的情况下失败是巧合吗?

postgresql insert sql-returning

18
推荐指数
3
解决办法
2824
查看次数

传递ExceptionDispatchInfo而不仅仅是Exception有什么意义?

我理解ExceptionDispatchInfo.Capture(e).Throw()(保留原始堆栈跟踪)的价值,但是Capture早期使用并传递ExceptionDispatchInfo周围与仅仅传递被捕获的优势是什么Exception

作为一个具体的例子,比较

static Exception CaptureException(Action action)
{
  try
  {
    action();
    return null;
  }
  catch (Exception e)
  {
    return e;
  }
}

public void Test1()
{
  ExceptionDispatchInfo.Capture(CaptureException(
       () => throw new IOException("Test")))
    .Throw();
}
Run Code Online (Sandbox Code Playgroud)

static ExceptionDispatchInfo CaptureDispatchInfo(Action action)
{
  try
  {
    action();
    return null;
  }
  catch (Exception e)
  {
    return ExceptionDispatchInfo.Capture(e);
  }
}

public void Test2()
{
  CaptureDispatchInfo(() => throw new IOException("Test")).Throw();
}
Run Code Online (Sandbox Code Playgroud)

,两者都给出了基本相同的堆栈跟踪(它的async变体类似于此.).所以,我真的不明白为什么这个ExceptionDispatchInfo类存在,而不仅仅是一个组合ExceptionDispatchInfo.Capture(e).Throw()方法.

.net c# exception-handling

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

为什么List <T> .Sort使用Comparer <int> .Default的速度是等效自定义比较器的两倍多?

结果

使用1000万随机ints 的列表(每次相同的种子,平均10次重复):

listCopy.Sort(Comparer<int>.Default)需要314ms.

运用

sealed class IntComparer : IComparer<int>
{
  public int Compare(int x, int y)
  {
    return x < y ? -1 : (x == y ? 0 : 1);
  }
}
Run Code Online (Sandbox Code Playgroud)

listCopy.Sort(new IntComparer())需要716毫秒.

一些变化:

  • 使用struct IntComparer而不是sealed class:771ms
  • 使用public int Compare(int x, int y) { return x.CompareTo(y); }:809ms

评论

Comparer<int>.Default返回一个GenericComparer<int>.根据dotPeek,我们有:

internal class GenericComparer<T> : Comparer<T> where T : IComparable<T>
{
  public …
Run Code Online (Sandbox Code Playgroud)

c# sorting performance

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

记忆和类型

为了简单起见,我将使用这个人为的示例类(重点是我们从方法中获得了一些昂贵的数据):

class HasNumber a where
  getNumber :: a -> Integer
  getFactors :: a -> [Integer]
  getFactors a = factor . getNumber
Run Code Online (Sandbox Code Playgroud)

当然,我们可以对这个类进行memoizing实现,例如:

data Foo = Foo {
  fooName :: String,
  fooNumber :: Integer,
  fooFactors :: [Integer]
}

foo :: String -> Integer -> Foo
foo a n = Foo a n (factor n) 

instance HasNumber Foo where
    getNumber = fooNumber
    getFactors = fooFactors
Run Code Online (Sandbox Code Playgroud)

但是,要求将"因子"字段手动添加到将成为HasNumber实例的任何记录中似乎有点难看.下一个想法:

data WithFactorMemo a = WithFactorMemo {
    unWfm :: a,
    wfmFactors :: [Integer]
}

withFactorMemo …
Run Code Online (Sandbox Code Playgroud)

haskell memoization

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

C#中的显式方法表而不是OO - 好吗?坏?

我希望标题听起来不太主观; 我绝对不是要开始就OO进行辩论.我只想讨论解决以下问题的不同方法的基本利弊.

让我们来看一个这个最小的例子:你想表达一个抽象数据类型T,其函数可以将T作为输入,输出或两者:

  • f1:取一个T,返回一个int
  • f2:取一个字符串,返回一个T.
  • f3:取T,返回另一个T.

我想避免向下转发和任何其他动态类型.我也想尽可能避免变异.

1:基于抽象类的尝试

abstract class T {
  abstract int f1();
  // We can't have abstract constructors, so the best we can do, as I see it, is:
  abstract void f2(string s);
  // The convention would be that you'd replace calls to the original f2 by invocation of the nullary constructor of the implementing type, followed by invocation …
Run Code Online (Sandbox Code Playgroud)

c# oop generics polymorphism functional-programming

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

Haskell FFI:顶级 FunPtr 到顶级函数?

似乎最好只为顶级函数创建一次 FunPtr,而不是在需要时创建一个新函数(针对同一函数)并处理其释放。

我是否忽略了除 FunPtr 之外的其他获取 FunPtr 的方法foreign import ccall "wrapper"?如果没有,我的解决方法将如下面的代码所示。这样安全吗?

type SomeCallback = CInt -> IO ()

foreign import ccall "wrapper" mkSomeCallback :: SomeCallback -> IO (FunPtr SomeCallback)

f :: SomeCallback
f i = putStrLn ("It is: "++show i)

{-# NOINLINE f_FunPtr #-}
f_FunPtr :: FunPtr SomeCallback
f_FunPtr = unsafePerformIO (mkSomeCallback f)
Run Code Online (Sandbox Code Playgroud)

编辑:已验证“每次都创建一个新的”变体(main = forever (mkSomeCallback f))实际上会泄漏内存(如果不freeHaskellFunPtr这样做的话)。

haskell ffi

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

`Task.ContinueWith`参数的`antecedent`参数点?

给定a Task t,之间是否存在语义差异

t.ContinueWith(ante => DoSomethingWith(ante));
Run Code Online (Sandbox Code Playgroud)

t.ContinueWith(ante => DoSomethingWith(t));
Run Code Online (Sandbox Code Playgroud)

,假设t以后没有变异?

antecedent参数是否仅存在以避免在第二个变体中分配闭包?

c# closures task-parallel-library

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