/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?


Previous Thread:

Other urls found in this thread:

wiki.haskell.org/Zygohistomorphic_prepromorphisms
isomorphic.net/javascript
youtube.com/watch?v=ad4BVmPni7A
twitter.com/SFWRedditImages

how do we fix this general

A monad is just a monoid in the category of endofunctors.
What's the problem?

Install Gentoo onto it

command line static website generator

>general
>fix

>be me, HR
>looking through candidates
>one is much more suitable for the job than the others
>check his facebook
>pepe the frog
>into the discard pile

>Bought books on Java and Python to self study
>Haven't done anything with them in almost 2 weeks
>Fucking apathy

Also, why are the easy leetcode questions so hard?

going through the unix programming environment. made a palindrome detector with egrep

more like pajeetcode

Good, you prevented cancer

I wish that was a true story

Is there a monad to make Haskell good? How about a monad to make it strict?

putting /usr/bin/killall -9 uwsgi.py in the root crontab

Haskell is already good, and you can use deepseq or unboxed for strictness

>Tell myself i'll read my Python book in between semesters
>It's still closed
At least now I have an idea for something fun I want to make so maybe i'll crack it open.

How many iOS developer are here? I rarely see Objective-c / Swift in this board.

How's my confirmation UI?

youre pretty much the only one here lad.

Alright champ. Post some Haskell code to calculate
mean [1..3e7]

without stack overflow because heaps of garbage or taking longer than a minute to calculate.
No (n + 1) * n / 2 allowed.

>friend classes
when will it end?

I think if you issue a challenge, you should probably provide yours as well to compare to.
Not Hasklet-kun though.

gf classes

I'm about to get into Swift.
How is it in your experience?

>video
I love it. Every time I see it posted I have to watch the first 2m and 30s.

I like it, really simple sintax, year ahead of Objective-C's mess

I think iOS in general is more aligned with people who'd go to reddit. Just a feeling. No offense meant.

I like it, really simple sintax, years ahead of Objective-C's mess

Was it necessary to make and delete two posts just to correct a simple typo?

I don't see many Android developers here as well

*syntax

;^)

Do you have a better alternative for practice problems?

I dun goofed, sorry about that

>write this code
>but you're not allowed to optimize it

Cool, what apple computer are you using?
I've got an old 2012 mac mini I'm using for the occasion.

>3e7 elements
Does length even do this?

fug

Who'd develop for Android though? From what I hear it's just not feasible to make android apps for money. And it shows on the play store.
Only decent software is open source projects.

mid 2014 base 15 inch rMBP, no dedicated graphics (Iris Pro 5200). Pretty great display/build quality, performance on anything that isn't 3D is godtier.
> I've got an old 2012 mac mini
Mac Minis are decent little machines, I wish Apple would update them.

>Only decent software is open source projects
There's plenty of open source Android and even iOS software out there though.

Yeah that's what I'm saying.

import Data.Vector.Unboxed as V

average :: (Unbox a, Fractional a) => Vector a -> a
average f = V.sum f / fromInteger (toEnum (V.length f))

result = average (V.enumFromTo 1 (3e7 :: Double))


Unfortunately Unboxed Vector doesn't implement Foldable due to having an Unbox constraint.
I believe Monofoldable fixes this.
Also a better genericLength would be nice, and called gLength.

Then you could do

average f = sum f / gLength f

As God intended.

There are many ways of calculating it, for example:
data StrictDouble = SD !Double
data StrictInteger = SI !Integer

meanA :: [Double] -> StrictDouble -> StrictInteger -> (Double, Integer)

meanA [] (SD sum) (SI len) = (sum, len)
meanA (x : xs) (SD sum) (SI len) = meanA xs (SD (sum + x)) (SI (len + 1))

mean xs = sum / fromInteger len
where (sum, len) = meanA xs (SD 0) (SI 0)

Takes like half a minute to run. It uses strict types.
The thing is that you can't reason at all with Haskell, because even if you profile your shit, you will have to resort to throw strict butter at it until it sticks. Laziness is a curse.

C can calculate that shit in under a second, no garbage required.

That's to save you the trouble of generating random 3e7 elements, but be my guest if you want to.

>Are you doing some funky memory ordering to have the array sorted but non-linearly in for cache-line efficiency?
What does this mean?

Yeah i really never understood why default laziness is a thing. Makes absolutely no sense.

both C and C++ allow this. (up to a point, 10e22 is the upperbound iirc)
(C++17 allows 1p7 as an alternative syntax.)

and apparently C99 also allows 'p' in place of 'e.'
I learn something new every day. I don't think I've ever seen any C that uses 'p' for exponent/normalized notation.

I want to learn a function language to see what it's all about, but I also want to get some use out of it after I learn it, should I learn scheme, erlang, haskell, or something else?

How mind-blowingly silly of me

In the REPL, it turns out

foldl' (+) 0 [1..3e7] / 3e7 :: Double

Is actually quite fast!
(Not C fast - but it is interpreted in the REPL, after all)

>/ 3e7
No point hand-writing a length encoded list
Consider this equivalent to passing in the length for a C style array.

learn F#

what languages have you used before?

elixir or f#

Not that guy, but I've gone through a lot of the easiest problems on a few code challenge sites, and you should try codewars. I know it has a lot of challenges for both Python and Java, and the kata system is good for finding problems that are at your skill level. Codewars is somewhat math-oriented, but I still found plenty of problems (in C++) that didn't require much beyond basic algebra.

Kattis (open.kattis.com) is also decent, but it's a bit more math-oriented than Codewars. Use the submit.py script, it's a lot easier than using the website to submit answers.

Then there's exercism (exercism.io). It's focused on getting feedback for your work, and the submission process vaguely resembles using git (basically, you "pull" the problem description, and then you "push" your solution - not 100% accurate, but gets you acquainted with submitting work using a terminal). I've gotten good feedback on my submissions through it as well.

Was that after memoization was applied?

I literally just put that into the REPL.
(I imported foldl'/strict foldl from Data.Foldable first)

import Data.List

data S a = S {-# UNPACK #-} !Integer {-# UNPACK #-} !Double

mean :: [Double] -> Double
mean xs =
let step (S n t) a = S (n + 1) (t + a) in
let S n t = foldl' step (S 0 0) xs
in t / (fromInteger n)

main :: IO ()
main = do
putStrLn "Starting calculation..."
result

Would appreciate your own pitch for them if you don't mind.

C, C++, Python2, Go, PHP, JS, and maybe other less relevant things. I mostly use and enjoy Go now, using C and C++ where appropriate/needed. For web stuff I've considered picking up Typescript.

Do you mean Int? You can't unpack Integer

Also, if you use MagicHash and UnboxedTuples, you could be using

(# Int#, Double# #)
as the state - but that wouldn't work with foldl'

I'd like to inform you, my enlightened fellow, that operator overloading is a big cause of failure to inline.
I support the general attempt but don't do operator overloading for another 20 years or so. Maybe then the technology will be there to support it.

No it isn't.

Am I missing something? Are haskell people tripping over themselves implementing a simple mean?

I suggest you inspect your code that makes heavy use of operator overloading.
I've had plenty of instances where a function external to the operator overloads (in a piece of code that used operator overloads heavily) failed to inline until I rewrote it to SSA form.

It's rarely worth doing. I can see the case for vector operations maybe since they're not that common to nest deeply.

>multiple people give you straightforward fast solutions
>1 is even very close to the typical solution

>h-hhaskell btfo! why not use C
>meanwhile C can't even average 2 ints

Thanks user, I do indeed mean Int.

This is now pretty fast:

import Data.List

data S a = S {-# UNPACK #-} !Int {-# UNPACK #-} !Double

mean :: [Double] -> Double
mean xs =
let step (S n t) a = S (n + 1) (t + a) in
let S n t = foldl' step (S 0 0) xs
in t / (fromIntegral n)

main :: IO ()
main = do
putStrLn "Starting calculation..."
result

5 seconds, that's pretty good. It still generates quite some garbage.

You chose the right flavor combo of foldl'.
result = sum / fromInteger len
where (sum, len) = foldl' (\(x, y) a -> x `seq` y `seq` (x + a, y + 1)) (0, 0) [1..3e7]

This takes also half a minute and generates as much garbage as the Vector implementation. And if there were no seqs then that would be a stack overflow.

There is no substantial difference between an operator and a method or a free function depending on its context.

have you never seen them trying to calculate the Fibonacci sequence?
or poorly implement the Sieve of Eratosthenes (by not actually implementing it..)?
its all they can do.

Thanks user, that's pretty neat. I ought to sit down and actually learn this stuff instead of half-remembering bits from talks

Maybe you shouldn't use an arbitrary precision Integer then

>depending on context
Yes obviously. The problem is the cases operator overloads cause. You start writing in the wrong form for perfectly understandable reasons.

Go ahead and profile your code. See where you have many hits on your functions and look for lack of inlining. C++ compilers really aren't up to task.

Of course you might not care about the performance. Maybe the operator overloads save you enough trouble that it's worth it. That's fine. I'm just stating my experience.

How would haskell average 2 ints?

Surely overloaded operators are inlined in exactly the same places methods would be inlined?

This is a function problem, not an operator overloading problem

mean x y = (x + y) / 2


It's a dynamically typed function, you can use it with floats too

No one knows what the hell a monoid or an endofunctor is.

That's what he gets for having a Facebook.

To be fair, many of the people posting here aren't going to make money off of the applications they develop.

>dynamically typed
polymorphically typed
there is a huge difference

You're not getting it. Yes when you're down at IR level functions and operators are the same. It's the usage that ends up being problematic.
See above. Yes in some sense we can say it's just function inlining that's insufficient. But littering your code with calls upon calls (especially ones with implicit 'this' pointers) is not giving the compiler an easy job.

An endofunctor is a morphism preserving mapping from a category to itself.
A monoid is an algebra defined by (M, 0, +) where 0 is the zero element for + and + is associative

Fractional a => a -> a -> a

It was a joke anons, also trying to get the Pythonistas interested in Haskell

Your complaint is that operators allow you to write lots of function calls in a short space.

>this pointers
Method problem.

all of this programmer jargon intimidates me, as a little boy interested in CS

What you should have said is that Haskell has statically type safe runtime dynamics.

A monoid is a typeclass that lets you combine two things of a type, with an identity element and associativity

t. Scala, keeping things simple

Yeah that's exactly what I've been saying. I dunno you repeating this is making me think you don't understand this as a problem. In the context of current C++ development it's a problem.

Also this: wiki.haskell.org/Zygohistomorphic_prepromorphisms

if you continue to smash your head against the keyboard it eventually pays off. Git gud

Don't worry. It's not hard to learn and you're working on computers that let you search. Terminology is especially easy to search for.

because all the weenies are contributing their garbage to scalaz or cats or shapeless or what have you.

indeed

here's what isomorphic means in the context of javascript: isomorphic.net/javascript

>wiki.haskell.org/Zygohistomorphic_prepromorphisms

???

I understand why you consider it a problem, but in my opinion that is the fault of the people reading the code.

Operators to me are no different than functions.
I use a functional language though, which is much more keen on inlining.

Reminder that Haskell is obsolete

youtube.com/watch?v=ad4BVmPni7A

I view it as a feature problem. The entire construct is made to allow you to write code concisely. At first glance it seems an obvious best choice even. So if you're presented with one thing and get another there's reason to be upset.

Operators are just functions.
It is simply a matter of built in functions.
You are used to languages in which all operators are built-in, and hence easily optimised.

I'm only 15 seconds in and he's just a horrible speaker.
What does he say?

user if you don't stop replying to me with obvious stuff I'm gonna be upset with you. I feel insulted.

The obstinate disembodied SPJ

In many languages, you can indicate to the compiler that you would like the function to be inlined.

Operators are no different.

You see, the typical computer science undergrad, who has likely taken Calculus I and II, Linear Algebra, Probability Theory, and not much else, will not know these terms. The more mathy computer science courses that every computer science major has to take -- complexity theory, discrete mathematics, and algorithms -- also do not reference these terms. You are defining these in the context of category theory, which is not a standard part of any computer science curriculum, and in many cases not offered at all.

If you want anyone to know what the hell you are talking about, you must speak as though no one you are talking to knows anything about category theory. What the hell is this in terms of sets, or some other fundamental mathematical construct that I might have actually used in a computer science curriculum?