我有在F#和C#之间共享的代码,我需要在C#HashSet和F#Set之间进行转换.是否有捷径可寻?
在F#我有这个功能:
module ModuleName
let X (y: int -> unit) = ()
Run Code Online (Sandbox Code Playgroud)
我如何在C#中调用它?理想情况下它看起来像
ModuleName.X(x => x*x);
Run Code Online (Sandbox Code Playgroud)
但是这个lambda语法不会隐式转换为FSharpFunc.
鉴于我有IO功能:
// this can either be IO or some other side effect
//that makes the function less pure
printf "HI"
Run Code Online (Sandbox Code Playgroud)
我想测试IO被正确调用.测试IO被正确调用的必要解决方案是将IO语句包装在对象中,模拟对象,使用依赖注入传递对象,并验证使用正确的参数调用正确的方法.我想知道如果不是使用依赖注入来测试F#,更好的方法是检查函数的输出(通过声明返回正确的值或函数)并删除IO调用; 因此,通过消除IO调用的副作用,使功能再次变为纯净.
我正在考虑将所有IO包装在一个特殊的模块中.
let MyPrint print statement = print statement ; statement
Run Code Online (Sandbox Code Playgroud)
所以我可以删除IO函数并在我的测试中断言正确的操作发生如下:
被测代码:
let PrintHi = fun(print) -> MyPrint print "HI"
let DoNothing = fun(print) -> ()
let DoIf conditional =
if conditional then PrintHi
else DoNothing
Run Code Online (Sandbox Code Playgroud)
FsUnit:
[<Test>] member test.
let printStub value = ()
``Test Hi Is Printed When TRUE`` ()=
let testedFunc = DoIf true
testedFunc(printStub) …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查我的函数是否返回 Some(x)
testedFunc() |> should be (sameAs Some)
testedFunc() |> should be Some
testedFunc() |> should equal Some
Run Code Online (Sandbox Code Playgroud)
都行不通。我宁愿不使用:
match testedFunc() with
| Some -> Pass()
| None -> Fail()
Run Code Online (Sandbox Code Playgroud)
有人知道这样做的方法吗?
一个元组用管道输送:
let a = (1,2)
let f a b = ()
a ||> f
Run Code Online (Sandbox Code Playgroud)
一个三联管道:
let a = (1,2,3)
let f a b c = ()
a |||> f
Run Code Online (Sandbox Code Playgroud)
但这不适用于四重奏:
let a = (1,2,3,4)
let f a b c d= ()
a ||||> f
Run Code Online (Sandbox Code Playgroud)
你如何管理四重功能?
使用计算表达式时,第一个定义有效,但第二个定义不适用于Zero.
这有什么区别:
member o.Zero() = 3
Run Code Online (Sandbox Code Playgroud)
还有这个:
member o.Zero = fun() -> 3
Run Code Online (Sandbox Code Playgroud)
第一个评估到unit -> int第二个评估(unit -> int).有什么不同?
鉴于元组:
let tuple = (true, 1)
Run Code Online (Sandbox Code Playgroud)
我如何在条件中使用此元组?像这样的东西:
if tuple.first then //doesnt work
Run Code Online (Sandbox Code Playgroud)
要么
if x,_ = tuple then // doesnt work
Run Code Online (Sandbox Code Playgroud)
我不想这样做:
let isTrue value =
let b,_ = value
b
if isTrue tuple then // boring
Run Code Online (Sandbox Code Playgroud)
有没有一种很好的方法来评估条件中的元组值而不创建单独的函数?
当扩展记录时,编译器是否在内存中创建了一个新位置(深层复制?),或者编译器是否使记录可变并修改了值?
例如:
type MyRecord = { A : string
; B : string
}
let record = { A = "A"; B = "B" }
let record = { record with A = "new A" } //copy or overwrite?
Run Code Online (Sandbox Code Playgroud)
因为我覆盖record了编译器复制还是覆盖?是否存在性能问题?
我正在寻找一种方法将此列表减少为布尔值.这是原作:
let ones = [1;1;1;1]
let twos = [2;2;2;2]
let bad = [1;2;3]
let isAllOnes = List.forall (fun op -> op = 1)
let isAllTwos = List.forall (fun op -> op = 2)
let isOneOrTwo ops = isAllOnes ops || isAllTwos ops
isOneOrTwo ones |> should be True
isOneOrTwo twos |> should be True
isOneOrTwo bad |> should be False
Run Code Online (Sandbox Code Playgroud)
我试图用一种减少来重构这个.像这样的东西:
let isOneOrTwo ops = [isAllOnes; isAllTwos] |> List.tryFind (fun acc -> acc ops)
(isOneOrTwo ones).IsSome |> should be True …Run Code Online (Sandbox Code Playgroud) 我MixPanel.flush在我的onDestroy方法中调用,但看起来应用程序在MixPanel有机会发送/刷新其数据之前结束.
我没有在我的MixPanel分析屏幕中看到任何数据,除非我在调用onDestroy之后使用断点暂停我的Android应用程序MixPanel.flush().
有什么方法可以让我的应用程序保持打开,让MixPanel完成?