|
For that matter, enumerations. A distressing number of languages allow enumerations to decompose into integers and of those quite a few also allow integers to suffice as the enumeration value. The point of an enumeration is to restrict and make explicit a limited set of values which are acceptable inputs to something. If you allow coercions you completely defeat the point.
|
# ? Jul 16, 2016 13:56 |
|
|
# ? May 17, 2024 01:57 |
|
Java is probably the only mainstream language that does enums correctly. They're still a poor man's algebraic data types though.
|
# ? Jul 16, 2016 14:05 |
|
Rust calls their algebraic (sum) types enums.
|
# ? Jul 16, 2016 16:07 |
|
Internet Janitor posted:For that matter, enumerations. A distressing number of languages allow enumerations to decompose into integers and of those quite a few also allow integers to suffice as the enumeration value. The point of an enumeration is to restrict and make explicit a limited set of values which are acceptable inputs to something. If you allow coercions you completely defeat the point. Explicit conversions make it easy to serialize enums, and are totally sensible. Implicit conversion is potentially bad though.
|
# ? Jul 16, 2016 16:42 |
|
dwazegek posted:Wait doesn't php also have the thing where "12ab" == "12cd"? https://3v4l.org/rZJLS nope?
|
# ? Jul 16, 2016 16:53 |
|
Soricidus posted:Java is probably the only mainstream language that does enums correctly. They're still a poor man's algebraic data types though. C++ has "enum class" now.
|
# ? Jul 16, 2016 17:49 |
|
sarehu posted:C++ has "enum class" now. C++ has loving Lambda expressions now.
|
# ? Jul 16, 2016 18:48 |
|
Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read.
|
# ? Jul 16, 2016 19:25 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. Yes. Lambdas are great when used appropriately.
|
# ? Jul 16, 2016 19:32 |
|
Lambada in C# are the bomb
|
# ? Jul 16, 2016 19:42 |
|
Java 8 stream processing is significantly more usable with lambdas.
|
# ? Jul 16, 2016 19:49 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. If a function is confusing then I definitely prefer when it has a descriptive name, but for simple things, I think lambdas are actually much easier to read.
|
# ? Jul 16, 2016 20:12 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. higher order functions allow useful abstractions, reduce bugs, and making code easier to read by expressing intent more clearly the following: code:
|
# ? Jul 16, 2016 20:15 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. To be fair, it might not be your fault. They're easy to misuse.
|
# ? Jul 16, 2016 20:37 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. Do you think for-loop bodies that aren't just a single named function call are harder to read?
|
# ? Jul 16, 2016 21:40 |
|
i finally found a real-world case where i felt that lambdas/streams improved my code the other day. can't remember exactly what it was but i think i used flatMap instead of a nested for loop? flatMap is good. lambdas are good.
|
# ? Jul 16, 2016 22:09 |
|
once you're used to them, higher order functions are almost always better than the equivalent for loops they're easier to read they're easier to write they're generally less lines of code they communicate intent you or someone else can't make some dumb fencepost error or some other bug with hand-rolling yet another for loop in some languages with iterators they can be generally just as fast as the equivalent for loops
|
# ? Jul 16, 2016 22:30 |
|
comedyblissoption posted:in some languages with iterators they can be generally just as fast as the equivalent for loops Or faster, IIRC in Rust loops over slices/vectors with iterators are usually optimized to elide bounds checks while for loops often aren't.
|
# ? Jul 16, 2016 22:59 |
|
rust doesn't mutably alias references so it can theoretically optimize faster than C or C++ in the general case as well in c++/rust, you could still avoid bounds checks by grabbing the raw array buffer and using a for loop on that, but you'd be an insane man in the general case
|
# ? Jul 16, 2016 23:04 |
|
comedyblissoption posted:lambdas are really good for enabling the use of higher order functions pervasively Not gonna lie, that looks very nice.
|
# ? Jul 17, 2016 01:19 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. Lambdas are awesome. Your opinions are bad and you should feel bad.
|
# ? Jul 17, 2016 02:45 |
|
ToxicSlurpee posted:Am I the only person with a burning, awful hatred for lambda expressions? I feel like they're absolutely unnecessary and just serve to make code harder to read. Lambdas are great. As are C# events. You should try to use them more.
|
# ? Jul 17, 2016 02:54 |
|
wait hang on what the heck. why are we suddenly event-lovers here
|
# ? Jul 17, 2016 03:32 |
|
you have to be careful in C# b/c sometimes the Tasks library (async and put stuff on the thread pool) or the RX library (observable streams of events) are better abstractions than events depending on what youre trying to do
|
# ? Jul 17, 2016 04:20 |
|
Soricidus posted:Java is probably the only mainstream language that does enums correctly. They're still a poor man's algebraic data types though. Speaking of, I came across a bug last week that boiled down to the fact that we ended up (through a series of old and weird design decisions) comparing the hashcode of enums between different JVMs leading to false negative results.
|
# ? Jul 17, 2016 06:12 |
|
door.jar posted:Speaking of, I came across a bug last week that boiled down to the fact that we ended up (through a series of old and weird design decisions) comparing the hashcode of enums between different JVMs leading to false negative results. You can always just implement your own hashc--nope, can't even finish typing that.
|
# ? Jul 17, 2016 08:09 |
|
comedyblissoption posted:once you're used to them, higher order functions are almost always better than the equivalent for loops They're harder to debug though
|
# ? Jul 17, 2016 09:17 |
|
Soricidus posted:They're harder to debug though Please tell me how map is harder to debug than for.
|
# ? Jul 17, 2016 13:06 |
|
for loops are faster unless there's loop fusion involved so dont use em in low-latency settings i guess
|
# ? Jul 17, 2016 13:14 |
|
leper khan posted:Please tell me how map is harder to debug than for. if you got a bad compiler that hasn't inlined the state machine or w/e then its bad
|
# ? Jul 17, 2016 13:14 |
|
So I learned to program with Java. Now, at work we use Java 8 and I run in the occassional lambda expression. I've been trying to understand them for a while but coming from Java 7, they just won't click for me. I read about them, I try a thing or two, I think "ah that makes sense", and then a few days later I've forgotten why it made sense. Does anyone have any resources that explain them in a way that makes sense for someone who's used to think in purely Java 7 OOP?
|
# ? Jul 17, 2016 13:32 |
|
Carbon dioxide posted:So I learned to program with Java. They're just functions declared within other functions. Say you have a button that takes a function as argument somewhere for when it's clicked. You can use one to create the function to be used next to the instantiation of the button instead of 80 lines away in a named function. In C#, an object is allocated behind the scenes to hold the function. Not sure about Java, but it's probably similar. Those details are largely irrelevant though.
|
# ? Jul 17, 2016 13:50 |
|
leper khan posted:Please tell me how map is harder to debug than for. Hmm, good point, how could a set of little functions being executed in an undefined order and possibly in parallel be harder to debug than an explicit sequence of actions taking place in a single stack frame? I retract my comment.
|
# ? Jul 17, 2016 13:57 |
|
If you're coming from Java 7, you're probably already familiar with the idea of anonymous classes and stuff like:code:
Soricidus posted:Hmm, good point, how could a set of little functions being executed in an undefined order and possibly in parallel be harder to debug than an explicit sequence of actions taking place in a single stack frame? I retract my comment. If the function you're mapping over the collection has no side effects then it's trivially just as easy to debug. If you only care about the result, you step past the entire operation and inspect the result, while if you care about what it's doing with a particular value you step inside when the code is processing that particular element. If you map with functions that have arbitrary side effects that race with each other, you deserve all the pain you get and it's pretty weird to blame it on the map construct, since it would be horrendous and painful even if it was in a for loop.
|
# ? Jul 17, 2016 14:00 |
|
Soricidus posted:Hmm, good point, how could a set of little functions being executed in an undefined order and possibly in parallel be harder to debug than an explicit sequence of actions taking place in a single stack frame? I retract my comment. map and friends are purely deterministic, unless you're using some library with weird loving semantics the issue is that in many implementations you have bookkeeping around the lazyness/generator structure that bad debuggers won't elide
|
# ? Jul 17, 2016 14:08 |
|
Carbon dioxide posted:So I learned to program with Java. This is for C#, but this video made Lambdas click for me: https://www.youtube.com/watch?v=KRjeu9Thp3s
|
# ? Jul 17, 2016 14:56 |
|
Carbon dioxide posted:So I learned to program with Java. Lambda expressions are basically single method interfaces whose implementations are generated on the fly. Suppose you have a single method interface such as code:
code:
code:
code:
code:
code:
And then you can start passing function references around as arguments, which is even cooler.
|
# ? Jul 17, 2016 15:08 |
|
pigdog posted:And then you can start passing function references around as arguments, which is even cooler. Our team is still stuck on Java 6. Do you have any idea how long I've been waiting for Java to get proper support for function handlers? (I spent five years at a Python shop before that. First project at the new job, I tried to pass a function from one class to another, and ended up going down the rabbit hole that is Java's awful, awful support for reflection)
|
# ? Jul 17, 2016 15:28 |
|
Thanks for the link to the vid and the explanations, folks!
|
# ? Jul 17, 2016 16:23 |
|
|
# ? May 17, 2024 01:57 |
|
Jabor posted:If you map with functions that have arbitrary side effects that race with each other, you deserve all the pain you get and it's pretty weird to blame it on the map construct, since it would be horrendous and painful even if it was in a for loop. A map can be a good replacement for a foreach-loop, which is not always the same as a for-loop.
|
# ? Jul 17, 2016 17:13 |