1

I'm continuing my foray into functional swift and very much enjoying the challenge. I'm working with Transforms which turn elements into lazy sequences.

To state the error up front, I'm getting: Cannot convert value of type 'Transform' (aka '(Int) -> LazySequence>') to expected argument type '() -> LazySequence<[]>'

My problem is with composing them but I need to give some context to show the issue.

Here's the Transform:

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

I've worked round this a few times but keep getting stuck. Any thoughts on how to resolve appreciated.

(My previous question is similar territory but a different issue: Swift: Lazily encapsulating chains of map, filter, flatMap )

For a playground:

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
Cortado-J
  • 2,035
  • 2
  • 20
  • 32

1 Answers1

1

I have a partial answer, but maybe it can help you get somewhere.

First of all, Transform<T, U> is defined as (T) -> LazySequence<[U]>, so U and V generic types can not be specialized as a 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
    }
}

Second, your |~> operator accepts a Transform as a right hand parameter, so you can not use it with the return type of the >~> parameter. I was able to get the result with the following line:

let result4 = start.flatMap(composed)

You could probably overload the |~> operator to accept the correct type, but it would not look nice. Or maybe it would, with enough typealiasing :)

pckill
  • 3,709
  • 36
  • 48
  • Thanks, @pckill. I can see why the Sequence specialization needs removing and your other comments useful. I'll post back when I work this out. – Cortado-J Feb 26 '19 at 21:24