我对 Haskell 的求和类型只有一个理论概念。然而我感觉到它们在 Haskell 中真的很重要,并且从根本上改变了你对数据建模的方式。因为我相信它们在动态类型语言中也很有用,所以我尝试在 Javascript 中实现一个合理的近似(我只对 Haskell 有肤浅的了解)。
这是一个或多或少有用的Namesum 类型示例,它能够处理各种名称格式。我知道 Haskell 区分类型构造函数和数据构造函数,并且可能有充分的理由进行这种区分。但是,我想不可能将这个概念映射到 Javascript。和模式匹配都不匹配。
无论如何,我的实际问题是:以下实现是否体现了和类型的性质以及它们在 Haskell 中的应用方式?
请注意:我不确定 SO 上是否欢迎此类跨语言问题。如果我应该避免它们,请告诉我。
// auxiliary functions
const A = f => x => f(x);
const show = api => api.show;
// the type constructor
const Name = (...xs) => A(({length: len}) => {
switch (len) {
case 1: {
let [{length: len}] = xs; // no pattern matching but destructuring
if (len > 1) { // no Char type in JS …Run Code Online (Sandbox Code Playgroud)javascript haskell functional-programming pattern-matching gadt
所以scala 编译器抱怨模式匹配对方法来说可能并不详尽foo,我想知道为什么。这是代码:
abstract class Foo {
def foo(that: Foo): Unit = (this, that) match {
case (Foo_1(), Foo_1()) => //case 1
case (Foo_1(), Foo_2()) => //case 2
case (Foo_2(), Foo_1()) => //case 3
case (Foo_2(), Foo_2()) => //case 4
// Compiler warning
}
def fooThis(): Unit = this match {
case Foo_1() => //do something
case Foo_2() => //do something
// Works fine
}
def fooThat(that: Foo): Unit = that match {
case Foo_1() => //do something
case …Run Code Online (Sandbox Code Playgroud) 可能这个问题应该由 Kotlin 的作者提出,但我确信在 SO 上有许多 Kotlin 用户对其架构有深入的了解。
例如,我想要以下代码:
data class Person(val name: String, val age: Int)
when (person) {
("John", _) -> print("It is John") //it won't compile
else -> print("It's not John")
}
Run Code Online (Sandbox Code Playgroud)
由于解构使用的component1, component2, etc.方法,我很好奇为什么不能如上所示使用这种简单的值比较。是when机制修改的问题还是破坏本身的问题?
为什么 Haskell 不能在Num类型上执行模式匹配,而不需要我们指定Eq为类型类?
例如:
h :: Num a => a -> a
h 0 = -1
h x = x + 1
Run Code Online (Sandbox Code Playgroud)
编译这个函数时,ghci抱怨:
* Could not deduce (Eq a) arising from the literal `0'
from the context: Num a
bound by the type signature for:
h :: forall a. Num a => a -> a
at functions.hs:9:1-20
Possible fix:
add (Eq a) to the context of
the type signature for:
h :: forall a. …Run Code Online (Sandbox Code Playgroud) 我有一个枚举:
enum T {
A(String),
}
Run Code Online (Sandbox Code Playgroud)
我想匹配此枚举的变量,但此代码不起作用:
match t {
T::A("a") => println!("a"),
T::A("b") => println!("b"),
_ => println!("something else"),
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以做到这一点,但在我看来它太冗长了:
match t {
T::A(value) => match value.as_ref() {
"a" => println!("a"),
"b" => println!("b"),
_ => println!("something else"),
},
}
Run Code Online (Sandbox Code Playgroud)
有没有更短的方法来做到这一点?
嗨,我有 2 列的数据框:
+----------------------------------------+----------+
| Text | Key_word |
+----------------------------------------+----------+
| First random text tree cheese cat | tree |
| Second random text apple pie three | text |
| Third random text burger food brain | brain |
| Fourth random text nothing thing chips | random |
+----------------------------------------+----------+
Run Code Online (Sandbox Code Playgroud)
我想生成第 3 列,其中一个单词出现在文本中的 key_word 之前。
+----------------------------------------+----------+-------------------+--+
| Text | Key_word | word_bef_key_word | |
+----------------------------------------+----------+-------------------+--+
| First random text tree cheese cat | tree | text | |
| …Run Code Online (Sandbox Code Playgroud) 我的日志中有以下几行要排除,因此我想使用 aRegexFilter来执行此操作:
[信息] 2018-05-20 14:52:15.993 [qtp22844606-20] TimingFilter - 请求时间:16 毫秒
[信息] 2018-05-20 14:52:18.998 [qtp22844606-17] TimingFilter - 请求时间:15 毫秒
[信息] 2018-05-20 14:52:22.001 [qtp22844606-20] TimingFilter - 请求时间:0 毫秒
[信息] 2018-05-20 14:52:24.992 [qtp22844606-17] TimingFilter - 请求时间:0 毫秒
我的配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<RegexFilter regex=".*TimingFilter.*" useRawMsg="true" onMatch="DENY" onMismatch="ACCEPT"/>
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
Run Code Online (Sandbox Code Playgroud)
当切换“接受”和“拒绝”时,没有消息通过,因此 RegexFilter 本身似乎被拾取。我也试过 "\bTimingFilter\b", ".*\bTimingFilter\b.*" 没有成功。
我正在使用 Log4J …
我匹配多个单词的查询如下,
{"query":
{"bool":{"must":[{"terms":{"my_field":"word1 word2"}}]}
Run Code Online (Sandbox Code Playgroud)
执行时,尽管存在以下查询的数据,但结果集为空。而不是上面的查询,如果我使用
{"bool":{"must":[{"terms":{"my_field":"word1"}}]}
然后弹性搜索正在返回数据。如何匹配完整的句子?
它有一种优雅的方式来在 scalaIterable[A]集合上进行模式匹配,检查它是否为空,它是否只包含一个元素(并获取它),它是否恰好包含 N 个元素(并获取它们),如果它包含至少一个或更多元素(并得到它或它们),等等。
随着List它的琐碎,但我没能有同等Iterable的工作。
我正在尝试在 Haskell 中创建自己的自定义数据类型。
我有以下数据类型:
type Length = Integer
type Rotation = Integer
data Colour = Colour { red, green, blue, alpha :: Int }
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个自定义数据类型,它可以是上述数据类型之一。我有以下几点:
data Special
= L Length
| R Rotation
| Col Colour
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
但是,如果我有数据类型的实例,我希望能够提取Length,Rotation和Colour值Special。
如果我有:
L length
Run Code Online (Sandbox Code Playgroud)
请问length这里是类型Special或类型的Length?如果length是类型Special有没有办法提取它所以它是类型Length?
例如,下面的代码有效吗?
takeL (x:xs)
| x == (L length) = length
Run Code Online (Sandbox Code Playgroud)
任何见解表示赞赏。
haskell functional-programming pattern-matching algebraic-data-types custom-data-type