标签: pattern-matching

在 PySpark 中将 StringType 列转换为 ArrayType

我有一个包含“EVENT_ID”列的数据框,其数据类型为字符串。我正在运行 FPGrowth 算法,但抛出以下错误

Py4JJavaError: An error occurred while calling o1711.fit. 
:java.lang.IllegalArgumentException: requirement failed: 
The input column must be array, but got string.
Run Code Online (Sandbox Code Playgroud)

列 EVENT_ID 有值

E_34503_Probe
E_35203_In
E_31901_Cbc
Run Code Online (Sandbox Code Playgroud)

我正在使用下面的代码将字符串列转换为数组类型

df2 = df.withColumn("EVENT_ID", df["EVENT_ID"].cast(types.ArrayType(types.StringType())))
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误

Py4JJavaError: An error occurred while calling o1874.withColumn.
: org.apache.spark.sql.AnalysisException: cannot resolve '`EVENT_ID`' due to data type mismatch: cannot cast string to array<string>;;
Run Code Online (Sandbox Code Playgroud)

如何将此列转换为数组类型或使用字符串类型运行 FPGrowth 算法?

python pattern-matching python-3.x pyspark fpgrowth

1
推荐指数
1
解决办法
7686
查看次数

GADT 上的模式匹配失败

我在 ReasonML 上玩了更多,发现type t以下示例中的模式匹配无法处理错误

错误:此模式匹配 t(float) 类型的值,但预期模式匹配 t(int) 类型的值 类型 float 与类型 int 不兼容

  type t('a) =
    | One: t(int)
    | Two: t(float);

  let x =
    fun
    | One => None
    | Two => None;
Run Code Online (Sandbox Code Playgroud)

现在在某种程度上,如果这是关于函数的返回类型,这对我来说是有意义的。

我找到了一个等价问题的答案(我认为)。对于第二部分,答案似乎是忽略构造函数的绑定类型。在 ReasonML 中是否可能相同?

Ps:请在术语上迂腐地纠正我,我还在学习什么是什么。

Pps:我知道我可以通过显式键入来解决原始问题,x但我真的很喜欢fun它的语法,因为它很有趣。

ocaml pattern-matching gadt reason

1
推荐指数
1
解决办法
282
查看次数

Elixir 功能在什么时候会成为误导?

一般来说,函数式编程以更清晰和简洁而自豪。您没有副作用/状态管理的事实使开发人员更容易推理他们的代码并确保行为。这个真理能达到多远?

我仍在学习 Elixir,但给出了 Coding Gnome 的代码:

def make_move(game = %{ game_state: state }, _guess)
  when state in [:won, :lost] do
    ...
end

def make_move(game = %{ game_state: state }, _guess)
  when state in [:pending] do
    ...
end

def make_move(game, guess) do
  ...
end
Run Code Online (Sandbox Code Playgroud)

人们可以在 Javascript 中毫不费力地将其编写为:

def make_move(game = %{ game_state: state }, _guess)
  when state in [:won, :lost] do
    ...
end

def make_move(game = %{ game_state: state }, _guess)
  when state in [:pending] do
    ...
end

def make_move(game, guess) …
Run Code Online (Sandbox Code Playgroud)

functional-programming elixir pattern-matching

1
推荐指数
1
解决办法
77
查看次数

如何通过文字进行模式匹配并同时为其分配变量?

我如何将这两者结合起来,以便能够进行模式匹配,同时在变量中具有匹配的文字?

fun1 :: Int -> String

fun1 1 = -- ..... how to bind 1 to a variable in the function declaration?
fun1 55 = -- ..... how to bind 55 to a variable in the function declaration?
fun1 123 = -- ..... how to bind 123 to a variable in the function declaration?

fun1 a = -- ...........   all is OK
Run Code Online (Sandbox Code Playgroud)

variables binding haskell pattern-matching variable-assignment

1
推荐指数
1
解决办法
70
查看次数

如果 A 的泛型子类型声明为返回参数,为什么我不能返回 A 的具体子类型?

abstract class IntTree
object Empty extends IntTree
case class NonEmpty(elem: Int, left: IntTree, right: IntTree) extends IntTree

def assertNonNegative[S <: IntTree](t: S): S = {
  t match {
    case Empty => Empty  // type mismatch, required: S, found: Empty.type
    case NonEmpty(elem, left, right) =>
      if (elem < 0) throw new Exception
      else NonEmpty(elem, assertNonNegatve(left), assertNonNegative(right)) // req: S, fd: NonEmpty.type
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试使用签名实现该功能的失败尝试def assertNonNegative[S <: IntTree](t: S): S。除了将签名更改为 之外def assertNonNegative(t: IntTree): IntTree,我找不到实现它的方法。

示例的相关性:
在“Scala 中的函数式编程原则”课程中关于子类型和泛型 (4.4) …

generics tree scala pattern-matching type-bounds

1
推荐指数
1
解决办法
170
查看次数

如何在 Haskell 中简化此功能?

我认为以这种方式编写代码是多余的。不管类型构造器是什么,返回值都是一样的。有没有办法一次性写入返回值?

data End = Leftend (Int,Int) | Rightend (Int, Int)
            deriving (Eq, Ord, Show)


cmp:: End->End->Ordering
cmp (Leftend (l, h1))  (Rightend (r,h2))
        | l < r = LT
        | l == r = EQ
        | l > r = GT
cmp (Leftend (l, h1))  (Leftend (r,h2))
        | l < r = LT
        | l == r = EQ
        | l > r = GT
cmp (Rightend (l, h1))  (Rightend (r,h2))
        | l < r = LT
        | l == r …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming pattern-matching

1
推荐指数
1
解决办法
81
查看次数

模式匹配实例

我在https://www.baeldung.com/java-pattern-matching-instanceof上遇到了这个惊人的话题。但是当我尝试运行以下代码时,它会引发编译时错误:

if(obj instanceof String s) {
    System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)

错误说:

语言级别“14”不支持“instanceof”中的模式

Error:(36, 34) java: instanceof 中的模式匹配是一个预览功能,默认情况下是禁用的。(使用 --enable-preview 在 instanceof 中启用模式匹配)

但是我安装了 Java 14。

java pattern-matching preview-feature java-14

1
推荐指数
1
解决办法
560
查看次数

减少深度嵌套属性的匹配缩进

我需要引用结构深处的值,该结构包含Option嵌套在struct属性中的嵌套在Result.

我目前的(工作)解决方案是:

    let raw = &packet[16..];
    match PacketHeaders::from_ip_slice(raw) {
        Err(_value) => {
            /* ignore */
        },
        Ok(value) => {
            match value.ip {
                Some(Version4(header)) => {
                    let key = format!("{}.{}.{}.{},{}.{}.{}.{}", 
                        header.source[0], header.source[1], header.source[2], header.source[3],
                        header.destination[0], header.destination[1], header.destination[2], header.destination[3],
                    );

                    let Count {packets, bytes} = counts.entry(key).or_insert(Count {packets: 0, bytes: 0});
                    *packets += 1;
                    *bytes += packet.len();

                    if p > 1000 { /* exit after 1000 packets */
                        for (key, value) in counts {
                            println!("{},{},{}", key, value.packets, …
Run Code Online (Sandbox Code Playgroud)

pattern-matching algebraic-data-types rust

1
推荐指数
1
解决办法
519
查看次数

f# 模式匹配序列 :? seq&lt;_&gt; (IEnumerable)

我想将结果发送到将内容输出到控制台/日志的单个方法

我曾希望检测结果是否包含 IEnumerable 并遍历该集合以获取结果。

这无法识别 seq 并简单地将其标识为Other Object.

对不起,冗长。

let rec LogResultGeneric (logInfo: string -> unit, logError: string -> unit) (result: Result<_, _>) =
    let innerSelect (item: _) =
        match item |> box with
        | :? Result<_, _> as res ->
            "RESULT" |> logInfo
            res |> LogResultGeneric(logInfo, logError)
        | _ ->
            "VALUE" |> logInfo
            item |> LogValueGeneric logInfo

    "DISPLAY OUTCOME : " + result.ToString() |> logInfo 
    match result with
    | Error msg ->
        "ERROR RESULT" |> logError
        match …
Run Code Online (Sandbox Code Playgroud)

generics f# types pattern-matching

1
推荐指数
1
解决办法
118
查看次数

F# 忽略模式匹配中的模式

我可能会以错误的方式思考这个问题,但我想忽略除Some案例之外的任何案例。这是我正在使用的一些示例代码,| _ -> ignore但这似乎是错误的。有没有更好或更惯用的方法来做到这一点?我正在将一些 OOP C# 代码转换为 F#,可能会出错。

match solarSystem.MinerCoords |> Map.tryFind minerId with
| Some currentMinerCoords ->
    match solarSystem.Minables |> Map.tryFind currentMinerCoords with
    | Some _ ->
        do! GetMinerActor(minerId).StopMining() |> Async.AwaitTask
    | _ -> ignore
| _ -> ignore
Run Code Online (Sandbox Code Playgroud)

f# functional-programming pattern-matching

1
推荐指数
1
解决办法
150
查看次数