在Scala中解压选项元组

时间:2018-09-28 08:41:24

标签: scala tuples

我想对两个Option [BigDecimal] val进行操作。我正试图通过地图来做到这一点。

(a,b) map {case(a1: BigDecimal, b1: BigDecimal) => a + b
       case _ => 0}

但是显然我不能在可选的Tuple上使用地图。有谁知道如何使用地图实现这一目标?

6 个答案:

答案 0 :(得分:3)

以下是具有Seq的解决方案:

Seq(a,b).flatten.sum

答案 1 :(得分:0)

但是您可以使用match

  (v1, v2) match {
    case (Some(a), Some(b)) => a + b
    case _ => 0
  }

如果同时存在v1v2,则返回sum,否则返回0

更新: 如果您想要所有现有元素的总和,可以执行以下操作:

val result = Seq(value._1, value._2)
  .flatten
  .fold[Integer](0)((a, b) => a + b)

答案 2 :(得分:0)

您可以轻松做到:

(a, b) match {
  case (Some(sa), Some(sb)) => sa + sb
  case _ => 0
}

您也可以:

(a, b) match {
  case (Some(sa), Some(sb)) => sa + sb
  case (Some(sa), None) => sa
  case (None, Some(sb)) => sb
  case _ => 0
}

如果您要考虑其中只有一个等于None的情况

答案 3 :(得分:0)

如果您想默认为0,请使用以下命令:

a.getOrElse(0) + b.getOrElse(0)

如果要检查两个值均有效,请执行以下操作:

a.map(av => b.map(av + _))

或这个

for {
  av <- a
  bv <- b
} yield av + bv

如果Noneab,则两者都将给出None,否则将给出Some(a+b)。如果您希望在这种情况下将结果默认设置为getOrElse(0),请在结果上使用0

答案 4 :(得分:0)

根据需要使用Seqmap

Seq(a,b).map(_.getOrElse(BigDecimal(0))).sum

(其中ab属于Option[BigDecimal]类型)

答案 5 :(得分:-1)

我认为这是您想要做的。

public List<ANote> findPath(ANote start, ANote end)
        {
            if (start == null || end == null || start == end || !start.walkable || !end.walkable)
                return null;


        List<ANote> openSet = new List<ANote>();
        List<ANote> closedSet = new List<ANote>();

        start.parent = null;
        openSet.Add(start);
        start.h = getDistance(start, end);

        while (openSet.Any())
        {
            openSet = openSet.OrderBy(o => o.f).ToList();
            ANote current = openSet[0];

            if (current == end)
                break;

            openSet.Remove(current);
            closedSet.Add(current);

            foreach (ANote neighbor in current.adjacted)
            {
                if (closedSet.Contains(neighbor))
                    continue;


                double _gScore = current.g + 1; // For me every distance was 1

                if (openSet.Contains(neighbor) && _gScore >= neighbor.g)
                    continue;

                if (!openSet.Contains(neighbor))
                    openSet.Add(neighbor);


                neighbor.parent = current;
                neighbor.g = _gScore;
                neighbor.h = getDistance(neighbor, end);
            }
        }

        return reconstructPath(start, end);
    }

    private List<ANote> reconstructPath(ANote start, ANote end)
    {
        List<ANote> backNotes = new List<ANote>();
        ANote current = end;
        while (current.parent != null)
        {
            backNotes.Add(current);
            current = current.parent;
        }


        return backNotes;
    }

public class ANote
    {
        public ANote parent { get; set; }
        public double f { get { return g + h; } }
        public double g { get; set; }
        public double h { get; set; }
        public int x { get; set; }
        public int y { get; set; }
        public bool walkable { get; set; }

        public List<ANote> adjacted { get; set; } = new List<ANote>();

        public ANote(int x, int y)
        {
            this.x = x;
            this.y = y;
            walkable = true;
        }
    }

您也可以这样:

val a = Some(1)
val b = Some(2)

  val ans = (a, b) match {
    case (Some(a1), Some(b1)) => a1 + b1

    case _ => 0
  }
println(ans) // 3