/dpt/ - Daily Programming Thread

What are you working, Jow Forums?
Old thread:

Attached: 1515584981438.png (400x400, 120K)

Other urls found in this thread:

ziglang.org/documentation/master/#C
crystal-lang.org/docs/syntax_and_semantics/c_bindings/
cons.io/
lambdanative.org/
twitter.com/SFWRedditVideos

python best lang

>new thread before bump limit
retard animuposter

What are some languages with an effortless C ffi?

D
It's also going for some C++ interfacing

Nim and D. I would say Go too but it suffers from a massive FFI calling overhead.

Nim.

Attached: Nim.png (1102x1002, 120K)

foreign import ccall "exp" c_exp :: Double -> Double

D can call the C libs directly
void main()
{
import core.stdc.stdio : printf;
"hello world\n".printf;
}

>we don't make a distinction between O(n) and O(2n) because the constant member becomes irrelevant by using a better processor

Nani the fuck I'm reading

C++

Constants are usually not considered for time-complexity. Due to Moore's law, iterating 2N elements would take the same time as N elements in 18 months.

every day there is yet more proof that Haskell is the ultimate language for imperative programming

>imperative haskell
show me lol

I'll take their word for it, but to me it will always be twice as slow

O(2n) is only meaningful when compared to O(n) though, on its own it means nothing. big O notation tells you about how the complexity of the algorithm changes with the size of the input

It is, but explained it better.

What do you mean?

the only way haskell would ever be imperative is if you built a lambda calculus processor

what

Attached: 1465542231484.jpg (454x558, 159K)

>balancing an arbitrary BTree
>arbitrary
There's no hope, keep poping the elements and add them to a newly created tree. Then set the old pointer to the given tree to the new tree.

zig ziglang.org/documentation/master/#C
crystal crystal-lang.org/docs/syntax_and_semantics/c_bindings/

imperative means you are writing code that does that tells the system directly what to do with no ambiguity
assembly is the ultimate imperative language
the opposite is declarative, which is where you say what you want to happen but leave the implementation details up to the computer
all functional languages are declarative because processors don't operate on a lambda calculus model, thus the code needs to be converted to normal procedural style, which has alot of ambiguity involved

What is the closest thing to a private subnet in qemu/libvirt? I need to emulate a network infrastructure via kvm, with a main network for internet access and various subnetworks with hosts inside them.

looks imperative to me

import System.Environment ( getArgs )
import Data.IORef ( newIORef, readIORef, writeIORef )

-- imperative helper functions
new = newIORef
get = readIORef
put = writeIORef

pop listRef = do
(head:tail) return False

when boolRef body = do
bb body
False -> return ()

unless boolRef body = do
bb return ()
False -> body

while :: IO Bool -> IO () -> IO()
while condition body = do
more do
body
while condition body
False ->
return ()

-- haskell imperative language is best imperative language
main = do
argsRef >= new
firstRef

that's wrong user

>Trying to do nested loops in C
>For some reason the program doesn't exit out of the inner loop and runs endlessly.

#include
#include


int i, x, z;

int main(int argc, char** argv) {

i = 1;

scanf("%d",&x);

while(i

>tells the system directly what to do with no ambiguity
even C fails this test, retard

what

Attached: 1517483975801.jpg (328x353, 21K)

doesn't look imperative to me because it's written in haskell

this better be homework, because if you can't see the error here you're a little retarded

>this guy doesn't compile his haskell to vhdl fpga layouts

nope

it's not a test, it's a scale. No language can be considered 100% imperative because CPUs optimize machine code as it runs. Assembly is just the most imperative language, and things like C are close because their abstractions map 1 to 1 to Assembly with little ambiguity. Functional programming is at the opposite end of the scale with things like lazy evaluation and all the caching the undoubtedly goes on to leverage the advantages of pure functions

it’s sspelled rarted

imperative doesn't mean "close to machine"

practically it does. you're issuing commands to the machine to tell it what to do. If processors were lambda calculus based, then writing functional code would be writing machine code, it would be imperative

>because CPUs optimize machine code
Not at my machine

no it wouldn't you idiot

Just saw what was wrong with it and fixed it.

it would. imperative is issuing commands. if your processor evaluated functions, writing functional code would literally be issuing commands to your processor. As it stands right now functional code is just something for your compiler to interpret into state-based machine code

functional languages don't automatically cache any more than imperative ones do. the optimizations there are largely the same.
I get what you're saying, but your reducing the concept to absurdity, which is why you're getting so much blow back.
imperative means you tell the system how to do something, declarative means you tell it what you want done
Even SQL is imperative if you know how all the backend machinery of it works. You know what kind of methods and performance you'll get from specifying joins in a given order or with given indexes.
Declaring that only assembly is imperative isn't useful. I was taking the piss with the imperative haskell example, but that really is imperative code so far as that sort of thing matters to other programmers.
I told it to mutate a set of variables following a particular line of logic. That step by step imposition on the process is the root of things.
To use a metaphor, you're not wrong with your insight, you're just screaming the world isn't really round since it's slightly bulged at the equator and has ridges and mountains, while failing to see why calling it "round" is useful for the common less granular view

good man. debugging is a pain in the ass, but it's a skill you have to have to be a good programmer

I think I'm in love again~~
fn get_default2 &'m mut V {
if map.contains(&key) {
return match map.get_mut(&key) {
Some(value) => value,
None => unreachable!()
};
}

map.insert(key, V::default()); // OK now.
map.get_mut(&key).unwrap()
}

>functional languages don't automatically cache any more than imperative ones do. the optimizations there are largely the same.
so are you saying that if you call a function twice with the same arguments it isn't using a cached result? that's like, the entire point of why you would use FP. Optimizations like that are highly ambigious but given the right domain, they're incredibly useful. And like I already said, imperative-declarative is a scale, not a boolean

wtf. why the hell would you have a function named get_default insert the default value every time you have a miss instead of just returning the default value?
if you scan a hashmap for a series of values, you're going bloat the shit out of it with useless default value entries

wtf

gcc -o program.c
gdb ./a.out
b 5
r
p i
p x
p d

wtf

I have my reasons :3

C can and does do the same thing. But those optimizations are always super localized in any language, because having a fat dictionary of values you might or might not need isn't a good optimization.
Haskell doesn't do any automatic memoization or anything.
The only weird optimization it does is keeping the computed values in lists that have infinite declarations. If you take the 50th element, you'll have all 50 in memory.
If the list is declared outside of a function so that it has global status in the gc, you'll be stuck with them, so don't take the 10000th item.
It's more of a pain in the ass than anything.
C has no generators or lists built via continuations, so any similar optimization doesn't make sense there.
But your average function isn't saving it's return values. Haskell just compiles really, really well, and will compute them again, just like C generally would
( either language can and do fold them into constants if they only take constants at compile time )

Maybe you’re privy to some new information but for the past 20 years, if you call a function twice in a functional language it isn’t using a cached result.

>cuint
watch your language

>Haskell doesn't do any automatic memoization or anything.
Seems wierd to me because that's the biggest advantage of having everything be pure functions, you don't need a big dictionary of values, you can do a much deeper compile-time analysis of pure functional code and optimize the hell out of it to the point it might not even resemble the code you've written

It's a common misconception. I had it myself before getting corrected and learning more about it a decade or so ago.

then why do people call functional languages declarative?

The truth is ugly.

my earlier example was tongue in cheek, all written in the IO monad.
in haskell, most functions don't touch "the world", and so the compiler is free to manipulate and change and twist and turn them as it will
they are much closer to SQL style declarative than C style imperative. but you can write imperatively in haskell if you find it useful, which is what I did.
C is much harder to optimize because every statement in it can be telling the compiler to change state in the program, so the optimizations must be much, much less aggressive.
in haskell, if I say to map a function over a list, haskell can and will return without having done so, and only actually map the function over the list as values from the resultant list are required to satisfy those imperative functions running in the IO monad.
it can leave work undone if the values are never used, and just forgets them via gc afterwards
functions don't memoize because it's a bad optimization to force, since it trades a lot of space and lookup and save time in order to maybe speed up some calls
better to leave it to the programmers discretion there

Most of them aren't declarative.

all lists, all expressions, all function calls, all of it just gets initially tossed back as a thunk, a function the runtime can follow if need be, or can ignore if it wants.
this is what laziness means
and why some call it "declarative"

>functions don't memoize because it's a bad optimization to force, since it trades a lot of space and lookup and save time in order to maybe speed up some calls
you can determine at compile time where functions calls can have the same result, its a zero-cost optimization

that is, you only write a definition for what you want done, you don't specify how it gets done.
that's constant folding, not memoization
memoization is remembering arguments to lookup return values instead of executing the function body

It's called declarative because you can do this

Attached: &&.png (272x130, 7K)

using top level pattern matching for an operator definition doesn't make something declarative

congrats on totally missing the point

How come you never congratulate me but you'll congratulate user in public?

be quiet you failure

Attached: 1454233711543.jpg (540x675, 99K)

You don't evaluate the function at compile time, you identify calls that will have the same input at compile time to reduce the total calls,like

function Lol(arg)
return Rofl(arg) + Rofl(arg) + Rofl(arg)
end

only has to evaluate Rofl once everytime you call Lol

that's still not memoization, and that optimization is also done in C if the functions are declared pure or the compiler proves they are.
as I said earlier, it's often fairly localized optimization to make
If two different functions call Rolf with the same args, they're probably just going to call Rolf twice.

>declared pure
GCC extension before you turn the blade on me :)

Is there anyone that's good with EF Core here?
I'm conflicted as to how I should handle eager loading for a specific query, since my lazy loading with proxies is exponentially slower than eager loading in a few cases.
But I don't wanna disable lazy loading for good, since it's really useful for a lot of the rest of the things I do.

Attached: 1488107604585.png (1000x1047, 997K)

"C with GCC extensions" is not C

I wanted to thank the anons that pointed me to python for beginners.I'm enjoying learning this stuff and I feel less lost thanks a lot anons.

Pretty much everything that isn't some weird old compiler tech like COBOL.

I know, I know. But if you want to declare a function pure to help the optimizer undo your repetitive shit code, you'll mark it anyway.
The compiler can still prove the function is pure if it's in the same compilation unit to perform the optimization.

Your code won't even compile because K is not Hash + Eq, which is needed for HashMaps. The lifetime is not needed, it can be elided.
Also, just use the Entry API:
fn get_default2(map: &mut HashMap, key: K) -> &mut V {
map.entry(key).or_insert_with(Default::default)
}

Attached: 1521471013628.jpg (675x949, 112K)

Actually, Entry has a default method already:
map.entry(key).or_default()

gods rust is fucking ugly. and people bitch about C++.

All C-like languages are ugly.

wtf

/dpt/-chan, daisuki~

Ask your much beloved programming literate anything

Thank you for using an anime image.

Attached: shot0033.jpg (1920x1080, 310K)

>Ask your much beloved programming literate anything
CHICKEN or GNU Guile for gamedev and why?

I mean it doesn't have to be pretty just efficient no?

Code is read many more times than it is written.

Code must be correct first, fast second. If it is wrong then it will never be fast. Readability and conciseness aid understanding and make it easier to produce correct code. Here 'pretty' is shorthand for readable.

>Code is read many more times than it is written.
Code must be paid zeroth, correct first, fast second.

i am not familiar with both but chicken compiling to c, it's easier to interop with it (you can mix scheme and C code together). it also avoid stuff like bignum or complex numbers by default, you don't need that for video game development.
there is a new scheme: gerbil. see more here cons.io/
what game do you want to make?

Attached: dogot.webm (480x360, 1.66M)

>There's no mutability specifier called const in the language
>Can't switch over a string because supposedly it's not constant
>However just replacing the switch with if/else makes it ok somehow
BASED AND REDPILLED

Attached: 1536063434957.jpg (1000x1000, 80K)

Ok.So much to learn.

here an example of mixing scheme and c code with chicken-scheme
(define my-strlen
(foreign-lambda* int ((c-string str))
"int n = 0;
while(*(str++)) ++n;
C_return(n);") )

(my-strlen "one two three")

Attached: lisper_be_like.webm (240x426, 939K)

shit that's cool

I want to fetch the first 8 images from a google images search, given the keyword, in Java, any tips? dont know where to start

Attached: perdiste.png (77x65, 43K)

I plan on making a relatively simple crpg at some point. Nothing fancy graphics-wise. I know I could do it in Lua or something instead but I like Scheme a lot more than Lua.

Just download 8 pictures of black man - white woman couples, it's the same either way

>For instance, on KNC, we have found that reducing the number of target regions can improve performance, as they incur significant implicit synchronisation overheads, but might require extending the scope of the regions such that they include instructions that should be performed on the host for portability. Importantly, the target synchronisation overhead will not be an issue when targeting NVIDIA GPUs with OpenMP 4.0, as the compiler implementations can leverage the performant asynchronous queues provided by the CUDA runtime
how can you offload computation to a compute device asynchronously? what about memory coherence my anonymous friends? dont u want to let the calculation finish before proceeding? I dont think RAM have DIRTY bits dont they?

maybe lambda native will give you everything you need to do that.
lambdanative.org/
there is also Kawa scheme that will give you access to the java ecosystem. Kawa does mean river in Japanese.

any books on string parsing algorithms?

Thanks. Would prefer to avoid the Java ecosystem though.

>the language
What fucking language?

Why? Java's ecosystem is rich and vast.

Attached: 4LbbDEv.webm (350x198, 708K)

Based Java