|
Nolgthorn posted:https://www.codewars.com/ I'm liking this a lot -- thanks for the suggestion. The solution review and comparison is a really great way to learn.
|
# ? Jan 13, 2019 14:31 |
|
|
# ? Jun 5, 2024 06:19 |
|
GeorgieMordor posted:I'm liking this a lot -- thanks for the suggestion. The solution review and comparison is a really great way to learn. I've learned a lot from the comparison section. It was really helpful when learning more about ES6+ and Java 8
|
# ? Jan 14, 2019 03:45 |
|
Hah, they want you to pay them to see your upvotes now.
|
# ? Jan 18, 2019 15:06 |
|
Friday, fighting off a cold and just staring at this function for like an hour wondering why the gently caress it isn't working when my array has a single empty string in it:JavaScript code:
Edit: Actually I am in fact confused now? http://jsfiddle.net/2na5wvbh/ Works exactly how I expect it to. But when I debug my application: Element 3 doesn't have its empty value replaced? Debugging it, it does step into controlArray[i] = 1; but...? Double Edit: OK it does actual work, but the Watch window is lovely and doesn't reflect this. If I mouse over controlArray in the actual code window I can see these values get updated. Sab669 fucked around with this message at 21:36 on Jan 18, 2019 |
# ? Jan 18, 2019 20:57 |
|
roomforthetuna posted:Aha, I found a sort-of answer to my question, which is that evidently webpack is doing something a bit different from the way package.json people think you'll be building stuff. As such, having I didn't ever quite resolve why some packages would would, but I found the reason for the failure is that "baseUrl": "C:/herp/derp" in tsconfig.json gets transformed by the loader module into "C:/herp/derp/C:/herp/derp" by going through code that says if (!path.Absolute(yourPath)) then append it to current working directory. If you instead (or additionally) put it in webpack.config like code:
|
# ? Jan 20, 2019 17:30 |
|
Hey dudes. What's the best way to refresh data from a server every 30 seconds or so? So points to using setInterval, but the answers are mainly from 2010, so not sure if its' still the best way. Eg call a fetch functino from setInterval. Don't think I need websockets for this.
|
# ? Jan 21, 2019 01:46 |
|
Dominoes posted:Hey dudes. What's the best way to refresh data from a server every 30 seconds or so? So points to using setInterval, but the answers are mainly from 2010, so not sure if its' still the best way. Eg call a fetch functino from setInterval. Don't think I need websockets for this.
|
# ? Jan 21, 2019 04:33 |
|
That ought to cover the purely technical answer for the face-value interpretation, but do you mind describing the use case a little more?
|
# ? Jan 21, 2019 04:41 |
|
I needed to run code on a schedule, for example at the top of every hour and I opted to use setTimeout instead. Calculating the time until the top of the hour and calculating again after I finished doing whatever I was doing. But if I didn't care, I can't imagine what's wrong with setInterval as long as you account for possible timeouts.
|
# ? Jan 21, 2019 14:53 |
|
Ah, ok. I was thinking that if the true update needs were indeterminate, it might do better with a websocket instead of xhr polling, but it sounds like that's not the case.
|
# ? Jan 21, 2019 15:00 |
|
Don't get me wrong I love websockets. Websockets are the best thing ever and as soon as I have an excuse I'll use them. Like for example if the server ever needs to tell me anything. I'd take websockets over polling for sure. The thing that's hard about them is maintaining the connection, detecting a lost connection, and managing requests that involve a response. So a wrapper something like https://www.npmjs.com/package/passage-rpc
|
# ? Jan 21, 2019 15:26 |
|
Disclaimer: I wrote that ages ago. Nobody is downloading it. It works perfectly.
|
# ? Jan 21, 2019 15:38 |
|
Nolgthorn posted:Disclaimer: I wrote that ages ago. Nobody is downloading it. It works perfectly. Most (all?) websocket libraries that I've worked with in various languages had the concept of a "ping interval". That is, the library itself would keep the socket open by pinging the server at a set interval. Never had to worry about sockets being closed for no reason . Of course, the socket can be closed for a variety of reasons, but the libraries do let you know via their listeners.
|
# ? Jan 21, 2019 16:04 |
|
The main focus of the library is RPC rather than maintaining connection, it's for client side apps especially single page apps that forego http entirely, I guess that isn't really popular yet. Maybe it's time I updated it and better the documentation.
Nolgthorn fucked around with this message at 17:00 on Jan 21, 2019 |
# ? Jan 21, 2019 16:58 |
|
I have a typescript/express question, hope this is the right thread for it. I recently wrote a pretty simple site in typescript to learn about express and server-side javascript development in general. My problem is that I created a middleware function that attaches some basic information about the currently logged in user to res.locals. Because res.locals has type 'any' all of its properties have type 'any' as well. This means that later when a request is being handled, when I reference the login status object, its type is 'any'. My question is, is there some way I can add an object to res.locals (or the request or response objects in general) and keep the typescript compiler aware of its type? I've defined an interface named LoginStatus that describes the properties of this object.
|
# ? Jan 21, 2019 18:12 |
|
Rahu posted:My question is, is there some way I can add an object to res.locals (or the request or response objects in general) and keep the typescript compiler aware of its type? I've defined an interface named LoginStatus that describes the properties of this object. Just tried this in the typescript playground and it seems to work: code:
|
# ? Jan 21, 2019 18:41 |
|
That seems to work, thanks
|
# ? Jan 21, 2019 18:47 |
|
roomforthetuna posted:setInterval is still alright, though there's a small risk of weirdness if a request takes over 30 seconds for some reason. Do you actually want a request every 30 seconds, or do you want, say, 30 seconds after the previous request completing? I generally prefer the latter, so I do it with a setTimeout to send the request again at the end of the response-handler (and error-handler) rather than setInterval, which protects against the small risk of ending up with two or more requests in flight at once. rt4 posted:That ought to cover the purely technical answer for the face-value interpretation, but do you mind describing the use case a little more?
|
# ? Jan 21, 2019 18:50 |
|
Another one: Is there an elegant way to dedupe an array of tuples? This approach doesn't seem to work, nor does Lodash's uniq. Array.includes doesn't seem to help either.JavaScript code:
JavaScript code:
Dominoes fucked around with this message at 00:52 on Jan 22, 2019 |
# ? Jan 22, 2019 00:45 |
|
A Set is going to compare the object identity, not the content of the arrays. If you do:code:
|
# ? Jan 22, 2019 00:57 |
|
Dominoes posted:Another one: Is there an elegant way to dedupe an array of tuples? code:
And if you want to make it generic you're going to be doing something even worse like transforming your tuples into JSON strings, then you can put *those* into a set to remove duplicates and then de-JSON the elements of the set afterwards.
|
# ? Jan 22, 2019 01:24 |
|
code:
code:
|
# ? Jan 22, 2019 11:10 |
I think you're looking for uniqWith? The _.isEqual example should work. e: if I was writing it myself I'd probably base it on an object with hashed indices, like so: code:
Osmosisch fucked around with this message at 12:19 on Jan 22, 2019 |
|
# ? Jan 22, 2019 12:07 |
|
I don't use reduce very often but if you prefer.code:
code:
Nolgthorn fucked around with this message at 12:20 on Jan 22, 2019 |
# ? Jan 22, 2019 12:09 |
|
code:
Not sure what the performance implications of joining every tuple in the array is but doing key lookup should be faster than array indexing. Blinkz0rz fucked around with this message at 14:34 on Jan 22, 2019 |
# ? Jan 22, 2019 14:29 |
|
That would also work without the if statement.
|
# ? Jan 22, 2019 14:44 |
|
Nolgthorn posted:That would also work without the if statement.
|
# ? Jan 22, 2019 14:49 |
Yeah, that's the same solution as I posted a bit upthread, though I lean on lodash (I prefer to let one library handle all the browser compatibility stuff vs. shim/transpile). e: I did some jsbenching, looks like for 10k elements the hash version is about 100x faster, with the native reduce and Object.values() faster than lodash's version. https://jsbench.me/7qjr7x9o5c/1 The if statement does not appear to have significant impact. Osmosisch fucked around with this message at 16:37 on Jan 22, 2019 |
|
# ? Jan 22, 2019 15:32 |
|
Love the variety. Esp like the reduce approach.
|
# ? Jan 22, 2019 21:49 |
|
Dominoes posted:Another one: Is there an elegant way to dedupe an array of tuples? This approach doesn't seem to work, nor does Lodash's uniq. Array.includes doesn't seem to help either. Don't forget the true galaxy brain approach: JavaScript code:
Roadie fucked around with this message at 08:00 on Jan 23, 2019 |
# ? Jan 23, 2019 07:58 |
|
Is jsperf broken or something, yesterday it told me using lodash was fastest too.
|
# ? Jan 23, 2019 11:18 |
Nolgthorn posted:Is jsperf broken or something, yesterday it told me using lodash was fastest too. There's a bunch of variance there since all the benchmarks are running in your browser. So for example if you (or some background process) are doing poo poo in another tab during one of the tests it will gently caress with the results. Or a garbage collection is triggered, or any of tons of other possibilities. e: added the galaxy brain solution, it's on my machine about 25% below hashbased which is not surprising given that JSON.parse isn't exactly a free operation. Osmosisch fucked around with this message at 11:58 on Jan 23, 2019 |
|
# ? Jan 23, 2019 11:53 |
|
Roadie posted:Don't forget the true galaxy brain approach: That's the stuff
|
# ? Jan 25, 2019 09:28 |
|
Dumb Lowtax posted:That's the stuff Fun fact: It's also dependent on the specific interaction of map and JSON.stringify/parse, so if you swap those for other functions weird poo poo might happen.
|
# ? Jan 26, 2019 04:23 |
|
Roadie posted:Fun fact: It's also dependent on the specific interaction of map and JSON.stringify/parse, so if you swap those for other functions weird poo poo might happen. Like yeah this behavior is specific to a reversible mapping to/from a unique identifier, but if you switched the JSON functions for any other reversible to/from unique value serializer/deserializer the outcome would be the same. I mean yeah, if you switched them out for other functions like ... parseInt and console.log then weird poo poo would happen, but it's always the case that if you swap out a function for a function that does something different then something different will happen. How is this case special such that you're remarking on it?
|
# ? Jan 26, 2019 04:34 |
|
roomforthetuna posted:What? In what way? It's the normal expected way map and the JSON functions work, they're not doing anything specific to the pairing are they? Array.map supplies three arguments to its callback, not one. If you use a whateverParsing function that isn't as quick to dump non-matching args as the JSON.x ones are, it'll use those to do...... something.
|
# ? Jan 26, 2019 04:37 |
|
Why explicit straight forward code is better 101.
|
# ? Jan 26, 2019 04:45 |
|
Roadie posted:Array.map supplies three arguments to its callback, not one. If you use a whateverParsing function that isn't as quick to dump non-matching args as the JSON.x ones are, it'll use those to do...... something. But you're right that it's dependent on the specific implementation, in that it's technically undefined behavior to use JSON.stringify this way, since its second parameter is documented as a function or an array of filters or null or unset, and array.map will feed it an index which is a number which is none of those things, and its third parameter is documented as as a string or number or null, and array.map will feed it an array in that position.
|
# ? Jan 26, 2019 07:16 |
|
A while ago, I designed a few exercises to help a friend learn Javascript. Below is one such exercise:code:
code:
code:
|
# ? Jan 30, 2019 01:46 |
|
|
# ? Jun 5, 2024 06:19 |
|
That last one looks like a TypeScript shortcut not EcmaScript, I’ve made that mistake.
|
# ? Jan 30, 2019 02:04 |