我已经将 NodeJS 项目的代码更改为 Typescript。一切工作正常,除了显然第三方包(文件类型,https://www.npmjs.com/package/file-type)似乎不接受require编译.js文件中生成的。要改变这一点,我必须将 tsconfig.json 的“module”属性更改为“commonjs”以外的另一个值。然而,它破坏了代码并产生了很多问题。
我的 tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowJs": true,
"lib": ["ES6"],
"esModuleInterop": true,
"moduleResolution": "node",
"outDir": "build",
"rootDir": "src",
"skipLibCheck": true,
"strict": true
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
const filetype = __importStar(require("file-type"));
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\user\Desktop\repos\art-api\node_modules\file-type\index.js from C:\Users\user\Desktop\repos\art-api\build\middlewares\process-image.js not supported.
Instead change the require of index.js in C:\Users\user\Desktop\repos\art-api\build\middlewares\process-image.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\user\Desktop\repos\art-api\build\middlewares\process-image.js:33:31)
at Object.<anonymous> …Run Code Online (Sandbox Code Playgroud) 我是函数式编程的新手,刚刚遇到了在头部和尾部划分列表的模式。
\n我有Javascript和Python的背景,据我所知,这些语言不采用这种划分,因此它似乎是与函数式编程范式相关的设计,因为像Haskell这样的语言采用了这种划分。例如,在 F# 中(根据Understandingpatternmatchingwithconsoperator),列表是类型的可区分联合
\ntype list<\'T> = // \'\n | Nil\n | Cons of \'T * list<\'T>\nRun Code Online (Sandbox Code Playgroud)\n其中我们有一个 和 的[]并集\xe2\x80\x98a*list<\xe2\x80\x98a>。因此,列表是按设计“划分”的。
头部和尾部的划分列表与函数范式有何关系?它有助于解决哪些问题?我希望能找到一些例子来说明这种划分是有用的和/或需要的。
\n我目前正在关注《软件基础》一书,目前正在阅读“列表”章节。 然而,我很难理解模式匹配的具体情况,并且由于是 Coq 的初学者,我不确定如何找到这个问题的答案。
因此,练习是创建一个来计算列表(更具体地说是一个包)中有Fixpoint多少 nat 。vs
我决定为此使用模式匹配,但如果我尝试定义这样的函数:
Fixpoint count' (v: nat) (s: bag) : nat :=
match s with
| nil => O
| h :: t => match h with
| v => S (count' v t)
end
end.
Run Code Online (Sandbox Code Playgroud)
并尝试将此功能应用于,比方说,
Example test_count1: count' 1 [1;2;3;1;4;1] = 3.
Run Code Online (Sandbox Code Playgroud)
我最终会得到6 = 3。我的理解是,匹配h始终v是“true”,因此它最终会计算列表中的每个元素。
但为什么会发生这种情况呢?h我们如何使用模式匹配来比较和的值v?
PS:我已经使用比较 ifh和vequal 的辅助函数解决了这个练习,但我想知道这是否只能使用内置模式匹配来实现。