Money pooling based on random tiered payout

This idea was created in response to the recession. The goal was to inject cash into the economy as rapidly as possible. A reaction to the “shovel ready” bureaucratic approach.

I subsequently found that this is not an altogether new idea. Perhaps the tiers and random elements are something new.
~~~~~
Abstract
To accelerate the velocity of money flow for economic stimulus, a payout of a pool of money among contributors randomly placed in payout level tiers is advocated. The randomness of selection and increased frequency of payout creates a network effect that increases demand for goods and services. This will also have social benefits by increasing morale and unity of purpose, unlike present political inspired economic approaches.

Key words: Taxes, Economy, Unemployment

Reverse Taxes

Taxes are a necessary evil. One purpose of taxation is to fund social services that benefit all. For example, taxes pay for road maintenance. The street in front of my house gets repaired because my neighbors and I pay our taxes. However, if my own driveway needs maintenance, I have to fund that since only I own it and gain value (well, the bank owns it still, details). Yet, if I don’t maintain my driveway and it becomes a dangerous eyesore and attracts vermin, not only will my standard of living suffer, but eventually my neighbors will too: esteem, property values, and so forth. So, it behooves my neighbors to either drive me out with flaming torches and pitchforks or more civilly, encourage me to get with the program.

The problem is that families, especially in today’s economy, are not able to attend to many issues in a timely manner. They are cash strapped and, unfortunately, consumer fixated. Regardless of the reasons, there are personal as well as social-economic reasons for this. Even certain Wealth based religions and philosophies have a hand in making this situation worse.

A reverse tax is a tax that instead of going to a government agency, goes directly to private tax payers. These tax payers choose on their own what to do with it. Some will splurge, drink, or go further into debt. However, many will choose to act responsibly and improve their lives and immediate environment. Regardless of what they do, the funds will be quickly injected into the economy.

With Stochastic Reverse Taxation Subsidization (SRTS), funds can be made available that bypass the “public” route, where the individual directly chooses what needs to be funded. Since in each period those receiving payout are random (but everyone will get paid), the odds are that a portion of these people will buy goods and pay for services. This will increasingly stimulate demand with each period.

This is a direct application of changing one aspect of a system to influence the whole. In this case we are increasing the velocity of money. Instead of top-down approaches, a direct mid-level insertion is made. Thus, money is exchanged quicker, spread more rapidly without the red-tape and bureaucratic siphoning.

Implementation

Mathematics

Just how much would be available? There were 144,103,375 individual income tax returns in FY 2009. If we include the disenfranchised and those in the in the underground or shadow economy, the actual number of people who “could” file are actually much larger. Let’s just make it 200M. (the population of the USA is about 310,409,288 people). If we make the contribution $42, that is 13,037,190,096 about $13 Billion to stochastically redistribute. Choosing 42 weeks for a campaign, we will distribute $310 million dollars a week. This does not sound like a lot, but it is the quick “injection”; it is not waiting for “shovel-ready projects” that just fatten those in the loop.

If we create four payout tiers, then it is now a simple matter to determine the number of people and how much each be allocated per week. Can it be made no-loss, that is, no one would lose their $42? Probably not. We want there to be sizable distributions. Since we want some sizeable feedback back into the general system, the top tier will get a substantial return.
• Tier 1: X1 people would each get $1 million.
• Tier 2: x2 people would get thousands.
• Tier 3: x3 people would get hundreds
• Tier 4: x4 people would just get their $5 dollars back.

The randomness comes from assigning contributors to a tier. Each cycle will create a new random assignment but with algorithms to make sure every person gets to be in each tier after a few cycles. That is, the same person will not be in the same tier all the time.

Compare this to the recent tax breaks to spur the economy. What did the normal tax payer get $50? And that is a one-time year long break, I guess that would buy a few more take-out meals for lunch.

A mathematician/economist would have to create the algorithms for this process. Maybe it is not possible to do this and make it attractive for people to join in.

Note that this is different than the fixed payout algorithms or schedules like those used at eMoneyPool or Indian Chitty pools.

The problem with conventional pools is that the incentive is reduced the more people are part of the pool since the length of the ‘campaign’ is proportional to the desired payout level.

Administration

Using the existing Tax System?

The simplest approach is to just have a new checkbox on the tax form stating that one would like to participate in the Reverse Tax System. One possibility is to use a contribution value one, five, ten dollars. The payoff would be proportional. This would be the worse approach to use if we want to stimulate the economy. For that we would need a more accelerated schedule, perhaps monthly, optimally weekly. Using the Internal Revenue infrastructure is just convenient since the tax payer information is already available.

But, the tax schedule is yearly. We need a more aggressive schedule. Weekly! Not only would this be more effective in any stimulus effect, it would make the system more attractive, since there would be more ‘churn’.

Requirements and Limitations

Non-profit

This is a stimulus package for the middle class. Thus, the Wall Street crowd should not even know about it. :)

Not a lottery

This approach is similar to a multi-state lottery. Many differences however. Lotteries are for-profit and there are many losers (most!). The approach suggested here is not a payment, but a temporary allocation of funds. If the math is done correctly there is minimal loss other than normal inflationary devaluation of liquid assets within the payment schedule period and administrative costs.

Tax Free

Enough with the taxes. The payouts should be tax free. They should be totally reintroduced into the system so that demand is increased.

No competition

The effectiveness of this approach would be reduced if there are competing systems. Furthermore, it would be much easier for abuse if other systems were allowed.

No new bureaucracy

Taxes, are unavoidable, but they are also a giant spigot that feeds a growing never satiated beast. Bureaucracies grow. And grow. And become less effective, so they sprout new growths, that grow. And grow.

No debt generation

There is the possibility for a family to receive a guaranteed amount within a certain time period. So, institutions and businesses would be happy to capitalize on that. For example, offering various loans or “deals”. A great example of this are pay date loan companies. These should all be disallowed. An easy way to assure this is to not preannounce or make a big deal about it. It’s not that many large payments would be optimal anyway.

How to incorporate compound interest and investment?

Traditional “chits” or money pools are for short term financial of poor or modest groups. The modern banking and financial industry is used for more long term goals. If pools are used, money that should go into interest generating investments will suffer.

History

Something I posted on my blog on December 5, 2011, with the title “Demand Stimulation By Stochastic Reverse Taxation”. I came up with the idea for a massive “chit fund” that could pay out more often in a random schedule and people would get a payout based on a tiered system.

A few years ago I had a crazy idea. What if people pooled part of their money together and then redistributed it among the pool participants? Yes, not a new idea. A form of this even appears in “Stranger in a Strange Land” by Robert Heinlein. but what I was thinking was doing this on a massive scale, very massive. What if this massive “office chit pool” guaranteed that at each distribution at least hundreds of people would get a million bucks, others would get thousands, and the rest just a few hundred?

I abandoned the idea. It would not work, for one thing, you would need a lot of people, and the more people the longer it would be for each to be in line for the big payout. Would anyone wait years putting money into the pool just so one year they can get a substantial payout just to buy some needed item? No. It would probably be more advantages to just invest the money, at least there would be an accumulating interest payment.

Another try

Ideas, sometimes even bad ones, keep coming back. See this graph: They keep coming back

On July 19, 2010, I had another thought, what if instead of a rotation, one uses a random distribution into a payment hierarchy.

One pays into the pool, and at each distribution period one is randomly placed into a payout tier and the income distributed. In subsequent draws one is not placed into the same tier as the prior draws. When everyone has participated in each tier, the cycle could be repeated.

This is more reasonable. Using randomization, payout tiers, and shorter cycles would make this more acceptable. It would be like an income co-op.

Revisiting The Idea

Months later I revisited the idea. Could this be useful as some kind of economic stimulus? A “for the people” stimulus? Something to boost employment, increase opportunity, increased morale, and motivation? Probably not, there are no Silver Bullets. But, could it help?

Further reading
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Use JAXB to export Subversion log to Java

In a previous blog post I obtained the subversion XML log output. Now I need to convert that into Java objects in order to provide some special reporting requirements. Below I just present the unmarshal code.

Updates
Dec 3, 2014: cloned code into a github repository

As mentioned before, by using a high-level Java API to Subversion, like SVNKit, we can generate logs and have those already unmarshaled into objects. This is the recommended approach.

Let’s continue with the “brute force” way of accessing the output XML log output.

In listing one the Unmarshal class is a utility that hides the JAXB unmarshal code. The actual use of JAXB is a few lines, but this class provides methods that accepts various sources. For example using a file path:
Log theLog = new Unmarshal().path(“src/test/resources/log.xml”);

Unmarshal.java
package com.octodecillion.svn;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.SAXException;

import com.google.common.base.Preconditions;
import com.google.common.io.CharSource;
import com.google.common.io.Resources;

/**
 * @author jbetancourt
 */
public class Unmarshal {
    /** */
    public Log path(String path) throws JAXBException, SAXException,
            IOException {
        Preconditions.checkNotNull(path, "'path' param is null");
        return url(new File(path).toURI().toURL());
    }

    /**  */
    public Log url(String url) throws JAXBException, SAXException, IOException {
        Preconditions.checkNotNull(url, "'url' param is null");
        CharSource charSrc = Resources.asCharSource(new URL(url),
                Charset.defaultCharset());
        return unmarshall(charSrc);
    }

    /**  */
    public Log url(URL url) throws JAXBException, SAXException, IOException {
        Preconditions.checkNotNull(url, "'url' param is null");
        CharSource charSrc = Resources.asCharSource(url,
                Charset.defaultCharset());
        return unmarshall(charSrc);
    }

    /** */
    public Log string(String xml) throws JAXBException, SAXException,
            IOException {
        Preconditions.checkNotNull(xml, "'xml' param is null");
        return unmarshall(CharSource.wrap(xml));
    }

    /** */
    public Log unmarshall(CharSource in) throws JAXBException, SAXException,
            IOException {
        Preconditions.checkNotNull(in, "'in' param is null");
        JAXBContext jaxbContext = JAXBContext.newInstance(Log.class);
        Log theLog = null;
        
        try(Reader reader = in.openStream()){
            StreamSource source = new StreamSource(reader);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<Log> jxbElement = unmarshaller.unmarshal(source, Log.class);
            theLog = jxbElement.getValue();
        }
        
        return theLog;
    }
}


Listing 1, Unmarshal class

The Classes tree that captures the SVN log output XML
The log is in this format:

<log>
  <logentry revision="20950">
	<author>jbetancourt</author>
	<date>2014-11-10T20:12:11.910891Z</date>
	<paths>
		<path text-mods="false" kind="file" action="D" prop-mods="false">/2014/Acme/branches/rabbit-trap/www/images/beep.png
		</path>
	</paths>
	<msg>initial commit</msg>
   </logentry>
   <logentry revision="20948">
.....
</log>

Listing 0, example log contents

To use JAXB we create annotated classes to match the log XML.

These are simple use of JAXB. I’m sure there are better approaches. Note, the getter/setters
were not put in. Does a JAXB processor need these? Can’t it just use reflection?

The toString() in these classes return a JSON marshaling of the object. This is helpful for unit testing and debug.

Log.java

package com.octodecillion.svn;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author j.betancourt
 */
@XmlRootElement
public class Log {
	@XmlElement(name = "logentry")
	List<LogEntry> entries;

        @Override
	public String toString() {
		StringBuilder bld = new StringBuilder();
		for(LogEntry entry : entries){
			bld.append(entry.toString());
		}
		
		return bld.toString();
	}	
	
}

Listing 2, Log class

LogEntry.java
package com.octodecillion.svn;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.google.common.base.Joiner;

/**
 * @author j.betancourt
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class LogEntry {
	@XmlAttribute
	String revision;
	String author;
	String date;
	@XmlElementWrapper(name="paths")
	@XmlElement(name="path")
	List<Path>paths;
	String msg;

        // getter/setters not shown 

	@Override
	public String toString() {
        	StringBuilder bld = new StringBuilder("[");		
		bld.append(Joiner.on(",").join(this.paths)).append("]");		
		return String.format("{revision:%s,author:%s,date:%s,paths:%s,msg:%s}", this.revision,this.author,this.date,bld.toString(),this.msg);
	}
}


Listing 3, LogEntry class

Path.java
package com.octodecillion.svn;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

/**
 * @author j.betancourt
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Path {
	@XmlValue
	String value;
	@XmlAttribute(name = "text-mods")
	String textmods;
	@XmlAttribute(name="kind")
	String kind;
	
	@XmlAttribute(name="action")
	String action;
	@XmlAttribute(name="prop-mods")
	String propmods;

	@Override
	public String toString() {
	   return String.format("{value:%s,kind:%s,action:%s,textmods:%s,propmods:%s}", 
             value.replaceAll("\\n+",""),kind,action,textmods,propmods);
	}
}


Listing 4, Path class

A JUnit 4 test

package com.octodecillion.svn;

import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;

/**
 * @author j.betancourt
 */
public class UnMarshallTest {

	@Test
	public final void test() throws Exception {
		Log theLog = new Unmarshal().path("src/test/resources/log.xml");
		String actual = toSingleLine(theLog.toString());
		String expected1 = "{revision:20950,author:jbetancourt,date:2014-11-10T20:12:11.910891Z,paths:[{value:/2014/Acme/branches/rabbit-trap/www/images/beep.png,kind:file,action:D,textmods:false,propmods:false}],msg:initialcommit}{revision:20948,author:jbetancourt,date:2014-11-10T19:55:58.629641Z,paths:[{value:/2014/Acme/branches/rabbit-trap/www/images/desert.png,kind:file,action:D,textmods:false,propmods:false}],msg:changedicontint}{revision:20942,author:jbetancourt,date:2014-11-10T15:30:08.770266Z,paths:[{value:/2014/Acme/branches/rabbit-trap/www/scripts/acme/traps/rocket.js,kind:file,action:M,textmods:true,propmods:false},{value:/2014/Acme/branches/rabbit-trap/www/scripts/acme/traps/sled.js,kind:file,action:M,textmods:true,propmods:false}],msg:Added'usestrict'.}{revision:20941,author:rsmith,date:2014-11-10T15:20:41.707766Z,paths:[{value:/2014/Acme/branches/rabbit-trap/www/ads/umbrella/promo.html,kind:file,action:M,textmods:true,propmods:false},{value:/2014/Acme/branches/rabbit-trap/www/ads/images/umbrella.jpg,kind:file,action:A,textmods:true,propmods:true}],msg:promotionMerge}";
		String expected = toSingleLine(expected1);
		Assert.assertEquals("Created wrong object structure", expected, actual);
	}

	/**  */
	String toSingleLine(String s) {
		String s1 = s.replaceAll("\\n+", "");
		return s1.replaceAll("\\s+", "");
	}

}

Listing 5, JUnit test

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.octodecillion</groupId>
  <artifactId>svnunmarshal</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SvnUnMarshal</name>
  <description>Example of how to use JAXB to unmarshal svn log</description>
  <!-- 
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
        </plugin>       
    </plugins>
  </build>
   -->
  <dependencies>
    <dependency>
        <groupId>org.tmatesoft.svnkit</groupId>
        <artifactId>svnkit</artifactId>
        <version>1.8.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>xmlunit</groupId>
        <artifactId>xmlunit</artifactId>
        <version>1.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.3</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>1.13</version>
        <scope>test</scope>
    </dependency>
  </dependencies>
</project>


Listing 6, Maven POM

Further reading

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Run svn command from Java using Commons exec

In a project I will need to marshal a Subversion log into Java. First step was getting Java to talk to a subversion repository. There are many options for doing this. Two of these are using a SVN API like SVNKit or invoking the command line svn executable.

Lets say you decide on the second option, invoking the svn binary, how would you do it? Easiest way is to use the Apache Commons Exec library. In listing below I use Exec and the Guava API in a Java 1.7 source level. Of course, this is not a ‘reusable’ solution, for example, the arguments to the two implemented commands are fixed. But, that is all I need right now.

Example use: new Command().log(“some/repo/somewhere”);

Expand.java

package com.octodecillion.svn;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;

import com.google.common.base.Preconditions;
import com.google.common.io.Resources;

/**
 * Invoke SVN commands from Java.
 * Just log and list for now.
 * <p>
 * Requires the command line SVN executable.
 * These are specified in svn.properties file in class path.
 *
 * Note: Thread safety has not been tested.
 *
 * @author j.betancourt
 *
 */
public class Command {

    /**
     * Constructor.
     * @throws IOException
     */
    public Command() throws IOException {
        try(BufferedReader is = 
             (Resources.asCharSource(Resources.getResource("svn.properties"),
			Charset.defaultCharset())).openBufferedStream()
            ){

            Properties props = new Properties();
            props.load(is);
            commandLineClientLocation = props.getProperty("commandLineClientLocation",
				DEFAULT_SVN_PROGRAM_LOCATION);
            commandLineClientFileName = props.getProperty("commandLineClientFileName", 
				DEFAULT_SVN_EXE);
        }
    }

    /**
     * @see http://svnbook.red-bean.com/en/1.4/svn.ref.svn.c.log.html
     *
     * @param url
     * @return
     * @throws IOException
     * @throws ExecuteException
     * @since Nov 18, 2014
     */
    public String log(String url) throws ExecuteException, IOException {
        Preconditions.checkNotNull(url, "url param is null");
        CommandLine cmdLine = createCmdLine()
                .addArgument("log")
                .addArgument("--stop-on-copy")
                .addArgument("--verbose")
                .addArgument("--xml")
                .addArgument(url);

        return executeCommand(cmdLine);
    }

    /**
     * @see http://svnbook.red-bean.com/en/1.4/svn.ref.svn.c.list.html
     * @param baseURL
     * @return
     * @throws IOException
     * @throws ExecuteException
     * @since Nov 19, 2014
     */
    public String list(String url) throws ExecuteException,IOException {
        Preconditions.checkNotNull(url, "url param is null");
        CommandLine cmdLine = createCmdLine()
                .addArgument("list")
                .addArgument("--xml")
                .addArgument(url);
        return executeCommand(cmdLine);
    }

    private CommandLine createCmdLine() {
        return CommandLine.parse(commandLineClientFileName);
    }

    /**
     * Execute command line at working directory.
     * @param cmdLine
     * @return String that captured the error and standard output streams
     * @throws ExecuteException
     * @throws IOException
     * @since 2014-11-20
     */
    private String executeCommand(CommandLine cmdLine) 
		throws ExecuteException,IOException {
        DefaultExecutor exec = new DefaultExecutor();
        exec.setWorkingDirectory(new File(commandLineClientLocation));

        String str ="";
        try(ByteArrayOutputStream outputStream = 
              new ByteArrayOutputStream()){

            exec.setStreamHandler(new PumpStreamHandler(outputStream));
            exec.execute(cmdLine);
            str =  outputStream.toString();
        }

        return str;
    }

    private static final String DEFAULT_SVN_EXE = "svn.exe";
    private static final String DEFAULT_SVN_PROGRAM_LOCATION = 
		"\\Program Files\\CollabNet\\Subversion Client";

    private String commandLineClientLocation = DEFAULT_SVN_PROGRAM_LOCATION;
    private String commandLineClientFileName = DEFAULT_SVN_EXE;
}

 

Further reading

  • Guava
  • Apache Commons Exec
  • SVNkit
  • Calling SVN commands from a java program
  • Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    FORTH language processor on comet

    The Philae spacecraft that landed on Comet 67P/Churyumov–Gerasimenko (67P) had an experiment on board that was driven by a FORTH language processor, the Intersil HS-RTX2010RH.

    The experiment/hardware was the The Surface Electric Sounding and Acoustic Monitoring Experiment (SESAME).

    What are they using for newer space platforms? ARM, INTEL, …?

    Historical minutia
    Forth gave rise to this bumper sticker: FORTH ♡ IF HONK THEN

    Updates
    November 26, 2014: Turns out the RTX2010 is used extensively on this mission. It has ten of them and one pair controlled the flight.

    Further reading

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    Use dojo’s promise.all in non-failure mode

    The “all” function “Takes multiple promises and returns a new promise that is fulfilled when all promises have been resolved or one has been rejected.” This makes a lot of sense. If you are invoking multiple asynchronous services in parallel and want to wait for all of them before continuing with a “reactive” function, you probably want to not run this function if one of the services fails.

    In some scenarios you do want all the service invocations to complete and then deal with the results whether success or failure. Thus, it would be nice to have a version of ‘all’ that did this.

    The now deprecated DeferredList may have supported this. See this reference.

    Some() function implementation
    Below in listing one I created a function ‘some’ that transforms the input to ‘all’ to accomplish this. This simply takes each promise argument (in array or object) and chains an always(…) Thenable. This Thenable promise link takes a handler that will be invoked whether the prior promise is resolved or rejected. It acts like a ‘finally’ in a try .. catch .. finally.

    A better approach would have been to create an alternative to the ‘all’ function that did this.

    /**
     * Takes an object or array of promises and creates a new array of 
     * promises that do not fail.
     * <p>
     * The Dojo/All function is fulfilled when all promises 
     * have been resolved or when one has been rejected.
     * In certain scenarios even if some promies
     * @author J. Betancourt 
     * @since 2014/11/04
     * @param objectOrArray  Object|Array?
     * @returns {Array} of promises.
     */
    ;function some(objectOrArray) {
        "use strict";
        
        var promises = [];
        require([ "dojo/_base/array", "dojo/Deferred", 'dojo/when' ],
          function(array, Deferred, when) {
            var origPromises = [];
            if (objectOrArray instanceof Array) {
                origPromises = objectOrArray;
            } else {
                for ( var key in objectOrArray) {
                    if (Object.hasOwnProperty.call(objectOrArray, key)) {
                        origPromises.push(objectOrArray[key]);
                    }
                }
            }
            // create array with each item a promise chain.
            array.forEach(origPromises, function(item){
               promises.push(when(item).always(function(res){return res;}));
            });
        });
        
        return promises;  // array of dojo/promise/Promise
        
    } // end some(objectOrArray) function
    

    Listing 1

    Example
    Below in Listing 2, this function is used. Available on jsFiddler.

    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"  
        data-dojo-config="async:false"></script>
    <script src="scripts/some.js"></script>
    
    <script>
        require([ "dojo/Deferred", "dojo/promise/all",
    	 "dojo/dom", "dojo/on","dojo/domReady!" ], 
            function(Deferred, all, dom, on) {
            "use strict";    
            
            function show(msg){
                dom.byId('output').innerHTML += msg + '<br/>';
            }
            
            /**
             * Use setTimeout to simulate an async process.
             * @param {string} name the name of this async invoke
             * @param {boolean} fail if true fail the promise
             * @returns {Promise} dojo/promies/Promise
             */
            function async(name, fail){
                var deferred = new Deferred();
                setTimeout(function(){
                    if(fail){
                        deferred.reject(name + "Failed!");
                    }else{
                        deferred.resolve(name);
                    }
                },1000);
                
                return deferred.promise;
            }
            
            /**
             * On click of 'StartButton' button 
    		 * wait for an array of promises. 
             */
            on(dom.byId("startButton"), "click", function() {
                var multiples = [async('V0'),
    			 async('V1',true),async('V2')];
    			 
                var p = all(some(multiples))
                .then(function(result){
                    show(result);
                    console.log(result);
                },
                function(err){
                    // this should not be reached.
                    show(err);
                    console.log(err);
                });
            });
        });
    </script>
    </head>
    <body>
        <button type="button" id="startButton">Start</button><p/>
        <div id="output" style="border: 1px black solid;width:20em;margin-bottom:20px;">Ready<br/></div>
    </body>
    </html>
    

    Listing 2

    Further reading

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    JavaScript Chaining Promises to Promises

    When you create a Promise you can chain asynchronous behavior by using the then(success,fail) method. The success and fail are callback functions invoked appropriately when the promise is resolved or rejected. Note that since ‘then(…)’ returns a promise, this technique is called promise chaining: p.then(…).then(….).

    What I discuss in this post are two subjects
    1. How to ‘stitch’ multiple promises together?
    2. How to ‘splice’ in a value into a promise chain?
    3. How to chain using a promise object as an argument?

    What if in complex application you already have promises objects and want to create promise chains linking them to each other? How would you do this with ‘then’ methods? Amazingly, in most examples of using Promises on the web this simple scenario is never presented directly. Granted it doesn’t *seem* to be relevant.

    Here is the source of a simple demo based on one of the Dojo Deferred examples. It is ‘normal’ promise chaining. I’m using this as the basis for the investigation presented here. The output displayed on page should be: completed with value: 75

    The actual chaining ‘stitching’ is setupTrans().then(function(x){return action()}).then(outputValue);

    <!DOCTYPE html>
    <html >
    <head>
    
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"  data-dojo-config="async:false"></script>
    	
    <script>
    require(["dojo/when", "dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],function(when, Deferred, dom, on){
    	  var balance = 100;
    
    	  function asyncProcess(x){
    	    var deferred = new Deferred();
    	
    	    setTimeout(function(){
    	      if(x === 'plus' || x === 'minus'){
    	      	 x === 'plus' ? (balance += 25) : (balance -= 25);
    	      } 
    	      deferred.resolve(balance);
    	    }, 1000);
    	
    	    return deferred.promise;
    	  }
    
    	  function setupTrans(){
    	  	return asyncProcess('z');
    	  }
    
    	  function debitAccount(){
    		return asyncProcess('minus');
    	  }
    	
    	  function creditAccount(){
    		return asyncProcess('plus');
    	  }
    
    	  function outputValue(value){
    	    dom.byId("output").innerHTML += "<br/>completed with value: " + value;
    	  }
    	  
    	  on(dom.byId("startButton"), "click", function(){
    	  	var debit = true;
    	  	var action = debit ? debitAccount : creditAccount;
    	  	
    	  	setupTrans()
    	  	.then(function(x){return action()})
    	  	.then(outputValue);
    	  	
    	  });
    });
    </script>
    </head>
    <body class="claro">
        <h1>Output:</h1>
    	<div id="output"></div><p />
    	<button type="button" id="startButton">Start</button>
    </body>
    </html>
    

    What is ugly is the inline function. Luckily I found the “Flattening Promise Chains” article where the author shows how to get rid of the function and simplify. However that article didn’t answer my question, how can you simply and directly chain promise objects. What the article showed is that you have to use the correct structure or design. Well, of course, but …


    Answer
    I thought this would be difficult. The Dojo documentation page never mentions that instead of the first ‘then’ argument being onFulfilled handler function it could be an actual promise object. But this is mentioned in specs such as the Promises/A+ specification. Or does it? The ‘Promise resolution procedure’ seems to mix compile time view with runtime, or I’m reading it incorrectly. Q documentation is very good on this. Will have to research further.

    To directly chain separate promise chains the above code sample is modified as follows by just using an existing promise as the argument to another chains then(…) method. However, this can only be done using a promise returning function.

      on(dom.byId("startButton"), "click", function(){
      	var debit = true;
      	var action = debit ? debitAccount : creditAccount;
      	
      	setupTrans()
      	.then(action)
      	.then(outputValue);
      	
      });
    

    Or, to inline the decision:

      on(dom.byId("startButton"), "click", function(){
      	var debit = true;
      	
      	setupTrans()
      	.then(debit ? debitAccount : creditAccount)
      	.then(outputValue);
      	
      });
    


    Chaining using actual promise objects inside the then(f,r) is made easy using the approach shown next, using “scalars”.

    Chaining values into promise chains
    While looking at this subject I thought about a situation where we want to directly insert a known value into the chain so that the next ‘link’ will use this as the prior handler’s return value.

    We have a chain, and I want to put the great Answer in there. This won’t work: var p1 = promiseReturningFunction().then(…).then(’42′).then(….).

    Sure you can wrap ’42′ in an inline function then(function(){return ’42′}), but that is yucky. How about a function that does that:

         /** return a function that returns the value */
         function now(value){
        	  return function(){return value;};
          }
    

    Now you can do: var p1 = async().then(…).then(now(’42′)).then(….);

    Here is the prior source code with the changes. This is just some of the script changes. The output on screen is:

    completed with value: 75
    completed with value: Goodbye

          /* ...  */
    
          function outputValue(value){
            dom.byId("output").innerHTML += "<br/>completed with value: " + value;
          }
          
          function now(valueOrPromise){
        	  return function(){return when(value);};
          }
          
          on(dom.byId("startButton"), "click", function(){
            var debit = true;
            var action = debit ? debitAccount : creditAccount;
            
            var promise1 = setupTrans()
            .then(debit ? debitAccount : creditAccount)
            .then(outputValue)
            .then(function(){
            	console.log("now do another promise chain...");
            });
            
            promise1.then((now('Goodbye'))).then(outputValue);
            
          });
    


    Using promise object directly in then(f,r) by using now(x)
    With the Promises implementation in Dojo, you can’t use promiseObject.then(anotherPromiseObject). And you can’t use promiseObject.then(when(anotherPromiseObject)). At least, I haven’t figured how to do it.

    With the now(x) function shown above, this is easy. The code below will output:

    completed with value: 75
    completed with value: 100
    completed with value: undefined
    completed with value: finished both chains

         /** return function that returns the value 
             or apply Dojo's when(...) to return a Promise 
          */
         function now(valueOrPromise){
        	  return function(){return when(value);};
          }
          
          on(dom.byId("startButton"), "click", function(){
            var debit = true;
            var action = debit ? debitAccount : creditAccount;
            
            var promise1 = setupTrans()
            .then(action)
            .then(outputValue)
            .then(function(){
            	console.log("now do another promise chain...");
            });
            
            debit = false;
            action = debit ? debitAccount : creditAccount;
            var promise2 = setupTrans()
            .then(action)
            .then(outputValue);
            
            promise1
            .then(now(promise2)).then(outputValue)
            .then(now("finished both chains"))
            .then(outputValue);
            
          });
    });
    

    Groovy language has Promises too
    GPars brings Promises to the Groovy language.

    Further reading

    1. Flattening Promise Chains
    2. A Deeper Dive Into JavaScript Promises
    3. How-to Compose Node.js Promises with Q
    4. Promise Anti-patterns
    5. dojo/when
    6. q
    7. General Promise Resources
    8. Promises/A+
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    Force failure of JavaScript promise in Dojo

    For development and unit testing you may need to force a promise chain to fail in various ways.

    In listing 1 below is shown a simple XHR request. Now to make sure our promise chain error handling is working correctly we want the Ajax call to fail.

    One way of achieving this to just change the url of the XHR destination. This is fine here, but is not a generic approach since not every async function or service is based on a URL or even single value.

    Another approach is shown in listing 2. Here we cancel the promise. In Dojo’s promise implementation we can cancel(reason,flag) a promise giving a reason and a flag, and even invoke a cancel handler if the promise was created with one. If this flag is true, a successful async call will result in a failure. Unfortunately, unless you type at superhuman speeds, this can’t be added at a debugger breakpoint and must be coded in temporarily.

    Listing 1

    /**
     * XHR call.
     * @returns promise
     */
    function _loadData(requestParams) {
        var promise;
        require([ "dojo/request"], function(request) {
            promise = request.post("someurl", {
                data : requestParams,
                handleAs : "json",
                preventCache : true
            })
        });
        
        return promise;
    }
    

    Listing 2

    function _loadData(requestParams) {
        var promise;
        require([ "dojo/request"], function(request) {
            promise = request.post("someurl", {
                data : requestParams,
                handleAs : "json",
                preventCache : true
            })
        });
        
        promise.cancel("force error",true);
        return promise;
    }    
    

    There are XUnit type testing frameworks for JavaScript that may be able to do this failure testing better. I have not looked into this yet.

    Further reading

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    A new kind of rowing stroke

    A few months ago I got an idea for a different kind of sport Rowing stroke. I don’t remember the exact train of thought, but it had to do with swimming. Anyway, …

    The physics of rowing and dynamics of the water are very complex. My idea is that you want to move the water past the boat faster, reduce the amount of oars in the water, and increase the glide of the boat. Thus, instead of all the rowers rowing at the same pace, the rowing is staggered.

    At the beginning of the race, the traditional rowing stroke is used. We want the maximum power to accelerate the boat quickly.

    In the middle of the race, a staggered stroke is used. Here the front rower begins the stroke and at the end of the “drive’ phase of the stroke, while the blade is still in the water, the second rower begins their stroke. This is repeated for each rower in turn.

    Toward the end of the race, the traditional synchronized power stroke is resumed.

    In essence the effect, if done smoothly, is the strokes become just one long stroke. As if only one person “drove” a very long stroke from bow to stern. Like birds in flight, the water becomes part of solution, a slipstream.

    Advantages? I’m only guessing here.
    - Rowers get more rest, and more energy available toward end.
    - The glide of the boat is increased.
    - Water is moved faster, velocity of boat.
    - Less oars in the water so less friction.

    – Josef

    Further reading

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    JavaScript Promise example, reverse geocode location

    In prior post we wrapped the Google Map API in a Promise. Now we wrap navigator.geolocation.getCurrentPosition in a similar promise. This allows us to to neatly chain these two to get the browser’s current address.

    The example is using Dojo’s Promise implementation, but the approach is applicable to other Javascript Promise libraries or native support.

    In listing 1 below I reproduce the former getGeocode function.

    Listing 1, Get geocode using Google service

    function geoCode(request) {
        var deferred;
        require([ 'dojo/Deferred' ], function(Deferred) {
            deferred = new Deferred();        
            new google.maps.Geocoder().geocode(request, 
              function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    deferred.resolve(results);
                } else {
                    deferred.reject(status);
                }
            });
            
        });
        
        return deferred.promise;
        
    }
    
    

    In listing 2 below we have a similar function

    Listing 2, get browser location.

    function getLocation(){
        var deferred;
        require([ 'dojo/Deferred' ], function(Deferred) {
            deferred = new Deferred();
            navigator.geolocation.getCurrentPosition(
                function navSuccess(position){
                    deferred.resolve(position);
                },function navFail(){
                    deferred.reject(posError);
                }
            );
        });
    
        return deferred.promise;
    }
    

    Hmmm. The code is very similar. It seems that these two functions could be done using a Promise wrapper function, like toPromise(doFunction, onSuccess, onFailure). Is there such a beast?

    Now we can use them together, as shown in listing 3. Here instead of a simple promise chaining we use the technique presented in “Break promise chain and call a function based on the step in the chain where it is broken (rejected)” to “catch” the correct error and not have it bubble up the ‘then’ chain.

    Listing 3, get browser location, then reverse geocode

    function findCurrentLocation(){
        var request;
    
        if (navigator.geolocation) {
            getNavLocation().then(
                function navSuccess(position){
                    request = {
                        'location' : 
                          new google.maps.LatLng(
                            position.coords.latitude, 
                            position.coords.longitude)
                    };
    
                    return getGeoCode(request).
                       otherwise(function geoCodeError(status){
                         throw new Error(status);
                       }
                    );
                }, function navFailed(err){
                    console.log('navLocation() failed: " + err); 
                    throw new Error(err);       
                })
                .then(function geoCodeSuccess(results){
                        console.log('navLocation() success: " + results);
                    } 
                );
        }
    }
    

    Above, .otherwise(onFailure) is just a shortcut for .then(null, onFailure)

    Of course, the code doesn’t have enough error handling and actual use of the data obtained.

    Further reading

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

    Wrap Google map geocoder.geocode in a Promise

    Futures and promises are a programming construct to make asynchronous programming easier to apply. They are provided in many JavaScript libraries and frameworks. Below we use Dojo’s implementation to wrap a non-promised based async operation, getting a Geocode from the Google Map API.

    The Google geocode service takes a request object (which contains address information) and a callback function that takes a results object and a status string. If status is ok, the results object contains the geocode information (and a lot of other useful information).

    In listing one the geoCode function wraps this in a Promise. Inside the function we create a deferred object, then invoke the above async geocode service, then we return the Promise interface of the Deferred object. Note, this is taken from a working application code, and that code works, this below, not tested.

    Listing 1.

    var geocoder = new google.maps.Geocoder();
     
    function geoCode(request) {
        var deferred;
        require([ 'dojo/Deferred' ], function(Deferred) {
            deferred = new Deferred();        
            geocoder.geocode(request, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    deferred.resolve(results);
                } else {
                    deferred.reject(status);
                }
            });
            
        });
        
        return deferred.promise;
        
    }
    
    

    You can use this as follows:

    var promise = getGeocode(request);
    

    Note that we don’t wait for the async operation to complete, or more correctly, invoke our callback function. The invoker of our geoCode function just gets a Promise object. So?

    The Promise object is something that may get a value when it is fulfilled or it may get rejected. You find this out by reacting to those states. How? By using callbacks. So we are right back to where we started, after all the original geocode function also used callbacks. Ah, but here is the difference. With Promises, you are using something with more features and power. The Promise framework allows you to work with async code in a more frameworky way.

    One of the nice features of Promises is that they can be “then’able”. “then” functions return a Promise, so they can be chained together: p.then(….).then(….).then(…..); Within each ‘then’, you supply a left and a right callback function, the left is for the resolved, the right is for the rejected state. Example:

    var p = getGeocode(request)
    p.then(successFunction,failureFunction);
    

    You could also inline everything:

    getGeocode(request).then(
        function(results){
           var x = do_something_with_result(results);
           return x;
        },
        function(status){
           do_something_with_status(results);
           throw "failed"
        }
    });
    

    These left and right are not chains themselves, there is one chain, but the left or right could be invoked depending on whether the preceding “then” resolved or rejected the promise. Of course, there are more details and each Promise implementation adds their own terminology and features. For example, Dojo has an always function that accepts a callback to invoke no matter what the previous ‘then’ did with the promise.

    Apparently moves are afoot to make ‘futures’ part of the core JavaScript standard. These may be following the Promises/A+ specification.

    A future blog post will provide a very easy to understand intro to Promises. There doesn’t seem to be a Promise for dummies on the web.

    Further reading

    YouTube

    JavaScript Promises: Thinking Sync in an Async World

    Redemption from Callback Hell

    Times Developer Network – Promises, Promises

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.