/dpt/ - Daily Programming Thread

Functional Edition

Previous Thread: What are you working on, Sup Forums?
Is it functional?

Other urls found in this thread:

github.com/kennyledet/Algorithm-Implementations
twitter.com/SFWRedditImages

First for OOP is a horrible meme.

OOP is terribly bad, on that we can all agree.
But should it be considered a war crime?

Third for I like OOP

Honestly, 2000 lines or anything in that magnitude or several above it is not enough to make an impact on performance for storage. You could pretty much use any willy nilly storage container if you wished to store it and it would still be negligible.

If down-to-the-wire performance is an issue, I'm pretty sure there's a way to nextline without needing to pass to container. and then you nextline until you need to getline. And then you may as well learn ASM.

I'll repeat my question from previous thread:

I'm making a graphics builder for stuff as a side project for my job.

Anyone have any experience with standard control mechanisms for select/drag/hittest/etc? I'm technically an EE so I've never had to build something like this before. I've got the draw and hittesting working, but for some reason I'm stuck on how to implement the select/multiple select and how to handle mouse move/mouse down/mouse up

Not asking how to implement it function wise, but how I should implement the overall control loop.

Programming es hard

>3 posters in this thread
>3 replies + OP
>I'm the first reply in this thread
>This post must be either OP or the second reply
Please don't samefag alternating opinions. It's clear that you're just shitposting.

Stop samefagging.

I changed it up but now its just completely crashing after I enter a value for the Numerator.

Working on my computational optimization research. It's written with a functional style in javascript. Haters come at me. :^)

Line 33 and 35. You're giving it an int where it expects an int pointer.
Use &f->numer and &f->denom.

>computational optimization
enjoy doing something 100% niche and 95% useless

Well I'm not samefagging and I do like OOP
But I'm definitely shitposting, that much is true
:^)))

what does the image mean op?

also, does c++ have functional aspects?
like idontknow, maybe mapping, folding, filtering, or even good typing

Thank you so much! It's always something that's so simple that needs to be changed.

how pajeet am I if my first job involves converting C# to Node.js and coding in Node.js?

>to learn a language you need to practice it by doing projects

Great, but how? I mean, how to start?

I have this idea: a program that counts how many (You)'s you get in a thread. Sounds simple enough but I have no idea how to start the program. Do I need to do a research first? What about other projects?

Learn gdb. It'll save you so much headache in the long run. Like, learn how to use it now.

user's word or name req.
replace words with lines if they're seperated by lines
import System.Environment
import System.Random
main ∷ IO ()
main = do
s ← words readFile "path"
r ← randomRIO (0, length s)
putStrLn $ s !! r


OP here
I posted

lol, my algorithm beats the current state of the art and it's also generalizable to any problem...

C++11 and newer have been getting more functional aspects in the standard library.

Okay guys quick I just started learning programming a couple of days ago, starting with C#. If OOP really is the Devil's paradigm, what should I learn instead?

>does c++ have functional aspects?
It has lambdas and "parametric polymorphism"
>mapping, folding, filtering, or even good typing
Difficult to implement without garbage collection. The standard library doesn't provide full FP versions of these things in order to attempt container-agnosticism but you could easily implement your own.
e.g.
template
std::vector map (F f, const std::vector& vec) {
std::vector out;
out.reserve(vec.size());
for (auto& x : vec)
out.push_back(f(x));
return out;
}

projects are good if you can come up with the ideas for them and are motivated enough to do them.

You can use Hackerrank.com if you lack motivation and just want to do interview tier coding questions.

>generalizable to any problem
this is what CORtards actually believe

Too bad that can't fix all of the other glaring flaws in the language.

anyone can help me configure spacemacs autocompletion with c++ ?

You should learn Haskell

C++ and friends are nowhere near real functional programming
There's more to it than just map, filter & fold

Don't listen to the autists. You'll only waste time jumping from language to language. C# is good enough and you're better off just continuing down the path you're on and switching to a better language later.

yeah, flaws like not accounting for your stupidity

you're going to end up using OOP for anything useful, might as well learn an OOP language now.

instead of learning c# id recommend python or c/c++. im a microdrone but using a programming language developed by microsoft just feels wrong to me.

I second this. You should be able to eventually learn for yourself why OOP isn't so good, but at this point in time it doesn't matter.

>you're better off just continuing down the path you're on and switching ... later.
horrible advice

>might as well learn an OOP language
>C/C++
Neither of those are OOP languages

Just keep learning C#.

>Neither of those are OOP languages
well, close enough

Doing a prompt that requires me to reset a randomly-generated name while also checking for collisions.

void robot::reset()
{
fstream nameHistoryFile("nameHistory.txt", fstream::in | fstream::out | fstream::app);

string tempNewName = generateName(), nameFromHistory = "";

reGenName:
while (getline(nameHistoryFile, nameFromHistory, ',')) {
if (tempNewName == nameFromHistory) {
tempNewName = generateName();
goto reGenName;
}
}

nameHistoryFile

>c/c++
Why are you grouping two completely different languages together?
That's like saying Java/Javascript.

>You're going to end up using OOP for anything useful
No. OOP is awful and shouldn't be used to write anything.

Ditch the goto and replace that line with "continue"...

I see.
anyways, is it true that c++ is sought after in the job market

> optimization
> javascript

lol

The functional differences between the two might be huge, but the syntax is pretty much exactly the same

Stop being so autistic and edgy

Won't the destructor automatically close the file?
Also do this: Might be a bit bad for readability but you could also do
nameHistoryFile

Except this shit is a global black box algorithm for optimizing arbitrary sequences of binary data. It can do MINLP problems, or really whatever I want to throw at it. Fuck, my code can even write code.

It's just the command and control server that has to handle loads of data being spewed out by clients, so it's written in Node for high concurrency (and also ease of implementation)

>but the syntax is pretty much exactly the same
std::cout

But it wouldn't check to see if the tempNewName that was generated inside the while loop collides.

SQL (Why is this considered a language..?), Java, Java script, C#, Python, and c++ are the most common / in demand languages for jobs.

You want to do the whole loop over?
You're definitely doing something in an awfully weird way

And don't remember you're getting lines from the file, so you can't just "go back"

using cout = std::cout;
using chrono = std::chrono;

cout

Oh wow, that really won you the internet argument. Good for you.

If you've honestly written something in JS that beats state of the art CO stuff used by companies then I don't see why you're not marketing it instead of bragging on 2+2 chan about it.

And don't throw that writing code shit at me. Any idiot can write code.

{
using namespace std;
using namespace chrono;
cout

That still looks nothing like C.

printf("%d:", microsecondsElapsed(start_time,end_time));

Employed Haskell programmer reporting in

Which mcdonalds do you work at?

So you created a seemingly equivalent program which looks nothing alike.
Good one.

>mcdonalds
>m c d o n a l d s
>m ..... o n a . d s

I'm going to publish a paper on it in about a month, but I'm still tuning the implementation at the moment.

Also automatic code generation (i.e. here's a problem, write something from scratch) is pretty cutting edge stuff.

>Structured Query Language
>Why is this considered a language..?

>you're getting lines from the file, so you can't just "go back"
I'll have to re-do that part then. Thanks for the feedback.

So, which cherry picked conditions does your code do better than state of the art?

>muh special snowflake genetic algorithm

If by binary data then you mean its a MILP limited to [0,1], none of that is impressive

at least his algorithm is right up to 40% of the time

It shits all over GA's

It's a 0-1 MILNP. Note the N. It assumes unpredictable nonlinearites in the interactions between the bits, which lets it handle way more different problem types.

*MINLP

Fuck

>Well quite a few of them.
>-It's a global search algorithm, so it's not hindered by highly noisy, nonconvex search spaces
>-it's a black box algorithm, so it can be easily applied to different problems without needing to extract certain prerequisite data from your fitness function
>-it operates on binary data, so you can have it optimize machine instructions, decision trees, mechanical parts, floating point values, etc.
>-it converges to optimal solutions really (REALLY!) fucking fast (orders of magnitude better)

Have you got something to hide? Why delete this?

Sup Forums dickwaving is less important than somehow being beaten to publishing. Better safe than sorry. :^)

>makes state-of-the-art bleeding edge algorithms that need to be kept secret before publication
>doesn't know that everything posted on the website gets archived and indexed for searching

Nah, I haven't posted my secret sauce. But I don't want people getting any ideas.

>implying anyone would want to expose their autism by publishing a paper on CO
although this is Sup Forums, i guess you do have a reason to be worried

Yet, you bothered to delete your post that had already been indexed and can be found on Google, and any autist who would care about your shitty algorithm would only take more notice at a deleted post.

I'm doing baby's first linked list on C. I'm a little puzzled with something.
struct linked_list{
struct node *root;
int size;
};

struct node{
char value;
struct node *next;
};

void add_member(struct linked_list *list, char value){
struct node *ptr = list->root;
struct node *element = (struct node *) malloc(sizeof(struct node));
element->value = value;
element->next = NULL;

if(ptr == NULL)
list->root = element; //right here is where I'm puzzled
else{
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = element;
}

list->size += 1;
return;
}

It used to be ptr = element when it's the first element, but that gave me a segmentation fault.
With this line, however, everything is fine. What gives? The while is going through the list using the node pointer and adding an element seem to work. Why wouldn't it work on the very first one?

Disregard this, I suck cocks.

The rest of the reasonable comment aside, Haskell DOES have a surprisingly shitty community. It's stupid, because it doesn't have to be, but the culture is self-reinforcing at this point.

Read the github page for Sup Forums's API. Find your language's JSON libraries. Count all posts in the thread that are responses to a particular post you give the program (i.e., your (you) post).

Rate my FizzBuzz, Sup Forums.


for _ in range(int(42 == 420/10)):
while int(not _):
_+=int(not 1336 == 666)
with open(hex(_), "a+) as ride_my_eggplant:
ride_my_eggplant.write(str(_) * 6661337420)

Oops, sorry, typo.

for _ in range(int(42 == 420/10)):
while int(not _):
_+=int(not 1336 == 666)
with open(hex(_), "a+") as ride_my_eggplant:
ride_my_eggplant.write(str(_) * 6661337420)


Okay, here is my finished FizzBuzz. I think it's nice and readable.

Why does C++ use double colons to access namespaces instead of just a dot like most other languages?

Bjarne and the C++ committee did as much as they could to make the language's syntax as disgusting as possible.

I can clearly see that.

pajeet enough to poo in loo

SICP neophyte here again. I'm trying to build a tail-recursive program that sums the squares of numbers

#lang racket

( define sum_of_squares ( lambda ( L )
( cond
( ( equal? L ' ( ) ) 0 )
( #t ( + ( * ( car L ) ( car L ) ) ( sum_of_squares ( cdr L ) ) ) )
)
))

( define sumOfSquares
( lambda (L)
(sumOfSquaresHelper L '(0))
)
)

( define sumOfSquaresHelper (lambda (L acc)
(cond
(( equal? L '()) acc)
( (set! acc (+ ((toNumb(acc)) (+ ( * (car L) (car L))))))
(sumOfSquaresHelper (cdr L) acc))
)
)
)

( define (toList n)
(cond
((number? n) (list n))
((#t n))
)
)

(define (toNumb L)
(cond
(equal? '())(0)
((list?) (car L))
(#t L)
)
)

(sum_of_squares '(2 2))
(sumOfSquares '(2 2))


Right now it's throwing the error "application: not a procedure;
expected a procedure that can be applied to arguments
given: '(0)
arguments...: [none]"

What the fuck, the toNumb is definitely a procedure. It's taking an argument. AUGH

Yes, I'm aware that I'm garbage. Being garbage is step 1 to being less garbage.

SQL is not pleb tier

I hate it, and the fact that Rust uses it as well.

Use a real programming language to write real programs.

void println="Hello World"

I haven't bothered looking at the whole thing but your equal? cond clause is bad, you need more parens

The problem is
(acc)

That means "run the procedure 'acc' with no arguments", and acc isn't a procedure (in this case it is '(0))

...

>need more parens

Jesus christ how shitty is this language

At least in it was too many parens

the fuck you expected

>javascript
typical js devs

First of all, that's some heinous Lisp formatting, closing brackets on new lines and spaces before operators make it annoying to read. If your editor isn't helping you with this I'd advise you to change. Emacs will always be the best lisp editor, but most will support it.

The cond clause in tonumb isn't valid, in the first condition you're only passing equal? one argument and you don't need the brackets for the answer. These should both be wrapped in a set of brackets.
An expression in lisp is always in the form (operator operand-1 ... operand-n) or it's an self-evaluating atom. In the answer to the first conditional you don't want to call the operator named 0 with no arguments, you simply want to return the self-evaluating value 0.

>(((Lisp)))

>this convoluted swap

Post it to AI if it's really that good.
github.com/kennyledet/Algorithm-Implementations

Underrated post.

It's elegant. Brackets and basic evaluation rules are the only syntax you need to know in Lisp. Most other languages feels like visual white noise and doesn't compile before you tidy up a dozen typos.

Is learning python the hard way a good book? Should I use it to learn python? Or should I use another book?

Scots are NOT Jewish.

No. Avoid anything and everything by Zed Shaw.