User:AnatolV/Helper Functions: Difference between revisions
Content added Content deleted
(adding R HFs) |
(adding funcs, editing) |
||
Line 1: | Line 1: | ||
This page presents only helper functions that are generic enough. They were used in one or more contributions on RC and will be used again and again.<br> |
|||
Note: Many samples of using these functions can be found here on RC. |
|||
=R Helper Functions= |
=R Helper Functions= |
||
One of the R's great powers is its unlimited number of great and good packages, virtually thousands of them. For any applications big or small you can find a package.<br> |
|||
E.g., in the case of Voronoi diagram there are many of packages: deldir, alphahull, dismo, ggplot, ggplot2, tripack, CGAL, etc. Not to mention all linked packages. Do you need random colors? Again, find a few packages more...<br> |
|||
So, sometimes you can save time and a hard drive space avoiding downloading and installing packages, and using a few helper functions instead. |
|||
;Note: |
;Note: |
||
* All plotting helper functions are using matrix mat or 2 vectors X,Y from the dump file created by plotmat(). |
* All plotting helper functions are using matrix mat or 2 vectors X,Y from the dump file created by plotmat(). |
||
Line 7: | Line 12: | ||
{{Works with|R|3.3.1 and above}} |
{{Works with|R|3.3.1 and above}} |
||
<lang r> |
<lang r> |
||
# plotmat(): Simple plotting using matrix mat (filled with 0/1). v. 8/31/16 |
## HFR#1 plotmat(): Simple plotting using matrix mat (filled with 0/1). v. 8/31/16 |
||
# Where: mat - matrix; fn - file name; clr - color; ttl - plot title; |
# Where: mat - matrix; fn - file name; clr - color; ttl - plot title; |
||
# dflg - writing dump file flag (0-no/1-yes): psz - picture size. |
# dflg - writing dump file flag (0-no/1-yes): psz - picture size. |
||
Line 27: | Line 32: | ||
} |
} |
||
# plotv2(): Simple plotting using 2 vectors (dumped into ".dmp" file). |
## HFR#2 plotv2(): Simple plotting using 2 vectors (dumped into ".dmp" file). |
||
# Where: fn - file name; clr - color; ttl - plot title; psz - picture size. |
# Where: fn - file name; clr - color; ttl - plot title; psz - picture size. |
||
# v. 8/31/16 |
# v. 8/31/16 |
||
Line 45: | Line 50: | ||
dev.off(); graphics.off(); |
dev.off(); graphics.off(); |
||
cat(" *** END:", date(), "\n"); |
cat(" *** END:", date(), "\n"); |
||
} |
|||
## HFR#3 randHclr(): Return random hex color. |
|||
randHclr <- function() { |
|||
m=255; r=g=b=0; |
|||
r <- sample(0:m, 1, replace=TRUE); |
|||
g <- sample(0:m, 1, replace=TRUE); |
|||
b <- sample(0:m, 1, replace=TRUE); |
|||
return(rgb(r,g,b,maxColorValue=m)); |
|||
} |
|||
</lang> |
|||
=JavaScript Helper Functions= |
|||
JavaScript has many standard libraries becoming embedded objects. e.g., Math, Date, Array, |
|||
RegExp, etc. But also there are many custom "monster" like (in size and documentation) libraries, e.g., D3.js, |
|||
Node.js, Riot.js, jQuery, Dojo, YUI, etc. <br> |
|||
The problem with these libraries is that they are forcing you to learn, actually, "new" language. In addition, there is no guarantee, |
|||
that they would be stable and maintained well.<br> |
|||
The good alternative is always to use just 2-3 small helper functions if it is possible for your task. |
|||
<lang javascript> |
|||
// HFJS#1 Like in PARI/GP: return random number 0..max-1. |
|||
function randgp(max) {return Math.floor(Math.random()*max)} |
|||
// HFJS#2 Return random hex color. |
|||
function randhclr() { |
|||
return "#"+ |
|||
("00"+randgp(256).toString(16)).slice(-2)+ |
|||
("00"+randgp(256).toString(16)).slice(-2)+ |
|||
("00"+randgp(256).toString(16)).slice(-2) |
|||
} |
} |
||
</lang> |
</lang> |
||
=PARI/GP Helper Functions= |
|||
<<coming soon>> |