带有惰性序列变换的Swift问题

时间:2019-02-26 08:46:29

标签: swift functional-programming lazy-sequences

我将继续涉足功能敏捷领域,并非常享受挑战。 我正在使用将元素转换为惰性序列的Transforms。

要预先指出错误,我得到: 无法转换类型的值 'Transform'(又名'(Int)-> LazySequence>') 预期参数类型 '()-> LazySequence <[]>'

我的问题是撰写它们,但我需要提供一些背景信息以证明问题。

这是变换:

typealias Transform<T, U> = (T) -> LazySequence<[U]>

我可以定义正向申请:

precedencegroup LazyForwardApplication {
  associativity: left
}

infix operator |~>: LazyForwardApplication

func |~> <T: LazySequenceProtocol, U>(
  input: T,
  transform: @escaping Transform<T.Elements.Element,U>
  ) -> LazySequence<FlattenSequence<LazyMapSequence<T.Elements, LazySequence<[U]>>>> {

  return input.flatMap(transform)
}

返回类型有点麻烦,但效果很好:

let start = [10,20,30].lazy

let add4_5_6: Transform<Int, Int> = {
  let result = [ $0 + 4, $0 + 5, $0 + 6]
  print("> add4_5_6(\($0)) -> \(result)")
  return result.lazy
}

//请注意,我将调试放入了一部分,因此可以确定它是延迟发生的。

let result1 = start |~> add4_5_6
result1.forEach{ print($0) }
// 14, 15, 16, 24, 25, 26, 34, 35, 36

另一个类似的例子:

let add7000_8000: Transform<Int, Int> = {
  let result = [ $0 + 7000, $0 + 8000]
  print("> add7000_8000(\($0)) -> \(result)")
  return result.lazy
}

let result2 = start |~> add7000_8000
result2.forEach{ print($0) }
// 7010, 8010, 7020, 8020, 7030, 8030

我可以将它们串联在一起:

// Double application
let result3 = start |~> add4_5_6 |~> add7000_8000
result3.forEach{ print($0) }
// 7014, 8014, 7015, 8015, 7016, 8016,
// 7024, 8024, 7025, 8025, 7026, 8026,
// 7034, 8034, 7035, 8035, 7036, 8036

但是我也希望能够组成它们:

// Forward Composition
precedencegroup LazyForwardComposition {
  associativity: right
}
infix operator >~>: LazyForwardComposition

func >~> <T, U: Sequence, V: Sequence>(
  left:  @escaping Transform<T,U>,
  right: @escaping Transform<U,V>
  ) -> (T) -> LazySequence<FlattenSequence<LazyMapSequence<[U], LazySequence<[V]>>>> {

  return { input in
    let b: LazySequence<[U]> = left(input)
    let c = b.flatMap(right)
    return c
  }
}

这是我得到错误的地方:

let composed = add4_5_6 >~> add7000_8000
// ERROR IN ABOVE LINE: Cannot convert value of type
'Transform<Int, Int>' (aka '(Int) -> LazySequence<Array<Int>>')
to expected argument type
'(_) -> LazySequence<[_]>'

let result4 = start |~> composed
result4.forEach{ print($0) }

结果将与result3相同

我已经为此工作了几次,但一直陷于困境。 关于解决问题的任何想法。

(我之前的问题是相似的领域,但问题不同: Swift: Lazily encapsulating chains of map, filter, flatMap

对于游乐场:

typealias Transform<T, U> = (T) -> LazySequence<[U]>

// And I can define Forward Application:

precedencegroup LazyForwardApplication {
  associativity: left
}

infix operator |~>: LazyForwardApplication

func |~> <T: LazySequenceProtocol, U>(
  input: T,
  transform: @escaping Transform<T.Elements.Element,U>
  ) -> LazySequence<FlattenSequence<LazyMapSequence<T.Elements, LazySequence<[U]>>>> {

  return input.flatMap(transform)
}

// The return type is a bit of a mouthful but it works fine:

let start = [10,20,30].lazy

let add4_5_6: Transform<Int, Int> = {
  let result = [ $0 + 4, $0 + 5, $0 + 6]
  print("> add4_5_6(\($0)) -> \(result)")
  return result.lazy
}

// Note that I put the debug in partly so I can be sure that it's happening lazily.

let result1 = start |~> add4_5_6
result1.forEach{ print($0) }
// 14, 15, 16, 24, 25, 26, 34, 35, 36

// And another similar example:

let add7000_8000: Transform<Int, Int> = {
  let result = [ $0 + 7000, $0 + 8000]
  print("> add7000_8000(\($0)) -> \(result)")
  return result.lazy
}

let result2 = start |~> add7000_8000
result2.forEach{ print($0) }
// 7010, 8010, 7020, 8020, 7030, 8030

// And I can chain these together inline: 

// Double application
let result3 = start |~> add4_5_6 |~> add7000_8000
result3.forEach{ print($0) }
// 7014, 8014, 7015, 8015, 7016, 8016,
// 7024, 8024, 7025, 8025, 7026, 8026,
// 7034, 8034, 7035, 8035, 7036, 8036

// But I'd like to be able to compose them too:

// Forward Composition
precedencegroup LazyForwardComposition {
  associativity: right
}
infix operator >~>: LazyForwardComposition

func >~> <T, U: Sequence, V: Sequence>(
  left:  @escaping Transform<T,U>,
  right: @escaping Transform<U,V>
  ) -> (T) -> LazySequence<FlattenSequence<LazyMapSequence<[U], LazySequence<[V]>>>> {

  return { input in
    let b: LazySequence<[U]> = left(input)
    let c = b.flatMap(right)
    return c
  }
}

// And here's where I get an error:

let composed = add4_5_6 >~> add7000_8000
// ERROR IN ABOVE LINE: Cannot convert value of type 'Transform<Int, Int>' (aka '(Int) -> LazySequence<Array<Int>>') to expected argument type '(_) -> LazySequence<[_]>'

let result4 = start |~> composed
result4.forEach{ print($0) }

// The result would come out the same as result3

1 个答案:

答案 0 :(得分:1)

我有一个部分答案,但也许可以帮助您到达某个地方。

首先,Transform<T, U>被定义为(T) -> LazySequence<[U]>,因此UV泛型不能被特殊化为Sequence

func >~> <T, U, V>(
    left:  @escaping Transform<T,U>,
    right: @escaping Transform<U,V>
    ) -> (T) -> LazySequence<FlattenSequence<LazyMapSequence<[U], LazySequence<[V]>>>> {

    return { input in
        let b = left(input)
        let c = b.flatMap(right)
        return c
    }
}

第二,您的|~>运算符接受Transform作为右手参数,因此您不能将其与>~>参数的返回类型一起使用。我能够通过以下行得到结果:

let result4 = start.flatMap(composed)

您可能会重载|~>运算符以接受正确的类型,但是看起来并不好。也许有足够的typealiasing:)