Skip to content

Instantly share code, notes, and snippets.

View KeyurRamoliya's full-sized avatar
🎯
Focusing

Keyur Ramoliya KeyurRamoliya

🎯
Focusing
View GitHub Profile
@KeyurRamoliya
KeyurRamoliya / Using-yield-for-Deferred-Execution.md
Last active September 14, 2023 15:52
Using yield for Deferred Execution

Using yield for Deferred Execution

C# provides the yield keyword, which can be used to create iterator methods. These methods produce a sequence of values lazily, allowing for deferred execution. This can be particularly useful when working with large data sets or when you want to avoid loading everything into memory at once.

In the below example, GetNumbers() is an iterator method that yields a sequence of squared numbers. The key here is that the values are generated one by one as you iterate through the sequence, and they're not all calculated and stored in memory at once.

The yield keyword allows you to maintain the state of the iterator between iterations and offers a clean way to implement custom iterators without explicitly managing state variables.

This approach is beneficial for scenarios where loading or calculating all the values at once is memory-intensive or time-consuming. It's a powerful technique to create more memory-efficient and responsive code.

@KeyurRamoliya
KeyurRamoliya / Using Memory<T> and Unsafe Code for Memory Manipulation.md
Created September 14, 2023 15:59
Using Memory<T> and Unsafe Code for Memory Manipulation

Using Memory<T> and Unsafe Code for Memory Manipulation

C# provides the Memory<T> struct, representing a chunk of memory, allowing you to work efficiently with arrays and spans. Combining Memory<T> with unsafe code will enable you to perform low-level memory manipulation operations for performance-critical scenarios.

Below is an example that demonstrates copying data using Memory<T> and unsafe code. In this example, Memory<T> represents source and target arrays, and unsafe code is employed to perform a memory copy operation between the arrays. The fixed statement pins the arrays in memory to avoid them being moved by the garbage collector during unsafe operations.

While this technique can lead to performance improvements, it comes with risks and should be used cautiously. Unsafe code can introduce memory-related bugs and security vulnerabilities if not handled correctly. Understand the pitfalls thoroughly and consider using safer alternatives when possible.

using System;
@KeyurRamoliya
KeyurRamoliya / Use Template Literals for String Interpolation in JavaScript.md
Created September 14, 2023 16:18
Use Template Literals for String Interpolation in JavaScript

Use Template Literals for String Interpolation in JavaScript

Template literals, introduced in ECMAScript 6 (ES6), provide a more concise and readable way to interpolate variables and expressions within strings compared to traditional string concatenation.

Take a look at the example below. Template literals are enclosed in backticks (`) instead of single or double quotes. You can include placeholders ${expression} within the string, and JavaScript will automatically evaluate the expression and substitute it with its value.

Benefits of using template literals:

  • Improved readability: Interpolating variables within the string directly enhances code readability.
  • Multi-line strings: Template literals support multi-line strings without needing manual line breaks.
  • Complex expressions: You can include complex JavaScript expressions within placeholders.
@KeyurRamoliya
KeyurRamoliya / Asynchronous Streams with IAsyncEnumerable.md
Created September 14, 2023 16:21
Asynchronous Streams with IAsyncEnumerable

Asynchronous Streams with IAsyncEnumerable

C# 8.0 introduced the concept of asynchronous streams using IAsyncEnumerable<T>. This feature allows you to handle data sequences where each element is generated asynchronously and efficiently. It's beneficial when dealing with I/O-bound operations, such as reading from a network stream or a database.

In the below example, the GenerateNumbersAsync method is an IAsyncEnumerable<int> that asynchronously produces numbers. The await foreach loop asynchronously consumes these numbers as they become available.

Asynchronous streams are incredibly valuable for scenarios where you want to avoid blocking the calling thread while waiting for asynchronous data to arrive. This can include scenarios like streaming data from a web API or reading a large file asynchronously.

Use asynchronous streams carefully, as they can add complexity to your code. They are most beneficial when dealing with scenarios where asynchronous data production is a natural fit.

@KeyurRamoliya
KeyurRamoliya / Use Destructuring to Extract Values from Objects and Arrays in JavaScript.md
Created September 14, 2023 16:23
Use Destructuring to Extract Values from Objects and Arrays in JavaScript

Use Destructuring to Extract Values from Objects and Arrays in JavaScript

Destructuring is a powerful feature in JavaScript that allows you to extract values from objects and arrays easily. It can make your code more concise and readable, especially when working with complex data structures.

Destructuring Objects:

You can extract specific properties from an object and assign them to variables using object destructuring:

const person = { firstName: 'John', lastName: 'Doe', age: 30 };
@KeyurRamoliya
KeyurRamoliya / Use the Spread Operator to Copy Arrays and Objects.md
Created September 14, 2023 16:26
Use the Spread Operator to Copy Arrays and Objects

Use the Spread Operator to Copy Arrays and Objects

The spread operator (...) is a useful feature in JavaScript that allows you to create copies of arrays and objects easily. It's handy when you want to avoid mutating the original data or when you need to merge data from multiple sources into a new array or object.

For more details, read my Spread Operator blog at the link below.

The Spread Operator in JavaScript

Copying Arrays:

@KeyurRamoliya
KeyurRamoliya / Custom Attributes for Metadata and Reflection.md
Created September 14, 2023 16:28
Custom Attributes for Metadata and Reflection

Custom Attributes for Metadata and Reflection

Custom attributes in C# allow you to attach metadata to types, methods, properties, and other code elements. You can then use reflection to inspect this metadata at runtime. This is especially useful for scenarios like code analysis, serialization, or custom behaviors based on metadata.

Consider the below example, we define a custom attribute MyCustomAttribute, and apply it to a class and a method. Then, we use reflection to retrieve and inspect these attributes at runtime.

Custom attributes can be used for various purposes, such as defining validation rules, serialization instructions, or custom documentation. They are commonly used in libraries and frameworks to extend the functionality of code based on metadata.

using System;
@KeyurRamoliya
KeyurRamoliya / List Comprehensions In Python.md
Created September 14, 2023 16:30
List Comprehensions In Python

List Comprehensions In Python

Python's list comprehensions are a powerful and concise way to create lists. They can make your code more readable and reduce the need for explicit loops. Instead of writing a loop to generate a list, you can use list comprehension in a single line.

Using list comprehensions reduces the amount of code you need to write and makes your code more Pythonic and easier to understand.

Consider the below example demonstrating the use of list comprehensions to create a list of squares for all numbers from 1 to 10.

# Without list comprehension
@KeyurRamoliya
KeyurRamoliya / Using Promises for Asynchronous Operations.md
Created September 14, 2023 16:33
Using Promises for Asynchronous Operations

Using Promises for Asynchronous Operations

JavaScript is often used for tasks that involve asynchronous operations, like fetching data from an API. Promises are a fundamental concept for handling asynchronous code and managing the flow of execution.

A Promise represents a value that may not be available yet but will be at some point in the future, or it may fail. Promises provide a clean and structured way to handle asynchronous operations.

Below is a basic example of using Promises for an asynchronous operation, such as fetching data from an API using fetch.

A few key points about Promises:

@KeyurRamoliya
KeyurRamoliya / Document Your Code with Docstrings in Python.md
Last active October 2, 2023 10:54
Document Your Code with Docstrings in Python

Document Your Code with Docstrings in Python

Documentation is essential for understanding and maintaining code, and Python provides a straightforward way to add documentation to your functions, classes, and modules using docstrings. A docstring is a string literal that occurs as the first statement in a module, function, class, or method. It serves as documentation for that object.

Docstrings provide several benefits:

  • Documentation: They explain what a function, class, or module does, what arguments it accepts, and what it returns. This helps you and others understand and use your code correctly.
  • Auto-generating documentation: Many documentation tools and systems can extract docstrings to generate documentation in various formats, such as HTML or PDF.
  • IDE support: Integrated development environments (IDEs) can display docstring information when you hover over or inspect functions or classes, making it easier to work with your code.
  • Clarity: Well-documented code is more accessible and maintainable