Posts by Luiz Felipe • 32,886 points
746 posts
-
4
votes1
answer74
viewsQ: What is Clone-on-write (Cow)?
I’m studying Rust and recently discovered the existence of Cow, one smart-Pointer that works to make Clone-on-write. The description of the page seemed confusing to me, since I don’t have much…
-
6
votes1
answer60
viewsA: How do I keep an original copy when using Sort in Javascript arrays?
This is because you are not actually making a copy of the array. Note here: var rank = [292, 130, 55, 232, 213, 62, 152]; var rankOrdered = rank; When you do var rankOrdered = rank, you are…
-
6
votes1
answer107
viewsQ: How does the use of "Set" to remove repeated values from an array in Javascript work?
Is it known almost general which, in Javascript, can be used this trick with Set and Operator spread for remove values duplicates of an array: const arr = [1, 1, 1, 2, 2, 3]; const deduped = [...new…
-
7
votes1
answer107
viewsA: How does the use of "Set" to remove repeated values from an array in Javascript work?
Why the Set removes duplicate elements? The term Set, English for "set", comes from the mathematical concept of Ensemble. Of the definition, a set is composed of elements (or no element, the empty…
-
5
votes1
answer89
viewsA: Is it possible to apply or simulate immutability to an "object" of the Map type in Javascript?
TL;DR It is impossible to make an instance of Map unchanging in Javascript. First of all, a few things need to be clarified. All the mechanisms used in the question to make an object "immutable" are…
-
6
votes1
answer85
viewsA: What’s the difference between mod and Rust?
The difference is that they do different things. : D mod The keyword mod declaring a module in Rust. Modules are used to control scope and privacy. For an introduction to the concept of modules, see…
-
5
votes1
answer53
viewsA: What good is lib.rs in Rust?
The archive lib.rs is the entry-point conventional libraries written in Rust. All the items (functions, structures, enumerations, traits, etc) exported in that entry-point can be accessed by other…
-
7
votes1
answer313
viewsA: What are the precautions to be taken when using top-level await in Javascript?
The top-level await (TLA) is a new Javascript feature that allows operator usage await in the higher scope of Ecmascript modules (ESM). This higher scope is called top-level. The behavior of await…
-
4
votes2
answers86
viewsA: Why does Node.js accept value assignment with the "Undefined" identifier?
Why Node.js accepts value assignment for "Undefined"? It’s not just Node.js. Like undefined is not a keyword, can be used as an identifier, so it can act as a property name, variable, etc. See:…
-
6
votes2
answers71
viewsA: How to define a dynamic named CSS property via Javascript?
You can use the method setProperty, available in the object Element.style. For example: const body = document.querySelector('body'); function changeCSS() { const propName = 'background-color'; const…
-
3
votes3
answers104
viewsA: Syntactic difference between classes and constructor functions in Javascript?
It is often mistakenly thought that all that is delimited by keys ({ and }) is a block. And that’s not always the case. Obviously it does not apply to literal objects, which also use the keys to…
-
2
votes1
answer37
viewsA: What’s the difference between using one or two Arrow Functions in Useeffect?
It is known that functions returned by the function passed to the useEffect are used by React as the Cleanup Effect, that is, it will be executed when the component is disassembled. Therefore, do:…
-
4
votes2
answers68
viewsQ: Automatic null value check versus types like "Option<T>"?
I recently started learning Rust and was introduced to the type Option<T>, representing, through a sum type, the presence or absence of a value (mutually exclusive possibilities). This same…
-
3
votes1
answer39
viewsA: How to remove the " n" with jQuery?
You don’t need to use jQuery for this. Use the method String.prototype.trim, of Javascript itself. For example: const str = ' \n \n pix\n'; // Remove os espaços em branco. const result = str.trim();…
-
3
votes1
answer72
viewsA: How to convert a Promise-based function to callback in Javascript?
By convention, the first parameter of the callback is always what indicates the error, if occurred. The others are the possible success values. As a promise can only solve one value at a time, so…
-
3
votes3
answers81
viewsA: Is it possible to use functions in an arbitrary order in C++?
Actually this is more of a feature of language. In C++ (as in C), you must have the function declared before using it. Note, in your first example, that the function add_number was declared (and…
-
7
votes4
answers150
viewsA: What regular expression to use to replace symbols in a string?
Only as an alternative to another answer, can solve this problem with just a scan of the string instead of a scan for each token mapped as a result of the use of String.prototype.replaceAll. As each…
-
4
votes2
answers129
viewsQ: How to choose a random array element in Javascript?
I already know how shuffle an array, but how do I choose a random element of arrays? let arr = ['a', 'b', 'c']; How do I choose any element of arr?…
-
5
votes2
answers129
viewsA: How to choose a random array element in Javascript?
Just choose an index within the limits of the array and then index it: // Escolhendo o índice aleatório para o array `arr`: const randomIndex = Math.floor(Math.random() * arr.length); // Indexando o…
-
3
votes2
answers67
viewsA: What does the "jsx" prop do in Next.js' jsx <style?
In fact it’s not the same. When you add to prop jsx, the "compiler" (it is a plugin babel) acknowledges that the library styled-jsx should assume the "stylization". From there, the plugin of Babel…
-
3
votes1
answer47
viewsA: How to verify if object has specific property without the operator "in"?
You can use the function Reflect.has, which has the same operation as the operator in, only in a qualified function format in the overall object Reflect. So: if…
-
4
votes1
answer47
viewsA: Why does the "delete" operator not remove references to a deleted Javascript property?
Because the delete does not have the function of "erase" the existence of a memory value, only remove a property from an object. Of documentation: The operator delete removes a property from an…
-
3
votes1
answer73
viewsA: What is the purpose of the Promises Timers API on Node.js?
It has no purpose very different from those that already exist; the idea is to facilitate the use of timers in cases where promises are used. I’m sure these new Apis have been coming in strong on…
-
3
votes1
answer95
viewsA: How to import content from a JSON within an ESM (Ecmascript modules) module in Node.js?
The new Ecmascript Modules (ESM) syntax has been stabilized in recent versions of Node.js to replace Commonjs, non-standard by language specification. Although the "goal" of ESM and CJS are the…
-
5
votes2
answers100
viewsQ: What does "ad-hoc" mean in the context of computing?
Eventually I see some terms preceded by "ad-hoc", but I still can’t quite understand what it means. When used "ad-hoc" in a term related to computation, is there a common meaning? What does it mean…
terminologyasked Luiz Felipe 32,886 -
5
votes1
answer44
viewsA: How to use Result in user-defined function?
As stated in the question itself, the Result is an enumeration. In short, the problem is only to return the values that should be "within" the Result. When a enum, should be made clear which of the…
-
2
votes1
answer32
viewsA: What is the real difference between Fs.readFile() and Fs.createReadSream()?
The function fs.readFile reads the specified file and loads all its contents into a variable. This means that the program will need to allocate enough memory to hold what is contained in the file.…
-
7
votes3
answers9745
viewsA: What are Javascript Promises (promises)?
A promise is a "box" (formally, in Javascript, a object) that encapsulates a value that will be obtained from an asynchronous operation. Generally this value comes from I/O operations, which are…
-
9
votes0
answers259
viewsQ: What is the relevance of category theory to computer science?
I’m studying a little about functional programming and not infrequently I end up coming across people who talk about Category Theory, which makes me think it might be something with some importance…
-
2
votes1
answer41
viewsA: Promise solved or rejected multiple times by "blocking" execution of the rest of the code
Note that from the getter userEmail, you are creating a promise, which is returned. In the index.js, you call that getter: const email = await new GetEmail().userEmail; I mean, you’re creating a…
-
7
votes1
answer102
viewsA: How does . pipe() work on Node.js?
Basically, the method of pipe "directs" the flow of a readable stream for a writable stream. In the case of the question, pipe the "packages" of the read stream from the archive to the write stream…
-
2
votes1
answer36
viewsA: How to remove null decimal part (00) of Javascript number formatting?
The simplest answer is to remove the ,00 manually in case it exists, since there is no format option that covers that format. function fmt(num) { return num .toLocaleString('pt-PT', {…
-
5
votes1
answer124
viewsA: What does the "value used here after move" error mean in Rust?
Rust is an extremely different language from the others mainstream, And that differential factor is precisely what this question is about. Although somewhat "strange", this factor is also seen as…
rustanswered Luiz Felipe 32,886 -
2
votes4
answers350
viewsA: How to add values from a dictionary with arrays by the Python key?
You can use the method dict.items to iterate over each key and dictionary value. Since the value of each element of the dictionary is a list of numbers (and list are eternal values), you can use the…
-
5
votes2
answers84
viewsA: How to access the position of multiple matchs of a regex in a string?
You are using the function re.search to do the match of the regular expression. According to the documentation: Scan through the string looking for first position where regular expression produces a…
-
5
votes1
answer146
viewsA: What is the difference between the "in" operator and the "hasOwnProperty" method in Javascript?
What is the difference between the operator in and the method hasOwnProperty? When using one or the other? Operator in The operator in can be used in objects to verify the existence of some…
-
3
votes1
answer57
viewsA: Unreported variable type error in class constructor
Notice that the class ErrorHandler extend Error (and therefore must follow the contract stipulated by Error). However, it does not define any property statusCode explicitly, so that the type to…
-
2
votes1
answer60
viewsA: What use is memoryview in Python?
Briefly, in Python, the basic types for manipulating binary data are bytes and bytearray. They, as well as arrays, for example, they are supported by the memoryview, who uses the "buffer Protocol"…
-
3
votes1
answer122
viewsA: What is the difference between "javascript:void(0)" and only "void(0)" when defining onclick in HTML?
When evaluated as Javascript, the two codes will do the same thing: evaluate to undefined (which is what the operator void does). In practice, the effect of this is the same: nothing. The difference…
-
4
votes1
answer60
viewsA: How can a value be invoked (as a function) at the same time as it contains properties (as an object) in Javascript?
In Javascript, every function is an object. And it’s quite simple, escaping the formality of language, to prove it. See: function x() {} x instanceof Object; //-> true In more detail, "function…
-
3
votes3
answers68
viewsA: Regular expression to take one or more occurrences that precede and follow a given letter
This is because regular expression disregards which sequences of e can come one after the other. That way, even though this works: /(\w+\se\s\w+)/g foo aaa e bbb bar baz ccc e ddd qux This wouldn’t…
-
5
votes2
answers229
viewsA: If Python strings are immutable, how can we change them with the replace() method?
According to class documentation str: Python text data is treated as objects str, or strings. Strings are strings immutable of code points Unicode. So, since strings are immutable in Python, it is…
-
3
votes1
answer167
viewsA: How to use setTimeout to show a component only after a certain time with React?
That won’t work because the setInterval does not behave properly to the model that React works with. In JSX, everything put between keys ({}) is evaluated as an expression by React and rendered in…
-
4
votes1
answer55
viewsA: How do I "capture" the full route of an http call with express router?
To get the request path Use the property path. Of documentation: // https://example.com/users?sort=desc console.dir(req.path); //=> '/users' To get the full URL Utilise req.protocol to obtain the…
-
3
votes1
answer103
viewsQ: What is the Unicode flag "u" in regular expressions? What is its function?
Some time ago I discovered that regular expressions can also use the flag u, which I think is short for Unicode. What is the purpose of this flag? I know it was added in some recent version of…
-
4
votes1
answer54
viewsA: Error "Argument of type is not Assignable to" in function with Rest parameters in Typescript
The problem is that the guy Role[] is different from the type expected by the construction (...roles: Role[]). The guy Role[] means any array whose elements are attributable to the type Role.…
-
1
votes1
answer37
viewsA: this is assuming the value of Window
This is happening because you are using a Arrow Function, whose this Binding has lexical functioning. This means that the this shall not refer to the EvilCircle, but yes to the this of the higher…
-
4
votes1
answer68
viewsQ: What are dependent types and dependent language?
About what they are statically typed languages I am already aware, but the concept of dependently typed language is new to me. I think it has to do with something called dependent types, but I also…
-
4
votes1
answer74
viewsA: How to flatten a nested object that may contain repeated Javascript keys?
The hole is a little lower. Note that you want to "flatten" an object that can contain, in the various nesting levels, repeated keys. And as I explained here, in Javascript, an object nay can own…
-
5
votes1
answer115
viewsA: How to type keys (Keys) of objects nested in Typescript?
I think it is not possible to do this with arrays (at least not in a way simple) because the compiler has no way of knowing the type of the element associated with a specific key for each element of…