User:AnatolV/Helper Functions: Difference between revisions
Content added Content deleted
(adding GP funcs) |
(updating preludes, adding 1 HFJS) |
||
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> |
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: |
|||
⚫ | |||
* Good helper function usually encapsulates typical set of actions for current tasks, supplies often used default values, interacting with other helper functions, etc. |
|||
⚫ | |||
=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> |
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> |
||
Line 9: | Line 11: | ||
* The file names used are without extension (which will be added as ".png", ".dmp" and ".dat" when needed). |
* The file names used are without extension (which will be added as ".png", ".dmp" and ".dat" when needed). |
||
* Requesting dump file is useful if the generating/plotting time is big. Having a dump file makes it easy and fast to repeat plotting with different colors, titles, etc. |
* Requesting dump file is useful if the generating/plotting time is big. Having a dump file makes it easy and fast to repeat plotting with different colors, titles, etc. |
||
* If number of generated dots is very big then plotting from a dump file could be very slow too. Actually, plotv2() shows almost "pure" plotting time. |
* If number of generated dots is very big then plotting from a dump file could be very slow too. Actually, plotv2() shows almost "pure" plotting time.<br><br> |
||
{{Works with|R|3.3.1 and above}} |
{{Works with|R|3.3.1 and above}} |
||
<lang r> |
<lang r> |
||
Line 67: | Line 70: | ||
The problem with these libraries is that they are forcing you to learn, actually, "new" language. In addition, there is no guarantee, |
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> |
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. |
The good alternative is always to use just 2-3 small helper functions if it is possible for your task.<br> |
||
The same I would suggest JavaScript users about "declarative JavaScript". |
|||
It reminds me OOP (Object Oriented Programming) hype! But OOP is dead already, and |
|||
now declarative JavaScript is hyped?? |
|||
Don't let you be foolish again. Remember: you need to learn and use "monster" libraries!?<br><br> |
|||
What "declarative JavaScript" is promising (and actually giving you by presented new functions, tags, etc.)?<br> |
|||
This is a fantastically sounding: "You are telling JavaScript what to do, but not programming actually". |
|||
Think! It is the same as good HFJS is doing! For example, using randhclr() and pmat01() is the same |
|||
as "telling JavaScript": "Use random hex color and plot matrix you have". It's just 1 statement: pmat01(mat,randhclr()).<br><br> |
|||
In other words, look at these new libraries as a set of good helper functions for your projects.<br> |
|||
So, all these new libraries are good for certain type of projects, especially if company |
|||
have created this particular library and using it for inner projects. The good such sample is Facebook JavaScript library React.js, which is having/supporting a lot of unique functions and tags. Think again: |
|||
do you need all of them? |
|||
<lang javascript> |
<lang javascript> |
||
// HFJS#1 Like in PARI/GP: return random number 0..max-1. |
// HFJS#1 Like in PARI/GP: return random number 0..max-1. |
||
Line 79: | Line 97: | ||
("00"+randgp(256).toString(16)).slice(-2) |
("00"+randgp(256).toString(16)).slice(-2) |
||
} |
} |
||
// HFJS#3: Plot any matrix mat (filled with 0,1) |
|||
function pmat01(mat, color) { |
|||
// DCLs |
|||
var cvs = document.getElementById('canvId'); |
|||
var ctx = cvs.getContext("2d"); |
|||
var w = cvs.width; var h = cvs.height; |
|||
var m = mat[0].length; var n = mat.length; |
|||
// Cleaning canvas and setting plotting color |
|||
ctx.fillStyle="white"; ctx.fillRect(0,0,w,h); |
|||
ctx.fillStyle=color; |
|||
// MAIN LOOP |
|||
for(var i=0; i<m; i++) { |
|||
for(var j=0; j<n; j++) { |
|||
if(mat[i][j]==1) { ctx.fillRect(i,j,1,1)}; |
|||
}//fend j |
|||
}//fend i |
|||
}//func end |
|||
</lang> |
</lang> |
||
=PARI/GP Helper Functions= |
=PARI/GP Helper Functions= |
||
PARI/GP plotting functions are good only for instant draft math function curves plotting. |
PARI/GP built-in plotting functions are good only for instant draft math function curves plotting. |
||
Not to mention bad color support and scaling.<br> |
Not to mention bad color support and scaling.<br> |
||
In addition, there are no built-in string manipulation functions, except of concatenation.<br> |
In addition, there are no built-in string manipulation functions, except of concatenation.<br> |