F#和系列,如何编写系列。选择?

F#和系列,如何编写系列。选择?,f#,deedle,F#,Deedle,我决定写一个系列。选择函数,类似于我们在Seq上的函数,我想知道你是否认为这是最好的编写方法,回到Seq,因为那里有一个函数 let choose (f: 'K * 'V -> ('K * 'U) option) (e : Series<'K,'V>) = e |> Series.map (fun k v -> k, v) |> Series.values |> (Seq.choose f)

我决定写一个系列。选择函数,类似于我们在Seq上的函数,我想知道你是否认为这是最好的编写方法,回到Seq,因为那里有一个函数

let choose (f: 'K * 'V -> ('K * 'U) option) (e : Series<'K,'V>) =
        e |> Series.map (fun k v -> k, v)
          |> Series.values
          |> (Seq.choose f)
          |> Series.ofValues
          |> Series.map (fun _ v -> snd v)
          |> Series.indexWith (e |> Series.keys)
让我们选择(f:'K*'V->('K*'U)选项)(e:系列)=
e |>Series.map(乐趣k v->k,v)
|>系列值
|>(序号f)
|>数值系列
|>Series.map(乐趣->社交网络)
|>Series.indexWith(e |>Series.keys)
谢谢

几乎就是您所需要的,它只是有一个稍微不同的签名(特别是,不能更改密钥)。如果您确实需要更改按键,则会变得更加困难,但仍然不需要通过
Seq
。这还取决于您希望如何处理
e
中缺少的值

未经测试(因为我这里没有Visual Studio),并假设
e
没有缺失值,但应该给出以下想法:

let choose (f: 'K * 'V -> ('K * 'U) option) (e : Series<'K,'V>) =
    e |> Series.mapAll (fun k (Some v) -> f (k, v)) // Series<'K, ('K, 'U)> 
      |> Series.dropMissing // drops cases where f returned None
      |> fun e1 -> e1.SelectKeys (fun kvp -> fst kvp.Value.Value) // changes the keys to the ones f returned
      |> Series.mapValues snd // removes the keys from the values
让我们选择(f:'K*'V->('K*'U)选项)(e:系列)=
e |>Series.mapAll(funk(Some v)->f(k,v))//Series这里是我为我的“工具箱”写的东西。以防万一这会为将来的某个人节省时间

    /// Choose only on values keeping the same keys
    let chooseV (f: 'V -> 'U option) (e : Series<'K,'V>) =
        e |> Series.mapAll (fun _ (Some v) -> f v)
          |> Series.dropMissing

    /// Choose on both keys and values, changing the keys to type 'J
    let chooseKV (f: 'K * 'V -> ('J * 'U) option) (e : Series<'K,'V>) =
        e |> Series.mapAll (fun k (Some v) -> f (k, v))
          |> Series.dropMissing
          |> fun e1 -> e1.SelectKeys (fun kvp -> fst kvp.Value.Value)
          |> Series.mapValues snd

    /// Choose on just the values using the keys in the function
    let choose (f: 'K * 'V -> ('K * 'U) option) (e : Series<'K,'V>) =
        e |> Series.mapAll (fun k (Some v) -> f (k, v))
          |> Series.dropMissing
          |> Series.mapValues snd 
///仅在保留相同键的值上选择
让我们选择(f:'V->'U选项)(e:系列)=
e |>Series.mapAll(fun(一些v)->f v)
|>Series.dropMissing
///选择键和值,将键更改为键入'J'
让我们选择KV(f:'K*'V->('J*'U)选项)(e:系列)=
e |>Series.mapAll(有趣的k(一些v)->f(k,v))
|>Series.dropMissing
|>fun e1->e1.SelectKeys(fun kvp->fst kvp.Value.Value)
|>Series.mapValues snd
///使用函数中的键仅选择值
让我们选择(f:'K*'V->('K*'U)选项)(e:系列)=
e |>Series.mapAll(有趣的k(一些v)->f(k,v))
|>Series.dropMissing
|>Series.mapValues snd

谢谢,应该是:|>fun e1->e1.SelectKeys(fun kvp->fst kvp.Value.Value)。我想,如果我删除了上面指令中所有缺失的值,那么kvp.Value.Value将永远不会返回缺失值的异常。我说得对吗?啊,对,因为
kvp
KeyValuePair
。此外,如果您更改了密钥,您还可以允许更改其类型。除了f的类型之外,代码甚至不需要更改。