/dpt/ - Daily Programming Thread

Previous thread: What are you working on, Sup Forums?

Other urls found in this thread:

strawpoll.me/10939705
en.cppreference.com/w/cpp/language/lambda
en.wikipedia.org/wiki/Heap_(data_structure)
twitter.com/SFWRedditGifs

first for anime

>muh lambdas

Studying fucking dynamic programming for fucking interviews fuck

I'm redoing some school assignments in c++ to get more familiar with the language, and last project I worked was just an array list class. I was going to add a copy constructor, but wanted to run it, and make sure it still worked as is so far.
I think I just had some problems with
>using namespace std
This is just some of the error
/tmp/ccI9fUGv.o: In function `main':
ArrayListdriver.cpp:(.text+0x42): undefined reference to `std::cout'
ArrayListdriver.cpp:(.text+0x47): undefined reference to `std::basic_ostream& std::operator

>dynamic programming
define

Dynamically-typed?

If you think that using namespace is the root of your problem, you simply don't understand C++ or its syntax.

Try writing all of your applications without namespace.

Then once you get used to it, i.e. after you understand operator overloading and everything involved in it, try using namespace again.

Logically speaking, it's simply the removal of redundant namespace calls. The only time using namespace should EVER cause a problem, is if a library or header you're using has a name collision with another library or header.
In which case you start specifying scope by sticking std or whatever everywhere.

tl;dr the problem is not using namespace.

Hmmm, never mind. It's going back to all this undefined stuff even with and without using namespace std.
I'll have to mess around more.

strawpoll.me/10939705

Inline, anonymous function declarations are pretty obvious, why did it take so long for them to be added to mainstream languages?

It's a type of algorithm. Djikstra's algorithm is considered a dynamic programming algorithm.

>I enjoy passing function pointers to other functions while writing the relevant code somewhere it totally doesn't belong

>passing function pointers
nigger wtf are you doing

Have you ever written a hash table?

...

...

>dependency injection is wrong

He's dynamically picking a function.

Good resource for a beginner to learn .net CORE?

What do they teach in college? I want to start learning it myself before enrolling in tafe or uni

>what is object polymorphism

How do you think virtuals work?
They (indirectly) carry a function

A meme.

Is this poor form when inserting data into a database?
I'm not sure if this is how you compose SQL statements.
sqlite3_stmt *stmt2; /* insert some stuff into database */
char *random = random_text(45);
char insert[1000];
sprintf(insert, "INSERT INTO sample(field) VALUES(\"%s\");", random);
free(random);
sqlite3_prepare_v2(db, insert, -1, &stmt2, NULL);
sqlite3_step(stmt2);
sqlite3_finalize(stmt2);

so pass objects or function pointers instead of memey one-liner lambdas, what's the problem

I made a script to dived a image up into blocks.
from PIL import Image
im = Image.open("arrow.jpeg") #build the image object.
pix = im.load() #build the image object this has all the pixels

#Get the width and hight of the image for iterating over and make sure it's devisable by 10.
image_croped_x = (im.size[0] - (im.size[0]%10))
image_croped_y = (im.size[1] - (im.size[1]%10))

#setting some vars.

red = 255
green = 0

image_done = False
line_done = False
block_done = False
y_point = 0
y_count = 0
x_start = 0
x_end = 10
x_point = 0
while image_done == False:

if line_done == True:
if x_point >= image_croped_x - 5:
print "image is done"
image_done == True
break
else:
x_start += 10
x_end += 10
line_done = False
y_point = 0
print "starting new line"


else:
print "Image is not done - State: %r" % image_done

while line_done == False:

if block_done == True:
if y_point > image_croped_y:
print "line is Done"
line_done = True
break
else:
if red == 255:
red = 0
green = 255
else:
red = 255
green = 0
y_count = 0
block_done = False
print "line is not done - State: %r" % line_done
print "block Is starting - State: %r" % block_done

while block_done == False:
if y_count == 9:
block_done = True
break
for x_point in range(x_start ,x_end):
print "%d %d %d %d " % (y_count, y_point, x_point, image_croped_x)
pix[x_point,y_point] = (red, green , 0)
if block_done == False:
y_point += 1
y_count += 1


im.show()

Finally seeing some applications of polymorphism and Dijkstra's and so on with this transportation game I'm making. I class everything as extended from a node and use Dijkstra's to find the shortest routes between them and so on. Pretty fun stuff :)

Something that doesn't exist in C.

Holy shit you're dumb

You know what, why do you use objects?
Why don't you use some other fucking retarded concept that uses objects under the hood but adds another fucking 10 books of UML and stupid constraints, syntax and imcomposable inventions of idiots that don't know what they're doing?

For instance, why not have a virtual object factory that returns the object you want to pass in?

Oh, but that's totally not a meme.
Using X disguised as Y isn't a meme, using X is.

>function pointers
Can't be portably or conveniently generated at runtime.

you could emulate it, or just use plain function pointers and structs

lambdas are good for one liners.
That's really all.
Why write a function that performs one line of code, and then pass the pointer of that function?

Where would you even place this function? It only does one thing, and it's out of place in comparison to the rest of your functions.

nice strawman argument

>generated at runtime
yet lambdas can? what shitty meme lang are you using?

>why use X when you can use X to emulate Y

A lambda is just a function value, like how 3 is a number value. What did you think the function pointer was pointing to?

Java?

Yes, because

Lambdas aren't function pointers, they're objects of anonymous classes with overloaded operator()s. Those are member functions that could have associated state.

The point is you can't generate function pointers at runtime but you can generate objects of lambda classes are runtime.

>strawman
It's perfectly true.


Do you know why C# and Java and F# have inline object expressions?
Do you realise that they were trying to achieve the same functionality?
An object that inherits is like an extensible map or record of functions

>yet lambdas can? what shitty meme lang are you using?
Python, C#. C++. Scheme, Common Lisp...

java and C++

please, show us how to dynamically generate a lambda expression at runtime in C++, show us how it's done

Eat a dick.
en.cppreference.com/w/cpp/language/lambda

what are the image dimensions?

Either you didn't read what I said or you didn't understand.

Expressions don't get generated at runtime, but lambdas are bound to values that can be.
Lambda objects can be generated at runtime because lambdas are just anonymous classes with a function call operator.

freecodecamp

i feel like i failed myself somewhere

you're thick as a fucking brick

and how is overloading an lambda object less meme than using normal objects

To learn anything .NET, MSDN is the answer.

>overloading a lambda object
What are you talking about?

>Lambda objects can be generated at runtime because lambdas are just anonymous classes with a function call operator.
what are YOU talking about? post code, in C++

you're thick as a fucking brick

how is overloading an lambda object less meme than using normal objects

>overloading an lambda object
You don't even know what the words you use mean.

What is "overloading a lambda object"?
Please explain

>overloading an lambda object
You don't even know what the words you use mean.

>Lambda objects can be generated at runtime
You don't even know what the words you use mean.

KILL YOURSELF

I'm not the one who used them
I want user to explain what overloading a lambda object is

auto printer(int x) {
return [x](){ std::cout

I don't think they expect you to invent dynamic programming solutions on the spot.

That's kind of hard.
Imagine inventing the solution to 0-1 knapsack off the back of your head. Who the fuck does that?

you're fucking retarded

So, for example, comparisons

With OOP you're forced to use some kind of ICompatator object, which contains a single virtual function, which you must define elsewhere (I will be assuming you acknowledge object expressions are the lambdas of OOP)
This ICompatator object literally contains a function (generally by pointer/reference) but a few people don't recognise this as it's hidden behind the OOP concepts of members and polymorphism.

The alternative is you pass in a function explicitly, and use a lambda, which means you don't have to define something elsewhere. You can define your one-use function inline.


The example is dumb but it demonstrates the point.
Instances of ___my_anonymous_lambda_class_328925 are generated at runtime, with different bound variables.
This is the primary difference to function pointers.

>Who the fuck does that?
Me. I often ask candidates for a solution to the multiply constrained knapsack problem, or, multidimensional knapsack problem.

Those who can't answer are not fit for the role.

The position usually is about optimization in low-level hardware CPU-architectures

Cheers

Posted in the other thread, but that one is kill

I guess 2 is optimal over 1. Though the only optimization there is taking (2 - 1) out of the equation when you call factorial(2).

That was an oversight by me, but I've only been writing python/code in general for just under two weeks. I think it's fair.

Regardless, thanks

Lambda objects are generated at runtime, like most objects.
import random

def create_a_new_lambda():
value = random.random()
return lambda : value

create_a_new_lambda()()
create_a_new_lambda()()
create_a_new_lambda()()
create_a_new_lambda()()

By the way, an object doesn't have to be something created by calling a constructor.

you define your one-use function inline at COMPILE TIME, not runtime.

you're welcome

Maybe he's talking about closures? I'm not 100% sure on how C++ implements them. If captured variables are statically allocated, or if after the function returns the variables are moved out of the function similarly to how Lua implements upvalues. If they are allocated on the stack and moved out after function exit, then the binding happens at runtime, so "Lambda objects have to be generated at runtime". But really only the binding occurs at runtime. I think he doesn't quite understand what he's talking about.

i'm talking about C++ (not python) since brought it up

Yes, closures. The binding occurs at runtime, which is the generation of a lambda object. A lambda in C++ is an anonymous class with a function call operator.

Look at my example here auto printer(int x) { return [x](){ std::cout

see >>function pointers
>Can't be portably or conveniently generated at runtime.
he's implying that lambdas can be generated at runtime, but in C++ (at least) you can't generate a different function from what you wrote in the source code (portably without using hacks), the distinction between normal functions and lambdas is more or less just syntax

>Oh no, he's right! Maybe if I pretend he's only right for Python and lambdas have wildly different semantics in C++ I'll be okay!

you can call a function pointer with a runtime number too you complete moron

Sounds like you'd use linear programming to solve that.
Not that I'm knowledgeable on the subject.
Everywhere I've read about linear programming absolutely refuses to dive deep into the subject of actually solving the linear program in favor of just setting it up and letting it roll through MATLAB.

>A lambda in C++ is an anonymous class with a function call operator.
Yes, but the code itself cannot be generated at runtime. Each application would require an embedded JIT. Only the data is binded during runtime, just like virtual functions. The compiler already knows exactly what variables are captured during compilation. you can't "move" a lambda generation into another function and have it work.

Lambda objects can be.
When you use the term 'lambda' in C++ you refer to one of three things that are all part of the same system, or the system all together

1. The expression / syntax
2. The anonymous classes generated by 1
3. The objects / instances of 2

Objects can be made at runtime

Look at what it's doing
myPrinter is one of the lambda objects generated by printer
printer takes parameters and returns a lambda
the lambda it returns TAKES NO PARAMETERS

see we're talking about different things, you can generate different function logic at runtime

The lambda isn't being called with a number.

Hey Sup Forums, I have a working pathfinding implementation based on A*. It works but I have an issue, it runs quite slowly for what I want it to do. What I want it to do is check A* of around 2000 elements against 2000 elements quickly, and It isn't going too quick (It has been going about 3 minutes and it is about 1/3 of the way through). Is there any tips you would have to optimise it?

i was actually kidding, but it amazes me people still replied to my comment
kek

*can't

Are you absolutely sure that you have actually implemented A*?
What happens if you create a maze with no walls or movement penalties?

Are you aware of what a heap is?
I get a lot of people trying to write A* without knowing what a heap is.

>i was actually kidding

Thank the lord. There are interviewers who have those expectations, though.

>muh design patterns
>>>/loo/

>muh using words to describe things
>>>/specialed/

Problem: you have two kinds of geometrical objects, points and lines. Write a function to reflect one object around another.

Statically typed solution:
Point reflect(Point obj, Point pivot);
Point reflect(Point obj, Line pivot);
Line reflect(Line obj, Point pivot);
Line reflect(Line obj, Line pivot);


Dynamically typed solution:
def reflect(obj, pivot):
fun = None
if isinstance(obj, Point):
if isinstance(pivot, Point):
fun = reflectPointAroundPoint
elif isinstance(pivot, Line):
fun = reflectPointAroundLine
elif isinstance(pivot, Line):
if isinstance(pivot, Point):
fun = reflectLineAroundPoint
elif isinstance(pivot, Line):
fun = reflectLineAroundLine
if fun:
return fun(obj, pivot)
return None

def reflectPointAroundPoint:
pass

def reflectPointAroundLine:
pass

def reflectLineAroundPoint:
pass

def reflectLineAroundLine:
pass

>>>/poo/in/loo

Yeah, as in the heap you malloc to?

It is close to A*, and while it doesn't have walls as such, there are differently weighted tiles (So I suppose you could make a wall by having a huge addition to fscore). I wouldn't say it was a 100% pure implementation, but it works.

No, as in the min/max heap data structure.
It's not absolutely necessary to use a heap to implement A*, but you do need it if you want it to run fast in an amortized fashion.

Yes, the code isn't generated, but lambda objects ARE.

In the first part of The objects are generated at runtime
This is generating functors (C++ usage) at runtime
Functors (C++) are function objects

There is a direct relation to virtual classes
Here's the example I gave earlier, OOP style:

base classes / utility
template
class Function { virtual B operator() (A) const = 0; };

template
class Function {
virtual R operator() () const = 0;
// C++ would error without this specialisation
};

#define comma ,
#define to comma


code on the usage side
#include
class customFunction : Function {
public:
customFunction(int parameter) : x(parameter) {}
std::string operator() () const {
return std::string("hello ") + std::to_string(x);
}
private:
int x;
};

int main() {
customFunction functor(5);
cout

en.wikipedia.org/wiki/Heap_(data_structure)

He probably meant a heap as in the data structure. It's used to implement priority queues, which can be used in heuristic search algorithms like A*.

>muh programming buzzwords
>>>/DESIGNATED/

>muh memes
kys

>#define comma ,
>#define to comma

Except you can generate functions with different logic using high order functions. Saying functions can't be dynamically created because the code that generates it is static is like saying objects can't be dynamically instantiated because their classes are static.

Function

>>muh programming buzzwords
yeah like lambdas

Design patterns ARE memes.

I hope you know that to reflect something, you need more than just a single point as a point of reference.

Alright, I will check it out. Thanks, it also sometimes will hang on positions which is a bit odd, but I am sure I can weed out why.

>Except you can generate functions with different logic using high order functions.
in that case you can "generate" functions with function pointers as well.

He's using functions to refer to functors or function objects
These can have bound state
Function pointers can't, they would need an additional parameter

hello neet, how was your day?

Oh and if you mean the redundancy of comma, that's a mistake
I used to do it because you can pass a macro "comma" to another macro, but you can't pass a , to a macro

You sure?