News for hackers. Stuff that matters.
Re:Rich Hickey – Inside Transducers [video] by charlysisto on Friday November 21, 2014 @08:38AM (#8641165)
So Transducers generalize the usage of Enumerable functions such as map, reduce, filter, flatMap (...).

Please could someone tell me if this is conceptually different from ruby's Enumerable module which only needs the class its included in to implement `each` so anything can be enumerable ? Or is it a similar but just in translated to the FP world ?

Re:Rich Hickey – Inside Transducers [video] by YuriNiyazov on Friday November 21, 2014 @08:51AM (#8641225)
Yes; There are differences.

1. Transducers are parallel under the covers. Since the expectations is that the code that the predicate or mapping functions that you pass into map, filter and reduce are pure (no variables are changed, no state is modified, just a calculation that is only dependent on arguments) the parallelism is hidden away from you, but it's there. Ruby's Enumerable can't do parallelism

2. When you compose transducers, there are no intermediate sequences generated. The simplest example is this:

In Ruby: [1, 2, 3].map {|x| x + 1}.map {|x| x+ 2} will generate an intermediate array [2, 3, 4] after the first map, and then will generate [4, 5, 6] when it has evaluated the whole expression.

In Clojure (I am sure I got the syntax wrong for this one) (transduce (map #(+ 2 %) (map inc)) [1 2 3]) will create an intermediate mapping function that will first increment, then add 2 to its argument, and then will map once using that intermediate function.

Re:Rich Hickey – Inside Transducers [video] by cfeduke on Friday November 21, 2014 @08:56AM (#8641248)
Transducers _can be_ parallelized (TODO) but some transducer implementations do contain state, like `take` and therefore cannot be parallelized.

The big thing is no intermediate results.

Re:Rich Hickey – Inside Transducers [video] by YuriNiyazov on Friday November 21, 2014 @09:43AM (#8641441)
If you watch the first Rich Hickey Transducers talk, he shows that some transducers (the ones that rely in the underlying reduce implementation that uses fork/join and assumes that reducing the collection is associative) are already parallelized. I agree with you regarding `take`
Re:Rich Hickey – Inside Transducers [video] by cfeduke on Friday November 21, 2014 @01:01PM (#8642409)
IIRC from yesterday's talk he was explaining that these will be easy to parallelize, and its slated for 1.7. I had thought they were already parallel myself until I saw the talk; and possibly some of them are, but the item is still Open.

http://dev.clojure.org/jira/browse/CLJ-1553

Re:Rich Hickey – Inside Transducers [video] by YuriNiyazov on Friday November 21, 2014 @02:23PM (#8642837)
I was just talking about the "reduce fold" being already parallel.
Re:Rich Hickey – Inside Transducers [video] by nickik on Friday November 21, 2014 @12:46PM (#8642339)
The big thing is not even that, the big thing is that you can seperate your logic from the data that you want to process. So instead of (map inc [1 2 3 4]) you can do (def inc-transducer (map inc)) and then use that for any datastructures, channels or whatever other transducer context people come up with. To be sure, you would not do that with (map inc) but if you have more logic, you could.
Re:Rich Hickey – Inside Transducers [video] by seanmcdirmid on Friday November 21, 2014 @09:07AM (#8641287)
So transducers are like .Net/LINQ enumerables?
Re:Rich Hickey – Inside Transducers [video] by Skinney on Friday November 21, 2014 @12:05PM (#8642111)
Nah, Clojure seqs are more like .NET/LINQ enumerables.

Functions that work over seqs/enumerables return a new seq/enumerable. From what I understand, and I'm sure I am wrong, a transducer receives and transforms a value, and may call the next transducer with the transformed value.

The nice thing is that a transducer does not create intermediate results (a seq/enumerable), and that it doesn't make any assumptions on the underlying datastructure. So you can, for instance, send a value through a transducer and directly place it in a list, instead of creating a set of enumerables and then call .ToList on that. Since transducers can work with any value, you also aren't tied to enumerables. So transducers should be a little more universal and performant than enumerables.

Re:Rich Hickey – Inside Transducers [video] by icefox on Friday November 21, 2014 @01:18PM (#8642485)
A contrived example in C#/LINQ

  Enumerable.Range(1, 100)
	  .Where(n => n % 2 == 0)
	  .Select(n => n * 2)
          .ToList();
And as a transducer it could looks something like this?

  sequence(
    Enumerable.Range(1, 100),
    compose(
      filter(n => n %2 == 0),
      map(x => x + 1)
    )
  ).ToList();
On the linq side it would create only 2 enumerable objects (one for where and select) and the ToList would result in a copy the object pointer as it was passing through each enumerable. For the transducer example rather than having 2 enumerable's we would have 2 transducer objects?

I absolutely understand the case where a language's map/filter/reduce/etc results in a whole new list, but for the above case the performance/memory overhead improvement doesn't see that huge and really minor. The fact that it removes the overhead of having to support IEnumerable and as you mentioned is only dealing with functions is what seems like a win. At first I was thinking that plain old LINQ is also more readable, but my contrived c# transducer example doesn't look that bad in the end. Am I incorrect on any of this?

Re:Rich Hickey – Inside Transducers [video] by ajanuary on Friday November 21, 2014 @04:24PM (#8643449)
With Linq you can't really create an object/function that describes the transformation that should happen. I start with an enumerable, describe how to transform /that/ enumerable, and in the end have something that describes a transformation applied to a particular initial data. I can't do anything with it but laziy read the transformed values for the original input.

With transducers I can describe a transformation that should happen to /some/ reducable input, and in the end have something that represents just the transformation. I. An then apply that to any reducable input I have.

[edit]

Your example becomes:

  var incrementedEvens = compose(
      filter(n => n %2 == 0),
      map(x => x + 1)
  );
  transduce(Enumerable.range(0, 100), incrementEvens);

  var lowerAlph = compose(
    filter(x => Character.isAlpha(x)),
    map(x => x.ToLowercase())
  );
  System.in.setTransducer(lowerAlpha);
You can really do that in Linq.
Re:Rich Hickey – Inside Transducers [video] by charlysisto on Friday November 21, 2014 @09:24AM (#8641359)
Thanks for the answer. Indeed Ruby in general has the limitations inherent to its "non functional" nature. However regarding 2) Enumeration can now (in ruby 2.0) be lazy and thus take each item one after the other through the whole chain.

(1..Float::INFINITY).lazy.map{|a| p a; a*10}.map{|a| p a+1} # => 1 11 2 21 etc...

So it can work with streams & so on... But I know (from a Jessica Kerr presentation) it also has limitations there.

Re:Rich Hickey – Inside Transducers [video] by drostie on Friday November 21, 2014 @11:28AM (#8641937)
Transducers are a different kind of thing, not just a generalized usage of enumerable functions.

Clojure transducers are isomorphic to functions of type `a -> [b]`, that is, functions which take some thing and return lists of other things. You can implement map and filter in this language:

    tmap f a = [f a]
    tfilter p a = if p a then [a] else []
You can also compose two such functions:

    concat :: [[x]] -> [x]
    concat [] = []
    concat (x : xs) = x ++ concat xs
    
    concatMap f list = concat (map f list)
    
    compose lb_a lc_b = \a -> concatMap lc_b (lb_a a)
One reduction function (`foldr`) turns out to just be the identity function [a] -> [a], as I recall.

The interesting thing about Clojure transformers is that they have a strange continuation-passing form, such that this function `compose` that I wrote above for the type `a -> [b]` is actually in Clojure just a function composition. That is, instead of `a -> [b]` we see `forall r. (b -> r -> r) -> (a -> r -> r)`, which composes with normal function composition.

Re:Rich Hickey – Inside Transducers [video] by moomin on Friday November 21, 2014 @08:51AM (#8641224)
What's you're thinking of is more like ISeq in Clojure, or Foldable is Haskell. The more interesting generalisation is not between arrays, vectors and hashsets, but between data structures and event streams.