May. 14th, 2010

juan_gandhi: (Default)
Last night I've managed to write a class LazyPair where x is fixed and y is lazily calculated, via a given function.

Then I started thinking about variance. It is positive on y, but how about x? It is not so for x, since we have a function, and its argument should have negative variance. But I wrote in LazyPair definition that it implements Product2, that is, I'm saying it is "something like" a Cartesian product. So. If it is a Cartesian product, it should be covariant on both arguments.

So, whad do we have, given X and YX, and, of couse, we have X×YX → X×Y - but it is not a natural transformation. In short, bad luck. No functoriality. So I probably have to throw away the idea of lazy pair.

So there's a question regarding an adequate categorical interpretation of caching in general. I kind of can't figure out, how to deal with it.

In my specific case, in my code, I'll be replacing LazyPair[X, Y] with (X, X=>Y) (this means X×YX in Scala). But then thunk disappears. Something is wrong here.

Upd: thank you, [livejournal.com profile] huzhepidarasa! Got this stuff working:
  test("lazy pair should be covariant in first parameter") {
    val p: Product2[Int, Int] = LazyPair(123, (x:Int) => x * x)
    val q: Product2[Any, Int] = p
    expect(123) {q._1}
  }

  test("lazy pair should be covariant in second parameter") {
    val p: Product2[Int, Int] = LazyPair(8, (x:Int) => x * x)
    val q: Product2[Int, Any] = p
    expect(64) {q._2}
  }

  test("lazy pair assignment does not involve evaluation") {
    var n = 0;
    val p: Product2[Int, Int] = LazyPair(8, (x:Int) => {n = n + 1; x * x})
    expect(0){n}
    val q: Product2[Int, Any] = p
    expect(0){n}
    expect(64) {q._2}
    expect(1){n}
  }


(and thank you Martin for removing < from Scala!)
juan_gandhi: (Default)
source

parallel quicksort ([:...:] mean parallel array)
sort :: [:Float:] -> [:Float:]
sort a = if (lengthP a <= 1) then a
         else sa!0 +++ eq +++ sa!1
   where
      m = a!0
      lt = [: f | f<-a, f<m :]
      eq = [: f | f<a, f == m :]
      gr = [: f | f<-a, f>m :]
      sa = [: sort a | a <- [:lt,gr:] :]


The trick is this: the same quicksort is done in parallel, using parallel array comprehension in the list line.
We could write lt +++ eq +++ gr, but then there's no parallelism. And here parallelism is provided by doing parallel array comprehension. "sort both left and right in parallel - because we can" - of course it causes parallel sorting of smaller pieces, etc.
juan_gandhi: (Default)
здесь

Introduction
Measuring and Weighing
Foams
Emulsions
Colloids, Gels and Suspensions
Oils and Fats
Solutions
Crystallization
Protein Chemistry
Biology
Scaling recipes
The Effects of Heating
Acids and Bases
Oxidation and reduction
Recipes
juan_gandhi: (Default)
What's the difference between Sets.<String>EMPTY and Sets.<Integer>EMPTY?

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78 9 10 111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 13th, 2025 12:42 pm
Powered by Dreamwidth Studios