Categories
discuss

use velocity, meteor, and travis ci

I made a basic application in Meteor, and used velocity with mocha. I tried to find ways to build on travis ci. Travis suggested using the following for a .travis.yml

language: node_js
node_js:
  - "0.10"
before_install:
  - "curl -L http://git.io/3l-rRA | /bin/sh"
services:
  - mongodb
env:
  - LAIKA_OPTIONS="-t 5000"

Unfortunately, this seems to be based on the deprecated laika framework.

Is there any way to use the velocity framework on a meteor app and have it build on travis ci?

Answer

I had the same problem while trying to get the cucumber tests up and running on Travis.

Turns out it is as simple as installing Meteor through the recommended way on their official site. Here is the configuration I use on my project:

sudo: required
language: node_js
node_js: 
  - "0.12"
install: 
  - "curl https://install.meteor.com | /bin/sh"
  - "meteor update"
script: 
  - "meteor --test --once"
env:
  - "SELENIUM_BROWSER=phantomjs"
Categories
discuss

Prevent browserify from including module’s dependencies

I’d like to use my NodeJS module in the browser – so I’m using browserify to process it.

Now, how can I stop browserify from including the module’s dependencies in the bundle file? In this case the dependency is lodash and I’ll be loading it separately in the index.html.

Here’s what I’ve got so far:

index.html

<script src="lodash.js"></script>
<script src="my-module.js"></script>

index.js

var _ = require('lodash');

_.each([0, 1, 2], function(item) {
    console.log(item);
});

gulp.js

var browserify = require('browserify'),
  source = require('vinyl-source-stream');

gulp.task('browserify', function() {
  return browserify()
    .require('./index.js', {
      expose: 'my-module'
    })
    .bundle()
    .pipe(source('my-module.js'))
    .pipe(gulp.dest('./'));
});

Answer

browserify-shim offers the option of setting up globals.

Here are the changes I’ve made to my code.

package.json

{
  "browserify-shim": {
    "lodash": "global:_"
  },
  "browserify": {
    "transform": ["browserify-shim"]
  }
}

gulp.js

gulp.task('browserify', function() {
  return browserify('./index.js')
    .require('./index.js', {
      expose: 'my-module'
    })
    .transform('browserify-shim', {
      global: true
    })
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('./'));
});
Categories
discuss

Java8 streams sequential and parallel execution produce different results?

Running the following stream example in Java8:

    System.out.println(Stream
        .of("a", "b", "c", "d", "e", "f")
        .reduce("", (s1, s2) -> s1 + "/" + s2)
    );

yields:

/a/b/c/d/e/f

Which is – of course – no surprise. Due to http://docs.oracle.com/javase/8/docs/api/index.html?overview-summary.html it shouldn’t matter whether the stream is executed sequentially or parallel:

Except for operations identified as explicitly nondeterministic, such as findAny(), whether a stream executes sequentially or in parallel should not change the result of the computation.

AFAIK reduce() is deterministic and (s1, s2) -> s1 + "/" + s2 is associative, so that adding parallel() should yield the same result:

    System.out.println(Stream
            .of("a", "b", "c", "d", "e", "f")
            .parallel()
            .reduce("", (s1, s2) -> s1 + "/" + s2)
    );

However the result on my machine is:

/a//b//c//d//e//f

What’s wrong here?

BTW: using (the preferred) .collect(Collectors.joining("/")) instead of reduce(...) yields the same result a/b/c/d/e/f for sequential and parallel execution.

JVM details:

java.specification.version: 1.8
java.version: 1.8.0_31
java.vm.version: 25.31-b07
java.runtime.version: 1.8.0_31-b13

Answer

From reduce’s documentation:

The identity value must be an identity for the accumulator function. This means that for all t, accumulator.apply(identity, t) is equal to t.

Which is not true in your case – “” and “a” creates “/a”.

I have extracted the accumulator function and added a printout to show what happens:

BinaryOperator<String> accumulator = (s1, s2) -> {
    System.out.println("joining "" + s1 + "" and "" + s2 + """);
    return s1 + "/" + s2;
};
System.out.println(Stream
                .of("a", "b", "c", "d", "e", "f")
                .parallel()
                .reduce("", accumulator)
);

This is example output (it differs between runs):

joining "" and "d"
joining "" and "f"
joining "" and "b"
joining "" and "a"
joining "" and "c"
joining "" and "e"
joining "/b" and "/c"
joining "/e" and "/f"
joining "/a" and "/b//c"
joining "/d" and "/e//f"
joining "/a//b//c" and "/d//e//f"
/a//b//c//d//e//f

You can add an if statement to your function to handle empty string separately:

System.out.println(Stream
        .of("a", "b", "c", "d", "e", "f")
        .parallel()
        .reduce((s1, s2) -> s1.isEmpty()? s2 : s1 + "/" + s2)
);

As Marko Topolnik noticed, checking s2 is not required as accumulator doesn’t have to be commutative function.

Categories
discuss

Dagger2 – null instead of injected object

For making things simple, suppose I want to inject EmailValidator from apache validators into my activity:

public class MainActivity extends FragmentActivity {

    @Inject
    EmailValidator emailValidator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

I have a MainModule class:

@Module
public class MainModule {

    @Provides
    public EmailValidator providesEmailValidator() {
        return EmailValidator.getInstance();
    }
}

and MainComponent interface:

@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {

    EmailValidator getEmailValidator();
}

When trying to use my validator in activity, I’m getting a nullpointer exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.apache.commons.validator.routines.EmailValidator.isValid(java.lang.String)' on a null object reference

Obviously I’m missing something. I know that dagger creates component implementation for me. Should I use it? How?

If I do the following in my onCreate method:

        emailValidator = Dagger_MainComponent.create().getEmailValidator();

then everything works fine.

But I want to be able to use @Inject annotation anywhere (probably on setter/constructor instead of a field) instead.

What am I missing?

I did something similar with dagger1 and it worked. Of course I needed to call ObjecGraph.inject(this) in activity. What’s the dagger2 equivalent?

EDIT:

Ok, so I’ve found a solution. If anyone will ever have such a problem, there are some snippets:

1) I’ve created an application class:

public class EmailSenderApplication extends Application {

    private MainComponent component;

    @Override
    public void onCreate() {
        super.onCreate();

        component = Dagger_MainComponent
                .create();

        component.inject(this);
    }

    public MainComponent component() {
        return component;
    }
}

2) In AndroidManifest.xml:

<application
        android:name=".EmailSenderApplication"
        ...

3) And finally, in the activity class where I want to inject some components those two ugly as hell lines:

component = ((EmailSenderApplication) getApplication()).component();
component.inject(this);

Answer

Looks like you need to build your component as in:

component = Dagger_ MainComponent.builder()
        .mainModule(new MainModule())
        .build();

Typically, you do this in the onCreate method of your Application.

One good resource that may help you is the example apps in the Dagger 2 repo.

I also found this PR helpful, from a suggested update to Jake Wharton’s u2020 sample app (from the main Dagger 2 Engineer). It gives a good overview of the changes you need to make when going from Dagger 1 to 2 and, apparently that’s what he points people to as well.

Categories
discuss

List of voice commands which trigger heart rate check

What is the list of all existing voice commands which create an intent with vnd.google.fitness.VIEW action and vnd.google.fitness.data_type/com.google.heart_rate.bpm mime type?

The documentation shows only two of them:

  1. “OK Google, what’s my heart rate?”
  2. “OK Google, what’s my bpm?”

but I’ve discovered more, for example:

  1. “OK Google, check my pulse.”

Answer

the documentation also states those are “Example Phrases”.
The reason for that it’s because such a list does not exist.

Voice commands on those type of platforms (Google Now, Siri) use quite more advanced algorithms to recognize what a person wants, and not just recognize the actual words. That is called “natural voice”.

That all means, that a “List of voice commands” does not exist as the platform is capable of adapt and interpret variations.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..