Code Monkey home page Code Monkey logo

ndash's Introduction

ndash

General-purpose utility and extension methods

IEnumerable Extensions

AreUnique(By): IEnumerable<T> -> bool

Returns true if all elements in a collection are are unique.

Usage:

bool yes = new[] { 1, 2, 3, 4, 5 }.AreUnique();
// -> true

var people = new[]
{
    new Person("John", "Doe"),
    new Person("Jane", "Doe"),
    new Person("Jon", "Snow")
};

var no = people.AreUniqueBy(p => p.LastName);
// -> false

CryptoShuffle: IEnumerable<T> -> IEnumerable<T>

Returns a shuffled copy of the collection, using a cryptographically secure random number generator (just because).

Usage:

IEnumerable<int> shuffled = new[] { 1, 2, 3, 4, 5 }.CryptoShuffle();
// -> [3, 1, 4, 2, 5]
// (or some other randomized order)

Disjunction: (IEnumerable<T>, IEnumerable<T>) -> DisjunctionResult<T>

Returns the left and right collections, minus overlapping values.

Usage:

var left  = new[] { 1, 2, 3 };
var right = new[] { 3, 4, 5 };

var (leftOnly, rightOnly) = left.Disjunction(right);
// leftOnly = [1, 2]
// rightOnly = [4, 5]

DisjunctiveUnion: (IEnumerable<T>, IEnumerable<T>) -> IEnumerable<T>

Returns a the left collection concatenated by the right collection, minus overlapping values.

Usage:

var left  = new[] { 1, 2, 3 };
var right = new[] { 3, 4, 5 };

var everythingButIntersection = left.DisjunctiveUnion(right);
// -> [1, 2, 4, 5]

Flatten: IEnumerable<IEnumerable<T>> -> IEnumerable<T>

Flattens a nested collection. Synonymous with nestedCollection.SelectMany(list => list).

Usage:

int[][] matrix = new[]
{
    new[] { 1, 2, 3 },
    new[] { 4, 5, 6 },
    new[] { 7, 8, 9 },
};

int[] array = matrix.Flatten();
// -> [1, 2, 3, 4, 5, 6, 7, 8, 9]

ForEach: (IEnumerable<T>, Action<T>) -> void

Like List<T>.ForEach, but usable on any IEnumerable

Usage:

var countries = new[] { "Canada", "United States", "Mexico" };

countries.ForEach(Console.WriteLine);

Merge: (IDictionary<K, V>, IDictionary<K, V>) -> IDictionary<K, V>

Merges 2 dictionaries, with the left dictionary winning on key conflicts.

Usage:

var person = new Dictionary<string, string>
{
    { "First Name", "John" },
    { "Last Name",  "Doe"  },
};

var employee = new Dictionary<string, string>
{
    { "First Name", "Chris"        },
    { "Last Name",  "Cringle"      },
    { "Occupation", "Delivery man" },
};

var merged = person.Merge(employee);
/*
{
    "First Name": "John",
    "Last Name": "Doe",
    "Occupation": "Delivery man"
}
*/

MergeRight: (IDictionary<K, V>, IDictionary<K, V>) -> IDictionary<K, V>

Merges 2 dictionaries, with the right dictionary winning on key conflicts.

SeparateBy: (IEnumerable<T>, Func<T, bool>) -> SeparateByResult<T>

Separates the collection into 2 collections: 1 with elements where the predicate is true, and the other where it's false.

Usage:

int IsEven(number) => number % 2 == 0;

var numbers = new[] { 1, 2, 3, 4, 5 };

var (evens, odds) = numbers.SeparateBy(IsEven);
// evens = [2, 4]
// odds = [1, 3, 5]

Shuffle: IEnumerable<T> -> IEnumerable<T>

Copies and shuffles the collection.

Optionally accepts a custom Random object, or a Func<int, int> random number generator.

If given a custom callback, provides the number of elements in the array.

Usage:

var cards = new[] { 1, 2, 3, 4, 5 };
var shuffled = cards.Shuffle();
// [3, 2, 4, 5, 1]
// (or some other randomized order)

Usage (custom shuffler):

using RNG = System.Security.Cryptography.RandomNumberGenerator;

var cards = new[] { 1, 2, 3, 4, 5 };

// synonymous with `cards.CryptoShuffle()`
var wellShuffled = cards.Shuffle(count => RNG.GetInt32(count));

Swap: (IList<T>, int, int) -> IList<T>

Swaps the elements at 2 indices. This mutates the original list!

Usage:

var letters = new[] { "a", "b", "c", "d" };
letters.Swap(0, 3);
// -> ["d", "b", "c", "a"]

ndash's People

Contributors

jrhart08 avatar

Stargazers

 avatar Dylan Yates avatar Kevin Cavnar-Johnson avatar Walton Leavell avatar

Watchers

James Cloos avatar  avatar

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.