News for hackers. Stuff that matters.
Re:Rich Hickey – Inside Transducers [video] by jonahx on Friday November 21, 2014 @06:51AM (#8640854)
From that link:

"The reduce function is the base transformation; any other transformation can be expressed in terms of it (map, filter, etc)."

This seems so obvious in retrospect -- I can't believe I had never made that connection before.

Re:Rich Hickey – Inside Transducers [video] by cousin_it on Friday November 21, 2014 @09:33AM (#8641409)
Yeah. That's actually how you implement lists in lambda calculus, as opaque functions that accept a "visitor". There are two different ways of doing it:

    -- Mogensen-Scott encoding
    data ScottList a = ScottList (forall r. (a -> ScottList a -> r) -> r -> r)

    -- Boehm-Berarducci encoding
    data ChurchList a = ChurchList (forall r. (a -> r -> r) -> r -> r)
Roughly, the first encoding gives you pattern matching, and the second gives you foldr (reduce). Either of these operations is sufficient to do anything with the list.

Also note that both of these are encodings of lazy (potentially infinite) lists. To encode strict (guaranteed finite) lists, you really need algebraic data types like in ML, the visitor pattern can't do that.

Re:Rich Hickey – Inside Transducers [video] by ultimape on Friday November 21, 2014 @07:27AM (#8640937)
I had a similar epiphany while learning about regular expressions in Perl. That connection with text processing is what helped me understand list comprehensions. They seem strikingly similar.

For JS, I personally prefer lo-dash for this kind of work, or dropping in polyfills from MDN.

For some context, here are .map functions applied to normal arrays http://jsperf.com/native-vs-array-js-vs-underscore/54

I've been looking for similar libraries that work on typed-arrays because they are so much more efficient when working with web-workers or with raw canvas data. My attempts at hacking it in feel like they are just bad ideas: http://jsperf.com/float32array-map/2

Re:Rich Hickey – Inside Transducers [video] by grayrest on Friday November 21, 2014 @08:20AM (#8641100)
Most people use underscore/lodash for this stuff. The difference is that the js transducers libraries don't create intermediate arrays, only do enough work to produce the requested output, and work on top of anything that can be coerced into the iteration protocol. I've seen demos of using Facebook's Immutable JS, CSP.js [1], and I don't see why you couldn't put them on top of Typed Arrays or a FRP library like Kefir.

[1] http://jlongster.com/s/nationjs-slides/

Re:Rich Hickey – Inside Transducers [video] by ultimape on Friday November 21, 2014 @08:30AM (#8641135)
Yeah, the overhead of creating intermediate typed arrays is exactly what I'm trying to avoid with this.
Re:Rich Hickey – Inside Transducers [video] by jdd on Friday November 21, 2014 @05:08PM (#8643662)
Lo-Dash 3.0 will have support for lazy evaluation in its chaining syntax that supports shortcut fusion and avoids intermediate arrays as well.
Re:Rich Hickey – Inside Transducers [video] by ultimape on Saturday November 22, 2014 @04:17AM (#8645179)
Do you know if they work with typed arrays?
Re:Rich Hickey – Inside Transducers [video] by tincholio on Saturday November 22, 2014 @04:31AM (#8645199)
Graham Hutton has a very nice tutorial [0] on the universality of fold, which shows this elegantly (in Haskell, though).

[0] Graham Hutton, "A tutorial on the universality and expressiveness of fold", J. Functional Programming 9(4): 355–372, July 1999. http://www.cs.nott.ac.uk/~gmh/fold.pdf

Re:Rich Hickey – Inside Transducers [video] by ajuc on Friday November 21, 2014 @07:53AM (#8641010)
For me the "ephiphany" was when I realized reduce don't need to be (T,T)->T, but can be (T1, T2) -> T1.
Re:Rich Hickey – Inside Transducers [video] by dkersten on Friday November 21, 2014 @11:35AM (#8641978)
They can also be (T1,T2)->T3
Re:Rich Hickey – Inside Transducers [video] by ajuc on Friday November 21, 2014 @02:37PM (#8642935)
Not really?

When you reduce with function f(T1, T2) -> T3 the result of previous iteration becomes first argument for the next iteration, so they must be of the same "type".

Or am I missing something?

Re:Rich Hickey – Inside Transducers [video] by dkersten on Sunday November 23, 2014 @06:24AM (#8648470)
Ah, no, you're right.

I was thinking that mapping can be f(T1)->T2 and how reducing can change types too, but I guess I wasn't paying enough attention because of course the reduce signature is f(accumulator, input) and the output is accumulator, so yea, you're absolutely right: f(T1,T2)->T1