为什么 Typescript 的人会创建infer
关键字?根据文档,这是您将如何使用它的示例:
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
Run Code Online (Sandbox Code Playgroud)
我不明白为什么需要这样做。为什么不能只是:
type ReturnType<T> = T extends (...args: any[]) => R ? R : any;
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?为什么infer
需要关键字?
我正在研究功能组合并有一个例子:
Function<String, String> test = (s) -> s.concat("foo");
String str = test.andThen(String::toUpperCase).apply("bar");
Run Code Online (Sandbox Code Playgroud)
该示例按预期编译并运行。但是,如果我使用 更改组合的顺序compose()
,则需要显式转换:
String str = test.compose((Function <String, String>)
String::toUpperCase).apply("bar");
Run Code Online (Sandbox Code Playgroud)
如果没有显式转换String::toUpperCase
为Function <String, String>
,则会出现编译器错误:
Error:
incompatible types: cannot infer type-variable(s) V
(argument mismatch; invalid method reference
incompatible types: java.lang.Object cannot be converted to java.util.Locale)
String s = test.compose(String::toUpperCase).apply("bar");
^-------------------------------^
Run Code Online (Sandbox Code Playgroud)
问题是为什么compose()
需要显式强制转换,而andThen()
在这种情况下不需要?
我知道有很多关于此的问题(和答案),但是当使用 .net6 和 automapper 11.01.1 时,这些问题都不适合我。他们似乎已经删除了其中的许多Ignore
,IgnoreAllUnmapped
并且ForAllOtherMembers
在最新的 automapper 中。如果我使用ignore with ForAllMembers
(before或after ForMember
),它将忽略所有字段,甚至是我用地图指定的字段。
问题:我有两个具有相同名称字段的类,但我只想映射一些并忽略其余的。(请不要说“为什么需要自动映射器”,这不是这里的问题)。
在这种情况下我需要使用自动映射器,但不确定他们是否不再支持这个?我可能错过了一个nuget吗?我只使用“AutoMapper 11.01.1”
public class User1
{
public string Name { get; set; } = "Foo";
public int Age { get; set; } = 7;
public string Phone { get; set;} = "123456789";
}
public class User2
{
public string FirstLastName { get; set; }
public int Age { get; set; }
public string Phone { get; set; } …
Run Code Online (Sandbox Code Playgroud) 我目前正在使用 travis ci 在补丁进入 github 时检查补丁,并试图找出 clang-format 3.9(因为 travis ci 目前仅支持最新的 ubuntu 14.04)以在扫描时忽略整个目录或文件变化。
我的 .travis.yml 文件:
language: c++
sudo: required
dist: trusty
install:
- sudo apt-get update
- sudo apt-get install clang-format-3.9 python3
- ./travisci/check_patch.py
Run Code Online (Sandbox Code Playgroud)
我的 travisci/check_patch.py 文件:
#!/usr/bin/env python3
from subprocess import Popen, PIPE, STDOUT
# Run clang to check if code changes cause a diff output and return 1 if so.
cmd = "git show origin/master..@ | clang-format-diff-3.9 -p 1 -style=file"
diff = Popen(cmd, stdout=PIPE, shell=True).communicate()[0]
if diff: …
Run Code Online (Sandbox Code Playgroud) Gcloudignore 的工作方式类似于 gitignore,因为您可以将某些文件从上传到 GCF 中排除。有时,当您拥有包含大量生成文件的大型项目时,排除除少数文件之外的所有文件会很有用。
.gcloudignore
# Ignore everything
# Or /*
*
# Except the Cloud Function files we want to deploy
!/package.json
!/index.js
Run Code Online (Sandbox Code Playgroud)
下面的 gcloudignore 文件给了我们:File index.js or function.js that is expected to define function doesn't exist in the root directory.
意思是 index.js 被忽略并且无法读取。
但是,以下忽略文件语法适用于部署:
# Ignore everything
/[!.]*
/.?*
# Except the Cloud Function files we want to deploy
!/package.json
!/index.js
Run Code Online (Sandbox Code Playgroud)
我尝试查看 gcloud 程序代码,但我想知道是否有人知道为什么会这样?
对于我的具体情况,我想减少使用功能组合;例如:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
这有一个编译错误:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
相反,我需要这样做:
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce((a, b) -> Sets.intersection(a, b).immutableCopy())
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
然而,这失去了组合提供的无点风格。
为什么 Stream API …
我使用除法算法。
根据https://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations,除法具有时间复杂度(以下之一):
O(n log n log log n)
O(n log n 2O(log* n))
O(n**2)
O(M(n))
Run Code Online (Sandbox Code Playgroud)
到目前为止,我在 Python 中使用了这个算法,但我需要在平台上独立描述它。对于今天的 Python(或类似语言)用户来说,这些时间复杂度定义中的哪一个是正确的?
我正在尝试序列化一个我无法触及任何内容的类。问题是我想忽略一些getWhatever() getter 方法,但我无法使用@JsonIgnore标记 getter 方法,因为这意味着触及 DTO 类。
该序列化是通过 Spring REST Web 服务中的@RestController方法进行的,因此如果它能成为考虑到这一点的解决方案,那就太好了。
我想到了一个我不喜欢的解决方案...它将为我想要序列化的 DTO 类创建一个自定义序列化器,这样我就可以控制什么被序列化,什么不被序列化,然后,从@RestController,不是返回 DTO 类(我认为这是更优雅的),而是在使用ObjectMapper获取 JSON 字符串并强制自定义序列化程序后返回String 。
我不喜欢这个解决方案,因为:
预先感谢您...任何帮助将不胜感激
编辑(解决方案) 由于@Cassio Mazzochi Molin 的想法,我终于采用了这个解决方案:
interface FooMixIn {
@JsonIgnore
Object getFoo();
@JsonIgnore
Object getBar();
}
@Service
public class ServiceFooSerializer extends SimpleModule{
public ServiceFooSerializer(){
this.setMixInAnnotation(Foo.class, FooMixIn.class);
}
}
Run Code Online (Sandbox Code Playgroud) 在回答这个关于捕获局部变量的 lambda 的问题时,我定义了一个简单的 lambda 来捕获一个局部变量,并表明 lambda 有一个包含该变量值的字段。根据各种来源(例如此处、此处),当 lambda 捕获局部变量时,其值存储在“合成”字段中。Java 虚拟机规范(第4.7.8 节)似乎暗示了这一点,其中说:
未出现在源代码中的类成员必须使用 Synthetic 属性进行标记,否则必须设置其 ACC_SYNTHETIC 标志。此要求的唯一例外是编译器生成的方法,它们不被视为实现工件,即表示 Java 编程语言的默认构造函数的实例初始化方法(第 2.9.1 节)、类或接口初始化方法(第 2.9.2 节) ),以及 Enum.values() 和 Enum.valueOf() 方法。
lambda 的字段不是定义的异常之一,并且 lambda 的字段未在源代码中声明,因此根据我的理解,该字段应该是根据此规则合成的。
通过反射可以很容易地证明场的存在。但是,当我使用该Field.isSynthetic
方法进行检查时,它实际上返回false
. 这种方法的文档说:
如果此字段是合成字段,则返回 true;否则返回 false。
我正在 Java 10.0.1 中使用 JShell 进行测试:
> class A { static Runnable a(int x) { return () -> System.out.println(x); } }
| created class A
> Runnable r …
Run Code Online (Sandbox Code Playgroud) 例如,
int(x)
float(x)
str(x)
它们的时间复杂度是多少?
java ×3
java-8 ×2
python ×2
automapper ×1
c# ×1
clang ×1
clang-format ×1
closures ×1
dto ×1
gcloud ×1
jackson ×1
java-10 ×1
java-stream ×1
keyword ×1
lambda ×1
mapping ×1
python-3.x ×1
reducing ×1
spring ×1
travis-ci ×1
typescript ×1