Is there a way to work with underscore.js from amber?
How would you write this for example in amber: console.clear(); var numbers = [10, 5, 100, 2, 1000]; console.log(_.min(numbers)); console.log(_.max(numbers)); In general how to handle special characters in amber. For example instance variables like 'first_name' fails to compile. Another example: | numbers_ | numbers_ := #(10 5 100 2 1000). console log: numbers_. |
Am 14.01.2013 um 23:16 schrieb laci <[hidden email]>: > Is there a way to work with underscore.js from amber? > How would you write this for example in amber: > > console.clear(); > var numbers = [10, 5, 100, 2, 1000]; > console.log(_.min(numbers)); > console.log(_.max(numbers)); > console clear. numbers := #( 10 5 100 2 1000). underscore := <_>. console log: (underscore min: numbers) > In general how to handle special characters in amber. > For example instance variables like 'first_name' fails to compile. > Another example: > | numbers_ | > numbers_ := #(10 5 100 2 1000). > console log: numbers_. I don't know the reasons for the restriction in amber but are you sure you need them? Norbert |
Regarding "I don't know the reasons for the restriction in amber but are you sure you need them? "
I don't need it. Just curious. It is quite common naming convention but I can live without it. Regarding calling underscore.js, I'm afraid it doesn't work. Typically you call external libraries/namespaces like 'window namespace function'. For example interfacing with knockout.js looks like that: window ko applyBindings: model. or < ko.applyBindings(self['@model']) >. I would think this should work: < console.clear(); var numbers = [10, 5, 100, 2, 1000]; console.log(_.min(numbers)); console.log(_.max(numbers)); > I guess the compiler limitation falls over and it fails silently. Your suggestion fails too unfortunately. |
Hi,
Am 15.01.2013 um 00:28 schrieb laci <[hidden email]>: > Regarding "I don't know the reasons for the restriction in amber but are you > sure you need them? " > I don't need it. Just curious. It is quite common naming convention but I > can live without it. > > Regarding calling underscore.js, I'm afraid it doesn't work. > Typically you call external libraries/namespaces like 'window namespace > function'. > For example interfacing with knockout.js looks like that: > window ko applyBindings: model. > or > < ko.applyBindings(self['@model']) >. > > I would think this should work: > < > console.clear(); > var numbers = [10, 5, 100, 2, 1000]; > console.log(_.min(numbers)); > console.log(_.max(numbers)); >> > I guess the compiler limitation falls over and it fails silently. > > Your suggestion fails too unfortunately. I'm sorry. I didn't try underscore. I just assumed it is a global namespace and therefor it should have worked. Now I took the time to investigate it. The problem is that _ in underscore.js is a function. A function in javascript (being an object) can have functions. That doesn't map well to the smalltalk side. To make it possible you could something like the following. | underscore fName underscoreObject | underscore := <_>. fName := 'min'. underscoreObject := < o = {}; o[fName] = underscore[fName]; >. underscoreObject min: #(10 5 100 2 1000) If you need a lot of underscore functionality you could add a utility for underscore. I made on quick. It has two methods <cut> Smalltalk current createPackage: 'Underscore' properties: #{}! Object subclass: #Underscore instanceVariableNames: '' package: 'Underscore'! Underscore class instanceVariableNames: 'underscore'! !Underscore class methodsFor: 'not yet classified'! doesNotUnderstand: aMessage aMessage sendTo: underscore ! prepare underscore := < o = {}; for(var prop in _) { if(_.hasOwnProperty(prop)) { o[prop] = _[prop] } }>. ! ! </cut> With this you can use it like Underscore min: #( 10 5 100 2 1000) I hope this helps. I hope you know that usually you wouldn't need underscore at all in smalltalk. Your example in normal smalltalk would be #( 10 5 100 2 1000) min Amber smalltalk does not implement all of the methods. So min could be written as (#( 10 5 100 2 1000) sort: [:a :b| a < b ]) first and there are plenty of ways producing the same result. But if time comes amber will have more of the Collection protocol of smalltalk I hope. I suspect reduceLeft/Right would be cool additions. Then you would do #( 10 5 100 2 1000) reduceLeft: [:a :b| a min: b ] or better I would do :) because I love that style. Ok, hope this helps better this time, Norbert |
In the source prepare should be called initialize. So you shouldn't have to do a manual step. New version attached
Norbert Am 15.01.2013 um 11:08 schrieb Norbert Hartl <[hidden email]>: > Hi, > > Am 15.01.2013 um 00:28 schrieb laci <[hidden email]>: > >> Regarding "I don't know the reasons for the restriction in amber but are you >> sure you need them? " >> I don't need it. Just curious. It is quite common naming convention but I >> can live without it. >> >> Regarding calling underscore.js, I'm afraid it doesn't work. >> Typically you call external libraries/namespaces like 'window namespace >> function'. >> For example interfacing with knockout.js looks like that: >> window ko applyBindings: model. >> or >> < ko.applyBindings(self['@model']) >. >> >> I would think this should work: >> < >> console.clear(); >> var numbers = [10, 5, 100, 2, 1000]; >> console.log(_.min(numbers)); >> console.log(_.max(numbers)); >>> >> I guess the compiler limitation falls over and it fails silently. >> >> Your suggestion fails too unfortunately. > > I'm sorry. I didn't try underscore. I just assumed it is a global namespace and therefor it should have worked. Now I took the time to investigate it. The problem is that _ in underscore.js is a function. A function in javascript (being an object) can have functions. That doesn't map well to the smalltalk side. To make it possible you could something like the following. > > | underscore fName underscoreObject | > underscore := <_>. > fName := 'min'. > underscoreObject := < o = {}; o[fName] = underscore[fName]; >. > underscoreObject min: #(10 5 100 2 1000) > > If you need a lot of underscore functionality you could add a utility for underscore. I made on quick. It has two methods > > <cut> > Smalltalk current createPackage: 'Underscore' properties: #{}! > Object subclass: #Underscore > instanceVariableNames: '' > package: 'Underscore'! > > Underscore class instanceVariableNames: 'underscore'! > > !Underscore class methodsFor: 'not yet classified'! > > doesNotUnderstand: aMessage > aMessage sendTo: underscore > ! > > prepare > underscore := < o = {}; for(var prop in _) { if(_.hasOwnProperty(prop)) { o[prop] = _[prop] } }>. > ! ! > </cut> > > With this you can use it like > > Underscore min: #( 10 5 100 2 1000) > > I hope this helps. I hope you know that usually you wouldn't need underscore at all in smalltalk. Your example in normal smalltalk would be > > #( 10 5 100 2 1000) min > > Amber smalltalk does not implement all of the methods. So min could be written as > > (#( 10 5 100 2 1000) sort: [:a :b| a < b ]) first > > and there are plenty of ways producing the same result. But if time comes amber will have more of the Collection protocol of smalltalk I hope. I suspect reduceLeft/Right would be cool additions. Then you would do > > #( 10 5 100 2 1000) reduceLeft: [:a :b| a min: b ] > > or better I would do :) because I love that style. > > Ok, hope this helps better this time, > > Norbert > Underscore.st (469 bytes) Download Attachment |
Beautiful. Thanks for your comprehensive reply.
I am still juggling between extending amber code-base versus embracing third-party JavaScript libraries. Anyway great piece of code. One note. Underscore min: #( 10 5 100 2 1000) answered Underscore instead of 2. I might have made a mistake. I'll check it out this evening. |
In reply to this post by NorbertHartl
Norbert Hartl wrote: > Am 14.01.2013 um 23:16 schrieb laci<[hidden email]>: > >> Is there a way to work with underscore.js from amber? >> How would you write this for example in amber: >> >> console.clear(); >> var numbers = [10, 5, 100, 2, 1000]; >> console.log(_.min(numbers)); >> console.log(_.max(numbers)); >> > console clear. > numbers := #( 10 5 100 2 1000). > underscore :=<_>. <underscore = _>. The <...> JS inline code was meant to represent a statement and not an expression. Thought it may be used as such, but the compiler may change. > console log: (underscore min: numbers) > >> In general how to handle special characters in amber. >> For example instance variables like 'first_name' fails to compile. >> Another example: >> | numbers_ | >> numbers_ := #(10 5 100 2 1000). >> console log: numbers_. > > I don't know the reasons for the restriction in amber but are you sure you need them? > > Norbert |
In reply to this post by NorbertHartl
Norbert Hartl wrote: > Amber smalltalk does not implement all of the methods. So min could be written as > > (#( 10 5 100 2 1000) sort: [:a :b| a< b ]) first > > and there are plenty of ways producing the same result. But if time comes amber will have more of the Collection protocol of smalltalk I hope. I suspect reduceLeft/Right would be cool additions. Then you would do > > #( 10 5 100 2 1000) reduceLeft: [:a :b| a min: b ] #( 10 5 100 2 1000) inject: Infinity into: [:a :b| a min: b ] > > or better I would do :) because I love that style. > > Norbert |
Am 15.01.2013 um 13:35 schrieb Herby Vojčík <[hidden email]>: > > > Norbert Hartl wrote: >> Amber smalltalk does not implement all of the methods. So min could be written as >> >> (#( 10 5 100 2 1000) sort: [:a :b| a< b ]) first >> >> and there are plenty of ways producing the same result. But if time comes amber will have more of the Collection protocol of smalltalk I hope. I suspect reduceLeft/Right would be cool additions. Then you would do >> >> #( 10 5 100 2 1000) reduceLeft: [:a :b| a min: b ] > #( 10 5 100 2 1000) inject: Infinity into: [:a :b| a min: b ] >> thanks again, Norbert >> or better I would do :) because I love that style. >> >> Norbert |
In reply to this post by laci
If you are using the first version in the mail please note that a ^ was missing. It should be
^ aMessage sendTo: underscore Norbert Am 15.01.2013 um 12:58 schrieb laci <[hidden email]>: > Beautiful. Thanks for your comprehensive reply. > I am still juggling between extending amber code-base versus embracing > third-party JavaScript libraries. Anyway great piece of code. > > One note. Underscore min: #( 10 5 100 2 1000) answered Underscore instead > of 2. > I might have made a mistake. I'll check it out this evening. > > > > -- > View this message in context: http://forum.world.st/using-underscore-js-from-amber-tp4663405p4663483.html > Sent from the Amber Smalltalk mailing list archive at Nabble.com. |
In reply to this post by Herby Vojčík
Am 15.01.2013 um 13:28 schrieb Herby Vojčík <[hidden email]>: > > > Norbert Hartl wrote: >> Am 14.01.2013 um 23:16 schrieb laci<[hidden email]>: >> >>> Is there a way to work with underscore.js from amber? >>> How would you write this for example in amber: >>> >>> console.clear(); >>> var numbers = [10, 5, 100, 2, 1000]; >>> console.log(_.min(numbers)); >>> console.log(_.max(numbers)); >>> >> console clear. >> numbers := #( 10 5 100 2 1000). >> underscore :=<_>. > Here it would be cleaner to put > <underscore = _>. > The <...> JS inline code was meant to represent a statement and not an expression. Thought it may be used as such, but the compiler may change. Ok, makes sense. thanks Norbert >> console log: (underscore min: numbers) >> >>> In general how to handle special characters in amber. >>> For example instance variables like 'first_name' fails to compile. >>> Another example: >>> | numbers_ | >>> numbers_ := #(10 5 100 2 1000). >>> console log: numbers_. >> >> I don't know the reasons for the restriction in amber but are you sure you need them? >> >> Norbert |
In reply to this post by Herby Vojčík
Indeed, Herby is perfectly right.
Inlined JS statements should not be used as expressions. I was thinking about being more restrictive, but such a change could break people's code. Now it might be required anyway at some point. Nico Herby Vojčík <[hidden email]> writes: > Norbert Hartl wrote: >> Am 14.01.2013 um 23:16 schrieb laci<[hidden email]>: >> >>> Is there a way to work with underscore.js from amber? >>> How would you write this for example in amber: >>> >>> console.clear(); >>> var numbers = [10, 5, 100, 2, 1000]; >>> console.log(_.min(numbers)); >>> console.log(_.max(numbers)); >>> >> console clear. >> numbers := #( 10 5 100 2 1000). >> underscore :=<_>. > Here it would be cleaner to put > <underscore = _>. > The <...> JS inline code was meant to represent a statement and not an > expression. Thought it may be used as such, but the compiler may change. >> console log: (underscore min: numbers) >> >>> In general how to handle special characters in amber. >>> For example instance variables like 'first_name' fails to compile. >>> Another example: >>> | numbers_ | >>> numbers_ := #(10 5 100 2 1000). >>> console log: numbers_. >> >> I don't know the reasons for the restriction in amber but are you sure you need them? >> >> Norbert -- Nicolas Petton http://nicolas-petton.fr |
In reply to this post by laci
Another solution would be to alias the '_' underscore variable and allow JS functions calls as the following (untested): < window.underscore = window._; window.underscore.allowJavaScriptCalls = true; > then: window underscore min: #(...) That's exactly the purpose of 'allowJavaScriptCalls'. To be used with caution though: the underscore function is a Smalltalk object, so name clash can happen. Cheers, Nico laci <[hidden email]> writes: > Beautiful. Thanks for your comprehensive reply. > I am still juggling between extending amber code-base versus embracing > third-party JavaScript libraries. Anyway great piece of code. > > One note. Underscore min: #( 10 5 100 2 1000) answered Underscore instead > of 2. > I might have made a mistake. I'll check it out this evening. > > > > -- > View this message in context: http://forum.world.st/using-underscore-js-from-amber-tp4663405p4663483.html > Sent from the Amber Smalltalk mailing list archive at Nabble.com. -- Nicolas Petton http://nicolas-petton.fr |
Hi all,
I started Amber last autumn and I started to share some of the basic stuff I successfully extended Amber with on Github. I'll soon have some more things to share and I was wondering which way might be best. Could we setup "one" general kind of "solution" to provide Amber extensions? I'm not too sure if I should use, Github, Git submodules, an examples folder subfolder on github, Smalltalkhub with Kaliningrad, ss3 with Kaliningrad,... pull requests to the general Amber repository to add stuff to the examples folder directly,... Will there soon be a solution which might be included in Pharo?... What are your preferences? How do you share? Who's to ask and who's to blame? ;-) Cheers! Sebastian |
In reply to this post by NorbertHartl
Am 15.01.2013 um 13:50 schrieb Norbert Hartl <[hidden email]>: > > Am 15.01.2013 um 13:28 schrieb Herby Vojčík <[hidden email]>: > >> >> >> Norbert Hartl wrote: >>> Am 14.01.2013 um 23:16 schrieb laci<[hidden email]>: >>> >>>> Is there a way to work with underscore.js from amber? >>>> How would you write this for example in amber: >>>> >>>> console.clear(); >>>> var numbers = [10, 5, 100, 2, 1000]; >>>> console.log(_.min(numbers)); >>>> console.log(_.max(numbers)); >>>> >>> console clear. >>> numbers := #( 10 5 100 2 1000). >>> underscore :=<_>. >> Here it would be cleaner to put >> <underscore = _>. >> The <...> JS inline code was meant to represent a statement and not an expression. Thought it may be used as such, but the compiler may change. > > Ok, makes sense. thanks > Norbert > Norbert > >>> console log: (underscore min: numbers) >>> >>>> In general how to handle special characters in amber. >>>> For example instance variables like 'first_name' fails to compile. >>>> Another example: >>>> | numbers_ | >>>> numbers_ := #(10 5 100 2 1000). >>>> console log: numbers_. >>> >>> I don't know the reasons for the restriction in amber but are you sure you need them? >>> >>> Norbert > |
Free forum by Nabble | Edit this page |