Old thread: What are you working on, Jow Forums?
/dpt/ - Daily Programming Thread
Other urls found in this thread:
en.cppreference.com
docs.oracle.com
pastebin.com
jeffe.cs.illinois.edu
en.m.wikipedia.org
twitter.com
Improved Gpredict using Qt and modern C++
>What are you working on, Jow Forums?
k-means.
nothing atm
For me, it's Haskell.
dynamic typing makes me ANGRY
I used to write in Java on Windows machines but recently I installed Ubuntu. How do I pass commands to the terminal? For example, how do I open an image on my Desktop via a Java method?
Windows example for executing JAR files:
public static void executeJAR(String jarFilePath) {
try {
Runtime.getRuntime().exec(new String[] {
"cmd",
"/c",
"start "+
"java -jar "+jarFilePath
});
} catch (IOException e) {
e.printStackTrace();
}
}
I have gotten this far but no terminal window seems to be opening when I pass for example "/bin/echo 'top'" as a command string to the method. Am I missing something?
public static void executeTerminalCommand(String command) throws IOException, InterruptedException {
Process p = null;
p = Runtime.getRuntime().exec(command);
int returnCode = p.waitFor();
System.out.println("Terminal command '"+command+"' executed. Return code: "+returnCode);
}
lmao gay
How do I parse off a JSON value in Swift 4? The array/split technique is tiresome. Is there not an easy way to do this like there is in php or python?
let url = URL(string: "api.coinbase.com
let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
if let localURL = localURL {
if let string = try? String(contentsOf: localURL) {
var items = string.split(separator: ":")
print(items[4])
}
}
}
task.resume()
Why do you think a terminal window should be opening? Your code just executes a given command, all else is discarded. If you want the output of a given command you'll need to pipe it to a file and read that.
tl;dr command > output.txt
then read output.txt
Don't know what you're doing here, but either use JSONSerialization to turn it into dictionaries/array/strings/numbers, or make a Decodable struct that can be constructed from the json in question.
Also, you probably want a dataTask not a downloadTask here
Agreed, but it must be possible to open files via the terminal (for example an image on the desktop). How would I pass this such that I execute the Java method and the image opens?
>How would I pass this such that I execute the Java method and the image opens?
Try the command "feh ", I guess? Frankly I'm not sure there's a way to do it like you want. Is that actually possible on Windows?
PL design question: would it be terribly stupid to make memory allocation block until the request can be satisfied? So, instead of an outright OOM, you might use a timeout if you're interested in recovering instead of waiting for another thread or process to free up resources.
is it possible to do something like this below in c++ with or without macros? As long as it's fast.
(int x, int y) = getCoordinates(...);
std::tie
a solution to a problem that shouldn't be a problem.
Couple of ways not mentioned yet:
1) returning a struct:
struct Tuple {
int a;
char b;
};
struct Tuple doThing(){
Tuple r = {1, 'c'};
return r;
}
void foo(){
t = doThing();
//...
}
2) working with pointers:
void doThing(int *a, char* c){
*a = 1;
*b = 'c';
}
void foo(){
int a;
char b;
doThing(&a,&b);
}
Could also try xdg-open /path/to/pic. This will work with most DEs and opens the file in the "preferred program" like start in Windows.
Using something like "feh " doesn't seem to work directly, but creating an executable and running that seemed to work just fine. Kind of a shitty workaround, but I tried "feh " in a openPic.sh file and did "./openPic.sh" in the Runtime exec, which certainly works
But is it a decent solution? To be honest, my main reason for doing this is that it also makes it explicit that heap allocation is not suitable in a real time scenario. Everything that has an unbounded worst case time complexity in my language comes in a future, and real time things must be satisfied in the present (they can't be brought into a continuation of a future).
How would I use JSONSerialization here to turn it into a dictionary? I'm a Swift noob, python dev here used to 4 lines to get something done
Can we discuss how I might architect a project I'm doing?
My idea is to have multiple nodes (RPis) each with their own sensor(s). I'd like a master node to co-ordinate them all.
Would you have the master node hold a class on each sensor? Would you have the sub-nodes announce they have arrived and what sensors they have?
I'm trying to work out how this could be done flexibly.
Note: This is not homework, this is a genuine side-project I have for myself.
I tried the Bellman-Ford Algorithm but I don't know if I got it right. Maybe I just made it way too simple. I'll try to step it up with more nodes and see if I can do it.
It's just JSONSerialization.jsonObject(with: myData) where myData is the data you got from the server in your dataTask()'s completion handler. It's still a bit of a PITA because everything is an Any that you need to coerce to the proper dictionary/array/string/number at each level you descend.
Using Decodable support to turn the data directly into a usable format and error out automatically if it fails is more viable long-term but takes more work up front.
std::tuple getCoordinates();
auto [x, y] = getCoordinates();
std::tie(x, y) = getCoordinates
thanks that works with opening files and programs
but what would be the way of running e.g.a Python or JAR file and leave the terminal window open? For example, I can open the terminal and type "java -jar file.jar" or "python file.py". How could the same be achieved through java such that the terminal opens and displays output (e.g. print statements)?
Thanks, but I have a feeling there should be some easier way.
thx senpaitachi
Here's something interesting, lads.
type D a = a -> a
toD :: Monoid a => a -> D a
toD = mappend
fromD :: Monoid a => D a -> a
fromD = ($ mempty)
// toD . fromD = fromD . toD = id
D a is basically a more efficient way to use a as a monoid. (++) :: [a] -> [a] -> [a] is O(n), but (.) :: D [a] -> D [a] -> D [a] is O(1).
mappend = (++)
(*x).y
is the same as
x->y
can someone tell me why it was decided for the syntax to take such departure from the cemented conventions?
that is besides more readability
Where can I get some good material to learn about Algorithms and Data Structures?
I've also been recommended to learn about Theory of Computation?
what conventions are you talking about?
This is called a difference list. If you look in prelude, you'll see: showsPrec, shows, showParen, showChar, showString, which involve a type Shows = String -> String
you know there is a certain flow in C programs,its like writing poetry and this thing looks like an abrupt stop.
The point is it works with any monoid.
well list is the free monoid after all
You're just not used to it.
maybe,just seemed odd to me.
>using "C programs" and "poetry" in the same sentence
dumb frogposter
>C is pottery
#include
main()
{
int a,b,c;
int count = 1;
for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )
for(; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
}
c was targeting the least performing computer of the seventies. anything reducing the size of the source code was done. that's why you have ->, ++, --, reuse of keywords (static), int as the implicit type, ...
ahh makes sense.
CLRS or bust
>CLRS
MIT?
How is that possible in O(1)?
A value of D [a] looks like (\x -> _ : _ : ... : x), so appending two (D [a])s, as well as going from a D [a] back to an [a], clearly takes constant time. The O(n) part is going from an [a] to a D [a].
People consider this a programming language
brainfuck is more readable than this crap
Continuing my investigations into semantics for my language program.
Last week was syntax; I now have a pretty consistent system, I think. Hopefully this weekend I'll get to start programming.
It's a shitposting language. Nothing of value has ever been programmed in it.
>>variable = other variable
>PEOPLE CONSIDER THIS A """PROGRAMMING LANGUAGE"""
when did you first realise you were an NPC (non programming character)
what's the purpose of this if statement?
Here is what I have so far. Given the sentence
> Things are said to be named "equivocally" when, though they have a common name, the definition corresponding with the name differs for each
which is the first line of Aristotle's Organon, we get the structure
? says {
? names things "equivocally"
[when] (the definition (corresponding with the name)) differs (for each)
[though] they have (a common name)
}
agreed
If the character is not a null character.
why would it be a null character?
it's equivalent to
if (i != 0) { /* ... */ }
Is this the thinly veiled homo erp thread?
why, we need it for macros
We do?
no
my mistake, actually its equivalent to
if ((i & 0xff) != 0)
Runtime endian detection?
May invoke undefined behavior
what if
listen guys
what if
i spent most of my time figuring out a programming language
instead of actual getting things done with it
>Type D a -> a -> a -> a -> a :: D @a map a -> a -> a
wonderful...
It will execute the code in if statement if the system is Little Endian.
Little Endian: i = 0x01000000
Big Endian: i = 0x00000001
Thus, *c will be 0x01 for Little Endian, and 0x00 for Big Endian systems.
Replace *c with *x. I misread it.
help, I have a function that takes two arguments but I want to map it over a list of tuples, but the tuple is considered one argument
basically, i want this
f(a,b) but i'm given (a,b) not a and b by themselves
template
struct D { std::function data };
template
struct Monoid { std::function mappend; T mempty };
template
D toD(Monoid mon, T a) { return { [&](T b){ return mon.mappend(a, b); }; } }
template
T fromD(Monoid mon, D d) { return d.data(mon.mempty); }
>ah, much better
(setf c '(a b))
(f (car c) (cadr c))
in haskell, this is just
map (uncurry f) list_of_tuples
wtf is this language, looks stupid, too many parentheses
interdasting
ok, now this is epic
I could have used using, actually
Not much better though
>car
>cadr
wtf, is that really how you access elements of a list
It's one way of doing it
In Scheme, this is just
(define (tuply function)
(lambda (list) (apply function list)))
(map (tuply your-function) the-tuples)
why car though
it's like calling length "train"
what language?
In python, this is [f(*x) for x in list_of_tuples]
lisp btfo
python
>f(*x)
unreadable as fuck, wtf is *x mean lmao
you want the * operator then
it's the spread operator, same as JS
its a pointer to x
operator? is that a function or what
can I map that into a list?
ben?
Have a real programmer explain it to you
It's a special syntactic form that can be used in a function application.
If x is a tuple (a, b, c, ...) then f(x) is f(a, b, c, ...). Basically filling the role of apply in languages like Lisp and C++.
It's supposed to mirror the def myfn(*args): form.
Whoops, I meant f(*x) of course.
so it's a macro, not a function?
but I want to work with functions, I don't like macros like list comprehensions or that * macro, how do I do it using plain functions
So glad Clojure dropped such dumb names.
Well I suppose you could use a wrapper like lambda x: f(*x) if you want to make a function that takes a tuple instead of a regular argument list.
I wish it only dropped the names.
from functools import partial
def list_apply(f, args):
if size(args) = 1:
return f(args[0])
return list_apply(partial(f, args[0]), args[1:])