vlatkozelka Posted January 15, 2015 Posted January 15, 2015 so i was studying for web services exam , and i stumble upon this while looking at some Xquery code for the first time : for $p in distinct-values(document("bib.xml")//publisher) let $a :=avg(document("bib.xml")//book[publisher = $p]/price) return <publisher> <name>{ $p/text() }</name> <avgprice>{ $a }</avgprice> </publisher> :returns all publishers with their average book price: and then i spit my tea all over the keyboard... apparently u can return multiple times ? hell u can loop it O.o . I've always were more into programming(java , c ...) than scripting for multiple reasons like lack of usual punctuation and brackets and the stupid $ sign and the stupid := , but THIS , this just draws the line for me , i feel like i dont wanna code anymore seems stupid i know , but i felt like i have to rewire my brain in order to accept such a thing just to pass one exam lol .
woolwind Posted January 15, 2015 Posted January 15, 2015 I do feel your pain. That is just wrong. though I presume what is actually happening is it returns once at the end of the for loop and hands back a list or some such containing all the values. :-p
BillieJoe67 Posted January 15, 2015 Posted January 15, 2015 Just think of it a python's yield I guess, it's (generally) more efficient and uses less RAM than filling an array and returning it (the return you like )
vlatkozelka Posted January 16, 2015 Author Posted January 16, 2015 Just think of it a python's yield I guess, it's (generally) more efficient and uses less RAM than filling an array and returning it (the return you like ) how is it more ram efficient ? when u return an array ur simply returning the pointer to the first item , i doubt it can be more efficient than that Oo
BillieJoe67 Posted January 16, 2015 Posted January 16, 2015 But the pointer is pointing at a massive memory chunk full of whatever you're returning.. With yield you're just using the same memory (effectively) over and over to return different objects Here's a fairly terrible example /* Example one, allocate 1000 ints, initialise them and return */int* dem_ints = malloc(1000*sizeof(int));for (int i = 0; i < 1000; ++i) { dem_ints[i] = i;}return dem_ints;/* Example two, pretend C has a yield statement :v */for (int i = 0; i < 1000; ++i) { yield i;}
vlatkozelka Posted January 16, 2015 Author Posted January 16, 2015 But the pointer is pointing at a massive memory chunk full of whatever you're returning.. With yield you're just using the same memory (effectively) over and over to return different objects Here's a fairly terrible example /* Example one, allocate 1000 ints, initialise them and return */int* dem_ints = malloc(1000*sizeof(int));for (int i = 0; i < 1000; ++i) { dem_ints[i] = i;}return dem_ints;/* Example two, pretend C has a yield statement :v */for (int i = 0; i < 1000; ++i) { yield i;} still the same shit ... its not like ur gonna be using less ram . And about that "using same blocks " thing ... free() anyone ? Its the same for both , but the syntax is horrible with for{return ; } thats all im saying even worse later as i was studying i find this shit : return if condition() {}else {} ... its just looks bad to ma brain lol anyways i did the exam today ... finished it and still had 23 minutes left It started out as multiple choice and bs , then he started asking for code im like
BillieJoe67 Posted January 16, 2015 Posted January 16, 2015 Of course it's going to use less ram, it'll use 999*sizeof(int) less ram (not really but we'll pretend) http://www.jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now