Skip to main content

Posts

A simple recipe to use grunt watch to setup simple autorun workflow while developing any type of project

It is great to have a tool that watches changes and reruns code (like tests or server code etc) as you are saving your files. This idea here of course works great with any Node project but hey, even if you are not coding in Node (or even in language other than JS), you can still use this idea, it's very generic and will work for just about any type of project if you just set this basic setup once. While there are many approaches to doing this depending on the project type (like webpack's watch which is super-powerful for web projects, nodemon  for server restart cases, chokidar etc), my current favorite for a generic  method that will work with any type of project is based on grunt-watch. So read on! I setup this simple repository with a simple readme guide on how to do this. https://github.com/binodpanta/node-examples/tree/master/gruntwatch I think it will save you a lot of time when working on NodeJS projects. Even if you don't use Grunt for your ac...

Using a Java stream (Java 8+) to convert List collection into primitive data type arrays

You need to convert an ArrayList<Integer> to int, similar case for others like Long > long. Prior to Java 8, you would have to try something like int[] myarray =   ll.toArray(new Integer[size]); But this will fail to compile! So it forces you to use Integer[] instead of int[]. However, you may have client code that expect int[] and not Integer[]. So the only other way would be to iterate over the list and convert to array the traditional way by adding element by element. Fine, but not elegant. I discovered that the stream based method above works even for primitive data types like int. So converting an array list of integers to an array is quite straightforward with a mapToInt () intermediate Intstream (or LongStream for long) as follows ArrayList<Integer>  ll  =  new  ArrayList<Integer>();       int[] myarray =  ll .stream().mapToInt( c  ->  c ).toArray(); The c-...

How to interact with pre-deployed Ethereum Smart contracts in the Mist browser

How to view/watch pre-deployed Smart contracts in Mist Check out this git repo https://github.com/binodpanta/ethereum-examples/tree/master/predeployed-contracts-mist Here I have described how you can load up pre-deployed Smart contracts in the Mist browser, if you have deployed them using Truffle or other method This is useful if you want to view your already deployed contracts. Sometimes you don't want to have to re-run contract code from Mist if you have already used truffle to deploy the contract

Generating JavaScript mapping functions with custom arguments

I recently learned this trick which I am sure is not new, but I sort of discovered on my own The issue is you want your mapping function to be customizable, but the standard javascript mapping function only takes one arg, the value out of the thing being mapped so the trick is to generate your mapping function with all your arguments in the closure, meaning you write a function that returns the mapping function. your function will take all your args // here 'band' is the arg that we want to use to generate a custom mapping function var mapFuncGenerator = function(band) {                         return function(rawdata) {                             var ts = rawdata.ts;                             var compositeValue = rawdata[band][4];            ...

Bash shell: Reading information from files and adding dynamically generated content to files

Simply put, using the bash for loop and other standard commands it can be a quite powerful method to write custom content to files/databases etc For example #!/bin/bash list=$(cat somefile | grep jar | sed 's/aaa/bbb/g') # add each of the paths as a system scope dependency to the pom counter=1 for OUTPUT in $list do         pathvar=/$OUTPUT         echo "          <dependency>             <groupId>x</groupId>             <artifactId>$counter</artifactId>             <version>1.0</version>             <systemPath>$pathvar</systemPath>             <scope>system</scope>          </dependency>         " >> pom.xml  ...

Using asyncawait library in NodeJS to simplify async code

Often you find yourself using async code and handling delayed response in JavaScript There are two well known approaches that are often used * Callbacks. This is the conventional way, and is frankly annoying. I won't say much more about these. * Promises. Of course they are all the rage right now. They are lovely. doSomething().then(doAnother()) etc. The only issue is that when processing promise code you have to deal with creating and returns functions and callback-like syntax still, and soon code starts to look busy again So, if you are in NodeJS, you have a few options to use async..await pattern. Here I use the asycnawait library. Other options are co, async etc. The idea is quite simple. You write a function inside async(). Within the function, write code as if it's synchronous, and use await() to resolve promises. Example           let yourFunction = function() {               var result = await(functionT...

Using m4 macro processor to process templates via shell scripts

I recently had to develop a bash script in order to automate the submission of a job The job submission script could be run like this someTextOutput | thatCommand So 'thatCommand' can be piped input directly. My issue was that 'someTextOutput' for me needed to be template replaced with user provided inputs. For example I wanted to do this % aCommand var1 var2 This should replace values of tokens in a template file with var1 and var2 and the resulting output should become 'someTextOutput' I discovered the m4 template processor tool, and here was my solution, or close to it. Cool! #!/bin/bash # some input handling code ... # now send over template processed output to the target command cat /tmp/templateFile.txt | m4 -Dvar1="$1" -Dvar2="$2" | thatCommand I can use this in so many applications, all without having to resort to other tools/scripts when I don't need to. Happy scripting!