Monday, February 03, 2014

Because Java 8, because Lambda, and because job security.

package java8.test;

import java.util.Arrays;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

public class Main {
 
 interface IDuo<A, B, R> {
  R apply(A n, B g);
 }

 interface ITrio<A, B, C, R> {
  R apply(A n, B p, C g);
 }

 interface IQuad<A, B, C, D, R> {
  R apply(A n, B a, C b, D g);
 }
 
 interface IFI extends IDuo<Integer, IFI, Integer> {} 
 interface IIFI extends ITrio<Integer, Integer, IIFI, Integer> {}
 interface IIIFI extends IQuad<Integer, Integer, Integer, IIIFI, Integer> {}
 
 static UnaryOperator<Integer> predecessor = n -> n - 1;
 static Predicate<Integer> isZero = n -> n == 0;
 static BinaryOperator<Integer> add = (a, b) -> a + b;
 static BinaryOperator<String> concat = (a, b) -> a + b; 
 static BinaryOperator<Integer> multiply = (a, b) -> a * b;
 
 static IFI factorial = (n, g) -> isZero.test(n) ? 1 : multiply.apply(n,g.apply(predecessor.apply(n), g));
 static IIFI tailFactorial = (n, p, g) -> isZero.test(n) ? p : g.apply(predecessor.apply(n), multiply.apply(n, p), g);
 static IIIFI fibonacci = (n, a, b, g) -> isZero.test(n) ? a : g.apply(predecessor.apply(n), b, add.apply(a,b), g);

 public static void main(String[] args) {  
  Arrays.asList(args)
    .stream()
    .map(Integer::decode)
    .map(n -> String.format(
       Arrays.asList("factorial(%d) = %d",
            "tailFactorial(%d) = %d", 
            "fibonacci(%d) = %d")
            .stream()
            .reduce("",
              (a, b) -> Arrays.asList(a,
                      (a.isEmpty() ? a : System.getProperty("line.separator")),
                      b).stream().reduce("", concat)), 
       n, factorial.apply(n, factorial), 
       n, tailFactorial.apply(n, 1, tailFactorial), 
       n, fibonacci.apply(n, 0, 1, fibonacci)))
    .forEach(System.out::println);
 }
}

Because Java 8 and because Lambda

package java8.test;

import java.util.Arrays;

public class Main {

 interface Duo {
  int apply(int n, Duo g);
 }

 interface Trio {
  int apply(int n, int prod, Trio g);
 }

 interface Quad {
  int apply(int n, int a, int b, Quad g);
 }

 static Duo factorial = (n, g) -> n > 0 ? n * g.apply(n - 1, g) : 1;
 static Trio tailFactorial = (n, p, g) -> n == 0 ? p : g.apply(n - 1, n * p, g);
 static Quad fibonacci = (n, a, b, g) -> n == 0 ? a : g.apply(n - 1, b, a + b, g);

 public static void main(String[] args) {  
  Arrays.asList(args)
    .stream()
    .map(Integer::decode)
    .map(n -> String.format(
       Arrays.asList("factorial(%d) = %d",
            "tailFactorial(%d) = %d", 
            "fibonacci(%d) = %d")
            .stream()
            .reduce("",
              (a, b) -> a
                  + (a.isEmpty() ? a : System.getProperty("line.separator"))
                  + b), 
       n, factorial.apply(n, factorial), 
       n, tailFactorial.apply(n, 1, tailFactorial), 
       n, fibonacci.apply(n, 0, 1, fibonacci)))
    .forEach(System.out::println);
 }
}

Friday, August 07, 2009

The Arrow of Time --- Thesis Offense

No sane intelligent being would use a non-positional encoding of information when a positional one is available. Well, no intelligent being that can sense the passage of time.

Order is intimately tied to time. To think of a sequence you need an intuition of time.

Actually, imagining all the basic data structures of mathematics requires a certain such intuition.
To imagine a set, you need an intuition of difference between arbitrary objects --- the elements --- as in element A is different from element B. So, an intuition of individuality.
To imagine a multiset, you need an intuition of similarity between objects, so you can think of 2 objects of type A and 3 objects of type B, for instance. This could be called an intuition of plurality.
To imagine a sequence, you need an intuition of time, which is a dimension of the space-time continuum that we inhabit. This can be identified with an intuition of order.
To imagine a matrix, you need an intuition of dimension or co-ordinates.
Note that a matrix is also a (hyper)graph. A graph is a generalization of a sequence in the sense that if, in the case of a sequence when referring to an element you can think of its predecessor and successor, in a graph, for every vertex you can think of a set of predecessors and successors.

Seems interesting that all these intuitions seem less and less justifiable as we understand more about the "real world". They all seem to belong in Plato's world of Forms.

How can you even ask if A is different from B, when it's impossible to separate A from the not-A? Where does Something become Another? And then further more how can you pretend that A1 and A2 are of the same kind? About the intuition of dimension, current ideas suggest that there are actually 10 or more dimensions, but that the supplemental ones only manifest themselves at a very small scale. Even though time and its arrow do have some strong backing in physics, the conceptual symmetry alone make it tempting me to consider the possibility of the non-existence of time. There is also the tantalizing possibility of (intelligent) beings that do not perceive time, or the passage thereof. And no, I am not thinking of programmers with a deadline or PhD students, but of beings that do not make the cognitive compromise of "time" just so they can imagine they're making some more sense of the world.

All the intuitions required for imagining sets. multisets, sequences and so on, are, in this view, merely notions of limited usefulness or range of applicability that allow us not to despair in the face of the cold, limitless and ultimately incomprehensible reality.

How can we be sure that the behind that blank stare of the baby that never learns to speak and doesn't even seem to interact with the surrounding world there isn't an uncompromising mental process that refuses to ingest "chunks" of the reality, and instead incessantly looks for the continuation of the message? EEG might help, but does it always?

Monday, July 02, 2007

All my Life, is Changing Every Day, In Every Possible Way

This is actually a follow-up on a previous post, Information is Life In Formation.

I recently read on SlashdotFreeman Dyson about an article of Freeman Dyson, a well-known physicist and mathematician, supposedly about "Open Source Biology". His article, entitled Our Biotech Future, in turn, references the work of Carl Woese, famous American microbiologist - the discoverer of Archaea among other things.

Specifically, it references two of Woese's articles, "A New Biology for a New Century"(abstract|full text) and Biology's Next Revolution. Making a nice and interesting story abruptly short, in those articles Woese lends support to the idea that in the beginning, Life wasn't about Species. Instead, Life was Open Source, in the sense that in the primordial soup, primitive life-forms were exchanging genetic material in a kind of free, horizontal manner, like a sort of massively parallel rewriting system of sorts.

And all was wonderful in the Garden of Eden, till this organism (likened to Bill Gates) appeared, that didn't allow exchange of genetic material with the others. And so, Bacteria, Archaea and Eukarya or Eukaryotes developed in species, only transferring genetic material in a vertical manner - to their descendents, instead of the previously ubiquitous horizontal transfer. The species-based Life prevailed, probably being more resilient to destructive change, and the primordial soup organisms disappeared, but this also meant a significant slow down of evolution.One should note that HGT is still common in Bacteria, and even between Bacteria and fungi, for example, and it is thought to be one of the important causes of the development of bacterial drug resistance.

Nowadays, Dyson argues, the wheel has turned full circle and humans, through the use of advanced breeding and genetic engineering are able to transfer genetic material from some organisms to others, essentially being able to break the interspecies barrier, creating chimeras, transgenic organisms, like in the golden age of horizontal genetic material exchange.
The Young FamilyWell, that sounded to me a lot like that previous post, that stated that all Life needed is Media for Information pattern storage and interaction, an initial Seeding of the media, and Time. Oh, and about the Tree of Life, there seems to be none. In the presence of HGT, the Tree of Life has no real root, but an initial Seeding.

How does this relate to cellular automata like Conway's Game of Life, where stable, resilient-to-change patterns also appear? Would a similar computational device be able to create a "full-blown" (whatever that means) species structure? Food for further thought.

Anyway, I (just may) have told you so [tm] :-). Actually, the most striking idea seems the fact that, although at the publishing of the former post about Information being Life in Formation I had almost no idea about HGT and the details about the development of primitive Life, and was composing a picture based on the Computational, Information-theoretic approach and intuitions, its main conclusions are largely confirmed by biological "facts". So, even if the Universe isn't, after all, a gigantic cellular automaton as some would say, it still seems that Information processing and Life are so intimately, essentially linked that there is no conceivable way one could exist without the other: the essence of Life lies at least as much in its Software, as in its Hard(Wet)ware (see also another post).

The fine print:

Sunday, June 10, 2007

Designer genes

Slashdot fortune: Do molecular biologists wear designer genes?

Saturday, May 12, 2007

123133721

Friday, April 20, 2007

The Illusionist

The Illusionist (No Strings Attached)

The spirit drinks are finished,
Rusted are the wheels and chains,
No smoky trails in mirror relished,
Dismantled by our weary brains.

The magic flowers have withered,
The old hat is ripped and torn,
No phantom rabbits lingered,
In former white gloves never worn.

No dazzling tricks upon us
From your coffers to bestow,
But for the ghostly chorus
"As above, so below".


-- O. Cean - 1337 Magickal Spells

Saturday, April 14, 2007

More numbers

Find numbers N and M such that:
  1. N!*M! + N*(N+M)*(M+M)^N = (N+M)!
  2. N!*M!*(N+M)! = N*(N*M)!