小编Mau*_*eys的帖子

测量字符串的相同性(在Javascript中)

原则上这个问题可以解决与语言无关的问题,但具体来说我正在寻找一个Javascript实现.

是否有任何库可以让我测量两个字符串的"相同性"?更一般地说,有没有任何算法可以实现这一点,我可以实现(在Javascript中)?

以下面的字符串为例

地球下地幔中自旋跃迁中单晶镁碳化物的异常弹性

并且还要考虑以下,略微调整的字符串.请注意不同的粗体部分

在Sart - Transition的Eart hs下Mant le中,Sin gle Cry stal Magne sio-Sid erite的异常弹性.

Javascript的本机相等运算符不会告诉你很多关于这些字符串之间的关系.在这种特殊情况下,您可以使用正则表达式匹配字符串,但一般情况下只有在您知道期望的差异时才有效.如果输入字符串是随机的,则此方法的一般性会很快崩溃.

方法......我可以想象编写一个算法,将输入字符串拆分为任意数量N的子串,然后将目标字符串与所有这些子字符串进行匹配,并使用匹配量作为相同度的度量.但这感觉就像一个没有吸引力的方法,我甚至不想考虑O将依赖多大N.

在我看来,这种算法中有很多自由参数.例如,字符的区分大小写是否应该对测量的贡献与字符的顺序保存相同/更多/更少,似乎是设计者可以做出的任意选择,即:

identicality("Abxy", "bAxy")identicality("Abxy", "aBxy")

更具体地定义需求...... 第一个例子是我可以使用它的场景.我正在加载一堆字符串(学术论文的标题),我检查我的数据库中是否有它们.但是,源可能包含拼写错误,约定,错误等等的差异,这使匹配变得困难.在这个特定场景中,可能有一种更简单的方法来匹配标题:因为你可以预期可能出现的问题,这可以让你写下一些正则表达式的野兽.

javascript string algorithm comparison

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

推断关键字以从基类获取泛型类型

考虑下面的类

class SomeBaseClass<T extends string | number> {
    ...
}
Run Code Online (Sandbox Code Playgroud)

以及以下使用infer关键字的条件类型

type ExtractInner<T> = T extends SomeBaseClass<infer U> ? U : T;
Run Code Online (Sandbox Code Playgroud)

有了这个,我们可以SomeBaseClass像这样提取使用的泛型类型(这或多或少来自文档)

type InferredString = ExtractInner<SomeBaseClass<string>>  // InferredString is just type string
Run Code Online (Sandbox Code Playgroud)

在我稍微更高级的场景中,情况如下

class StringVersion extends SomeBaseClass<string> {
    ...
}
class NumberVersion extends SomeBaseClass<number> {
    ...
}
Run Code Online (Sandbox Code Playgroud)

如何定义一种类型ExtractInnerWithExtend,使其等同于ExtractInner?IE:

type InferredStringWithExtend = ExtractInnerWithExtend<StringVersion>  // InferredStringWithExtend should be string
type InferredNumberWithExtend = ExtractInnerWithExtend<NumberVersion>  // InferredStringWithExtend should be number
Run Code Online (Sandbox Code Playgroud)

ExtractInner在这种情况下使用将导致简单的string | number …

typescript typescript-typings typescript2.0

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

在接口C#8中使用静态,内部和受保护的访问修饰符

C#8现在支持接口成员上的访问修饰符,它的用法使我感到困惑。请看下面的例子

public interface IFoobar 
{   // these members are all valid
    protected string Protected { get; set; }
    internal string Internal { get; set; }
    static string Static { get; set; }
}

public class Foobar : IFoobar // <-- error, Internal and Protected members not implemented
{
    protected string Protected { get; set; }
    internal string Internal { get; set; }
    static string Static { get; set; } // only this one implements IFoobar
}
Run Code Online (Sandbox Code Playgroud)

我的期望是Foobar上述将完全实现IFoobar …

c# c#-8.0

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

在Typescript中的静态方法中访问类类型参数的解决方法

以下错误

静态成员不能引用类类型参数。

以下代码的结果

abstract class Resource<T> {
    /* static methods */
    public static list: T[] = [];
    public async static fetch(): Promise<T[]> {
        this.list = await service.get();
        return this.list;
    }
    /*  instance methods */ 
    public save(): Promise<T> {
        return service.post(this);
    }
}

class Model extends Resource<Model> {
}

/* this is what I would like, but the because is not allowed because :
"Static members cannot reference class type parameters."
*/

const modelList = await Model.fetch() // inferred type would …
Run Code Online (Sandbox Code Playgroud)

javascript generics inheritance typescript

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