我用JS制作了类似拖放元素的东西.
function Draggable(elm) {
this.d = elm;
this.style.position = "absolute";
elm.onselectstart = elm.ondragstart = function() { return false; }
elm.addEventListener('mousedown', this._start.bindAsEventListener(this), false);
}
Draggable.prototype._start = function (event) {
this.deltaX = event.clientX;
this.deltaY = event.clientY;
if (!this.dm) {
this.dm = document.createElement("div");
this.dm.setAttribute("class", "dragger");
this.dm.onmousemove = this._move.bindAsEventListener(this);
this.dm.onmouseup = this._stop.bindAsEventListener(this);
this.dm.onselectstart = RetFalse;
this.dm.ondragstart = RetFalse;
}
document.body.appendChild(this.dm);
this.lastX = this.lastY = 0;
this.ondragstart();
return false;
}
Draggable.prototype._move = function (event) {
var newx = (event.clientX - this.deltaX);
var newy = (event.clientY …Run Code Online (Sandbox Code Playgroud) getText = do
c <- getChar
s <- getText
return (c : s)
main = do
s <- getText
putStr s
Run Code Online (Sandbox Code Playgroud)
我期望看到的是每次按下'Enter'后输入线都会被回显.但没有任何回应...(我知道这是一个无限循环)它似乎不会" return"直到它上面的所有"IO"执行....
但是,以下代码:
main = do
s <- getContents
putStr s
Run Code Online (Sandbox Code Playgroud)
它在输入后立即显示该行.
鉴于功能getChar,我可以写一个getText表现得像getContents?
我有一个std::map对象。
std::map<std::string, std::string> m;
m.insert({ "abcd", "foo" });
m.insert({ "1234", "bar" });
Run Code Online (Sandbox Code Playgroud)
我想获取并删除第一个元素,例如:
auto iter = m.begin();
auto [key, value] = std::move(*iter);
m.erase(iter);
do_something_with(key, value);
Run Code Online (Sandbox Code Playgroud)
这被认为是安全的吗?(从迭代器移动应该使键成为空字符串,这使得映射m无效。)
是否存在现有的函数(或语法),例如:
inline void noop(...) { /* EMPTY */ }
Run Code Online (Sandbox Code Playgroud)
所以我可以做这样的事情:
template <class... T>
void foo(T... args) {
noop( do_side_effect<T>(args)... );
}
Run Code Online (Sandbox Code Playgroud)
- - - - - - - 编辑 - - - - - -
因为这样的东西不能编译:
template <class... T>
void foo(T... args) {
do_side_effect<T>(args)...;
}
template <class... T>
void foo(T... args) {
( do_side_effect<T>(args)... );
}
Run Code Online (Sandbox Code Playgroud)
------------ 编辑 2 ------------
显示可能用例的示例。
template <class T, class F, int... indices>
void _tuple_foreach_details(T&& tuple, F& functor, std::index_sequence<indices...>){
noop( functor(std::get<indices>(tuple))... );
/* this means: …Run Code Online (Sandbox Code Playgroud) 以下代码在let子句中得到了"解析错误"(在GHC 7.8.3中):
someFunction = do {
let foo = bar;
return foo;
}
Run Code Online (Sandbox Code Playgroud)
如何纠正它以获得相当于此?
someFunction = do
let foo = bar
return foo
Run Code Online (Sandbox Code Playgroud) 我想实现这样的事情:
class A {
var a, b, c, d: Int
init() {
reset()
}
func reset() {
a = 1
b = 2
c = 3
d = 4
}
func blablabla() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
哪些无法编译,错误信息:
在初始化之前使用变量"self.a"
它不会使我必须将代码复制从意义reset()到init().
这是缺陷还是有另一种方法可以做到这一点?
我在尝试理解该fmt库时遇到了一些困难。
我们从print函数开始:
fmt::print("Hello, {}", a);
Run Code Online (Sandbox Code Playgroud)
这是 的定义print:
template <typename... T>
FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
// Implementation
}
Run Code Online (Sandbox Code Playgroud)
该print函数采用 aformat_string<T...>作为参数。T...所以我们可以在编译时做一些事情。
但是如何T...在编译时推断呢?
下面是如何format_string实现的(代码取自10.0.0版本)。对于编译时格式字符串,它有一个隐式 constexpr 构造函数,该构造函数采用string_view- 兼容类型作为参数。
template <typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;
template <typename Char, typename... Args> class basic_format_string {
private:
basic_string_view<Char> str_;
public:
template <typename S,
FMT_ENABLE_IF(
std::is_convertible<const S&, basic_string_view<Char>>::value)>
FMT_CONSTEVAL FMT_INLINE basic_format_string(const S& s) : str_(s) { …Run Code Online (Sandbox Code Playgroud) 有功能可以做功能arrayToList吗:
import Data.Array.ST
import Control.Monad.ST
genArray :: ST s [Int]
genArray = do
a <- new Array (0, 99) 0 :: ST s (STArray s Int Int)
writeArray a 0 1
{- ... write something to the array ... -}
return arrayToList(a)
Run Code Online (Sandbox Code Playgroud)
如果没有,怎么写一个?