Skip to main content

Posts

Showing posts from July, 2017

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];                             return [ts, compositeValue];                         }                     }; // use three different mapping functions based on value of 'band'                     var graphAlpha = doc.data.ma

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         ((counter++)) done

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(functionThatReturnsPromise());               return result;

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!