/dpt/ - Daily Programming Thread

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

Other urls found in this thread:

youtube.com/watch?v=uiJycy6dFSQ
regex101.com
youtube.com/watch?v=HQYsFshbkYw
youtube.com/watch?v=dAJbBIzDeV0
ideone.com/
gcc.godbolt.org/
stackoverflow.com/questions/10405436/anonymous-functions-using-gcc-statement-expressions
gcc.gnu.org/onlinedocs/gcc/C-Extensions.html#C-Extensions
mindprod.com/jgloss/unmainnaming.html
luajit.org/ext_ffi.html
github.com/lattera/glibc/blob/master/libio/getchar.c
twitter.com/SFWRedditGifs

Is Scala a meme?

of the highest order

youtube.com/watch?v=uiJycy6dFSQ

I see functional programming being pushed here on Sup Forums more than anywhere else.
Why are people so obsessed with having zero side-effects when not even CPUs work like that?
Your computer is one giant state machine, after all.

how do I make this regex match 'a' even in the first line?

(?

Only pure functional langugaes are about zero/near zero side effects. Functional programming can be done in most langugaes, it's just depends on whether or not it suits the problem at hand.

>Your computer is one giant state machine, after all.
Which is why almost all programming langugaes are designed to help abstract that away to varying degrees.

btw, picture is from regex101.com

the best regex tester to date

>regex
>not even a regular expression

a few reasons.

one, its easier to reason about
two, its easier for the compiler to make optimizations
three, its fun!

seriously, try it :)

What does Sup Forums think of this guy? I thought he was pretty good

youtube.com/watch?v=HQYsFshbkYw

(?

A lot of times, i find myself writing tiny one-liner functions to make code look cleaner, but I'd rather not have it be in the global scope if it's gonna be used in one function only.

How do I do nested/lambda functions in C?

thnx, though it works on the site, it crashes my python code

>look-behind requires fixed-width pattern
googling whats up with that now...

epic bait

Try this then
(?

OpenGL question:

If I don't request a specific version of OpenGL with glfwWindowHint, my laptop's Intel GPU (with MESA) will default to OpenGL 3.0. However, if request it to use 3.3, it will comply and use 3.3. That is, by default my graphics card uses an older version of OpenGL. Not only is this completely baffling to me, but requesting a specific version is actually a bit of a problem because my desktop's graphics card supports OpenGL 4.5, and I don't want it to be using OpenGL 3.3 for no reason.

How can I get my graphics card to use the most recent version of OpenGL available?

Well you can use function pointers but C sadly doesn't let you define new functions inside amother

Am I the only one on Sup Forums who would date a cute convincing passing trap?

that one seems to work nicely

>Explanation: The negative lookbehind (?

>why are people moving away from assembly when CPUs don't even work like that?

what are you listening to /dpt/?

youtube.com/watch?v=dAJbBIzDeV0

>hello how is life family circumstances and everything - ah that's nice

no

static inline. Keeps it local to the translation unit. Allows functions to contain that name outside of the translation unit. Each instance of the function has it's own local and static variables apart from the others of the same function. It's about as close as you'll get to lambdas, maybe with gcc's nested and properly-scoped function definitions it's 80% of the way there.

Hey guys, any help will be greatly apreciated.

I'm working on a line following program for my college degree, to be used on Parrot Ar-Drone 2.0 UAVs. My code is to be executed using Node.js, and the ar-drone node from FelixGe. I use OpenCV for image processing.

I have many reasons to believe my image processing is good, the red line on the floor is seen very well by the drone's camera, using color filtering, and the mass center (using contours) gives me good results as well.

However, my program doesn't work, as the drone cannot seem to follow the line appropriately. I'm looking for ideas as to how i should use my mass center to give commands (movements) to the drone.

>My code is to be executed using Node.js
Stopped reading there.
Whatever you're doing is shit.

I also think it is not the best tool, but i have no choice, it's the teacher's choice

Could someone try this to see if it works?
#include
#define lambda(return_type, function_body) \
({ \
return_type __fn__ function_body \
&__fn__; \
})

int main(void)
{
int (*max)(int, int) = lambda(int, (int x, int y) { return (x > y) ? x : y; });
printf("max: %d\n", max(192, 4));
}

you tell us.
ideone.com/

>Using a Von Neumann machine
>(current-year)

It works.

it works on my computer, but not on this site, and it doesn't work on gcc.godbolt.org/ either.

which is weird, because i have -std=c89 enabled and everything

It works on ideone. I just tried it. Yeah godbolt only compiles C++ even with -std=c89, GNU C extensions aren't enabled for C++.

bigger on HackerNews than Sup Forums. what other programmer communities are there other than reddit anyways?
also pure FP is great because you're not constrained to the details of the underlying architecture. things like lazy evaluation with pure FP can change the way you think of data vs. code entirely (for example, lists as a control structure) or just change how you solve problems completely
it's often slower but it makes the development cycle much faster (especially when you have to debug your code)
GCC has an extension for nested functions and you can sort of hack them into lambdas with a lot of weird tricks: stackoverflow.com/questions/10405436/anonymous-functions-using-gcc-statement-expressions
of course anyone who has to work with your code will hate you if you do this

Well, this is new to me. Never heard of these here ganoo compound statement expression majiggers.

Stop this programming fag meme.

Too bad they don't exist in real life. Before puberty it's illegal, after puberty they are trash.

So last night an user posted a programming challenge. There were a few people who solved the challenge, some with good time and space complexity. I thought it was fun, so I have a new challenge, which I've already solved. It requires a non-bruteforce algo to solve in a reasonable amount of time.

Imagine you have a list of numbers from 1 to 2**k - 1. You can reorder the numbers however you like. How many different sequences of the numbers from 1 to 2**k - 1 that when put into a binary search tree result in a perfectly balanced tree?

For k = 3 there are 7 nodes. Some answers are...
4 2 6 1 3 5 7
4 2 1 3 6 7 5
4 6 7 2 3 5 1
and so on
There are a total 80 different sequences that fulfill the requirement when k = 3. How many different sequences are there that fulfill the requirement when k = 4?

BONUS: k = 5?
PHD: k = n

>what other programmer communities are there other than reddit anyways?
LtU

gcc.gnu.org/onlinedocs/gcc/C-Extensions.html#C-Extensions
There are a truly retarded amount of extensions. If you want super optimal code dig into attributes too. Shit is strictly non-portable though.

A good chunk of those extensions are C99/C11 features that they just let you use in C89.

Symbol tables for block structured languages are a mess of a data structure. Thank goodness for extended due dates.

I use a lot of these extensions in -ansi mode and I've never had a problem getting other compilers to accept my code, especially combining declarations with initializers.

This is GNU C only.

guess so, but not like youd expect them to shill FP any less than Sup Forums or HN

Can you draw the binary search tree for the second one? I'm a bit confused here.

How are you implementing them? I figured it would be a random tree of maps. It doesn't seem too complicated.

>4 2 1 3 6 7 5

4

4
/
2

4
/
2
/
1

4
/
2
/ \
1 3


4
/ \
2 6
/ \
1 3


4
/ \
2 6
/ \ \
1 3 7


4
/ \
2 6
/ \ / \
1 3 5 7


The second doesn't represent a binary search tree. It's a list that if you put the values into a BST in order it makes a balanced tree. For an example of a sequence that doesn't work
4 3 2 1 5 7 6

Nice dubs. So in other words, you're looking for how many sequences that, when inserted directly into a binary search tree without any rebalancing, result in a balanced tree?

>399399
wooo

You got it.

This is an interesting problem. I feel it grows too fast for me to "cheat" and figure it out by way of example. I'll take a crack at it and see what happens.

should I learn meme languages/frameworks like PHP, JS/Node, Ruby/RoR, etc?
I realized these are the best paid jobs in my country, and I've refused to learn them until now ;_;

Top level, built in procedures and constants go in their own BST.

For everything else, there is an array of 16 pointers to ID structs, each representing the current procedure at that nest level. A procedure has a linked list of ID structs for its parameter list, and a BST for all local variables, type names/aliases, and procedures under it.

It's not so much confusing, just a mess, especially considering I have to support overloading.

...

rollin

rollin

Good god man. Is that the way you were taught to do it? Seems like you might be able to clean it up. For one, an array of linked list of procedures, each increasing index as a scoping level? Why? You would have to search each scoping level for the blocks you are referencing when blocks of the same nesting level really have nothing holding them together.

roll

Is Lua a meme?

Everything is a meme.

No. It's very well designed, just a dozen or so concepts to it and you get a language as powerful as Python without the big standard lib. It's simple, you can learn it in best part of a couple days. A lot of applications are scripted with it: video games (source games, ComputerCraft) and text editors (vim, geany). Also there's Torch with the JIT.

Lua is a pretty neat language if you give people the ability to create extensions for your software (mostly games) with it.

Look at Factorio, Garry's Mod, Audioshield, ...

>you can learn it in best part of a couple days.
I feel like the easy stuff is fine but the hard stuff like embedding into C/C++, threading, metatable wizardry, flies over my head and I still can't get gud.

xD Just got here nice problem :) starting :D

is learning to code from this a good idea?
mindprod.com/jgloss/unmainnaming.html

Isnt the answer k=4 6400? And for k=n P(n)=P(n-1)^2?

__device__ short atomicAddShort(short* address, short val)
{
unsigned int *base_address = (unsigned int *)((char *)address - ((size_t)address & 2)); //tera's revised version (showtopic=201975)
unsigned int long_val = ((size_t)address & 2) ? ((unsigned int)val > 16);
}
else {
unsigned int overflow = ((long_old & 0xffff) + long_val) & 0xffff0000;
if (overflow)
atomicSub(base_address, overflow);
return (short)(long_old & 0xffff);
}
}


So the CUDA function AtomicAdd works on 32bit integers, and the function above extends it to shorts. If I change the 2s to 4s can I extend it to work on 8bit chars?

:)

>embedding into C/C++
The API does take some time to learn. The LuaJIT fixes that.
luajit.org/ext_ffi.html
>threading
There is no threading... coroutines are easy to understand if you understand python generators. They are more general than generators though. SOme of the whackier shit might take some time to understand. Roberto has a paper out on implementing other cooperative multitasking paradigms with coroutines if you are used to functional multitasking paradigms (continations, etc.).
>metatable wizardry
It's simple, but obnoxious at the same time. 90% of the time you are just trying to implement classes, which can learn to do if you read through the chapter on it in the PiL book. It does take a while to become a true wizard in the stuff. Or you can use the JITs ffi and just make classes the C way by declaring structs and packing functions into a table that deal with that struct.
>files
? I guess if it's your first language.

The real trick is learning to use the libraries available for it. lpeg is fucking fantastic. I've seen people just implement their own languages on top of lua with that library. There's the long-dead metalua, which added hygienic macros to the language. I also tend to use a lot of Torch's and the Kepler project's libraries.

Honestly, it seems like the language is dying. Mike Pall just abandoned the JIT, the mailing list is down to

No, it's much much larger. The equation is close in form, but it doesn't even work for k = 3.

Nope

Does 'implementation of foo()' imply what are the literal series of statements that make up the function foo()?

If it is, where can I read the implementation of getchar();? Is it written somewhere?

;D

Yup saw it all :| I overlooked some stuff back on paper... :)

>where can I read the implementation of getchar();? Is it written somewhere?
It's in the source code of your C library.

>getchar()
github.com/lattera/glibc/blob/master/libio/getchar.c
Don't expect large old C codebases to be straightforward.

If I'm on linux it's referred to as glibc, correct?

Oh thanks, this is also on my linux system though, isn't it?

Most likely. However, as says, reading the glib source code is not easy.

k=4 -> 13344 ?

Several orders of magnitude off. Are you trying to do it by hand or program it?

>reading the glib source code is not easy.

I can see that now. Will further reading on glibc help me understand how to read it? Or what would I have to look into?

Maybe it would be a little easier to try reading the source code of the musl C standard library instead. It may not be exactly what's running on your system, but it's quite a bit simpler.

By Hand... xDDD Because of missing some *1000 in getting the solution by hand maybe i will try to program it :) didnt think it would be that much :) Im trying to find the formula not brute force it... :)

>Honestly, it seems like the language is dying.
fugg!! :D

"flies over my head" (as in im a retard) not "files"

Like most things, there probably isn't a closed form solution.

It took me a week to find the closed form formula after I had spent two weeks making increasingly optimal solutions and still couldn't find k = 6 in less than my lifetime. After I finished I looked around to see if the problem had been done before, because I might be able to use it as a basis for a thesis. Turns out some shmuck made their dissertation about this problem and they found the closed form formula already. That's why I put 'PHD' next to k = n.

>they found the closed form formula already.

Well now, don't I have egg on my face!

1940864 ? :)

Yep, sorry pal. Onto the filtered list you go

Getting there.

Btw this problem is pure Mathematics for combinatorics... I Like it :)

This isnt the number? XD still by hand :)
2263424? :) last time i forget one case i think :)

...

dumb tripfag cancer
go back to plebbit

>go back to plebbit

I've been here since before you were born, sonny.

Sorry, no.

2269184? getting closer?

If truth, you'd already know that tripfags are the cancer of anonymous imageboard since day 1. End yourself right now, attentionwhore.

...

Hey, uh, different user... this is probably wrong, but is 6642155520000 around the correct order of magnitude?