小编Jon*_*ych的帖子

在Promise.race中完成哪些承诺

上下文:我需要进行大量可并行化的异步调用(想想大约300到3000个ajax调用).但是,我不想通过立即调用它们来拉紧浏览器或服务器.我也不想按顺序运行它们,因为它需要很长时间才能完成.我决定一次运行五个左右,并导出此功能:

async function asyncLoop(asyncFns, concurrent = 5) {
    // queue up simultaneous calls 
    let queue = [];
    for (let fn of asyncFns) {
        // fire the async function and add its promise to the queue
        queue.push(fn());
        // if max concurrent, wait for the oldest one to finish
        if (queue.length >= concurrent) {
            await queue.shift();
        }
    }
    // wait for the rest of the calls to finish
    await Promise.all(queue);
};
Run Code Online (Sandbox Code Playgroud)

其中asyncFns是一个可迭代的(尚未调用的)异步函数.

问题:这是有效的,但我发现,最老的是第一个完成的并不总是如此.我想修改函数,以便它使用Promise.race等到第一个promise成功,然后从那里继续.但是,我不知道要删除哪个承诺:

        // if max concurrent, wait for the …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous async-await es6-promise

12
推荐指数
2
解决办法
1815
查看次数

如何将圆上的坐标转换为正方形上的坐标?

我正在研究LBP2中的游戏,它修改了控制器提供输入的方式.这个问题: 如何将正方形上的坐标转换为圆上的坐标? 用我正在做的事情帮助了我很多,但我确实遇到了一个问题.我需要他们给出的反函数.它们来自方形 - >圆形,我尝试过全面搜索如何将圆形映射到正方形.

上一个问题中给出的函数是:

xCircle = xSquare*sqrt(1 - 0.5*ySquare ^ 2)

yCircle = ySquare*sqrt(1 - 0.5*xSquare ^ 2)

将正方形映射到圆形

我的问题是xCircle和yCircle ......如何找到xSquare和ySquare?

我已经尝试了我所知道的所有代数,填写了两页笔记,试图获得wolfram alpha以获得反函数,但这个问题超出了我的能力范围.

谢谢你看看.

math coordinate-systems

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

迭代器可以改变迭代的集合吗?Java的

我试图使用迭代器中的迭代次数作为计数器,但是想知道这样做的后果.

private int length(Iterator<?> it) {
    int i = 0;

    while(it.hasNext()) {
        it.next();
        i++;
    }

    return i;
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我担心迭代器可能在幕后做什么.也许当我迭代堆栈时,它会弹出堆栈中的项目,或者如果我使用优先级队列,它会修改优先级.

关于迭代器的javadoc说这个:

next
E next()
返回迭代中的下一个元素.
返回:
迭代中的下一个元素
抛出:
NoSuchElementException - 如果迭代没有更多元素

我没有看到保证迭代这个未知的集合不会修改它.我是在考虑不切实际的边缘情况,还是这是一个问题?有没有更好的办法?

java iterator

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

如果java抽象类"Number"的任何对象等于零

我正在尝试创建一个泛型函数,它接受任何类型的Number条件并且如果该数字等于零则有条件地执行某些操作.我希望任何人能够传递任何扩展Number的类(BigDecimal,BigInteger,Byte,Double,Float,Integer,Long或Short)

到目前为止,我试图instanceof找出数字的类型,然后将其与等效类型进行比较

public static boolean isZero(Number number) {
    if (number instanceof BigDecimal) {
        return BigDecimal.ZERO.equals(number);
    } else if (number instanceof BigInteger) {
        return BigInteger.ZERO.equals(number);
    } else if (number instanceof Byte) {
        return new Byte((byte) 0).equals(number);
    } else if (number instanceof Double) {
        return new Double(0).equals(number);
    } else if (number instanceof Float) {
        return new Float(0).equals(number);
    } else if (number instanceof Integer) {
        return new Integer(0).equals(number);
    } else if (number instanceof Long) {
        return new …
Run Code Online (Sandbox Code Playgroud)

java numbers

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

如果一个类型真正可移动构造,如何获得

以此代码为例:

#include <type_traits>
#include <iostream>

struct Foo
{
    Foo() = default;
    Foo(Foo&&) = delete;
    Foo(const Foo&) noexcept
    {
        std::cout << "copy!" << std::endl;
    };
};

struct Bar : Foo {};

static_assert(!std::is_move_constructible_v<Foo>, "Foo shouldn't be move constructible");
// This would error if uncommented
//static_assert(!std::is_move_constructible_v<Bar>, "Bar shouldn't be move constructible");

int main()
{
    Bar bar {};
    Bar barTwo { std::move(bar) };
    // prints "copy!"
}
Run Code Online (Sandbox Code Playgroud)

因为Bar是从Foo派生的,所以它没有移动构造函数.它仍然可以通过使用复制构造函数来构造.我了解了为什么它从另一个答案中选择了复制构造函数:

如果y是类型S,则std::move(y)类型S&&的引用与类型兼容S&.因此S x(std::move(y))完全有效并调用复制构造函数S::S(const S&) …

c++ stl type-traits move-semantics

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

SFINAE检查std :: less是否有效

在我的代码中,如果一个对象小于另一个对象,我希望在另一个对象之前执行一个操作.但是,如果类型不具有可比性,则与订单无关.为此,我尝试使用SFINAE:

template<typename T, typename = decltype(std::declval<std::less<T>>()(std::declval<T>(), std::declval<T>()))> 
bool ComparableAndLessThan(const T& lhs, const T& rhs) {
    return std::less<T>()(lhs, rhs);
}
bool ComparableAndLessThan(...) {
    return false;
}

struct foo {};
int main()
{
    foo a,b;
    if (ComparableAndLessThan(a, b)) {
        std::cout << "a first" << std::endl;
    } else {
        std::cout << "b first" << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这没用.如果我创建一个对象而不给它一个操作符<或std :: less专门化,我会收到此错误.

error C2678: binary '<': no operator found which takes a left-hand operand of type 'const foo' (or there is no acceptable conversion)
note: …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae

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

Java - 在类<?的泛型类上使用newInstance()扩展myClass>:InstantiationException

我正在尝试使用Generic Classes,我遇到了一个无法克服的障碍.简而言之,我遇到了一个错误,我不明白它为什么被抛出:InstantiationException

在文档中,它将此异常定义为:

当应用程序尝试使用类Class中的newInstance方法创建类的实例时抛出,但指定的类对象无法实例化,因为它是接口或是抽象类.

现在让我头疼的问题是我不使用abstract或interface关键字.我也听说过它可能是由于没有默认构造函数(我有).只是为了确定,我将代码减少到尽可能小,但仍然会出错:

package Sandbox;

public class Sandbox {


    public static void main(String[] args) {
        Sandbox box = new Sandbox();
    }

    public Sandbox() {
        aMethod(subThread.class);
    }

    public void aMethod(Class<? extends superThread> type) {
        try {

            System.out.println("isInterface: "+type.isInterface());
            System.out.println("isAssignableFrom of subThread: "+type.isAssignableFrom(subThread.class));

            superThread t = type.newInstance();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private class superThread { // implements Runnable {

        public superThread() {}

        public void run() {}
    }

    private class subThread …
Run Code Online (Sandbox Code Playgroud)

java generics subclass instantiationexception

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

UTF-8 编码显示字符的最大代码点数是多少?

我们试图将表情符号插入数据库,但遇到了奇怪的行为。事实证明这与utf-8编码有关。会工作得很好,但不会。这是我们了解 utf-8 代码点的时候。是一个代码点长,但为 2:由 Hot Pepper (U+1F336) 和 Variation Selector-16 (U+FE0F) 组成。

\n

了解这一点后,我们将数据库存储宽度增加到2,这解决了 的问题,但我们发现了一个新问题。键帽表情符号 (1\xef\xb8\x8f\xe2\x83\xa32\xef\xb8\x8f\xe2\x83\xa33\xef\xb8\x8f\xe2\x83\xa3) 为 3 个字符:1 个 (U+31 )、变体选择器-16 (U+FE0F) 和组合封闭键帽 (U+20E3)。

\n

“好吧,”我们说,“把它增加到 4。” 然后 \xe2\x80\x8d\xe2\x99\x82\xef\xb8\x8f 带有 5 个代码点:Face Palm (U+1F926)、表情符号修改器 Fitzpatrick Type-3 (U+1F3FC)、零宽度连接器 ( U+200D)、男性符号 (U+2642) 和变体选择器-16 (U+FE0F)。我们进行了更多尝试,发现英格兰国旗由 7 个代码点组成:

\n
    \n
  • U+1F3F4:飘扬的黑旗
  • \n
  • U+E0067:标签拉丁文小写字母 G
  • \n
  • U+E0062:标签拉丁小写字母 B
  • \n
  • U+E0065:标签拉丁文小写字母 E
  • \n
  • U+E006E:标签拉丁文小写字母 N
  • \n
  • U+E0067:标签拉丁文小写字母 G
  • \n
  • U+E007F:取​​消标签
  • \n
\n

所以问题是,显示的 unicode 字符可以使用的最大代码点数量是多少?是否有任何代码点高于 7 个的表情符号(或其他 utf-8 字符)示例?

\n

此问题与问题UTF-8 编码字符的最大字节数是多少?类似但不同。。这需要一个 UTF-8 代码点,最大字节数是多少?(剧透:4)。 …

utf-8

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