是否可以为专用/构造的泛型类型扩展泛型类?我想用一种方法扩展Int Arrays以计算其元素的总和.
例如
extension Array<Int> {
func sum() -> Int {
return reduce(0) { $0 + $1 }
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以将Clang/LLVM与Eclipse CDT一起使用?如果是这样,它如何配置为实际使其工作?
是否可以(ab)使用Cabal使用简单的命令创建一个通用的Haskell项目,类似于使用Sbt或Maven在Scala世界中可以做的事情?
例如
> cabal create AwesomeProject
> ls
AwesomeProject.hs awesomeProject.cabal LICENSE README Setup.hs
Run Code Online (Sandbox Code Playgroud)
或者还有其他工具吗?
我试图在.NET核心(2.0)控制台应用程序中指定程序集版本,以便我可以通过以下方式以编程方式访问它:
open System.Reflection
let main argv =
printfn "Assembly Version is: %s" <| Assembly.GetEntryAssembly().GetName().Version.ToString()
0
Run Code Online (Sandbox Code Playgroud)
将版本字段添加到我的.fsproj文件的属性组中,例如:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Version>1.0.0.1</Version>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
不会更改我的测试应用程序打印的版本(它保持在0.0.0.0).
什么工作是添加AssemblyInfo.fs文件,其中设置AssemblyVersion属性,但如果可能的话我想避免这种情况并使用.fsproj文件.这可能吗?
我也很高兴有一个指针,指向我一般可以找到关于.fsproj的文档.
是否可以在本地限制模块的导入,最好将其与Module Abbreviations
?目标是避免使用导入符号来污染当前模块.
例如(受OCaml启发)类似的东西:
let numOfEvenIntegersSquaredGreaterThan n =
let module A = Microsoft.FSharp.Collections.Array in
[|1..100|] |> A.filter (fun x -> x % 2 = 0)
|> A.map (fun x -> x * x)
|> A.filter (fun x -> x > n)
|> A.length
let elementsGreaterThan n =
let module A = Microsoft.FSharp.Collections.List in
[1..100] |> A.filter (fun x -> x > n)
Run Code Online (Sandbox Code Playgroud)
另外有没有办法实现与命名空间类似的东西?
假设我有以下代码:
type Vehicle =
| Car of string * int
| Bike of string
let xs = [ Car("family", 8); Bike("racing"); Car("sports", 2); Bike("chopper") ]
Run Code Online (Sandbox Code Playgroud)
我可以使用不完整的模式匹配过滤上面的列表,如下所示:
> for Car(kind, _) in xs do
> printfn "found %s" kind;;
found family
found sports
val it : unit = ()
Run Code Online (Sandbox Code Playgroud)
但它会导致:warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Bike (_)' may indicate a case not covered by the pattern(s). Unmatched elements will be ignored.
由于忽略了无与伦比的元素是我的意图,是否有可能摆脱这种警告?
是否有一种方法可以使列表推导工作,而不会导致MatchFailureException?例如:
> …
Run Code Online (Sandbox Code Playgroud) 给定谓词"p",它表示解决方案是否足够好.成本函数"f"表示可能的解决方案有多好,以及在一系列可能的解决方案中搜索"最佳"(即最低成本)解决方案的函数.如何取消评估的惯用方法 - 如果谓词确保当前解决方案"足够好" - 看起来像.
即是这样的:
let search p f solutionSpace =
solutionSpace |> Seq.map (fun x -> f x, x)
|> Seq.ignoreAllFollowingElementsWhenPredicateIsTrue (fun (c, s) -> p c)
|> Seq.minBy (fun (c, _) -> c)
Run Code Online (Sandbox Code Playgroud) 我无法理解默认参数如何与命名函数中的多个子句交互.归结起来,为什么以下片段有效?
defmodule Lists do
def sum([], total \\ 0), do: total
def sum([h|t], total), do: h + sum(t, total)
end
Run Code Online (Sandbox Code Playgroud)
根据我的理解,编译器将其扩展为:
defmodule Lists do
def sum([]), do: sum([], 0)
def sum([], total), do: total
def sum([h|t], total), do: h + sum(t, total)
end
Run Code Online (Sandbox Code Playgroud)
所以我希望发生以下情况:
iex(1)> Lists.sum [1,2,3,4]
** (FunctionClauseError) no function clause matching in Lists.sum/1
Run Code Online (Sandbox Code Playgroud)
相反,它有效:
iex(1)> Lists.sum [1,2,3,4]
10
Run Code Online (Sandbox Code Playgroud)
使用Elixir 0.12.4.
我想为Quickcheck生成随机浮点数,这些浮点数限制在某个范围内,例如 0.0 到 1.0,用于测试处理概率的函数。我希望能够做一些成功的事情:
quickcheck! {
fn prop(x: f64, y: f64) -> bool {
assert!(x <= 1.0);
assert!(y <= 1.0);
(x * y < x) && (x * y < y)
}
}
Run Code Online (Sandbox Code Playgroud)