bow-swift / bow

🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
https://bow-swift.io
Other
645 stars 32 forks source link

Make Coyoneda map stack safe #630

Closed ferranpujolcamins closed 3 years ago

ferranpujolcamins commented 3 years ago

Currently Coyoneda uses function composition to join the inner function with a function we map over. The problem with this is that if you try to evaluate[1] a coyoneda value that has been mapped a lot of times you get a stack overflow.

This PR solves this issue by using a new Function1LazyComposition that keeps a type-erase list of functions that can be called one after the other.

This solution is similar to how Coyoneda was implemented in Bow before, and also how it is implemented in Scala, but Function1LazyComposition keeps track of what the input and output types of the composed function are.

[1] For example when calling lower() or flatMap

ferranpujolcamins commented 3 years ago

Done!

ferranpujolcamins commented 3 years ago

Done

ferranpujolcamins commented 3 years ago

Should be ready now

truizlop commented 3 years ago

Awesome!