Step into our coding showcase series, where you can elevate your projects, exhibit your coding prowess, and collaborate with like-minded developers...
For further actions, you may consider blocking this person and/or reporting abuse
Using this:
Instead of this:
It's so beautiful hahaha
I've always loved the one-liner for summing up an array of numbers in JavaScript. A lot of people take a hard line on
reduce
, but I think it looks really nice here.One liners are the absolute best!
Toggling by using:
has to be one of my favorites.
I think pulling from arrays with some filters in Ruby is nice, such as
uniq
removes dupes andcompact
removesnil
and.select(&:even?)
will limit the output to even numbers.what is the syntax
&:even?
?I've heard it called a "symbol to proc" or "symbol proc" and it's short for this:
It's calling the method
even?
on each number passed in. Behind the scenes it creates a proc and then passes in each number. Here are the ruby docs for Symbol#to_proc and here's a pretty decent break down of symbol to proc.I find using nested hashmaps instead of if-else statements rather elegant when running decision based flows.
I've recently refactored a codebase with a lot of nested if-else control flows using this method. Said project is now much easier to read since decision logic is abstracted from the function using it.
What I also like is that it's easier to type your decision map with jsdoc or ts.
Example
Same functionality with if-else
Fast inverse square root, the function that revolutionized 3D games industry:
It's cool, but I wouldn't ever call it "elegant"!
I didn't get a damn thing, but that one is just beautiful
This is my best code snippet yet. It does exactly what you see it does
Duff's Device
From Duff's device
The unroll with uneven ending version is a thing of beauty:
I find this super handy if you’re building DOM elements without a framework.
I like the way you handle the props in your function, I just would prefer to immediately add some content to the new element. I slightly extended your function to add some content (a text, a child node or an array of child nodes)
There is a really tricky pattern to build "creator" functions for DOM elements based on your function (thanks to Tao from VanJS) :
The code above could be written more compact, but this makes it really hard to understand. makefnc returns a function that creates a special type of element. You can use it that way:
Using the tags function is even more elegant. Below, I show a little code example:
There is an even more compact approach on the same principle presented here, that adds an auto-append feature, so you do not even need to append the elements manually.
I didn’t bother with the content as you can just add a prop
textContent
or similar, but glad to see you’re extending it!Sure, in that case it's just a question of convenience. But you can also build nested elements, so it´s a way to compose your page or parts of it :
By the way, CSS styles use a syntax, which is a bit different from JSON:
With your approach, people need to translate the style definition like this:
As you need to process the styles anyway, would it not be better to use the usual syntax?
If you do not want to process the CSS manually, you can use this function:
Perhaps I'm the one here that post non brackets-semicolon snippets, It's GDScript. But here you go
Reversing a string in Javascript looks super simple and easy to me since day one. You could reverse a string in Javascript using a reverse for loop, but we can utilize the powerfull built-in Array methods reverse() and join() to create a one-liner that does the same thing.
In Laravel I learned something by accident. I was trying to retrieve the roles of the user using this Query Builder.
User::find(1)->rolesPivot()->get()->pluck(‘roles’)->implode(‘, ’);
This outputs (example value only)
Admin, Reporter, Staff
Or this implementation of the Observer Design pattern in react.
I found a simple way, to overcome the context limitation of a setInterval.
Demo here: codesandbox.io/s/demosetintervalco...
Without this pattern, the context value never changes.
Still cannot get over how simplified handling click event is in javascript