例:
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,抑或是可以在奇数的情况下失败是巧合吗?
我理解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()方法.
使用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:771mspublic int Compare(int x, int y) { return x.CompareTo(y); }:809msComparer<int>.Default返回一个GenericComparer<int>.根据dotPeek,我们有:
internal class GenericComparer<T> : Comparer<T> where T : IComparable<T>
{
public …Run Code Online (Sandbox Code Playgroud) 为了简单起见,我将使用这个人为的示例类(重点是我们从方法中获得了一些昂贵的数据):
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) 我希望标题听起来不太主观; 我绝对不是要开始就OO进行辩论.我只想讨论解决以下问题的不同方法的基本利弊.
让我们来看一个这个最小的例子:你想表达一个抽象数据类型T,其函数可以将T作为输入,输出或两者:
我想避免向下转发和任何其他动态类型.我也想尽可能避免变异.
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) 似乎最好只为顶级函数创建一次 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这样做的话)。
给定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# ×4
haskell ×2
.net ×1
closures ×1
ffi ×1
generics ×1
insert ×1
memoization ×1
oop ×1
performance ×1
polymorphism ×1
postgresql ×1
sorting ×1