Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I opened a scala code and it was 3 nested for loops. Yeah sorry, im not gonna read past that.
-
@ganjaman Optimized code is harder to understand. While I guess these aren't to copy and paste into prod, they're definitely great resources for learning how these algorithms work.
-
@AlgoRythm no, i mean rule #1 of functional programming is to use recursion over loops. This repo is not "x in y language", its "x in C, slightly modified to run in y language"
-
@ganjaman I see, I was unaware. Functional programming is not my cup of tea. I just love my objects.
How do functional programs avoid stack overflow when using recursive functions over loops? -
@AlgoRythm theres a lot of methods and its one of the harder parts, like choosing head/tail recursion, or using inner maps or seqs, but most overflow issues can be solved with lazy evaluation (basically the next 1 input is considered at any given point, makes infinite recursion semi-possible)
-
@ganjaman That sounds like yet another reason I'll probably only learn functional programming when I'm old and bored and run out of crosswords.
-
Lythenas2426y@AlgoRythm @AlgoRythm lazy evaluation is also one of the reasons why you can write e.g. Fibinacci function in one line and it is so much fucking faster than all conventional methods
-
Lythenas2426y@Fast-Nop if you implement them both the same way I expect they have almost the same performance. But if you implement fibs in one line in haskell it probably takes more time to print the numbers then calculate them. I can calculate the 10000 fib pretty much instantly. To get the same performance you would have to implement it either iteratively or cache all fibs in other languages. Also in other languages you need something like BigInt which is usually annoying to work with.
-
@irene 1. CS students in their homework, and 2. people who want to check whether tail call optimisation works as expected.
-
Lythenas2426y@irene Fibonacci is just a very simple example everyone should understand. And it demonstrates nicely why lazyness is a nice feature
-
Lythenas2426y@irene we were talking about lazy evaluation a few comments above. And it is one of the main reasons e.g. Haskell is so fast and efficient. If you calculate the 10000th fib you take up literally no ram because you throw away the others. But you can still use the same function to calculate a range of fibs if you want.
-
Lythenas2426y@irene if you write a function to return the first n fibs and you just want the nth fib and not the others the calculation will take up the space of n ints. With lazynesa it doesn't because the compiler knows you only want the nth.
If the function is lazy it is more versatile -
Lythenas2426y@irene my point is you can use the same function to calculate the nth fib and to calculate the first n fibs. And if it's lazy it only uses as much space as it needs to store what you actually use later
Computer science students and data scientists rejoice, "All algorithms" implemented in many common languages:
https://github.com/TheAlgorithms
rant