(ab) using compiler

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

(ab) using compiler

drush66
Hello,

Would it be possible to use existing built in compiler to translate a string like:

'[:x :y | x + y]'

into string containing source for anonymous javascript function?

Davorin Ruševljan
http://www.cloud208.com/

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

NorbertHartl

Am 17.12.2012 um 10:40 schrieb Davorin Ruševljan <[hidden email]>:

> Hello,
>
> Would it be possible to use existing built in compiler to translate a string like:
>
> '[:x :y | x + y]'
>
> into string containing source for anonymous javascript function?
>
You mean something like that?

(Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource

?

Norbert

P.S.: Btw. Hi, first post to this list :)
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

drush66
On Monday, December 17, 2012 10:57:46 AM UTC+1, Norbert Hartl wrote:

You mean something like that?

(Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource


yes, though my Amber (one started from amber-lang home page) does not understand evaluateExpression :)

I am tinkering a bit with Amber + CouchDB, and thought that maybe it would be possible to provide CouchDB map and reduce functions as Smalltalk blocks and compile them to javascript by Amber before putting them into the database views.

P.S.: Btw. Hi, first post to this list :)

thanks! (I did post quite some time before, but it was such a long time ago, that we can count this a s first again!)
 
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

gokr
On 12/17/2012 11:22 AM, Davorin Ruševljan wrote:

> On Monday, December 17, 2012 10:57:46 AM UTC+1, Norbert Hartl wrote:
>
>
>     You mean something like that?
>
>     (Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource
>
>
> yes, though my Amber (one started from amber-lang home page) does not
> understand evaluateExpression :)
>
> I am tinkering a bit with Amber + CouchDB, and thought that maybe it
> would be possible to provide CouchDB map and reduce functions as
> Smalltalk blocks and compile them to javascript by Amber before putting
> them into the database views.

Just as a curiosity - I did implement a "view server" in Pharo (or
perhaps it was Squeak) for CouchDB a while back so that you could use
Squeak for map/reduce functions.

Worked fine, but of course, the map reduce functions are normally so
small I really don't see the big point.

And since then I have also moved over to Riak for my "NoSQL fix". :)

regards, Göran

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

drush66
On Monday, December 17, 2012 12:22:40 PM UTC+1, Göran Krampe wrote:

Just as a curiosity - I did implement a "view server" in Pharo (or
perhaps it was Squeak) for CouchDB a while back so that you could use
Squeak for map/reduce functions.

Worked fine, but of course, the map reduce functions are normally so
small I really don't see the big point.

And since then I have also moved over to Riak for my "NoSQL fix". :)


yes, using smalltalk view server is an option, but it requires smalltalk to be installed on the machine hosting the CouchDB.

My current question came from different angle - trying to using Amber to write CouchApps. In CouchApp the application code itself is served from the CouchDB server - which has among other things consequence that application can query database without "same origin" workarounds. And they can get replicated.

So my imagined AmberCouchApp would be developed in  Amber IDE, but instead of being saved to local fs it would get stored directly in CouchDB. In such scenario when CouchApp is pushed to the server from Amber, I thought that it might be nice to write view functions as smalltalk blocks and push them together with smalltalk presentation code. It is not a biggie - but it would probably be nice.

There is already somehow similar idea developed with storing amber source code to the online database (but not couchdb), but I do not have link at hand.

Thanks for the input!

(though I still welcome a good tip how to get javascript source for the block)

Davorin


Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

Nicolas Petton
In reply to this post by drush66

That's because on amber-lang.net the old compiler is used. You can do
instead:

(Compiler new loadExpression: '[1+2]') compiledSource

Nico

Davorin Ruševljan <[hidden email]> writes:

> On Monday, December 17, 2012 10:57:46 AM UTC+1, Norbert Hartl wrote:
>>
>>
>> You mean something like that?
>>
>> (Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource
>>
>>
> yes, though my Amber (one started from amber-lang home page) does not
> understand evaluateExpression :)
>
> I am tinkering a bit with Amber + CouchDB, and thought that maybe it would
> be possible to provide CouchDB map and reduce functions as Smalltalk blocks
> and compile them to javascript by Amber before putting them into the
> database views.
>
> P.S.: Btw. Hi, first post to this list :)
>
>
> thanks! (I did post quite some time before, but it was such a long time
> ago, that we can count this a s first again!)
>  

--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

Nicolas Petton
In reply to this post by NorbertHartl

Welcome to Amber Norbert :)

Norbert Hartl <[hidden email]> writes:

> Am 17.12.2012 um 10:40 schrieb Davorin Ruševljan <[hidden email]>:
>
>> Hello,
>>
>> Would it be possible to use existing built in compiler to translate a string like:
>>
>> '[:x :y | x + y]'
>>
>> into string containing source for anonymous javascript function?
>>
> You mean something like that?
>
> (Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource
>
> ?
>
> Norbert
>
> P.S.: Btw. Hi, first post to this list :)
--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

gokr
In reply to this post by drush66
On 12/17/2012 11:22 AM, Davorin Ruševljan wrote:

> On Monday, December 17, 2012 10:57:46 AM UTC+1, Norbert Hartl wrote:
>
>
>     You mean something like that?
>
>     (Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource
>
>
> yes, though my Amber (one started from amber-lang home page) does not
> understand evaluateExpression :)
>
> I am tinkering a bit with Amber + CouchDB, and thought that maybe it
> would be possible to provide CouchDB map and reduce functions as
> Smalltalk blocks and compile them to javascript by Amber before putting
> them into the database views.

Just as a curiosity - I did implement a "view server" in Pharo (or
perhaps it was Squeak) for CouchDB a while back so that you could use
Squeak for map/reduce functions.

Worked fine, but of course, the map reduce functions are normally so
small I really don't see the big point.

And since then I have also moved over to Riak for my "NoSQL fix". :)

regards, Göran

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

drush66
In reply to this post by Nicolas Petton
On Monday, December 17, 2012 1:00:29 PM UTC+1, nicolas petton wrote:

That's because on amber-lang.net the old compiler is used. You can do
instead:

(Compiler new loadExpression: '[1+2]') compiledSource


Thanks,

1) can this code be used as standalone, or it needs some other supporting code/library to work?
2) how does one invoke in Amber global function?
3) can I somehow provide it with name space to use? for instance resulting code I would like end up with javascript function that looks like:

function(doc) {
    emit(doc.user, doc.name);
}

Hmm.. more I look at it, it seems like a too big stretch.

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

Nicolas Petton
I'm answering inside your email.

Davorin Ruševljan <[hidden email]> writes:

> On Monday, December 17, 2012 1:00:29 PM UTC+1, nicolas petton wrote:
>>
>>
>> That's because on amber-lang.net the old compiler is used. You can do
>> instead:
>>
>> (Compiler new loadExpression: '[1+2]') compiledSource
>>
>>
> Thanks,
>
> 1) can this code be used as standalone, or it needs some other supporting
> code/library to work?

It is very likely that would will need amber libraries to run your
function, as you will probably call amber methods.

> 2) how does one invoke in Amber global function?

I don't understand this question :)

> 3) can I somehow provide it with name space to use? for instance resulting
> code I would like end up with javascript function that looks like:
>
> function(doc) {
>     emit(doc.user, doc.name);
> }

Not really. The compiled code *is* Amber compiled code to JS, not JS
that's supposed to be run standalone.

Cheers,
Nico
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

drush66
On Monday, December 17, 2012 2:15:59 PM UTC+1, nicolas petton wrote:
> 2) how does one invoke in Amber global function?

I don't understand this question :)

I wanted to ask, how can I invoke javascript function from amber smalltalk code. I. e. what code I need to write in amber smalltalk so that resulting javascript code would be for instance:

emit("something);
 
But the rest of your answers rendered this question as not so important.

> 3) can I somehow provide it with name space to use? for instance resulting
> code I would like end up with javascript function that looks like:
>
> function(doc) {
>     emit(doc.user, doc.name);
> }

Not really. The compiled code *is* Amber compiled code to JS, not JS
that's supposed to be run standalone.


Ok, so it is not a good idea, I will stick with only javascript for view functions.

Thanks!

Davorin

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

Nicolas Petton

What do you want to do?

If what you need is to call javascript libraries from Amber, there is a
way to do it. JavaScript objects can be used from Amber seemlessly (it
will use a proxy internally). Here's an example with jQuery:

(window jQuery: 'body') css: 'background' put: 'red'

it is explained in the doc here:
http://amber-lang.net/documentation.html#JSObjectProxy

and on the github wiki here:
https://github.com/NicolasPetton/amber/wiki/From-smalltalk-to-javascript-and-back

Cheers,
Nico

Davorin Ruševljan <[hidden email]> writes:

> On Monday, December 17, 2012 2:15:59 PM UTC+1, nicolas petton wrote:
>>
>> > 2) how does one invoke in Amber global function?
>>
>> I don't understand this question :)
>>
>
> I wanted to ask, how can I invoke javascript function from amber smalltalk
> code. I. e. what code I need to write in amber smalltalk so that resulting
> javascript code would be for instance:
>
> emit("something);
>  
> But the rest of your answers rendered this question as not so important.
>
>> 3) can I somehow provide it with name space to use? for instance
>> resulting
>> > code I would like end up with javascript function that looks like:
>> >
>> > function(doc) {
>> >     emit(doc.user, doc.name);
>> > }
>>
>> Not really. The compiled code *is* Amber compiled code to JS, not JS
>> that's supposed to be run standalone.
>>
>>
> Ok, so it is not a good idea, I will stick with only javascript for view
> functions.
>
> Thanks!
>
> Davorin
>

--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

drush66

On Monday, December 17, 2012 2:41:59 PM UTC+1, nicolas petton wrote:

What do you want to do?

If what you need is to call javascript libraries from Amber, there is a
way to do it. JavaScript objects can be used from Amber seemlessly (it
will use a proxy internally). Here's an example with jQuery:

(window jQuery: 'body') css: 'background' put: 'red'


Well, those examples have explicit receiver (like window in example above). What I was asking about was direct invocation of function without explicit receiver, like invocation of emit() in example below:
function(doc) {
  ....
  emit(1);
  ....
}

What would I need to put as a receiver in equivalent smalltalk code instead of xyz?

xyz emit:1


Though, since using Amber to generate standalone javascript functions from a smalltalk block is not an easily workable idea, the whole question is somehow less important.

Davorin

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

NorbertHartl

Am 17.12.2012 um 15:02 schrieb Davorin Ruševljan <[hidden email]>:

Well, those examples have explicit receiver (like window in example above). What I was asking about was direct invocation of function without explicit receiver, like invocation of emit() in example below:
function(doc) {
  ....
  emit(1);
  ....
} 

What would I need to put as a receiver in equivalent smalltalk code instead of xyz?

xyz emit:1 

In this case it is probably best to use javascript direct. Javascript can be put into <> to be executed. So a

< emit(1) >

should work. Even returned values can then be assigned to smalltalk vars etc.

Norbert
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

drush66
On Monday, December 17, 2012 3:07:14 PM UTC+1, Norbert Hartl wrote:
In this case it is probably best to use javascript direct. Javascript can be put into <> to be executed. So a

< emit(1) >

should work. Even returned values can then be assigned to smalltalk vars etc.


thanks!

Davorin

Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

Herby Vojčík
In reply to this post by NorbertHartl


Norbert Hartl wrote:

>
> Am 17.12.2012 um 15:02 schrieb Davorin Ruševljan
> <[hidden email] <mailto:[hidden email]>>:
>
>> Well, those examples have explicit receiver (like window in example
>> above). What I was asking about was direct invocation of function
>> without explicit receiver, like invocation of emit() in example below:
>> function(doc) {
>> ....
>> emit(1);
>> ....
>> }
>>
>> What would I need to put as a receiver in equivalent smalltalk code
>> instead of xyz?
>>
>> xyz emit:1
>
> In this case it is probably best to use javascript direct. Javascript
> can be put into <> to be executed. So a
>
> < emit(1) >
>
> should work. Even returned values can then be assigned to smalltalk vars
> etc.

"emit value: 1" should work as well, but it is not direct call (gone
through smalltalk machinery where #value: eventually calls it, so is
slower; but more smalltalkish).


> Norbert

Herby
Reply | Threaded
Open this post in threaded view
|

Re: (ab) using compiler

gokr
In reply to this post by drush66
On 12/17/2012 11:22 AM, Davorin Ruševljan wrote:

> On Monday, December 17, 2012 10:57:46 AM UTC+1, Norbert Hartl wrote:
>
>
>     You mean something like that?
>
>     (Compiler new evaluateExpression: '[:x :y | x + y]') compiledSource
>
>
> yes, though my Amber (one started from amber-lang home page) does not
> understand evaluateExpression :)
>
> I am tinkering a bit with Amber + CouchDB, and thought that maybe it
> would be possible to provide CouchDB map and reduce functions as
> Smalltalk blocks and compile them to javascript by Amber before putting
> them into the database views.

Just as a curiosity - I did implement a "view server" in Pharo (or
perhaps it was Squeak) for CouchDB a while back so that you could use
Squeak for map/reduce functions.

Worked fine, but of course, the map reduce functions are normally so
small I really don't see the big point.

And since then I have also moved over to Riak for my "NoSQL fix". :)

regards, Göran