Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
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.

Adbot
ADBOT LOVES YOU

Soricidus
Oct 21, 2010
freedom-hating statist shill
Java is probably the only mainstream language that does enums correctly. They're still a poor man's algebraic data types though.

Vanadium
Jan 8, 2005

Rust calls their algebraic (sum) types enums. :colbert:

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

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.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

dwazegek posted:

Wait doesn't php also have the thing where "12ab" == "12cd"?

https://3v4l.org/rZJLS

nope?

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

Absurd Alhazred
Mar 27, 2010

by Athanatos

sarehu posted:

C++ has "enum class" now.

C++ has loving Lambda expressions now.

ToxicSlurpee
Nov 5, 2003

-=SEND HELP=-


Pillbug
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.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

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.

kloa
Feb 14, 2007


Lambada in C# are the bomb :byob1:

MrMoo
Sep 14, 2000

Java 8 stream processing is significantly more usable with lambdas.

sunaurus
Feb 13, 2012

Oh great, another bookah.

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.

comedyblissoption
Mar 15, 2006

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 really good for enabling the use of higher order functions pervasively

higher order functions allow useful abstractions, reduce bugs, and making code easier to read by expressing intent more clearly

the following:
code:
//C#
var numbers = new List<int>{ 101, 202, 303, 404, 505, 606, 707, 808, 909 }
var processedNumbers = numbers
    .Where(IsOdd)
    .Where(x => x > 300)
    .Map(x => x * 1000)
    .Take(2)
    .ToList();
tends to be much more easily readable than the equivalent for loop once youre used to the pattern

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

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.

vOv
Feb 8, 2014

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?

Soricidus
Oct 21, 2010
freedom-hating statist shill
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.

comedyblissoption
Mar 15, 2006

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

Linear Zoetrope
Nov 28, 2011

A hero must cook

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.

comedyblissoption
Mar 15, 2006

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

Absurd Alhazred
Mar 27, 2010

by Athanatos

comedyblissoption posted:

lambdas are really good for enabling the use of higher order functions pervasively

higher order functions allow useful abstractions, reduce bugs, and making code easier to read by expressing intent more clearly

the following:
code:
//C#
var numbers = new List<int>{ 101, 202, 303, 404, 505, 606, 707, 808, 909 }
var processedNumbers = numbers
    .Where(IsOdd)
    .Where(x => x > 300)
    .Map(x => x * 1000)
    .Take(2)
    .ToList();
tends to be much more easily readable than the equivalent for loop once youre used to the pattern

Not gonna lie, that looks very nice.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

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.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

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. :feelsgood:

Gul Banana
Nov 28, 2003

wait hang on what the heck. why are we suddenly event-lovers here

comedyblissoption
Mar 15, 2006

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

door.jar
Mar 17, 2010

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.

Storysmith
Dec 31, 2006

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.

Soricidus
Oct 21, 2010
freedom-hating statist shill

comedyblissoption posted:

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

They're harder to debug though

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Soricidus posted:

They're harder to debug though

Please tell me how map is harder to debug than for. :allears:

Malcolm XML
Aug 8, 2009

I always knew it would end like this.
for loops are faster unless there's loop fusion involved so dont use em in low-latency settings i guess

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

leper khan posted:

Please tell me how map is harder to debug than for. :allears:

if you got a bad compiler that hasn't inlined the state machine or w/e then its bad

Carbon dioxide
Oct 9, 2012

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?

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Carbon dioxide posted:

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?

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.

Soricidus
Oct 21, 2010
freedom-hating statist shill

leper khan posted:

Please tell me how map is harder to debug than for. :allears:

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.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If you're coming from Java 7, you're probably already familiar with the idea of anonymous classes and stuff like:

code:
Runnable callback = new Runnable() {
  void run() {
    // do something
  }
};
handler.post(callback);
Lambdas are essentially the same thing, with nicer syntax.

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.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

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

kloa
Feb 14, 2007


Carbon dioxide posted:

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?

This is for C#, but this video made Lambdas click for me:

https://www.youtube.com/watch?v=KRjeu9Thp3s

pigdog
Apr 23, 2004

by Smythe

Carbon dioxide posted:

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?

Lambda expressions are basically single method interfaces whose implementations are generated on the fly.

Suppose you have a single method interface such as

code:
interface SomeListener {
	String doStuff(Integer input);
}
and you're using a method that makes use of it, such as

code:
public void addListener(SomeListener listener) { ... };
In Java 7, if you wanted for your listener to convert the input Integer to hexadecimal, you'd need to write a class for it, or use an anonymous implementation something like this:

code:
addListener(new SomeListener() {
	@Override
	public String doStuff(Integer input) {
		return Integer.toHexString(input);
        }
});
In Java 8, that's more easily written as this
code:
addListener((input) -> {
		return Integer.toHexString(input);
        }
);
or more commonly as
code:
addListener(input -> Integer.toHexString(input));
which can in turn be shortened to
code:
addListener(Integer::toHexString);
It's p. sweet.

And then you can start passing function references around as arguments, which is even cooler.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

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? :cry:

(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)

Carbon dioxide
Oct 9, 2012

Thanks for the link to the vid and the explanations, folks!

Adbot
ADBOT LOVES YOU

fritz
Jul 26, 2003

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply