
Type-safe Transducers in Clojure, Scala, and Haskell - luu
http://blog.podsnap.com/ducers2.html
======
kentosi
This line made me laugh out loud: "... whenever something is even slightly
ugly in Scala, you introduce an implicit to make it confusing instead."

~~~
frowaway001
Odersky wrote a simple implementation, and it looks a lot nicer. It also
doesn't use any casts or implicit conversaions:
[https://gist.github.com/odersky/6b7c0eb4731058803dfd](https://gist.github.com/odersky/6b7c0eb4731058803dfd)

~~~
podsnap
True. In retrospect, trying to make the Scala transducer type look like
Haskell and then hacking into working order was didn't do justice to either
language. As I sort of note in the text, the attempt to do it this way was at
least partially meant to be "amusing."

------
brandonbloom
This is interesting, but it's still missing some very important features of
transducers. As you alluded, stateful operations [1] won't work at all in your
Haskell model. Also, you've missed early termination [2], which Rich's talk
[3] covered well.

[1]
[https://github.com/clojure/clojure/blob/b01adb859c66322c5cff...](https://github.com/clojure/clojure/blob/b01adb859c66322c5cff1ca9dc1ad98585f2cdc1/src/clj/clojure/core.clj#L2719-L2744)

[2]
[https://github.com/clojure/clojure/blob/b01adb859c66322c5cff...](https://github.com/clojure/clojure/blob/b01adb859c66322c5cff1ca9dc1ad98585f2cdc1/src/clj/clojure/core.clj#L2694-L2698)

[3]
[https://www.youtube.com/watch?v=6mTbuzafcII](https://www.youtube.com/watch?v=6mTbuzafcII)

~~~
wyager
>As you alluded, stateful operations [1] won't work at all in your Haskell
model.

Couldn't you just parametrize over IO/ST/etc.? E.g. something like

    
    
        IO r -> a -> IO r
    

I don't have a great mental model of transducers yet, so I'm not confident
about that.

~~~
jules
You can do that, but it's more elegant if you define a functional stateful
transducer analogous to scanl for lists.

~~~
davdar
That's like saying "your program is more elegant if it has no monads". It's an
incorrect statement. The monadic version is perfectly elegant. Even better:
it's the right one.

~~~
jules
By the same logic scanl should just have the type:

    
    
        (b -> IO a) -> [b] -> IO [a]
    

instead of

    
    
        (a -> b -> a) -> a -> [b] -> [a]
    

Just putting the whole thing in the IO/ST monad isn't the right solution when
you need a very specific form of local state.

