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
Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Dominoes posted:

New JS versions already compile down to existing JS; just have the transpiler interpret array1 == array2 as whatever verbose syntax you currently need to use. Code bases that rely on the current comparison behavior can skip the new language/transpiler.

This is much better handled by using a library function call. You don't want to fundamentally change the existing language semantics. What if I WANT to check for reference equality?

Adbot
ADBOT LOVES YOU

necrotic
Aug 2, 2005
I owe my brother big time for this!
And knowing it's an array at compile time is practically impossible without something like typescript.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
All the talk about comparing arrays completely glosses over that this program does not in any sense need to ever compare two arrays together, since you're basically doing a modified Gaussian sum

code:
function pairs(a, allowReverse, allowDuplicates) {
    a = a.sort();

    var acc = [];
    var minJ = 0;

    for (var i = 0; i <= a.length - 1; i++) {
        if (!allowDuplicates && i > 0 && a[i] == a[i - 1]) {
            continue;
        }

        for (var j = a.length - 1; j >= minJ; j--) {
            if (!allowDuplicates && j < a.length - 1 && a[j] == a[j + 1]) {
                continue;
            }

            if (i != j && a[i] + a[j] == 10) {
                acc.push([a[i], a[j]]);
            }
        }

        if (!allowReverse) {
            minJ = i;
        }
    }

    return acc;
}

Vulture Culture fucked around with this message at 07:05 on Jun 19, 2017

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Maluco Marinero posted:

basic facts about JavaScript equality and how to rigorously test it properly.

Please elaborate what you mean by this.

Dreadrush
Dec 29, 2008
I'm a lodash programmer who dabbles in vanilla javascript occasionally. How I would answer in an interview test:

code:
const arr = [1, 1, 2, 4, 4, 5, 5, 5, 6, 7, 9];

const pairs = _.flatMap(arr, (value, index) => (
  _(arr)
    .reject((val, i) => i === index)
    .map(otherValue => [value, otherValue])
    .filter(values => _.sum(values) === 10)
    .value()
));

const uniquePairs = _.uniqWith(pairs, _.isEqual);

const sortedUniquePairs = _(pairs)
  .map(pair => _.orderBy(pair))
  .uniqWith(_.isEqual)
  .value();

console.log(pairs, uniquePairs, sortedUniquePairs);

Honest Thief
Jan 11, 2009
What's recommended to track js memory heaps and such? I tend to rely on chrome tools, but is there some other way?

Kekekela
Oct 28, 2004

Dominoes posted:

JS is an evolving language; no excuse for comparing the arrays directly not to work.

Deep comparing of arrays by default isn't how C# et al work either.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Maluco Marinero posted:

How do you propose to implement that without breaking all the code that depends on referential checks for high performance?

Quadruple equals for deep equality with coercion and quintuple equals for deep non-coercing equality :can:

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Munkeymon posted:

Quadruple equals for deep equality with coercion and quintuple equals for deep non-coercing equality :can:

YOU'VE DONE IT NOW!

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Skandranon posted:

YOU'VE DONE IT NOW!

We're going to need a new Boolean type that carries information on which type of equality was used to derive the value.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Lumpy posted:

We're going to need a new Boolean type that carries information on which type of equality was used to derive the value.
I propose we adopt the four Boolean values of LP: true, false, both, and neither.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Ghost of Reagan Past posted:

I propose we adopt the four Boolean values of LP: true, false, both, and neither.

Don't forget FILE_NOT_FOUND

Pollyanna
Mar 5, 2005

Milk's on them.


Just had a recruiter start a phone screening by asking me what this does:

code:
'123'[['repeat','split'][+('two'<'three')]](3)
I asked him if this was the kind of code I could expect to see in their codebase, he told me that it came straight from there.

I think I'm gonna pass on this one.

(Other questions I hosed up: what's the difference between an Int and a Float, and if Ints are just part of the set of Floats, why not use a Float for everything? Also, quick, what's the regex for extracting phone numbers from 10000 HTML files off the top of your head?)

Pollyanna fucked around with this message at 21:00 on Jun 20, 2017

The Fool
Oct 16, 2003


Pollyanna posted:

Also, quick, what's the regex for extracting phone numbers from 10000 HTML files off the top of your head?)

code:
\d\d\d-\d\d\d-\d\d\d\d
Not ideal, but that's what I could do without using something like regex101

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself

Pollyanna posted:

Just had a recruiter start a phone screening by asking me what this does:

code:
'123'[['repeat','split'][+('two'<'three')]](3)
I asked him if this was the kind of code I could expect to see in their codebase, he told me that it came straight from there.

I think I'm gonna pass on this one.

(Other questions I hosed up: what's the difference between an Int and a Float, and if Ints are just part of the set of Floats, why not use a Float for everything? Also, quick, what's the regex for extracting phone numbers from 10000 HTML files off the top of your head?)

God I hate when recruiters ask questions on the phone.

Like before I knew was lazy loading was, I was asked what it was over the phone and was like "uhh idk, but I'm sure I could google it and tell you"

reversefungi
Nov 27, 2003

Master of the high hat!

Pollyanna posted:

Just had a recruiter start a phone screening by asking me what this does:

code:
'123'[['repeat','split'][+('two'<'three')]](3)
I asked him if this was the kind of code I could expect to see in their codebase, he told me that it came straight from there.

I think I'm gonna pass on this one.

(Other questions I hosed up: what's the difference between an Int and a Float, and if Ints are just part of the set of Floats, why not use a Float for everything? Also, quick, what's the regex for extracting phone numbers from 10000 HTML files off the top of your head?)

In what ungodly universe would that actually appear in any reasonable kind of code? I spent some time with it in JSBin, apparently ('two'<'three') evaluates to false, then the '+' converts it to a 0, which accesses 'repeat' from the preceding array, so you end up calling '123'.repeat(3). No way in hell could I have figured that out without some tinkering first though.


edit: How about
code:
\d{3}-\d{3}-\d{4}
for the regex?

necrotic
Aug 2, 2005
I owe my brother big time for this!
That will miss a bunch of variations. Asking that over the phone (or ever really) is pretty bad though.

The Fool
Oct 16, 2003


Because I had some free time this afternoon:
code:
(?>(?>\+?)(?>1?)(?>-?)(?>\(?)\d{3}(?>\)?)(?>-?))?(?>\d{3}?)(?>-?)(?>\d{4})
https://regex101.com/r/9HnE2p/1

Took about 20 minutes playing with regex101.

I'm bad at regexes

lunar detritus
May 6, 2009


necrotic posted:

That will miss a bunch of variations. Asking that over the phone (or ever really) is pretty bad though.

How do you even reply to that kind of question over the phone?

"SURE, THE ANSWER IS SLASH D OPEN CURLY BRACE THREE CLOSE CURLY BRACE DASH..."

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

The Dark Wind posted:

In what ungodly universe would that actually appear in any reasonable kind of code? I spent some time with it in JSBin, apparently ('two'<'three') evaluates to false, then the '+' converts it to a 0, which accesses 'repeat' from the preceding array, so you end up calling '123'.repeat(3). No way in hell could I have figured that out without some tinkering first though.

That's also assuming it's JavaScript, which was not obvious as ES5 does not have a string.repeat(), ES6 does. So this is some stupid puzzle adapted from 10+ years ago and the guy was lying about it coming from production code, or he is sitting on one of the biggest code-bombs ever, and it's being actively developed with modern technology. I could at least excuse it (not really) if it came from some legacy system that they are afraid of and trying to deconstruct, but this... How the hell do you find these people Poly?

necrotic
Aug 2, 2005
I owe my brother big time for this!
I like to imagine they implemented their own repeat 10 years ago and put it on the String prototype.

Pollyanna
Mar 5, 2005

Milk's on them.


Skandranon posted:

That's also assuming it's JavaScript, which was not obvious as ES5 does not have a string.repeat(), ES6 does. So this is some stupid puzzle adapted from 10+ years ago and the guy was lying about it coming from production code, or he is sitting on one of the biggest code-bombs ever, and it's being actively developed with modern technology. I could at least excuse it (not really) if it came from some legacy system that they are afraid of and trying to deconstruct, but this... How the hell do you find these people Poly?

His exact words were "this is something you can expect to encounter in our code base", which I took to mean "this is what you're gonna be working with". Maybe I misunderstood.

Also, I have weird luck and I apply to lots of places on AngelList. This one was a throwaway where my note to them was "Do you do cats?".

geeves
Sep 16, 2004

Pollyanna posted:

Just had a recruiter start a phone screening by asking me what this does:

code:
'123'[['repeat','split'][+('two'<'three')]](3)
I asked him if this was the kind of code I could expect to see in their codebase, he told me that it came straight from there.

I think I'm gonna pass on this one.

(Other questions I hosed up: what's the difference between an Int and a Float, and if Ints are just part of the set of Floats, why not use a Float for everything? Also, quick, what's the regex for extracting phone numbers from 10000 HTML files off the top of your head?)

The correct answer is, "Who ever wrote that should be defenestrated immediately."

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Pollyanna posted:

His exact words were "this is something you can expect to encounter in our code base", which I took to mean "this is what you're gonna be working with". Maybe I misunderstood.

Also, I have weird luck and I apply to lots of places on AngelList. This one was a throwaway where my note to them was "Do you do cats?".

lol.

geeves posted:

The correct answer is, "Who ever wrote that should be defenestrated immediately."

Yeah, it's this or, "I could reduce this down to an answer but we need to address the fact that production code looks like this before I can continue caring about this interview."

MrMoo
Sep 14, 2000

geeves posted:

The correct answer is, "Who ever wrote that should be defenestrated immediately."

Or just: "so you need a new team lead then?"

Video Nasty
Jun 17, 2003

The Fool posted:

Because I had some free time this afternoon:
code:
(?>(?>\+?)(?>1?)(?>-?)(?>\(?)\d{3}(?>\)?)(?>-?))?(?>\d{3}?)(?>-?)(?>\d{4})
https://regex101.com/r/9HnE2p/1

Took about 20 minutes playing with regex101.

I'm bad at regexes

This was actually a lot of fun, haven't done this in a bit but could have probably aced the phone interview.

code:
(\d*|(?:\+|\(|\-|\+|\)\d+?(?:\)))\d+?(?:\-)\d+?)
https://regex101.com/r/9HnE2p/2

e: I'm incompetent and only matched all numbers. I'll just see myself out.

Video Nasty fucked around with this message at 03:13 on Jun 21, 2017

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.

Video Nasty posted:

e: I'm incompetent and only matched all numbers. I'll just see myself out.

It's okay, we're just trying to get a sense of how you think

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Moment's API is making me a little :psyduck: and I want to run something by you guys because I'm getting the right answer but don't fully understand/haven't realized yet why. I have a local date+time and I'm getting a UTC offset from a weather API. I'm trying to reliably get moment to spit out an epoch time that corresponds to that location's time instead of device location. This gives me the right answer:

JavaScript code:
// "2017-06-21T00:12:00" and -7 for example would be 00:12 PDT
moment.utc(localArrivalTime).subtract(serviceResult.utcOffset, 'hours').unix()
That gives me the right answer, but I'm not sure why add isn't the way to go there (instead of subtract).

Yes, I know there's a time zone library, but I'm hoping to avoid pulling in a whole 'nother library for this one line since this is mobile-centric thing.

E: at least I think this is right - could just be plugging the wrong times into epochconverter :v:

Munkeymon fucked around with this message at 16:19 on Jun 21, 2017

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Pollyanna posted:

Just had a recruiter start a phone screening by asking me what this does:

code:
'123'[['repeat','split'][+('two'<'three')]](3)
I asked him if this was the kind of code I could expect to see in their codebase, he told me that it came straight from there.

I think I'm gonna pass on this one.

(Other questions I hosed up: what's the difference between an Int and a Float, and if Ints are just part of the set of Floats, why not use a Float for everything? Also, quick, what's the regex for extracting phone numbers from 10000 HTML files off the top of your head?)

It's probably a bad sign fro my mental health that I figured out what it did without running it, but I did have to pick it apart in the debugger to confirm what I was thinking. The only reason something like that should ever come up is if you've got a requirement to find people who can debug minified JS for some reason.

Pollyanna
Mar 5, 2005

Milk's on them.


I mean, I understood what it was trying to do once I stared at it for a bit, but my brain kind of shut off automatically as a safety procedure when I first saw it.

LP0 ON FIRE
Jan 25, 2006

beep boop
I need a callback when a web font is loaded, but I can't find any cross browser solution.

Font loading is a problem when using a web font on canvas. I'm using jquery fontselect to select a Google web font from a drop down. The font selection drop down doesn't actually guarantee the font will be ready in time for the text to rendered on canvas. I can prove this by selecting a font from the drop down before the correct font face is visible in the preview. Text using web fonts before they are loaded appear to show up as Times New Roman. This is unlike using a web font on other elements that redraw themselves as the correct font by the time they are loaded.

The conundrum of this problem is that there appears to be no cross browser solution to loading web fonts. If I could properly get a callback on all browsers, this wouldn't be a problem.

necrotic
Aug 2, 2005
I owe my brother big time for this!
You should be able to use the load event unless you have to target older browsers. With jQuery you could listen for load on all existing and future link tags with:

code:
$(document).on('load', 'link', callback);
If you need to support older browsers then you're probably boned, as link is the only way to load fonts outside of stylesheets. If you could switch to loading through stylesheets then you could do some CSS trickery to detect loaded fonts but its kludgy as gently caress (and would require forking and adding a ton of code to the font selector plugin).

LP0 ON FIRE
Jan 25, 2006

beep boop
It looks like Google's Webfont Loader looks to be way more promising than I thought, mostly because I can't see of it being documented anywhere that you can place the event handler in Webfont.load except for one Stack Overflow post I came across THANK GOD.

I can call WebFont.load inside a function, and then render my canvas when the font is ready to be rendered on screen:

code:

function loadFont(fontFamily){
	WebFont.load({
		google: {
			families: [fontFamily]
		},
		active: function() {renderScreen();},
	});
	
}

The only other place I've seen that event being attached in the documentation is the global var when the page is loaded, but not any other fonts that may load within the lifespan of the page.

necrotic
Aug 2, 2005
I owe my brother big time for this!
The configuration section of the readme says explicitly that the config options listed can go into the load call. In the very first sentence of that section.

LP0 ON FIRE
Jan 25, 2006

beep boop

necrotic posted:

The configuration section of the readme says explicitly that the config options listed can go into the load call. In the very first sentence of that section.

You're right, but good documentation would actually show an example of it happening.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice
Wasn't somebody in here starting an immutable date library? Is this you: https://js-joda.github.io/js-joda/ ??

necrotic
Aug 2, 2005
I owe my brother big time for this!
It's this guy.

Dominoes posted:

Hey dudes. Looking for API feedback on the datetime module I've been working on. And a re-attack on why I can't get it to import as an npm module (But can by placing the source file in the same folder as my project). I've decided to de-couple it from DateFns, since I'm unable to get around it not accepting these custom types as valid when using certain funcs, as well as API limitations I hadn't noticed with it earlier. (ie it's impossible to safely turn a string into a date with that module).

I'd also like to verify that I'm not alone in thinking not having separate date, time, datetime, and delta types is absurd.

necrotic
Aug 2, 2005
I owe my brother big time for this!
Also gently caress yes a joda clone.

Edit by the author of joda even. Finally, a not poo poo date lib for js. No offense dominoes, it's a hard problem.

Dominoes
Sep 20, 2007

Yep, twas me... And that library looks exactly like what I was trying to build... to the point where it means I should abandon the project, despite the basics being done. Date-fns was a false-messiah, but this may be the real deal.

Adbot
ADBOT LOVES YOU

geeves
Sep 16, 2004

necrotic posted:

Also gently caress yes a joda clone.

Edit by the author of joda even. Finally, a not poo poo date lib for js. No offense dominoes, it's a hard problem.

Joda Time was a godsend pre-Java 8, so it's nice to see that JsJoda exists. Wish I had known about it sooner. Moment is decent, but its API is just weird. A more Java-like implementation is most welcome. And may be something to which I can actually contribute.

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