Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Post Assign

Name: OP !9kbJjvwmwE 2012-12-27 14:19

I find post/pre-increment/decrement very, very useful (I'm refering to x++, ++x etc.). Often it's what saves me from having to declare redundant variables or, god forbid, redundant functions.
The time has come in which I wonder whether this kind of magic is possible with assignment as well (in any language).
In the following JavaScript code, the expression in the parentheses returns the value assigned to the variable x (or the value of x after it had been assigned the new value):
(x = 1)
Is there a language that provides a way to write a similar expression which will return the value of x *before* it's assigned the new value?
An imaginary example would be:
var x = "before";
var y = (x = "after");

in which y will be assigned the value "before" (instead of "after", as it would if this was run as JavaScript).

Better yet, in a language that does *not* provide a native syntax to achieve this, can you think of a hack that do it? For example, in JavaScript, the best way I can think of to achieve this without declaring additional variables is:
var x = "bef";
var y = [x, x = "aft"][0];

Here in the second line I declare an anonymous Array with the first element set to x and the second element set to the result of the expression setting x to the new value, and then retrieve the first element of that array (the second element is irrelevant here) and assign it to y.

In the next post I will explain why such a procedure would ever be useful for anything (which you are likely to ask).

Name: Anonymous 2012-12-27 14:32

Just write a macro.

Name: OP !9kbJjvwmwE 2012-12-27 14:54

I just realized that in my example I actually did create an additional variable, although nameless. I thought of this example /after/ typing "the best way I can think of".

Anyway, the reason I came to believe such a procedure can be useful is a particular while loop I was dealing with. Consider the following (simplified) AS3 example:


// An object containing a private array of Blah instances.
// Provides a method to retrieve instances by index, which
// returns null if the index is out of range.
var bh:BlahHolder = new BlahHolder;

// An additional Blah instance I cannot push into bh's array
var y:Blah = new Blah();


var x:Blah;
var i:uint = 0;
while (x = (bh.getBlah(i++) || y))
{
    ... stuff involving x ...
}

In this example, in each loop recursion, x will be assigned the next Blah instance in bh's array, and when done with the last element, x will be assigned y. Alas, this will not break the loop. The loop will henceforth continue to run with x being repeatedly set to y.
A possible solution will be to add the somewhat minimalist line
if (x === y) break;
Which is nice.
Another solution is to change the while line to:
while (x = (bh.getBlah(i++) || ([y, y = null][0]) ))
Which, think about it it now, isn't much sleeker than the former solution.
I'm also guessing that the latter is more resource-consuming. Am I wrong about that?

Can you come up with a better solution?

Name: Anonymous 2012-12-27 15:10

>>2
The word "macro" can refer to many things I can think of. What do you mean by that? To what language does that apply? Is this a syntax thing, or an input macro (as in, a macro to write predetermined code blocks, which is irrelevant to this post)?

Name: Anonymous 2012-12-27 15:15

>>4
You have yet to achieve satori. Please leave and do not return until you have. Also learn Sympta.

Name: Anonymous 2012-12-27 15:34

Is there a language that provides a way to write a similar expression which will return the value of x *before* it's assigned the new value?
Yes, posix shell

Name: OP !9kbJjvwmwE 2012-12-27 15:42

>>5
Sympta returns nothing relevant on any search engine I tried, even when coupled with "language", "code" or when filtering the words "drug", "vitamin" and "dosage". The closest to relevant results I could find was related to hardware, namely mobile computers. Would you care to enlighten, or at least to inform me about what you refer to?

Name: Anonymous 2012-12-27 15:48

>>7
Sympta
Are you fucking braindead? It's Symta, fagshit.

Name: Anonymous 2012-12-27 15:52

>>7
That's surprising. I guess google doesn't index this site. In the mean time, learn lisp.

(defmacro := (lval rval) (let ((temp (gensym))) `(let ((,temp ,lval)) (setf ,lval ,rval) ,temp)))

Name: >>5 2012-12-27 15:54

No >>8-san, I am the fagshit.

Name: Anonymous 2013-09-01 13:19



                 _,,,.........,,,,_
            ,..::''"´:::;::::'´::::::::::`゙'::..、
          .,.::'´::__:::::;::´::::::::::::::::::::::::::::::\
        /:::::''´:::::`::::::::: ̄`゙':::、::::::::::::::::::ヽ,
       .,.:'::/::::::::::::::::::ヽ;::::::::::::::::::\:::::::::::::::::':,
      /::::::::::;:::/ `ヽ,::::::ト、:::::::::::ヽ;:::::「ヽ:::::::::::',
      ,':::/:::::::l/     ';:::::} \::::::::::';::ノ ,ゝ、::::::::l
     ;:::::':::::::;' 二_   |:::,    ヽ;::::r' イ ノ:::::::::;
     l::::i::::::::{ィ´r)ヾ、  レ' _,,.二、ハ::;>‐r'::::::::::/
     |;:::':::::::lr‐弋_リ'r-、-ァ´r) l!`Y!:::::i:::::';::::::::/
     |::,:::、::::'、、" _.ノ   、 ゝ-‐゚メ::::;'::::::|:::::':::::',
      ,::'、::\;ゝ   .     ー "升:/l::::::;:::::::::::::ヽ
      }::/.}::::人    、     /::レ'ノ:::;:'::|:::::::::::::::::\
      ,':/ ノ:;:':::::::\  `  ´   ,:'::::::://:::::;:::::::::::::::::::::::':,
     {:{ ,'::;::::::::/:::/ヽ.,_,,..  イ':::::::/´::::::::/:::::::::::::::::ノ:::::ノ
     r {/::::::::;':: /`ヽ ハ}  |::::::;:':::::::::::/::_;;:: -‐ i''" ̄ヽ
     },:':::::::::::{ /  '-く`''ー-r!:::::{--‐ ''i"´    |    ,
    /::{::::::: r、/    ヽ   l:::::',:::::::::::|__,,... -!_   }
   / ヽ/ヽ:r' 、\    rノー- ,::::::} ̄ ̄|::::::::::::::::::;'>ヽ  ;
  {   ;':::::: }   \\ ノ'、::::::ノ::::;:'__ .ノ::::::::::::/    ∨
   ヽ /7ー、/    \ Y  ̄ /::/     ̄`ア      ',

Name: Anonymous 2013-09-01 14:50



わ からない事は上白沢先生に相談しよう
きっと答えてくれないが気休めにはなるぞ!

┌───────────────────────────
|質問:
└───────────────────────────
       iヽ、
     ____」____ゝ'´/':\ /|        ハクタクシステム Ver.1.00
     ヽ. _/___/___::\/        ____________________________________
      <Y---r二二二二ハ.フ.     |
       ,.L__:::::|::::i::::L-」::l::7>       | 質問の答え:
     /     ̄' 'ー---─' `ヽ.     |
    ./   ,.      ⌒ヽ.     ',     |
   ,'  / / ‐/- i:::::::::::i   `ヽ!.    |
    i   i  !/ア!-;!、ハ:::::::ハ-!、.i . i.   |
   |  ハ .! ! !__r! ' レ' ァ!‐;!、i i |   |
   |  i レ' ! "    ,   !.rハ/レ'ヽ!  <
   |  ! .|ハ、   、..__   "/:::| .|     l___________________________________
    | ./i  |::::!>、._  _,,.. イ .|::::| |  ,.、    __
   .| ./ !,..-|::::|'´/ヽ二ン`7ー、|:::|. | i !,,.-''" ノ
   |/ /  .|::::|〈 イ::::V:::::::::!〉:`i:::ト、! .! ヽ -=ニ__
  .!r〈_   !,ヘ!:::ヽヘハ::::;イノ:::::;ヽ! ヽ,!   !  ‐--,' 「`ヽ_/`i
  !.ゝr、__r、i::::::::::::/ ハ Y::::::::::i  r/ ヽ   ー,--'  ,'ゝ-^ー‐'´ヽ.
  !/  、/ヽ;::::::〈  ハ ',:::::;イ、_ /!   7`  ̄    L!_!ハ!_ハ_i.」 i
  .〈    ヽ、/:::::::::ヽ/Vヽ、〉::::! !`ー,ヽ-'       从゚ ー ゚く」 | |
  .レ\ ,.イ`ヽ:::::::::::::ハ::::::::::::ハ、 |ノ          }>!|,イ〒イヽ! |<{

Don't change these.
Name: Email:
Entire Thread Thread List