Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 437 438 [439] 440 441 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 841219 times)

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6570 on: November 09, 2014, 12:04:20 pm »

a lambda is just a function which doesn't have a name. other than that they're just a normal function. But because they don't have a name they have to be written out fully where they're used instead of using the function name. Clearly, if a function is used more than once then you might as well just make it a normal function rather than a lambda.

Here's a C++ example:

Code: [Select]
#include <algorithm>
#include <cmath>
void abssort(float* x, unsigned n) {
    std::sort(x, x + n,
        // Lambda expression begins
        [](float a, float b) {
            return (std::abs(a) < std::abs(b));
        } // end of lambda expression
    );
}

Normally, the std::sort function takes a pointer to a function used to define the comparision operation for the data type you created. Here, rather than pass a pointer to that function the coder has defined a "nameless" c++ function (it's actually "named" []) entirely within the parameter list of the std::sort function. Basically the entire body of the nameless lambda function is a parameter inside the other function call.
« Last Edit: November 09, 2014, 12:21:00 pm by Reelya »
Logged

DJ

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6571 on: November 09, 2014, 02:35:21 pm »

Why would you ever want to use that?
Logged
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...

Arx

  • Bay Watcher
  • Iron within, iron without.
    • View Profile
    • Art!
Re: if self.isCoder(): post() #Programming Thread
« Reply #6572 on: November 09, 2014, 02:36:50 pm »

I think if you want to use a function as an argument for another function, but only once and so you don't want the function cluttering up your code. I can't say I see the need myself, but I'm not a professional.
Logged

I am on Discord as Arx#2415.
Hail to the mind of man! / Fire in the sky
I've been waiting for you / On this day we die.

DJ

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6573 on: November 09, 2014, 02:39:17 pm »

It looks more cluttering to me to write out a whole function inside an argument list than to use a normal function.
Logged
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6574 on: November 09, 2014, 02:52:13 pm »

Ruby has great examples of lambda's.  Lets say you want to sort a list of hashes by property 'b'.

Code: [Select]
list = [{'a' => 3,'b' =>3},{'a' => 3,'b' =>1},{'a' => 3,'b' =>8},{'a' => 3,'b' =>2}]


newlist = list.sort() {|elem1,elem2|  elem1['b'] <=> elem2['b']  } #Spaceship operator is a magic amazing comparator used for sorting.

Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6575 on: November 09, 2014, 03:43:13 pm »

Because functional programming. Let's use a classic 'reduce' operation as an example.
Code: [Select]
int reduce( int ListOfStuff[], int AmountOfStuff, foo(int, int) operation )
{
   int Output = ListOfStuff[0];
   for( int i = 1; i < AmountOfStuff; i++ )
   {
      Output = operation( Output, ListOfStuff[i] );
   }
}

Now, say we wanted to do this general pattern, we could make this relatively simple function do an absurdly large number of things
Code: [Select]
// Sum all the values
reduce( ListOfStuff, AmountOfStuff, lambda function(A,B){ return A + B; });

// Multiply all the values
reduce( ListOfStuff, AmountOfStuff, lambda function(A,B){ return A * B; });

Alright, so now I've explained why function pointers are useful. So what's special about lambdas? Well, for one, you don't need to dance around naming conventions or define these tiny functions elsewhere (and thus potentially obscuring what is going on), then wrangle up function pointers for them and so on.

There's also another interesting factoid about them: static variables.
Spoiler (click to show/hide)

In C++, you can also capture scope of things outside itself; which is really the big deal but into which I have not delved.

So it has its uses, though like most stuff in C++, there's multiple other ways of doing it; but it does have its uses.
« Last Edit: November 09, 2014, 03:53:00 pm by alway »
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6576 on: November 09, 2014, 08:11:50 pm »

Most of the use I find is with capturing variables in outer scopes.

Code: [Select]
int[] myArray = GetInts();
int myValue = GetWhatImLookingFor();
int numberOfMyValue = myArray.Count(x => x == myValue); // lambda

So unlike a normal function, I can access myValue and I dont have to define it as a class property/static or any workarounds like that. Plus I can avoid the tired old "foreach thing, if its myValue, add one to some counter variable etc", and using a lambda with Count is more readable and immediately clear what is going on.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6577 on: November 09, 2014, 10:07:05 pm »

Syntax error.

Also, not a lambda.
Yeah, I didn't know what a lambda is. Also, how does it give a syntax error?
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: if self.isCoder(): post() #Programming Thread
« Reply #6578 on: November 09, 2014, 11:27:53 pm »

Spoiler (click to show/hide)

I cannot get that to work in firefox, under either window or linux. As far as I can tell, that kind of thing only works in inkscape. I'll post my code below, just in case.

Spoiler (click to show/hide)
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #6579 on: November 10, 2014, 02:49:24 am »

How does web security even work x.x

I've been trying to make my webapp-in-progress a modicum of security. My current scheme is encrypting the password client-side and sending it to a server for verification, then if it's correct sending back a random string of letters that serves as a token to be passed with future POST requests. But what is there to say that the hashed password won't be intercepted and then used in future attempts to authenticate falsely?! D: Or some malicious person named Eve could inject javascript into the page somehow and strip off the password. :|
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6580 on: November 10, 2014, 02:56:35 am »

How does web security even work x.x

I've been trying to make my webapp-in-progress a modicum of security. My current scheme is encrypting the password client-side and sending it to a server for verification, then if it's correct sending back a random string of letters that serves as a token to be passed with future POST requests. But what is there to say that the hashed password won't be intercepted and then used in future attempts to authenticate falsely?! D: Or some malicious person named Eve could inject javascript into the page somehow and strip off the password. :|

Essentially there is always the risk that someone can sniff the authentication token and steal the session, or figure out some way of causing your client to leak the session and or authentication information

https://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project is a good starting point to identify common attacks against web clients that can leak either session or authentication information.

In summary, you can't always protect against token sniffing, To my mind, having and using HTTPS and really safely handling user input (escaping before displaying on page, using parametrized queries) are the key things to get right in order to minimize the risks.
Logged
alternately, I could just take some LSD or something...

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #6581 on: November 10, 2014, 03:17:53 am »

What are the two things you mentioned--escaping before displaying on page and parametrized queries? Those are as good as alien to me.

Is HTTPS gained by simply enabling SSL on one's site/server?

edit: I realized what escaping is.
« Last Edit: November 10, 2014, 03:19:55 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6582 on: November 10, 2014, 03:31:07 am »

What are the two things you mentioned--escaping before displaying on page and parametrized queries? Those are as good as alien to me.

Is HTTPS gained by simply enabling SSL on one's site/server?

edit: I realized what escaping is.

Parameterized queries is a style of accessing an SQL database. I don't know if you're using one or not but I'll try to explain.

It used to be for the longest time that you would write SQL queries acting on user input by simply adding strings together, i.e SELECT * FROM USER WHERE NAME='+username+';  with the username value provided by a user form input.

The problem was, malicious attackers would simply give their username as ';SELECT * FROM USER, the database engine would then interpret this as two queries, the first would be SELECT * FROM USER WHERE NAME='' and the second would be SELECT * FROM USER as you can see the second one doesn't do what you might expect and just selects all users, instead of one with specific username.

You could also do more active attacking type things, like giving UPDATE USER SET password = 'knownpassword'; as your username and updating all the users to use a password the attacker knows.

At first the response was just to develop methods to escape the user data, but this quickly became error prone and dangerous and often would fail on very unexpected edge cases.
In response to this they developed parameterized queries, they look like this SELECT * FROM USER WHERE NAME = ?
You would then pass the user data in at runtime to the database like this query_db(SELECT * FROM USER WHERE NAME = ?, username) //this changes based on your chosen language and db API

The database would then know to always treat the username value as data and would never execute it as part of the SQL commands, preventing the user from attacking your site in this manner.

HTTPS is indeed gained by enabling SSL, although technically the current standard cryptographic suite used for HTTPS is in fact TLS. There are plenty of guides to enabling SSL/TLS available for most common webservers. You don't even have to purchase a proper SSL cert, if it's just a testing application you can simply use a self signed certificate.

Logged
alternately, I could just take some LSD or something...

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6583 on: November 10, 2014, 03:41:25 am »

So, I've been learning Python over the last few months, and I knew I would do some things badly.  And for the most part I've recognized them and stopped doing them.  But I just realized that one thing has just totally slipped past me.  Which is that, despite knowing the whole time that
Code: [Select]
for i in list:works, I've been writing it as:
Code: [Select]
for i in range(len(list)):Because that's what I've done in the past, I think there was some tutorial that did it that way, and it just... didn't occur to me to ask why if there was a better way.  WTF me.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6584 on: November 10, 2014, 03:48:12 am »

So, I've been learning Python over the last few months, and I knew I would do some things badly.  And for the most part I've recognized them and stopped doing them.  But I just realized that one thing has just totally slipped past me.  Which is that, despite knowing the whole time that
Code: [Select]
for i in list:works, I've been writing it as:
Code: [Select]
for i in range(len(list)):Because that's what I've done in the past, I think there was some tutorial that did it that way, and it just... didn't occur to me to ask why if there was a better way.  WTF me.

These will have different results, assuming I'm recalling my python correctly and it may be that you need one style or the other
Code: [Select]
for i in list:For each iteration this will make i an instance of whatever object is in list for that current step of the iteration

Code: [Select]
for i in range(len(list)):Whereas this will make i an index into the list as range returns a list of number from 0 to len(list), so to get the actual item from the list you'd have to do
Code: [Select]
item = list[i]
Logged
alternately, I could just take some LSD or something...
Pages: 1 ... 437 438 [439] 440 441 ... 796