Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View richlander's full-sized avatar

Rich Lander richlander

View GitHub Profile
@richlander
richlander / list.cs
Last active August 29, 2015 14:23
Code sample with documented output
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Demonstrates common uses of List<T>.
// Doc: https://msdn.microsoft.com/library/6sh2ey19.aspx
@richlander
richlander / ComputeSumsWithSIMD.cs
Created July 20, 2015 05:16
Compute the sums of the values in two arrays of integers, with Vector<T>
// Task: Compute the sums of the values in two arrays of integers, A and B.
// Traditional approach:
for (int i = 0; i < size; i++)
{
C[i] = A[i] + B[i];
}
// With Vector<int> you can instead do this:
@richlander
richlander / constructors.fs
Created July 20, 2015 17:23
Constructors as first-class function values
// old approach
let makeStrings chars lens =
List.zip chars lens
|> List.map (fun (char, len) -> new System.String(char, len))
// new approach
let makeStrings' chars lens =
List.zip chars lens
|> List.map System.String // use ctor as function
@richlander
richlander / simplified-mutable-values.fs
Created July 20, 2015 17:24
Simplified mutable values
// old approach - need a `ref` value
let sumSquares n =
let total = ref 0
{ 1 .. n } |> Seq.iter (fun i ->
total := !total + i*i
)
!total
// new approach - `mutable` just works
let sumSquares' n =
@richlander
richlander / implicit-quotation-method-arguments.fs
Created July 20, 2015 17:25
Implicit quotation of method arguments
type Test =
static member EchoExpression([<ReflectedDefinition(true)>] x : Expr<_>) =
let expression, value = (* decompose AST of x *)
printfn "%s evaluates to %O" expression value
let x, y, z = 42, 89.0, 92.5
Test.EchoExpression(x) // "x evaluates to 42"
Test.EchExpression(Math.Max(y, z)) // "Math.Max(y, z) evaluates to 92.5"
@richlander
richlander / vs2015-enc-csharp.cs
Created July 20, 2015 17:39
Example of supported C# syntax for EnC in Visual Studio 2015
public MainWindow()
{
InitializeComponent();
Loaded += async (o, e) => {
await Task.Delay(1000);
_strings = new string[] { "EnC now supports more features!", "Thanks to Roslyn!"};
label.Content = (from str in _strings
where str.StartsWith("Enc")
select str).FirstOrDefault();
@richlander
richlander / csharp-static-imports.cs
Created July 20, 2015 17:42
C# 6 Static Imports
using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
static void Main()
{
WriteLine(Sqrt(3 * 3 + 4 * 4));
WriteLine(Friday - Monday);
@richlander
richlander / vb-static-imports.vb
Created July 20, 2015 17:43
VB Static Imports
Imports System.Console
Imports System.Math
Imports System.DateOfWeek
Module Program
Sub Main()
WriteLine(Sqrt(3 * 3 + 4 * 4))
WriteLine(Friday - Monday)
End Sub
End Module
@richlander
richlander / multi-line-string-literals.vb
Created July 20, 2015 17:44
VB Multiline string literals
Dim s = "Write your haiku with
No vbCrLf
In VB14"
@richlander
richlander / csharp-multiline-string-literals.cs
Created July 20, 2015 17:47
C# Multiline String Literals
var s = @"Write your haiku with
No \r\n escapes
In C# v1";