threepointone / bs-nice

css-in-reason
180 stars 14 forks source link

write base62_of_int in pure reason #5

Closed threepointone closed 6 years ago

threepointone commented 6 years ago

currently uses bs.raw to inline javascript.

threepointone commented 6 years ago

https://github.com/threepointone/bs-nice/blob/e16f9692fb1b6e3514c2fc6c67f615649ab6ea9f/src/nice.re#L986

thangngoc89 commented 6 years ago

I'm working on it :D

darekrossman commented 6 years ago

Heh I took a stab at it too, was my first attempt azt writing any Reason code... looks like we came up with similar implementations @thangngoc89! I didn't add the rei or tests tho - still figuring it all out.

thangngoc89 commented 6 years ago

@subpopular ha. Nice attempt ! It's basically 1:1 map with the original JS code. But I want to use Reason stdlib only so I avoided using Js.*

P/s: Isn't number * Math.sign(number) === Math.abs(number)?

darekrossman commented 6 years ago

@thangngoc89 Interesting...I'm on like day 2 of Reason so this is good for me : ) One question though...

let newX = float_of_int(x^) /. 62. |> floor |> int_of_float; Why use float just to use floor then back to int? Doesn't x / 62 return a floored int?

Also, good point on the abs and keeping with the stdlib. TIL.

thangngoc89 commented 6 years ago

Ah. good point!. My rtop is currently broken so too lazy to check that. But isn't too magic ?

darekrossman commented 6 years ago

Seems expected that an int / int would return an int (always been my experience, though not in JS) - but again - everything is magic to me at this point heh :)

thangngoc89 commented 6 years ago

Yeah. I created a simple check at (DON"T OPEN IF YOU'RE ON MOBILE) https://reasonml.github.io/try/?reason=DYUwLgBAdiDuAaEC8EAUAPAlMgfBA3gFAQToQD0EAbAEwDchAvg4aJDAjcmlrgcRABmwAPYBDMAH0RgyQEsoYDNnIA6ajXUAfPMJEiAThB0QFUmZL0S6EJi0GG0c01AgAGCGBEQAjG-8B2EQkcoJoHPCoctgAhCgRNFGYQQIkAFIAzqqiAOZJAox2hJnZInkARAAmIjDlmEA

It's a loop from 0 to 1 milion to compare result between the 2 methods. And you're totally right. But it still feels like magic to me. I call that smart code

darekrossman commented 6 years ago

Slightly different approach with advice from @thangngoc89 https://github.com/threepointone/bs-nice/pull/7

threepointone commented 6 years ago

:) what a happy problem to have. what should I do here, I can't accept both clearly :/

thangngoc89 commented 6 years ago

@threepointone benchmark !

https://rebench.github.io/?rebench-data=NoIgNgpgLgBAHjAvDAjAJgMwBYDcIA0woaBIAThKZLAMYAWAhmQM5IzAA+AOgHYwwByAAwD8vfgJSjxgtNL6CM8iVmWCArGoEA2LQHYtADi0BOLQy0AjLTS0ATLRC0AzLQHMtdLQEstAKy0Aay0wLQBbLR4tAHstAActAEctMi1mLSgtAFctADctAHctOC0ATy0ALy0AQS0AIS0AYS0AES0AUS0AMS0AcS0ACS0ASS0AKS0AaS0AGS0AWS0AOS0AeS0ABS0ARS0AJS0AZS0AFS0AVS0ANS0AdS0ADS0ATS0ALQFeDgBdHF5eagwSwMZgQbRoAD60WcEO8PFgyAQiAAfDAAN4yQFImAUZwACgYlmYeLgAEpSX8FICaNEeLkICxvLS2Li8VwQOyKTICnRvJAYCSAHowZHIFCk9EyfiAngQAoPNjOMDRBhQKEwuFQIUSgD0ADoYOCDRxUUrotEyDATTBNeqIWbVZT+PwaXSGcwmXwAFzIRqMMh6iDMGgMOIQOx4+hMZjAODCgC0hrQMAAVDBZfLvhKANTZmCu+mM2mCp3OhA+9Nyh6lgC+pYL7s9Jd4df+PGBoPBds1JIpIG+hBAKFIFCo0CBILBkOhsPhbE1SFRGKp4+YpTClmiYFYiClMEOUDIcLcerc0DZCmdMHZQnQ2HU2j0hhMhJodggzjcvL8gTAYR40RxIkLBQFkuQFHApQVNUdSNC07RdL0AzDGMkwzPMSyrBs2x7IcJznFctwPM8bzsnuXIrrAFA0DAzh8MgeI8Pg+YSiie7MAU3hQPQ6aSpeVowEIi75nuHAwBCwl0YxMA6kmzF8Im4KpgKfCyeCErWmuG5bqw1p+kwgbBqG4Y5nmNB7q2ChSYSxKaqSzFkSAFGWbwHZTt28K9ng3w+UAA

I must admit, the second approach by @subpopular is more clever. But JIT can optimize a while loop better than a recursive function

threepointone commented 6 years ago

just going to pick the one that looked simpler to me. thank you to the both of you for the effort!