diff --git a/content/javascript/concepts/destructuring/destructuring.md b/content/javascript/concepts/destructuring/destructuring.md new file mode 100644 index 00000000000..c7954d9d51e --- /dev/null +++ b/content/javascript/concepts/destructuring/destructuring.md @@ -0,0 +1,120 @@ +--- +Title: 'Destructuring' +Description: 'Destructuring in JavaScript extracts values from arrays or properties from objects and assigns them to variables in a single, concise statement.' +Subjects: + - 'Computer Science' + - 'Web Development' +Tags: + - 'Arrays' + - 'Destructuring' + - 'JavaScript' + - 'Objects' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/front-end-engineer-career-path' +--- + +**Destructuring** in JavaScript extracts values from [arrays](https://www.codecademy.com/resources/docs/javascript/arrays) or properties from [objects](https://www.codecademy.com/resources/docs/javascript/objects) and assigns them to [variables](https://www.codecademy.com/resources/docs/javascript/variables) in a single, concise statement. It simplifies working with complex arrays and objects by reducing repetitive code. + +## Array Destructuring + +Array destructuring extracts values from arrays in order, assigning them to variables in a single statement: + +```js +const colors = ['red', 'green', 'blue']; +const [firstColor, secondColor, thirdColor] = colors; + +console.log(firstColor); +console.log(secondColor); +console.log(thirdColor); +``` + +The output for this is: + +```shell +red +green +blue +``` + +Items that aren’t needed can also be skipped: + +```js +const colors = ['red', 'green', 'blue']; +const [primaryColor, , tertiaryColor] = colors; + +console.log(tertiaryColor); // Output: blue +``` + +## Object Destructuring + +Object destructuring extracts specific properties from an object using their key names: + +```js +const car = { make: 'Toyota', model: 'Camry', year: 2021 }; +const { make, model } = car; + +console.log(make); // Output: Toyota +console.log(model); // Output: Camry +``` + +The output for this code is: + +```shell +Toyota +Camry +``` + +Variables can also be renamed or assigned default values: + +```js +const car = { make: 'Toyota', model: 'Camry', year: 2021 }; +const { model: carModel, color = 'white' } = car; + +console.log(carModel); // Output: Camry +console.log(color); // Output: white +``` + +## Nested Destructuring + +Destructuring also supports nested structures for deeply nested objects or arrays: + +```js +const user = { + id: 1, + info: { name: 'Alice', address: { city: 'Seattle', zip: 98101 } }, +}; + +const { + info: { + name, + address: { city }, + }, +} = user; + +console.log(name); +console.log(city); +``` + +The output of this code is: + +```shell +Alice +Seattle +``` + +## Codebyte Example + +The following codebyte example uses nested destructuring to extract values directly from an object: + +```codebyte/javascript +const user = { + name: 'John Doe', + hobbies: ['reading', 'cycling', 'coding'], + address: { city: 'New York', zip: 10001 } +}; + +const { name, hobbies: [firstHobby], address: { city } } = user; + +console.log(`${name} enjoys ${firstHobby} in ${city}.`); +``` diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md b/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md new file mode 100644 index 00000000000..64369428209 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md @@ -0,0 +1,82 @@ +--- +Title: '.logical_xor()' +Description: 'Performs element-wise logical exclusive OR (XOR) operation on boolean tensors, returning a tensor where each element is `True` if the corresponding elements of the inputs differ, and `False` otherwise.' +Subjects: + - 'AI' + - 'Computer Science' + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Booleans' + - 'Functions' + - 'PyTorch' + - 'Tensor' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +In PyTorch, the **`.logical_xor()`** function performs an element-wise **logical exclusive OR (XOR)** operation between two tensors. It returns a new tensor where each element is `True` if exactly one of the corresponding elements in the input tensors is `True`, and `False` otherwise. + +For non-boolean tensors, zeros are treated as `False` and non-zeros as `True`. This operation is often used in conditions involving mutually exclusive logic or binary masking. + +## Syntax + +```pseudo +torch.logical_xor(input, other, *, out=None) +``` + +**Parameters:** + +- `input`: The first input tensor (boolean or numeric). +- `other`: The second input tensor (boolean or numeric). Must be broadcastable to the shape of `input`. +- `out` (Optional): A tensor to store the result. It must have the same shape as the output. The dtype is typically `torch.bool`, but integer types (like `torch.int16`) are also supported to represent `0` and `1`. + +**Return value:** + +Returns a tensor containing the element-wise logical XOR of the two input tensors. + +## Example + +The following example demonstrates the use of `.logical_xor()` for boolean and numeric tensors: + +```py +import torch + +# Boolean tensors +a = torch.tensor([True, False, True, False]) +b = torch.tensor([True, True, False, False]) + +# Element-wise logical XOR +res = torch.logical_xor(a, b) + +print('a:', a) +print('b:', b) +print('logical_xor(a, b):', res) + +# Numeric tensors — zeros are False, non-zeros are True +x = torch.tensor([0, 1, 2, 0]) +y = torch.tensor([1, 0, 2, 0]) + +print('\nx:', x) +print('y:', y) +print('logical_xor(x, y):', torch.logical_xor(x, y)) + +# Using an integer out tensor (int16) to store 0/1 results +out_buf = torch.empty(4, dtype=torch.int16) +torch.logical_xor(x, y, out=out_buf) +print('\nlogical_xor(x, y) with out=int16:', out_buf) +``` + +The above code produces the following output: + +```shell +a: tensor([ True, False, True, False]) +b: tensor([ True, True, False, False]) +logical_xor(a, b): tensor([False, True, True, False]) + +x: tensor([0, 1, 2, 0]) +y: tensor([1, 0, 2, 0]) +logical_xor(x, y): tensor([ True, True, False, False]) +logical_xor(x, y) with out=int16: tensor([1, 1, 0, 0], dtype=torch.int16) +```