Jump to content

User:AnatolV/Helper Functions: Difference between revisions

adding funcs, editing
(adding R HFs)
 
(adding funcs, editing)
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=
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:
* All plotting helper functions are using matrix mat or 2 vectors X,Y from the dump file created by plotmat().
Line 7 ⟶ 12:
{{Works with|R|3.3.1 and above}}
<lang r>
## 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;
# dflg - writing dump file flag (0-no/1-yes): psz - picture size.
Line 27 ⟶ 32:
}
 
## HFR#2 plotv2(): Simple plotting using 2 vectors (dumped into ".dmp" file).
# Where: fn - file name; clr - color; ttl - plot title; psz - picture size.
# v. 8/31/16
Line 45 ⟶ 50:
dev.off(); graphics.off();
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>
=PARI/GP Helper Functions=
<<coming soon>>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.