Motivation

Clojure's transducers represent an elegant and high-efficient way to transform data. JS implementation fared well, so I decided to try implementing them in Swift as well.

Examples

transduce(
    map({$0 + 1}) |>
    filter({$0 % 2 == 0}) |>
    map({$0 * 2}), append, [], [1, 2, 3, 4, 5])

// [4, 8, 12]

Code above is roughly equvivalent to following code with regular map / filter calls:

[1, 2, 3, 4, 5]
    .map({$0 + 1})
    .filter({$0 % 2 == 0})
    .map({$0 * 2})

Problems with Swift

Types. Swift is strongly typed so we get function signatures like this one:

func transduce<AccumType, ElemType1, ElemType2> (
        tfn : Transducer<AccumType, ElemType1, ElemType2>,
        rfn : (AccumType, ElemType2) -> AccumType,
    initial : AccumType, arr : [ElemType1]) -> AccumType]

I doubt anyone would find it nice.

Performance is even bigger problem for now. Currently it is anything but ready for production use.
As tested on Xcode Version 6.1.1 (6A2008a), it is at least 100x worse than standard map / filter chain.

Further info

See transducers implementation in this GitHub repo.

Shameless plug

We develop mobile and web apps, you can hire us to work on your next project.