Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
weird
Jun 4, 2012

by zen death robot

qntm posted:

then what's this?

code:
error: function definition is not allowed here
        int bar(int b){
on my compiler

Adbot
ADBOT LOVES YOU

Hunter2 Thompson
Feb 3, 2005

Ramrod XTreme
Does C++ have a way to get string representations of enums yet?

brap
Aug 23, 2004

Grimey Drawer
an inner function can be convenient because you can close over the locals declared above it in the containing function.
this is useful in python because the lambda syntax is limited to one liners and linters will balk and tell you to use an inner function instead of a lambda

qntm
Jun 17, 2009
I always thought of a lambda as just a special case of inner function which is why I'm puzzled to see people talking about them as if they're different things

VikingofRock
Aug 24, 2008




meatpotato posted:

Does C++ have a way to get string representations of enums yet?

Sure does!

C++ code:
#include <iostream>
#include <string>

enum class Foo {
	YOS,
	POS
};

std::string to_string(const Foo foo) {
	switch (foo) {
		case Foo::YOS: return "YOS";
		case Foo::POS: return "POS";
	}
}

std::ostream& operator<<(std::ostream& os, const Foo foo) {
	return os << to_string(foo);
}

int main() {
	std::cout << Foo::YOS << Foo::POS << std::endl; //prints "YOSPOS"
}

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!

qntm posted:

I always thought of a lambda as just a special case of inner function which is why I'm puzzled to see people talking about them as if they're different things

lambdas are just anonymous functions, so I guess it's more accurate to say it's the other way around? usually the only difference is that you have a more expression-like in-line syntax (like \x => ... or w/e) so it's more like every other variable declaration. that means you don't have to assign them to a name if you are just going to return them, but inner functions are mostly equivalent otherwise.

Hunter2 Thompson
Feb 3, 2005

Ramrod XTreme

VikingofRock posted:

Sure does!

C++ code:
#include <iostream>
#include <string>

enum class Foo {
	YOS,
	POS
};

std::string to_string(const Foo foo) {
	switch (foo) {
		case Foo::YOS: return "YOS";
		case Foo::POS: return "POS";
	}
}

std::ostream& operator<<(std::ostream& os, const Foo foo) {
	return os << to_string(foo);
}

int main() {
	std::cout << Foo::YOS << Foo::POS << std::endl; //prints "YOSPOS"
}

C++ :golfclap:

Here's my "improvement" but I don't know if this is horrible or good:

C++ code:
#include <iostream>
#include <string>

// Add new Foos here, not elsewhere. Good luck.
#define FOO_LIST \
    X(YOS) \
    X(POS) \
    X(BITCH)

enum Foo {
#define X(x) x,
    FOO_LIST
#undef X
};

std::string to_string(const Foo foo) {
	switch (foo) {
#define X(x) case x: return #x;
                FOO_LIST
#undef X
	}
}

std::ostream& operator<<(std::ostream& os, const Foo foo) {
	return os << to_string(foo);
}

int main() {
	std::cout << Foo::YOS << Foo::POS << Foo::BITCH << std::endl; //prints "YOSPOSBITCH"
}
Edit: I can't figure out how to get it to work with enum class but there you go

Hunter2 Thompson fucked around with this message at 20:12 on Jan 26, 2016

VikingofRock
Aug 24, 2008





:golfclap:

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

qntm posted:

then what's this?

a gcc extension with wacky semantics that requires runtime code generation and inherently leaks memory (it's a closure that can be passed around as a normal function pointer, so it has to generate a trampoline which passed in the captured values).

Jeffrey of YOSPOS
Dec 22, 2005

GET LOSE, YOU CAN'T COMPARE WITH MY POWERS

Plorkyeran posted:

a gcc extension with wacky semantics that requires runtime code generation and inherently leaks memory (it's a closure that can be passed around as a normal function pointer, so it has to generate a trampoline which passed in the captured values).
yeah looking at the ridiculous assembly gcc generates might be the best part - there's definitely gcc extensions in there (computerd gotos, declared labels, inner functions sort-of closing over things, etc)

Jeffrey of YOSPOS fucked around with this message at 21:53 on Jan 26, 2016

suffix
Jul 27, 2013

Wheeee!
the trampoline is stored on the stack, so it should not leak memory but is only valid until the function returns

i wondered how that would work with the nx flag, turns out gcc just disables it. if you have this anywhere in your program your stack doesn't get no-execute protection

Soricidus
Oct 21, 2010
freedom-hating statist shill

suffix posted:

the trampoline is stored on the stack, so it should not leak memory but is only valid until the function returns

i wondered how that would work with the nx flag, turns out gcc just disables it. if you have this anywhere in your program your stack doesn't get no-execute protection

brb, adding gcc extensions to debian server packages

Bloody
Mar 3, 2013

qntm posted:

so what's the difference between that and what you were talking about?


then what's this?

idk i guess just syntax so that your lambda/inner declaration cannot occur as a result of like
void foo()
{
... stuff
...
// whoops hosed up a closing brace
void bar()
{
more stuff...
}
}
because there is probably at least one case where that has happened and been the most ludicrous runtime bug to solve

Bloody
Mar 3, 2013

languages that are not hosed up horrorshows give you cleaner ways of declaring this:
code:
            Func<int, int> a = (int foo) =>
                               {
                                   return foo 	- 1; 
                                   
                               };
and you have a named method-scoped function that is very easy to differentiate from a non-scoped function and can capture local scope and you can pass it around and all that poo poo

the only thing that is not flawless about this is that you have to manually specify Func<T> you can't just var away the whole thing

gonadic io
Feb 16, 2011

>>=
All I'm seeing is an argument for whitespace sensitivity

Soricidus
Oct 21, 2010
freedom-hating statist shill

gonadic io posted:

All I'm seeing is an argument for whitespace sensitivity

or auto-formatting on commit, which i guess is essentially the same thing but less enforced

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

autoformatters own because people can stop bitching about indentation and line breaks and whatever

fritz
Jul 26, 2003

https://twitter.com/gavinjoyce/status/691773956144119808

Shaggar
Apr 26, 2006
lmao. that's so loving javascript

bobbilljim
May 29, 2013

this christmas feels like the very first christmas to me
:shittydog::shittydog::shittydog:

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison
npm install verbose is loving terrifying

Arcsech
Aug 5, 2008

uncurable mlady posted:

npm is loving terrifying

JewKiller 3000
Nov 28, 2006

by Lowtax
who exactly is it who thought "man, the way i just list all my dependencies in this file and maven+eclipse take care of it for me, even telling me when version numbers are wrong... imagine if we had that, but without any of the good parts, and in javascript!"

Shaggar
Apr 26, 2006
p-langers

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
0% chance anyone involved with npm had ever used maven

Chamook
Nov 17, 2006

wheeeeeeeeeeeeee

Bloody posted:

the only thing that is not flawless about this is that you have to manually specify Func<T> you can't just var away the whole thing

One of my few good colleagues tells me that this is because the c# compiler can't determine if it should be a Func or an expression, so now we have a handy helper for it:

code:

public static Func<T> Func(Func<T> f) => f;

And then one for all the different number of parameters on the Func, and now we can use var with lambdas.

Valeyard
Mar 30, 2012


Grimey Drawer

MALE SHOEGAZE posted:

react's api exposes like 6 functions. it's the definition of simple

Just use backbone with underscore for templating

Valeyard
Mar 30, 2012


Grimey Drawer

This is pretty bad, but considering there is a toggle for the progress bar in the first place implies they already know there is a performance hit. What other reason would you have for allowing it to be turned off?

cowboy beepboop
Feb 24, 2001

don't defend npm it's unseemly

tef
May 30, 2004

-> some l-system crap ->

Valeyard posted:

This is pretty bad, but considering there is a toggle for the progress bar in the first place implies they already know there is a performance hit. What other reason would you have for allowing it to be turned off?

(this is kinda common behaviour for cli tools though?)

FamDav
Mar 29, 2008
I thought the IO being non-blocking was the point

tef
May 30, 2004

-> some l-system crap ->
:golfclap:

pseudorandom name
May 6, 2007

progress bar toggles and the like are for when you want to pipe the output to a file and don't want the control codes messing it up

distortion park
Apr 25, 2011


https://googleblog.blogspot.co.uk/2016/01/alphago-machine-learning-game-go.html?m=1

Looks like computers are now better at go too. The way they did it is cool too. Rip humans

distortion park
Apr 25, 2011


Literally all we're good for is loving. Ian m banks was right.

distortion park
Apr 25, 2011


The previous post should be considered forward dated to 2025

fritz
Jul 26, 2003

avatar the color of a tv tuned to a dead channel

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

pointsofdata posted:

https://googleblog.blogspot.co.uk/2016/01/alphago-machine-learning-game-go.html?m=1

Looks like computers are now better at go too. The way they did it is cool too. Rip humans

that looks way more promising and impressive than facebook's attempt

Adbot
ADBOT LOVES YOU

Nippashish
Nov 2, 2005

Let me see you dance!

pointsofdata posted:

https://googleblog.blogspot.co.uk/2016/01/alphago-machine-learning-game-go.html?m=1

Looks like computers are now better at go too. The way they did it is cool too. Rip humans

https://storage.googleapis.com/deepmind-data/assets/papers/deepmind-mastering-go.pdf

  • Locked thread