问题标签 [scala-option]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
2669 浏览

scala - Scala 习惯用法首先从迭代器中找到 Some of Option

我有一个选项迭代器,并想找到第一个成员:

  1. 一些
  2. 并且满足一个谓词

最好的惯用方法是什么?

另外:如果在此过程中抛出异常,我想忽略它并继续下一个成员

0 投票
1 回答
1302 浏览

scala - Scala 的 Option fold 以何种方式变态?

这个问题的答案表明,Scala 中 Option 上的 fold 方法是一种变态。来自维基百科的catamophism是“从初始代数到其他代数的独特同态。这个概念已被应用于函数式编程作为折叠”。所以这看起来很公平,但让我把初始代数作为F-algebras类别中的初始对象。

因此,如果 Option 上的折叠真的是一个变态,则需要一些函子 F,以创建 F 代数的类别,其中 Option 将是初始对象。我不知道这个函子会是什么。

对于 Lists 类型A的函子FF[X] = 1 + A * X. 这是有道理的,因为 List 是一种递归数据类型,所以如果X是,List[A]那么上面的内容读取类型列表A要么是空列表 ( 1),要么是 ( ) an和 a+的对 ( ) 。但 Option 不是递归的。将只是(无或)。所以我看不到函子在哪里。*AList[A]Option[A]1 + AA

为了清楚起见,我意识到 Option 已经是一个仿函数,因为它需要Ato Option[A],但是对列表所做的事情是不同的,它A是固定的,并且仿函数用于描述如何递归地构造数据类型。

在相关的说明中,如果它不是变质,它可能不应该被称为折叠,因为这会导致一些混乱

0 投票
5 回答
1213 浏览

scala - How do you stop building an Option[Collection] upon reaching the first None?

When building up a collection inside an Option, each attempt to make the next member of the collection might fail, making the collection as a whole a failure, too. Upon the first failure to make a member, I'd like to give up immediately and return None for the whole collection. What is an idiomatic way to do this in Scala?

Here's one approach I've come up with:

In other words, if any call to findPartByName returns None, allParts returns None. Otherwise, allParts returns a Some containing a collection of Parts, all of which are guaranteed to be valid. An empty collection is OK.

The above has the advantage that it stops calling findPartByName after the first failure. But the foldLeft still iterates once for each name, regardless.

Here's a version that bails out as soon as findPartByName returns a None:

I currently find the second version more readable, but (a) what seems most readable is likely to change as I get more experience with Scala, (b) I get the impression that early return is frowned upon in Scala, and (c) neither one seems to make what's going on especially obvious to me.

The combination of "all-or-nothing" and "give up on the first failure" seems like such a basic programming concept, I figure there must be a common Scala or functional idiom to express it.

0 投票
1 回答
413 浏览

scala - Scalaz .option - 这个简写是什么?

我看到以下代码:

这是否等同于:

我问是因为我很难在 Scalaz 上为这种.option方法找到 doco。

0 投票
1 回答
387 浏览

scala - 为理解创建演员时出现 ClassCastException

Child1在此示例中,我有一个创建子类型演员的演员。Child1构造函数采用两个StringSomeTrait ,它们是从混合到实例中的变量中提取的SomeActor

SomeActor实例上创建:

有了这个我得到奇怪的错误:

SomeActor cannot be cast to SomeTrait.

似乎从Option 容器中提取变量SomeTrait会引发该异常。

我在这里想念什么?

它不仅发生在for comprehensions 上。此外,当我尝试执行str1.getOrElse("")或添加吸气剂时SomeTraitdef getStr1 = str1.getOrElse("")

0 投票
1 回答
718 浏览

scala - 如何为 Option 实现 flatMap

我正在尝试实现mapandflatMap作为 的扩展/丰富Option,而不作弊并查看它是如何在 Scalaz 中实现的。

所以这是我在卡住之前得到的:

我真正需要的是基本功能,所以我可以做这样的事情:

有什么提示吗?

0 投票
1 回答
121 浏览

scala - DRY use of multiple fields of an Option's value

Background

I'm using a java web UI library (Vaadin, but that's irrelevant - I'm not using the Scaladin add-on) which has a TextField that has a method setValue(String) to set the current text value.

As it's a java library, it has no notion of Option but can represent a null value as e.g. an empty string (it's configurable). So I've written an implicit wrapper class as follows:

This allows me to set the value using the := operator directly on a TextField instance and allows me to deal with the Option to null conversion in a single place.

Problem

When I have e.g. a form with a bunch of text fields showing the values for some object of type X, then if I have an Option[X] I'm doing this:

In other words, I want to call the := with Some(string) in case I have maybeX is Some but with None if maybeX is none. In addition, suppose x.valueD is actually an Option[String], I would call

(I could simply always call flatten, but it wouldn't have an effect if there are no nested options)

Is there a nicer way to write this, getting rid of all the duplicate for comprehensions and flattening the result? Maybe using a macro?

I'd prefer not to use reflection (I'm sure it will be possible to put all the fields in a sequence, zip it with a sequence containing the property names, then map this using a function that returns None or does the relflection lookup, and also flattens the result).

0 投票
9 回答
2786 浏览

scala - 用于从两个选项中获取单个选项并在两个可用时抛出异常的惯用 scala

我简要研究了 Either 类型,但它似乎是用于“表示两种可能类型之一的值”。我是否缺少一些数据结构或 Monad?本质上,我想要一个明确的(如果两者都有价值,则抛出错误)如果可用,则获取其中一个或获取 None

0 投票
2 回答
154 浏览

scala - Scala 方法参数:集合或默认值的选项

我有将 [Int, MyClass] 的 Map 作为参数的方法。像这样的东西:

myMethod(someMap : Map[Int, MyClass])

但是, someMap 可能并不总是存在(Java 世界中的 null 和 Scala 中的 None)。从 API 的角度来看,以下哪项是该方法的更好设计:

  1. 将其包装在选项中: myMethod(someMap : Option[Map[Int, MyClass]] = None)

  2. 定义一个默认的空地图: myMethod(someMap : Map[Int, MyClass] = Maps.empty)

第一个选项看起来很优雅,但是它具有额外的复杂性,即必须在 Some() 中包装 Map(如果不是 None)并且在实现者中必须执行 getOrElse 来解开它。第一个选项还让 api 的使用者清楚地知道,地图对象可能实际上并不存在(无)

在第二个选项中,不必进行包装(在 Some 中)或展开,但每次没有现有 Map 对象时都必须实例化一个空容器。

此外,反对 1 的论点:Option 本身是 0 或 1 项的容器,而 Map 也是一个容器(集合)。将一个容器包裹在另一个容器中是一个好的设计吗?

从 API 设计的角度来看,哪种方法更好?

0 投票
2 回答
280 浏览

scala - Option.fold - 为什么它的第二个参数不是二元运算符?

我确信这是有充分理由的,但我没有看到。

Fold在(说)List回报

op在所有元素之间应用折叠运算符的结果和z

foldLeft它与做同样的事情但具有明确的顺序有明显的关系foldRight(因此不需要关联运算符)

FoldOption退货时

如果非空,则返回应用f到 thisscala.Option的值的结果。scala.Option否则,计算表达式ifEmpty

ifEmpty是(在)z列表的位置。 f是(在的位置)op

对于None(使用我的心智模型Option作为可能包含或不包含值的“容器”,是一个“空”容器),一切正常,Option.fold返回零(的值ifEmpty)。

Some(x)不过,for不应该f采用两个参数,z因此x它与 on 序列一致(包括与andfold具有相似的关系)。foldLeftfoldRight

肯定有一个反对这一点的实用论点 - 在实践中f只是x作为参数可能更方便。在大多数情况下,如果它确实也采取z了,那将被忽略。但一致性也很重要......

那么有人可以向我解释为什么foldOption 仍然是“正确的”fold吗?