Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Metaprogramming #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Metaprogramming/FilterSimple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metaprogramming
{
class FilterSimple
{

public List<String> filter(String[] names)
{
List<String> result = new List<string>();

foreach(String name in names)
{
if (
name.Length >= 10 && name.Length <= 200 &&
name.Contains("Mich") &&
name.IndexOf("V") == 0 &&
name.EndsWith("ov") &&
!(
name.Length >= 50 &&
name.Length <= 65 &&
name.Contains("Abu") &&
name.IndexOf("Lev") == 0 &&
name.EndsWith("iov")
)
)
result.Add(name);
}
return result;
}
}
}
63 changes: 63 additions & 0 deletions Metaprogramming/Metaprogramming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metaprogramming
{
class Metaprogramming
{
private static Dictionary<String, Object> conditions = new Dictionary<String, Object>()
{
["length"] = new int[] {10, 200},
["contains"] = "Mich",
["starts"] = "V",
["ends"] = "ov",
["not"] = new Dictionary<String, Object>()
{
["length"] = new int[] {50,65},
["contains"] = "Abu",
["starts"] = "Lev",
["ends"] = "iov"
}

};


private static Dictionary<String, Func<string, Object, Boolean>> operations = new Dictionary<string, Func<string, object, bool>>()
{
["length"] = (s, v) => s.Length >= ((int[])v)[0] && s.Length <= ((int[])v)[1],
["contains"] = (s, v) => s.Contains((String)v),
["starts"] = (s, v) => s.IndexOf((String)v) == 0,
["ends"] = (s, v) => s.EndsWith((String)v),
["not"] = (s,v) => !(check(s,(Dictionary<String, Object>) v))
};

private static Boolean check(String s, Dictionary<String, Object> conditions)
{
Boolean valid;
Copy link

@Raixur Raixur Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use type keyword bool


foreach(var condition in conditions)
{
valid = operations[condition.Key].Invoke(s, condition.Value);
if (!valid) return false;
}

return true;
}

public List<String> filter(List<String> names)
{
List<String> result = new List<string>();
foreach(String name in names)
{
if(check(name, conditions))
{
result.Add(name);
}
}
return result;
}
}
}
40 changes: 40 additions & 0 deletions Metaprogramming/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metaprogramming
{
class Program
{
private static String[] names =
{
"Marcus Aurelius Antoninus Augustus",
"Darth Vader",
"Victor Michailovich Glushkov",
"Victor Michailovich Glushkov1",
"Gottfried Wilhelm von Leibniz",
"Mao Zedong",
"Vladimir Sergeevich Soloviov",
"Ibn Arabi",
"Lev Nikolayevich Tolstoy",
"Muammar Muhammad Abu Minyar al-Gaddafi",
"Rene Descartes",
"Fyodor Mikhailovich Dostoyevsky",
"Benedito de Espinosa"
};
static void Main(string[] args)
{
new FilterSimple().filter(names).ForEach(delegate (String name)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use lambdas instead of delegate (just a recommendation).

{
Console.WriteLine(name);
});
new Metaprogramming().filter(names.ToList<String>()).ForEach(delegate (String name)
{
Console.WriteLine(name);
});
Console.ReadKey();
}
}
}