/dpt/ - Daily Programming Thread

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

On web based controller for mpd problem is im not web dev so i cant make nice UI in this shit... Any ideas?

C++ is an amazing multi-paradigm language with no competition

>made before the bump limit
Weebshits getting desperate

Made exactly at 310 posts, you autist.

When will the DPT OP WARS end?
>leave for 2 years
>war still ongoing

will there be a documentary about it one day?

No, linked at exactly 310, but made 10 posts early, you autist.

What does that even have to do with programming?

post 309: 01:43:51 EST
this thread: 01:44:16 EST
post 310: 01:44:29 EST

stop posting

hey haskellfags
is
anime = "shit"


a function?

Its a function of your bad taste and general pajeetery

>lying on an anonymous image board

I really don't understand functional programming.

How would you write a program that writes a 32-bit integer to a 28-bit integer spread over 4 bytes where the most significant bit of every byte is skipped?

And how could you could do this in a pure way without using state?

Answer is monads, but seriously if you need to do that dont. Use tool appropriate for that, use C

I'm writing a Discord bot for my server since mobile versions lack a lot of administrative features plus it's fun Python exercise. I'm modelling it after BotServ's ZBot. Uses sqlite for persistent data (fantasy commands).

is there any redeemable quality to anime that can overshadow the bad plots, the awful voices, the bipolar characters and poorly drawn people in it?

Has anyone ever written an ERP bot?

You should write an ERP bot.

What's the best version available (for free) of "Cracking the coding interview" that I can get? And where can I find it?

How is writing a bot done?

>How would you write a program that writes a 32-bit integer to a 28-bit integer spread over 4 bytes where the most significant bit of every byte is skipped?
?
You split your number into array/list of bits
After that it is elementary

In case you missed, yea it is a function

There is quality anime dont watch weebo shit, its like judging all tv shows by spanish tele novelas or something...

You're watching seasonal trash.
Low-budget low-impact slice of life, harem and cute girls doing cute things anime is an acquired taste and you can't really learn to appreciate it until you've watched more anime.

Go watch some overrated, well-loved entry-level anime before attempting to watch cute girls doing cute things seasonal trash.

Try watching anime that aren't shit. I recommend Shin Sekai Yori.

it's the best form of escapism i've found so far. Perhaps i'll move to VR once it's developed enough.

Regarding state (your problem could be done without thinking about state)

Think of it this way
You have a function F and want it to use a state
So instead, you make this function return a function (that takes the state as a parameter)

F ... params ... = \state -> ... result ...
e.g., we want to increment our input by our state
F x = \state -> x + state
So we can then "run" this by doing
let result = (F x) state

Now this isn't very useful - we want to be able to change the state, obviously
So rather than returning 1 value, we also return the new state
F x = \state -> (x + state, state)
Now we can get out the result (on the left) and the new state (on the right)
let (result, state2) = (F x) state

So lets say we've got a function G that does the opposite of F
G increments the state by the parameter, and returns nothing (i.e. void) (we'll use the unit type () to represent void
G x = \state -> ( (), state + x)

Now we can call this to increment our state
let (result, state2) = (G x) state
-- result = (), we could just as easily avoid naming it, i.e.
let (_, state2) = (G x) state

-- we could also have just taken the result of our earlier function by doing this
let (result, _) = (F x) state


Now obviously it's not very convenient to do this:
let state1 = 0 in
let (_, state2) = (G 3) state1 in -- state += 3
let (result, _) = (F 5) state2 in --5 + state
result


(PT 1)

Is this satire?
Why the fuck would you do all this shit when you can do this in C with some bitwise fiddling and 2 nested for loops?

Rid yourself of the anime filth, and join the Visual Novel master race, where glory and Japanese Multinational jobs awaits you

So we make an operator >>= that chains lets us chain computations together, feeding the result state from one into the initial state for the other, and letting us manipulate the value it gave us
it works like this
stateful1 >>= (\result -> stateful2)
i.e., result is the result from the first stateful computation, and we use that result to make a new stateful computation (e.g. if x%4 == 0 then skip)

stateful1 >>= resultToStateful2 =
\state ->
let (result1, state1) = stateful1 state in -- run our first state computation
let stateful2 = resultToStateful2 result1 in -- get our new state computation
let (result2, state2) = stateful2 state1 -- run it
(result2, state2) -- return our final result and our altered state


Now we can do this instead:

let result =
let wholeComputation = (G 3) >>= (\GResult -> F 5) in
wholeComputation 0
-- GResult was just (), because all it did was modify state
-- we could have just done (\_ -> F 5) or const (F 5)


in Haskell we can use do notation to more concisely represent this

do
GResult >= (mostly) defines the behaviour of the monad

In fact, there's more to this.
In this example, we don't need a monad, all we need is an applicative - the difference is with an applicative we can't "get the result out" - but we don't use it, we use the state

So we can just do this
(G 3) >> (F 5)
or

do
G 3
F 5


(PT 2)

I'm explaining how the state monad works

He asked how you can do stateful computations in a stateless language

so now that we have our state monad & do notation, we can do stateful computations very simply

generally there are three functions commonly paired with the state monad - get, set, modify

get = \state -> (state, state)
-- leaves the state unchanged, but also gives it back as the result for us to peek at)

put x = \state -> ( (), x )
-- changes the state to x

modify f = \state -> ( (), f x )
-- changes the state by applying the function f
--We can implement modify using get and set:

modify f = do
state

what the fuck is this?

this is how you can implement stateful computations

user is doing gods work, its pretty good tutorial

it's how to do bitwise bit fiddling in haskell

guys need help

i have 4 vectors
std::vector current, toadd, toremove, newobjects

every few frames i ask user to generate list of relevant objects, now, before i copy all newobjects to current i have to know what objects are actually new and add them to 'toadd' list, and objects that are no longer relevant and put them in 'toremove'. how do i do it?

You'll be using one of the APIs to do it. Real simple stuff. Just set up a bot account, get the auth token and put that in your login script, then authorize the bot to join your server. After that it's just scanning every message looking for whatever keywords you programmed it to look for and writing appropriate responses.

I'd suggest using the javascript or python APIs

>Has anyone ever written an ERP bot?
Not that I know of.
>You should write an ERP bot.
That'd be pretty cool if I made a ERP bot that got popular and I started making that Fenoxo money.

Why would ANYONE want to do that?

Some problems are best expressed using a state. So if you need states you use state monad

you dont have to implement it like user above it already exists in standard library

'tism

As I said, you can do what the other user asked without this, this is just an explanation of state

In fact the commonly used foldl (or "reduce" or "accumulate") does stateful computation over something that can be put into a list

well you don't have to worry your little head about it, you can just add MTL to your cabal and then do
import Control.Monad.State

How can that poor excuse of a language be useful in any real-life setting?

Even fucking JavaScript, aka Scheme with C syntax for your browser, can do bit-fiddling.

I'm more than certain most anons here weren't healthy to begin with before they came here.

You have to do that all that shit fuckery to implement a state machine?

Jesus Christ, no wonder Haskell fags are hostile and militant about their language on this board.

You dumb autists didn't get his point. Why would anyone want to do that shit in Haskell?

>well you don't have to worry your little head about it, you can just add MTL to your cabal and then do
>import Control.Monad.State
I literally told him he doesn't have to, someone else did

The point is this is an explanation of "stateless state"
Maybe it would help if you think about it this way

f x = x + 2 -- stateless function
f x state = (x + 2, newState) -- stateful function
>> -- something that lets us sequence stateful functions (ignoring their results, e.g. void functions)
>>= -- something that lets us combine stateful functions and get their results

I answered why you would want to use state in haskell didnt I? AND YOU DONT HAVE TO IMPLEMENT IT just import it

>The point is this is an explanation of "stateless state"
What kind of fucking mental gymnastics is this?

This is the whole "IO monads are stateful containers, but Haskell is still pure because only the runtime is tainted" backwards shit logic all over

>import impure state monad
>"b-b-b-but haskell is pure because the code is only RETURNED and only the runtime is tainted"

I put it in parentheses for a reason.
This is state and this is pure.
You don't like that but it's true.
It will give you the same result for the same inputs no matter how many times you run it.

the state monad is pure, it's Haskell that is impure (as well as the IO and ST monads, ST is a thread)

I never said that, i actually didnt say anything about purity. I answered question: Why would you want to use state in haskell

>the code IS pure, only haskell runtime is impure
M E N T A L
G Y M N A S T I C S

>modifies state
>pure
Pick one

>It will give you the same result for the same inputs no matter how many times you run it.
Then it's not a state machine, you fucking idiot, and your example showed nothing.

Haskell is a language by academics for academics. It's one of the primary reasons it doesn't see much use outside of it's dedicated user base.

It takes "Purity" too seriously. This just over complicates things with mathematical concepts where other languages take a more practical approach.

IT DOESNT MODIFY STATE it only appears to be, nothing in state monad is impure

You could at least read what I wrote

It gives you back a state machine.
You still have to put the initial state in, to get the output state.
i.e. run the state

there are normally 3 functions given for this
runState -- gives you back the result and the final state
evalState -- gives you back the result
execState -- gives you back the final state

This isn't true

>this is how you modify state in Haskell....
>but it's not modifying state after all lol

>only the code """"returned"""" is impure, the code itself is pure
Fucking autism

It performs stateful computations
>> and >>= thread a value (the state) through the computations, giving you back a state machine that asks for the initial state
then you give it the initial state

then it gives you the final state

Problem is these faggots dont know anything about haskell so they attack their own interpretation of it...

Oh, care to enlighten me then? I do not see how emulating state change and introducing structures from category theory isn't more complex.

Look, it's EXTREMELY simple
Is this function pure?
myFunction x y = (x + y, y - 1)

Is this function pure?
myFunction x state = (x + state, state - 1)

Is this function pure?
myFunction x = ( (), 100 )

Is this function pure?
myFunction state = ( (), 100 )

int foo(int j)
{
static int i = 0;
return ++i + j;
}


This code is pure too, as only the runtime (aka the OS) is tainted. The code itself isn't impure, the code returned by GCC is.

It's not that complicated, the idea is just that you make functions with an extra parameter (called stateful functions), that return an extra value (the result state), and then you just chain them together
Then you feed in the initial state

here

here's the non-autistic version
/* write 32-bit int as 28-bit 4 byte array, skipping most significant bit */
#define BYTES 4
#define BITS 7
int i, j, k = 0;
for (i = BYTES - 1; i >= 0; i--)
{
arr[i] = 0;
for (j = 0; j < BITS; j++)
{
if (integer & (1

every [] n = []
every xs n = (take n xs): ((drop n xs) `every` n)

bytes (bits) = (bits `every` 8)
-- every 8 elements of bits

clearHighest [] = []
clearHighest (True:xs) = False : xs
clearHighest (False:xs) = False : (clearHighest xs)

sample = concat . (clearHighest ) . bytes

I've only done some preliminary tests but I think this does what user wanted

Working on a shitty imageboard using php and mysql, even tho i made a previous versione w/o db

It is indeed more complex than any other language that just bites the bullet and accepts mutable state where you just define the variable and then change the variable.

Whoops, "every" doesn't work as expected

C++ added constexpr
D added pure

Actually ignore me, it does work, I just messed up the tests

>programming in haskell
>not formally proving his functions
Might as well just program imperatively if you're not going to do it properly.

tell me what user wants, I might be able to implement it

It's not more complicated when you just import it

It's like asking why C++ bothers with the entirety of vector.cpp when they could just bite the bullet and accept builtin vectors

Plus you get the benefit of the standard library, which all have functions that work with monads in general, such as traverse_ or forM_

In the end you could write Haskell code that looked imperative if you wanted

constexpr is strict and is basically the same as pre-calculating and inserting the value into your code by hand in most of the use cases I've seen it in.

The constraints on constexpr generally limit it to use cases that allow the compiler to evaluate it. You're literally saying either the values in the object are able to be evaluated at compile time or the result of the function is able to be evaluated at compile time).

Don't mind the Haskell autists. You can do that without using state in a more sensible language quite easily (though it wouldn't be as efficient as using state):
(defun elim-8th-bits (n &optional (exp 4) (acc 0))
(if (= exp 0)
acc
(test n
(1- exp)
(ash (+ acc
(logand n (ash 127 (* 8 (1- exp)))))
-1))))
Here logand does a bitwise AND and ash does a bit shift.

Haskellfags completely REKT by Lisp

I lol'd

samefag

>test n
Change it to elim-8th-bits n. Forgot to change the function name.

>he doesn't know about the post cooldown timer
Hello newfag

>what are proxies

constexpr functions can be called at runtime, but if they're called with constexpr parameters than they can be evaluated at compile time

that's because they're pure
you can say the same of state in haskell (but I don't think GHC optimises State, as it's user code and not part of the standard library (though it should be added!). ST is, and you can do any state with ST)


see

>he thinks anyone is emotionally invested enough on a /dpt/ to not only enable a HTTP proxy or a VPN, but also clear cookies, all in less than a minute

>click "VPN on"
>CTRL+SHIFT+DELETE, "Clear Browsing Data"

Yeah there's no way an idiot like you could press 3 keys and mouse click twice in a minute

>What does that even have to do with programming?

It's a meme that every /dpt/ has to feature that pink haired anime gril.

Hold on, that doesn't add the last bit to the result. This should fix it:
(defun elim-8th-bits (n &optional (exp 4) (acc 0))
(if (= exp 1)
(+ acc (logand n (ash 127 (* 8 (1- exp)))))
(elim-8th-bits n
(1- exp)
(ash (+ acc
(logand n (ash 127 (* 8 (1- exp)))))
-1))))
The fact that the same sum repeats itself in both the base and recursion cases shows this would look much better iteratively.

>he nonironically believes people take the /dpt/ this seriously

How sad must your life be?

>gril

You're still replying?
I didn't actually think you did samefag but now I'm starting to change my mind

And here it is iteratively, with a few tweaks:
(defun elim-8th-bits-neurotypical (n)
(do* ((exp 4 (1- exp))
(acc 0 (+ (ash acc -1)
(logand n
(ash 127 (* 8 exp))))))
((= exp 0) acc)))

>I didn't actually think you did samefag

I'm not the first guy you replied to

and i won't be the last

Who here /dlang/?
Is it any good?

This translates quite well to C code, barring syntactical differences.
int elim_8th_bits_neurotypical(int n)
{
int exp, acc;
for (exp = 4, acc = 0; exp > 0; exp--)
acc = (acc >> 1) + n & (127

It's better than C++.
Probably.

>4u

...

>jokes on you, i was only pretending to be samefagging

What the fuck is going on in your brain right now? Are you having a stroke?

epin
That's the last recourse of Hascucks when faced with a superior language, cry "muh parentheses".

Ignore him, he's not actually me
I wasn't

You missed the other week when a Haskell user was complaining about a sample of Lisp code being unreadable, and a Lisp user was complaining about the number of parentheses produced by a sample of Haskell code

can your language do this??

$ cat hello\ world.hs
main = putStrLn "Hello World"
$ ghc -O2 hello\ world.hs
$ du -h hello\ world
1.2M hello world

#include

>1.2M