/dpt/ - Daily Programming Thread

- Functional Programming Edition -

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

Other urls found in this thread:

pastebin.com/kwS2KMkY
github.com/tonsky/FiraCode
web.itu.edu.tr/kesgin/mul06/intel/instr/seto.html
developer.android.com/training/index.html
twitter.com/AnonBabble

I have another "I am a fucking retarded babby" question for Java.

This is a question on a study guide, so not homework.

>Write a method that will take in a primitive integer as its sole parameter and return an array of integers.

I wrote the header:
public static int[] main(int size)
{
}

>Each location in the array will have a value identical to its index times 10. The size of the array is determined by the value of the integer parameter.

Now I have no idea how to do this. Any starts?

C#

in Haskell this is just

flip take (iterate (10*) 1)

firstly this won't be your main function, give it a better name.

In your function, declare an array of int having size elements.

Use a for loop to set each array[i] to i * 10

This is what I've got.

int [] myArray = new int[size]
for (int i = 1; 1 < myArray.length; i++)
{
myArray[i] = i*10;
}

Would this work or is there more to be added?

typo, the middle of the for loop should be "i < myArray.length" not 1.

>1 < myArray.length

typo, as noted above

Your approach is right but 1) you're missing a return statement, and 2) you have to write a separate method, not use main. main must return void and take in String[].

1

well i finished my resume robot. it mines emails from craigslist then send my resume to them. basically mines five emails on specific parameters, then it generates a txt file with them then another email script compares that txt file to an already applied file if we applied that one it skips it if not it sends my email there.

Do you think you'll get replies that way? Jw.

Give up. You're clearly too stupid to pay attention in class. Remove yourself so that someone who didn't get in based on affirmative action can have a slot.

well it's for not programming jobs so ya. my resume is encoded into the email.

it's like entry level jobs and it's in acity 95% of the postings just say 'email your resume' that's how they kinda do so iwas like well i've been coding for two weeks now let's automate this. now i don't really have an excuse not to apply to jobs as a robot literally does it for me.

why be a dick? I'm just trying to get help -- not have someone do any work for me.

Just wondering, is that tail optimized?

Haskell's lazily evaluated, so it'd be a thunk on the heap

start at 0 not 1

C++

That's not quite right - that sequence grows exponentially.

(\n -> [0,10..n*10])

oh whoops, misunderstood
you can do this pointwise with enumFromThenTo but it's an unwieldy name

enumFromThenTo 0 10 . (10*)

or

flip take (iterate (+10) 0)

This is functional programming edition.

IntStream.iterate(0, n -> n + 10).limit(size);

return IntStream.range(0,10).map(x->x*10).toArray();

function fuck(uselessInteger) {

return[1,2,3,4];

}

What kind of a question is that?

Replace range(0,10) with range(0,size)

(DEFMACRO CONFIRM (ACTION)
`(PROG (CONFIRMED)
START
(PRINT '(ARE YOU SURE YOU WISH TO DO THIS ? Y / N)) (TERPRI)
(SETQ CONFIRMED (READ))
(CASE (CONFIRMED)
(Y (RETURN ,ACTION))
(N (PRINT '(CANCELLED)) (TERPRI))
(T (GO START)))))
(DEFUN REMOVE-FILE (FILE)
(DELETE-FILE (PROBE-FILE FILE)))
(PROG (FP)
(SETQ FP (READ-LINE))
(CONFIRM (REMOVE-FILE FP)))

I made a python script that takes a file name, search for subtitles in a website, downloads the zipped/rar subtitle, checks if there's different srt versions and picks the better one then copies it to the original file's folder with the same name.

this is my first python script and I've done it basically googling every step and messing around. I've noticed that I've never seen a python script uglier than mine. I want to paste it in here so you guys tell me if even though it works, there's room to improve and ways I can accomplish something that I don't know.

here it is, please don't laugh.

pastebin.com/kwS2KMkY

wut

Start by refactoring, maybe.

Move shit you repeat a lot like this:
str(re.findall('(.*)\.\d{3}\.', filename, re.IGNORECASE)).strip("]['.,")

into their own functions.

Also the empty string evaluates to False, IIRC so you can just chain those
name_of_show = x()
if name_of_show == "":
name_of_show = y()
if name_of_show == "":
name_of_show = z()

Like this
name_of_show = x() or y() or z()

Hope that helps you get started.

>not making confirmed into a gensym

how do I get that font in vim

Oh, and stick to English --or Spanish, if you must -- but don't mix languages.

Thanks!

I think that's Fira Code

>or Spanish, if you must
No. Don't do this. Always code in english.

with the arrows too?

this

Yeah, it comes with ligatures. github.com/tonsky/FiraCode

Support depends on your terminal emulator (or GUI editor/IDE)

>girl a few years younger than me (gf's sister; senior in highschool) asks for programming help
>look at her problem
>I'm already in bed but I love doing easy programming problems, so I say sure
>text her "Can I send you the info on skype? It's too much to text"
>No reply
>I start making the demonstration code and a graph to show her the output of the function
>30 mins late
>no reply

Why are teenagers terrible

tell me, can I get it for vim and with st or urxvt

>high school girls
>good people

pick one

>helping stacies out
you are part of the problem

>DEFUN
wew, must a fun language :^)

She's the kind of person who sends 20 one-word texts and blows my phone up

>github.com/tonsky/FiraCode
There is a list of supported terminals.

I like solving problems. I'd probably end up researching nukes for the nazis during ww2 because I like solving problems so much, and disregard the consequences

That's cute. In a sane language it would just be something like 10*Range[n].

You don't understand, ok!
Helping Stacies gives them powerk, not very good, you are part of the problem.

If you wanted to solve a problem, you should have coded something that would definitely get her in trouble..

>doesn't list vim or st or urxvt anywhere

omg. are you actually helping her i did tell someone okay look u pthis module and some if else. your literally pussy whipped.

just tell her word for word -

ur not a programmer nigger

from one of the programming challenges, one of them were,
> Virtual Machine with a Script that Writes "Hello, World"
what does that mean?

simulate a CPU, it's instruction set, persistent memory, and a screen device to print out "Hello, world!" entirely in software.

Thanks

Dicking around in C. I just started this language about 4 hours ago... I've set it up to show the number, too. My question: is there a way (bit-flag, switch case?) to toggle the ability? So, at the user's whim, they can choose whether or not to show "3 Fizz" vs "Fizz"


//Fizzbuzz!!! Modified to show the number alongside "Fizz","Buzz"
#include


int main()
{
int x;
for(x = 1; x

Took me 4 hours to create this:
import java.util.Scanner;

public class HelloGPA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean response;
double creditSum = 0;
double cumulative;
double product = 0;


System.out.println("Current GPA?");
double currentGPA = sc.nextDouble();
System.out.println("Current credits?");
double currentCredits = sc.nextDouble();

do {
System.out.println("Credits?");
double credits = sc.nextDouble();
creditSum += credits;
System.out.println("Letter grade");
char grade = sc.next().charAt(0);
double gradePoint = switchGrade(grade);
product += newClass(credits, gradePoint);
System.out.println("Add class?(y or n)");
char addClass = sc.next().charAt(0);
if (addClass == 'y') {
response = true;
} else {
response = false;
}
} while (response == true);
cumulative = (product + (currentGPA*currentCredits))/(currentCredits+creditSum);
System.out.println("Your current GPA is: " + currentGPA);
System.out.println("Your new GPA is: " + cumulative);
}

private static double newClass(double credits, double grade) {
return credits * grade;
}

private static int switchGrade(char grade) {
switch (grade) {
case 'a':
grade = 4;
break;
case 'b':
grade = 3;
break;
case 'c':
grade = 2;
break;
case 'd':
grade = 1;
break;
default:
grade = 0;
break;
}
return grade;
}
}


Took me too long. Anyway how do I begin creating an Android app out of it? Learn XML?

Anybody got experience with Curses in Perl?

Stop reinventing the wheel.
(y-or-n-p "Are you sure you wish to do this? Y/N")

Trying to figure out this Spider Solitaire game in Assembly...

>tfw you fell for the square root meme

You create an array with size 'n', and then you make each element in the array equal to its index times ten. You should be able to figure out how to do this based on previous examples.

>(T (GO START)))))
>)))))
Fuckin lisp

Just change the formatting string. Something like
#include

int main() {
char* fizzbuzz, *fizz, *buzz;
int choice;

// Ask the user what kind of format he wants
scanf("%1d", &choice);
choice = choice % 2;
fizzbuzz = choice ? "%d FizzBuzz\n" : "FizzBuzz\n";
fizz = choice ? "%d Fizz\n" : "Fizz\n";
buzz = choice ? "%d Buzz\n" : "Buzz\n";

int x;
for(x = 1; x

lisp is such a meme

even if statements are fucking confusing

map (*10) [0..(n - 1)]
[x * 10 | x

They're just ternary statements.
(if (> val max) max val)

uh huh

Oh lawrd I'm so close to an erection right now.

I just figured out a problem I've been thinking about for a fucking year. How to get the overflow of multiplication.

I'm trying to develop a C++ BigInt library thingy (I know there's libraries that can do it better than I can. It's just for educational purposes). And the only way I could think of doing multiplication is either repeated addition or cap the number array to the sqrt of the base. But now I just found a way to manipulate the numbers such that I get the overflow for any multiplication

I can't bother to explain it, so here's my mathwork. If you can understand it, fine. But all you need to know is that for the last line, everything being mulitplied by "base" is the carry and the rest are all guaranteed to be

> ternary statements
they're more like expressions, not statements, because they have a value

Go find a library for writing Android apps with. Write the basic hello world ones to see if you can make them work. Start porting your GPA calculator over.

You're right. My mistake.

>Go find a library
....................

this should not be your first step especially when you're just learning

start with a plain hello world android app and go from there

idk but I'm experienced with islamic death curses

Can't you just use SETO?
web.itu.edu.tr/kesgin/mul06/intel/instr/seto.html

In order to properly do multiplication, you need to know by how much did it overflow. Just knowing it overflowed is not helpful

I plan on using that for addition, though.

how do I fix my threaded program? I think the producers deadlocked with the consumers. I have a stack that the producers push to and the consumers pop. After like a minute my program locks up.

Shit, thanks m8, i knew there was a way

How do you suppose he's going to write an app with buttons and text fields if he isn't using some kind of library?

what language, what method of synchronization, blah de blah

if using mutexes and semaphores posting the order of acquistion will suffice

the android framework is capable enough. third-party libraries just make it """"easier"""" to accomplish the same tasks

i did the android studio hello world. was more of a guide on how to use android studio than a real guide on mobile applications. they did use xml ontop of java so idk if that's the thing.

I am a hipster. I download threads of my waifu like this
zsh ~/script/Sup Forums-download.sh $(xclip -o)
where
# Sup Forums-download.sh
curl $1 | fmt | gawk -f ~/script/extract-Sup Forums.awk | \
uniq | parallel -j6 wget -c {}

and
--extract-Sup Forums.awk
{
for (i = 1; i

developer.android.com/training/index.html

Ok, so I already completed this simple problem, but the line "and you run ReverseTxtLines infile.txt outfile.txt then oufile.txt contains..." is where I'm not sure what to do. Where do I assume theyre "running" it through, the command line? If so, how do I adapt my program to run through there successfully? Otherwise, is the instructions just being general enough that running the program in an IDE and then inserting the appropriate file names good enough?

Here's the full instructions:
Write a program in Java that reads each line in a standard txt file, reverses its lines, and
writes them to another file.
For example, if the file infile.txt contains the lines

I went down to the river.
I set down on the bank.
I tried to think but couldn't.
So I jumped in and sank.

and you run ReverseTxtLines infile.txt outfile.txt then oufile.txt contains

So I jumped in and sank.
I tried to think but couldn't.
I set down on the bank.
I went down to the river.

Note: Use exception handling to detect improper inputs.

Is the Android framework not a library?

Your main() method has a parameter of String[] args
You should call the program from the command line with java myprogram infile.txt and parse that file using args[0] to get the name of the file.

C and mutex

the push and pop are basically the same since they both grab the same lock
blocking(){
lock();
wait();
while(no room){
signal();
wait();
}
/*critical section */
signal();
unlock();
}

Two changes: r3, the way i written here, is not guaranteed to be accurate. It's definition has an overflow case where all values are the max possible values

It's easily fixed by writing
r3= ((a1*r2%s) + (b1*r1%s))%s
Three modulus operations, but fuck, it needs to be done. That or just only store values < 2^32 like I originally planned.

Also the same with c
c= a1*r2//s + b1*r1//s + ((a1*r2%s) + (b1*r1%s))//s

This is the only way I can get the overflow for multiplication.

Why doesn't C++ have C# style properties? I thought it was trying to be every language at the same time.
But seriously, C# style properties would be really fucking great, FUCK getter/setter methods.

Not necessary since 0*10 is 0 and Java initializes ints to 0 by default.

you could just stop being autistic and makes these variables public

wait Im an idiot.
pthread_mutex_lock will wait for a lock until it comes free.
You only use pthread_cond_wait if you want to wait for a signal.

But muh restricting access.
What if I only want to give the outside world read-only access to a member? I'd have to write a getter method, but that shit sucks balls.

Can someone explain to me why you would ever need to use private/public class members?
Why wouldn't you just use opaque types and/or static state variables?

Am I wrong in assuming that private/public class members are just another way to babyproof your libraries against your users?
Because this is basically as bad as adding tons and tons of redundant sanity checks just so you can stupidproof your library from segfaults from improper usage and preventing your users from ever learning anything about good practices.

Because C++ is way older than C#, and C# was designed specifically with all the obtuse problems of Java and C++ developed OOP practices in mind, in order to avoid repeating them.

C# is the true OOP language, and doesn't have the legacy code problem to deal with when making changes.

const multiply = (x, y) =>
x * y

const multiplyBy10 = x =>
x * 10

const multiplyByIndex = function() {
return multiply(arguments[0], arguments[1])
}

const filledArray = n =>
Array(n).fill(1)

const indexedArray = n =>
filledArray(n).map(multiplyByIndex)

console.log(
"user's homework:",
indexedArray(10).map(multiplyBy10)
)

>I'm just trying to get help -- not have someone do any work for me.
except what you're asking about is so trivial that to explain it is to give you the solution
it's not something interesting or worthwhile to talk about, it's a for loop

wow wpf is actually kind of nice. wish F# had first class support for it