Google提供了大量在F#字典(或其他集合)中添加和删除条目的示例.但我没有看到相当于的例子
myDict["Key"] = MyValue;
Run Code Online (Sandbox Code Playgroud)
我试过了
myDict.["Key"] <- MyValue
Run Code Online (Sandbox Code Playgroud)
我也试图将词典声明为
Dictionary<string, mutable string>
Run Code Online (Sandbox Code Playgroud)
以及这方面的几个变种.然而,我都打不上的正确组合,但...如果它是实际上可能在F#.
编辑:违规代码是:
type Config(?fileName : string) =
let fileName = defaultArg fileName @"C:\path\myConfigs.ini"
static let settings =
dict[ "Setting1", "1";
"Setting2", "2";
"Debug", "0";
"State", "Disarray";]
let settingRegex = new Regex(@"\s*(?<key>([^;#=]*[^;#= ]))\s*=\s*(?<value>([^;#]*[^;# ]))")
do File.ReadAllLines(fileName)
|> Seq.map(fun line -> settingRegex.Match(line))
|> Seq.filter(fun mtch -> mtch.Success)
|> Seq.iter(fun mtch -> settings.[mtch.Groups.Item("key").Value] <- mtch.Groups.Item("value").Value)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
System.NotSupportedException: This value may not be mutated
at Microsoft.FSharp.Core.ExtraTopLevelOperators.dict@37-2.set_Item(K key, V value)
at …Run Code Online (Sandbox Code Playgroud) CORE文档向我展示了如何快乐地模拟各种构建的Perl函数.但是,我不确定如何替换'-d'和c.用我的方法.所以这实际上只是一个关于如何在CORE :: GLOBAL中用短划线替换函数的问题.
手动参考会很好.
package Testing::MockDir;
use strict;
use warnings;
use Exporter();
use Symbol 'qualify_to_ref';
*import = \&Exporter::import;
our @EXPORT_OK = qw(SetMockDir UnsetMockDir);
our %EXPORT_TAGS = (
'all' => \@EXPORT_OK,
);
my %path2List = ();
my %handle2List = ();
BEGIN {
*CORE::GLOBAL::opendir = \&Testing::MockDir::opendir;
*CORE::GLOBAL::readdir = \&Testing::MockDir::readdir;
*CORE::GLOBAL::closedir = \&Testing::MockDir::closedir;
######################### the "-" is really the problem here
*CORE::GLOBAL::-d = \&Testing::MockDir::mock_d; # This does not work <<<<<
}
sub mock_d ($) {
die 'It worked';
}
sub SetMockDir {
my …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的rc4加密/解密实用程序作为第一个项目.我一直试图将给定的字符串转换为字节数组,然后可以通过核心算法进行操作.如何将字符串转换为函数f#中的字节数组?
//From another thread
let private replace find (repl : string) (str : string) = str.Replace(find, repl)
//let private algorithm bytes = blah blah blah
let Encrypt (decrypted : string) =
decrypted.Chars
|> Array.map(fun c -> byte.Parse(c)) // This line is clearly not working
// |> algorithm
|> BitConverter.ToString
|> replace "-" ""
Run Code Online (Sandbox Code Playgroud)
在C#中的FYI看起来像:
public static string Encrypt(string decrypted)
{
byte[] bytes = new byte[decrypted.Length];
for (int i = 0; i < decrypted.Length; ++i)
bytes[i] = (byte)decrypted[i];
Algorithm(ref bytes);
return …Run Code Online (Sandbox Code Playgroud) 我有一个简单的自定义异常,如下所示,但我不喜欢使用Throw函数,我真的不喜欢同时使用Throw和Throw2函数.有更优雅的方式吗?有没有一种方法可以在没有中间函数的情况下直接抛出MyError或Error?
#light
module Utilities.MyException
type MyError(code : int, msg : string) =
member e.Msg = msg
member e.Code = code
new (msg : string) = MyError(0, msg)
exception Error of MyError
let public Throw (msg : string) =
let err = new MyError(msg)
raise (Error err)
let public Throw2 (code : int) (msg : string) =
let err = new MyError(code, msg)
raise (Error err)
Run Code Online (Sandbox Code Playgroud)
我正在使用它,如下所示,但我想使用其中一个不起作用的变体
Throw(System.String.Format("Could not parse boolean value '{0}'", key))
//The string isn't of the …Run Code Online (Sandbox Code Playgroud) 我无法让用户为他们的服务器创建真正的证书,但我想做一些安全检查.因此,以下内容太轻,因为在我阅读时,没有检查证书.
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Run Code Online (Sandbox Code Playgroud)
您建议我让客户检查x509证书?鉴于我使用的是.NET语言(c#/ f#).
我正在编写一个简单的ini文件解析器,我在"do"子句中初始化对象时遇到了一些问题.它希望我返回一个单位,但如果我尝试输入"忽略"或直接返回"()",我无法获得空白功能来执行副作用.
此代码作为单独的函数工作,因为我可以忽略结果.
#light
module Utilities.Config
open System
open System.IO
open System.Text.RegularExpressions
open System.Collections.Generic
type Config(?fileName : string) =
let fileName = defaultArg fileName @"C:\path\myConfigs.ini"
static let defaultSettings =
dict[ "Setting1", "1";
"Setting2", "2";
"Debug", "0";
"State", "Disarray";]
let settingRegex = new Regex(@"\s*(?<key>([^;#=]*[^;#= ]))\s*=\s*(?<value>([^;#]*[^;# ]))")
let fileSettings = new Dictionary<string, string>()
let addFileSetting (groups : GroupCollection) =
fileSettings.Add(groups.Item("key").Value, groups.Item("value").Value)
do File.ReadAllLines(fileName)
|> Seq.map(fun line -> settingRegex.Match(line))
|> Seq.filter(fun mtch -> mtch.Success)
|> Seq.map(fun mtch -> addFileSetting(mtch.Groups) // Does not …Run Code Online (Sandbox Code Playgroud)