以下类型扩展名
module Dict =
open System.Collections.Generic
type Dictionary<'K, 'V> with
member this.Difference(that:Dictionary<'K, 'T>) =
let dict = Dictionary()
for KeyValue(k, v) in this do
if not (that.ContainsKey(k)) then
dict.Add(k, v)
dict
Run Code Online (Sandbox Code Playgroud)
给出错误:
签名和实现不兼容,因为类型参数'TKey'的声明需要形式为'TKey:equality的约束
但是当我添加约束时,它会给出错误:
此类型扩展的声明类型参数与原始类型"Dictionary < , >" 上的声明类型参数不匹配
这尤其神秘,因为以下类型扩展没有约束并且有效.
type Dictionary<'K, 'V> with
member this.TryGet(key) =
match this.TryGetValue(key) with
| true, v -> Some v
| _ -> None
Run Code Online (Sandbox Code Playgroud)
现在我有一些奇怪的想法:只有在访问某些成员时才需要约束吗?
我在F#中以非常标准的方式实施了Levenshtein Distance作为练习
let lastchar (s:string) = s.Substring(s.Length-1, 1)
let lastchar_substring (s:string) len = s.Substring(len-1, 1)
let rec levdist (sa:string) (sb:string) alen blen = match alen, blen with
| -1, -1 -> levdist sa sb sa.Length sb.Length
| 0, 0 -> 0
| _ , 0 -> alen
| 0, _ -> blen
| _ -> List.min [ (* How do I make this tail recursive...? *)
(levdist sa sb (alen-1) blen) + 1;
(levdist sa sb alen (blen-1)) + 1; …Run Code Online (Sandbox Code Playgroud) 什么是Prolog运算符^?
查看Prolog内置指令op,列出了内置运算符.
我看
**是取幂
/ \是或
但是^是什么?
当前三个答案中的每一个都是有价值的,我学到了一些东西:
我有一个NUnit单元测试,它是用普通的F#库编写的,但它是针对可移植类库中的F#代码.
当我运行此测试(在Visual Studio 2013中)时,我得到以下异常:
Result Message: System.MissingMethodException : Method not found:
'Microsoft.FSharp.Control.FSharpAsync`1<System.IO.TextReader> FSharp.Data.Runtime.IO.asyncReadTextAtRuntime(System.Boolean, System.String, System.String, System.String, System.String)'.
Run Code Online (Sandbox Code Playgroud)
这就是我在Portable Class Library中的app.config中所拥有的:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这就是我在普通F#库的app.config中所拥有的:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud) 考虑这个界面:
type A<'a> =
abstract X : 'a
Run Code Online (Sandbox Code Playgroud)
让我们尝试用它int作为通用参数来实现它:
{ new A<int> with member this.X = 5 } // all is well
Run Code Online (Sandbox Code Playgroud)
现在,让我们尝试unit一个论点:
// Compiler error: The member 'get_X : unit -> unit' does not have the correct type to override the corresponding abstract method.
{ new A<unit> with member this.X = () }
Run Code Online (Sandbox Code Playgroud)
现在,如果我们定义一个非泛型接口,一切都运行良好:
type A_int =
abstract X : int
{ new A_int with member this.X = 5 } // works
type A_unit = …Run Code Online (Sandbox Code Playgroud) 在F#编译器的同一范围内使用相同的变量两次时,没有警告或反馈.例如
let s = "abc"
let s = "def"
printfn "%A" s
Run Code Online (Sandbox Code Playgroud)
结果是
def
Run Code Online (Sandbox Code Playgroud)
我已经看到
有没有办法在Visual Studio中对F#中的值进行阴影警告?
F#值阴影 - 是否可以在同一范围内禁用值阴影
有没有办法通过编译器警告或在编辑器中可视化获得有关阴影变量的反馈.如何才能做到这一点?
我已经宣布了这样一个元组:
module MyModule =
let private INVALID_TUPLE = ("0", DateTime.MinValue)
Run Code Online (Sandbox Code Playgroud)
当我在模块中引用它时,它总是为null:
let private invalidForNone someOtherTuple =
match someOtherTuple with
| None -> INVALID_TUPLE // it's null
| Some(t) -> t
Run Code Online (Sandbox Code Playgroud)
此外,当我在元组声明上放置一个断点时,它永远不会命中.
如果我在脚本(fsx)文件中执行完全相同的操作,启动调试,执行,元组声明命中的断点以及对元组的引用是好的.
ILSpy for my module显示生成了一些启动代码,其中包含一个创建INVALID_TUPLE的Main方法.显然,这不是出于某种原因运行?
这是一个重现行为的示例(现在我意识到它与MSTest执行代码的方式有关).从C#单元测试中调用它; 结果将为null.实际上,F#代码中的断点根本不会执行.
module NullTupleTest
open System
let private INVALID_TUPLE = ("invalid", DateTime.MinValue)
let private TupleTest someTuple =
match someTuple with
| None -> INVALID_TUPLE
| Some(dt) -> dt
let Main = TupleTest None
Run Code Online (Sandbox Code Playgroud) MyRule1和MyRule2MyRule2 依赖于取决于 MyRule1MyRule1因此,“ before”方法应在MyRule2“ before”方法之前运行。在JUnit 4中,可以通过RuleChain通过这种方式实现:
static MyRule1 myRule1 = new MyRule1();
static MyRule2 myRule2 = new MyRule2(myRule1);
@Rule
TestRule ruleChain = RuleChain.outerRule(myRule1)
.around(myRule2);
Run Code Online (Sandbox Code Playgroud)
在JUnit 5中,我必须以这种方式实现它:
static MyRule1 myRule1 = new MyRule1();
@RegisterExtension
static MyRule2 myRule2 = new MyRule2(myRule1);
Run Code Online (Sandbox Code Playgroud)
与MyRule2:
class MyRule2 implements BeforeAllCallback {
private final MyRule1 myRule1;
public MyRule2(MyRule1 myRule1) {
this.myRule1 = myRule1;
}
@Override
public void beforeAll(ExtensionContext extensionContext) {
this.myRule1.beforeAll();
X x = …Run Code Online (Sandbox Code Playgroud) 假设我有两个浮点数A和B的向量。我需要找到A和B的点积。sign(AB)-如果它是正数或负数或0。向量的大小很小,小于100。但是,我需要非常快地执行此操作!
您可以假设A中的所有元素都是[0,1]范围内的浮点数,B中的所有元素都是[-500,+ 500]。我一直在寻找精确的解决方案,但是如果实际上没有给出很多错误的答案,近似的解决方案也可以(我知道,“很多”是主观的,但是如果不谈论硬件或实现,我就无法在上面给出确切的数字) )
我使用-O4探索了速度最快的Pragma编译器指令。我探索了实现方面的更多改进,使其能够基于基础处理器的自动矢量化支持实现可并行化。像对于avx指令集一样,保留8个独立变量并找到点积,以便利用全部8个寄存器容量。但是我认为我们仍然可以更快!基本思想是,我们只需要确定点积的符号,因此在精度与速度之间进行权衡存在很大范围。因此,我正在尝试提出一些数学或算法解决方案来实现这种折衷。我的一个想法是使用FFT(快速傅立叶变换)来减少乘法次数。我尝试探索的另一个想法是按位技巧,但意识到按位进行浮动是不可能的。
您可能会想为什么优化如此小的任务如此重要,但是我认为这可能是一个非常有用的问题:
是否可以使用 TensorFlowJS 从 OpenAI GPT-2 生成文本?
如果不是,限制是什么,例如模型格式或...?
f# ×6
algorithm ×2
c++ ×1
dictionary ×1
f#-data ×1
gpt-2 ×1
iso-prolog ×1
java ×1
junit ×1
junit5 ×1
lambda ×1
libraries ×1
nlp ×1
ocaml ×1
optimization ×1
performance ×1
prolog ×1
prolog-setof ×1
shadowing ×1
tensorflow ×1
tuples ×1
unit-type ×1