Skip to content

Instantly share code, notes, and snippets.

@marcinnajder
Created November 28, 2022 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcinnajder/e7a7b5d3de6a947c1197e35e9a266565 to your computer and use it in GitHub Desktop.
Save marcinnajder/e7a7b5d3de6a947c1197e35e9a266565 to your computer and use it in GitHub Desktop.
using System;
//using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Linq;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Workshop.CSharp.Advanced.Tests")]
namespace Workshop.CSharp.Advanced
{
static class MojeRozszerzenia
{
public static IEnumerable<T> Top<T>(this IEnumerable<T> items, int count)
{
var counter = 0;
foreach (var item in items)
{
if (counter++ >= count)
{
yield break;
}
yield return item;
}
}
public static void ExecuteAcionNTimes(this int n, Action<int> cosDoZrobienia)
{
for (int i = 0; i < n; i++)
{
cosDoZrobienia.Invoke(i);
}
}
public static bool Exists_<T>(this IEnumerable<T> elementy, Func<T, bool> warunek)
{
foreach (var element in elementy)
{
if (warunek(element) == true)
{
return true;
}
}
return false;
}
public static T Find<T>(this IEnumerable<T> elementy, Func<T, bool> warunek)
{
foreach (var element in elementy)
{
if (warunek(element))
{
return element;
}
}
return default(T);
}
public static IEnumerable<R> Zip<T1, T2, R>(this IEnumerable<T1> list1, IEnumerable<T2> list2, Func<T1, T2, R> konwerter)
{
var iterator1 = list1.GetEnumerator();
var iterator2 = list2.GetEnumerator();
while (iterator1.MoveNext() && iterator2.MoveNext())
{
yield return konwerter(iterator1.Current, iterator2.Current);
}
}
public static IEnumerable<T> FindAll_<T>(this IEnumerable<T> items, Func<T, bool> predicate)
{
foreach (var item in items)
{
if (predicate(item))
{
yield return item;
}
}
}
public static IEnumerable<T> FindAll_<T>(this IEnumerable<T> items, Func<T, int, bool> predicate)
{
int i = 0;
foreach (var item in items)
{
if (predicate(item, i++))
{
yield return item;
}
}
}
public static IEnumerable<R> ConvertAll_<T, R>(this IEnumerable<T> items, Func<T, R> converter)
{
foreach (var item in items)
{
yield return converter(item);
}
}
public static IEnumerable<R> ConvertAll_<T, R>(this IEnumerable<T> items, Func<T, int, R> converter)
{
int i = 0;
foreach (var item in items)
{
yield return converter(item, i++);
}
}
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{
action(item);
}
}
public static A Fold<T, A>(this IEnumerable<T> items, A seed, Func<A, T, A> aggregator)
{
A total = seed;
foreach (var item in items)
{
total = aggregator(total, item);
}
return total;
}
public static IEnumerable<R> Collect<T, R>(this IEnumerable<T> items, Func<T, IEnumerable<R>> getSubItems)
{
foreach (var item in items)
{
var subItems = getSubItems(item);
foreach (var subItem in subItems)
{
yield return subItem;
}
}
}
}
class ZapisanoOsobeWBazieEventArgs : EventArgs
{
public int IdOsoby { get; set; }
}
class Comarch
{
public Comarch GetEnumerator() => this;
public string Current { get => "marcin"; }
public bool MoveNext() => true;
}
class Program
{
class Osoba
{
public int Id { get; set; }
private string _name;
public string Name
{
get { return _name; }
set
{
if (value == null)
{
throw new Exception("asdasdaasd");
}
_name = value;
}
}
public List<Adres> Adresy { get; set; }
// public Osoba()
// {
// }
// public Osoba(int id, string name, List<Adres> adresy)
// {
// Id = id;
// Name = name;
// Adresy = adresy;
// }
}
class Adres
{
public string City { get; set; }
}
static void WystarujZadanieJutro(Action<DateTime> zadanie)
{
var t = new Thread(() =>
{
Thread.Sleep(1000);
zadanie(DateTime.Now);
});
t.Start();
}
private static void asdasdadasdasdasdadad(DateTime s)
{
}
static IEnumerable<int> Return123()
{
var result = new List<int>();
result.Add(11);
result.Add(1100);
return result;
}
static IEnumerable<int> Return123_()
{
// var result = new List<int>();
yield return 11;
yield return 1100;
// return result;
}
static IEnumerable<int> Psikus()
{
while (true)
{
yield return 666;
}
}
public static IEnumerable<int> Range(int start, int count)
{
var end = start + count;
for (int i = start; i < end; i++)
{
yield return i;
}
}
IEnumerable<FileInfo> GetFileSequence(string folderPath)
{
yield break;
}
static void Main(String[] args)
{
var osoby = new List<Osoba>
{
new Osoba()
{
Id = 1,
Name = "Marcin",
Adresy = new List<Adres>
{
new Adres { City = "Wroclaw"} ,
new Adres { City = "Krakow"}
}
},
new Osoba() {
Id = 2,
Name = "Gosia",
Adresy = new List<Adres>
{
new Adres { City = "Warszawa"} ,
}}
};
// 1. wykorzystanie zwyklej listy
// var osobyZ2Adresami = osoby
// .FindAll(o => o.Adresy.Count >= 2)
// .ConvertAll(o => o.Id);
// int counter = 0;
// foreach (var osoba in osoby)
// {
// if (counter++ < 100)
// {
// /// o.Id
// }
// else
// {
// break;
// }
// }
// 2. wykorzustanie statucnzych metod
// var temp1 = FindAll(osoby, o => o.Adresy.Count >= 2);
// var temp2 = ConvertAll(temp1, o => o.Id);
// var temp3 = Top(temp2, 10);
// foreach (var id in temp3)
// {
// }
// 3. wykorzustanie extension metod
// var temp3 = osoby
// .FindAll_(o => o.Adresy.Count >= 2)
// .ConvertAll_(o => o.Id)
// .Top(10);
// var aa = MojeRozszerzenia.Top(
// MojeRozszerzenia.ConvertAll_(
// MojeRozszerzenia.FindAll_(
// osoby,
// o => o.Adresy.Count >= 2),
// o => o.Id),
// 10);
// foreach (var id in temp3)
// {
// }
// 3. wykorzustanie LINQ
// var temp3 = osoby
// .Where(o => o.Adresy.Count >= 2)
// .Select(o => o.Id)
// .Take(10);
// var dic = Fold(osoby, new Dictionary<int, Osoba>(), (dic, o) =>
// {
// dic.Add(o.Id, o);
// return dic;
// });
// var a3 = Fold(
// new List<int> { 1, 2, 3, 4, 66, 7, 8 },
// 0,
// (prev, current) => prev + current);
// int a = new int[] { 1, 2, 3, 4, 66, 7, 8 }.Aggregate(
// 0, (prev, current) => prev + current);
// int a2 = new int[] { 1, 2, 3, 4, 66, 7, 8 }.Aggregate(
// int.MinValue,
// (prev, current) => Math.Max(prev, current));
// var names = new List<string>()
// {
// "marcin", "gosia", "janusz", null
// };
// var temp1 = FindAll(names, n => n != null);
// var temp2 = ConvertAll(temp1, (n, index) => index + ". " + n.Length);
//ForEach(temp2, Console.WriteLine);
// names
// .FindAll(n => n != null) // WHERE
// .ConvertAll(n => n.Length) // SELECT
// .ForEach(x =>
// {
// //todo: ...
// });
var datyUrodzenia = new List<DateTime>()
{
new DateTime(2000,1,1),
new DateTime(2004,1,1),
new DateTime(203,1,1)
};
}
private static void WypiszZeCSharpJestOk(int x)
{
Console.WriteLine(" C# jest ok " + x);
}
}
}
// Runner.StartRepl(Assembly.GetExecutingAssembly());
// Generics.RunZipUnzip();
// Delegates.RunZip();
// Delegates.RunCompose();
// Iterators.RunZip();
// Iterators.RunFileSequence();
// Runner.ExecuteAll(Assembly.GetExecutingAssembly());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment