|
I fixed the bugs and it passes all the tests but is the most correct way of doing this? mapFold doesnt even exist in Microsofts documentation
|
# ? Mar 31, 2019 06:14 |
|
|
# ? Jun 8, 2024 05:45 |
|
You should have pasted text You could calculate the moves and do the perspective check at the end to determine if you need the min or the max from the sequence. That way you can delete a whole lot of stuff from the function. I am not a great programmer, but they way I look at it is that if you have multiple lines doing the same stuff you could probably remove them and put them in one function and have the logic that makes it functionally different at the end or you pass it in as a parameter. Something like this: code:
This is just going from your existing code, there are some really smart ways of working with trees in F#, so maybe if you take a step back there is probably a really clever way of doing this. Mr Shiny Pants fucked around with this message at 11:15 on Mar 31, 2019 |
# ? Mar 31, 2019 11:06 |
|
New mandatory dependency for all new and legacy projects (but especially legacy): https://github.com/aaronpowell/DisappointinglyAttributed/
|
# ? Apr 1, 2019 16:22 |
|
Question on HashSet<T> and custom classes. I am working on a program to help automate the creation of firewall rules from a spreadsheet submitted by the user. As part of that process, I am trying to match or find the longest match for the list of ports requested from the object groups on the firewalls. To do this, i have a class that encodes a protocol entry in an object group:C# code:
To determine whether object-group a is a subset of object-group b, I had the following: C# code:
As a fix, i changed the ProtocolEntry.PortNumbers() routine to return a list of ((int)Protocol << 16) | Port. Now it works and accounts for Protocol, but it feels clumsy. I tried using HashSet<ProtocolEntry>, but the HashSet.IsSubsetOf routine did not work, always returning false. Is there something special I need to do to my classes to make them work in this instance? I have the equality methods: C# code:
Thanks in advance.
|
# ? Apr 1, 2019 18:14 |
|
The code is fine, the logic is wrong. If you compare the sets of individual port numbers, TCP 80-100 is a subset of TCP 60-120. If you compare the sets of protocol entries, you have two sets each with one item, and the two items are different. Go back to comparing the lists of individual ports, and just fix PortNumbers() so that it returns a list of tuples of port type and number. Or split it into two methods, TCPPortNumbers and UDPPortNumbers, and check both lists separately. Or return a Dictionary<PortType, int[]> and check every key in both dictionaries. NihilCredo fucked around with this message at 19:31 on Apr 1, 2019 |
# ? Apr 1, 2019 19:27 |
|
NihilCredo posted:The code is fine, the logic is wrong. The code works fine when I convert a list of ProtocolEntries to a list of ints. The generator method and the extension method will take a list of ports, say TCP/80, TCP/443, TCP/8080-8089 and convert it to a list of ints { 80, 443, 8080, ..., 8089 }. The “fix” just slaps the protocol enum value into the high order word. That now works perfectly, though it feels like (and is, really) a kludge. What didn’t work is: C# code:
So I was curious if there was something I needed to implement to make the above code work.
|
# ? Apr 2, 2019 00:18 |
|
Suppose you have a ProtocolEntry x, which represents tcp ports 60-120. And you have another ProtocolEntry y, which represents tcp ports 80-100. The issue is that your HashSet has no way of knowing that y represents a "subset" of x - all it knows is that the ProtocolEntrys are not equal, so neither HashSet is a subset of the other. If you want a slightly-less-kludgy version of your current solution, you can do as Nihil Credo suggests - use a helper method that, instead of returning a port number (that's only meaningful if you already know the protocol), returns a tuple of <protocol, port number>. Then you can stick those in a set and subset it. If you want a cleaner solution ... fundamentally, with your current api, the code doing the comparison has absolutely no way to tell that a particular protocol entry represents a range of ports with defined endpoints. The only way that it can look at a ProtocolEntry is to iterate over all the individual ports that the ProtocolEntry contains. So you're going to have to expand the api with this particular use case in mind.
|
# ? Apr 2, 2019 07:42 |
New Visual Studio what does it do?
|
|
# ? Apr 2, 2019 17:08 |
|
Jabor posted:Suppose you have a ProtocolEntry x, which represents tcp ports 60-120. And you have another ProtocolEntry y, which represents tcp ports 80-100. Yeah, returning a collection of Tuple<Protocol, int> is less of a kludge and is the same functionally as ((int)Protocol << 16) | Port. It works, too. Thanks, and thanks to Nihil Credo.
|
# ? Apr 2, 2019 17:42 |
|
Shy posted:New Visual Studio what does it do? Big one is real-time collaborative editing and debugging that, as far as we could try, actually works and is really good. Other interesting novelties are a nice vertical-space-saving title bar and random Intellisense slowdowns and freezes during regular use.
|
# ? Apr 2, 2019 19:44 |
|
NihilCredo posted:random Intellisense slowdowns and freezes during regular use. M$ ripping off more R# features
|
# ? Apr 2, 2019 20:07 |
|
Might check it out for the c# 8 preview stuff that I missed in December: - Null warnings on specified reference types - Range/index slicing in arrays - Asynch IEnumerable<T>
|
# ? Apr 2, 2019 20:18 |
|
code:
|
# ? Apr 3, 2019 19:41 |
|
Also F# got anonymous records.
|
# ? Apr 3, 2019 22:28 |
|
SixPabst posted:
What’s the error?
|
# ? Apr 3, 2019 22:32 |
|
raminasi posted:What’s the error? Not all code paths return a value. I think in 17 it was smart enough to see that there's really only one execution path and removes the switch
|
# ? Apr 3, 2019 23:11 |
|
Scaramouche posted:Might check it out for the c# 8 preview stuff that I missed in December: How can a class be async? What am I missing here? edit: Oh, wait is it enumerators?
|
# ? Apr 4, 2019 04:19 |
|
LongSack posted:How can a class be async? What am I missing here? It's effectively being capable of doing: await foreach(var result from GetThings())
|
# ? Apr 4, 2019 13:40 |
|
SixPabst posted:Not all code paths return a value. Wonder if that's related to new language features around pattern matching? Or were those all present in C# 7? Not that it makes sense, but switch finally got some useful features nailed onto it so it's like a dusty basement
|
# ? Apr 4, 2019 14:04 |
|
Munkeymon posted:Wonder if that's related to new language features around pattern matching? Or were those all present in C# 7? The C# compiler team is very good about not introducing breaking changes in general, so it's always noteworthy when they do, whether intentional or by mistake.
|
# ? Apr 4, 2019 15:14 |
|
Question about LINQ. Cisco firewalls have service object groups which consist of a collection of entries which can be a single port, a range of ports, or another object group. In my firewall modeling app, my ServiceObjectGroup class has a GetPorts() method that returns a IEnumerable<int> that is the complete collection of port numbers in the group. The class looks like this (stuff redacted): C# code:
I wanted to add a method which returned an IEnumerable<(Protocol, int)>. I couldn’t figure out how to do this with LINQ. This failed: C# code:
C# code:
LongSack fucked around with this message at 04:56 on Apr 5, 2019 |
# ? Apr 5, 2019 04:53 |
|
You didn't share the compiler error but I'm guessing the problem is the unnecessary "new" operator.
|
# ? Apr 5, 2019 05:24 |
|
SixPabst posted:
Seems related to https://github.com/dotnet/roslyn/issues/33783 which should ship in VS2019 Update 1.
|
# ? Apr 5, 2019 06:04 |
|
NiceAaron posted:You didn't share the compiler error but I'm guessing the problem is the unnecessary "new" operator. The “new” operator is used to create some new class from a LINQ expression. The idea is to take an IEnumerable<int> and produce an IEnumerable<(Protocol, int)>. select new ... is the normal way to do that with LINQ, but since the Protocol is not captured in the LINQ expression, I couldn’t figure out how to do that.
|
# ? Apr 5, 2019 06:05 |
|
LongSack posted:The “new” operator is used to create some new class from a LINQ expression. The idea is to take an IEnumerable<int> and produce an IEnumerable<(Protocol, int)>. select new ... is the normal way to do that with LINQ, but since the Protocol is not captured in the LINQ expression, I couldn’t figure out how to do that. The "new" operator doesn't have any special meaning in a LINQ expression, it does the same thing that it does outside of a LINQ expression. You don't need the "new" operator to create a tuple. Compare with this line in your foreach loop: code:
|
# ? Apr 5, 2019 06:15 |
|
Yeah, sounds like you may have mixed up anonymous types and the new tuples feature.
|
# ? Apr 5, 2019 06:57 |
|
brap posted:Seems related to https://github.com/dotnet/roslyn/issues/33783 which should ship in VS2019 Update 1. Yep, that sure seems like the same thing. Thanks!
|
# ? Apr 5, 2019 19:43 |
|
brap posted:Yeah, sounds like you may have mixed up anonymous types and the new tuples feature. I did indeed.
|
# ? Apr 6, 2019 23:58 |
|
NiceAaron posted:You didn't share the compiler error but I'm guessing the problem is the unnecessary "new" operator. And you were absolutely correct. Taking the "new" out fixed it. The error message should have been a clue if i had bothered to read it all code:
|
# ? Apr 7, 2019 18:14 |
|
Thinking about it, I can't come up with a meaningful use case for anonymous types now that C# has named tuples. The best I can think of is a sort of anti-feature - anonymous types are restricted to the local scope (can't be a return type, property, field, function parameter, etc.), so you can't be tempted to be lazy and just return a giant tuple instead of defining an actual type with a name.
|
# ? Apr 7, 2019 18:59 |
|
NihilCredo posted:Thinking about it, I can't come up with a meaningful use case for anonymous types now that C# has named tuples. The only place I see is in EF Fluent configuration to designate multi-column keys. Not sure if a tuple would work there.
|
# ? Apr 8, 2019 15:15 |
|
I am hoping to intercept awaiters and do some custom scheduling for a subsection of my code. The sense I get is I would wrap the invocations in tasks and manually extract awaiters out of them. However, the .NET documentation for tasks sounds like they'll fire off in other threads. Is that the norm? I want to stay on one thread here and I'm alternating through this stuff sequentially.
|
# ? Apr 9, 2019 16:18 |
|
NihilCredo posted:Thinking about it, I can't come up with a meaningful use case for anonymous types now that C# has named tuples. If you're going to have NewtonSoft.Json serialize whatever object graph you're making, you'll want to use anonymous objects unless they change the way they handle tuples by default.
|
# ? Apr 9, 2019 17:35 |
|
Rocko Bonaparte posted:I am hoping to intercept awaiters and do some custom scheduling for a subsection of my code. The sense I get is I would wrap the invocations in tasks and manually extract awaiters out of them. However, the .NET documentation for tasks sounds like they'll fire off in other threads. Is that the norm? I want to stay on one thread here and I'm alternating through this stuff sequentially. It's certainly possible to intercept awaiters, and I've done that lots. However "wrap the invocation in a task" doesn't mean anything. Do you mean "Task.Run(lambda that performs invocation)"? that's the wrong approach, and will indeed use a thread. I wrote a bunch of stuff about custom awaiters, and how the compiler expands out the "await" statement. If you read that you'll see that you get complete control over everything, and this will unlock what you need. https://web.archive.org/web/20171014234653/https://blogs.msdn.microsoft.com/lucian/2012/12/11/how-to-write-a-custom-awaiter/ One idiom you might decide to settle on is "await MyAsyncMethod().WithCustomAwaiter()" where you tell the compiler to use your custom machinery at the invocation site. (There was a new feature in C# in 2017 or so which also lets you declare your own tasklike type where you tell the compiler to use your custom machinery at the declaration site, "async MyCustomTasklike<int> FooAsync() {...}".) I've used these techniques ("custom scheduling") to [1] write something that "hibernates" an async method to disk so it can be reloaded upon a future invocation and resume execution from where it left off, and [2] write a probabilistic programming language within C# so that when you do "var x = await RollDice()" then the program execution continues six times for each different value of x. Another idea, depending on what you need, is to replace your current SynchronizationContext. The normal Task and Task.GetAwaiter() just defer to the current SynchronizationContext to decide how to schedule the work.
|
# ? Apr 9, 2019 18:42 |
|
I didn't expect such a good response!ljw1004 posted:It's certainly possible to intercept awaiters, and I've done that lots. However "wrap the invocation in a task" doesn't mean anything. Do you mean "Task.Run(lambda that performs invocation)"? that's the wrong approach, and will indeed use a thread. quote:I wrote a bunch of stuff about custom awaiters, and how the compiler expands out the "await" statement. If you read that you'll see that you get complete control over everything, and this will unlock what you need. https://web.archive.org/web/20171014234653/https://blogs.msdn.microsoft.com/lucian/2012/12/11/how-to-write-a-custom-awaiter/ quote:(There was a new feature in C# in 2017 or so which also lets you declare your own tasklike type where you tell the compiler to use your custom machinery at the declaration site, "async MyCustomTasklike<int> FooAsync() {...}".) https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#generalized-async-return-types. If so, I wouldn't have inferred that from that blurb. quote:I've used these techniques ("custom scheduling") to [1] write something that "hibernates" an async method to disk so it can be reloaded upon a future invocation and resume execution from where it left off, and [2] write a probabilistic programming language within C# so that when you do "var x = await RollDice()" then the program execution continues six times for each different value of x. So this kind of scares the poo poo out of me and I'm curious about it too because I did intend on being able to serialize all my state to disk in the middle of God-knows-what. What I expected to have to do was manually reconstruct all of these awaiters when I reloaded. If I can can just serialize them at will then huzzah. quote:Another idea, depending on what you need, is to replace your current SynchronizationContext. The normal Task and Task.GetAwaiter() just defer to the current SynchronizationContext to decide how to schedule the work. I guess I can explain the dumb thing I'm doing. I'm messing around with a Python interpreter with microthreading; I couldn't get IronPython to do what I needed to do, so I started doodling my own. I had a trial coroutine opcode that was working just fine when everything was all Python. However, it kind of went to hell when I had, say, Python code call C# code call Python code. That kind of jump was necessary to implement class declarations in a way similar to CPython, by the way. If the class body had something in it that would cause it to pause, everything would go to hell because I didn't have a means to have the C# code sitting above it to stop its frame and reschedule something else to run.
|
# ? Apr 10, 2019 07:01 |
|
I'm creating a new app to replace a horrible old VB.NET app that hasn't been touched for years. This is involving a certain amount of reading the code and also some reverse engineering. I don't know VB from a hole in the ground. At one point in the code, the VB app calls a method that accepts a String parameter and passes in a Double. Specifically: Sub XmlTextWriter.WriteString(Text As String) is being passed the return value of Function MyClass.MyMethod() As Double I think this is equivalent to calling .ToString() on the Double and passing that in instead, but I've not been able to find anything on the internet that explicitly says that this is the case. Does anyone know? (If it is equivalent to calling .ToString() then it's a bug in the old application, because it would be appropriate here to stringify the double using the invariant culture)
|
# ? Apr 10, 2019 17:12 |
|
I'm no VB expert either, but I suspect there might be some hidden casting going on there, probably with the "Text As String" bit. If not, there might be another cast somewhere before it goes into the XmlTextWriter. I would agree that it is probably appropriate to explicitly .ToString() it with the invariant culture.
|
# ? Apr 10, 2019 17:20 |
|
Protocol7 posted:I'm no VB expert either, but I suspect there might be some hidden casting going on there, probably with the "Text As String" bit. I wasn't clear, but that's the declaration of the method that shows up in intellisense. "Text As String" in VB is the equivalent of "string Text" in C# (unless I have misunderstood)
|
# ? Apr 10, 2019 17:33 |
|
Hammerite posted:I wasn't clear, but that's the declaration of the method that shows up in intellisense. "Text As String" in VB is the equivalent of "string Text" in C# (unless I have misunderstood) Oh, duh. That's right. Can you post a snippet of the function call? Could help clear up if there's any unknown behavior.
|
# ? Apr 10, 2019 17:41 |
|
|
# ? Jun 8, 2024 05:45 |
|
Hammerite posted:I'm creating a new app to replace a horrible old VB.NET app that hasn't been touched for years. This is involving a certain amount of reading the code and also some reverse engineering. I don't know VB from a hole in the ground. I've had decent luck with pushing VB .NET through this tool: http://converter.telerik.com/
|
# ? Apr 10, 2019 17:50 |