Skip to content

Instantly share code, notes, and snippets.

@JoeSimmonds
Last active February 22, 2017 09:16
Show Gist options
  • Select an option

  • Save JoeSimmonds/ab26cfd3243c409b2e7523a71a4cbed1 to your computer and use it in GitHub Desktop.

Select an option

Save JoeSimmonds/ab26cfd3243c409b2e7523a71a4cbed1 to your computer and use it in GitHub Desktop.
using System;
namespace ConsoleApplication
{
delegate int Op (int a);
delegate int TwoOp (int a, int b);
delegate Op CurriedOp (int a);
class OpCurryer {
private TwoOp f;
private int a;
public OpCurryer(TwoOp f) {
this.f = f;
}
private int op2(int b) {
return f(a, b);
}
private Op op1(int a) {
this.a = a;
return new Op(op2);
}
public CurriedOp GetCurriedOp() {
return new CurriedOp(op1);
}
}
class Composer {
private Op op1;
private Op op2;
public Composer(Op op1, Op op2) {
this.op1 = op1;
this.op2 = op2;
}
private int Composed(int a) {
int x = op1(a);
int y = op2(x);
return y;
}
public Op GetComposedOp() {
return new Op(Composed);
}
}
public class Program
{
private static int DoubleUp(int a) {
Console.WriteLine("Doubling Up...");
return a + a;
}
private static int Square(int a) {
Console.WriteLine("Squaring ...");
return a * a;
}
private static int Addition(int a, int b) {
Console.WriteLine("Adding ...");
return a + b;
}
private static Op Compose(Op f1, Op f2) {
Composer co = new Composer(f1, f2);
return co.GetComposedOp();
}
private static int Compose(Op f1, Op f2, int input) {
int x = f1(input);
int y = f2(x);
return y;
}
private static CurriedOp Curry(TwoOp f) {
OpCurryer oc = new OpCurryer(f);
return oc.GetCurriedOp();
}
public static void Main(string[] args)
{
Op op1 = new Op(DoubleUp);
Op op2 = new Op(Square);
Console.WriteLine("first class");
Console.WriteLine(op1(45));
Console.WriteLine("composition -- sort of");
Op composed = new Op(DoubleUp);
composed += new Op(Square);
Console.WriteLine(composed(5));
Console.WriteLine("composition -- No, not really");
Console.WriteLine(Compose(op1, op2, 5));
Console.WriteLine("Better composition");
Console.WriteLine(Compose(op1, op2)(5));
Console.WriteLine("Currying");
TwoOp add = new TwoOp(Addition);
Console.WriteLine(Curry(add)(5)(12));
Console.WriteLine("Partial application");
TwoOp addAgain = new TwoOp(Addition);
Op addFive = Curry(add)(5);
Console.WriteLine(addFive(12));
Console.WriteLine(addFive(1084));
}
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
delegate int Op (int a);
public class Program
{
private static int DoubleUp(int a) { return a + a; }
private static IEnumerable<int> GetRange(int uBound) {
for (int cur = 1; cur <= uBound; cur++) {
yield return cur;
}
}
private static IEnumerable<int> InfiniteList(int uBound) {
int cur = 1;
while (true)
{
yield return cur;
cur++;
}
}
public static void Main(string[] args)
{
Console.WriteLine("Method group conversion");
Op op1 = DoubleUp;
Console.WriteLine(op1(45));
Console.WriteLine("Anonymous methods");
int b = 23;
Op op2 = delegate(int a) { return a + b;};
Console.WriteLine("Watch out for mutability in the closure");
Console.WriteLine(op2(45));
b = 5;
Console.WriteLine(op2(45));
Console.WriteLine("Iterators");
foreach (int number in GetRange(12))
{
Console.WriteLine(number);
}
Console.WriteLine("Lazy Evaluation");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
delegate int Op(int a);
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Lambdas");
Op op1 = a => {return a + a;};
Console.WriteLine(op1(7));
Op op2 = a => a + a;
Console.WriteLine(op2(12));
Console.WriteLine("Query Expressions");
var source = new List<int> {12, 13, 2, 15, 27, 20, 100, 34, 17, 18};
var newList = from a in source
where a%2 == 0
orderby a ascending
select a * -1;
foreach (int x in newList) Console.WriteLine(x);
Console.WriteLine("Method chaining syntax");
var anotherList = source.Where(a => a%2==0)
.OrderBy(a => a)
.Select(a => a * -1);
foreach (int x in anotherList) Console.WriteLine(x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment