#could you not just do `is_hungry(
1 messages · Page 1 of 1 (latest)
You can, store it in a func which was your question.
On mobile and I dont remember the exact syntax but you can use the lambda expression
I guess my ask isn't coming across clearly enough
@ornate siren here's what I was looking to make
using System;
using System.Collections.Generic;
using System.Linq;
public static class FuncUtils<X, Y> {
public static Func<X, Y, bool> OR(IEnumerable<Func<X, Y, bool>> funcs) {
return (x, y) => funcs.Any(f => f(x,y));
}
public static Func<X, Y, bool> AND(IEnumerable<Func<X, Y, bool>> funcs) {
return (x, y) => funcs.All(f => f(x,y));
}
}
I haven't tested it out yet, but this is hte idea
the only thing that this doesn't yet achieve for me is, I want the templatized type to be generic enough that it represents the set of all arguments, regardless of what the function parameter signature ends up looking like
you could make it more generic by doing
public static Func<T, bool> AND<T>(IEnumerable<Func<T, bool>> funcs)
{
return val => funcs.All(f => f(val));
}
Hmm, how does val represent multiple values? I don't understand that
You can use value tuple.
sorry yea i was thinking you would have to use a tuple there. i dont know if theres another option here
Actually this kinda goes against the design principles of C# as a strongly typed language. What you could do is box the value as a C# object, but you'll need to know what type it was when you need to get the value out of it.
A way compatible with C# design principles would be to have some kind of class container for your value with polymorphism.
Is strong typing not already violated with type templating?
I think the strategy of passing a tuple is fine, I'll just go with that
If you're talking about generics, no. They are actually strongly typed at compile type. The compiler creates specific types for each generic specialization.
Just to make it clear: generics are not the same as C++ templates. There are huge differences between the two.
actually it doesnt have to be a tuple, it could be a class/struct. either way i do find it kinda questionable on how often you'd be using this method
listen, getting this down will give me some super expressive code, so it's worth it
Honestly, generics are usually a rabbit hole you don't want to go down. I usually try to avoid. Because halfway throw the implementation you discover a limitation that is not possible to overcome.