Skip to content

Instantly share code, notes, and snippets.

@dlhartveld
dlhartveld / TAP-to-IObservable.cs
Last active December 13, 2015 18:39
A Rx.NET hands-on-lab ex. 7 variant that uses TAP This version allows the user to type in a prefix string, for which the results will be looked up at the dictionary service. The results will be printed on the console.
static DictServiceSoapClient client = new DictServiceSoapClient("DictServiceSoapClient");
static TextBox txt = new TextBox();
static Form form = new Form() { Controls = { txt } };
static void Work()
{
var input = Observable.FromEventPattern(txt, "TextChanged")
.Throttle(TimeSpan.FromMilliseconds(200))
.Select(x => ((TextBox)x.Sender).Text)
@dlhartveld
dlhartveld / SimpleRxDemo.cs
Last active December 13, 2015 18:49
Simple example of an observable sequence of integers.
var xs = Observable.Range(1, 10)
.Select(x => x + 1)
.Where(x => x % 3 == 0);
xs.ObserveOn(Scheduler.Default).Subscribe(
el => Console.WriteLine("Got element: {0}", el),
ex => Console.WriteLine("Error: {0}", ex.Message),
() => Console.WriteLine("Completed.")
);
@dlhartveld
dlhartveld / DelegatesAndLambdaExpressions.cs
Last active December 13, 2015 19:48
An example of C# delegates and lambda expressions and their use.
class Delegate
{
delegate int BinOp(int x, int y);
BinOp Plus()
{
return (x, y) => x + y;
}
int Calculate(int x, int y)
@dlhartveld
dlhartveld / LinqToObjectsExample.cs
Created February 16, 2013 01:41
A simple example of a LINQ query over a collection of objects (strings, in this case). helloLength1 and helloLenght2 are equivalent - the compiler just desugars the LINQ expression to the same AST that the second expression results in.
List<string> ss = new List<string>();
ss.Add("Hello");
ss.Add("World");
var helloLength1 = from s in ss
where s.StartsWith("H")
select s.Length;
var helloLength2 = ss.Where(s => s.StartsWith("H")).Select(s => s.Length);
@dlhartveld
dlhartveld / ExtensionMethods.cs
Last active December 13, 2015 20:58
Demonstration of extension methods. Note the static class PersonExtensions, the static method NameLenght, and the this keyword in front of the Person parameter.
class Person
{
private string name;
public Person(string name)
{
this.name = name;
}
public string Name { get { return this.name; } }
@dlhartveld
dlhartveld / Person.java
Last active December 14, 2015 06:08
An example of JDK 8 default methods.
public interface Person {
public abstract String getFirstName();
public abstract String getLastName();
public default String getDisplayableFullName() {
return getLastName() + ", " + getFirstName();
}
public abstract boolean isStudent();
@dlhartveld
dlhartveld / Collection.java
Created February 27, 2013 16:25
Snippet from the JDK8 Collection interface demonstrating the addition of a default stream() method.
public interface Collection<E> extends Iterable<E> {
// Traditional, abstract methods:
int size();
boolean contains(Object o);
boolean add(E e);
boolean remove(Object o);
// Etc...
@dlhartveld
dlhartveld / FunctionalInterfaces.java
Last active December 14, 2015 08:18
An example of JDK8 functional interfaces.
// Existing interfaces that are now considered functional interfaces:
interface java.lang.Runnable {
void run();
}
interface java.util.concurrent.Executor {
void execute(java.lang.Runnable command);
}
@dlhartveld
dlhartveld / LambdaConcat.java
Last active July 17, 2022 18:53
Example Java 8 lambda expression: concatenation of two strings. The first version explicitly defines types and a method body. The second version omits these. It uses implicit typing for its parameters, and lacks the return statement.
java.util.function.BiFunction<String, String, String> concat1
= (String s, String t) -> {
return s + t;
};
java.util.function.BiFunction<String, String, String> concat2
= (s, t) -> s + t;
@dlhartveld
dlhartveld / LambdaIterable.java
Last active December 14, 2015 08:19
JDK8 example: implementing an empty Iterable<Object> (functional interface with default method) with a lambda expression.
interface java.lang.Iterable<T> {
abstract Iterator<T> iterator();
default void forEach(Consumer<? super T> consumer) {
for (T t : this) {
consumer.accept(t);
}
}
}