我正在使用StackExchange.Redis来访问Redis实例.
我有以下工作C#代码:
public static void Demo()
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("xxx.redis.cache.windows.net,ssl=true,password=xxx");
IDatabase cache = connection.GetDatabase();
cache.StringSet("key1", "value");
}
Run Code Online (Sandbox Code Playgroud)
这是我希望的等效F#代码:
let Demo() =
let cx = ConnectionMultiplexer.Connect @"xxx.redis.cache.windows.net,ssl=true,password=xxx"
let cache = cx.GetDatabase()
cache.StringSet("key1", "value") |> ignore
Run Code Online (Sandbox Code Playgroud)
但是这不会编译 - '没有重载匹配方法StringSet'.StringSet方法需要RedisKey和RedisValue类型的参数,并且在C#中似乎有一些编译器魔法将调用代码中的字符串转换为RedisKey和RedisValue.F#中似乎不存在魔法.有没有办法达到同样的效果?
我试图通过使用以下Json Schema代码在代码中定义架构来复制以下示例Newtonsoft.Json.Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
},
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": { "$ref": "#/definitions/address" }
}
Run Code Online (Sandbox Code Playgroud)
这和我到目前为止一样接近.(示例在F#中,但也可能在C#中.)
码:
open Newtonsoft.Json.Schema
open Newtonsoft.Json.Linq
let makeSchema =
let addressSchema = JSchema()
addressSchema.Properties.Add("street_address", JSchema(Type = Nullable(JSchemaType.String)))
addressSchema.Properties.Add("city", JSchema(Type = Nullable(JSchemaType.String)))
addressSchema.Properties.Add("state", JSchema(Type = Nullable(JSchemaType.String)))
addressSchema.Required.Add …Run Code Online (Sandbox Code Playgroud) 我试图了解有歧视的工会和记录类型; 具体如何组合它们以获得最大的可读性.这是一个例子 - 比如运动队可以得分(联赛得分和球门差异),或者可以暂停联赛,在这种情况下它没有积分或球门差异.这是我试图表达的方式:
type Points = { LeaguePoints : int; GoalDifference : int }
type TeamState =
| CurrentPoints of Points
| Suspended
type Team = { Name : string; State : TeamState }
let points = { LeaguePoints = 20; GoalDifference = 3 }
let portsmouth = { Name = "Portsmouth"; State = points }
Run Code Online (Sandbox Code Playgroud)
问题出现在最后一行的末尾,我说'State = points'.我得到'表达式应该有类型TeamState,但这里有类型点'.我该如何解决这个问题?
我有一个简单的Suave.io服务器形式:
let Ok o = o |> JsonConvert.SerializeObject |> Successful.OK
let NotOk o = o |> JsonConvert.SerializeObject |> RequestErrors.BAD_REQUEST
type Result<'T> =
| Success of 'T
| Failure of string
let DoThing someParam anotherParam =
let stats = Success(999) // <- business code here
match stats with
| Success s -> s |> Ok
| Failure m -> m |> NotOk
...
let app =
choose
[ GET >=> choose
[
pathScan "/someroute/%i/%i" (fun (p1, p2) ->
DoThing p1 p2) …Run Code Online (Sandbox Code Playgroud) 我正在编写FSCheck生成器来创建具有以下属性的字符串:
这是我的生成器代码:
namespace Example
open FsCheck.Arb
module public Generation =
let hasChars (s : string) =
(isNull s |> not)
&& s.Length > 0
let isTrimmed (s : string) =
s.Trim().Length = s.Length
let isContinuous (s : string) =
s
|> Seq.exists ((=) ' ')
|> not
[<AbstractClass; Sealed>]
type public Generators = class end
type public ContinuousString = ContinuousString of string with
member x.Get = match x with ContinuousString r -> r
override x.ToString() = x.Get …Run Code Online (Sandbox Code Playgroud)