我有一堆目标,每个目标都用于不同的外壳,如下所示:
BASH_VERSION := 4.4
FISH_VERSION := 3.0.0
…
ZSH_VERSION := 5.4.2-r1
.PHONY: dev-pure-on-bash
dev-pure-on-bash:
$(MAKE) dev-pure-on TARGET=bash VERSION="${BASH_VERSION}" ARGS="VERSION=${BASH_VERSION}"
.PHONY: dev-pure-on-elvish
dev-pure-on-elvish:
$(MAKE) dev-pure-on TARGET=elvish VERSION="${ELVISH_VERSION}" ARGS="VERSION=${ELVISH_VERSION}"
.PHONY: dev-pure-on-fish
dev-pure-on-fish:
$(MAKE) dev-pure-on TARGET=fish VERSION="${FISH_VERSION}" ARGS="VERSION=${FISH_VERSION}"
…
.PHONY: dev-pure-on-xonsh
dev-pure-on-xonsh:
$(MAKE) dev-pure-on TARGET=xonsh VERSION="${XONSH_VERSION}" ARGS="VERSION=${XONSH_VERSION}"
.PHONY: dev-pure-on-zsh
dev-pure-on-zsh:
$(MAKE) dev-pure-on TARGET=zsh VERSION="${ZSH_VERSION}" ARGS="VERSION=${ZSH_VERSION}"
.PHONY: dev-pure-on
dev-pure-on:
docker run \
--name run-pure-on-${TARGET} \
--rm \
--interactive \
--tty \
--volume=$$PWD:/home/pure/.pure/ \
pure-on-${TARGET}-${VERSION}
Run Code Online (Sandbox Code Playgroud)
我尝试使用模式规则但没有成功:
dev-pure-on-%:
$(MAKE) dev-pure-on TARGET=$* VERSION="${$*_VERSION}" ARGS="VERSION=${$*_VERSION}" …Run Code Online (Sandbox Code Playgroud) 我偶然发现了 Rust 编译器的一个非常奇怪的行为:
fn main() {
match bool_result(false) {
Err(_) => println!("Got error"),
Ok(value) if value => println!("Got value TRUE"),
Ok(value) if !value => println!("Got value FALSE"),
// Ok(z) => println!("WTF: {}", z), // uncomment to compile
}
}
fn bool_result(x: bool) -> Result<bool, ()> {
Ok(x)
}
Run Code Online (Sandbox Code Playgroud)
除非我取消对最后一个匹配臂的注释,否则上面的代码段不会编译出现以下错误。但是布尔值只有 TRUE/FALSE,那么为什么 rustc 认为匹配并不详尽呢?
fn main() {
match bool_result(false) {
Err(_) => println!("Got error"),
Ok(value) if value => println!("Got value TRUE"),
Ok(value) if !value => println!("Got value FALSE"),
// Ok(z) => println!("WTF: …Run Code Online (Sandbox Code Playgroud) 我必须根据 Seq 的大小做出决定。
所以,要么我可以这样做:
if(mySeq.size() > 0) // do your stuff
Run Code Online (Sandbox Code Playgroud)
或者,我可以这样做:
mySeq match {
case x :: _ => // do your stuff
}
Run Code Online (Sandbox Code Playgroud)
我应该更喜欢哪一个?
我想修改段落中两行之间存在的一行。例如我有这个文件包含
fruits
apple
banana
end fruits
----
all list
egg
milk
banana
end list
Run Code Online (Sandbox Code Playgroud)
我想将水果块内(水果和最终水果之间)的香蕉改为浆果。怎么做?
有没有办法抽象数据类型并只使用值?
data Color a = Blue a | Green a | Red a | Yellow a | Magenta a deriving (Show)
就像是 :
calcColor :: Color a -> Color a -> Maybe a
calcColor (_ x) (_ y) = Just $ x + y
Run Code Online (Sandbox Code Playgroud)
它不一定在函数声明中。我正在考虑的一个选择是拥有类似的东西,fromJust但即使这样也感觉有点多余。
fromColor :: Color a -> a
fromColor (Blue t) = t
fromColor (Red t) = t
fromColor (Green t) = t
fromColor (Yellow t) = t
Run Code Online (Sandbox Code Playgroud)
从我设法质疑它的方式来看,标题可能看起来像一个重复的问题。
我不确定,但这是由社区决定的。我对 Haskell 很陌生,所以也许我的问题看起来很愚蠢,但我仍然认为这是一个有效的案例,因为我实际上遇到了这种情况。 …
在同一情况下使用 None 和 Some() 的最优雅的情况是什么?就像是:
val data: Option[Int] = getSomeData()
data match {
case None || Some(data) && data > 50 =>
case _ =>
}
Run Code Online (Sandbox Code Playgroud) 给定一些扩展共同特征的案例类,我希望能够根据它们的类型定义一个排序(实际上不一定,目标是以这种方式对它们进行排序)。例如:
sealed trait Element
case class A(x: Int) extends Element
case class B(x: Int) extends Element
case class C(x: Int) extends Element
case class D(x: Int) extends Element
case class E(x: Int) extends Element
case class F(x: Int) extends Element
val elements: List[Element] = List(
A(5), F(3), E(1), C(19), A(3), F(1)
)
Run Code Online (Sandbox Code Playgroud)
排序为F -> A -> all other cases,因此结果列表为List(F(3), F(1), A(5), A(3), E(1), C(19))。相同类型元素之间的顺序无关紧要。
我想出了多种不同的解决方案,但它们看起来都很复杂,我只是觉得我错过了一些明显的方法来实现这一点。这就是我使用排序实现它的方式:
val sorted = elements.sorted{(a: Element, b: Element) => (a, b) match …Run Code Online (Sandbox Code Playgroud) sorting functional-programming scala pattern-matching case-class
假设我有这张地图: Map[String, Int]
为了迭代它的值,我必须这样做:
myMap.foreach(t => {
val word = t._1
val number = t._2
//do stuff with word and number here
})
Run Code Online (Sandbox Code Playgroud)
有没有办法做这样的事情:
myMap.foreach( (word, number) => {
//do stuff with word and number here
})
Run Code Online (Sandbox Code Playgroud)
使用 Scala 2.13.2 atm
dictionary functional-programming scala pattern-matching scala-collections
我试图在单个case子句中匹配所有二元运算符,但以下代码给出了错误:
object BinOp is not a case class, nor does it have a valid unapply/unapplySeq member
Note: def unapply(a: AST.this.Expr, b: AST.this.Expr): Option[(AST.this.Expr, AST.this.Expr)] exists in object BinOp, but it cannot be used as an extractor as it has more than one (non-implicit) parameter.
Run Code Online (Sandbox Code Playgroud)
遍历树的核心代码:
tree match {
case ... other ... cases
case BinOp(a, b) => traverse(a), traverse(b)
}
Run Code Online (Sandbox Code Playgroud)
AST 类如下:
sealed trait Expr
case class Num(value: java.lang.Number) extends Expr
sealed trait BinOp extends Expr {
val …Run Code Online (Sandbox Code Playgroud) 我的 Scala 项目具有以下特征
sealed trait Value
case class Float(f: Double) extends Value
case class Bool(b: Boolean) extends Value
case object Error extends Value
Run Code Online (Sandbox Code Playgroud)
后来我有了价值lst: List[Value]。
我正在尝试获取列表中第一个元素的值,lst.head.f但出现以下错误Cannot resolve symbol f。
从我从类似主题的其他堆栈溢出问题中收集的信息来看,它应该可以工作,但事实并非如此。
pattern-matching ×10
scala ×6
case-class ×2
awk ×1
bash ×1
case ×1
class ×1
dictionary ×1
haskell ×1
if-statement ×1
makefile ×1
rust ×1
sed ×1
sorting ×1
traits ×1
types ×1
uppercase ×1