assigning to a list

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

assigning to a list

go2ghana
  Hi,
what is the simplest way to transform the following python code to haXe?

dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]

Cordially,
Axel

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

Tony Polinelli
I dont think there is an equivalent-
var dr = (maxVal-r) / d;
var dg = (maxVal-g) / d;
var db = (maxVal-b) / d;
nice question- can this be bettered?


On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
 Hi,
what is the simplest way to transform the following python code to haXe?

dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]

Cordially,
Axel

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

Justin Donaldson
No, unfortunately haXe doesn't support tuple assignment or "autovivification" the same way that other scripting languages do.  Both of these techniques are easy to use, but run contrary to the strict variable typing and declaration system that haXe promotes.


You would have to use for loops (or functional mapping) + iterables.  Here's how I might do it, it's not as efficient as possible, but it's pretty simple:

using Lambda;
//...
        var arr = [64,48,32]; // r,g,b
        var maxVal = Math.NEGATIVE_INFINITY;
        for (a in arr) if (maxVal < a) maxVal = a;
        var d = 1;

        var f = function(x:Int) return Std.int((x - maxVal)/d);  // make sure it's an int?
        var arr_adj = arr.map(f).array(); // arr_adj has the adjusted values now


The last two lines are roughly equivalent to the tuple assignment line in python.  The values are just returned in an array, and you can assign them to individual variables if necessary (although I rarely would do that when handling rgb information).

-Justin


On Sun, Apr 25, 2010 at 11:01 AM, Tony Polinelli <[hidden email]> wrote:
I dont think there is an equivalent-
var dr = (maxVal-r) / d;
var dg = (maxVal-g) / d;
var db = (maxVal-b) / d;
nice question- can this be bettered?


On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
 Hi,
what is the simplest way to transform the following python code to haXe?

dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]

Cordially,
Axel

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net
aim: iujjd
twitter: jjdonald

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

John A. De Goes

"Destructuring assignment" is perfectly type safe, but haXe does not support this or tuples (except as the argument to functions).

In this case you might do:

using Lambda;
...
var color = [r, g, b].map(function(c) { return (maxVal - c) / d; });
var r = color[0], g = color[1], b = color[2];

A bit ugly but gets the job done.

Regards,

John

On Apr 25, 2010, at 2:48 PM, Justin Donaldson wrote:

No, unfortunately haXe doesn't support tuple assignment or "autovivification" the same way that other scripting languages do.  Both of these techniques are easy to use, but run contrary to the strict variable typing and declaration system that haXe promotes.


You would have to use for loops (or functional mapping) + iterables.  Here's how I might do it, it's not as efficient as possible, but it's pretty simple:

using Lambda;
//...
        var arr = [64,48,32]; // r,g,b
        var maxVal = Math.NEGATIVE_INFINITY;
        for (a in arr) if (maxVal < a) maxVal = a;
        var d = 1;

        var f = function(x:Int) return Std.int((x - maxVal)/d);  // make sure it's an int?
        var arr_adj = arr.map(f).array(); // arr_adj has the adjusted values now


The last two lines are roughly equivalent to the tuple assignment line in python.  The values are just returned in an array, and you can assign them to individual variables if necessary (although I rarely would do that when handling rgb information).

-Justin


On Sun, Apr 25, 2010 at 11:01 AM, Tony Polinelli <[hidden email]> wrote:
I dont think there is an equivalent-
var dr = (maxVal-r) / d;
var dg = (maxVal-g) / d;
var db = (maxVal-b) / d;
nice question- can this be bettered?


On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
 Hi,
what is the simplest way to transform the following python code to haXe?

dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]

Cordially,
Axel

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net
aim: iujjd
twitter: jjdonald
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

Justin Donaldson
destructuring assignment is my new phrase for the day :)

-Justin

On Sun, Apr 25, 2010 at 3:21 PM, John A. De Goes <[hidden email]> wrote:

"Destructuring assignment" is perfectly type safe, but haXe does not support this or tuples (except as the argument to functions).

In this case you might do:

using Lambda;
...
var color = [r, g, b].map(function(c) { return (maxVal - c) / d; });
var r = color[0], g = color[1], b = color[2];

A bit ugly but gets the job done.

Regards,

John

On Apr 25, 2010, at 2:48 PM, Justin Donaldson wrote:

No, unfortunately haXe doesn't support tuple assignment or "autovivification" the same way that other scripting languages do.  Both of these techniques are easy to use, but run contrary to the strict variable typing and declaration system that haXe promotes.


You would have to use for loops (or functional mapping) + iterables.  Here's how I might do it, it's not as efficient as possible, but it's pretty simple:

using Lambda;
//...
        var arr = [64,48,32]; // r,g,b
        var maxVal = Math.NEGATIVE_INFINITY;
        for (a in arr) if (maxVal < a) maxVal = a;
        var d = 1;

        var f = function(x:Int) return Std.int((x - maxVal)/d);  // make sure it's an int?
        var arr_adj = arr.map(f).array(); // arr_adj has the adjusted values now


The last two lines are roughly equivalent to the tuple assignment line in python.  The values are just returned in an array, and you can assign them to individual variables if necessary (although I rarely would do that when handling rgb information).

-Justin


On Sun, Apr 25, 2010 at 11:01 AM, Tony Polinelli <[hidden email]> wrote:
I dont think there is an equivalent-
var dr = (maxVal-r) / d;
var dg = (maxVal-g) / d;
var db = (maxVal-b) / d;
nice question- can this be bettered?


On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
 Hi,
what is the simplest way to transform the following python code to haXe?

dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]

Cordially,
Axel

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net
aim: iujjd
twitter: jjdonald
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net
aim: iujjd
twitter: jjdonald

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

clemos
me too :)
https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment_%28Merge_into_own_page.2fsection%29
reading about it makes me think it would be a great feature, very
elegant, though I have no idea how hard it would be to implement...

+++++
Clément

On Mon, Apr 26, 2010 at 2:50 AM, Justin Donaldson <[hidden email]> wrote:

> destructuring assignment is my new phrase for the day :)
>
> -Justin
>
> On Sun, Apr 25, 2010 at 3:21 PM, John A. De Goes <[hidden email]> wrote:
>>
>> "Destructuring assignment" is perfectly type safe, but haXe does not
>> support this or tuples (except as the argument to functions).
>> In this case you might do:
>>
>> using Lambda;
>>
>> ...
>>
>> var color = [r, g, b].map(function(c) { return (maxVal - c) / d; });
>>
>> var r = color[0], g = color[1], b = color[2];
>>
>> A bit ugly but gets the job done.
>> Regards,
>> John
>> On Apr 25, 2010, at 2:48 PM, Justin Donaldson wrote:
>>
>> No, unfortunately haXe doesn't support tuple assignment or
>> "autovivification" the same way that other scripting languages do.  Both of
>> these techniques are easy to use, but run contrary to the strict variable
>> typing and declaration system that haXe promotes.
>>
>>
>> You would have to use for loops (or functional mapping) + iterables.
>> Here's how I might do it, it's not as efficient as possible, but it's pretty
>> simple:
>>
>> using Lambda;
>> //...
>>         var arr = [64,48,32]; // r,g,b
>>         var maxVal = Math.NEGATIVE_INFINITY;
>>         for (a in arr) if (maxVal < a) maxVal = a;
>>         var d = 1;
>>
>>         var f = function(x:Int) return Std.int((x - maxVal)/d);  // make
>> sure it's an int?
>>         var arr_adj = arr.map(f).array(); // arr_adj has the adjusted
>> values now
>>
>>
>> The last two lines are roughly equivalent to the tuple assignment line in
>> python.  The values are just returned in an array, and you can assign them
>> to individual variables if necessary (although I rarely would do that when
>> handling rgb information).
>>
>> -Justin
>>
>>
>> On Sun, Apr 25, 2010 at 11:01 AM, Tony Polinelli <[hidden email]>
>> wrote:
>>>
>>> I dont think there is an equivalent-
>>> var dr = (maxVal-r) / d;
>>> var dg = (maxVal-g) / d;
>>> var db = (maxVal-b) / d;
>>> nice question- can this be bettered?
>>>
>>> On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
>>>>
>>>>  Hi,
>>>> what is the simplest way to transform the following python code to haXe?
>>>>
>>>> dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]
>>>>
>>>> Cordially,
>>>> Axel
>>>>
>>>> --
>>>> haXe - an open source web programming language
>>>> http://haxe.org
>>>
>>>
>>>
>>> --
>>> Tony Polinelli
>>> http://touchmypixel.com
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>>
>> --
>> Justin Donaldson
>> PhD Candidate, Informatics
>> Indiana University
>> http://www.scwn.net
>> aim: iujjd
>> twitter: jjdonald
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
>
> --
> Justin Donaldson
> PhD Candidate, Informatics
> Indiana University
> http://www.scwn.net
> aim: iujjd
> twitter: jjdonald
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

Tarwin Stroh-Spijer
Why not just pass a dynamic back that's got a TypeDef if your worried about what your function is giving back? You'll get proper typing that way too!


Tarwin Stroh-Spijer
_______________________

Touch My Pixel
http://www.touchmypixel.com/
phone: +61 3 8060 5321
_______________________


On Mon, Apr 26, 2010 at 6:19 PM, clemos <[hidden email]> wrote:
me too :)
https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment_%28Merge_into_own_page.2fsection%29
reading about it makes me think it would be a great feature, very
elegant, though I have no idea how hard it would be to implement...

+++++
Clément

On Mon, Apr 26, 2010 at 2:50 AM, Justin Donaldson <[hidden email]> wrote:
> destructuring assignment is my new phrase for the day :)
>
> -Justin
>
> On Sun, Apr 25, 2010 at 3:21 PM, John A. De Goes <[hidden email]> wrote:
>>
>> "Destructuring assignment" is perfectly type safe, but haXe does not
>> support this or tuples (except as the argument to functions).
>> In this case you might do:
>>
>> using Lambda;
>>
>> ...
>>
>> var color = [r, g, b].map(function(c) { return (maxVal - c) / d; });
>>
>> var r = color[0], g = color[1], b = color[2];
>>
>> A bit ugly but gets the job done.
>> Regards,
>> John
>> On Apr 25, 2010, at 2:48 PM, Justin Donaldson wrote:
>>
>> No, unfortunately haXe doesn't support tuple assignment or
>> "autovivification" the same way that other scripting languages do.  Both of
>> these techniques are easy to use, but run contrary to the strict variable
>> typing and declaration system that haXe promotes.
>>
>>
>> You would have to use for loops (or functional mapping) + iterables.
>> Here's how I might do it, it's not as efficient as possible, but it's pretty
>> simple:
>>
>> using Lambda;
>> //...
>>         var arr = [64,48,32]; // r,g,b
>>         var maxVal = Math.NEGATIVE_INFINITY;
>>         for (a in arr) if (maxVal < a) maxVal = a;
>>         var d = 1;
>>
>>         var f = function(x:Int) return Std.int((x - maxVal)/d);  // make
>> sure it's an int?
>>         var arr_adj = arr.map(f).array(); // arr_adj has the adjusted
>> values now
>>
>>
>> The last two lines are roughly equivalent to the tuple assignment line in
>> python.  The values are just returned in an array, and you can assign them
>> to individual variables if necessary (although I rarely would do that when
>> handling rgb information).
>>
>> -Justin
>>
>>
>> On Sun, Apr 25, 2010 at 11:01 AM, Tony Polinelli <[hidden email]>
>> wrote:
>>>
>>> I dont think there is an equivalent-
>>> var dr = (maxVal-r) / d;
>>> var dg = (maxVal-g) / d;
>>> var db = (maxVal-b) / d;
>>> nice question- can this be bettered?
>>>
>>> On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
>>>>
>>>>  Hi,
>>>> what is the simplest way to transform the following python code to haXe?
>>>>
>>>> dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]
>>>>
>>>> Cordially,
>>>> Axel
>>>>
>>>> --
>>>> haXe - an open source web programming language
>>>> http://haxe.org
>>>
>>>
>>>
>>> --
>>> Tony Polinelli
>>> http://touchmypixel.com
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>>
>> --
>> Justin Donaldson
>> PhD Candidate, Informatics
>> Indiana University
>> http://www.scwn.net
>> aim: iujjd
>> twitter: jjdonald
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
>
> --
> Justin Donaldson
> PhD Candidate, Informatics
> Indiana University
> http://www.scwn.net
> aim: iujjd
> twitter: jjdonald
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

John A. De Goes

You misunderstand the purpose.

Destructuring assignment is a way to extract data from structures during assignment.

Suppose you have a tuple, (r, g, b), which is called "color". Then using DA, you can write:

   var (r, g, b) = color

The first component is extracted as "r", the second component as "g", and the third component as "b".

Similarly, if you have an array of 2 items called minMax, and you know the first one is "min" and the second "max", then you can extract the data with DA as follows:

   var [min, max] = minMax

Finally, if you have some complicated object, and just want to extract 2 fields from it (say, "width" and "height"), then DA will allow you to write:

   var {width, height} = complicatedObject

DA becomes more powerful on nested structures:

   var { window { backgroundColor (r, g, b) } }

DA uses similar machinery to the "extraction" part of pattern matching:

   case Window(_, _, Color(r, g, b))

Regards,

John

On Apr 26, 2010, at 2:54 AM, Tarwin Stroh-Spijer wrote:

Why not just pass a dynamic back that's got a TypeDef if your worried about what your function is giving back? You'll get proper typing that way too!


Tarwin Stroh-Spijer
_______________________

Touch My Pixel
http://www.touchmypixel.com/
phone: +61 3 8060 5321
_______________________


On Mon, Apr 26, 2010 at 6:19 PM, clemos <[hidden email]> wrote:
me too :)
https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment_%28Merge_into_own_page.2fsection%29
reading about it makes me think it would be a great feature, very
elegant, though I have no idea how hard it would be to implement...

+++++
Clément

On Mon, Apr 26, 2010 at 2:50 AM, Justin Donaldson <[hidden email]> wrote:
> destructuring assignment is my new phrase for the day :)
>
> -Justin
>
> On Sun, Apr 25, 2010 at 3:21 PM, John A. De Goes <[hidden email]> wrote:
>>
>> "Destructuring assignment" is perfectly type safe, but haXe does not
>> support this or tuples (except as the argument to functions).
>> In this case you might do:
>>
>> using Lambda;
>>
>> ...
>>
>> var color = [r, g, b].map(function(c) { return (maxVal - c) / d; });
>>
>> var r = color[0], g = color[1], b = color[2];
>>
>> A bit ugly but gets the job done.
>> Regards,
>> John
>> On Apr 25, 2010, at 2:48 PM, Justin Donaldson wrote:
>>
>> No, unfortunately haXe doesn't support tuple assignment or
>> "autovivification" the same way that other scripting languages do.  Both of
>> these techniques are easy to use, but run contrary to the strict variable
>> typing and declaration system that haXe promotes.
>>
>>
>> You would have to use for loops (or functional mapping) + iterables.
>> Here's how I might do it, it's not as efficient as possible, but it's pretty
>> simple:
>>
>> using Lambda;
>> //...
>>         var arr = [64,48,32]; // r,g,b
>>         var maxVal = Math.NEGATIVE_INFINITY;
>>         for (a in arr) if (maxVal < a) maxVal = a;
>>         var d = 1;
>>
>>         var f = function(x:Int) return Std.int((x - maxVal)/d);  // make
>> sure it's an int?
>>         var arr_adj = arr.map(f).array(); // arr_adj has the adjusted
>> values now
>>
>>
>> The last two lines are roughly equivalent to the tuple assignment line in
>> python.  The values are just returned in an array, and you can assign them
>> to individual variables if necessary (although I rarely would do that when
>> handling rgb information).
>>
>> -Justin
>>
>>
>> On Sun, Apr 25, 2010 at 11:01 AM, Tony Polinelli <[hidden email]>
>> wrote:
>>>
>>> I dont think there is an equivalent-
>>> var dr = (maxVal-r) / d;
>>> var dg = (maxVal-g) / d;
>>> var db = (maxVal-b) / d;
>>> nice question- can this be bettered?
>>>
>>> On Mon, Apr 26, 2010 at 3:20 AM, Axel Huizinga <[hidden email]> wrote:
>>>>
>>>>  Hi,
>>>> what is the simplest way to transform the following python code to haXe?
>>>>
>>>> dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]
>>>>
>>>> Cordially,
>>>> Axel
>>>>
>>>> --
>>>> haXe - an open source web programming language
>>>> http://haxe.org
>>>
>>>
>>>
>>> --
>>> Tony Polinelli
>>> http://touchmypixel.com
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>>
>> --
>> Justin Donaldson
>> PhD Candidate, Informatics
>> Indiana University
>> http://www.scwn.net
>> aim: iujjd
>> twitter: jjdonald
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
>
> --
> Justin Donaldson
> PhD Candidate, Informatics
> Indiana University
> http://www.scwn.net
> aim: iujjd
> twitter: jjdonald
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning to a list

Zjnue Brzavi
> Finally, if you have some complicated object, and just want to extract 2
> fields from it (say, "width" and "height"), then DA will allow you to write:
>    var {width, height} = complicatedObject
> DA becomes more powerful on nested structures:
>    var { window { backgroundColor (r, g, b) } }
> DA uses similar machinery to the "extraction" part of pattern matching:
>    case Window(_, _, Color(r, g, b))

seems to go well with multitypes too:
(source: http://alarmingdevelopment.org/?p=366)

data Attack = Magic | Melee
hit surprise defense attack = damage
  where effectiveness = power * (if surprise then 3 else 2)
      (power, damage) = case attack of
                        Magic -> (5, max 0 (effectiveness - defense))
                        Melee -> (4, 2 * effectiveness / defense)

--
haXe - an open source web programming language
http://haxe.org