/dpt/ - Daily Programming Thread

Previous Thread: What are you working on my dudes?

Other urls found in this thread:

github.com/deeepaaa/rana
ideone.com/iaDscd).
twitter.com/SFWRedditVideos

Mobile apps on cordova

traverse_

>363
Holy shit I had no idea

Nice image

50 posts after bump limit, I think that was a /dpt/ record.

inb4 monads

>traverse_
Looks like a pointless waste of time.

Other languages accomplish the same thing with built-in generic set comprehension.

Unless /dpt/ is full of people who are really really bad at explaining things.

>You can validate a collection of inputs at the same time!

So can C#, Java, Python, etc. with built-in functionality.

>What are you working on my dudes?
Getting a job as a Haskell programmer

I'm not entirely sure what this is doing, but it looks extremely complicated for what I think it's trying to do.

It's like foreach but abstracts over how the results of each iteration are composed. In most languages, the results of each iteration must be unit (void) with a side effect to actually do anything useful.

The point isn't any single use case.

The point is that you can do many, many things with a single combinator.

But side effects are easier to understand. Why would you use it in a language where you have foreach?

>abstracts over how the results of each iteration are composed
What do you mean by this?

I'm genuinely trying to understand, but this does not make sense at all. What's the concrete implications of this?

What opinions do you homosexuals have on algoroithms.

Do you download libraries with specific algorithms and just use them or do you read up on all the theory and spend a day coding said algorithm yourself?

Most of the code is in libraries and you wouldn't have to write it yourself.

So that particular example is doing something simple with a clusterfuck of esotericness, right?

Can you show me an example of it actually making your life easier? Bonus points for some sort of readability for the average human.

Depends on the algorithm and if I need to do things within the algorithm.

More often than not, a library is the best way to go.

You can reuse the "Result" data type. The meat of what is happening is in the last five lines (three if you consider that the types could have been inferred).

In my example (), the results are being composed by either having success or a list of errors that occurred.

I made a ncurses interface to browse Sup Forums.
its needs a lot work but curses is fun to play with.

>Do you download libraries with specific algorithms and just use them or do you read up on all the theory and spend a day coding said algorithm yourself?
I use languages that come with good standard libraries, or I use a comprehensive add-on library like Boost for C++.

Python 3
Java 8+
C++ (with Boost)
C (with glib)

because there not, most modern languages are making for/while loops obsolete as its error prone to have to manually manage data instead of having iterators built into the container types

Give me a project idea that I can hone my algorithms, data structures, graph theory, etc. knowledge with. I'm fucking dying of boredom and a lack of things to do.

github.com/deeepaaa/rana

>imageboard
>no images

I seriously don't get why you have such a hard time understanding this.

1) It does not need to be a language feature.
It is a library feature.
2) Haskell has list comprehensions.
3) traverse_ is a transformation of traverse
traverse is like a yielding foreach loop, except it gives back the same kind of container you gave it

It's not remotely esoteric
traverse_ action [a,b,c] = action a; action b; action c
traverse_ action [] = {...}

traverse_ action Just a = action a;
traverse_ action Nothing = {...}

for rice mostly but there might be a way to include images

I was thinking about noise algorithms in this case and they seem to be pretty simple in theory until you see the implementation which often is several hundred LOC

Take one that is GPL licensed and copy it into your code, which is GPL licensed as well.

data Result a = Success a | Errors [String] deriving Functor
instance Applicative Result where
pure = Success
Success f Success a = Success (f a)
Errors es Success _ = Errors es
Success _ Errors es = Errors es
Errors es Errors es' = Errors (es ++ es')

foo :: Int -> Result ()
foo x | x < 10 = Errors [show x ++ " < 10!"]
foo _ = Success ()

foos :: Result ()
foos = traverse_ foo [5, 11, 73, 0]
-- foos = Errors ["5 < 10!", "0 < 10!"]

I made a lot of mistakes in the example. Fixed now.

Hey - I think I was rude to you in the last thread, I mistakenly thought you were arguing that traverse_ is useless because you can use for loops in C++ and accumulate the failures by hand. I'm sorry.

Accumulating the failures is an example of composing the results of each iteration. Another might be going off and doing an asynchronous operation and getting a future of its result. Combining those would give you a future that completes when the futures from each iteration complete. These might seem completely different but traverse_ manages to abstract over both of them.

there are a million algorithm/data structures books out there, pick one that is implemented in your favorite programming language and work through the exercises

It's the same as e.g. collection.map(|x| do_something_with_x(x)).collect::(); except less flexible

>GPL licensed

So you're saying I should use a language where I have to invent my own version of a foreach loop to have exactly the same effect?

@56212231
Do you hate freedom?

agreed, I am trying to create a dhex clone to get comfy with ncurses

So it's just like mapc in Lisp.

>using Communism: The Licenseā„¢

Fuck the GPL.

GPL is anti-freedom.

You're the only person in the known universe who believes this.

No, it's not.

r8 my queue implementation

#define DEFAULT_QUEUE_SIZE 16
template
class fast_queue{
int size = 0;
int sizeMult = 1;
T *p_fifo;
T *container = new T[DEFAULT_QUEUE_SIZE];

void resize(){
++sizeMult;
T *tmp = new T[DEFAULT_QUEUE_SIZE*sizeMult];
std::memcpy((T*)tmp, (const T*)container, DEFAULT_QUEUE_SIZE*(sizeMult-1)*sizeof(T));
delete container;
container = tmp;
p_fifo = container;
}

public:
fast_queue(){
p_fifo = container;
}
~fast_queue(){
delete container;
}

inline void push_back(T value){
if(size == (DEFAULT_QUEUE_SIZE*sizeMult)){
resize();
}
container[size] = value;
++size;
}

inline T pop_back(){
++p_fifo;
return *(p_fifo-1);
}
};

For the last fucking time, it's in the standard library

Remember when your language didn't have foreach? Bet you money GHC Haskell still had traverse back then

Possibly, I don't know Lisp
In Haskell, fmap and traverse are not the same.
The latter lets you do side effects across the structure, while the former has no "order" or side effects.

no, foreach loops are built into container types of most modern languages

Alright thanks.

I guess that's true but I tried to implement in C++ just for fun, and it does the job (ideone.com/iaDscd).

Obviously it's quite specific with int vector and the result type but you can make it generic with templates.

Exercises don't motivate me. I'm looking for a larger scale project that I can spend considerable time getting to work and that utilizes higher level theory.

Well it had sequence_ at least

>I am looking for something I have a chance to give up on

It's not nearly as generic. You have to replicate all that boilerplate for every different effect - composing Results is only one.

You actually can implement Traversable in C++ because it has HKTs.

...

I do realize that and I even said that it's quite specific to this one example but as I said you could for one make it work on any container holding any type of data by templating the traverse function. You could then further generalize it by templating the Result so that it can hold types other than a bool and message pair, not sure what else could be done there though. I didn't mean to say that it's exactly a one-to-one replica, instead I decided to rewrite the original snippet in C++ just for the fuck of it.

>implying Microsoft isn't dumping an insane amount of resources into all sorts of open-source initiatives
>real nigga-tier jpeg artifacts

What language?

I need to learn about image processing.
I'm writing an image viewer in C and I'm using OpenGL to do it.
Right now my thing to do is get it to scale images in a sane manner.
How do I do this? Do I do it in the shader, or by manipulating the viewspace, or some combination of both?
I really want to implement bicubic interpolation in my scaling.
What do?

its done with ruby. I like the nokogiri gem.

The signature of traverse_ could look something like this:
template
F traverse_(std::function, T);

F being Applicative and T being Foldable are up to the functions being defined, so errors on that front will not be descriptive.

With concepts, the C++ would be very similar to the Haskell.

>#include
you misspelled stdio.h

No, I did not.

Dependent function types can encode simple function types. The simply-typed lambda calculus has dependent types.

>types
>not spaces

>nullptr
Fucking why

>a implies b
>b
>therefore a

It's the best approximation to a unit type in C++.

struct unit {};

I'm making fun of the people saying C++ has dependent types.

It does have dependent types though.

I was thinking that it was worse, because sizeof(unit) is 1, but now I realize that sizeof(nullptr_t) is sizeof(void *). So I guess C++ has no satisfying unit type.

I just went for the built-in one.

By the same logic, the STLC has dependent types.

I don't know what the STLC is but I'm almost certain it doesn't have dependent types. I didn't even explain my logic so it's beyond me how you've managed to extrapolate.

C++ has dependent types via non-type template parameters.

Hows ruby? Any use case outside rails/sinatra?

an uglier perl

Your logic is that it has "compile-time dependent types", i.e. types that depend on "values" of a type of types (or integers), except these "values" must be compile-time constants. I.e., because templates can be encoded by dependent types, C++ has dependent types.

If you can't do this, it does not have dependent types.
template
struct array {
A elements[N];
}

void main() {
int n;
std::cin >> n;
array a;
for(int i = 0; i < n; ++i) {
a.elements[i] = i;
}
}

It has dependent types.
Compile time cannot receive information form runtime.
Compiled C++ code does not have types beyond inheritance.

It's not complicated.

It doesn't matter if it's compile or runtime, they're still dependent types. By your logic, Coq doesn't have dependent types, because none of Scheme, OCaml or Haskell have them.

I get it now. You guys don't understand how dependent types can depend on values obtained only at runtime, or you don't know that it's even possible. So you don't actually know what dependent types are.

But its made by jap! Can i write it in moon runes?

So you really think that Coq doesn't have them? How retarded are you?

its fine. It has a way to accomplish everything I needed so far. Its performs better than I expected but I think python is still a bit faster.

No, I'm saying that you're mixing up type erasure with what I'm doing in . Coq has dependent types, C++ does not.

there are programs and libs (libcaca IIRC) to "render" pics in the console

Imagine C++ didn't have a runtime

Well, it does have runtime behaviour.

Right, but imagine it didn't

>curses is fun to play with

Then you could say it has dependent types, but only usable in scenarios where they are equivalent to weaker types anyways.

me on the right

>then
Why then?

>equivalent to weaker
Nope

Learn the lambda cube before you start spouting about this stuff.

STLC does not have dependent types.
System F does not have dependent types.
C++ does not have dependent types.
Dependent types (with a type of types) can encode all of these type systems.

C++ has dependent types you fucking idiot

If it did, you could write .

The point of the pic was just to point out I already have it saved on my computer. I ocassionally use it as the /dpt/ OP image.

You seem to think that CT has dependent types but CT + _ doesn't.

CT?

No, you couldn't. No part of type theory makes any comments whatsoever on IO or side effects etc. (other than effect systems obviously)
Even induction isn't part of the standard COC so what are you even trying to prove?

>bought PC with windows 10
>dual boot linux
>linux keeps freezing everytime

what do?

Plus you can have the compiler be the runtime.
Output is the error log & input is a file that's #include 'd
I believe you could use comma seperated values (not a CSV file) inside a variadic template instantiation

You can easily roll your own.

Pic related is a piece of OP's image, straight from the URL.

I guarantee my code is not the most concise or clean way of doing it, though, but you certainly don't need a library.

>botnet linux
Why did you decide to have the worst of both worlds?

I don't even know where you're going with this. The way the value is obtained is irrelevant. If C++ had dependent types, you could write that. Period.

>he fell for the linux meme

I'll spell it out for you
IO IS NOT REQUIRED FOR C.O.C.

As you tried to avoid admitting earlier, you think that if there, hypothetically, were no runtime, then it would be dependently typed.

Just don't run the executable.

Working on software to capture data from canopen network. I have a canopen interface and the only way to read information from it is by polling. Not event driven. I've got a loop polling the port and use producer-consumer pattern to read information from the port.

Is there a better way than polling and producer-consumer pattern? My CPU usage spikes to 100% on the thread polling. I'm running the code on a resource starved embedded computer so it cause issues with other code.

We're talking about C++, not the CoC.

If it had no runtime behaviour, C++ would be effectively dependently typed, because you are limiting the language to the cases where dependent types are no stronger than weaker types like forall in System F.

If a tree falls in a forest with nobody around to hear it, does it make a sound?