GVKun编程网logo

Swift Combine 按顺序制作串行序列(swift选择排序)

3

在本文中,我们将为您详细介绍SwiftCombine按顺序制作串行序列的相关知识,并且为您解答关于swift选择排序的疑问,此外,我们还会提供一些关于CombineTwoTables--leetcod

在本文中,我们将为您详细介绍Swift Combine 按顺序制作串行序列的相关知识,并且为您解答关于swift选择排序的疑问,此外,我们还会提供一些关于Combine Two Tables --leetcode、Combine Two Tables[leetcode]、Falling Max Swift Combine 出版商、hadoop中的shuffle过程(combine->partition)的有用信息。

本文目录一览:

Swift Combine 按顺序制作串行序列(swift选择排序)

Swift Combine 按顺序制作串行序列(swift选择排序)

如何解决Swift Combine 按顺序制作串行序列

这是一个简单的操场代码,以相反的顺序显示了两个订阅。如何修改代码以使其始终打印出 555?目前它随机打印 0 或 555,因为操作是异步的。请记住,这是一个概念,而不是要解决的实际问题,因此将所有内容放在一个水槽中不是解决方案。谢谢

import Combine

class Foo {

    let subject = PassthroughSubject<Void,Never>()
    var myProperty = 0

    var bag = Set<AnyCancellable>()

    init() {
        subject
            .sink { _ in print(self.myProperty) }
            .store(in: &bag)

        subject
            .map { _ in 555 }
            .assign(to: \\.myProperty,on: self)
            .store(in: &bag)

        subject.send()
    }
}

let testClass = Foo()

解决方法

PassthroughSubject 不保证将值发送给订阅者的顺序。您不能强制执行订单,而且该订单可能会在未来版本的 Combine 中更改。

一种将 myProperty 存储在其自己的主题中并订阅该主题以获取 +555 值的解决方案:

import Combine

class Foo {
    
    let subject = PassthroughSubject<Void,Never>()
    private var _myProperty = CurrentValueSubject<Int,Never>(0)
    var myProperty: Int { _myProperty.value }
    
    var bag = Set<AnyCancellable>()
    
    init() {
        _myProperty
            .sink { _ in print(self.myProperty) }
            .store(in: &bag)
        
        subject
            .map { _ in 555 }
            .assign(to: \\.value,on: _myProperty)
            .store(in: &bag)
        
        subject.send()
    }
}

let testClass = Foo()

Combine Two Tables --leetcode

Combine Two Tables --leetcode

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

FirstName, LastName, City, State


solution:

普通的LEFT JOIN 关联
SELECT p.FirstName, p.LastName, a.City, a.State FROM Person p LEFT JOIN Address  a ON p.PersonId = a.PersonId;

们也可以使用关键Using来声明我们相用哪个列名来进行联合
SELECT p.FirstName, p.LastName, a.City, a.State FROM Person p LEFT JOIN Address a  using(PersonId);

自动搜索相同的列 nature
SELECT p.FirstName, p.LastName, a.City, a.State FROM Person p NATURAL LEFT JOIN Address  a

git 地址:https://github.com/woshiyexinjie/leetcode-xin

Combine Two Tables[leetcode]

Combine Two Tables[leetcode]

# Write your MySQL query statement below
#select a.FirstName , a.LastName , b.City , b.State from Person as a , Address as b where a.PersonId = b.AddressId ;
select a.FirstName , a.LastName , b.City , b.State from Person as a left join Address as b on a.PersonId = b.PersonId ;


https://leetcode.com/discuss/33259/difference-between-inner-join-and-left-join

Falling Max Swift Combine 出版商

Falling Max Swift Combine 出版商

如何解决Falling Max Swift Combine 出版商

我正在为 Swift/Combine 开发一个发布商

给定输入流,我想记录最大值。 如果下一个数字较小,则从最后记录的最大值中取出一个并发出。

  1. Input: [1,2,3,4,5,1]
  2. Output: [1,2]

我可以使用以下代码轻松完成此操作,但是,我真的不喜欢实例变量

  1. var lastMaxInstanceValue: Float = 0
  2. publisher
  3. .map { newValue
  4. if newValue > lastMaxInstanceValue {
  5. lastMaxInstanceValue = newValue
  6. } else {
  7. lastMaxInstanceValue = max(0,lastMaxInstanceValue - 1)
  8. }
  9. }
  10. .assign(to: \\.percentage,on: self)
  11. .store(in: &cancellables)

所以我在这里写了一个发布者/订阅者,它封装了上面的 map 部分:

https://github.com/nthState/FallingMaxPublisher

对于我的发布者,代码变成:

  1. publisher
  2. .fallingMax()
  3. .assign(to: \\.percentage,on: self)
  4. .store(in: &cancellables)

我的问题是,是否需要我的 GitHub 发布者?可以在没有额外变量的情况下计算我想要的值吗?

解决方法

您可以使用 scan 运算符来实现此目的。 Scan 存储从每个上游值和当前累积值计算的累积值。但是,您需要给它一个初始值;根据您的示例,我使用了 0:

  1. publisher
  2. .scan(0,{ max,value in
  3. value >= max ? value : max - 1
  4. })

您可以为 fallingMax 实现 SignedInteger 运算符 - 就像在您的 github 中一样 - 像这样:

  1. extension Publisher where Output: SignedInteger {
  2. func fallingMax(initial: Output = 0,fadeDownAmount: Output = 1
  3. ) -> AnyPublisher<Output,Failure> {
  4. self.scan(initial,value in
  5. value >= max ? value : max - fadeDownAmount
  6. })
  7. .eraseToAnyPublisher()
  8. }
  9. }

根据@Rob 的建议,如果您不想提供初始值,而是使用第一个值作为初始输出,您可以使用 Optional(注意 .compactMap将其恢复为非可选):

  1. extension Publisher where Output: SignedInteger {
  2. func fallingMax(initial: Output? = .none,value in
  3. max.map { value >= $0 ? value : $0 - fadeDownAmount } ?? value
  4. })
  5. .compactMap { $0 }
  6. .eraseToAnyPublisher()
  7. }
  8. }

hadoop中的shuffle过程(combine->partition)

hadoop中的shuffle过程(combine->partition)

combine和partition都是函数,中间的步骤应该只有shuffle!

combine分为map端和reduce端,作用是把同一个key的键值对合并在一起,可以自定义的。
combine函数把一个map函数产生的<key,value>对(多个key,value)合并成一个新的<key2,value2>.将新的<key2,value2>作为输入到reduce函数中
这个value2亦可称之为values,因为有多个。这个合并的目的是为了减少网络传输。

partition是分割map每个节点的结果,按照key分别映射给不同的reduce,也是可以自定义的。这里其实可以理解归类。
我们对于错综复杂的数据归类。比如在动物园里有牛羊鸡鸭鹅,他们都是混在一起的,但是到了晚上他们就各自牛回牛棚,羊回羊圈,鸡回鸡窝。partition的作用就是把这些数据归类。只不过在写程序的时候,mapreduce使用 哈希 HashPartitioner 帮我们归类了。这个我们也可以自定义。

shuffle就是map和reduce之间的过程,包含了两端的combine和partition。


Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出
shuffle阶段的主要函数是fetchOutputs(),这个函数的功能就是将map阶段的输出,copy到reduce 节点本地。

今天关于Swift Combine 按顺序制作串行序列swift选择排序的介绍到此结束,谢谢您的阅读,有关Combine Two Tables --leetcode、Combine Two Tables[leetcode]、Falling Max Swift Combine 出版商、hadoop中的shuffle过程(combine->partition)等更多相关知识的信息可以在本站进行查询。

本文标签: