using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class WordCountProgram
{
static void Main(String[] args)
{
var operatorSet = new Tuple<String, Func<String, String>>[]
{ Tuple.Create<String, Func<String, String>>("w", i => String.Join(" ",i.Split(Estring.seperators, StringSplitOptions.RemoveEmptyEntries)) )
, Tuple.Create<String, Func<String, String>>("r", i => i.Reverse() )
, Tuple.Create<String, Func<String, String>>("u", i => i.ToUpper() )
, Tuple.Create<String, Func<String, String>>("h", i => helpMessage )
};
operatorSet.filter(i=>IsGivenInArgs(i.Item1,args)).foldl(GetStringFromArgs(args),(b,f) => f.Item2(b)).ShowScreen();
}
static bool IsGivenInArgs(string command,string[] options )
{
Regex regex = new Regex(@"^-(\w)*" + command + @"(\w)*");
return Array.Exists(options, (s) => regex.IsMatch(s));
}
static string GetStringFromArgs(string[] options)
{
Regex regex = new Regex(@"^-(\w)*");
return Array.Find(options, (s) => !regex.IsMatch(s));
}
static string helpMessage = "用法: DealSentenct [-w] [-r] [-u] <\"a sentence\">\n\n"
+ "其中 [] 表示可选参数项,句子串以双引号引出\n"
+ "-w 分解出单词并分行输出\n"
+ "-r 将原句子字串反向输出(若同时又-w选项,按单词分行反向输出)\n"
+ "-u 处理结果以大写字母输出\n"
+ "-h 输出此帮助信息\n";
}
static class Estring
{
static public String Reverse(this String s)
{
Char[] t = s.ToCharArray();
Array.Reverse(t);
return new String(t);
}
// print the object to console
static public Boolean ShowScreen<T>(this T m)
{
try { Console.WriteLine(m.ToString());}
catch { return false; }
return true;
}
static public Char[] seperators = { ' ', '\n', '\t', '.', '\"', ';', ',', '!', '?', '(', ')', '<', '>', '[', ']' };
}
/* ------------------------------------------------------------------------------------
* The Generic Extention to T[]
* Making the original function side-effect-less(Reference Tranparent)
* ------------------------------------------------------------------------------------
*/
static class XList
{
//take head of the array or throw an error
static public T head<T>(this T[] x)
{
return x[0];
}
//take all elements of the array except its head
static public T[] tails<T> (this T[] x)
{
int n = x.Length;
if (n == 0)
{
return x;
}
else
{
T[] y = new T[n-1];
for (int i = 0; i < (n-1); i++)
{
y[i] = x[i+1];
}
return y;
}
}
//let f acts on every element (Functor Map)
static public T2 [] map<T1, T2> (this T1[] x, Func<T1,T2> f)
{
if (x.Length == 0)
{
return new T2 [] {};
}
else
{
return f(x.head()).con(x.tails().map(f));
}
}
//the elements become the head of an array
static public T[] con<T>(this T a, T[] x)
{
var y = new T[x.Length + 1];
x.CopyTo(y, 1);
y[0] = a;
return y;
}
//applied to a predicate and a list, returns the list of those elements that satisfy the predicate;
static public T[] filter<T>(this T[] xs, Func<T,Boolean> p)
{
if (xs.Length == 0)
{
return new T[] {};
}
else
{
if (p(xs.head()))
{
return xs.head().con(xs.tails().filter(p));
}
else
{
return xs.tails().filter(p);
}
}
}
//applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:
static public T2 foldl<T1,T2>(this T1[] xs,T2 b, Func<T2,T1,T2> f)
{
if (xs.Length == 0)
{
return b;
}
else
{
return xs.tails().foldl(f(b,xs.head()), f);
}
}
}
}
网友评论