小编weg*_*gry的帖子

在 Java 中使用注释将所有参数设为最终参数

我最近一直在使用 Lombok,我想知道是否可以提供一个类级别或方法级别注释,为所有参数添加最终值。例如:

@finalizer
void foo(Bar bar);
Run Code Online (Sandbox Code Playgroud)

成为

void foo(final Bar bar);
Run Code Online (Sandbox Code Playgroud)

java annotations lombok

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

错误:变量“ x”仍在此深度重复

我正在尝试编写一个宏来计时各种功能执行的时间。

macro_rules! timer {
    ($( $x: expr ),+ ) => {
        let now = SystemTime::now();
        let val = $x;

        match now.elapsed() {
            Ok(elapsed) => {
                // This should include a message at some point alongside the timing
                println!("{}", elapsed.as_secs());
            }
            Err(e) => {
                println!("{:?}", e);
            }
        }
        val
    } 
}
Run Code Online (Sandbox Code Playgroud)

但是编译器抛出一个error: variable 'x' is still repeating at this depth

在另一种我在(F#)中尝试过的静态类型语言中,使用闭包是最简单的方法。在Rust中不可能有这样的通用宏吗?

macros rust

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

使用 ESM 在浏览器中动态或静态导入 json

我有以下示例运行,而 JS 在它上面没有打包器。

// index.js
;(async () => {
  const mod = await import('/index.json')

  console.log(mod)
})()
Run Code Online (Sandbox Code Playgroud)
{
  "file": "index.json"
}
Run Code Online (Sandbox Code Playgroud)

Chrome 80 无法加载 json

加载模块脚本失败:服务器以“application/json”的非 JavaScript MIME 类型响应。每个 HTML 规范对模块脚本执行严格的 MIME 类型检查。

Firefox 73 以类似的方式失败:

由于不允许的 MIME 类型(“application/json”),从“ http://127.0.0.1:8080/index.json ”加载模块被阻止。

这种行为是可以克服的吗?

javascript google-chrome dynamic-import es6-modules

5
推荐指数
2
解决办法
1113
查看次数

在Clojure中将比率格式化为百分比

这似乎应该是显而易见的,但是如何将Clojure的比率类型转换为百分比?

(format "%3s" (/ 1 2))
;; "1/2"

(format "%3f" (/ 1 2))
;; Throws an error
Run Code Online (Sandbox Code Playgroud)

clojure string-formatting

4
推荐指数
2
解决办法
1104
查看次数

Yarn 2 和 dart sass 与创建反应应用程序

我一直在尝试更新 Create React App 以使用 yarn 2 和即插即用 (PNP)。当我在 中使用nodeLinker: node-modules.yarnrc.yml,我可以成功启动开发服务器。没有它,我最终得到

./src/App.scss (./.yarn/$$virtual/css-loader-virtual-fe3fa7be11/0/cache/css-loader-npm-3.4.2-300ee159b3-2.zip/node_modules/css-loader/dist/cjs.js??ref--6-oneOf-5-1!./.yarn/cache/postcss-loader-npm-3.0.0-f4ab99b685-2.zip/node_modules/postcss-loader/src??postcss!./.yarn/cache/resolve-url-loader-npm-3.1.1-cf1a268137-2.zip/node_modules/resolve-url-loader??ref--6-oneOf-5-3!./.yarn/unplugged/sass-loader-virtual-14ae4e1150/node_modules/sass-loader/dist/cjs.js??ref--6-oneOf-5-4!./src/App.scss)
Error: A package is trying to access a peer dependency that should be provided by its direct ancestor but isn't

Required package: node-sass (via "node-sass")
Required by: sass-loader@virtual:74ba539c0b6c6c8346ea151c91664bff0bef13782983a6f90ddf1a26160140360771dcf40d0863b46ff7add674bc2c42a37daea25f24f4ea96f7843786460ecd#npm:8.0.2 (via /Users/me/color-contrast-matrix/.yarn/unplugged/sass-loader-virtual-14ae4e1150/node_modules/sass-loader/dist/)
Run Code Online (Sandbox Code Playgroud)

sass-loader yarnpkg

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

如何在 JS 中计算 BigInt 的模

给定 JS 中的 BigInt,如何计算它的模?

10n % 3 // Uncaught TypeError: can't convert BigInt to number
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题与类似链接的问题之间的区别在于,这个问题仅与 BigInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt相关,这是相当新的。

javascript bigint

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

使用Intellij和Scala安装和使用库

我正在尝试使用Scala的调度库,我来自Python背景,我可以

pip install dispatch
Run Code Online (Sandbox Code Playgroud)

然后从那里导入.我假设Scala不是这种情况.该分配位置有一个脚本,下载与SBT派遣,但我的IDE无法识别库.如何从项目中访问库?(我正在使用IntelliJ)

python import scala intellij-idea sbt

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

首先进行解构/拆包并在Clojure中休息

在Python 3中,如果要解压缩列表(或元组)的第一个和其余部分,则可以

x, *y = [1, 2, 3]
#x = 1, y = [2, 3]
Run Code Online (Sandbox Code Playgroud)

你如何在Clojure中的let块中执行此操作?我试着:as parts

(defn destructurer [vec]
  (let [[beginning the-rest :as parts] vec]
    [beginning the-rest]
    )
  )
;; (destructer [1 2 3])
;; [1 2] <- missing the 3
Run Code Online (Sandbox Code Playgroud)

clojure python-3.x iterable-unpacking

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

F# 交互式、NuGet 和外部库

我正在尝试在 OS X El Capitan 上的 Xamarin Studio 5.9.8 中运行 suave(一个网络服务器)的演示代码。

module ServerTest =
    open Suave                 // always open suave
    open Suave.Http.Successful // for OK-result
    open Suave.Web             // for config

    startWebServer defaultConfig (OK "Hello World!")
Run Code Online (Sandbox Code Playgroud)

当我实际构建代码时,它按预期工作。但是当我尝试以交互方式运行它时,使用 ctrl + return,我收到错误The namespace or module 'Suave' is not defined。我环顾四周,看起来可以与 Visual Studio 交互使用库。有没有办法让它在 OS X 上工作?

macos f# nuget xamarin suave

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

代码中的Pycharm和UTF-8

当我尝试运行此代码时:

with open('scraped.csv', 'w') as dump:
        writer = csv.writer(dump, delimiter='?', escapechar='\\',quoting=csv.QUOTE_NONE)
        writer.writerow(('author', 'ups', 'downs',
                         'score', 'sub_name', 'replys',
                         'created_utc', 'pulled down at'))
Run Code Online (Sandbox Code Playgroud)

在PyCharm中,我收到以下错误

  File ..., line 38, in <module>
'created_utc', 'pulled down at'))
UnicodeEncodeError: 'ascii' codec can't encode character '\u0119' in position 6: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

PyCharm无法在文件中支持UTF-8吗?或者我错过了某个地方的设置.Sublime中构建了相同的文件.

python utf-8 pycharm python-3.x

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

在Scala构造函数中是否有相当于Java的东西?

我有一个Scala 2.11构造函数,我希望其中一个类的属性与构造函数中的一个参数具有相同的名称.

class Transition( next: Int ) {
    this.next = next // works in Java
}
Run Code Online (Sandbox Code Playgroud)

这不编译.我想知道Scala中赋值语句的等价物是什么.

scala

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

使用Java 8和HashMap减少清洁地图

现在我有一个HashMap,我试图减少到总数.这是当前的代码:

HashMap map = new HashMap<String, Long>();
map.put("ex1", 757L);
map.put("ex2", 77L);

map.values()
   .stream()
   .reduce(0L, (a, b) -> a + b);
Run Code Online (Sandbox Code Playgroud)

有没有更优雅的方式来做到这一点?我想使用sum方法.

java reduce java-8 java-stream

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

如何从 TypeScript 中的未知类型对象读取密钥

以下代码片段无法使用typescript@4.0.2.

const foo: unknown = {bar: 'baz'}

if (foo && typeof foo === 'object' && 'bar' in foo) {
  console.log(foo.bar)
}
Run Code Online (Sandbox Code Playgroud)

tsc 错误是

Property 'bar' does not exist on type 'object'.
Run Code Online (Sandbox Code Playgroud)

如何确信tscfoo 可以拥有任意命名的键而无需显式强制转换?

这是游乐场链接。https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZiEAuGBXMBrMIDuYYC8MA3gEYCGATigOQUBeNAvgFAsCWcMAFAiDADIBMKAE8ADgFMQXPoQJEaIUgCtJwKDUHC6VLe3x8AlCRYwYoSCAA2kgHTWQAc16I7FSkZZMgA

structural-typing typescript

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