阅读关于从函数返回右值引用的这个答案让我思考,如何id在C++ 0x中编写函数.
基本上,我想做id一个什么都不做的功能,这个功能对程序没有任何可观察的影响.
我的第一次尝试如下:
#include <iostream>
class X
{
public:
X(std::string&& s) : s(std::move(s)) {};
X(const std::string& s) : s(s) {};
std::string s;
~X() { std::cout << "Destroying: " << s << std::endl; }
private:
X(const X&) {};
X(X&&) {};
};
template <class T>
T&& id(T&& x) { return static_cast<T&&>(x); }
int main()
{
auto&& x1 = X("x1");
std::cout << "Line 1" << std::endl;
auto&& x2 = id(X("x2"));
std::cout << "Line 2" << std::endl; …Run Code Online (Sandbox Code Playgroud) 无论如何在Haskell中定义如下的函数?
or True True = True
or True undefined = True
or True False = True
or undefined True = True
or undefined False = undefined
or undefined undefined = undefined
or False True = True
or False undefined = undefined
or False False = False
Run Code Online (Sandbox Code Playgroud)
我目前没有它的用例(虽然我对它感兴趣),我只是感兴趣,如果可能的话.
可以说我有类似以下内容:
class A { virtual void g() = 0 }
class B : public A { virtual void g() { ... } }
class C : public A { virtual void g() { ... } }
... f(bool x)
{
if (x) { return B(); } else { return C(); }
}
bool get_boolean();
int main()
{
bool b = get_boolean();
... x = f(b);
x.g();
}
Run Code Online (Sandbox Code Playgroud)
无论如何,在没有调用的情况下执行类似上面的操作new,即仅在堆栈上?
我正在创建自己的模块,我们可以调用它X::Y.当然,模块将在文件中X/Y.pm.
让我们说Y需要调用一个外部程序,prog.理想的情况是我只是想放prog的X,这样我就可以运行X/prog.我想没有硬编码X/prog的完整路径,并且无论当前工作目录集如何,模块都可以工作.
如何从模块内部找到模块的完整路径?或者有更好的方法吗?
我已经完成了一些编程和Haskell,并希望在Groovy中实现一些Haskell列表处理函数.下面是一个实现unfoldr.基本上A是生成的迭代器(即列表)的类型,并且B是状态.
有两件事我想给更强的类型:
Tuple<A,B>而不仅仅是Tuple其产生枚举从1至100的迭代实施例的代码是下面和上ideone链接这里.
class Unfoldr<A,B> implements java.util.Iterator<A>
{
public Unfoldr(Closure<Tuple> f, B init)
{
this.f = f;
this.state = f(init);
}
public synchronized A next()
{
if (hasNext())
{
A curr = state.get(0);
state = f(state.get(1));
return curr;
}
else
{
throw java.lang.NoSuchElementException;
}
}
public synchronized boolean hasNext()
{
return (state != null);
}
public void remove() { throw UnsupportedOperationException; }
private Closure<Tuple> f;
private Tuple …Run Code Online (Sandbox Code Playgroud) 对我之前的问题的回答表明Haskell表示plusWord2#为llvm.uadd.with.overflow.我希望使用进位进行长时间的添加,就像x86 ADC指令的工作方式一样.该指令不仅添加了两个参数,还添加了进位的内容.
然后可以添加如下的长数字:
ADD x1 y1
ADC x2 y2
ADC x3 y3
...
Run Code Online (Sandbox Code Playgroud)
导致每个单词一个指令(忽略所需的任何周围移动等).
我查看了GMP库,以及它在通用C代码中如何进行长时间的添加.这是摘录自mpn/generic/add_n.c
sl = ul + vl;
cy1 = sl < ul;
rl = sl + cy;
cy2 = rl < sl;
cy = cy1 | cy2;
Run Code Online (Sandbox Code Playgroud)
注意,它保存了原始加法和进位位加法的进位.这些操作中只有一个可以携带,因此或者之后的携带就足够了.
显然,GMP具有针对特定平台的特定汇编代码,但我认为通用代码是一个很好的基础,因为它可能被编写为编译成合理的代码.在plusWord2#原始的操作手段,我不需要做无聊的比较,以获得进位,所以我实现了GMP的通用代码就像在Haskell如下:
addWithCarry :: Word# -> Word# -> Word# -> (# Word#, Word# #)
addWithCarry x y c =
let
(# c1, r1 #) = plusWord2# x y
(# c2, r2 …Run Code Online (Sandbox Code Playgroud) 这是一个返回指针对齐的简单函数:
{-# LANGUAGE ScopedTypeVariables #-}
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable, alignment)
main = return ()
ptrAlign1 :: (Storable a) => Ptr a -> Int
ptrAlign1 _ = alignment (undefined :: a)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Could not deduce (Storable a0) arising from a use of `alignment'
from the context (Storable a)
bound by the type signature for
ptrAlign1 :: Storable a => Ptr a -> Int
at prog.hs:8:14-41
The type variable `a0' is ambiguous
Run Code Online (Sandbox Code Playgroud)
如果我ptrAlign像这样重写一个更混乱的派系:
ptrAlign2 :: (Storable a) => …Run Code Online (Sandbox Code Playgroud) 有没有办法定义" pairmap"如下所示:
pairmap f (x,y) = (f x, f y)
Run Code Online (Sandbox Code Playgroud)
以下是有效的:
pairmap (+2) (1::Int, 2::Float)
pairmap succ (1::Int, 'a')
pairmap Just ('a', True)
Run Code Online (Sandbox Code Playgroud)
等等
当然,在第一种情况下,两个元素必须是类Num,而在第二种情况下,两个元素都必须是类Enum.然而,在第三种情况下,不需要限制.
答案(但可以改进)
下面的代码(ideone)解决了这个问题,但请注意我的函数必须包装在一个数据类型中,该数据类型既包含输入和输出类型之间的关系,也包含输入类型的任何约束.这有效,但有一些样板.如果我能用更少的样板来实现这一目标会很好,所以任何答案都会受到赞赏(尽管这个解决方案对于我的目的而言相当合适).
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
import GHC.Exts (Constraint)
class Function f where
type Constraints f a :: Constraint
type instance Constraints f a = ()
type Result f a
type instance …Run Code Online (Sandbox Code Playgroud) Is there a name for the function defined as follows:
f :: [Either a b] -> Either [a] [b]
f x = let (y1, y2) = partitionEithers x in
case y1 of
[] -> Right y2
_ -> Left y1
Run Code Online (Sandbox Code Playgroud)
Basically if the list contains at least on Left it returns all the Lefts otherwise it returns all the Rights.
Alternatively, are there some generalisation of this function over a type class that I've missed?
Data.Dynamic有以下实现:
data Dynamic where
Dynamic :: TypeRep a -> a -> Dynamic
Run Code Online (Sandbox Code Playgroud)
我想到以下定义是等效的(至少我认为):
data Dynamic where
Dynamic :: Typeable a => a -> Dynamic
Run Code Online (Sandbox Code Playgroud)
因为可以使用 到达TypeRep a约束Typeable,withTypeable而在另一个方向上,可以使用Typeable到达约束。TypeRep atypeRep
我问这个问题是因为我经常创建带有类型类约束的 GADT,通常是为了创建“存在”类型,并且看到这种类型让我怀疑是否应该添加“见证”字段,而不是使用类型类约束?在这两种方法之间进行选择时我应该考虑什么?
进一步的想法...
考虑这样的事情:
data SillyListA m where
SillyListA :: Ord a => (a -> m ()) -> [a] -> SillyList m
data SillyListB m where
SillyListB :: (a -> a -> Ordering) -> (a -> m ()) -> [a] -> …Run Code Online (Sandbox Code Playgroud)