Importance of good orientation of the specimen (quality criteria for endoscopist and pathologist).
The biopsy must give answers to the following questions:
| Method name | Syntax & description |
| String.reverse | <string>.reverse() |
Returns the reversed string. Thus "Svend",
becomes "dnevS". |
|
| String.trim | <string>.trim() |
Returns the same string as <string>,
where the whitespace has been trimmed from the front
and back. Whitespace is defined as anything that
matches the regex \s, in JavaScript,
usually defined as all Unicode whitespace.
" ws s " thus
becomes "ws s". (A shorter version
of the original script was suggested by Tobias
Hinnerup). |
|
| String.toInt | <string>.toInt() |
| This function allows you to quickly transform a string into an array of ints, much like an char array in C or similar languages. The method returns the int array, and does not transform the original string. | |
| Array.intArrayToString | <array>.intArrayToString() |
The purpose of this function, is to work together with the
String.toInt method, and let a developer quickly
transform a String into first an int array, and then back into
a String. The method works on the array it's called on, and returns
a new string, the array being worked on, doesn't change. The array
must further be all natural numbers (0 and up). |
|
| Array.compareArrays | <array1>.compareArrays(<array2>) |
Normal JavaScript doesn't compare arrays in a way that's sometimes
handy. Using the following [1,2] == [1,2],
will give you false, quite opposite
what you most likely intended. This method let's you compare arrays, based
on their contents. Also note, that this function handles nested arrays
just fine also. |
|
| Array.map | <array>.map(<function>) |
The map function is
an old
one. Mapping means, that
taking the function <function>, and applying
it to all elements of the array, and returning the resulting array.
The function passed, must return a value, and should
accept only one input argument. Consider the following
"[1,2,3,4].map(function(n){return n+1;})", where
each number gets added one. |
|
| Array.foldr | <array>.foldr(<function>,<value>) |
Yet another function from the world of functional programming.
This one I've lifted from SML, though I'm sure it also appears
other places. It's basically an accumulator function. It takes the
<function>, which should accept two arguments,
and a start <value>. On an array [1,2,3],
the function f, and the start value 0, would result in:
f(5(f(4(f(3,0)))). A summing function, could be written,
using the function function(n,m){return n+m;} and
the start value 0. The name comes from "fold right", meaning
that it "folds" from right to left. |
|
| Array.foldl | <array>.foldl(<function>,<value>) |
| Identical to the above function, except that here, it folds from left to right. | |
| Array.exists | <array>.exists(<value>) |
Will return true, if <value> exists in
<array>, and false, if it doesn't. Value can be anything of course,
since in JavaScript, you can compare everything. Just note, that
if the array contains arrays, you can't use exists, since
[1,2,3] == [1,2,3] actually returns false in
JavaScript (see compareArrays). Rewriting the
exists function, so that it receives a function
as input (which can then do eloborate stuff), or uses the
compareArrays function, is easy. |
|
| Array.filter | <array>.filter(<function>) |
filter will allow you to filter out
all elements of an array, that does not satify a condition.
the argument <function> should accept
one argument, and return a boolean value. Given the array
t = [1,2,-2,3,-4,4,5], executing
t.filter(function (n){return n>0;});, returns
an array of all the positive elements (that is, [1,2,3,4,5].) |
|
| Array.random | <array>.random() |
random returns a random element from the array. |
The code has knowingly been made redundant, so all functions can
stand-alone. Not only does this improve readability, it also makes
all methods stand-alone. For example, filter could
easily have been written using foldl:
Array.prototype.filter = function(fnc) {
function con(a,b) {
if (fnc(a)) b.push(a);
return b;
}
return this.foldl(con,[]);
}
But not only does it make the code less transparent, it also increases needless dependencies. And the savings was a whopping two lines of code. Not worth it.
None of the above functions do native currying. It's not something
JavaScript is capable of basically. But using the generic solutions given in
Curried JavaScript functions,
it's possible to curry the above functions. I'll give an example, using
foldr, but I'm sure the rest is easy enough to follow.
First, you'll notice that most of the functions listed above, work
as methods, acting on an object, as opposed to static class methods. This
means, that if you want to curry something, it would be difficult (or
pointless), since you'd have to start off, by defining what the function
should work on. Changing the functions, from object methods, to
class methods, is easy though, simply remove the prototype
keyword, and ensure, that all references to this are
gone, and replaced with something fitting. Consider the following.
Array.foldr = function(fnc,start,arr) {
var a = start;
for (var i = arr.length-1; i > -1; i--) {
a = fnc(arr[i],a);
}
return a;
}
From it's original form:
Array.prototype.foldr = function(fnc,start) {
var a = start;
for (var i = this.length-1; i > -1; i--) {
a = fnc(this[i],a);
}
return a;
}
Using the solution in the linked currying article, you can create a currying function like this:
Array.foldr = function(fnc,start,arr) {
if (arguments.length < this.foldr.length) {
return curry(this.foldr,arguments,this);
}
var a = start;
for (var i = arr.length-1; i > -1; i--) {
a = fnc(arr[i],a);
}
return a;
}
A function, which takes and array, and adds all elements up, could then be created like thus:
var sum = Array.foldr(function(n,m){return n+m;},0);
alert(sum([1,2,3])); // shows 6
alert(sum([4,5,6])); // shows 15
Note, that depending on the start value, the result returned
could either be a string (if "" had been used), or a number.