|
nielsm posted:Inner classes. Anonymous classes. Woah. That's neat! I haven't gotten too indepth with C++ yet, used to python mainly, but does that class-inside-class functionality remain there? If not, what would be the best way to do it there?
|
# ? Dec 8, 2012 18:23 |
|
|
# ? May 29, 2024 16:02 |
Jewel posted:Woah. That's neat! I haven't gotten too indepth with C++ yet, used to python mainly, but does that class-inside-class functionality remain there? If not, what would be the best way to do it there? C++ does allow you to declare types within class/struct declarations, but it doesn't have the "inner class" semantics of Java. (It's comparable to a "static inner class" in Java, those don't have the implicit reference to the creating object.) Rather, declaring a type inside a class in C++ is effectively just a namespacing of it. On the other hand, C++ lets you make a clearer physical distinction between public interface (header files) and implementation (cpp files) and add lots of private details in the implementation. If your header file only declares an abstract class and maybe a couple of global functions that return references (smart pointers) to instances of that abstract class, then your implementation file(s) can declare multiple additional classes that all derive from the abstract class in the public interface, and those get returned from the publically declared functions. An important tool in making that kind of public interface/private implementation in C++ is using the anonymous namespace: C++ code:
Another thing you can use in C++ is "friend" declarations, that allows a class to declare that functions not member of the class are still allowed to access the protected and private fields of the class. It's usually something you should use sparingly. (I can't remember when I last time had to use a friend declaration.)
|
|
# ? Dec 8, 2012 19:02 |
|
Keep in mind this is just sort of a workaround for anonymous functions and lambdas, which I think C++ does support natively.
|
# ? Dec 8, 2012 20:43 |
|
Suspicious Dish posted:Keep in mind this is just sort of a workaround for anonymous functions and lambdas, which I think C++ does support natively. It does in C++11. The stomach example could be done like this: C++ code:
mjau fucked around with this message at 21:17 on Dec 8, 2012 |
# ? Dec 8, 2012 21:14 |
|
Not a programming question but a CoC request thingy. Mjau's post contains some c++ specific syntax highlighting, which is implemented with code=cpp. What languages are supported with this? What are their keywords? Maybe this info could go into the CoC faq thread and/or the BBcode link at the left of the 'make a post' page.
|
# ? Dec 8, 2012 21:24 |
|
From the SAVB changelog, linked at the bottom of every page:code:
|
# ? Dec 8, 2012 21:34 |
|
Who do we have to talk to about getting new languages added to that list? There's a lot of discussion regarding R and Matlab in threads on scientific/numerical computing, and having syntax highlighting would be a godsend.
|
# ? Dec 8, 2012 22:23 |
|
Choochacacko, but there's probably better things for him to be doing right now.
|
# ? Dec 8, 2012 22:28 |
|
Mario posted:From the SAVB changelog, linked at the bottom of every page: Hey, thanks.
|
# ? Dec 9, 2012 00:05 |
|
mjau posted:List<Nutrient> contents; What do you mean? What is it then? Also, since I think I never worked it out last time I was coding, how would I go about making a list of child classes in C#, like, uh. If I had a Animal, Avian::Animal, Dog::Animal, Cat::Animal, and Bird::Avian, how would I make a list of Animals that can store every one of those extended classes (Which would be, uhm, Dog, Cat, and Bird, I think. Avian and Animal would be.. Interfaces? Which means they can't be used by themselves? I'm bad with terminology, correct me if I'm wrong!)
|
# ? Dec 9, 2012 07:33 |
|
nielsm posted:Another thing you can use in C++ is "friend" declarations, that allows a class to declare that functions not member of the class are still allowed to access the protected and private fields of the class. It's usually something you should use sparingly. (I can't remember when I last time had to use a friend declaration.) "Hey, Bjourne, I got this great idea. Let's build up walls of encapsulation, and then allow people to poke right through it because they're BFFs forever!" "Great idea, I'll get right on it! Thanks, BFF." "No probs. I've got your back."
|
# ? Dec 9, 2012 08:38 |
|
It doesn't really break encapsulation: classes declare their friends, and friendship isn't transitive or anything. The only thing that totally destroys meaningful access control without an ODR violation (and if you're violating ODR anyway, you might as well just use your own class definition making everything public) is if you befriend a class template. The real problem is the C compilation model; the logical way to do access control is file-by-file, but that doesn't really work in a world that get modularity and re-use from textual inclusion.
|
# ? Dec 9, 2012 09:06 |
|
Jewel posted:What do you mean? What is it then? mjau fucked around with this message at 09:29 on Dec 9, 2012 |
# ? Dec 9, 2012 09:27 |
|
rjmccall posted:It doesn't really break encapsulation: classes declare their friends, and friendship isn't transitive or anything. It seems absurdly silly to build up walls of access control, and then poke right through them.
|
# ? Dec 9, 2012 09:46 |
|
Suspicious Dish posted:It seems absurdly silly to build up walls of access control, and then poke right through them. "I personally can't think of a use case for this feature, hence the feature is and will always be terrible". It makes writing unit tests a whole lot less clunky
|
# ? Dec 9, 2012 10:36 |
|
More than that, sometimes the right unit of encapsulation is not a single type — maybe you have several types that work in tandem, and nesting one inside the other isn't suitable. Java's default access control serves the same purpose. A good example would be a tightly-coupled serialization library which you'd like to have access to stuff you wouldn't really want to otherwise expose. Actually, Java's access control is a lot nicer than C++'s; you can build up an entire package-internal API in Java and still enforce class-level encapsulation, whereas friendship in C++ is really invasive. But something with invasive access can be careful and use less than is granted, whereas without any way to couple things at all you're just hosed. The other use for friends — and I think this was a lot of the original motivation — is to let classes declare coupled free functions, most notably operators. Operators have grown, er, organically in C++, so this is less important now, but at one point this was seen as a major gain.
|
# ? Dec 9, 2012 11:18 |
|
nielsm posted:Inner classes. Anonymous classes. Took me a while to wrap my head around all of this but it was all very helpful, many thanks.
|
# ? Dec 10, 2012 01:41 |
|
I've gotten myself into a position where I'm in charge of a small web application that talks to other internal services within my company. This has also brought with it the need to manage the project, expectations for the platform team, and communication with other teams, as well as the various technical aspects of keeping the application up-to-date, staying ahead of show-stopper bugs, and supporting the application's use. Is there any good literature about this kind of technically-oriented project management?
|
# ? Dec 10, 2012 04:40 |
|
I'm having issues coming up with the best way to sort an array of strings that have the following format:code:
|
# ? Dec 10, 2012 08:41 |
|
Your language probably has a way to say "Sort this sequence of things, using this function that I'm going to provide whenever you need to compare two of them". Figure out what that is, and you've reduced the problem to "How can I compare two of these strings and tell which one is 'bigger'?" The fiddly way is to start at the end of the string, and manually convert it to an integer character by character until you find a non-digit character. The lazy way is to use a regex or something to pull out the last series of digits, and then convert that to a number or whatever. Pick whichever one you feel like, really.
|
# ? Dec 10, 2012 09:38 |
|
Master_Odin posted:I'm having issues coming up with the best way to sort an array of strings that have the following format: If python, I'd use a lambda key: Python code:
pre:Original array: 8 9 0.3 10 2 6 8.2 80 6 3 7.7 70 0 2 2.2 17 9 2 7.9 2 6 1 0.1 90 7 5 8.3 57 9 6 5.8 97 2 7 3.0 25 0 1 0.9 47 ------ Sorted array: 9 2 7.9 2 8 9 0.3 10 0 2 2.2 17 2 7 3.0 25 0 1 0.9 47 7 5 8.3 57 6 3 7.7 70 2 6 8.2 80 6 1 0.1 90 9 6 5.8 97 Jewel fucked around with this message at 10:05 on Dec 10, 2012 |
# ? Dec 10, 2012 09:51 |
|
What is a lambda?
|
# ? Dec 10, 2012 10:21 |
|
tarepanda posted:What is a lambda? A lambda's kinda.. a one line function, I guess. Python code:
Python code:
|
# ? Dec 10, 2012 10:25 |
|
Jabor posted:Your language probably has a way to say "Sort this sequence of things, using this function that I'm going to provide whenever you need to compare two of them". edit: I realize I should have posted this in the Java thread and will move myself over there. I rewrote my post so many times I completely missed that I got to the point where I wasn't looking at an idea specific thing, but rather language specific. My bad. Master_Odin fucked around with this message at 10:29 on Dec 10, 2012 |
# ? Dec 10, 2012 10:27 |
tarepanda posted:What is a lambda? Stay a while and listen! Although in the context of Python specifically, it's an anonymous function declared with the "lambda" syntax, and Python's lambda functions can only consist of a single expression whose evaluation result is returned.
|
|
# ? Dec 10, 2012 10:29 |
|
nielsm posted:Stay a while and listen! Oh, yeah, that's super neat. It explains what the SO example in my post did, a function within a function.
|
# ? Dec 10, 2012 10:31 |
|
nielsm posted:Stay a while and listen! Is lambda syntax consistent across languages?
|
# ? Dec 10, 2012 10:37 |
|
Master_Odin posted:I originally wrote a different way to describe my issue, but I erased it and then forgot ot rewrite the language. I'm using Java. I've since found the "Collections" class/"Comparerators" class (or whatever it's spelled like) which is what I guess I'd want to use and have an ArrayList of 4 objects or something? Collections.Sort() is a static function that sorts any list according to its 'natural order', you can modify the natural order of a list of objects by implementing the Comparable interface and overriding the 'compareTo' method. Basically all of this: http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
|
# ? Dec 10, 2012 10:38 |
|
You should use lambdas sparingly. There's really no difference from an anonymous function, beyond the restrictions that you can't have a docstring, and you can only have expressions. So, really, only use lambdas in cases where they're immediately obvious, like a very simple sort key (sorted([('a', 13), ('b', 22)], key=lambda (a, b): b)). Otherwise, just use "def" to make a function and give it a name and docstring, and use all the lines of code you want.
|
# ? Dec 10, 2012 10:48 |
|
tarepanda posted:Is lambda syntax consistent across languages? What languages?
|
# ? Dec 10, 2012 12:47 |
|
Master_Odin posted:I'm having issues coming up with the best way to sort an array of strings that have the following format: code:
|
# ? Dec 10, 2012 13:14 |
|
I've got a list of 150 or so members and am trying to get the information into some kind of database (although final form will be csv to import into a wordpress blog) so I can present it more usefully. At the minute the data is http://ecprsgpp.wordpress.com/members/ and as you can imagine I'd rather have an automated solution, even if it's imperfect, than spending a whole morning copying and pasting information. Name (ideally first/last as two seperate fields) Institution Postal Address Website Research Interests Recent Publications That is the information that each member can have, although not all do. I think that covers everything, let me know if I'm missing anything or this is the wrong thread.
|
# ? Dec 10, 2012 15:39 |
|
tarepanda posted:Is lambda syntax consistent across languages? No more consistent than any other syntax
|
# ? Dec 10, 2012 18:28 |
|
tarepanda posted:Is lambda syntax consistent across languages? Worry more about the semantics than the syntax
|
# ? Dec 10, 2012 18:56 |
|
tarepanda posted:Is lambda syntax consistent across languages? I never got into lambda calculus and the wikipedia page at first glance seems to get technical in an offputting way. My explanation will be incomplete, but here goes: Lambdas are functions you can treat as data (i.e. as variables) - mini-programs, or deferred computations waiting for input, if you will - but not necessarily one-shots, since they may be able to be used multiple times. Basically, just a function, before you give it arguments. A function is just a lambda with a name attached to it. In python, foo = lambda x: x * 2 is the same as def foo(x): return x * 2. But let's say you had an array of numbers, let's say one through five, and you wanted to apply the calculation 2x+5 to all of them. Well, the easy way is to just loop over it, and apply a function that does that to all of them, and get the list [7, 9, 11, 13, 15]. Lambdas let you call a function someone passes in on some known input, rather than run a known function on input someone else passes in. In a way you can think of it as an inversion of control - rather than supplying a function with data, you're transforming the data with a function. The difference is subtle, but if you imagine the traditional "black box" input/output diagrams, it's the data inside the black box, and it's the algorithm that's the input, rather than the other way around (algorithm in the black box) - even though the output is the same. So, instead of thinking of transforming [1, 2, 3, 4, 5] into [7, 9, 11, 13, 15] with 2x+5, you think of it as mapping 2x+5 onto [1, 2, 3, 4, 5], producing the output [7, 9, 11, 13, 15]. The lambda is the representation of the steps necessary for the transformation, and you got your result by mapping the lambda onto the data. The key is that the function is just a special kind of data, and rather than looping over a list to construct a new list, what you *actually* have is a different view of the data. So why the hell does this matter when 2x+5 is the same thing whether you pass the data in or the function? Well, what if after you did 2x+5 you wanted to then do 6y+3 to the result of that? You could feed in [7, 9, 11, 13, 15] and get [45, 57, 69, 81, 93]. If you have 10 billion numbers then you need to produce the first set of 10 billion numbers, then calculate over those. Easy enough, but looping 20 billion times could be tedious and expensive, particularly if somewhere along the way you decide you only want the first 5 million numbers. Why not just calculate 6(2x+5)+3 = 12x+33 in the first place? Combining those two functions together, then applying that combined calculation to [1, 2, 3, 4, 5] is made possible because you treated the function as the input, and combined the two steps beforehand. So this is all kind of complex and I'm rambling but the point is that lambdas are most commonly used as a concise way of specifying how to transform data from one form into another, which is one of the foundations of functional programming. Functional programming has the interesting property of dealing really well with parallelism. So where you might do a for loop over [1, 2, 3, 4, 5], you can instead have a farm of servers doing 1-10,000,000, another doing 10,000,001 to 20,000,000, and so on. And there's really no reason that they have to do it in order, either, you can just assign them some data, and some function to apply to it, and map it over a huge number of computers. This is MapReduce (well, the mapping part, anyway, and a handwave-y explanation at that). Functional programming has other useful approaches to manipulating data, and I would highly recommend learning them. tl;dr: lambdas and composition of them are a foundation of one kind of abstraction over data. (to patch up holes in your programming awareness, consider reading Abelson and Sussman's SICP. There's a whole religion around it as the key to programming enlightenment, but I prefer to think of it as just legitimately good. I wouldn't try to understand everything at first, though, but just take what I can get.) Doctor w-rw-rw- fucked around with this message at 09:32 on Dec 11, 2012 |
# ? Dec 11, 2012 09:29 |
Is there anything like Kerberos which is less of a three-headed bitch to set up? I've heard it's the only game in town, but figured I'd ask to be sure. I need to do some user/group authentication/authorization.
|
|
# ? Dec 11, 2012 19:20 |
|
tarepanda posted:Is lambda syntax consistent across languages? In most languages that have them at all, "lambda" and "anonymous function" are synonyms - the term originates in the lambda calculus (where all functions are anonymous) and in many languages the keyword to define an anonymous function is lambda or \ (an ASCII approximation of λ) as a result. An "anonymous function" here is just a function value without an associated name (in the same way '5' is just a numeric value; it doesn't have a name until you assign it one with variable assignment or binding). In most cases, lambdas have all of the same power as named functions, and many languages don't even draw a distinction; a named function is just an anonymous function that's been assigned a name. In Lua, for example, the definition of a global function "foo" is exactly equivalent to defining an anonymous function and assigning it to the global variable "foo". It is not quite correct to say that "lambdas are functions you can treat as data"; some languages have first-class functions ("functions you can treat as data") without the ability to create anonymous functions. In python specifically, lambdas are a heavily restricted type of anonymous function that can consist only of a single expression. I don't recall if Python has general anonymous function support.
|
# ? Dec 11, 2012 19:47 |
ToxicFrog posted:In python specifically, lambdas are a heavily restricted type of anonymous function that can consist only of a single expression. I don't recall if Python has general anonymous function support. Python code:
e: okay reading a bit more in the docs for exec() that won't actually work, you can't use return that way.
|
|
# ? Dec 11, 2012 19:52 |
|
I really really love making GBS threads around with python and trying to make things that are weird and not how they were intended to be used. Such as fake if statements in lambdas:Python code:
Python code:
Python code:
Jewel fucked around with this message at 21:51 on Dec 11, 2012 |
# ? Dec 11, 2012 19:54 |
|
|
# ? May 29, 2024 16:02 |
|
What does it mean if a shared library has a name like libblas.so.3.2.1, instead of just libblas.so? I'm trying to compile some code that's getting angry at me because it depends on something called -lblas and it can't find it. I have the libblas.so.3.2.1 in /usr/lib64, but ld doesn't consider this acceptable. If I create a symbolic link called libblas.so and point it to the other file, is that going to work?
|
# ? Dec 12, 2012 00:14 |