Coder Social home page Coder Social logo

ardalis / ardalis.extensions Goto Github PK

View Code? Open in Web Editor NEW
150.0 7.0 34.0 1.34 MB

Some random C# extension methods I've found useful. Published as Ardalis.Extensions on Nuget.

License: MIT License

C# 100.00%
ardalis csharp dotnet extension-methods extensions hacktoberfest

ardalis.extensions's Introduction

Hi there ๐Ÿ‘‹

Want to help? Here are my "Good First Issue" issues.

ardalis

ardalis

Visitors since 11 Nov 2020

ardalis.extensions's People

Contributors

ardalis avatar bsilvia avatar josuave avatar markusgnigler avatar moatazmohamedallam avatar nisharuban avatar pratik2315 avatar seb-stian avatar shadynagy avatar snowfrogdev avatar thelazylemur avatar webwat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ardalis.extensions's Issues

What is the plan for this NuGet/repository?

First of all, thank you for this repository and NuGet package. It can really be useful.

But I was wondering, what is the plan for this NuGet package? Because I can see people adding some extension methods.
Some questions:

  • Some of these methods that people are adding, are not full-proof, we need to make sure that they're are resilient and does the job 100%.
  • Some of these methods are lacking proper documentation - XML structure. They should have proper XML comments, with remarks, external links, potential exceptions, return values and so on.
  • Tests should be too more rigorous, testing not one, but many possible inputs.
  • Changelog could be added to the repository too.
  • StyleAnalizers or something to unify code style would be nice too.

TL:DR What are the plans for this repository? Is this one time, initial contribution or there are any future plans?

Math Extensions

A set of extension methods should be made for numeric types that provide mathematic operations.

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus
  • Powers

Others?

Make ForEach return an Iterator, like `Tap` in other languages

At the moment, our implementation of ForEach can only be used as the only or last step in a LINQ operations pipeline as it forces the execution of the chain of operations and returns void.

Just because ForEach allows us to perform side effects doesn't mean that it needs to take us out of the Iterator pipeline. I'm thinking of the tap operator that exists in many other languages and libraries as an example of allowing the "streaming" to continue.

https://rxjs.dev/api/operators/tap
https://hexdocs.pm/elixir/main/Kernel.html#tap/2

This would allow for things like this:

var blueWhalesWeight = whales
 .Where(w =>  w.Color == "blue")
 .ForEach(w => Console.WriteLine(w.Name))
 .Select(w => w.Weight)
 .ToList();

This would also mean that it is no longer possible to just do:

whales
 .Select(w.Color == "blue")
 .ForEach(w => Console.WriteLine(w.Name));

I guess at the end of the day it depends on our philosophy regarding Iterators. There is a reason why many languages choose to not implement a ForEach on their Iterator and that is because it doesn't fit very well with the abstraction. Iterators tend to encourage a more "functional" approach by creating a pipeline of operations where data comes in on one end, goes through a set of pure functions, and comes out at the other end transformed. The ForEach kind of breaks that abstraction in two ways:

  1. the very intent of the method is to make side effects
  2. it doesn't return anything

Languages have compromised with this by introducing the tap operation, which allows for side effects, but keeps the flow of data going.

So, I guess I'm proposing that we either 1) make ForEach into a Tap or, if we prefer to leave ForEach the way it is, that we introduce a Tap extension method.

Add usage examples to README

Show basic usage of each extension in the README.

Organize using same organization as folder structure of code.

Show how to perform the operation without the extension, and then what it looks like with the extension.

ToInt method implementation

Hello.

I saw a ToInt() method in StringExtensions and it has raised me a question:

  • Why int.Parse() is being used instead of int.TryParse()? This way, if a string is not a valid number, we wouldn't get an exception.
  • Also, I think we should handle the cases where a string is a number that is out of bounds (int.MaxValue and int.MinValue) and throw exceptions or something else.

Add string.Reverse extension

Self-explanatory, adds new extension method for string that reverses it. For example: "abc" โ†’ "cba"

I would be happy to work on this.

ToInt might throw an exception

public static int ToInt(this string input)
{
if (string.IsNullOrEmpty(input))
{
return 0;
}
return int.Parse(input);
}

According to the above code block, we can convert a string to an integer.

I think this may fail in some cases.

For example, I get a result from an HTTP request. Let's assume it contains a value like this;

int a = response.Age.ToInt(); // response.Age returns 3A

In this case, you'll get an exception because of the value 3A.

What if we change the code block like this;

public static int ToInt(this string input)
{
	int number = 0;

	if (string.IsNullOrEmpty(input))
	{
		return number;
	}

	int.TryParse(input, out number);

	return number;
}

Otherwise, you could get an exception like that;

Run-time exception (line 14): Input string was not in a correct format.

Stack Trace:

[System.FormatException: Input string was not in a correct format.]
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.Int32.Parse(String s)
   at StringExtensions.ToInt(String input) :line 14
   at Program.Main() :line 24

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.