Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, March 18, 2015

Google plugin for loona problems on MACOS

1) It contains GWT 2.6.0 - that is old (it looks there is option to install new version manually, and link than).

2) App created from scratch was able to start only in superdev mode, the page is just blank in other modes.

3) Debug breakpoints not work in superdev, that is only working option.

I use last chrome on MacOS that is only available option to use GWT plugin.

link:
http://www.gwtproject.org/articles/superdevmode.html

Update:
I switched back to Ubuntu form most coding tasks where it is possible, Mac is good for browsing, but not so convinient for programming as linux.




Tuesday, June 3, 2008

Google docs



Made a little presentation from one of my posts.

Saturday, March 22, 2008

javascript conditional function definition (c #ifdef ?)

C:


#ifdef MOZILLA_OS
#define _XHTML_
#endif

#ifdef _XHTML_
int print()
{
printf("<node />");
}
#endif

#ifndef _XHTML_
int print()
{
printf("<node></node>");
}
#endif



Javascript:


var OS="MOZILLA";
var XHTML;

if(OS=="MOZILLA")
{
XHTML=1;
} else {XHTML=0;};

if (XHTML)
{
function print() {document.write("<node />");};
} else {
function print() {document.write("<node></node>");};
};

Sunday, March 16, 2008

javascript for code highiliting, v0.3

Placed v.0.3 version.
It is more pretty.

Moved "<>" in the begining of keywords processing loop (by definition in basic.js).

Joined keywords by "|" symbol in basic.js to use as single expression without looping.

Further could be done improvements of comments, they are not working well in the version.
basic.js renamed to basic.js.txt, because it tried to be loaded from iframe.

Bothering alerts also removed.

javascript page for code highliting

GNU source-highlight can be used for source highlighting for now.
However, it gives me an output, that is too formatted to be processed by blogger templates without problems. Also in my opinion,it is not very convenient for online purposes and can not be fast fixed (If blogger, for example will change its templates), because it is written in compiled language.

Basically, it is regexp based, regexp's applied in specified order, and give you final results.
if some code matched regexp, it is processed and removed from processing loop.

If you want to use your function for for processing sources it have to be called def_vars.

Global variables blocks and keywords have to be set inside in a way like it is done in basic.js.

I have finished with basic definition of universal language that have to be used in code,
and now highlite-processing code.

Current alfa of the javascript page placed there:http://roman.gritsulyak.googlepages.com/code-hi.html

Upper part used for downloading schema of processing, defined in javascript function.
Lower part, for processing code, input, output and presentation of how it will look like.

It also produces some shit but it can be easily filtered by hands by little snipplets.

Example of output:


fori while (a){return 0 ;}


For big code I'd better use gnu-source highlite and separate page, and for snipplets, this one gives good advice.

I have already Idea of how to fix it to work more clearly, but it requis additional effort, and I don't now, when time would be.

Idea is to 'tokenize' make array of tokens, and execute expression only once for each token, but not for all text.

Priority schema than have to be implemented and check if token satisfy highlited element.

Mozilla firefox: dynamicaly load javascript file in browser

Played with JavaScript concepts;

Tried to load JavaScript dynamically for the execution.
DOM solution was:


var scr = document.getElementById("ctl-file").value;
document.getElementById("current_ctl").innerHTML=scr;
var s=document.createElement("script");
s.setAttribute("type","text/javascript");
s.setAttribute("src", scr);
var bodyID = document.getElementsByTagName("body")[0];
bodyID.appendChild(s);
def_vars(); // <- this defined inside of document;
// it does not work in Mozilla


It loads to dom, but not executes.

Another proposed solutions are using framewoks (big, and I suppose that's why buggy, XMLHttpRequest (? no idea how it can solve problem)).

I have tried to use IFRAME tag, and implemented workaround based on this tag.

The working code for Mozilla is:


..
<iframe id="schema_exec"
src ="basic.js"
width="100%">
</iframe>
..
var scr = document.getElementById("ctl-file").value;
document.getElementById("current_ctl").innerHTML=scr;
document.getElementById("schema_exec").setAttribute("src", scr);
// previous command substituted basic.js by our file
code_for=document.getElementById("schema_exec").contentDocument.body.textContent;
eval(code_for);

Saturday, March 15, 2008

Evolving functional programming in Javascript(II)

In this post, described example of how "mutation" genetic programming term, can be applied for self-modifying functions.

Now I am inspired by idea of genetic programming in application to functional.

This approach is different from "classical" genetic programming approach, where solution to task of optimization is founded, using solutions generated by genetic algorithm, and calculating difference of solution searched, and proposed solution, with use of fitness function.

The idea is following:

- Functions could be supposed as living beings.
- Function code itself, presents it's DNA.
- Mutation can be presented by random change of function part.
- Crossover can be presented by splitting functions to parts and than joining parts of functions together to get new function.

From the existing languages for the prototyping such functional beings, JavaScript, looks like the most convenient for me.

Logic operators: Lazyness C++, Perl, Java, Javascript, ksh

Examples demonstrating lazy evaluation for || and && in different languages presented.

Consider following c++ code:


Fun1() || Fun2() || Fun3();
Fun1() && Fun2() && Fun3();


In c++, they are executed, only if the result of overall execution depends from execution of function that was not yet executed. '&&' , '||' and ',' are only c++ operators, thats order is direct from left-to right.

In examples:

Java1.6:
/**
* @author rtg
*
*/
public final class main {
public static boolean fun1(){System.out.println("fun1"); return true;}
public static boolean fun2(){System.out.println("fun2"); return false;}
public static boolean fun3(){System.out.println("fun3"); return true;}
/**
* @param args
*/
public static void main (String[] args) {
boolean res;
System.out.println("|| test:");
res = fun1() || fun2() || fun3();
System.out.printf("res:%b\n",res);
System.out.println("&& test:");
res = fun1() && fun2() && fun3();
System.out.printf("res:%b",res);
}
}


Results are the same as for

Javascript in firefox 2 is lazy too:
example

Perl's operators are lazy too:

$ cat lazy_or.pl
#!/usr/bin/perl

sub fun1 {print "fun1\n"; return 1;};
sub fun2 {print "fun2\n"; return 0;};
sub fun3 {print "fun3\n"; return 1;};

sub main
{
print "or:\n";
print "res=" . (fun1() or fun2() or fun3()) . "\n";
print "and:\n";
print "res=" . (fun1() and fun2() and fun3()) . "\n";
print "&&:\n";
print "res=" . (fun1() && fun2() && fun3()) . "\n";
print "||:\n";
print "res=" . (fun1() || fun2() || fun3()) . "\n";
}

main();

rtg@kubic-roman:~$ perl lazy_or.pl
or:
fun1
res=1
and:
fun1
fun2
res=0
&&:
fun1
fun2
res=0
||:
fun1
res=1
rtg@kubic-roman:~$ perl -v

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi


ksh example at the end is different in results from perl c++ and javascript.
After ksh testing I understand, why Java not accepts int's as booleans for the operator.

ksh interprets 0 as true and 1 as false, so it is the place were you have to be patient,
when swith to:

$ cat lazy.ksh
#!/usr/bin/ksh

fun1() {
echo "fun1"
return 1;
}

fun2() {
echo "fun2"
return 0;
}

fun3() {
echo "fun3"
return 1;
}

status(){
echo $?
}

fun1 || fun2 || fun3
status
fun1 && fun2 && fun3
status
rtg@kubic-roman:~$ ./lazy.ksh
fun1
fun2
0
fun1
1


Thats strange, that a lot of programmers in presented languages where very confident about non-laziness of the operators.
However, it is common for all presented languages, and it is no conceptual difference in these languages at the point described.

C++ example:
#include 

int fun1(){std::cout << "fun1\n"; return 1;}
int fun2(){std::cout << "fun2\n"; return 0;}
int fun3(){std::cout << "fun3\n"; return 1;}

int main()
{
int test_val;
std::cout << "\n || test:\n";
test_val = fun1() || fun2() || fun3();
std::cout << "result is:" << test_val << std::endl;
std::cout << "\n && test:\n";
test_val = fun1() && fun2() && fun3();
std::cout << "result is:" << test_val << std::endl;
}
result of code execution is:

|| test:
fun1
result is:1

&& test:
fun1
fun2
result is:0

Tuesday, March 11, 2008

Evolving functional programming in Javascript(I)

On this page presented self-modifying JavaScript code example.

It is implementation of mutating JavaScript function. "mutate()" function used for modification of "execute()" function. "execute()" function performs some iteration with web page.

You can modify code of the functions in the text boxes on the page , and it will affect execution.

It seems, that JavaScript in some aspects even cooler than lisp.
That is really good feature, is JavaScript code placed on same board, as all document in browser.

So "code is data" 'unique' lisp feature is even clearer in JavaScript.

Tuesday, March 4, 2008

Macro- and Template- like parametrization of JavaScript functions

Following example present some approaches that can be used to implement JavaScript constructs, that can mimic C++ templates, C #define, and Lisp macro's.

However, JavaScript syntax is still used in the examples. It is no such hacks of syntax in javascript as defmacro in lisp, and operator redefinition as in C++, so further used functional forms, with passing statements for execution as strings. Last example presents definition of control statement in terms of JavaScript object system.

This way, the basis for implementation, is the usage of eval function, that operates lexical contest, and JavaScript OOP.

For example,specific adder function can be built, that converts arguments in required way before made addition. We are adding arguments as strings and as integers in following example.

The JavaScript code is:

a = 5;

document.write("<br>"+ a + "<br>");

function parametrized_adder(param1,param2){
return function(a, b){
return eval( param1 +"(a) +" + param2 + "(b)");
};
};

var num_adder = parametrized_adder("parseInt","parseInt");

document.writeln("<br> int 5 + int 5 = " + num_adder(5,5) );
document.writeln("<br> str 5 + int 5 = " + (parametrized_adder("String","parseInt"))(5,5) );

And the result is:
5

int 5 + int 5 = 10
str 5 + int 5 = 55


The next example demonstrates workaround for argument passing by value. It is example of incf() function used for increasing value of its parameter by 1. In some tutorials on lisp incf, presented as first example of macro, because passing parameter in lisp is by value too.
Instead of using macro-way substitution we would use javascript object, that's values are mutable even if object passed to function.

var c = {value:0};
c.incf = function incf(para)
{
this.value++;
};

c.value = 5;
c.incf();

document.writeln("incf(5)=" + c.value);


result: incf(5)=6

Also, using the first presented technique, it is possible to define our own Lisp cond function equivalent, as it can be made by scheme and common lisp macro's. This function split arguments by pairs ; evaluate first; if it is true => evaluate second part.

function cond()
{
var items = cond.arguments.length
for (i = 0;i < items;i+=2)
{
if(eval(cond.arguments[i])) { eval(cond.arguments[i+1]);return true}
}
return false;
}

a = c // use previously defined c as prototype
b = c
a.value = 10;
b.value = 12;

cond ("a.value+b.value < 15", "a.value = 15; alert (\"a set to 15\")",
"a.value+b.value > 50", "b.value = 50; alert (\"b set to 50\")",
"a.value=7; b.value=7;true;","alert(\"both set to 7;\")");

document.writeln("final b=",b.value);
document.writeln("final a=",a.value);


result will be
final b=7 final a=7
and alert will be displayed about this setting.
also we can implement this example using object structure of javascript:


o_cond={body:[]};

o_cond.execute= function () {
for(i=0;i<this.body.length;i+=2)
{
if (eval(this.body[i])) {
eval(this.body[i + 1]);
return true;
};
};
};

o_cond.body = new Array ("a.value+b.value < 15" , "a.value = 15; alert (\"a set to 15\")",
"a.value+b.value > 50" , "b.value = 50; alert (\"b set to 50\")",
"a.value=7; b.value=7;true;","alert(\"both set to 7;\")");

o_cond.execute();


If previous values of a and b where 7 and 7 as set by previous example, than alert will be displayed that a set to 15; that indicates, that this approach(OOP), works for building lisp cond statement equivalent also.

firefox 2.0.12 under linux i686 was used for examples demonstrated.

Monday, February 25, 2008

Links: differences in syntax: Perl, tcl, Shell, C++, Python, Java, Javascript, Lisp

I have already mentioned in my post on comparing numbers and strings in shell, about problems of simultaneous use of different programming languages.

To conclude, having reference cards with description one language to another differences can be useful.

Listed is set of resources, intended for migration from one language to another.

Recommended (short,self-descriptive, useful):
languages comparison:
http://merd.sourceforge.net/pixel/language-study/syntax-across-languages/


Recommended, but not short:

Wikipedia page:
http://en.wikipedia.org/wiki/Comparison_of_programming_languages

Open directory listing for comparisons:
http://www.dmoz.org/Computers/Programming/Languages/Comparison_and_Review/

PLEAC - Programming Language Examples Alike Cookbook
Comparison of productivity of writing in different programming languages:
page.mi.fu-berlin.de/~prechelt/Biblio/jccpprtTR.pdf

Useful:
Java for c++ Programmers:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/

http://triton.towson.edu/~mzimand/os/Lect2-java-tutorial.html
Lisp to javascript converter, descriptive.
http://javascript.crockford.com/little.html
Another one Lisp to Javascript converter written in javascript.( You can look into source to look into the code)
http://www.joeganley.com/code/jslisp.html

Comparison Python with Java, Lisp i.t.c.

http://wiki.python.org/moin/LanguageComparisons


Three scripting concurrents:
http://mjtsai.com/blog/2002/11/25/perl_vs_python_vs_ruby/

Accumulator generator in different languages:
http://www.paulgraham.com/accgen.html

Tcl vs. Python, with nice short examples

http://homepages.cwi.nl/~sjoerd/PythonVsTcl-old.html


This thread describes differences between bash and perl.
http://www.perlmonks.org/?node_id=661859

And at the end resource with language comparison in action (memory, speed, size).
http://shootout.alioth.debian.org/

Tuesday, February 19, 2008

Javascript equivalent of perl attributes.

This javascript enabled page contains code for this article
It was checked under Mozilla Firefox 2.0.0.12


In one of my previous posts I have considered perl attributes.

Attributes are functions itself,that could be defined by user, and that have access to another function body, and can operate another functions, at the moment of definition of the functions with the attributes.

For example, when such definition met by Perl in the Perl script,

sub func_sub : attribute_func 
Perl executes attribute_func, passing func_sub to the function. Then attribute_func can change func_sub executable body, adding "header" and "footer" to the function, or alerting about definition met, for example.

Due to the fact, that JavaScript is also used widely for web applications, but on client side,
I have asked myself about equivalent JavaScript form.

It appeared, that for JavaScript it is even more simple equivalent exists, that can be used for understanding Perl attributes definition. Attribute can be implemented as high order function, that manipulates function body.

We can log function in and out using following attribute function defined as following closure:

function attr (fparam){
document.writeln("attr.beginning< br >");
fparam();
document.writeln("attr.end< br >");
}

Example of how it can be applied:

a = function(var_my)
{
attr(function()
{
document.writeln("this is first line of original function (a) < br >");
document.writeln("var passed:" + var_my + "< br >");
} )
};

document.writeln("perform call to a() < br >");
a("1");

More complex example of attribute , that modifies code of function
before function will be executed:

We define attribute that prints function's code given, and that adds additional strings of code to function, and define this new function under the different name, and than executes modified function.

It is example of self-modifying code.


// prints a lot of debug info,
// define new function like given, but with changes in body
// and execute it.
function attr2 (fparam){
document.writeln("attr.beginning < br >");
document.writeln("func get:" + fparam +"< br >");
var tempstr = new String;
tempstr = fparam.toString();
tempstr = tempstr.replace(");","); global_var*=2;
document.writeln(\"inserted by attr2\");" );
tempstr = tempstr.replace("function ()",
"function b_modified()");
var func = tempstr;
document.writeln("Now call func made by attr2:"
+ tempstr + "< br >");
eval(func);
b_modified();
document.writeln("attr.end< br >");
}

// define b as attr2(anonimous function given)
b = function()
{
attr2(function()
{
document.writeln("this is first line of original function (b)< br >");
document.writeln("global_var="+ global_var);
} )
};

// global_var is printed and modified inside b
var global_var=2;
b();



In the begining of article link to the place were all the code cited
can be viewed in action published.