Skip to content

Instantly share code, notes, and snippets.

@SteveBate
SteveBate / async_database.cs
Created March 30, 2015 13:22
Example of connecting to Sql Server using async, await and tasks
async void Main()
{
var cnn = new SqlConnection("Data Source=ERPDBUAT;Initial Catalog=Enterprise_Misc; Integrated Security=true");
cnn.Open();
var cmd = new SqlCommand("SELECT * FROM ShopService.DivisionRules WHERE DivCode = 'MHL001'", cnn);
await cmd.ExecuteReaderAsync().ContinueWith(async rdr => {
await rdr.Result.ReadAsync().ContinueWith(ok => {
rdr.Result.GetBoolean(rdr.Result.GetOrdinal("IsMobileEnabled")).Dump();
rdr.Result.GetBoolean(rdr.Result.GetOrdinal("IsWebEnabled")).Dump();
@SteveBate
SteveBate / Task.scala
Last active August 29, 2015 14:23
Implementation of my C# Task (pipe and filters) in Scala
import scala.collection.mutable.ListBuffer
object HelloWorld {
def Start() {
println("OnStart")
}
def Complete() {
println("OnComplete")
@SteveBate
SteveBate / actors.scala
Created June 29, 2015 14:07
Example of two Akka actors interacting in Scala
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
case class RequestMessage(req: String)
case class ResponseMessage(resp: String)
class ClientActor extends Actor {
def receive = {
case ResponseMessage(resp: String) => println(resp)
@SteveBate
SteveBate / linq.scala
Last active March 19, 2025 04:53
Scala LINQ equivalents
// http://stackoverflow.com/questions/8104846/chart-of-ienumerable-linq-equivalents-in-scala
xs.Aggregate(accumFunc) -> xs.reduceLeft(accumFunc)
xs.Aggregate(seed, accumFunc) -> xs.foldLeft(seed)(accumFunc)
xs.Aggregate(seed, accumFunc, trans) -> trans(xs.foldLeft(seed)(accumFunc))
xs.All(pred) -> xs.forall(pred)
xs.Any() -> xs.nonEmpty
xs.Any(pred) -> xs.exists(pred)
xs.AsEnumerable() -> xs.asTraversable // roughly
xs.Average() -> xs.sum / xs.length
@SteveBate
SteveBate / implicitsandoptions.scala
Last active August 29, 2015 14:23
A simple example touching on Implicits and Options much like C# extensions and F# Options
object Main {
implicit class Converter(val s: String) {
def MaybeInt: Option[Int] = {
try {
Some(s.toInt)
}
catch {
@SteveBate
SteveBate / readfile.scala
Last active August 29, 2015 14:24
quick demo of reading csv data and mapping to a value object before printing out a property on each object
import scala.io.Source
// value object - automatically implements equals and gethashcode
case class Line(lineNo: Int, partNo: String, title: String, description: String)
object Test {
// converts string array into Line value object
def toLine(args: Array[String]): Line = {
Line(args(0).toInt, args(1), args(2), args(3))
@SteveBate
SteveBate / budget.ml
Last active August 29, 2015 14:27
A trivial sample exploring the envelope budgeting method in OCaml
open Printf
type envelope = {
name: string;
amount: int;
}
type account = {
name: string;
balance: int;
@SteveBate
SteveBate / facebooktest.go
Created September 15, 2015 14:25
One possible implementation of a facebook interview question that requires the developer to take a list of users and attempt to union those records that appear to represent the same user. In the sample data, user rows 0, 2, and 3 are one user and row 1 is another therefore: Given an input of these four rows, the output should consist of only 2 rows
package main
import (
"fmt"
)
type contact struct {
name string
phone string
email string
@SteveBate
SteveBate / zipped_webrequest.go
Last active September 20, 2015 07:30
Golang example showing how to use interfaces (Writer) to compose functionality, in this case, retrieve data from a url, encode response to a base64 string, compress it, and then save to a file. Also includes sample aspects for logging and timing
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
@SteveBate
SteveBate / quicksort.cs
Created September 22, 2015 10:32
Quicksort algorithm implementations in Go and C#
void Main()
{
var numbers = new int[]{11, 3, 9, 15, 7, 6, 2, 1, 5, 23, 18, 4};
Quicksort(numbers, 0, numbers.Length-1);
foreach(var n in numbers){
Console.WriteLine(n);
}
}
public void Quicksort(int[] elements, int left, int right)